Rev 2071 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2071 | Rev 2505 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2006 Josef Cejka |
2 | * Copyright (c) 2006 Josef Cejka |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup genarch |
29 | /** @addtogroup genarch |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | /** @file |
32 | /** @file |
33 | */ |
33 | */ |
34 | 34 | ||
35 | #include <genarch/softint/division.h> |
35 | #include <genarch/softint/division.h> |
36 | 36 | ||
37 | #define ABSVAL(x) ( (x) > 0 ? (x) : -(x)) |
37 | #define ABSVAL(x) ((x) > 0 ? (x) : -(x)) |
38 | #define SGN(x) ( (x) >= 0 ? 1 : 0 ) |
38 | #define SGN(x) ((x) >= 0 ? 1 : 0) |
39 | 39 | ||
40 | static unsigned int divandmod32(unsigned int a, unsigned int b, unsigned int *remainder) |
40 | static unsigned int divandmod32(unsigned int a, unsigned int b, |
- | 41 | unsigned int *remainder) |
|
41 | { |
42 | { |
42 | unsigned int result; |
43 | unsigned int result; |
43 | int steps = sizeof(unsigned int) * 8; |
44 | int steps = sizeof(unsigned int) * 8; |
44 | 45 | ||
45 | *remainder = 0; |
46 | *remainder = 0; |
46 | result = 0; |
47 | result = 0; |
47 | 48 | ||
48 | if (b == 0) { |
49 | if (b == 0) { |
49 | /* FIXME: division by zero */ |
50 | /* FIXME: division by zero */ |
50 | return 0; |
51 | return 0; |
51 | } |
52 | } |
52 | 53 | ||
53 | if ( a < b) { |
54 | if (a < b) { |
54 | *remainder = a; |
55 | *remainder = a; |
55 | return 0; |
56 | return 0; |
56 | } |
57 | } |
57 | 58 | ||
58 | for ( ; steps > 0; steps--) { |
59 | for (; steps > 0; steps--) { |
59 | /* shift one bit to remainder */ |
60 | /* shift one bit to remainder */ |
60 | *remainder = ( (*remainder) << 1) | (( a >> 31) & 0x1); |
61 | *remainder = ((*remainder) << 1) | (( a >> 31) & 0x1); |
61 | result <<= 1; |
62 | result <<= 1; |
62 | 63 | ||
63 | if (*remainder >= b) { |
64 | if (*remainder >= b) { |
64 | *remainder -= b; |
65 | *remainder -= b; |
65 | result |= 0x1; |
66 | result |= 0x1; |
66 | } |
67 | } |
67 | a <<= 1; |
68 | a <<= 1; |
68 | } |
69 | } |
69 | 70 | ||
70 | return result; |
71 | return result; |
71 | } |
72 | } |
72 | 73 | ||
73 | 74 | ||
74 | static unsigned long long divandmod64(unsigned long long a, unsigned long long b, unsigned long long *remainder) |
75 | static unsigned long long divandmod64(unsigned long long a, |
- | 76 | unsigned long long b, unsigned long long *remainder) |
|
75 | { |
77 | { |
76 | unsigned long long result; |
78 | unsigned long long result; |
77 | int steps = sizeof(unsigned long long) * 8; |
79 | int steps = sizeof(unsigned long long) * 8; |
78 | 80 | ||
79 | *remainder = 0; |
81 | *remainder = 0; |
80 | result = 0; |
82 | result = 0; |
81 | 83 | ||
82 | if (b == 0) { |
84 | if (b == 0) { |
83 | /* FIXME: division by zero */ |
85 | /* FIXME: division by zero */ |
84 | return 0; |
86 | return 0; |
85 | } |
87 | } |
86 | 88 | ||
87 | if ( a < b) { |
89 | if (a < b) { |
88 | *remainder = a; |
90 | *remainder = a; |
89 | return 0; |
91 | return 0; |
90 | } |
92 | } |
91 | 93 | ||
92 | for ( ; steps > 0; steps--) { |
94 | for (; steps > 0; steps--) { |
93 | /* shift one bit to remainder */ |
95 | /* shift one bit to remainder */ |
94 | *remainder = ( (*remainder) << 1) | ((a >> 63) & 0x1); |
96 | *remainder = ((*remainder) << 1) | ((a >> 63) & 0x1); |
95 | result <<= 1; |
97 | result <<= 1; |
96 | 98 | ||
97 | if (*remainder >= b) { |
99 | if (*remainder >= b) { |
98 | *remainder -= b; |
100 | *remainder -= b; |
99 | result |= 0x1; |
101 | result |= 0x1; |
100 | } |
102 | } |
101 | a <<= 1; |
103 | a <<= 1; |
102 | } |
104 | } |
103 | 105 | ||
104 | return result; |
106 | return result; |
105 | } |
107 | } |
106 | 108 | ||
107 | /* 32bit integer division */ |
109 | /* 32bit integer division */ |
108 | int __divsi3(int a, int b) |
110 | int __divsi3(int a, int b) |
109 | { |
111 | { |
110 | unsigned int rem; |
112 | unsigned int rem; |
111 | int result; |
113 | int result; |
112 | 114 | ||
113 | result = (int)divandmod32(ABSVAL(a), ABSVAL(b), &rem); |
115 | result = (int) divandmod32(ABSVAL(a), ABSVAL(b), &rem); |
114 | 116 | ||
115 | if ( SGN(a) == SGN(b)) return result; |
117 | if (SGN(a) == SGN(b)) |
- | 118 | return result; |
|
116 | return -result; |
119 | return -result; |
117 | } |
120 | } |
118 | 121 | ||
119 | /* 64bit integer division */ |
122 | /* 64bit integer division */ |
120 | long long __divdi3(long long a, long long b) |
123 | long long __divdi3(long long a, long long b) |
121 | { |
124 | { |
122 | unsigned long long rem; |
125 | unsigned long long rem; |
123 | long long result; |
126 | long long result; |
124 | 127 | ||
125 | result = (long long)divandmod64(ABSVAL(a), ABSVAL(b), &rem); |
128 | result = (long long) divandmod64(ABSVAL(a), ABSVAL(b), &rem); |
126 | 129 | ||
127 | if ( SGN(a) == SGN(b)) return result; |
130 | if (SGN(a) == SGN(b)) |
- | 131 | return result; |
|
128 | return -result; |
132 | return -result; |
129 | } |
133 | } |
130 | 134 | ||
131 | /* 32bit unsigned integer division */ |
135 | /* 32bit unsigned integer division */ |
132 | unsigned int __udivsi3(unsigned int a, unsigned int b) |
136 | unsigned int __udivsi3(unsigned int a, unsigned int b) |
133 | { |
137 | { |
134 | unsigned int rem; |
138 | unsigned int rem; |
135 | return divandmod32(a, b, &rem); |
139 | return divandmod32(a, b, &rem); |
136 | } |
140 | } |
137 | 141 | ||
138 | /* 64bit unsigned integer division */ |
142 | /* 64bit unsigned integer division */ |
139 | unsigned long long __udivdi3(unsigned long long a, unsigned long long b) |
143 | unsigned long long __udivdi3(unsigned long long a, unsigned long long b) |
140 | { |
144 | { |
141 | unsigned long long rem; |
145 | unsigned long long rem; |
142 | return divandmod64(a, b, &rem); |
146 | return divandmod64(a, b, &rem); |
143 | } |
147 | } |
144 | 148 | ||
145 | /* 32bit remainder of the signed division */ |
149 | /* 32bit remainder of the signed division */ |
146 | int __modsi3(int a, int b) |
150 | int __modsi3(int a, int b) |
147 | { |
151 | { |
148 | unsigned int rem; |
152 | unsigned int rem; |
149 | divandmod32(a, b, &rem); |
153 | divandmod32(a, b, &rem); |
150 | 154 | ||
151 | /* if divident is negative, remainder must be too */ |
155 | /* if divident is negative, remainder must be too */ |
152 | if (!(SGN(a))) { |
156 | if (!(SGN(a))) { |
153 | return -((int)rem); |
157 | return -((int) rem); |
154 | } |
158 | } |
155 | 159 | ||
156 | return (int)rem; |
160 | return (int) rem; |
157 | } |
161 | } |
158 | 162 | ||
159 | /* 64bit remainder of the signed division */ |
163 | /* 64bit remainder of the signed division */ |
160 | long long __moddi3(long long a,long long b) |
164 | long long __moddi3(long long a,long long b) |
161 | { |
165 | { |
162 | unsigned long long rem; |
166 | unsigned long long rem; |
163 | divandmod64(a, b, &rem); |
167 | divandmod64(a, b, &rem); |
164 | 168 | ||
165 | /* if divident is negative, remainder must be too */ |
169 | /* if divident is negative, remainder must be too */ |
166 | if (!(SGN(a))) { |
170 | if (!(SGN(a))) { |
167 | return -((long long)rem); |
171 | return -((long long) rem); |
168 | } |
172 | } |
169 | 173 | ||
170 | return (long long)rem; |
174 | return (long long) rem; |
171 | } |
175 | } |
172 | 176 | ||
173 | /* 32bit remainder of the unsigned division */ |
177 | /* 32bit remainder of the unsigned division */ |
174 | unsigned int __umodsi3(unsigned int a, unsigned int b) |
178 | unsigned int __umodsi3(unsigned int a, unsigned int b) |
175 | { |
179 | { |
176 | unsigned int rem; |
180 | unsigned int rem; |
177 | divandmod32(a, b, &rem); |
181 | divandmod32(a, b, &rem); |
178 | return rem; |
182 | return rem; |
179 | } |
183 | } |
180 | 184 | ||
181 | /* 64bit remainder of the unsigned division */ |
185 | /* 64bit remainder of the unsigned division */ |
182 | unsigned long long __umoddi3(unsigned long long a, unsigned long long b) |
186 | unsigned long long __umoddi3(unsigned long long a, unsigned long long b) |
183 | { |
187 | { |
184 | unsigned long long rem; |
188 | unsigned long long rem; |
185 | divandmod64(a, b, &rem); |
189 | divandmod64(a, b, &rem); |
186 | return rem; |
190 | return rem; |
187 | } |
191 | } |
188 | 192 | ||
189 | unsigned long long __udivmoddi3(unsigned long long a, unsigned long long b, unsigned long long *c) |
193 | unsigned long long __udivmoddi3(unsigned long long a, unsigned long long b, |
- | 194 | unsigned long long *c) |
|
190 | { |
195 | { |
191 | return divandmod64(a, b, c); |
196 | return divandmod64(a, b, c); |
192 | } |
197 | } |
193 | 198 | ||
194 | /** @} |
199 | /** @} |
195 | */ |
200 | */ |
196 | 201 |