Add not-cancel.h for m68k/nptl
[glibc.git] / sysdeps / wordsize-32 / divdi3.c
blob7898541eb01a383e8ef00e8402a148c07ab22061
1 /* 64-bit multiplication and division
2 Copyright (C) 1989, 1992-2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <endian.h>
20 #include <stdlib.h>
21 #include <bits/wordsize.h>
23 #if __WORDSIZE != 32
24 #error This is for 32-bit targets only
25 #endif
27 typedef unsigned int UQItype __attribute__ ((mode (QI)));
28 typedef int SItype __attribute__ ((mode (SI)));
29 typedef unsigned int USItype __attribute__ ((mode (SI)));
30 typedef int DItype __attribute__ ((mode (DI)));
31 typedef unsigned int UDItype __attribute__ ((mode (DI)));
32 #define Wtype SItype
33 #define HWtype SItype
34 #define DWtype DItype
35 #define UWtype USItype
36 #define UHWtype USItype
37 #define UDWtype UDItype
38 #define W_TYPE_SIZE 32
40 #include <stdlib/longlong.h>
42 #if __BYTE_ORDER == __BIG_ENDIAN
43 struct DWstruct { Wtype high, low;};
44 #elif __BYTE_ORDER == __LITTLE_ENDIAN
45 struct DWstruct { Wtype low, high;};
46 #else
47 #error Unhandled endianity
48 #endif
49 typedef union { struct DWstruct s; DWtype ll; } DWunion;
51 /* Prototypes of exported functions. */
52 extern DWtype __divdi3 (DWtype u, DWtype v);
53 extern DWtype __moddi3 (DWtype u, DWtype v);
54 extern UDWtype __udivdi3 (UDWtype u, UDWtype v);
55 extern UDWtype __umoddi3 (UDWtype u, UDWtype v);
57 static UDWtype
58 __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp)
60 DWunion ww;
61 DWunion nn, dd;
62 DWunion rr;
63 UWtype d0, d1, n0, n1, n2;
64 UWtype q0, q1;
65 UWtype b, bm;
67 nn.ll = n;
68 dd.ll = d;
70 d0 = dd.s.low;
71 d1 = dd.s.high;
72 n0 = nn.s.low;
73 n1 = nn.s.high;
75 #if !UDIV_NEEDS_NORMALIZATION
76 if (d1 == 0)
78 if (d0 > n1)
80 /* 0q = nn / 0D */
82 udiv_qrnnd (q0, n0, n1, n0, d0);
83 q1 = 0;
85 /* Remainder in n0. */
87 else
89 /* qq = NN / 0d */
91 if (d0 == 0)
92 d0 = 1 / d0; /* Divide intentionally by zero. */
94 udiv_qrnnd (q1, n1, 0, n1, d0);
95 udiv_qrnnd (q0, n0, n1, n0, d0);
97 /* Remainder in n0. */
100 if (rp != 0)
102 rr.s.low = n0;
103 rr.s.high = 0;
104 *rp = rr.ll;
108 #else /* UDIV_NEEDS_NORMALIZATION */
110 if (d1 == 0)
112 if (d0 > n1)
114 /* 0q = nn / 0D */
116 count_leading_zeros (bm, d0);
118 if (bm != 0)
120 /* Normalize, i.e. make the most significant bit of the
121 denominator set. */
123 d0 = d0 << bm;
124 n1 = (n1 << bm) | (n0 >> (W_TYPE_SIZE - bm));
125 n0 = n0 << bm;
128 udiv_qrnnd (q0, n0, n1, n0, d0);
129 q1 = 0;
131 /* Remainder in n0 >> bm. */
133 else
135 /* qq = NN / 0d */
137 if (d0 == 0)
138 d0 = 1 / d0; /* Divide intentionally by zero. */
140 count_leading_zeros (bm, d0);
142 if (bm == 0)
144 /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
145 conclude (the most significant bit of n1 is set) /\ (the
146 leading quotient digit q1 = 1).
148 This special case is necessary, not an optimization.
149 (Shifts counts of W_TYPE_SIZE are undefined.) */
151 n1 -= d0;
152 q1 = 1;
154 else
156 /* Normalize. */
158 b = W_TYPE_SIZE - bm;
160 d0 = d0 << bm;
161 n2 = n1 >> b;
162 n1 = (n1 << bm) | (n0 >> b);
163 n0 = n0 << bm;
165 udiv_qrnnd (q1, n1, n2, n1, d0);
168 /* n1 != d0... */
170 udiv_qrnnd (q0, n0, n1, n0, d0);
172 /* Remainder in n0 >> bm. */
175 if (rp != 0)
177 rr.s.low = n0 >> bm;
178 rr.s.high = 0;
179 *rp = rr.ll;
182 #endif /* UDIV_NEEDS_NORMALIZATION */
184 else
186 if (d1 > n1)
188 /* 00 = nn / DD */
190 q0 = 0;
191 q1 = 0;
193 /* Remainder in n1n0. */
194 if (rp != 0)
196 rr.s.low = n0;
197 rr.s.high = n1;
198 *rp = rr.ll;
201 else
203 /* 0q = NN / dd */
205 count_leading_zeros (bm, d1);
206 if (bm == 0)
208 /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
209 conclude (the most significant bit of n1 is set) /\ (the
210 quotient digit q0 = 0 or 1).
212 This special case is necessary, not an optimization. */
214 /* The condition on the next line takes advantage of that
215 n1 >= d1 (true due to program flow). */
216 if (n1 > d1 || n0 >= d0)
218 q0 = 1;
219 sub_ddmmss (n1, n0, n1, n0, d1, d0);
221 else
222 q0 = 0;
224 q1 = 0;
226 if (rp != 0)
228 rr.s.low = n0;
229 rr.s.high = n1;
230 *rp = rr.ll;
233 else
235 UWtype m1, m0;
236 /* Normalize. */
238 b = W_TYPE_SIZE - bm;
240 d1 = (d1 << bm) | (d0 >> b);
241 d0 = d0 << bm;
242 n2 = n1 >> b;
243 n1 = (n1 << bm) | (n0 >> b);
244 n0 = n0 << bm;
246 udiv_qrnnd (q0, n1, n2, n1, d1);
247 umul_ppmm (m1, m0, q0, d0);
249 if (m1 > n1 || (m1 == n1 && m0 > n0))
251 q0--;
252 sub_ddmmss (m1, m0, m1, m0, d1, d0);
255 q1 = 0;
257 /* Remainder in (n1n0 - m1m0) >> bm. */
258 if (rp != 0)
260 sub_ddmmss (n1, n0, n1, n0, m1, m0);
261 rr.s.low = (n1 << b) | (n0 >> bm);
262 rr.s.high = n1 >> bm;
263 *rp = rr.ll;
269 ww.s.low = q0;
270 ww.s.high = q1;
271 return ww.ll;
274 DWtype
275 __divdi3 (DWtype u, DWtype v)
277 Wtype c = 0;
278 DWtype w;
280 if (u < 0)
282 c = ~c;
283 u = -u;
285 if (v < 0)
287 c = ~c;
288 v = -v;
290 w = __udivmoddi4 (u, v, NULL);
291 if (c)
292 w = -w;
293 return w;
295 strong_alias (__divdi3, __divdi3_internal)
297 DWtype
298 __moddi3 (DWtype u, DWtype v)
300 Wtype c = 0;
301 DWtype w;
303 if (u < 0)
305 c = ~c;
306 u = -u;
308 if (v < 0)
309 v = -v;
310 __udivmoddi4 (u, v, (UDWtype *) &w);
311 if (c)
312 w = -w;
313 return w;
315 strong_alias (__moddi3, __moddi3_internal)
317 UDWtype
318 __udivdi3 (UDWtype u, UDWtype v)
320 return __udivmoddi4 (u, v, NULL);
322 strong_alias (__udivdi3, __udivdi3_internal)
324 UDWtype
325 __umoddi3 (UDWtype u, UDWtype v)
327 UDWtype w;
329 __udivmoddi4 (u, v, &w);
330 return w;
332 strong_alias (__umoddi3, __umoddi3_internal)
334 /* We declare these with compat_symbol so that they are not visible at
335 link time. Programs must use the functions from libgcc. */
336 #if defined SHARED && defined DO_VERSIONING
337 # include <shlib-compat.h>
338 compat_symbol (libc, __divdi3, __divdi3, GLIBC_2_0);
339 compat_symbol (libc, __moddi3, __moddi3, GLIBC_2_0);
340 compat_symbol (libc, __udivdi3, __udivdi3, GLIBC_2_0);
341 compat_symbol (libc, __umoddi3, __umoddi3, GLIBC_2_0);
342 #endif