source: trunk/Softwares/Basic_test.or32/src/c/func_math.c @ 2

Last change on this file since 2 was 2, checked in by kane, 17 years ago

Import Morpheo

File size: 1.2 KB
Line 
1#include "func_math.h"
2
3//-----[ Multiplication ]--------------------------------------------------
4
5// Multiplication rapide
6unsigned int mul_soft(unsigned int op1,
7                      unsigned int op2)
8{
9  unsigned int num_bit,carry;
10  unsigned int op2_aux = op2;
11  unsigned int res     = 0;
12 
13  for(num_bit=0;num_bit<32; num_bit++)
14    {
15      if (op2_aux == 0)
16        break;
17
18      carry   = op2_aux & 1;
19      op2_aux = op2_aux >> 1;
20     
21      if (carry!=0)
22        res += (op1 << num_bit);
23    }
24  return res;
25}
26
27unsigned int mul_hard(unsigned int op1,
28                      unsigned int op2)
29{
30  return op1*op2;
31}
32
33//-----[ Division ]--------------------------------------------------------
34
35unsigned int div_soft( unsigned int op1,
36                       unsigned int op2)
37{
38  unsigned res;
39 
40  if (op2 == 0)
41    return 0;
42 
43  for (res = 0; op1 >= op2; res ++)
44    op1 -= op2;
45 
46  return res;
47}
48
49unsigned int div_hard( unsigned int op1,
50                       unsigned int op2)
51{
52//return (op1/op2);
53  return div_soft(op1,op2);
54}
55
56//-----[ Modulo ]----------------------------------------------------------
57
58unsigned int modulo( unsigned int x,
59                     unsigned int base)
60{
61  if (base==0)
62    return 0;
63 
64  while(x >= base)
65    x -= base;
66 
67  return x;
68}
Note: See TracBrowser for help on using the repository browser.