gcc/
[official-gcc.git] / libgcc / longlong.h
blob4fa9d46832b511811d2886a3ba0929d1dabe562f
1 /* longlong.h -- definitions for mixed size 32/64 bit arithmetic.
2 Copyright (C) 1991, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of the GNU C Library.
8 The GNU C Library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
13 In addition to the permissions in the GNU Lesser General Public
14 License, the Free Software Foundation gives you unlimited
15 permission to link the compiled version of this file into
16 combinations with other programs, and to distribute those
17 combinations without any restriction coming from the use of this
18 file. (The Lesser General Public License restrictions do apply in
19 other respects; for example, they cover modification of the file,
20 and distribution when not linked into a combine executable.)
22 The GNU C Library is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 Lesser General Public License for more details.
27 You should have received a copy of the GNU Lesser General Public
28 License along with the GNU C Library; if not, see
29 <http://www.gnu.org/licenses/>. */
31 /* You have to define the following before including this file:
33 UWtype -- An unsigned type, default type for operations (typically a "word")
34 UHWtype -- An unsigned type, at least half the size of UWtype.
35 UDWtype -- An unsigned type, at least twice as large a UWtype
36 W_TYPE_SIZE -- size in bits of UWtype
38 UQItype -- Unsigned 8 bit type.
39 SItype, USItype -- Signed and unsigned 32 bit types.
40 DItype, UDItype -- Signed and unsigned 64 bit types.
42 On a 32 bit machine UWtype should typically be USItype;
43 on a 64 bit machine, UWtype should typically be UDItype. */
45 #define __BITS4 (W_TYPE_SIZE / 4)
46 #define __ll_B ((UWtype) 1 << (W_TYPE_SIZE / 2))
47 #define __ll_lowpart(t) ((UWtype) (t) & (__ll_B - 1))
48 #define __ll_highpart(t) ((UWtype) (t) >> (W_TYPE_SIZE / 2))
50 #ifndef W_TYPE_SIZE
51 #define W_TYPE_SIZE 32
52 #define UWtype USItype
53 #define UHWtype USItype
54 #define UDWtype UDItype
55 #endif
57 /* Used in glibc only. */
58 #ifndef attribute_hidden
59 #define attribute_hidden
60 #endif
62 extern const UQItype __clz_tab[256] attribute_hidden;
64 /* Define auxiliary asm macros.
66 1) umul_ppmm(high_prod, low_prod, multiplier, multiplicand) multiplies two
67 UWtype integers MULTIPLIER and MULTIPLICAND, and generates a two UWtype
68 word product in HIGH_PROD and LOW_PROD.
70 2) __umulsidi3(a,b) multiplies two UWtype integers A and B, and returns a
71 UDWtype product. This is just a variant of umul_ppmm.
73 3) udiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
74 denominator) divides a UDWtype, composed by the UWtype integers
75 HIGH_NUMERATOR and LOW_NUMERATOR, by DENOMINATOR and places the quotient
76 in QUOTIENT and the remainder in REMAINDER. HIGH_NUMERATOR must be less
77 than DENOMINATOR for correct operation. If, in addition, the most
78 significant bit of DENOMINATOR must be 1, then the pre-processor symbol
79 UDIV_NEEDS_NORMALIZATION is defined to 1.
81 4) sdiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
82 denominator). Like udiv_qrnnd but the numbers are signed. The quotient
83 is rounded towards 0.
85 5) count_leading_zeros(count, x) counts the number of zero-bits from the
86 msb to the first nonzero bit in the UWtype X. This is the number of
87 steps X needs to be shifted left to set the msb. Undefined for X == 0,
88 unless the symbol COUNT_LEADING_ZEROS_0 is defined to some value.
90 6) count_trailing_zeros(count, x) like count_leading_zeros, but counts
91 from the least significant end.
93 7) add_ssaaaa(high_sum, low_sum, high_addend_1, low_addend_1,
94 high_addend_2, low_addend_2) adds two UWtype integers, composed by
95 HIGH_ADDEND_1 and LOW_ADDEND_1, and HIGH_ADDEND_2 and LOW_ADDEND_2
96 respectively. The result is placed in HIGH_SUM and LOW_SUM. Overflow
97 (i.e. carry out) is not stored anywhere, and is lost.
99 8) sub_ddmmss(high_difference, low_difference, high_minuend, low_minuend,
100 high_subtrahend, low_subtrahend) subtracts two two-word UWtype integers,
101 composed by HIGH_MINUEND_1 and LOW_MINUEND_1, and HIGH_SUBTRAHEND_2 and
102 LOW_SUBTRAHEND_2 respectively. The result is placed in HIGH_DIFFERENCE
103 and LOW_DIFFERENCE. Overflow (i.e. carry out) is not stored anywhere,
104 and is lost.
106 If any of these macros are left undefined for a particular CPU,
107 C macros are used. */
109 /* The CPUs come in alphabetical order below.
111 Please add support for more CPUs here, or improve the current support
112 for the CPUs below!
113 (E.g. WE32100, IBM360.) */
115 #if defined (__GNUC__) && !defined (NO_ASM)
117 /* We sometimes need to clobber "cc" with gcc2, but that would not be
118 understood by gcc1. Use cpp to avoid major code duplication. */
119 #if __GNUC__ < 2
120 #define __CLOBBER_CC
121 #define __AND_CLOBBER_CC
122 #else /* __GNUC__ >= 2 */
123 #define __CLOBBER_CC : "cc"
124 #define __AND_CLOBBER_CC , "cc"
125 #endif /* __GNUC__ < 2 */
127 #if defined (__alpha) && W_TYPE_SIZE == 64
128 #define umul_ppmm(ph, pl, m0, m1) \
129 do { \
130 UDItype __m0 = (m0), __m1 = (m1); \
131 (ph) = __builtin_alpha_umulh (__m0, __m1); \
132 (pl) = __m0 * __m1; \
133 } while (0)
134 #define UMUL_TIME 46
135 #ifndef LONGLONG_STANDALONE
136 #define udiv_qrnnd(q, r, n1, n0, d) \
137 do { UDItype __r; \
138 (q) = __udiv_qrnnd (&__r, (n1), (n0), (d)); \
139 (r) = __r; \
140 } while (0)
141 extern UDItype __udiv_qrnnd (UDItype *, UDItype, UDItype, UDItype);
142 #define UDIV_TIME 220
143 #endif /* LONGLONG_STANDALONE */
144 #ifdef __alpha_cix__
145 #define count_leading_zeros(COUNT,X) ((COUNT) = __builtin_clzl (X))
146 #define count_trailing_zeros(COUNT,X) ((COUNT) = __builtin_ctzl (X))
147 #define COUNT_LEADING_ZEROS_0 64
148 #else
149 #define count_leading_zeros(COUNT,X) \
150 do { \
151 UDItype __xr = (X), __t, __a; \
152 __t = __builtin_alpha_cmpbge (0, __xr); \
153 __a = __clz_tab[__t ^ 0xff] - 1; \
154 __t = __builtin_alpha_extbl (__xr, __a); \
155 (COUNT) = 64 - (__clz_tab[__t] + __a*8); \
156 } while (0)
157 #define count_trailing_zeros(COUNT,X) \
158 do { \
159 UDItype __xr = (X), __t, __a; \
160 __t = __builtin_alpha_cmpbge (0, __xr); \
161 __t = ~__t & -~__t; \
162 __a = ((__t & 0xCC) != 0) * 2; \
163 __a += ((__t & 0xF0) != 0) * 4; \
164 __a += ((__t & 0xAA) != 0); \
165 __t = __builtin_alpha_extbl (__xr, __a); \
166 __a <<= 3; \
167 __t &= -__t; \
168 __a += ((__t & 0xCC) != 0) * 2; \
169 __a += ((__t & 0xF0) != 0) * 4; \
170 __a += ((__t & 0xAA) != 0); \
171 (COUNT) = __a; \
172 } while (0)
173 #endif /* __alpha_cix__ */
174 #endif /* __alpha */
176 #if defined (__arc__) && W_TYPE_SIZE == 32
177 #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
178 __asm__ ("add.f %1, %4, %5\n\tadc %0, %2, %3" \
179 : "=r" ((USItype) (sh)), \
180 "=&r" ((USItype) (sl)) \
181 : "%r" ((USItype) (ah)), \
182 "rIJ" ((USItype) (bh)), \
183 "%r" ((USItype) (al)), \
184 "rIJ" ((USItype) (bl)))
185 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
186 __asm__ ("sub.f %1, %4, %5\n\tsbc %0, %2, %3" \
187 : "=r" ((USItype) (sh)), \
188 "=&r" ((USItype) (sl)) \
189 : "r" ((USItype) (ah)), \
190 "rIJ" ((USItype) (bh)), \
191 "r" ((USItype) (al)), \
192 "rIJ" ((USItype) (bl)))
193 /* Call libgcc routine. */
194 #define umul_ppmm(w1, w0, u, v) \
195 do { \
196 DWunion __w; \
197 __w.ll = __umulsidi3 (u, v); \
198 w1 = __w.s.high; \
199 w0 = __w.s.low; \
200 } while (0)
201 #define __umulsidi3 __umulsidi3
202 UDItype __umulsidi3 (USItype, USItype);
203 #endif
205 #if defined (__arm__) && (defined (__thumb2__) || !defined (__thumb__)) \
206 && W_TYPE_SIZE == 32
207 #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
208 __asm__ ("adds %1, %4, %5\n\tadc %0, %2, %3" \
209 : "=r" ((USItype) (sh)), \
210 "=&r" ((USItype) (sl)) \
211 : "%r" ((USItype) (ah)), \
212 "rI" ((USItype) (bh)), \
213 "%r" ((USItype) (al)), \
214 "rI" ((USItype) (bl)) __CLOBBER_CC)
215 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
216 __asm__ ("subs %1, %4, %5\n\tsbc %0, %2, %3" \
217 : "=r" ((USItype) (sh)), \
218 "=&r" ((USItype) (sl)) \
219 : "r" ((USItype) (ah)), \
220 "rI" ((USItype) (bh)), \
221 "r" ((USItype) (al)), \
222 "rI" ((USItype) (bl)) __CLOBBER_CC)
223 # if defined(__ARM_ARCH_2__) || defined(__ARM_ARCH_2A__) \
224 || defined(__ARM_ARCH_3__)
225 # define umul_ppmm(xh, xl, a, b) \
226 do { \
227 register USItype __t0, __t1, __t2; \
228 __asm__ ("%@ Inlined umul_ppmm\n" \
229 " mov %2, %5, lsr #16\n" \
230 " mov %0, %6, lsr #16\n" \
231 " bic %3, %5, %2, lsl #16\n" \
232 " bic %4, %6, %0, lsl #16\n" \
233 " mul %1, %3, %4\n" \
234 " mul %4, %2, %4\n" \
235 " mul %3, %0, %3\n" \
236 " mul %0, %2, %0\n" \
237 " adds %3, %4, %3\n" \
238 " addcs %0, %0, #65536\n" \
239 " adds %1, %1, %3, lsl #16\n" \
240 " adc %0, %0, %3, lsr #16" \
241 : "=&r" ((USItype) (xh)), \
242 "=r" ((USItype) (xl)), \
243 "=&r" (__t0), "=&r" (__t1), "=r" (__t2) \
244 : "r" ((USItype) (a)), \
245 "r" ((USItype) (b)) __CLOBBER_CC ); \
246 } while (0)
247 # define UMUL_TIME 20
248 # else
249 # define umul_ppmm(xh, xl, a, b) \
250 do { \
251 /* Generate umull, under compiler control. */ \
252 register UDItype __t0 = (UDItype)(USItype)(a) * (USItype)(b); \
253 (xl) = (USItype)__t0; \
254 (xh) = (USItype)(__t0 >> 32); \
255 } while (0)
256 # define UMUL_TIME 3
257 # endif
258 # define UDIV_TIME 100
259 #endif /* __arm__ */
261 #if defined(__arm__)
262 /* Let gcc decide how best to implement count_leading_zeros. */
263 #define count_leading_zeros(COUNT,X) ((COUNT) = __builtin_clz (X))
264 #define count_trailing_zeros(COUNT,X) ((COUNT) = __builtin_ctz (X))
265 #define COUNT_LEADING_ZEROS_0 32
266 #endif
268 #if defined (__AVR__)
270 #if W_TYPE_SIZE == 16
271 #define count_leading_zeros(COUNT,X) ((COUNT) = __builtin_clz (X))
272 #define count_trailing_zeros(COUNT,X) ((COUNT) = __builtin_ctz (X))
273 #define COUNT_LEADING_ZEROS_0 16
274 #endif /* W_TYPE_SIZE == 16 */
276 #if W_TYPE_SIZE == 32
277 #define count_leading_zeros(COUNT,X) ((COUNT) = __builtin_clzl (X))
278 #define count_trailing_zeros(COUNT,X) ((COUNT) = __builtin_ctzl (X))
279 #define COUNT_LEADING_ZEROS_0 32
280 #endif /* W_TYPE_SIZE == 32 */
282 #if W_TYPE_SIZE == 64
283 #define count_leading_zeros(COUNT,X) ((COUNT) = __builtin_clzll (X))
284 #define count_trailing_zeros(COUNT,X) ((COUNT) = __builtin_ctzll (X))
285 #define COUNT_LEADING_ZEROS_0 64
286 #endif /* W_TYPE_SIZE == 64 */
288 #endif /* defined (__AVR__) */
290 #if defined (__CRIS__) && __CRIS_arch_version >= 3
291 #define count_leading_zeros(COUNT, X) ((COUNT) = __builtin_clz (X))
292 #if __CRIS_arch_version >= 8
293 #define count_trailing_zeros(COUNT, X) ((COUNT) = __builtin_ctz (X))
294 #endif
295 #endif /* __CRIS__ */
297 #if defined (__hppa) && W_TYPE_SIZE == 32
298 #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
299 __asm__ ("add %4,%5,%1\n\taddc %2,%3,%0" \
300 : "=r" ((USItype) (sh)), \
301 "=&r" ((USItype) (sl)) \
302 : "%rM" ((USItype) (ah)), \
303 "rM" ((USItype) (bh)), \
304 "%rM" ((USItype) (al)), \
305 "rM" ((USItype) (bl)))
306 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
307 __asm__ ("sub %4,%5,%1\n\tsubb %2,%3,%0" \
308 : "=r" ((USItype) (sh)), \
309 "=&r" ((USItype) (sl)) \
310 : "rM" ((USItype) (ah)), \
311 "rM" ((USItype) (bh)), \
312 "rM" ((USItype) (al)), \
313 "rM" ((USItype) (bl)))
314 #if defined (_PA_RISC1_1)
315 #define umul_ppmm(w1, w0, u, v) \
316 do { \
317 union \
319 UDItype __f; \
320 struct {USItype __w1, __w0;} __w1w0; \
321 } __t; \
322 __asm__ ("xmpyu %1,%2,%0" \
323 : "=x" (__t.__f) \
324 : "x" ((USItype) (u)), \
325 "x" ((USItype) (v))); \
326 (w1) = __t.__w1w0.__w1; \
327 (w0) = __t.__w1w0.__w0; \
328 } while (0)
329 #define UMUL_TIME 8
330 #else
331 #define UMUL_TIME 30
332 #endif
333 #define UDIV_TIME 40
334 #define count_leading_zeros(count, x) \
335 do { \
336 USItype __tmp; \
337 __asm__ ( \
338 "ldi 1,%0\n" \
339 " extru,= %1,15,16,%%r0 ; Bits 31..16 zero?\n" \
340 " extru,tr %1,15,16,%1 ; No. Shift down, skip add.\n"\
341 " ldo 16(%0),%0 ; Yes. Perform add.\n" \
342 " extru,= %1,23,8,%%r0 ; Bits 15..8 zero?\n" \
343 " extru,tr %1,23,8,%1 ; No. Shift down, skip add.\n"\
344 " ldo 8(%0),%0 ; Yes. Perform add.\n" \
345 " extru,= %1,27,4,%%r0 ; Bits 7..4 zero?\n" \
346 " extru,tr %1,27,4,%1 ; No. Shift down, skip add.\n"\
347 " ldo 4(%0),%0 ; Yes. Perform add.\n" \
348 " extru,= %1,29,2,%%r0 ; Bits 3..2 zero?\n" \
349 " extru,tr %1,29,2,%1 ; No. Shift down, skip add.\n"\
350 " ldo 2(%0),%0 ; Yes. Perform add.\n" \
351 " extru %1,30,1,%1 ; Extract bit 1.\n" \
352 " sub %0,%1,%0 ; Subtract it.\n" \
353 : "=r" (count), "=r" (__tmp) : "1" (x)); \
354 } while (0)
355 #endif
357 #if (defined (__i370__) || defined (__s390__) || defined (__mvs__)) && W_TYPE_SIZE == 32
358 #if !defined (__zarch__)
359 #define smul_ppmm(xh, xl, m0, m1) \
360 do { \
361 union {DItype __ll; \
362 struct {USItype __h, __l;} __i; \
363 } __x; \
364 __asm__ ("lr %N0,%1\n\tmr %0,%2" \
365 : "=&r" (__x.__ll) \
366 : "r" (m0), "r" (m1)); \
367 (xh) = __x.__i.__h; (xl) = __x.__i.__l; \
368 } while (0)
369 #define sdiv_qrnnd(q, r, n1, n0, d) \
370 do { \
371 union {DItype __ll; \
372 struct {USItype __h, __l;} __i; \
373 } __x; \
374 __x.__i.__h = n1; __x.__i.__l = n0; \
375 __asm__ ("dr %0,%2" \
376 : "=r" (__x.__ll) \
377 : "0" (__x.__ll), "r" (d)); \
378 (q) = __x.__i.__l; (r) = __x.__i.__h; \
379 } while (0)
380 #else
381 #define smul_ppmm(xh, xl, m0, m1) \
382 do { \
383 register SItype __r0 __asm__ ("0"); \
384 register SItype __r1 __asm__ ("1") = (m0); \
386 __asm__ ("mr\t%%r0,%3" \
387 : "=r" (__r0), "=r" (__r1) \
388 : "r" (__r1), "r" (m1)); \
389 (xh) = __r0; (xl) = __r1; \
390 } while (0)
392 #define sdiv_qrnnd(q, r, n1, n0, d) \
393 do { \
394 register SItype __r0 __asm__ ("0") = (n1); \
395 register SItype __r1 __asm__ ("1") = (n0); \
397 __asm__ ("dr\t%%r0,%4" \
398 : "=r" (__r0), "=r" (__r1) \
399 : "r" (__r0), "r" (__r1), "r" (d)); \
400 (q) = __r1; (r) = __r0; \
401 } while (0)
402 #endif /* __zarch__ */
403 #endif
405 #if (defined (__i386__) || defined (__i486__)) && W_TYPE_SIZE == 32
406 #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
407 __asm__ ("add{l} {%5,%1|%1,%5}\n\tadc{l} {%3,%0|%0,%3}" \
408 : "=r" ((USItype) (sh)), \
409 "=&r" ((USItype) (sl)) \
410 : "%0" ((USItype) (ah)), \
411 "g" ((USItype) (bh)), \
412 "%1" ((USItype) (al)), \
413 "g" ((USItype) (bl)))
414 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
415 __asm__ ("sub{l} {%5,%1|%1,%5}\n\tsbb{l} {%3,%0|%0,%3}" \
416 : "=r" ((USItype) (sh)), \
417 "=&r" ((USItype) (sl)) \
418 : "0" ((USItype) (ah)), \
419 "g" ((USItype) (bh)), \
420 "1" ((USItype) (al)), \
421 "g" ((USItype) (bl)))
422 #define umul_ppmm(w1, w0, u, v) \
423 __asm__ ("mul{l} %3" \
424 : "=a" ((USItype) (w0)), \
425 "=d" ((USItype) (w1)) \
426 : "%0" ((USItype) (u)), \
427 "rm" ((USItype) (v)))
428 #define udiv_qrnnd(q, r, n1, n0, dv) \
429 __asm__ ("div{l} %4" \
430 : "=a" ((USItype) (q)), \
431 "=d" ((USItype) (r)) \
432 : "0" ((USItype) (n0)), \
433 "1" ((USItype) (n1)), \
434 "rm" ((USItype) (dv)))
435 #define count_leading_zeros(count, x) ((count) = __builtin_clz (x))
436 #define count_trailing_zeros(count, x) ((count) = __builtin_ctz (x))
437 #define UMUL_TIME 40
438 #define UDIV_TIME 40
439 #endif /* 80x86 */
441 #if (defined (__x86_64__) || defined (__i386__)) && W_TYPE_SIZE == 64
442 #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
443 __asm__ ("add{q} {%5,%1|%1,%5}\n\tadc{q} {%3,%0|%0,%3}" \
444 : "=r" ((UDItype) (sh)), \
445 "=&r" ((UDItype) (sl)) \
446 : "%0" ((UDItype) (ah)), \
447 "rme" ((UDItype) (bh)), \
448 "%1" ((UDItype) (al)), \
449 "rme" ((UDItype) (bl)))
450 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
451 __asm__ ("sub{q} {%5,%1|%1,%5}\n\tsbb{q} {%3,%0|%0,%3}" \
452 : "=r" ((UDItype) (sh)), \
453 "=&r" ((UDItype) (sl)) \
454 : "0" ((UDItype) (ah)), \
455 "rme" ((UDItype) (bh)), \
456 "1" ((UDItype) (al)), \
457 "rme" ((UDItype) (bl)))
458 #define umul_ppmm(w1, w0, u, v) \
459 __asm__ ("mul{q} %3" \
460 : "=a" ((UDItype) (w0)), \
461 "=d" ((UDItype) (w1)) \
462 : "%0" ((UDItype) (u)), \
463 "rm" ((UDItype) (v)))
464 #define udiv_qrnnd(q, r, n1, n0, dv) \
465 __asm__ ("div{q} %4" \
466 : "=a" ((UDItype) (q)), \
467 "=d" ((UDItype) (r)) \
468 : "0" ((UDItype) (n0)), \
469 "1" ((UDItype) (n1)), \
470 "rm" ((UDItype) (dv)))
471 #define count_leading_zeros(count, x) ((count) = __builtin_clzll (x))
472 #define count_trailing_zeros(count, x) ((count) = __builtin_ctzll (x))
473 #define UMUL_TIME 40
474 #define UDIV_TIME 40
475 #endif /* x86_64 */
477 #if defined (__i960__) && W_TYPE_SIZE == 32
478 #define umul_ppmm(w1, w0, u, v) \
479 ({union {UDItype __ll; \
480 struct {USItype __l, __h;} __i; \
481 } __xx; \
482 __asm__ ("emul %2,%1,%0" \
483 : "=d" (__xx.__ll) \
484 : "%dI" ((USItype) (u)), \
485 "dI" ((USItype) (v))); \
486 (w1) = __xx.__i.__h; (w0) = __xx.__i.__l;})
487 #define __umulsidi3(u, v) \
488 ({UDItype __w; \
489 __asm__ ("emul %2,%1,%0" \
490 : "=d" (__w) \
491 : "%dI" ((USItype) (u)), \
492 "dI" ((USItype) (v))); \
493 __w; })
494 #endif /* __i960__ */
496 #if defined (__ia64) && W_TYPE_SIZE == 64
497 /* This form encourages gcc (pre-release 3.4 at least) to emit predicated
498 "sub r=r,r" and "sub r=r,r,1", giving a 2 cycle latency. The generic
499 code using "al<bl" arithmetically comes out making an actual 0 or 1 in a
500 register, which takes an extra cycle. */
501 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
502 do { \
503 UWtype __x; \
504 __x = (al) - (bl); \
505 if ((al) < (bl)) \
506 (sh) = (ah) - (bh) - 1; \
507 else \
508 (sh) = (ah) - (bh); \
509 (sl) = __x; \
510 } while (0)
512 /* Do both product parts in assembly, since that gives better code with
513 all gcc versions. Some callers will just use the upper part, and in
514 that situation we waste an instruction, but not any cycles. */
515 #define umul_ppmm(ph, pl, m0, m1) \
516 __asm__ ("xma.hu %0 = %2, %3, f0\n\txma.l %1 = %2, %3, f0" \
517 : "=&f" (ph), "=f" (pl) \
518 : "f" (m0), "f" (m1))
519 #define count_leading_zeros(count, x) \
520 do { \
521 UWtype _x = (x), _y, _a, _c; \
522 __asm__ ("mux1 %0 = %1, @rev" : "=r" (_y) : "r" (_x)); \
523 __asm__ ("czx1.l %0 = %1" : "=r" (_a) : "r" (-_y | _y)); \
524 _c = (_a - 1) << 3; \
525 _x >>= _c; \
526 if (_x >= 1 << 4) \
527 _x >>= 4, _c += 4; \
528 if (_x >= 1 << 2) \
529 _x >>= 2, _c += 2; \
530 _c += _x >> 1; \
531 (count) = W_TYPE_SIZE - 1 - _c; \
532 } while (0)
533 /* similar to what gcc does for __builtin_ffs, but 0 based rather than 1
534 based, and we don't need a special case for x==0 here */
535 #define count_trailing_zeros(count, x) \
536 do { \
537 UWtype __ctz_x = (x); \
538 __asm__ ("popcnt %0 = %1" \
539 : "=r" (count) \
540 : "r" ((__ctz_x-1) & ~__ctz_x)); \
541 } while (0)
542 #define UMUL_TIME 14
543 #endif
545 #if defined (__M32R__) && W_TYPE_SIZE == 32
546 #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
547 /* The cmp clears the condition bit. */ \
548 __asm__ ("cmp %0,%0\n\taddx %1,%5\n\taddx %0,%3" \
549 : "=r" ((USItype) (sh)), \
550 "=&r" ((USItype) (sl)) \
551 : "0" ((USItype) (ah)), \
552 "r" ((USItype) (bh)), \
553 "1" ((USItype) (al)), \
554 "r" ((USItype) (bl)) \
555 : "cbit")
556 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
557 /* The cmp clears the condition bit. */ \
558 __asm__ ("cmp %0,%0\n\tsubx %1,%5\n\tsubx %0,%3" \
559 : "=r" ((USItype) (sh)), \
560 "=&r" ((USItype) (sl)) \
561 : "0" ((USItype) (ah)), \
562 "r" ((USItype) (bh)), \
563 "1" ((USItype) (al)), \
564 "r" ((USItype) (bl)) \
565 : "cbit")
566 #endif /* __M32R__ */
568 #if defined (__mc68000__) && W_TYPE_SIZE == 32
569 #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
570 __asm__ ("add%.l %5,%1\n\taddx%.l %3,%0" \
571 : "=d" ((USItype) (sh)), \
572 "=&d" ((USItype) (sl)) \
573 : "%0" ((USItype) (ah)), \
574 "d" ((USItype) (bh)), \
575 "%1" ((USItype) (al)), \
576 "g" ((USItype) (bl)))
577 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
578 __asm__ ("sub%.l %5,%1\n\tsubx%.l %3,%0" \
579 : "=d" ((USItype) (sh)), \
580 "=&d" ((USItype) (sl)) \
581 : "0" ((USItype) (ah)), \
582 "d" ((USItype) (bh)), \
583 "1" ((USItype) (al)), \
584 "g" ((USItype) (bl)))
586 /* The '020, '030, '040, '060 and CPU32 have 32x32->64 and 64/32->32q-32r. */
587 #if (defined (__mc68020__) && !defined (__mc68060__))
588 #define umul_ppmm(w1, w0, u, v) \
589 __asm__ ("mulu%.l %3,%1:%0" \
590 : "=d" ((USItype) (w0)), \
591 "=d" ((USItype) (w1)) \
592 : "%0" ((USItype) (u)), \
593 "dmi" ((USItype) (v)))
594 #define UMUL_TIME 45
595 #define udiv_qrnnd(q, r, n1, n0, d) \
596 __asm__ ("divu%.l %4,%1:%0" \
597 : "=d" ((USItype) (q)), \
598 "=d" ((USItype) (r)) \
599 : "0" ((USItype) (n0)), \
600 "1" ((USItype) (n1)), \
601 "dmi" ((USItype) (d)))
602 #define UDIV_TIME 90
603 #define sdiv_qrnnd(q, r, n1, n0, d) \
604 __asm__ ("divs%.l %4,%1:%0" \
605 : "=d" ((USItype) (q)), \
606 "=d" ((USItype) (r)) \
607 : "0" ((USItype) (n0)), \
608 "1" ((USItype) (n1)), \
609 "dmi" ((USItype) (d)))
611 #elif defined (__mcoldfire__) /* not mc68020 */
613 #define umul_ppmm(xh, xl, a, b) \
614 __asm__ ("| Inlined umul_ppmm\n" \
615 " move%.l %2,%/d0\n" \
616 " move%.l %3,%/d1\n" \
617 " move%.l %/d0,%/d2\n" \
618 " swap %/d0\n" \
619 " move%.l %/d1,%/d3\n" \
620 " swap %/d1\n" \
621 " move%.w %/d2,%/d4\n" \
622 " mulu %/d3,%/d4\n" \
623 " mulu %/d1,%/d2\n" \
624 " mulu %/d0,%/d3\n" \
625 " mulu %/d0,%/d1\n" \
626 " move%.l %/d4,%/d0\n" \
627 " clr%.w %/d0\n" \
628 " swap %/d0\n" \
629 " add%.l %/d0,%/d2\n" \
630 " add%.l %/d3,%/d2\n" \
631 " jcc 1f\n" \
632 " add%.l %#65536,%/d1\n" \
633 "1: swap %/d2\n" \
634 " moveq %#0,%/d0\n" \
635 " move%.w %/d2,%/d0\n" \
636 " move%.w %/d4,%/d2\n" \
637 " move%.l %/d2,%1\n" \
638 " add%.l %/d1,%/d0\n" \
639 " move%.l %/d0,%0" \
640 : "=g" ((USItype) (xh)), \
641 "=g" ((USItype) (xl)) \
642 : "g" ((USItype) (a)), \
643 "g" ((USItype) (b)) \
644 : "d0", "d1", "d2", "d3", "d4")
645 #define UMUL_TIME 100
646 #define UDIV_TIME 400
647 #else /* not ColdFire */
648 /* %/ inserts REGISTER_PREFIX, %# inserts IMMEDIATE_PREFIX. */
649 #define umul_ppmm(xh, xl, a, b) \
650 __asm__ ("| Inlined umul_ppmm\n" \
651 " move%.l %2,%/d0\n" \
652 " move%.l %3,%/d1\n" \
653 " move%.l %/d0,%/d2\n" \
654 " swap %/d0\n" \
655 " move%.l %/d1,%/d3\n" \
656 " swap %/d1\n" \
657 " move%.w %/d2,%/d4\n" \
658 " mulu %/d3,%/d4\n" \
659 " mulu %/d1,%/d2\n" \
660 " mulu %/d0,%/d3\n" \
661 " mulu %/d0,%/d1\n" \
662 " move%.l %/d4,%/d0\n" \
663 " eor%.w %/d0,%/d0\n" \
664 " swap %/d0\n" \
665 " add%.l %/d0,%/d2\n" \
666 " add%.l %/d3,%/d2\n" \
667 " jcc 1f\n" \
668 " add%.l %#65536,%/d1\n" \
669 "1: swap %/d2\n" \
670 " moveq %#0,%/d0\n" \
671 " move%.w %/d2,%/d0\n" \
672 " move%.w %/d4,%/d2\n" \
673 " move%.l %/d2,%1\n" \
674 " add%.l %/d1,%/d0\n" \
675 " move%.l %/d0,%0" \
676 : "=g" ((USItype) (xh)), \
677 "=g" ((USItype) (xl)) \
678 : "g" ((USItype) (a)), \
679 "g" ((USItype) (b)) \
680 : "d0", "d1", "d2", "d3", "d4")
681 #define UMUL_TIME 100
682 #define UDIV_TIME 400
684 #endif /* not mc68020 */
686 /* The '020, '030, '040 and '060 have bitfield insns.
687 cpu32 disguises as a 68020, but lacks them. */
688 #if defined (__mc68020__) && !defined (__mcpu32__)
689 #define count_leading_zeros(count, x) \
690 __asm__ ("bfffo %1{%b2:%b2},%0" \
691 : "=d" ((USItype) (count)) \
692 : "od" ((USItype) (x)), "n" (0))
693 /* Some ColdFire architectures have a ff1 instruction supported via
694 __builtin_clz. */
695 #elif defined (__mcfisaaplus__) || defined (__mcfisac__)
696 #define count_leading_zeros(count,x) ((count) = __builtin_clz (x))
697 #define COUNT_LEADING_ZEROS_0 32
698 #endif
699 #endif /* mc68000 */
701 #if defined (__m88000__) && W_TYPE_SIZE == 32
702 #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
703 __asm__ ("addu.co %1,%r4,%r5\n\taddu.ci %0,%r2,%r3" \
704 : "=r" ((USItype) (sh)), \
705 "=&r" ((USItype) (sl)) \
706 : "%rJ" ((USItype) (ah)), \
707 "rJ" ((USItype) (bh)), \
708 "%rJ" ((USItype) (al)), \
709 "rJ" ((USItype) (bl)))
710 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
711 __asm__ ("subu.co %1,%r4,%r5\n\tsubu.ci %0,%r2,%r3" \
712 : "=r" ((USItype) (sh)), \
713 "=&r" ((USItype) (sl)) \
714 : "rJ" ((USItype) (ah)), \
715 "rJ" ((USItype) (bh)), \
716 "rJ" ((USItype) (al)), \
717 "rJ" ((USItype) (bl)))
718 #define count_leading_zeros(count, x) \
719 do { \
720 USItype __cbtmp; \
721 __asm__ ("ff1 %0,%1" \
722 : "=r" (__cbtmp) \
723 : "r" ((USItype) (x))); \
724 (count) = __cbtmp ^ 31; \
725 } while (0)
726 #define COUNT_LEADING_ZEROS_0 63 /* sic */
727 #if defined (__mc88110__)
728 #define umul_ppmm(wh, wl, u, v) \
729 do { \
730 union {UDItype __ll; \
731 struct {USItype __h, __l;} __i; \
732 } __xx; \
733 __asm__ ("mulu.d %0,%1,%2" \
734 : "=r" (__xx.__ll) \
735 : "r" ((USItype) (u)), \
736 "r" ((USItype) (v))); \
737 (wh) = __xx.__i.__h; \
738 (wl) = __xx.__i.__l; \
739 } while (0)
740 #define udiv_qrnnd(q, r, n1, n0, d) \
741 ({union {UDItype __ll; \
742 struct {USItype __h, __l;} __i; \
743 } __xx; \
744 USItype __q; \
745 __xx.__i.__h = (n1); __xx.__i.__l = (n0); \
746 __asm__ ("divu.d %0,%1,%2" \
747 : "=r" (__q) \
748 : "r" (__xx.__ll), \
749 "r" ((USItype) (d))); \
750 (r) = (n0) - __q * (d); (q) = __q; })
751 #define UMUL_TIME 5
752 #define UDIV_TIME 25
753 #else
754 #define UMUL_TIME 17
755 #define UDIV_TIME 150
756 #endif /* __mc88110__ */
757 #endif /* __m88000__ */
759 #if defined (__mn10300__)
760 # if defined (__AM33__)
761 # define count_leading_zeros(COUNT,X) ((COUNT) = __builtin_clz (X))
762 # define umul_ppmm(w1, w0, u, v) \
763 asm("mulu %3,%2,%1,%0" : "=r"(w0), "=r"(w1) : "r"(u), "r"(v))
764 # define smul_ppmm(w1, w0, u, v) \
765 asm("mul %3,%2,%1,%0" : "=r"(w0), "=r"(w1) : "r"(u), "r"(v))
766 # else
767 # define umul_ppmm(w1, w0, u, v) \
768 asm("nop; nop; mulu %3,%0" : "=d"(w0), "=z"(w1) : "%0"(u), "d"(v))
769 # define smul_ppmm(w1, w0, u, v) \
770 asm("nop; nop; mul %3,%0" : "=d"(w0), "=z"(w1) : "%0"(u), "d"(v))
771 # endif
772 # define add_ssaaaa(sh, sl, ah, al, bh, bl) \
773 do { \
774 DWunion __s, __a, __b; \
775 __a.s.low = (al); __a.s.high = (ah); \
776 __b.s.low = (bl); __b.s.high = (bh); \
777 __s.ll = __a.ll + __b.ll; \
778 (sl) = __s.s.low; (sh) = __s.s.high; \
779 } while (0)
780 # define sub_ddmmss(sh, sl, ah, al, bh, bl) \
781 do { \
782 DWunion __s, __a, __b; \
783 __a.s.low = (al); __a.s.high = (ah); \
784 __b.s.low = (bl); __b.s.high = (bh); \
785 __s.ll = __a.ll - __b.ll; \
786 (sl) = __s.s.low; (sh) = __s.s.high; \
787 } while (0)
788 # define udiv_qrnnd(q, r, nh, nl, d) \
789 asm("divu %2,%0" : "=D"(q), "=z"(r) : "D"(d), "0"(nl), "1"(nh))
790 # define sdiv_qrnnd(q, r, nh, nl, d) \
791 asm("div %2,%0" : "=D"(q), "=z"(r) : "D"(d), "0"(nl), "1"(nh))
792 # define UMUL_TIME 3
793 # define UDIV_TIME 38
794 #endif
796 #if defined (__mips__) && W_TYPE_SIZE == 32
797 #define umul_ppmm(w1, w0, u, v) \
798 do { \
799 UDItype __x = (UDItype) (USItype) (u) * (USItype) (v); \
800 (w1) = (USItype) (__x >> 32); \
801 (w0) = (USItype) (__x); \
802 } while (0)
803 #define UMUL_TIME 10
804 #define UDIV_TIME 100
806 #if (__mips == 32 || __mips == 64) && ! __mips16
807 #define count_leading_zeros(COUNT,X) ((COUNT) = __builtin_clz (X))
808 #define COUNT_LEADING_ZEROS_0 32
809 #endif
810 #endif /* __mips__ */
812 #if defined (__ns32000__) && W_TYPE_SIZE == 32
813 #define umul_ppmm(w1, w0, u, v) \
814 ({union {UDItype __ll; \
815 struct {USItype __l, __h;} __i; \
816 } __xx; \
817 __asm__ ("meid %2,%0" \
818 : "=g" (__xx.__ll) \
819 : "%0" ((USItype) (u)), \
820 "g" ((USItype) (v))); \
821 (w1) = __xx.__i.__h; (w0) = __xx.__i.__l;})
822 #define __umulsidi3(u, v) \
823 ({UDItype __w; \
824 __asm__ ("meid %2,%0" \
825 : "=g" (__w) \
826 : "%0" ((USItype) (u)), \
827 "g" ((USItype) (v))); \
828 __w; })
829 #define udiv_qrnnd(q, r, n1, n0, d) \
830 ({union {UDItype __ll; \
831 struct {USItype __l, __h;} __i; \
832 } __xx; \
833 __xx.__i.__h = (n1); __xx.__i.__l = (n0); \
834 __asm__ ("deid %2,%0" \
835 : "=g" (__xx.__ll) \
836 : "0" (__xx.__ll), \
837 "g" ((USItype) (d))); \
838 (r) = __xx.__i.__l; (q) = __xx.__i.__h; })
839 #define count_trailing_zeros(count,x) \
840 do { \
841 __asm__ ("ffsd %2,%0" \
842 : "=r" ((USItype) (count)) \
843 : "0" ((USItype) 0), \
844 "r" ((USItype) (x))); \
845 } while (0)
846 #endif /* __ns32000__ */
848 /* FIXME: We should test _IBMR2 here when we add assembly support for the
849 system vendor compilers.
850 FIXME: What's needed for gcc PowerPC VxWorks? __vxworks__ is not good
851 enough, since that hits ARM and m68k too. */
852 #if (defined (_ARCH_PPC) /* AIX */ \
853 || defined (_ARCH_PWR) /* AIX */ \
854 || defined (_ARCH_COM) /* AIX */ \
855 || defined (__powerpc__) /* gcc */ \
856 || defined (__POWERPC__) /* BEOS */ \
857 || defined (__ppc__) /* Darwin */ \
858 || (defined (PPC) && ! defined (CPU_FAMILY)) /* gcc 2.7.x GNU&SysV */ \
859 || (defined (PPC) && defined (CPU_FAMILY) /* VxWorks */ \
860 && CPU_FAMILY == PPC) \
861 ) && W_TYPE_SIZE == 32
862 #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
863 do { \
864 if (__builtin_constant_p (bh) && (bh) == 0) \
865 __asm__ ("{a%I4|add%I4c} %1,%3,%4\n\t{aze|addze} %0,%2" \
866 : "=r" (sh), "=&r" (sl) : "r" (ah), "%r" (al), "rI" (bl));\
867 else if (__builtin_constant_p (bh) && (bh) == ~(USItype) 0) \
868 __asm__ ("{a%I4|add%I4c} %1,%3,%4\n\t{ame|addme} %0,%2" \
869 : "=r" (sh), "=&r" (sl) : "r" (ah), "%r" (al), "rI" (bl));\
870 else \
871 __asm__ ("{a%I5|add%I5c} %1,%4,%5\n\t{ae|adde} %0,%2,%3" \
872 : "=r" (sh), "=&r" (sl) \
873 : "%r" (ah), "r" (bh), "%r" (al), "rI" (bl)); \
874 } while (0)
875 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
876 do { \
877 if (__builtin_constant_p (ah) && (ah) == 0) \
878 __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{sfze|subfze} %0,%2" \
879 : "=r" (sh), "=&r" (sl) : "r" (bh), "rI" (al), "r" (bl));\
880 else if (__builtin_constant_p (ah) && (ah) == ~(USItype) 0) \
881 __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{sfme|subfme} %0,%2" \
882 : "=r" (sh), "=&r" (sl) : "r" (bh), "rI" (al), "r" (bl));\
883 else if (__builtin_constant_p (bh) && (bh) == 0) \
884 __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{ame|addme} %0,%2" \
885 : "=r" (sh), "=&r" (sl) : "r" (ah), "rI" (al), "r" (bl));\
886 else if (__builtin_constant_p (bh) && (bh) == ~(USItype) 0) \
887 __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{aze|addze} %0,%2" \
888 : "=r" (sh), "=&r" (sl) : "r" (ah), "rI" (al), "r" (bl));\
889 else \
890 __asm__ ("{sf%I4|subf%I4c} %1,%5,%4\n\t{sfe|subfe} %0,%3,%2" \
891 : "=r" (sh), "=&r" (sl) \
892 : "r" (ah), "r" (bh), "rI" (al), "r" (bl)); \
893 } while (0)
894 #define count_leading_zeros(count, x) \
895 __asm__ ("{cntlz|cntlzw} %0,%1" : "=r" (count) : "r" (x))
896 #define COUNT_LEADING_ZEROS_0 32
897 #if defined (_ARCH_PPC) || defined (__powerpc__) || defined (__POWERPC__) \
898 || defined (__ppc__) \
899 || (defined (PPC) && ! defined (CPU_FAMILY)) /* gcc 2.7.x GNU&SysV */ \
900 || (defined (PPC) && defined (CPU_FAMILY) /* VxWorks */ \
901 && CPU_FAMILY == PPC)
902 #define umul_ppmm(ph, pl, m0, m1) \
903 do { \
904 USItype __m0 = (m0), __m1 = (m1); \
905 __asm__ ("mulhwu %0,%1,%2" : "=r" (ph) : "%r" (m0), "r" (m1)); \
906 (pl) = __m0 * __m1; \
907 } while (0)
908 #define UMUL_TIME 15
909 #define smul_ppmm(ph, pl, m0, m1) \
910 do { \
911 SItype __m0 = (m0), __m1 = (m1); \
912 __asm__ ("mulhw %0,%1,%2" : "=r" (ph) : "%r" (m0), "r" (m1)); \
913 (pl) = __m0 * __m1; \
914 } while (0)
915 #define SMUL_TIME 14
916 #define UDIV_TIME 120
917 #elif defined (_ARCH_PWR)
918 #define UMUL_TIME 8
919 #define smul_ppmm(xh, xl, m0, m1) \
920 __asm__ ("mul %0,%2,%3" : "=r" (xh), "=q" (xl) : "r" (m0), "r" (m1))
921 #define SMUL_TIME 4
922 #define sdiv_qrnnd(q, r, nh, nl, d) \
923 __asm__ ("div %0,%2,%4" : "=r" (q), "=q" (r) : "r" (nh), "1" (nl), "r" (d))
924 #define UDIV_TIME 100
925 #endif
926 #endif /* 32-bit POWER architecture variants. */
928 /* We should test _IBMR2 here when we add assembly support for the system
929 vendor compilers. */
930 #if (defined (_ARCH_PPC64) || defined (__powerpc64__)) && W_TYPE_SIZE == 64
931 #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
932 do { \
933 if (__builtin_constant_p (bh) && (bh) == 0) \
934 __asm__ ("{a%I4|add%I4c} %1,%3,%4\n\t{aze|addze} %0,%2" \
935 : "=r" (sh), "=&r" (sl) : "r" (ah), "%r" (al), "rI" (bl));\
936 else if (__builtin_constant_p (bh) && (bh) == ~(UDItype) 0) \
937 __asm__ ("{a%I4|add%I4c} %1,%3,%4\n\t{ame|addme} %0,%2" \
938 : "=r" (sh), "=&r" (sl) : "r" (ah), "%r" (al), "rI" (bl));\
939 else \
940 __asm__ ("{a%I5|add%I5c} %1,%4,%5\n\t{ae|adde} %0,%2,%3" \
941 : "=r" (sh), "=&r" (sl) \
942 : "%r" (ah), "r" (bh), "%r" (al), "rI" (bl)); \
943 } while (0)
944 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
945 do { \
946 if (__builtin_constant_p (ah) && (ah) == 0) \
947 __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{sfze|subfze} %0,%2" \
948 : "=r" (sh), "=&r" (sl) : "r" (bh), "rI" (al), "r" (bl));\
949 else if (__builtin_constant_p (ah) && (ah) == ~(UDItype) 0) \
950 __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{sfme|subfme} %0,%2" \
951 : "=r" (sh), "=&r" (sl) : "r" (bh), "rI" (al), "r" (bl));\
952 else if (__builtin_constant_p (bh) && (bh) == 0) \
953 __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{ame|addme} %0,%2" \
954 : "=r" (sh), "=&r" (sl) : "r" (ah), "rI" (al), "r" (bl));\
955 else if (__builtin_constant_p (bh) && (bh) == ~(UDItype) 0) \
956 __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{aze|addze} %0,%2" \
957 : "=r" (sh), "=&r" (sl) : "r" (ah), "rI" (al), "r" (bl));\
958 else \
959 __asm__ ("{sf%I4|subf%I4c} %1,%5,%4\n\t{sfe|subfe} %0,%3,%2" \
960 : "=r" (sh), "=&r" (sl) \
961 : "r" (ah), "r" (bh), "rI" (al), "r" (bl)); \
962 } while (0)
963 #define count_leading_zeros(count, x) \
964 __asm__ ("cntlzd %0,%1" : "=r" (count) : "r" (x))
965 #define COUNT_LEADING_ZEROS_0 64
966 #define umul_ppmm(ph, pl, m0, m1) \
967 do { \
968 UDItype __m0 = (m0), __m1 = (m1); \
969 __asm__ ("mulhdu %0,%1,%2" : "=r" (ph) : "%r" (m0), "r" (m1)); \
970 (pl) = __m0 * __m1; \
971 } while (0)
972 #define UMUL_TIME 15
973 #define smul_ppmm(ph, pl, m0, m1) \
974 do { \
975 DItype __m0 = (m0), __m1 = (m1); \
976 __asm__ ("mulhd %0,%1,%2" : "=r" (ph) : "%r" (m0), "r" (m1)); \
977 (pl) = __m0 * __m1; \
978 } while (0)
979 #define SMUL_TIME 14 /* ??? */
980 #define UDIV_TIME 120 /* ??? */
981 #endif /* 64-bit PowerPC. */
983 #if defined (__ibm032__) /* RT/ROMP */ && W_TYPE_SIZE == 32
984 #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
985 __asm__ ("a %1,%5\n\tae %0,%3" \
986 : "=r" ((USItype) (sh)), \
987 "=&r" ((USItype) (sl)) \
988 : "%0" ((USItype) (ah)), \
989 "r" ((USItype) (bh)), \
990 "%1" ((USItype) (al)), \
991 "r" ((USItype) (bl)))
992 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
993 __asm__ ("s %1,%5\n\tse %0,%3" \
994 : "=r" ((USItype) (sh)), \
995 "=&r" ((USItype) (sl)) \
996 : "0" ((USItype) (ah)), \
997 "r" ((USItype) (bh)), \
998 "1" ((USItype) (al)), \
999 "r" ((USItype) (bl)))
1000 #define umul_ppmm(ph, pl, m0, m1) \
1001 do { \
1002 USItype __m0 = (m0), __m1 = (m1); \
1003 __asm__ ( \
1004 "s r2,r2\n" \
1005 " mts r10,%2\n" \
1006 " m r2,%3\n" \
1007 " m r2,%3\n" \
1008 " m r2,%3\n" \
1009 " m r2,%3\n" \
1010 " m r2,%3\n" \
1011 " m r2,%3\n" \
1012 " m r2,%3\n" \
1013 " m r2,%3\n" \
1014 " m r2,%3\n" \
1015 " m r2,%3\n" \
1016 " m r2,%3\n" \
1017 " m r2,%3\n" \
1018 " m r2,%3\n" \
1019 " m r2,%3\n" \
1020 " m r2,%3\n" \
1021 " m r2,%3\n" \
1022 " cas %0,r2,r0\n" \
1023 " mfs r10,%1" \
1024 : "=r" ((USItype) (ph)), \
1025 "=r" ((USItype) (pl)) \
1026 : "%r" (__m0), \
1027 "r" (__m1) \
1028 : "r2"); \
1029 (ph) += ((((SItype) __m0 >> 31) & __m1) \
1030 + (((SItype) __m1 >> 31) & __m0)); \
1031 } while (0)
1032 #define UMUL_TIME 20
1033 #define UDIV_TIME 200
1034 #define count_leading_zeros(count, x) \
1035 do { \
1036 if ((x) >= 0x10000) \
1037 __asm__ ("clz %0,%1" \
1038 : "=r" ((USItype) (count)) \
1039 : "r" ((USItype) (x) >> 16)); \
1040 else \
1042 __asm__ ("clz %0,%1" \
1043 : "=r" ((USItype) (count)) \
1044 : "r" ((USItype) (x))); \
1045 (count) += 16; \
1047 } while (0)
1048 #endif
1050 #if defined(__sh__) && !__SHMEDIA__ && W_TYPE_SIZE == 32
1051 #ifndef __sh1__
1052 #define umul_ppmm(w1, w0, u, v) \
1053 __asm__ ( \
1054 "dmulu.l %2,%3\n\tsts%M1 macl,%1\n\tsts%M0 mach,%0" \
1055 : "=r<" ((USItype)(w1)), \
1056 "=r<" ((USItype)(w0)) \
1057 : "r" ((USItype)(u)), \
1058 "r" ((USItype)(v)) \
1059 : "macl", "mach")
1060 #define UMUL_TIME 5
1061 #endif
1063 /* This is the same algorithm as __udiv_qrnnd_c. */
1064 #define UDIV_NEEDS_NORMALIZATION 1
1066 #define udiv_qrnnd(q, r, n1, n0, d) \
1067 do { \
1068 extern UWtype __udiv_qrnnd_16 (UWtype, UWtype) \
1069 __attribute__ ((visibility ("hidden"))); \
1070 /* r0: rn r1: qn */ /* r0: n1 r4: n0 r5: d r6: d1 */ /* r2: __m */ \
1071 __asm__ ( \
1072 "mov%M4 %4,r5\n" \
1073 " swap.w %3,r4\n" \
1074 " swap.w r5,r6\n" \
1075 " jsr @%5\n" \
1076 " shll16 r6\n" \
1077 " swap.w r4,r4\n" \
1078 " jsr @%5\n" \
1079 " swap.w r1,%0\n" \
1080 " or r1,%0" \
1081 : "=r" (q), "=&z" (r) \
1082 : "1" (n1), "r" (n0), "rm" (d), "r" (&__udiv_qrnnd_16) \
1083 : "r1", "r2", "r4", "r5", "r6", "pr", "t"); \
1084 } while (0)
1086 #define UDIV_TIME 80
1088 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
1089 __asm__ ("clrt;subc %5,%1; subc %4,%0" \
1090 : "=r" (sh), "=r" (sl) \
1091 : "0" (ah), "1" (al), "r" (bh), "r" (bl) : "t")
1093 #endif /* __sh__ */
1095 #if defined (__SH5__) && __SHMEDIA__ && W_TYPE_SIZE == 32
1096 #define __umulsidi3(u,v) ((UDItype)(USItype)u*(USItype)v)
1097 #define count_leading_zeros(count, x) \
1098 do \
1100 UDItype x_ = (USItype)(x); \
1101 SItype c_; \
1103 __asm__ ("nsb %1, %0" : "=r" (c_) : "r" (x_)); \
1104 (count) = c_ - 31; \
1106 while (0)
1107 #define COUNT_LEADING_ZEROS_0 32
1108 #endif
1110 #if defined (__sparc__) && !defined (__arch64__) && !defined (__sparcv9) \
1111 && W_TYPE_SIZE == 32
1112 #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
1113 __asm__ ("addcc %r4,%5,%1\n\taddx %r2,%3,%0" \
1114 : "=r" ((USItype) (sh)), \
1115 "=&r" ((USItype) (sl)) \
1116 : "%rJ" ((USItype) (ah)), \
1117 "rI" ((USItype) (bh)), \
1118 "%rJ" ((USItype) (al)), \
1119 "rI" ((USItype) (bl)) \
1120 __CLOBBER_CC)
1121 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
1122 __asm__ ("subcc %r4,%5,%1\n\tsubx %r2,%3,%0" \
1123 : "=r" ((USItype) (sh)), \
1124 "=&r" ((USItype) (sl)) \
1125 : "rJ" ((USItype) (ah)), \
1126 "rI" ((USItype) (bh)), \
1127 "rJ" ((USItype) (al)), \
1128 "rI" ((USItype) (bl)) \
1129 __CLOBBER_CC)
1130 #if defined (__sparc_v8__)
1131 #define umul_ppmm(w1, w0, u, v) \
1132 __asm__ ("umul %2,%3,%1;rd %%y,%0" \
1133 : "=r" ((USItype) (w1)), \
1134 "=r" ((USItype) (w0)) \
1135 : "r" ((USItype) (u)), \
1136 "r" ((USItype) (v)))
1137 #define udiv_qrnnd(__q, __r, __n1, __n0, __d) \
1138 __asm__ ("mov %2,%%y;nop;nop;nop;udiv %3,%4,%0;umul %0,%4,%1;sub %3,%1,%1"\
1139 : "=&r" ((USItype) (__q)), \
1140 "=&r" ((USItype) (__r)) \
1141 : "r" ((USItype) (__n1)), \
1142 "r" ((USItype) (__n0)), \
1143 "r" ((USItype) (__d)))
1144 #else
1145 #if defined (__sparclite__)
1146 /* This has hardware multiply but not divide. It also has two additional
1147 instructions scan (ffs from high bit) and divscc. */
1148 #define umul_ppmm(w1, w0, u, v) \
1149 __asm__ ("umul %2,%3,%1;rd %%y,%0" \
1150 : "=r" ((USItype) (w1)), \
1151 "=r" ((USItype) (w0)) \
1152 : "r" ((USItype) (u)), \
1153 "r" ((USItype) (v)))
1154 #define udiv_qrnnd(q, r, n1, n0, d) \
1155 __asm__ ("! Inlined udiv_qrnnd\n" \
1156 " wr %%g0,%2,%%y ! Not a delayed write for sparclite\n" \
1157 " tst %%g0\n" \
1158 " divscc %3,%4,%%g1\n" \
1159 " divscc %%g1,%4,%%g1\n" \
1160 " divscc %%g1,%4,%%g1\n" \
1161 " divscc %%g1,%4,%%g1\n" \
1162 " divscc %%g1,%4,%%g1\n" \
1163 " divscc %%g1,%4,%%g1\n" \
1164 " divscc %%g1,%4,%%g1\n" \
1165 " divscc %%g1,%4,%%g1\n" \
1166 " divscc %%g1,%4,%%g1\n" \
1167 " divscc %%g1,%4,%%g1\n" \
1168 " divscc %%g1,%4,%%g1\n" \
1169 " divscc %%g1,%4,%%g1\n" \
1170 " divscc %%g1,%4,%%g1\n" \
1171 " divscc %%g1,%4,%%g1\n" \
1172 " divscc %%g1,%4,%%g1\n" \
1173 " divscc %%g1,%4,%%g1\n" \
1174 " divscc %%g1,%4,%%g1\n" \
1175 " divscc %%g1,%4,%%g1\n" \
1176 " divscc %%g1,%4,%%g1\n" \
1177 " divscc %%g1,%4,%%g1\n" \
1178 " divscc %%g1,%4,%%g1\n" \
1179 " divscc %%g1,%4,%%g1\n" \
1180 " divscc %%g1,%4,%%g1\n" \
1181 " divscc %%g1,%4,%%g1\n" \
1182 " divscc %%g1,%4,%%g1\n" \
1183 " divscc %%g1,%4,%%g1\n" \
1184 " divscc %%g1,%4,%%g1\n" \
1185 " divscc %%g1,%4,%%g1\n" \
1186 " divscc %%g1,%4,%%g1\n" \
1187 " divscc %%g1,%4,%%g1\n" \
1188 " divscc %%g1,%4,%%g1\n" \
1189 " divscc %%g1,%4,%0\n" \
1190 " rd %%y,%1\n" \
1191 " bl,a 1f\n" \
1192 " add %1,%4,%1\n" \
1193 "1: ! End of inline udiv_qrnnd" \
1194 : "=r" ((USItype) (q)), \
1195 "=r" ((USItype) (r)) \
1196 : "r" ((USItype) (n1)), \
1197 "r" ((USItype) (n0)), \
1198 "rI" ((USItype) (d)) \
1199 : "g1" __AND_CLOBBER_CC)
1200 #define UDIV_TIME 37
1201 #define count_leading_zeros(count, x) \
1202 do { \
1203 __asm__ ("scan %1,1,%0" \
1204 : "=r" ((USItype) (count)) \
1205 : "r" ((USItype) (x))); \
1206 } while (0)
1207 /* Early sparclites return 63 for an argument of 0, but they warn that future
1208 implementations might change this. Therefore, leave COUNT_LEADING_ZEROS_0
1209 undefined. */
1210 #else
1211 /* SPARC without integer multiplication and divide instructions.
1212 (i.e. at least Sun4/20,40,60,65,75,110,260,280,330,360,380,470,490) */
1213 #define umul_ppmm(w1, w0, u, v) \
1214 __asm__ ("! Inlined umul_ppmm\n" \
1215 " wr %%g0,%2,%%y ! SPARC has 0-3 delay insn after a wr\n"\
1216 " sra %3,31,%%o5 ! Don't move this insn\n" \
1217 " and %2,%%o5,%%o5 ! Don't move this insn\n" \
1218 " andcc %%g0,0,%%g1 ! Don't move this insn\n" \
1219 " mulscc %%g1,%3,%%g1\n" \
1220 " mulscc %%g1,%3,%%g1\n" \
1221 " mulscc %%g1,%3,%%g1\n" \
1222 " mulscc %%g1,%3,%%g1\n" \
1223 " mulscc %%g1,%3,%%g1\n" \
1224 " mulscc %%g1,%3,%%g1\n" \
1225 " mulscc %%g1,%3,%%g1\n" \
1226 " mulscc %%g1,%3,%%g1\n" \
1227 " mulscc %%g1,%3,%%g1\n" \
1228 " mulscc %%g1,%3,%%g1\n" \
1229 " mulscc %%g1,%3,%%g1\n" \
1230 " mulscc %%g1,%3,%%g1\n" \
1231 " mulscc %%g1,%3,%%g1\n" \
1232 " mulscc %%g1,%3,%%g1\n" \
1233 " mulscc %%g1,%3,%%g1\n" \
1234 " mulscc %%g1,%3,%%g1\n" \
1235 " mulscc %%g1,%3,%%g1\n" \
1236 " mulscc %%g1,%3,%%g1\n" \
1237 " mulscc %%g1,%3,%%g1\n" \
1238 " mulscc %%g1,%3,%%g1\n" \
1239 " mulscc %%g1,%3,%%g1\n" \
1240 " mulscc %%g1,%3,%%g1\n" \
1241 " mulscc %%g1,%3,%%g1\n" \
1242 " mulscc %%g1,%3,%%g1\n" \
1243 " mulscc %%g1,%3,%%g1\n" \
1244 " mulscc %%g1,%3,%%g1\n" \
1245 " mulscc %%g1,%3,%%g1\n" \
1246 " mulscc %%g1,%3,%%g1\n" \
1247 " mulscc %%g1,%3,%%g1\n" \
1248 " mulscc %%g1,%3,%%g1\n" \
1249 " mulscc %%g1,%3,%%g1\n" \
1250 " mulscc %%g1,%3,%%g1\n" \
1251 " mulscc %%g1,0,%%g1\n" \
1252 " add %%g1,%%o5,%0\n" \
1253 " rd %%y,%1" \
1254 : "=r" ((USItype) (w1)), \
1255 "=r" ((USItype) (w0)) \
1256 : "%rI" ((USItype) (u)), \
1257 "r" ((USItype) (v)) \
1258 : "g1", "o5" __AND_CLOBBER_CC)
1259 #define UMUL_TIME 39 /* 39 instructions */
1260 /* It's quite necessary to add this much assembler for the sparc.
1261 The default udiv_qrnnd (in C) is more than 10 times slower! */
1262 #define udiv_qrnnd(__q, __r, __n1, __n0, __d) \
1263 __asm__ ("! Inlined udiv_qrnnd\n" \
1264 " mov 32,%%g1\n" \
1265 " subcc %1,%2,%%g0\n" \
1266 "1: bcs 5f\n" \
1267 " addxcc %0,%0,%0 ! shift n1n0 and a q-bit in lsb\n" \
1268 " sub %1,%2,%1 ! this kills msb of n\n" \
1269 " addx %1,%1,%1 ! so this can't give carry\n" \
1270 " subcc %%g1,1,%%g1\n" \
1271 "2: bne 1b\n" \
1272 " subcc %1,%2,%%g0\n" \
1273 " bcs 3f\n" \
1274 " addxcc %0,%0,%0 ! shift n1n0 and a q-bit in lsb\n" \
1275 " b 3f\n" \
1276 " sub %1,%2,%1 ! this kills msb of n\n" \
1277 "4: sub %1,%2,%1\n" \
1278 "5: addxcc %1,%1,%1\n" \
1279 " bcc 2b\n" \
1280 " subcc %%g1,1,%%g1\n" \
1281 "! Got carry from n. Subtract next step to cancel this carry.\n" \
1282 " bne 4b\n" \
1283 " addcc %0,%0,%0 ! shift n1n0 and a 0-bit in lsb\n" \
1284 " sub %1,%2,%1\n" \
1285 "3: xnor %0,0,%0\n" \
1286 " ! End of inline udiv_qrnnd" \
1287 : "=&r" ((USItype) (__q)), \
1288 "=&r" ((USItype) (__r)) \
1289 : "r" ((USItype) (__d)), \
1290 "1" ((USItype) (__n1)), \
1291 "0" ((USItype) (__n0)) : "g1" __AND_CLOBBER_CC)
1292 #define UDIV_TIME (3+7*32) /* 7 instructions/iteration. 32 iterations. */
1293 #endif /* __sparclite__ */
1294 #endif /* __sparc_v8__ */
1295 #endif /* sparc32 */
1297 #if ((defined (__sparc__) && defined (__arch64__)) || defined (__sparcv9)) \
1298 && W_TYPE_SIZE == 64
1299 #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
1300 __asm__ ("addcc %r4,%5,%1\n\t" \
1301 "add %r2,%3,%0\n\t" \
1302 "bcs,a,pn %%xcc, 1f\n\t" \
1303 "add %0, 1, %0\n" \
1304 "1:" \
1305 : "=r" ((UDItype)(sh)), \
1306 "=&r" ((UDItype)(sl)) \
1307 : "%rJ" ((UDItype)(ah)), \
1308 "rI" ((UDItype)(bh)), \
1309 "%rJ" ((UDItype)(al)), \
1310 "rI" ((UDItype)(bl)) \
1311 __CLOBBER_CC)
1313 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
1314 __asm__ ("subcc %r4,%5,%1\n\t" \
1315 "sub %r2,%3,%0\n\t" \
1316 "bcs,a,pn %%xcc, 1f\n\t" \
1317 "sub %0, 1, %0\n\t" \
1318 "1:" \
1319 : "=r" ((UDItype)(sh)), \
1320 "=&r" ((UDItype)(sl)) \
1321 : "rJ" ((UDItype)(ah)), \
1322 "rI" ((UDItype)(bh)), \
1323 "rJ" ((UDItype)(al)), \
1324 "rI" ((UDItype)(bl)) \
1325 __CLOBBER_CC)
1327 #define umul_ppmm(wh, wl, u, v) \
1328 do { \
1329 UDItype tmp1, tmp2, tmp3, tmp4; \
1330 __asm__ __volatile__ ( \
1331 "srl %7,0,%3\n\t" \
1332 "mulx %3,%6,%1\n\t" \
1333 "srlx %6,32,%2\n\t" \
1334 "mulx %2,%3,%4\n\t" \
1335 "sllx %4,32,%5\n\t" \
1336 "srl %6,0,%3\n\t" \
1337 "sub %1,%5,%5\n\t" \
1338 "srlx %5,32,%5\n\t" \
1339 "addcc %4,%5,%4\n\t" \
1340 "srlx %7,32,%5\n\t" \
1341 "mulx %3,%5,%3\n\t" \
1342 "mulx %2,%5,%5\n\t" \
1343 "sethi %%hi(0x80000000),%2\n\t" \
1344 "addcc %4,%3,%4\n\t" \
1345 "srlx %4,32,%4\n\t" \
1346 "add %2,%2,%2\n\t" \
1347 "movcc %%xcc,%%g0,%2\n\t" \
1348 "addcc %5,%4,%5\n\t" \
1349 "sllx %3,32,%3\n\t" \
1350 "add %1,%3,%1\n\t" \
1351 "add %5,%2,%0" \
1352 : "=r" ((UDItype)(wh)), \
1353 "=&r" ((UDItype)(wl)), \
1354 "=&r" (tmp1), "=&r" (tmp2), "=&r" (tmp3), "=&r" (tmp4) \
1355 : "r" ((UDItype)(u)), \
1356 "r" ((UDItype)(v)) \
1357 __CLOBBER_CC); \
1358 } while (0)
1359 #define UMUL_TIME 96
1360 #define UDIV_TIME 230
1361 #endif /* sparc64 */
1363 #if defined (__vax__) && W_TYPE_SIZE == 32
1364 #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
1365 __asm__ ("addl2 %5,%1\n\tadwc %3,%0" \
1366 : "=g" ((USItype) (sh)), \
1367 "=&g" ((USItype) (sl)) \
1368 : "%0" ((USItype) (ah)), \
1369 "g" ((USItype) (bh)), \
1370 "%1" ((USItype) (al)), \
1371 "g" ((USItype) (bl)))
1372 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
1373 __asm__ ("subl2 %5,%1\n\tsbwc %3,%0" \
1374 : "=g" ((USItype) (sh)), \
1375 "=&g" ((USItype) (sl)) \
1376 : "0" ((USItype) (ah)), \
1377 "g" ((USItype) (bh)), \
1378 "1" ((USItype) (al)), \
1379 "g" ((USItype) (bl)))
1380 #define umul_ppmm(xh, xl, m0, m1) \
1381 do { \
1382 union { \
1383 UDItype __ll; \
1384 struct {USItype __l, __h;} __i; \
1385 } __xx; \
1386 USItype __m0 = (m0), __m1 = (m1); \
1387 __asm__ ("emul %1,%2,$0,%0" \
1388 : "=r" (__xx.__ll) \
1389 : "g" (__m0), \
1390 "g" (__m1)); \
1391 (xh) = __xx.__i.__h; \
1392 (xl) = __xx.__i.__l; \
1393 (xh) += ((((SItype) __m0 >> 31) & __m1) \
1394 + (((SItype) __m1 >> 31) & __m0)); \
1395 } while (0)
1396 #define sdiv_qrnnd(q, r, n1, n0, d) \
1397 do { \
1398 union {DItype __ll; \
1399 struct {SItype __l, __h;} __i; \
1400 } __xx; \
1401 __xx.__i.__h = n1; __xx.__i.__l = n0; \
1402 __asm__ ("ediv %3,%2,%0,%1" \
1403 : "=g" (q), "=g" (r) \
1404 : "g" (__xx.__ll), "g" (d)); \
1405 } while (0)
1406 #endif /* __vax__ */
1408 #ifdef _TMS320C6X
1409 #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
1410 do \
1412 UDItype __ll; \
1413 __asm__ ("addu .l1 %1, %2, %0" \
1414 : "=a" (__ll) : "a" (al), "a" (bl)); \
1415 (sl) = (USItype)__ll; \
1416 (sh) = ((USItype)(__ll >> 32)) + (ah) + (bh); \
1418 while (0)
1420 #ifdef _TMS320C6400_PLUS
1421 #define __umulsidi3(u,v) ((UDItype)(USItype)u*(USItype)v)
1422 #define umul_ppmm(w1, w0, u, v) \
1423 do { \
1424 UDItype __x = (UDItype) (USItype) (u) * (USItype) (v); \
1425 (w1) = (USItype) (__x >> 32); \
1426 (w0) = (USItype) (__x); \
1427 } while (0)
1428 #endif /* _TMS320C6400_PLUS */
1430 #define count_leading_zeros(count, x) ((count) = __builtin_clz (x))
1431 #ifdef _TMS320C6400
1432 #define count_trailing_zeros(count, x) ((count) = __builtin_ctz (x))
1433 #endif
1434 #define UMUL_TIME 4
1435 #define UDIV_TIME 40
1436 #endif /* _TMS320C6X */
1438 #if defined (__xtensa__) && W_TYPE_SIZE == 32
1439 /* This code is not Xtensa-configuration-specific, so rely on the compiler
1440 to expand builtin functions depending on what configuration features
1441 are available. This avoids library calls when the operation can be
1442 performed in-line. */
1443 #define umul_ppmm(w1, w0, u, v) \
1444 do { \
1445 DWunion __w; \
1446 __w.ll = __builtin_umulsidi3 (u, v); \
1447 w1 = __w.s.high; \
1448 w0 = __w.s.low; \
1449 } while (0)
1450 #define __umulsidi3(u, v) __builtin_umulsidi3 (u, v)
1451 #define count_leading_zeros(COUNT, X) ((COUNT) = __builtin_clz (X))
1452 #define count_trailing_zeros(COUNT, X) ((COUNT) = __builtin_ctz (X))
1453 #endif /* __xtensa__ */
1455 #if defined xstormy16
1456 extern UHItype __stormy16_count_leading_zeros (UHItype);
1457 #define count_leading_zeros(count, x) \
1458 do \
1460 UHItype size; \
1462 /* We assume that W_TYPE_SIZE is a multiple of 16... */ \
1463 for ((count) = 0, size = W_TYPE_SIZE; size; size -= 16) \
1465 UHItype c; \
1467 c = __clzhi2 ((x) >> (size - 16)); \
1468 (count) += c; \
1469 if (c != 16) \
1470 break; \
1473 while (0)
1474 #define COUNT_LEADING_ZEROS_0 W_TYPE_SIZE
1475 #endif
1477 #if defined (__z8000__) && W_TYPE_SIZE == 16
1478 #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
1479 __asm__ ("add %H1,%H5\n\tadc %H0,%H3" \
1480 : "=r" ((unsigned int)(sh)), \
1481 "=&r" ((unsigned int)(sl)) \
1482 : "%0" ((unsigned int)(ah)), \
1483 "r" ((unsigned int)(bh)), \
1484 "%1" ((unsigned int)(al)), \
1485 "rQR" ((unsigned int)(bl)))
1486 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
1487 __asm__ ("sub %H1,%H5\n\tsbc %H0,%H3" \
1488 : "=r" ((unsigned int)(sh)), \
1489 "=&r" ((unsigned int)(sl)) \
1490 : "0" ((unsigned int)(ah)), \
1491 "r" ((unsigned int)(bh)), \
1492 "1" ((unsigned int)(al)), \
1493 "rQR" ((unsigned int)(bl)))
1494 #define umul_ppmm(xh, xl, m0, m1) \
1495 do { \
1496 union {long int __ll; \
1497 struct {unsigned int __h, __l;} __i; \
1498 } __xx; \
1499 unsigned int __m0 = (m0), __m1 = (m1); \
1500 __asm__ ("mult %S0,%H3" \
1501 : "=r" (__xx.__i.__h), \
1502 "=r" (__xx.__i.__l) \
1503 : "%1" (__m0), \
1504 "rQR" (__m1)); \
1505 (xh) = __xx.__i.__h; (xl) = __xx.__i.__l; \
1506 (xh) += ((((signed int) __m0 >> 15) & __m1) \
1507 + (((signed int) __m1 >> 15) & __m0)); \
1508 } while (0)
1509 #endif /* __z8000__ */
1511 #endif /* __GNUC__ */
1513 /* If this machine has no inline assembler, use C macros. */
1515 #if !defined (add_ssaaaa)
1516 #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
1517 do { \
1518 UWtype __x; \
1519 __x = (al) + (bl); \
1520 (sh) = (ah) + (bh) + (__x < (al)); \
1521 (sl) = __x; \
1522 } while (0)
1523 #endif
1525 #if !defined (sub_ddmmss)
1526 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
1527 do { \
1528 UWtype __x; \
1529 __x = (al) - (bl); \
1530 (sh) = (ah) - (bh) - (__x > (al)); \
1531 (sl) = __x; \
1532 } while (0)
1533 #endif
1535 /* If we lack umul_ppmm but have smul_ppmm, define umul_ppmm in terms of
1536 smul_ppmm. */
1537 #if !defined (umul_ppmm) && defined (smul_ppmm)
1538 #define umul_ppmm(w1, w0, u, v) \
1539 do { \
1540 UWtype __w1; \
1541 UWtype __xm0 = (u), __xm1 = (v); \
1542 smul_ppmm (__w1, w0, __xm0, __xm1); \
1543 (w1) = __w1 + (-(__xm0 >> (W_TYPE_SIZE - 1)) & __xm1) \
1544 + (-(__xm1 >> (W_TYPE_SIZE - 1)) & __xm0); \
1545 } while (0)
1546 #endif
1548 /* If we still don't have umul_ppmm, define it using plain C. */
1549 #if !defined (umul_ppmm)
1550 #define umul_ppmm(w1, w0, u, v) \
1551 do { \
1552 UWtype __x0, __x1, __x2, __x3; \
1553 UHWtype __ul, __vl, __uh, __vh; \
1555 __ul = __ll_lowpart (u); \
1556 __uh = __ll_highpart (u); \
1557 __vl = __ll_lowpart (v); \
1558 __vh = __ll_highpart (v); \
1560 __x0 = (UWtype) __ul * __vl; \
1561 __x1 = (UWtype) __ul * __vh; \
1562 __x2 = (UWtype) __uh * __vl; \
1563 __x3 = (UWtype) __uh * __vh; \
1565 __x1 += __ll_highpart (__x0);/* this can't give carry */ \
1566 __x1 += __x2; /* but this indeed can */ \
1567 if (__x1 < __x2) /* did we get it? */ \
1568 __x3 += __ll_B; /* yes, add it in the proper pos. */ \
1570 (w1) = __x3 + __ll_highpart (__x1); \
1571 (w0) = __ll_lowpart (__x1) * __ll_B + __ll_lowpart (__x0); \
1572 } while (0)
1573 #endif
1575 #if !defined (__umulsidi3)
1576 #define __umulsidi3(u, v) \
1577 ({DWunion __w; \
1578 umul_ppmm (__w.s.high, __w.s.low, u, v); \
1579 __w.ll; })
1580 #endif
1582 /* Define this unconditionally, so it can be used for debugging. */
1583 #define __udiv_qrnnd_c(q, r, n1, n0, d) \
1584 do { \
1585 UWtype __d1, __d0, __q1, __q0; \
1586 UWtype __r1, __r0, __m; \
1587 __d1 = __ll_highpart (d); \
1588 __d0 = __ll_lowpart (d); \
1590 __r1 = (n1) % __d1; \
1591 __q1 = (n1) / __d1; \
1592 __m = (UWtype) __q1 * __d0; \
1593 __r1 = __r1 * __ll_B | __ll_highpart (n0); \
1594 if (__r1 < __m) \
1596 __q1--, __r1 += (d); \
1597 if (__r1 >= (d)) /* i.e. we didn't get carry when adding to __r1 */\
1598 if (__r1 < __m) \
1599 __q1--, __r1 += (d); \
1601 __r1 -= __m; \
1603 __r0 = __r1 % __d1; \
1604 __q0 = __r1 / __d1; \
1605 __m = (UWtype) __q0 * __d0; \
1606 __r0 = __r0 * __ll_B | __ll_lowpart (n0); \
1607 if (__r0 < __m) \
1609 __q0--, __r0 += (d); \
1610 if (__r0 >= (d)) \
1611 if (__r0 < __m) \
1612 __q0--, __r0 += (d); \
1614 __r0 -= __m; \
1616 (q) = (UWtype) __q1 * __ll_B | __q0; \
1617 (r) = __r0; \
1618 } while (0)
1620 /* If the processor has no udiv_qrnnd but sdiv_qrnnd, go through
1621 __udiv_w_sdiv (defined in libgcc or elsewhere). */
1622 #if !defined (udiv_qrnnd) && defined (sdiv_qrnnd)
1623 #define udiv_qrnnd(q, r, nh, nl, d) \
1624 do { \
1625 USItype __r; \
1626 (q) = __udiv_w_sdiv (&__r, nh, nl, d); \
1627 (r) = __r; \
1628 } while (0)
1629 #endif
1631 /* If udiv_qrnnd was not defined for this processor, use __udiv_qrnnd_c. */
1632 #if !defined (udiv_qrnnd)
1633 #define UDIV_NEEDS_NORMALIZATION 1
1634 #define udiv_qrnnd __udiv_qrnnd_c
1635 #endif
1637 #if !defined (count_leading_zeros)
1638 #define count_leading_zeros(count, x) \
1639 do { \
1640 UWtype __xr = (x); \
1641 UWtype __a; \
1643 if (W_TYPE_SIZE <= 32) \
1645 __a = __xr < ((UWtype)1<<2*__BITS4) \
1646 ? (__xr < ((UWtype)1<<__BITS4) ? 0 : __BITS4) \
1647 : (__xr < ((UWtype)1<<3*__BITS4) ? 2*__BITS4 : 3*__BITS4); \
1649 else \
1651 for (__a = W_TYPE_SIZE - 8; __a > 0; __a -= 8) \
1652 if (((__xr >> __a) & 0xff) != 0) \
1653 break; \
1656 (count) = W_TYPE_SIZE - (__clz_tab[__xr >> __a] + __a); \
1657 } while (0)
1658 #define COUNT_LEADING_ZEROS_0 W_TYPE_SIZE
1659 #endif
1661 #if !defined (count_trailing_zeros)
1662 /* Define count_trailing_zeros using count_leading_zeros. The latter might be
1663 defined in asm, but if it is not, the C version above is good enough. */
1664 #define count_trailing_zeros(count, x) \
1665 do { \
1666 UWtype __ctz_x = (x); \
1667 UWtype __ctz_c; \
1668 count_leading_zeros (__ctz_c, __ctz_x & -__ctz_x); \
1669 (count) = W_TYPE_SIZE - 1 - __ctz_c; \
1670 } while (0)
1671 #endif
1673 #ifndef UDIV_NEEDS_NORMALIZATION
1674 #define UDIV_NEEDS_NORMALIZATION 0
1675 #endif