source: trunk/Softwares/Common/src/c/func_math.c @ 102

Last change on this file since 102 was 102, checked in by rosiere, 15 years ago

Previous commit with new files :P

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