Rev 810 | Rev 1196 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 810 | Rev 811 | ||
---|---|---|---|
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 | #include <softint/softint.h> |
29 | #include <genarch/softint/division.h> |
30 | 30 | ||
31 | #define ABSVAL(x) ( (x) > 0 ? (x) : -(x)) |
31 | #define ABSVAL(x) ( (x) > 0 ? (x) : -(x)) |
32 | #define SGN(x) ( (x) >= 0 ? 1 : 0 ) |
32 | #define SGN(x) ( (x) >= 0 ? 1 : 0 ) |
33 | 33 | ||
34 | static unsigned int divandmod32(unsigned int a, unsigned int b, unsigned int *remainder) |
34 | static unsigned int divandmod32(unsigned int a, unsigned int b, unsigned int *remainder) |
35 | { |
35 | { |
36 | unsigned int result; |
36 | unsigned int result; |
37 | int steps = sizeof(unsigned int); |
37 | int steps = sizeof(unsigned int) * 8; |
38 | 38 | ||
39 | *remainder = 0; |
39 | *remainder = 0; |
40 | result = 0; |
40 | result = 0; |
41 | 41 | ||
42 | if (b == 0) { |
42 | if (b == 0) { |
43 | /* FIXME: division by zero */ |
43 | /* FIXME: division by zero */ |
44 | return 0; |
44 | return 0; |
45 | } |
45 | } |
46 | 46 | ||
47 | if ( a < b) { |
47 | if ( a < b) { |
48 | *remainder = a; |
48 | *remainder = a; |
49 | return 0; |
49 | return 0; |
50 | } |
50 | } |
51 | 51 | ||
52 | for ( ; steps > 0; steps--) { |
52 | for ( ; steps > 0; steps--) { |
53 | /* shift one bit to remainder */ |
53 | /* shift one bit to remainder */ |
54 | *remainder = ( (*remainder) << 1) | (( divident >> 31) & 0x1); |
54 | *remainder = ( (*remainder) << 1) | (( a >> 31) & 0x1); |
55 | result <<= 1; |
55 | result <<= 1; |
56 | 56 | ||
57 | if (*remainder >= b) { |
57 | if (*remainder >= b) { |
58 | *remainder -= b; |
58 | *remainder -= b; |
59 | result |= 0x1; |
59 | result |= 0x1; |
60 | } |
60 | } |
61 | divident <<= 1; |
61 | a <<= 1; |
62 | } |
62 | } |
63 | 63 | ||
64 | return result; |
64 | return result; |
65 | } |
65 | } |
66 | 66 | ||
67 | 67 | ||
68 | static unsigned long divandmod64(unsigned long a, unsigned long b, unsigned long *remainder) |
68 | static unsigned long divandmod64(unsigned long a, unsigned long b, unsigned long *remainder) |
69 | { |
69 | { |
70 | unsigned long result; |
70 | unsigned long result; |
71 | int steps = sizeof(unsigned long); |
71 | int steps = sizeof(unsigned long) * 8; |
72 | 72 | ||
73 | *remainder = 0; |
73 | *remainder = 0; |
74 | result = 0; |
74 | result = 0; |
75 | 75 | ||
76 | if (b == 0) { |
76 | if (b == 0) { |
77 | /* FIXME: division by zero */ |
77 | /* FIXME: division by zero */ |
78 | return 0; |
78 | return 0; |
79 | } |
79 | } |
80 | 80 | ||
81 | if ( a < b) { |
81 | if ( a < b) { |
82 | *remainder = a; |
82 | *remainder = a; |
83 | return 0; |
83 | return 0; |
84 | } |
84 | } |
85 | 85 | ||
86 | for ( ; steps > 0; steps--) { |
86 | for ( ; steps > 0; steps--) { |
87 | /* shift one bit to remainder */ |
87 | /* shift one bit to remainder */ |
88 | *remainder = ( (*remainder) << 1) | (( divident >> 63) & 0x1); |
88 | *remainder = ( (*remainder) << 1) | ((a >> 63) & 0x1); |
89 | result <<= 1; |
89 | result <<= 1; |
90 | 90 | ||
91 | if (*remainder >= b) { |
91 | if (*remainder >= b) { |
92 | *remainder -= b; |
92 | *remainder -= b; |
93 | result |= 0x1; |
93 | result |= 0x1; |
94 | } |
94 | } |
95 | divident <<= 1; |
95 | a <<= 1; |
96 | } |
96 | } |
97 | 97 | ||
98 | return result; |
98 | return result; |
99 | } |
99 | } |
100 | 100 | ||
101 | /* 32bit integer division */ |
101 | /* 32bit integer division */ |
102 | int __divsi3(int a, int b) |
102 | int __divsi3(int a, int b) |
103 | { |
103 | { |
104 | unsigned int rem; |
104 | unsigned int rem; |
105 | int result; |
105 | int result; |
106 | 106 | ||
107 | result = (int)divandmod32(ABSVAL(a), ABSVAL(b), &rem); |
107 | result = (int)divandmod32(ABSVAL(a), ABSVAL(b), &rem); |
108 | 108 | ||
109 | if ( SGN(a) == SGN(b)) return result; |
109 | if ( SGN(a) == SGN(b)) return result; |
110 | return -result; |
110 | return -result; |
111 | } |
111 | } |
112 | 112 | ||
113 | /* 64bit integer division */ |
113 | /* 64bit integer division */ |
114 | long __divdi3(long a, long b) |
114 | long __divdi3(long a, long b) |
115 | { |
115 | { |
116 | unsigned long rem; |
116 | unsigned long rem; |
117 | long result; |
117 | long result; |
118 | 118 | ||
119 | result = (long)divandmod64(ABSVAL(a), ABSVAL(b), &rem); |
119 | result = (long)divandmod64(ABSVAL(a), ABSVAL(b), &rem); |
120 | 120 | ||
121 | if ( SGN(a) == SGN(b)) return result; |
121 | if ( SGN(a) == SGN(b)) return result; |
122 | return -result; |
122 | return -result; |
123 | } |
123 | } |
124 | 124 | ||
125 | /* 32bit unsigned integer division */ |
125 | /* 32bit unsigned integer division */ |
126 | unsigned int __udivsi3(unsigned int a, unsigned int b) |
126 | unsigned int __udivsi3(unsigned int a, unsigned int b) |
127 | { |
127 | { |
128 | unsigned int rem; |
128 | unsigned int rem; |
129 | return divandmod32(a, b, &rem); |
129 | return divandmod32(a, b, &rem); |
130 | } |
130 | } |
131 | 131 | ||
132 | /* 64bit unsigned integer division */ |
132 | /* 64bit unsigned integer division */ |
133 | unsigned long __udivdi3(unsigned long a, unsigned long b) |
133 | unsigned long __udivdi3(unsigned long a, unsigned long b) |
134 | { |
134 | { |
135 | unsigned long rem; |
135 | unsigned long rem; |
136 | return divandmod64(a, b, &rem); |
136 | return divandmod64(a, b, &rem); |
137 | } |
137 | } |
138 | 138 | ||
139 | /* 32bit remainder of the signed division */ |
139 | /* 32bit remainder of the signed division */ |
140 | int __modsi3(int a, int b) |
140 | int __modsi3(int a, int b) |
141 | { |
141 | { |
142 | unsigned int rem; |
142 | unsigned int rem; |
143 | divandmod32(a, b, &rem); |
143 | divandmod32(a, b, &rem); |
144 | 144 | ||
145 | /* if divident is negative, remainder must be too*/ |
145 | /* if divident is negative, remainder must be too */ |
146 | if (!(SGN(a))) { |
146 | if (!(SGN(a))) { |
147 | return -((int)rem); |
147 | return -((int)rem); |
148 | } |
148 | } |
149 | 149 | ||
150 | return (int)rem; |
150 | return (int)rem; |
151 | } |
151 | } |
152 | 152 | ||
153 | /* 64bit remainder of the signed division */ |
153 | /* 64bit remainder of the signed division */ |
154 | long __moddi3(long a, long b) |
154 | long __moddi3(long a, long b) |
155 | { |
155 | { |
156 | unsigned long rem; |
156 | unsigned long rem; |
157 | divandmod64(a, b, &rem); |
157 | divandmod64(a, b, &rem); |
158 | 158 | ||
159 | /* if divident is negative, remainder must be too*/ |
159 | /* if divident is negative, remainder must be too */ |
160 | if (!(SGN(a))) { |
160 | if (!(SGN(a))) { |
161 | return -((long)rem); |
161 | return -((long)rem); |
162 | } |
162 | } |
163 | 163 | ||
164 | return (long)rem; |
164 | return (long)rem; |
165 | } |
165 | } |
166 | 166 | ||
167 | /* 32bit remainder of the unsigned division */ |
167 | /* 32bit remainder of the unsigned division */ |
168 | unsigned int __umodsi3(unsigned int a, unsigned int b) |
168 | unsigned int __umodsi3(unsigned int a, unsigned int b) |
169 | { |
169 | { |
170 | unsigned int rem; |
170 | unsigned int rem; |
171 | divandmod32(a, b, &rem); |
171 | divandmod32(a, b, &rem); |
172 | return rem; |
172 | return rem; |
173 | } |
173 | } |
174 | 174 | ||
175 | /* 64bit remainder of the unsigned division */ |
175 | /* 64bit remainder of the unsigned division */ |
176 | unsigned long __umoddi3(unsigned long a, unsigned long b) |
176 | unsigned long __umoddi3(unsigned long a, unsigned long b) |
177 | { |
177 | { |
178 | unsigned long rem; |
178 | unsigned long rem; |
179 | divandmod64(a, b, &rem); |
179 | divandmod64(a, b, &rem); |
180 | return rem; |
180 | return rem; |
181 | } |
181 | } |
182 | 182 | ||
183 | unsigned long __udivmoddi3(unsigned long a, unsigned long b, unsigned long *c) |
183 | unsigned long __udivmoddi3(unsigned long a, unsigned long b, unsigned long *c) |
184 | { |
184 | { |
185 | return divandmod64(a, b, c); |
185 | return divandmod64(a, b, c); |
186 | } |
186 | } |
187 | 187 | ||
188 | 188 | ||
189 | 189 |