1 | #include "func_math.h" |
---|
2 | #include "func_io.h" |
---|
3 | |
---|
4 | //-----[ Multiplication ]-------------------------------------------------- |
---|
5 | |
---|
6 | // Multiplication rapide |
---|
7 | unsigned int mul_soft(unsigned int op1, |
---|
8 | unsigned int op2) |
---|
9 | { |
---|
10 | unsigned int size = 32; |
---|
11 | unsigned int num_bit,carry; |
---|
12 | unsigned int op2_aux = op2; |
---|
13 | unsigned int res = 0; |
---|
14 | |
---|
15 | for(num_bit=0;num_bit<size; num_bit++) |
---|
16 | { |
---|
17 | if (op2_aux == 0) |
---|
18 | break; |
---|
19 | |
---|
20 | carry = op2_aux & 1; |
---|
21 | op2_aux = op2_aux >> 1; |
---|
22 | |
---|
23 | if (carry!=0) |
---|
24 | res += (op1 << num_bit); |
---|
25 | } |
---|
26 | return res; |
---|
27 | } |
---|
28 | |
---|
29 | unsigned int mul_hard(unsigned int op1, |
---|
30 | unsigned int op2) |
---|
31 | { |
---|
32 | return op1*op2; |
---|
33 | } |
---|
34 | |
---|
35 | //-----[ Division ]-------------------------------------------------------- |
---|
36 | |
---|
37 | unsigned int div_soft( unsigned int op1, |
---|
38 | unsigned int op2) |
---|
39 | { |
---|
40 | unsigned res; |
---|
41 | |
---|
42 | if (op2 == 0) |
---|
43 | return 0; |
---|
44 | |
---|
45 | for (res = 0; op1 >= op2; res ++) |
---|
46 | op1 -= op2; |
---|
47 | |
---|
48 | return res; |
---|
49 | } |
---|
50 | |
---|
51 | unsigned int div_hard( unsigned int op1, |
---|
52 | unsigned int op2) |
---|
53 | { |
---|
54 | //return (op1/op2); |
---|
55 | return div_soft(op1,op2); |
---|
56 | } |
---|
57 | |
---|
58 | //-----[ Modulo ]---------------------------------------------------------- |
---|
59 | |
---|
60 | unsigned int modulo( unsigned int x, |
---|
61 | unsigned int base) |
---|
62 | { |
---|
63 | if (base==0) |
---|
64 | return 0; |
---|
65 | |
---|
66 | while(x >= base) |
---|
67 | x -= base; |
---|
68 | |
---|
69 | return x; |
---|
70 | } |
---|
71 | |
---|
72 | //------------------------------------------------------------------------- |
---|
73 | //-----[ Test ]------------------------------------------------------------ |
---|
74 | //------------------------------------------------------------------------- |
---|
75 | |
---|
76 | void test_mul_soft (void) |
---|
77 | { |
---|
78 | if (mul_soft(0x00000000,0x00000000) != 0x00000000) quit( 1); |
---|
79 | if (mul_soft(0x12345678,0x00000001) != 0x12345678) quit( 2); |
---|
80 | if (mul_soft(0x00000025,0x000064ab) != 0x000e8cb7) quit( 3); |
---|
81 | if (mul_soft(0x0000083b,0x000007bd) != 0x003fb08f) quit( 4); // (0x00000000003fb08f) |
---|
82 | if (mul_soft(0x0000083b,0xfffff843) != 0xffc04f71) quit( 5); // (0xffffffffffc04f71) |
---|
83 | if (mul_soft(0xfffff7c5,0x000007bd) != 0xffc04f71) quit( 6); // (0xffffffffffc04f71) |
---|
84 | if (mul_soft(0xfffff7c5,0xfffff843) != 0x003fb08f) quit( 7); // (0x00000000003fb08f) |
---|
85 | if (mul_soft(0x017e9157,0x00d5ce37) != 0x18883bb1) quit( 8); // (0x00013f8318883bb1) |
---|
86 | if (mul_soft(0x017e9157,0xff2a31c9) != 0xe777c44f) quit( 9); // (0xfffec07ce777c44f) |
---|
87 | if (mul_soft(0xfe816ea9,0x00d5ce37) != 0xe777c44f) quit(10); // (0xfffec07ce777c44f) |
---|
88 | if (mul_soft(0xfe816ea9,0xff2a31c9) != 0x18883bb1) quit(11); // (0x00013f8318883bb1) |
---|
89 | } |
---|
90 | |
---|
91 | void test_div_soft (void) |
---|
92 | { |
---|
93 | /* if (div_soft(0x00000000,0x00000001) != 0x00000000) quit( 1); */ |
---|
94 | /* if (div_soft(0x21071981,0x00000001) != 0x21071981) quit( 2); */ |
---|
95 | /* if (div_soft(0xdeadbeef,0x00000001) != 0xdeadbeef) quit( 3); */ |
---|
96 | if (div_soft(4 ,2 ) != 2 ) quit( 3); |
---|
97 | if (div_soft(680 ,40 ) != 17 ) quit( 4); |
---|
98 | if (div_soft(15431115 ,9835 ) != 1569 ) quit( 5); |
---|
99 | } |
---|
100 | |
---|
101 | void test_modulo (void) |
---|
102 | { |
---|
103 | if (modulo(16834,563) != 507) quit(1); |
---|
104 | } |
---|
105 | |
---|