util.java

来自「Java Op Processor java vhdl processor」· Java 代码 · 共 188 行

JAVA
188
字号
package test;import util.Dbg;import com.jopdesign.sys.Const;import com.jopdesign.sys.Native;/***	div. utilites, test von mul/div/rem**	todo: div, mod mit 0x80000000 ist FALSCH!!!*/public class Util {	public static int mul(int a, int b) {		int c, i;		boolean neg = false;		if (a<0) {			neg = true;			a = -a;		}		if (b<0) {			neg = !neg;			b = -b;		}		c = 0;		for (i=0; i<32; ++i) {			c <<= 1;			if ((a & 0x80000000)!=0) c += b;			a <<= 1;		}		if (neg) c = -c;		return c;	}public static int add(int a, int b) { return a+b; }	public static int div(int a, int b) {		boolean neg = false;		if (a<0) {			neg = true;			a = -a;		}		if (b<0) {			neg = !neg;			b = -b;		}		int c = 0;		int r = 0;		for (int i=0; i<32; ++i) {			c <<= 1;			r <<= 1;			if ((a & 0x80000000)!=0) {				r |= 1;			}			a <<= 1;			if (r>=b) {				r -= b;				c |= 1;			}		}		if (neg) {			c = -c;		}		return c;	}	public static int rem(int a, int b) {		boolean neg = false;		if (a<0) {			neg = true;			a = -a;		}		if (b<0) {			b = -b;		}		int c = 0;		int r = 0;		for (int i=0; i<32; ++i) {			c <<= 1;			r <<= 1;			if ((a & 0x80000000)!=0) {				r |= 1;			}			a <<= 1;			if (r>=b) {				r -= b;				c |= 1;			}		}		if (neg) {			r = -r;		}		return r;	}	public static void main(String[] args) {		int t1, toff;/*		int a = -1;		int b = -1234;*/		int c;		Dbg.initSer();/*		t1 = Native.rd(Const.IO_CNT);		t1 = Native.rd(Const.IO_CNT) - t1;		toff = t1;		Dbg.intVal(toff);		t1 = Native.rd(Const.IO_CNT);		c = a*b;		t1 = Native.rd(Const.IO_CNT) - t1;		Dbg.intVal(t1-toff);*/		t1 = Native.rd(Const.IO_CNT);		c = mul(-1, -1234);		t1 = Native.rd(Const.IO_CNT) - t1;		Dbg.intVal(t1);		for (;;) ;	}/*	public static void test(int a, int b) {		System.out.println(a+"*"+b+"=\t"+mul(a, b)+" : "+(a*b));		if (b!=0) {			System.out.println(a+"/"+b+"=\t"+div(a, b)+" : "+(a/b));			System.out.println(a+"%"+b+"=\t"+rem(a, b)+" : "+(a%b));		}	}	public static void main(String[] args) {		test(0, 0);		test(0, 1);		test(1, 0);		test(3, 2);		test(-3, 2);		test(3, -2);		test(-3, -2);		test(10000, -3);		test(10000, -287652987);		test(123456789, -3);		test(123456789, 123456789);		test(-1, -1);		test(0x7fffffff, 2);		test(2, 0x7fffffff);		test(0x80000000, 3);		test(3, 0x80000000);		test(0x80000000, 0x80000000);		test(0x80000000, 0x7fffffff);		test(0x7fffffff, 0x80000000);		test(0x7fffffff, 0x7fffffff);		test(0x80000001, 0x80000001);		test(0x80000001, 0x7fffffff);		test(0x7fffffff, 0x80000001);		test(0x7fffffff, 0x7fffffff);//		for (int i=1; i!=0; ++i) {//			for (int j=1; j!=0; ++j) {//				if (i*j != mul(i, j)) {//					System.out.println("Fehler: "+i+" "+j);//					System.exit(-1);//				}//			}//			System.out.print(i+"\r");		}	}*/}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?