1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include "math.h"
22
23USItype udivmodsi4 (USItype num, USItype den, word_type modwanted)
24{
25 USItype bit = 1;
26 USItype res = 0;
27
28 while (den < num && bit && !(den & (1L << 31))) {
29 den <<= 1;
30 bit <<= 1;
31 }
32 while (bit) {
33 if (num >= den) {
34 num -= den;
35 res |= bit;
36 }
37 bit >>= 1;
38 den >>= 1;
39 }
40 if (modwanted)
41 return num;
42 return res;
43}
44
45
46SItype __divsi3 (SItype a, SItype b)
47{
48 word_type neg = 0;
49 SItype res;
50
51 if (a < 0) {
52 a = -a;
53 neg = !neg;
54 }
55
56 if (b < 0) {
57 b = -b;
58 neg = !neg;
59 }
60
61 res = udivmodsi4 (a, b, 0);
62
63 if (neg)
64 res = -res;
65
66 return res;
67}
68
69
70SItype __modsi3 (SItype a, SItype b)
71{
72 word_type neg = 0;
73 SItype res;
74
75 if (a < 0) {
76 a = -a;
77 neg = 1;
78 }
79
80 if (b < 0)
81 b = -b;
82
83 res = udivmodsi4 (a, b, 1);
84
85 if (neg)
86 res = -res;
87
88 return res;
89}
90
91
92SItype __udivsi3 (SItype a, SItype b)
93{
94 return udivmodsi4 (a, b, 0);
95}
96
97
98SItype __umodsi3 (SItype a, SItype b)
99{
100 return udivmodsi4 (a, b, 1);
101}
102