db98e473c629dc27505389b72519a6d6b64e45f9
[libgcrypt.git] / mpi / longlong.h
1 /* longlong.h -- definitions for mixed size 32/64 bit arithmetic.
2    Note: This is the Libgcrypt version
3
4
5 Copyright (C) 1991, 1992, 1993, 1994, 1996, 1998,
6               2000, 2001, 2002, 2003, 2004, 2011 Free Software Foundation, Inc.
7
8 This file is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or (at your
11 option) any later version.
12
13 This file is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16 License for more details.
17
18 You should have received a copy of the GNU Library General Public License
19 along with this file; see the file COPYING.LIB.  If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 MA 02111-1307, USA. */
22
23 /* You have to define the following before including this file:
24
25    UWtype -- An unsigned type, default type for operations (typically a "word")
26    UHWtype -- An unsigned type, at least half the size of UWtype.
27    UDWtype -- An unsigned type, at least twice as large a UWtype
28    W_TYPE_SIZE -- size in bits of UWtype
29
30    SItype, USItype -- Signed and unsigned 32 bit types.
31    DItype, UDItype -- Signed and unsigned 64 bit types.
32
33    On a 32 bit machine UWtype should typically be USItype;
34    on a 64 bit machine, UWtype should typically be UDItype.
35 */
36
37 #define __BITS4 (W_TYPE_SIZE / 4)
38 #define __ll_B ((UWtype) 1 << (W_TYPE_SIZE / 2))
39 #define __ll_lowpart(t) ((UWtype) (t) & (__ll_B - 1))
40 #define __ll_highpart(t) ((UWtype) (t) >> (W_TYPE_SIZE / 2))
41
42 /* This is used to make sure no undesirable sharing between different libraries
43    that use this file takes place.  */
44 #ifndef __MPN
45 # define __MPN(x) __##x
46 #endif
47
48 /* Define auxiliary asm macros.
49
50    1) umul_ppmm(high_prod, low_prod, multipler, multiplicand) multiplies two
51    UWtype integers MULTIPLER and MULTIPLICAND, and generates a two UWtype
52    word product in HIGH_PROD and LOW_PROD.
53
54    2) __umulsidi3(a,b) multiplies two UWtype integers A and B, and returns a
55    UDWtype product.  This is just a variant of umul_ppmm.
56
57    3) udiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
58    denominator) divides a UDWtype, composed by the UWtype integers
59    HIGH_NUMERATOR and LOW_NUMERATOR, by DENOMINATOR and places the quotient
60    in QUOTIENT and the remainder in REMAINDER.  HIGH_NUMERATOR must be less
61    than DENOMINATOR for correct operation.  If, in addition, the most
62    significant bit of DENOMINATOR must be 1, then the pre-processor symbol
63    UDIV_NEEDS_NORMALIZATION is defined to 1.
64
65    4) sdiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
66    denominator).  Like udiv_qrnnd but the numbers are signed.  The quotient
67    is rounded towards 0.
68
69    5) count_leading_zeros(count, x) counts the number of zero-bits from the
70    msb to the first non-zero bit in the UWtype X.  This is the number of
71    steps X needs to be shifted left to set the msb.  Undefined for X == 0,
72    unless the symbol COUNT_LEADING_ZEROS_0 is defined to some value.
73
74    6) count_trailing_zeros(count, x) like count_leading_zeros, but counts
75    from the least significant end.
76
77    7) add_ssaaaa(high_sum, low_sum, high_addend_1, low_addend_1,
78    high_addend_2, low_addend_2) adds two UWtype integers, composed by
79    HIGH_ADDEND_1 and LOW_ADDEND_1, and HIGH_ADDEND_2 and LOW_ADDEND_2
80    respectively.  The result is placed in HIGH_SUM and LOW_SUM.  Overflow
81    (i.e. carry out) is not stored anywhere, and is lost.
82
83    8) sub_ddmmss(high_difference, low_difference, high_minuend, low_minuend,
84    high_subtrahend, low_subtrahend) subtracts two two-word UWtype integers,
85    composed by HIGH_MINUEND_1 and LOW_MINUEND_1, and HIGH_SUBTRAHEND_2 and
86    LOW_SUBTRAHEND_2 respectively.  The result is placed in HIGH_DIFFERENCE
87    and LOW_DIFFERENCE.  Overflow (i.e. carry out) is not stored anywhere,
88    and is lost.
89
90    If any of these macros are left undefined for a particular CPU,
91    C macros are used.  */
92
93 /* The CPUs come in alphabetical order below.
94
95    Please add support for more CPUs here, or improve the current support
96    for the CPUs below!  */
97
98 #ifdef __riscos__
99 #pragma continue_after_hash_error
100 #else /* !__riscos__ */
101 #if defined (__GNUC__) && !defined (NO_ASM)
102
103 /* We sometimes need to clobber "cc" with gcc2, but that would not be
104    understood by gcc1.  Use cpp to avoid major code duplication.  */
105 #if __GNUC__ < 2
106 # define __CLOBBER_CC
107 # define __AND_CLOBBER_CC
108 #else /* __GNUC__ >= 2 */
109 # define __CLOBBER_CC : "cc"
110 # define __AND_CLOBBER_CC , "cc"
111 #endif /* __GNUC__ < 2 */
112
113 /***************************************
114  ****  Begin CPU Specific Versions  ****
115  ***************************************/
116
117 /***************************************
118  **************  A29K  *****************
119  ***************************************/
120 #if (defined (__a29k__) || defined (_AM29K)) && W_TYPE_SIZE == 32
121 # define add_ssaaaa(sh, sl, ah, al, bh, bl) \
122   __asm__ ("add %1,%4,%5\n"   \
123            "addc %0,%2,%3"                                              \
124            : "=r" ((USItype)(sh)),                                      \
125             "=&r" ((USItype)(sl))                                       \
126            : "%r" ((USItype)(ah)),                                      \
127              "rI" ((USItype)(bh)),                                      \
128              "%r" ((USItype)(al)),                                      \
129              "rI" ((USItype)(bl)))
130 # define sub_ddmmss(sh, sl, ah, al, bh, bl) \
131   __asm__ ("sub %1,%4,%5\n"                                             \
132            "subc %0,%2,%3"                                              \
133            : "=r" ((USItype)(sh)),                                      \
134              "=&r" ((USItype)(sl))                                      \
135            : "r" ((USItype)(ah)),                                       \
136              "rI" ((USItype)(bh)),                                      \
137              "r" ((USItype)(al)),                                       \
138              "rI" ((USItype)(bl)))
139 # define umul_ppmm(xh, xl, m0, m1) \
140   do {                                                                  \
141     USItype __m0 = (m0), __m1 = (m1);                                   \
142     __asm__ ("multiplu %0,%1,%2"                                        \
143              : "=r" ((USItype)(xl))                                     \
144              : "r" (__m0),                                              \
145                "r" (__m1));                                             \
146     __asm__ ("multmu %0,%1,%2"                                          \
147              : "=r" ((USItype)(xh))                                     \
148              : "r" (__m0),                                              \
149                "r" (__m1));                                             \
150   } while (0)
151 # define udiv_qrnnd(q, r, n1, n0, d) \
152   __asm__ ("dividu %0,%3,%4"                                            \
153            : "=r" ((USItype)(q)),                                       \
154              "=q" ((USItype)(r))                                        \
155            : "1" ((USItype)(n1)),                                       \
156              "r" ((USItype)(n0)),                                       \
157              "r" ((USItype)(d)))
158 # define count_leading_zeros(count, x) \
159     __asm__ ("clz %0,%1"                                                \
160              : "=r" ((USItype)(count))                                  \
161              : "r" ((USItype)(x)))
162 # define COUNT_LEADING_ZEROS_0 32
163 #endif /* __a29k__ */
164
165
166 #if defined (__alpha) && W_TYPE_SIZE == 64
167 # define umul_ppmm(ph, pl, m0, m1) \
168   do {                                                                  \
169     UDItype __m0 = (m0), __m1 = (m1);                                   \
170     __asm__ ("umulh %r1,%2,%0"                                          \
171              : "=r" ((UDItype) ph)                                      \
172              : "%rJ" (__m0),                                            \
173                "rI" (__m1));                                            \
174     (pl) = __m0 * __m1;                                                 \
175   } while (0)
176 # define UMUL_TIME 46
177 # ifndef LONGLONG_STANDALONE
178 #  define udiv_qrnnd(q, r, n1, n0, d) \
179   do { UDItype __r;                                                     \
180     (q) = __udiv_qrnnd (&__r, (n1), (n0), (d));                         \
181     (r) = __r;                                                          \
182   } while (0)
183 extern UDItype __udiv_qrnnd ();
184 #  define UDIV_TIME 220
185 # endif /* !LONGLONG_STANDALONE */
186 #endif /* __alpha */
187
188 /***************************************
189  **************  ARM  ******************
190  ***************************************/
191 #if defined (__arm__) && W_TYPE_SIZE == 32 && \
192     (!defined (__thumb__) || defined (__thumb2__))
193 /* The __ARM_ARCH define is provided by gcc 4.8.  Construct it otherwise.  */
194 # ifndef __ARM_ARCH
195 #  ifdef __ARM_ARCH_2__
196 #   define __ARM_ARCH 2
197 #  elif defined (__ARM_ARCH_3__) || defined (__ARM_ARCH_3M__)
198 #   define __ARM_ARCH 3
199 #  elif defined (__ARM_ARCH_4__) || defined (__ARM_ARCH_4T__)
200 #   define __ARM_ARCH 4
201 #  elif defined (__ARM_ARCH_5__) || defined (__ARM_ARCH_5E__) \
202         || defined(__ARM_ARCH_5T__) || defined(__ARM_ARCH_5TE__) \
203         || defined(__ARM_ARCH_5TEJ__)
204 #   define __ARM_ARCH 5
205 #  elif defined (__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) \
206         || defined (__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) \
207         || defined (__ARM_ARCH_6K__) || defined(__ARM_ARCH_6T2__)
208 #   define __ARM_ARCH 6
209 #  elif defined (__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) \
210         || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) \
211         || defined(__ARM_ARCH_7EM__)
212 #   define __ARM_ARCH 7
213 #  else
214    /* could not detect? */
215 #  endif
216 # endif /* !__ARM_ARCH */
217
218 # define add_ssaaaa(sh, sl, ah, al, bh, bl) \
219   __asm__ ("adds %1, %4, %5\n"                                          \
220            "adc  %0, %2, %3"                                            \
221            : "=r" ((sh)),                                               \
222              "=&r" ((sl))                                               \
223            : "%r" ((USItype)(ah)),                                      \
224              "rI" ((USItype)(bh)),                                      \
225              "%r" ((USItype)(al)),                                      \
226              "rI" ((USItype)(bl)) __CLOBBER_CC)
227 # define sub_ddmmss(sh, sl, ah, al, bh, bl) \
228   __asm__ ("subs %1, %4, %5\n"                                          \
229            "sbc  %0, %2, %3"                                            \
230            : "=r" ((sh)),                                               \
231              "=&r" ((sl))                                               \
232            : "r" ((USItype)(ah)),                                       \
233              "rI" ((USItype)(bh)),                                      \
234              "r" ((USItype)(al)),                                       \
235              "rI" ((USItype)(bl)) __CLOBBER_CC)
236 # if (defined __ARM_ARCH && __ARM_ARCH <= 3)
237 #  define umul_ppmm(xh, xl, a, b) \
238   __asm__ ("@ Inlined umul_ppmm\n"                                      \
239         "mov    %|r0, %2, lsr #16               @ AAAA\n"               \
240         "mov    %|r2, %3, lsr #16               @ BBBB\n"               \
241         "bic    %|r1, %2, %|r0, lsl #16         @ aaaa\n"               \
242         "bic    %0, %3, %|r2, lsl #16           @ bbbb\n"               \
243         "mul    %1, %|r1, %|r2                  @ aaaa * BBBB\n"        \
244         "mul    %|r2, %|r0, %|r2                @ AAAA * BBBB\n"        \
245         "mul    %|r1, %0, %|r1                  @ aaaa * bbbb\n"        \
246         "mul    %0, %|r0, %0                    @ AAAA * bbbb\n"        \
247         "adds   %|r0, %1, %0                    @ central sum\n"        \
248         "addcs  %|r2, %|r2, #65536\n"                                   \
249         "adds   %1, %|r1, %|r0, lsl #16\n"                              \
250         "adc    %0, %|r2, %|r0, lsr #16"                                \
251            : "=&r" ((xh)),                                              \
252              "=r" ((xl))                                                \
253            : "r" ((USItype)(a)),                                        \
254              "r" ((USItype)(b))                                         \
255            : "r0", "r1", "r2" __AND_CLOBBER_CC)
256 # else /* __ARM_ARCH >= 4 */
257 #  define umul_ppmm(xh, xl, a, b)                                         \
258   __asm__ ("@ Inlined umul_ppmm\n"                                      \
259            "umull %1, %0, %2, %3"                                       \
260                    : "=&r" ((xh)),                                      \
261                      "=r" ((xl))                                        \
262                    : "r" ((USItype)(a)),                                \
263                      "r" ((USItype)(b)))
264 # endif /* __ARM_ARCH >= 4 */
265 # define UMUL_TIME 20
266 # define UDIV_TIME 100
267 # if (defined __ARM_ARCH && __ARM_ARCH >= 5)
268 #  define count_leading_zeros(count, x) \
269   __asm__ ("clz %0, %1"                                                 \
270                    : "=r" ((count))                                     \
271                    : "r" ((USItype)(x)))
272 # endif /* __ARM_ARCH >= 5 */
273 #endif /* __arm__ */
274
275 /***************************************
276  **********  ARM64 / Aarch64  **********
277  ***************************************/
278 #if defined(__aarch64__) && W_TYPE_SIZE == 64
279 # define add_ssaaaa(sh, sl, ah, al, bh, bl) \
280   __asm__ ("adds %1, %4, %5\n"                                          \
281            "adc  %0, %2, %3\n"                                          \
282            : "=r" ((sh)),                                               \
283              "=&r" ((sl))                                               \
284            : "r" ((UDItype)(ah)),                                       \
285              "r" ((UDItype)(bh)),                                       \
286              "r" ((UDItype)(al)),                                       \
287              "r" ((UDItype)(bl)) __CLOBBER_CC)
288 # define sub_ddmmss(sh, sl, ah, al, bh, bl) \
289   __asm__ ("subs %1, %4, %5\n"                                          \
290            "sbc  %0, %2, %3\n"                                          \
291            : "=r" ((sh)),                                               \
292              "=&r" ((sl))                                               \
293            : "r" ((UDItype)(ah)),                                       \
294              "r" ((UDItype)(bh)),                                       \
295              "r" ((UDItype)(al)),                                       \
296              "r" ((UDItype)(bl)) __CLOBBER_CC)
297 # define umul_ppmm(ph, pl, m0, m1) \
298   do {                                                                  \
299     UDItype __m0 = (m0), __m1 = (m1), __ph;                             \
300     (pl) = __m0 * __m1;                                                 \
301     __asm__ ("umulh %0,%1,%2"                                           \
302              : "=r" (__ph)                                              \
303              : "r" (__m0),                                              \
304                "r" (__m1));                                             \
305     (ph) = __ph; \
306   } while (0)
307 # define count_leading_zeros(count, x) \
308   __asm__ ("clz %0, %1\n"                                               \
309            : "=r" ((count))                                             \
310            : "r" ((UDItype)(x)))
311 #endif /* __aarch64__ */
312
313 /***************************************
314  **************  CLIPPER  **************
315  ***************************************/
316 #if defined (__clipper__) && W_TYPE_SIZE == 32
317 # define umul_ppmm(w1, w0, u, v) \
318   ({union {UDItype __ll;                                                \
319            struct {USItype __l, __h;} __i;                              \
320           } __xx;                                                       \
321   __asm__ ("mulwux %2,%0"                                               \
322            : "=r" (__xx.__ll)                                           \
323            : "%0" ((USItype)(u)),                                       \
324              "r" ((USItype)(v)));                                       \
325   (w1) = __xx.__i.__h; (w0) = __xx.__i.__l;})
326 # define smul_ppmm(w1, w0, u, v) \
327   ({union {DItype __ll;                                                 \
328            struct {SItype __l, __h;} __i;                               \
329           } __xx;                                                       \
330   __asm__ ("mulwx %2,%0"                                                \
331            : "=r" (__xx.__ll)                                           \
332            : "%0" ((SItype)(u)),                                        \
333              "r" ((SItype)(v)));                                        \
334   (w1) = __xx.__i.__h; (w0) = __xx.__i.__l;})
335 # define __umulsidi3(u, v) \
336   ({UDItype __w;                                                        \
337     __asm__ ("mulwux %2,%0"                                             \
338              : "=r" (__w)                                               \
339              : "%0" ((USItype)(u)),                                     \
340                "r" ((USItype)(v)));                                     \
341     __w; })
342 #endif /* __clipper__ */
343
344
345 /***************************************
346  **************  GMICRO  ***************
347  ***************************************/
348 #if defined (__gmicro__) && W_TYPE_SIZE == 32
349 # define add_ssaaaa(sh, sl, ah, al, bh, bl) \
350   __asm__ ("add.w %5,%1\n"                                              \
351            "addx %3,%0"                                                 \
352            : "=g" ((USItype)(sh)),                                      \
353              "=&g" ((USItype)(sl))                                      \
354            : "%0" ((USItype)(ah)),                                      \
355              "g" ((USItype)(bh)),                                       \
356              "%1" ((USItype)(al)),                                      \
357              "g" ((USItype)(bl)))
358 # define sub_ddmmss(sh, sl, ah, al, bh, bl) \
359   __asm__ ("sub.w %5,%1\n"                                              \
360            "subx %3,%0"                                                 \
361            : "=g" ((USItype)(sh)),                                      \
362              "=&g" ((USItype)(sl))                                      \
363            : "0" ((USItype)(ah)),                                       \
364              "g" ((USItype)(bh)),                                       \
365              "1" ((USItype)(al)),                                       \
366              "g" ((USItype)(bl)))
367 # define umul_ppmm(ph, pl, m0, m1) \
368   __asm__ ("mulx %3,%0,%1"                                              \
369            : "=g" ((USItype)(ph)),                                      \
370              "=r" ((USItype)(pl))                                       \
371            : "%0" ((USItype)(m0)),                                      \
372              "g" ((USItype)(m1)))
373 # define udiv_qrnnd(q, r, nh, nl, d) \
374   __asm__ ("divx %4,%0,%1"                                              \
375            : "=g" ((USItype)(q)),                                       \
376              "=r" ((USItype)(r))                                        \
377            : "1" ((USItype)(nh)),                                       \
378              "0" ((USItype)(nl)),                                       \
379              "g" ((USItype)(d)))
380 # define count_leading_zeros(count, x) \
381   __asm__ ("bsch/1 %1,%0"                                               \
382            : "=g" (count)                                               \
383            : "g" ((USItype)(x)),                                        \
384              "0" ((USItype)0))
385 #endif
386
387
388 /***************************************
389  **************  HPPA  *****************
390  ***************************************/
391 #if defined (__hppa) && W_TYPE_SIZE == 32
392 # define add_ssaaaa(sh, sl, ah, al, bh, bl) \
393   __asm__ ("    add %4,%5,%1\n"                                             \
394            "    addc %2,%3,%0"                                              \
395            : "=r" ((USItype)(sh)),                                      \
396              "=&r" ((USItype)(sl))                                      \
397            : "%rM" ((USItype)(ah)),                                     \
398              "rM" ((USItype)(bh)),                                      \
399              "%rM" ((USItype)(al)),                                     \
400              "rM" ((USItype)(bl)))
401 # define sub_ddmmss(sh, sl, ah, al, bh, bl) \
402   __asm__ ("    sub %4,%5,%1\n"                                             \
403            "    subb %2,%3,%0"                                              \
404            : "=r" ((USItype)(sh)),                                      \
405              "=&r" ((USItype)(sl))                                      \
406            : "rM" ((USItype)(ah)),                                      \
407              "rM" ((USItype)(bh)),                                      \
408              "rM" ((USItype)(al)),                                      \
409              "rM" ((USItype)(bl)))
410 # if defined (_PA_RISC1_1)
411 #  define umul_ppmm(wh, wl, u, v) \
412   do {                                                                  \
413     union {UDItype __ll;                                                \
414            struct {USItype __h, __l;} __i;                              \
415           } __xx;                                                       \
416     __asm__ ("  xmpyu %1,%2,%0"                                           \
417              : "=*f" (__xx.__ll)                                        \
418              : "*f" ((USItype)(u)),                                     \
419                "*f" ((USItype)(v)));                                    \
420     (wh) = __xx.__i.__h;                                                \
421     (wl) = __xx.__i.__l;                                                \
422   } while (0)
423 #  define UMUL_TIME 8
424 #  define UDIV_TIME 60
425 # else
426 #  define UMUL_TIME 40
427 #  define UDIV_TIME 80
428 # endif
429 # ifndef LONGLONG_STANDALONE
430 #  define udiv_qrnnd(q, r, n1, n0, d) \
431   do { USItype __r;                                                     \
432     (q) = __udiv_qrnnd (&__r, (n1), (n0), (d));                         \
433     (r) = __r;                                                          \
434   } while (0)
435 extern USItype __udiv_qrnnd ();
436 # endif /* !LONGLONG_STANDALONE */
437 # define count_leading_zeros(count, x) \
438   do {                                                                 \
439     USItype __tmp;                                                     \
440     __asm__ (                                                          \
441        "        ldi             1,%0                                       \n" \
442        "        extru,=         %1,15,16,%%r0  ; Bits 31..16 zero?         \n" \
443        "        extru,tr        %1,15,16,%1    ; No.  Shift down, skip add.\n" \
444        "        ldo             16(%0),%0      ; Yes.   Perform add.       \n" \
445        "        extru,=         %1,23,8,%%r0   ; Bits 15..8 zero?          \n" \
446        "        extru,tr        %1,23,8,%1     ; No.  Shift down, skip add.\n" \
447        "        ldo             8(%0),%0       ; Yes.   Perform add.       \n" \
448        "        extru,=         %1,27,4,%%r0   ; Bits 7..4 zero?           \n" \
449        "        extru,tr        %1,27,4,%1     ; No.  Shift down, skip add.\n" \
450        "        ldo             4(%0),%0       ; Yes.   Perform add.       \n" \
451        "        extru,=         %1,29,2,%%r0   ; Bits 3..2 zero?           \n" \
452        "        extru,tr        %1,29,2,%1     ; No.  Shift down, skip add.\n" \
453        "        ldo             2(%0),%0       ; Yes.   Perform add.       \n" \
454        "        extru           %1,30,1,%1     ; Extract bit 1.            \n" \
455        "        sub             %0,%1,%0       ; Subtract it.              "   \
456        : "=r" (count), "=r" (__tmp) : "1" (x));                        \
457   } while (0)
458 #endif /* hppa */
459
460
461 /***************************************
462  **************  I370  *****************
463  ***************************************/
464 #if (defined (__i370__) || defined (__mvs__)) && W_TYPE_SIZE == 32
465 # define umul_ppmm(xh, xl, m0, m1) \
466   do {                                                                  \
467     union {UDItype __ll;                                                \
468            struct {USItype __h, __l;} __i;                              \
469           } __xx;                                                       \
470     USItype __m0 = (m0), __m1 = (m1);                                   \
471     __asm__ ("mr %0,%3"                                                 \
472              : "=r" (__xx.__i.__h),                                     \
473                "=r" (__xx.__i.__l)                                      \
474              : "%1" (__m0),                                             \
475                "r" (__m1));                                             \
476     (xh) = __xx.__i.__h; (xl) = __xx.__i.__l;                           \
477     (xh) += ((((SItype) __m0 >> 31) & __m1)                             \
478              + (((SItype) __m1 >> 31) & __m0));                         \
479   } while (0)
480 # define smul_ppmm(xh, xl, m0, m1) \
481   do {                                                                  \
482     union {DItype __ll;                                                 \
483            struct {USItype __h, __l;} __i;                              \
484           } __xx;                                                       \
485     __asm__ ("mr %0,%3"                                                 \
486              : "=r" (__xx.__i.__h),                                     \
487                "=r" (__xx.__i.__l)                                      \
488              : "%1" (m0),                                               \
489                "r" (m1));                                               \
490     (xh) = __xx.__i.__h; (xl) = __xx.__i.__l;                           \
491   } while (0)
492 # define sdiv_qrnnd(q, r, n1, n0, d) \
493   do {                                                                  \
494     union {DItype __ll;                                                 \
495            struct {USItype __h, __l;} __i;                              \
496           } __xx;                                                       \
497     __xx.__i.__h = n1; __xx.__i.__l = n0;                               \
498     __asm__ ("dr %0,%2"                                                 \
499              : "=r" (__xx.__ll)                                         \
500              : "0" (__xx.__ll), "r" (d));                               \
501     (q) = __xx.__i.__l; (r) = __xx.__i.__h;                             \
502   } while (0)
503 #endif
504
505
506 /***************************************
507  **************  I386  *****************
508  ***************************************/
509 #if (defined (__i386__) || defined (__i486__)) && W_TYPE_SIZE == 32
510 # define add_ssaaaa(sh, sl, ah, al, bh, bl) \
511   __asm__ ("addl %5,%1\n"                                               \
512            "adcl %3,%0"                                                 \
513            : "=r" ((sh)),                                               \
514              "=&r" ((sl))                                               \
515            : "%0" ((USItype)(ah)),                                      \
516              "g" ((USItype)(bh)),                                       \
517              "%1" ((USItype)(al)),                                      \
518              "g" ((USItype)(bl))                                        \
519            __CLOBBER_CC)
520 # define sub_ddmmss(sh, sl, ah, al, bh, bl) \
521   __asm__ ("subl %5,%1\n"                                               \
522            "sbbl %3,%0"                                                 \
523            : "=r" ((sh)),                                               \
524              "=&r" ((sl))                                               \
525            : "0" ((USItype)(ah)),                                       \
526              "g" ((USItype)(bh)),                                       \
527              "1" ((USItype)(al)),                                       \
528              "g" ((USItype)(bl))                                        \
529            __CLOBBER_CC)
530 # define umul_ppmm(w1, w0, u, v) \
531   __asm__ ("mull %3"                                                    \
532            : "=a" ((w0)),                                               \
533              "=d" ((w1))                                                \
534            : "%0" ((USItype)(u)),                                       \
535              "rm" ((USItype)(v))                                        \
536            __CLOBBER_CC)
537 # define udiv_qrnnd(q, r, n1, n0, d) \
538   __asm__ ("divl %4"                                                    \
539            : "=a" ((q)),                                                \
540              "=d" ((r))                                                 \
541            : "0" ((USItype)(n0)),                                       \
542              "1" ((USItype)(n1)),                                       \
543              "rm" ((USItype)(d))                                        \
544            __CLOBBER_CC)
545 # define count_leading_zeros(count, x) \
546   do {                                                                  \
547     USItype __cbtmp;                                                    \
548     __asm__ ("bsrl %1,%0"                                               \
549              : "=r" (__cbtmp) : "rm" ((USItype)(x))                     \
550              __CLOBBER_CC);                                             \
551     (count) = __cbtmp ^ 31;                                             \
552   } while (0)
553 # define count_trailing_zeros(count, x) \
554   __asm__ ("bsfl %1,%0" : "=r" (count) : "rm" ((USItype)(x)) __CLOBBER_CC)
555 # ifndef UMUL_TIME
556 #  define UMUL_TIME 40
557 # endif
558 # ifndef UDIV_TIME
559 #  define UDIV_TIME 40
560 # endif
561 #endif /* 80x86 */
562
563 /***************************************
564  *********** AMD64 / x86-64 ************
565  ***************************************/
566 #if defined(__x86_64) && W_TYPE_SIZE == 64
567 # define add_ssaaaa(sh, sl, ah, al, bh, bl) \
568   __asm__ ("addq %5,%1\n"                                               \
569            "adcq %3,%0"                                                 \
570            : "=r" ((sh)),                                               \
571              "=&r" ((sl))                                               \
572            : "0" ((UDItype)(ah)),                                       \
573              "g"  ((UDItype)(bh)),                                      \
574              "1" ((UDItype)(al)),                                       \
575              "g"  ((UDItype)(bl))                                       \
576            __CLOBBER_CC)
577 # define sub_ddmmss(sh, sl, ah, al, bh, bl) \
578   __asm__ ("subq %5,%1\n"                                               \
579            "sbbq %3,%0"                                                 \
580            : "=r" ((sh)),                                               \
581              "=&r" ((sl))                                               \
582            : "0" ((UDItype)(ah)),                                       \
583              "g" ((UDItype)(bh)),                                       \
584              "1" ((UDItype)(al)),                                       \
585              "g" ((UDItype)(bl))                                        \
586            __CLOBBER_CC)
587 # define umul_ppmm(w1, w0, u, v) \
588   __asm__ ("mulq %3"                                                    \
589            : "=a" ((w0)),                                               \
590              "=d" ((w1))                                                \
591            : "0" ((UDItype)(u)),                                        \
592              "rm" ((UDItype)(v))                                        \
593            __CLOBBER_CC)
594 # define udiv_qrnnd(q, r, n1, n0, d) \
595   __asm__ ("divq %4"                                                    \
596            : "=a" ((q)),                                                \
597              "=d" ((r))                                                 \
598            : "0" ((UDItype)(n0)),                                       \
599              "1" ((UDItype)(n1)),                                       \
600              "rm" ((UDItype)(d))                                        \
601            __CLOBBER_CC)
602 # define count_leading_zeros(count, x) \
603   do {                                                                  \
604     UDItype __cbtmp;                                                    \
605     __asm__ ("bsrq %1,%0"                                               \
606              : "=r" (__cbtmp) : "rm" ((UDItype)(x))                     \
607              __CLOBBER_CC);                                             \
608     (count) = __cbtmp ^ 63;                                             \
609   } while (0)
610 # define count_trailing_zeros(count, x) \
611   do {                                                                  \
612     UDItype __cbtmp;                                                    \
613     __asm__ ("bsfq %1,%0"                                               \
614              : "=r" (__cbtmp) : "rm" ((UDItype)(x))                     \
615              __CLOBBER_CC);                                             \
616     (count) = __cbtmp;                                                  \
617   } while (0)
618 # ifndef UMUL_TIME
619 #  define UMUL_TIME 40
620 # endif
621 # ifndef UDIV_TIME
622 #  define UDIV_TIME 40
623 # endif
624 #endif /* __x86_64 */
625
626
627 /***************************************
628  **************  I860  *****************
629  ***************************************/
630 #if defined (__i860__) && W_TYPE_SIZE == 32
631 # define rshift_rhlc(r,h,l,c) \
632   __asm__ ("shr %3,r0,r0\n"  \
633            "shrd %1,%2,%0"   \
634            "=r" (r) : "r" (h), "r" (l), "rn" (c))
635 #endif /* i860 */
636
637 /***************************************
638  **************  I960  *****************
639  ***************************************/
640 #if defined (__i960__) && W_TYPE_SIZE == 32
641 # define add_ssaaaa(sh, sl, ah, al, bh, bl) \
642   __asm__ ("cmpo 1,0\n"      \
643            "addc %5,%4,%1\n" \
644            "addc %3,%2,%0"   \
645            : "=r" ((USItype)(sh)),                                      \
646              "=&r" ((USItype)(sl))                                      \
647            : "%dI" ((USItype)(ah)),                                     \
648              "dI" ((USItype)(bh)),                                      \
649              "%dI" ((USItype)(al)),                                     \
650              "dI" ((USItype)(bl)))
651 # define sub_ddmmss(sh, sl, ah, al, bh, bl) \
652   __asm__ ("cmpo 0,0\n"      \
653            "subc %5,%4,%1\n" \
654            "subc %3,%2,%0"   \
655            : "=r" ((USItype)(sh)),                                      \
656              "=&r" ((USItype)(sl))                                      \
657            : "dI" ((USItype)(ah)),                                      \
658              "dI" ((USItype)(bh)),                                      \
659              "dI" ((USItype)(al)),                                      \
660              "dI" ((USItype)(bl)))
661 # define umul_ppmm(w1, w0, u, v) \
662   ({union {UDItype __ll;                                                \
663            struct {USItype __l, __h;} __i;                              \
664           } __xx;                                                       \
665   __asm__ ("emul        %2,%1,%0"                                       \
666            : "=d" (__xx.__ll)                                           \
667            : "%dI" ((USItype)(u)),                                      \
668              "dI" ((USItype)(v)));                                      \
669   (w1) = __xx.__i.__h; (w0) = __xx.__i.__l;})
670 # define __umulsidi3(u, v) \
671   ({UDItype __w;                                                        \
672     __asm__ ("emul      %2,%1,%0"                                       \
673              : "=d" (__w)                                               \
674              : "%dI" ((USItype)(u)),                                    \
675                "dI" ((USItype)(v)));                                    \
676     __w; })
677 # define udiv_qrnnd(q, r, nh, nl, d) \
678   do {                                                                  \
679     union {UDItype __ll;                                                \
680            struct {USItype __l, __h;} __i;                              \
681           } __nn;                                                       \
682     __nn.__i.__h = (nh); __nn.__i.__l = (nl);                           \
683     __asm__ ("ediv %d,%n,%0"                                            \
684            : "=d" (__rq.__ll)                                           \
685            : "dI" (__nn.__ll),                                          \
686              "dI" ((USItype)(d)));                                      \
687     (r) = __rq.__i.__l; (q) = __rq.__i.__h;                             \
688   } while (0)
689 # define count_leading_zeros(count, x) \
690   do {                                                                  \
691     USItype __cbtmp;                                                    \
692     __asm__ ("scanbit %1,%0"                                            \
693              : "=r" (__cbtmp)                                           \
694              : "r" ((USItype)(x)));                                     \
695     (count) = __cbtmp ^ 31;                                             \
696   } while (0)
697 # define COUNT_LEADING_ZEROS_0 (-32) /* sic */
698 # if defined (__i960mx)  /* what is the proper symbol to test??? */
699 #  define rshift_rhlc(r,h,l,c) \
700   do {                                                                  \
701     union {UDItype __ll;                                                \
702            struct {USItype __l, __h;} __i;                              \
703           } __nn;                                                       \
704     __nn.__i.__h = (h); __nn.__i.__l = (l);                             \
705     __asm__ ("shre %2,%1,%0"                                            \
706              : "=d" (r) : "dI" (__nn.__ll), "dI" (c));                  \
707   }
708 # endif /* i960mx */
709 #endif /* i960 */
710
711
712 /***************************************
713  **************  68000  ****************
714  ***************************************/
715 #if (defined (__mc68000__) || defined (__mc68020__)                     \
716      || defined (__NeXT__) || defined(mc68020)) && W_TYPE_SIZE == 32
717 # define add_ssaaaa(sh, sl, ah, al, bh, bl) \
718   __asm__ ("add%.l %5,%1\n"                                             \
719            "addx%.l %3,%0"                                              \
720            : "=d" ((USItype)(sh)),                                      \
721              "=&d" ((USItype)(sl))                                      \
722            : "%0" ((USItype)(ah)),                                      \
723              "d" ((USItype)(bh)),                                       \
724              "%1" ((USItype)(al)),                                      \
725              "g" ((USItype)(bl)))
726 # define sub_ddmmss(sh, sl, ah, al, bh, bl) \
727   __asm__ ("sub%.l %5,%1\n"                                             \
728            "subx%.l %3,%0"                                              \
729            : "=d" ((USItype)(sh)),                                      \
730              "=&d" ((USItype)(sl))                                      \
731            : "0" ((USItype)(ah)),                                       \
732              "d" ((USItype)(bh)),                                       \
733              "1" ((USItype)(al)),                                       \
734              "g" ((USItype)(bl)))
735 # if (defined (__mc68020__) || defined (__NeXT__) || defined(mc68020))
736 #  define umul_ppmm(w1, w0, u, v) \
737   __asm__ ("mulu%.l %3,%1:%0"                                           \
738            : "=d" ((USItype)(w0)),                                      \
739              "=d" ((USItype)(w1))                                       \
740            : "%0" ((USItype)(u)),                                       \
741              "dmi" ((USItype)(v)))
742 #  define UMUL_TIME 45
743 #  define udiv_qrnnd(q, r, n1, n0, d) \
744   __asm__ ("divu%.l %4,%1:%0"                                           \
745            : "=d" ((USItype)(q)),                                       \
746              "=d" ((USItype)(r))                                        \
747            : "0" ((USItype)(n0)),                                       \
748              "1" ((USItype)(n1)),                                       \
749              "dmi" ((USItype)(d)))
750 #  define UDIV_TIME 90
751 #  define sdiv_qrnnd(q, r, n1, n0, d) \
752   __asm__ ("divs%.l %4,%1:%0"                                           \
753            : "=d" ((USItype)(q)),                                       \
754              "=d" ((USItype)(r))                                        \
755            : "0" ((USItype)(n0)),                                       \
756              "1" ((USItype)(n1)),                                       \
757              "dmi" ((USItype)(d)))
758 #  define count_leading_zeros(count, x) \
759   __asm__ ("bfffo %1{%b2:%b2},%0"                                       \
760            : "=d" ((USItype)(count))                                    \
761            : "od" ((USItype)(x)), "n" (0))
762 #  define COUNT_LEADING_ZEROS_0 32
763 # else /* not mc68020 */
764 #  define umul_ppmm(xh, xl, a, b) \
765   do { USItype __umul_tmp1, __umul_tmp2;                          \
766         __asm__ ("| Inlined umul_ppmm                         \n" \
767  "        move%.l %5,%3                                       \n" \
768  "        move%.l %2,%0                                       \n" \
769  "        move%.w %3,%1                                       \n" \
770  "        swap  %3                                            \n" \
771  "        swap  %0                                            \n" \
772  "        mulu  %2,%1                                         \n" \
773  "        mulu  %3,%0                                         \n" \
774  "        mulu  %2,%3                                         \n" \
775  "        swap  %2                                            \n" \
776  "        mulu  %5,%2                                         \n" \
777  "        add%.l        %3,%2                                 \n" \
778  "        jcc   1f                                            \n" \
779  "        add%.l        %#0x10000,%0                          \n" \
780  "1:    move%.l %2,%3                                         \n" \
781  "        clr%.w        %2                                    \n" \
782  "        swap  %2                                            \n" \
783  "        swap  %3                                            \n" \
784  "        clr%.w        %3                                    \n" \
785  "        add%.l        %3,%1                                 \n" \
786  "        addx%.l %2,%0                                       \n" \
787  "        | End inlined umul_ppmm"                                \
788               : "=&d" ((USItype)(xh)), "=&d" ((USItype)(xl)),     \
789                 "=d" (__umul_tmp1), "=&d" (__umul_tmp2)           \
790               : "%2" ((USItype)(a)), "d" ((USItype)(b)));         \
791   } while (0)
792 #  define UMUL_TIME 100
793 #  define UDIV_TIME 400
794 # endif /* not mc68020 */
795 #endif /* mc68000 */
796
797
798 /***************************************
799  **************  88000  ****************
800  ***************************************/
801 #if defined (__m88000__) && W_TYPE_SIZE == 32
802 # define add_ssaaaa(sh, sl, ah, al, bh, bl) \
803   __asm__ ("addu.co %1,%r4,%r5\n"                                       \
804            "addu.ci %0,%r2,%r3"                                         \
805            : "=r" ((USItype)(sh)),                                      \
806              "=&r" ((USItype)(sl))                                      \
807            : "%rJ" ((USItype)(ah)),                                     \
808              "rJ" ((USItype)(bh)),                                      \
809              "%rJ" ((USItype)(al)),                                     \
810              "rJ" ((USItype)(bl)))
811 # define sub_ddmmss(sh, sl, ah, al, bh, bl) \
812   __asm__ ("subu.co %1,%r4,%r5\n"                                       \
813            "subu.ci %0,%r2,%r3"                                         \
814            : "=r" ((USItype)(sh)),                                      \
815              "=&r" ((USItype)(sl))                                      \
816            : "rJ" ((USItype)(ah)),                                      \
817              "rJ" ((USItype)(bh)),                                      \
818              "rJ" ((USItype)(al)),                                      \
819              "rJ" ((USItype)(bl)))
820 # define count_leading_zeros(count, x) \
821   do {                                                                  \
822     USItype __cbtmp;                                                    \
823     __asm__ ("ff1 %0,%1"                                                \
824              : "=r" (__cbtmp)                                           \
825              : "r" ((USItype)(x)));                                     \
826     (count) = __cbtmp ^ 31;                                             \
827   } while (0)
828 # define COUNT_LEADING_ZEROS_0 63 /* sic */
829 # if defined (__m88110__)
830 #  define umul_ppmm(wh, wl, u, v) \
831   do {                                                                  \
832     union {UDItype __ll;                                                \
833            struct {USItype __h, __l;} __i;                              \
834           } __x;                                                        \
835     __asm__ ("mulu.d %0,%1,%2" : "=r" (__x.__ll) : "r" (u), "r" (v));   \
836     (wh) = __x.__i.__h;                                                 \
837     (wl) = __x.__i.__l;                                                 \
838   } while (0)
839 #  define udiv_qrnnd(q, r, n1, n0, d) \
840   ({union {UDItype __ll;                                                \
841            struct {USItype __h, __l;} __i;                              \
842           } __x, __q;                                                   \
843   __x.__i.__h = (n1); __x.__i.__l = (n0);                               \
844   __asm__ ("divu.d %0,%1,%2"                                            \
845            : "=r" (__q.__ll) : "r" (__x.__ll), "r" (d));                \
846   (r) = (n0) - __q.__l * (d); (q) = __q.__l; })
847 #  define UMUL_TIME 5
848 #  define UDIV_TIME 25
849 # else
850 #  define UMUL_TIME 17
851 #  define UDIV_TIME 150
852 # endif /* __m88110__ */
853 #endif /* __m88000__ */
854
855 /***************************************
856  **************  MIPS  *****************
857  ***************************************/
858 #if defined (__mips__) && W_TYPE_SIZE == 32
859 # if defined (__clang__) || (__GNUC__ >= 5) || (__GNUC__ == 4 && \
860                                                __GNUC_MINOR__ >= 4)
861 #  define umul_ppmm(w1, w0, u, v) \
862   do {                                                                  \
863     UDItype _r;                                                         \
864     _r = (UDItype) u * v;                                               \
865     (w1) = _r >> 32;                                                    \
866     (w0) = (USItype) _r;                                                \
867   } while (0)
868 # elif __GNUC__ > 2 || __GNUC_MINOR__ >= 7
869 #  define umul_ppmm(w1, w0, u, v) \
870   __asm__ ("multu %2,%3"                                                \
871            : "=l" ((USItype)(w0)),                                      \
872              "=h" ((USItype)(w1))                                       \
873            : "d" ((USItype)(u)),                                        \
874              "d" ((USItype)(v)))
875 # else
876 #  define umul_ppmm(w1, w0, u, v) \
877   __asm__ ("multu %2,%3 \n" \
878            "mflo %0 \n"     \
879            "mfhi %1"                                                        \
880            : "=d" ((USItype)(w0)),                                      \
881              "=d" ((USItype)(w1))                                       \
882            : "d" ((USItype)(u)),                                        \
883              "d" ((USItype)(v)))
884 # endif
885 # define UMUL_TIME 10
886 # define UDIV_TIME 100
887 #endif /* __mips__ */
888
889 /***************************************
890  **************  MIPS/64  **************
891  ***************************************/
892 #if (defined (__mips) && __mips >= 3) && W_TYPE_SIZE == 64
893 # if (__GNUC__ >= 5) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
894 typedef unsigned int UTItype __attribute__ ((mode (TI)));
895 #  define umul_ppmm(w1, w0, u, v) \
896   do {                                                                 \
897     UTItype _r;                                                        \
898     _r = (UTItype) u * v;                                              \
899     (w1) = _r >> 64;                                                   \
900     (w0) = (UDItype) _r;                                               \
901   } while (0)
902 # elif __GNUC__ > 2 || __GNUC_MINOR__ >= 7
903 #  define umul_ppmm(w1, w0, u, v) \
904   __asm__ ("dmultu %2,%3"                                               \
905            : "=l" ((UDItype)(w0)),                                      \
906              "=h" ((UDItype)(w1))                                       \
907            : "d" ((UDItype)(u)),                                        \
908              "d" ((UDItype)(v)))
909 # else
910 #  define umul_ppmm(w1, w0, u, v) \
911   __asm__ ("dmultu %2,%3 \n"    \
912            "mflo %0 \n"         \
913            "mfhi %1"                                                        \
914            : "=d" ((UDItype)(w0)),                                      \
915              "=d" ((UDItype)(w1))                                       \
916            : "d" ((UDItype)(u)),                                        \
917              "d" ((UDItype)(v)))
918 # endif
919 # define UMUL_TIME 20
920 # define UDIV_TIME 140
921 #endif /* __mips__ */
922
923
924 /***************************************
925  **************  32000  ****************
926  ***************************************/
927 #if defined (__ns32000__) && W_TYPE_SIZE == 32
928 # define umul_ppmm(w1, w0, u, v) \
929   ({union {UDItype __ll;                                                \
930            struct {USItype __l, __h;} __i;                              \
931           } __xx;                                                       \
932   __asm__ ("meid %2,%0"                                                 \
933            : "=g" (__xx.__ll)                                           \
934            : "%0" ((USItype)(u)),                                       \
935              "g" ((USItype)(v)));                                       \
936   (w1) = __xx.__i.__h; (w0) = __xx.__i.__l;})
937 # define __umulsidi3(u, v) \
938   ({UDItype __w;                                                        \
939     __asm__ ("meid %2,%0"                                               \
940              : "=g" (__w)                                               \
941              : "%0" ((USItype)(u)),                                     \
942                "g" ((USItype)(v)));                                     \
943     __w; })
944 # define udiv_qrnnd(q, r, n1, n0, d) \
945   ({union {UDItype __ll;                                                \
946            struct {USItype __l, __h;} __i;                              \
947           } __xx;                                                       \
948   __xx.__i.__h = (n1); __xx.__i.__l = (n0);                             \
949   __asm__ ("deid %2,%0"                                                 \
950            : "=g" (__xx.__ll)                                           \
951            : "0" (__xx.__ll),                                           \
952              "g" ((USItype)(d)));                                       \
953   (r) = __xx.__i.__l; (q) = __xx.__i.__h; })
954 # define count_trailing_zeros(count,x) \
955   do {
956     __asm__ ("ffsd      %2,%0"                                          \
957              : "=r" ((USItype) (count))                                 \
958              : "0" ((USItype) 0),                                       \
959                "r" ((USItype) (x)));                                    \
960   } while (0)
961 #endif /* __ns32000__ */
962
963
964 /***************************************
965  **************  PPC  ******************
966  ***************************************/
967 #if (defined (_ARCH_PPC) || defined (_IBMR2)) && W_TYPE_SIZE == 32
968 # define add_ssaaaa(sh, sl, ah, al, bh, bl) \
969   do {                                                                  \
970     if (__builtin_constant_p (bh) && (bh) == 0)                         \
971       __asm__ ("{a%I4|add%I4c} %1,%3,%4\n\t{aze|addze} %0,%2"           \
972              : "=r" ((sh)),                                             \
973                "=&r" ((sl))                                             \
974              : "%r" ((USItype)(ah)),                                    \
975                "%r" ((USItype)(al)),                                    \
976                "rI" ((USItype)(bl)));                                   \
977     else if (__builtin_constant_p (bh) && (bh) ==~(USItype) 0)          \
978       __asm__ ("{a%I4|add%I4c} %1,%3,%4\n\t{ame|addme} %0,%2"           \
979              : "=r" ((sh)),                                             \
980                "=&r" ((sl))                                             \
981              : "%r" ((USItype)(ah)),                                    \
982                "%r" ((USItype)(al)),                                    \
983                "rI" ((USItype)(bl)));                                   \
984     else                                                                \
985       __asm__ ("{a%I5|add%I5c} %1,%4,%5\n\t{ae|adde} %0,%2,%3"          \
986              : "=r" ((sh)),                                             \
987                "=&r" ((sl))                                             \
988              : "%r" ((USItype)(ah)),                                    \
989                "r" ((USItype)(bh)),                                     \
990                "%r" ((USItype)(al)),                                    \
991                "rI" ((USItype)(bl)));                                   \
992   } while (0)
993 # define sub_ddmmss(sh, sl, ah, al, bh, bl) \
994   do {                                                                  \
995     if (__builtin_constant_p (ah) && (ah) == 0)                         \
996       __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{sfze|subfze} %0,%2"       \
997                : "=r" ((sh)),                                           \
998                  "=&r" ((sl))                                           \
999                : "r" ((USItype)(bh)),                                   \
1000                  "rI" ((USItype)(al)),                                  \
1001                  "r" ((USItype)(bl)));                                  \
1002     else if (__builtin_constant_p (ah) && (ah) ==~(USItype) 0)          \
1003       __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{sfme|subfme} %0,%2"       \
1004                : "=r" ((sh)),                                  \
1005                  "=&r" ((sl))                                  \
1006                : "r" ((USItype)(bh)),                                   \
1007                  "rI" ((USItype)(al)),                                  \
1008                  "r" ((USItype)(bl)));                                  \
1009     else if (__builtin_constant_p (bh) && (bh) == 0)                    \
1010       __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{ame|addme} %0,%2"         \
1011                : "=r" ((sh)),                                           \
1012                  "=&r" ((sl))                                           \
1013                : "r" ((USItype)(ah)),                                   \
1014                  "rI" ((USItype)(al)),                                  \
1015                  "r" ((USItype)(bl)));                                  \
1016     else if (__builtin_constant_p (bh) && (bh) ==~(USItype) 0)          \
1017       __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{aze|addze} %0,%2"         \
1018                : "=r" ((sh)),                                           \
1019                  "=&r" ((sl))                                           \
1020                : "r" ((USItype)(ah)),                                   \
1021                  "rI" ((USItype)(al)),                                  \
1022                  "r" ((USItype)(bl)));                                  \
1023     else                                                                \
1024       __asm__ ("{sf%I4|subf%I4c} %1,%5,%4\n\t{sfe|subfe} %0,%3,%2"      \
1025                : "=r" ((sh)),                                           \
1026                  "=&r" ((sl))                                           \
1027                : "r" ((USItype)(ah)),                                   \
1028                  "r" ((USItype)(bh)),                                   \
1029                  "rI" ((USItype)(al)),                                  \
1030                  "r" ((USItype)(bl)));                                  \
1031   } while (0)
1032 # define count_leading_zeros(count, x) \
1033   __asm__ ("{cntlz|cntlzw} %0,%1"                                       \
1034            : "=r" ((count))                                             \
1035            : "r" ((USItype)(x)))
1036 # define COUNT_LEADING_ZEROS_0 32
1037 # if defined (_ARCH_PPC)
1038 #  define umul_ppmm(ph, pl, m0, m1) \
1039   do {                                                                  \
1040     USItype __m0 = (m0), __m1 = (m1);                                   \
1041     __asm__ ("mulhwu %0,%1,%2"                                          \
1042              : "=r" (ph)                                                \
1043              : "%r" (__m0),                                             \
1044                "r" (__m1));                                             \
1045     (pl) = __m0 * __m1;                                                 \
1046   } while (0)
1047 #  define UMUL_TIME 15
1048 #  define smul_ppmm(ph, pl, m0, m1) \
1049   do {                                                                  \
1050     SItype __m0 = (m0), __m1 = (m1);                                    \
1051     __asm__ ("mulhw %0,%1,%2"                                           \
1052              : "=r" ((SItype) ph)                                       \
1053              : "%r" (__m0),                                             \
1054                "r" (__m1));                                             \
1055     (pl) = __m0 * __m1;                                                 \
1056   } while (0)
1057 #  define SMUL_TIME 14
1058 #  define UDIV_TIME 120
1059 # else
1060 #  define umul_ppmm(xh, xl, m0, m1) \
1061   do {                                                                  \
1062     USItype __m0 = (m0), __m1 = (m1);                                   \
1063     __asm__ ("mul %0,%2,%3"                                             \
1064              : "=r" ((xh)),                                             \
1065                "=q" ((xl))                                              \
1066              : "r" (__m0),                                              \
1067                "r" (__m1));                                             \
1068     (xh) += ((((SItype) __m0 >> 31) & __m1)                             \
1069              + (((SItype) __m1 >> 31) & __m0));                         \
1070   } while (0)
1071 #  define UMUL_TIME 8
1072 #  define smul_ppmm(xh, xl, m0, m1) \
1073   __asm__ ("mul %0,%2,%3"                                               \
1074            : "=r" ((SItype)(xh)),                                       \
1075              "=q" ((SItype)(xl))                                        \
1076            : "r" (m0),                                                  \
1077              "r" (m1))
1078 #  define SMUL_TIME 4
1079 #  define sdiv_qrnnd(q, r, nh, nl, d) \
1080   __asm__ ("div %0,%2,%4"                                               \
1081            : "=r" ((SItype)(q)), "=q" ((SItype)(r))                     \
1082            : "r" ((SItype)(nh)), "1" ((SItype)(nl)), "r" ((SItype)(d)))
1083 #  define UDIV_TIME 100
1084 # endif
1085 #endif /* Power architecture variants.  */
1086
1087 /* Powerpc 64 bit support taken from gmp-4.1.2. */
1088 /* We should test _IBMR2 here when we add assembly support for the system
1089    vendor compilers.  */
1090 #if 0 /* Not yet enabled because we don't have hardware for a test. */
1091 #if (defined (_ARCH_PPC) || defined (__powerpc__)) && W_TYPE_SIZE == 64
1092 #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
1093   do {                                                                  \
1094     if (__builtin_constant_p (bh) && (bh) == 0)                         \
1095       __asm__ ("{a%I4|add%I4c} %1,%3,%4\n\t{aze|addze} %0,%2"           \
1096              : "=r" (sh), "=&r" (sl) : "r" (ah), "%r" (al), "rI" (bl));\
1097     else if (__builtin_constant_p (bh) && (bh) == ~(UDItype) 0)         \
1098       __asm__ ("{a%I4|add%I4c} %1,%3,%4\n\t{ame|addme} %0,%2"           \
1099              : "=r" (sh), "=&r" (sl) : "r" (ah), "%r" (al), "rI" (bl));\
1100     else                                                                \
1101       __asm__ ("{a%I5|add%I5c} %1,%4,%5\n\t{ae|adde} %0,%2,%3"          \
1102              : "=r" (sh), "=&r" (sl)                                    \
1103              : "%r" (ah), "r" (bh), "%r" (al), "rI" (bl));              \
1104   } while (0)
1105 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
1106   do {                                                                  \
1107     if (__builtin_constant_p (ah) && (ah) == 0)                         \
1108       __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{sfze|subfze} %0,%2"       \
1109                : "=r" (sh), "=&r" (sl) : "r" (bh), "rI" (al), "r" (bl));\
1110     else if (__builtin_constant_p (ah) && (ah) == ~(UDItype) 0)         \
1111       __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{sfme|subfme} %0,%2"       \
1112                : "=r" (sh), "=&r" (sl) : "r" (bh), "rI" (al), "r" (bl));\
1113     else if (__builtin_constant_p (bh) && (bh) == 0)                    \
1114       __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{ame|addme} %0,%2"         \
1115                : "=r" (sh), "=&r" (sl) : "r" (ah), "rI" (al), "r" (bl));\
1116     else if (__builtin_constant_p (bh) && (bh) == ~(UDItype) 0)         \
1117       __asm__ ("{sf%I3|subf%I3c} %1,%4,%3\n\t{aze|addze} %0,%2"         \
1118                : "=r" (sh), "=&r" (sl) : "r" (ah), "rI" (al), "r" (bl));\
1119     else                                                                \
1120       __asm__ ("{sf%I4|subf%I4c} %1,%5,%4\n\t{sfe|subfe} %0,%3,%2"      \
1121                : "=r" (sh), "=&r" (sl)                                  \
1122                : "r" (ah), "r" (bh), "rI" (al), "r" (bl));              \
1123   } while (0)
1124 #define count_leading_zeros(count, x) \
1125   __asm__ ("cntlzd %0,%1" : "=r" (count) : "r" (x))
1126 #define COUNT_LEADING_ZEROS_0 64
1127 #define umul_ppmm(ph, pl, m0, m1) \
1128   do {                                                                  \
1129     UDItype __m0 = (m0), __m1 = (m1);                                   \
1130     __asm__ ("mulhdu %0,%1,%2" : "=r" (ph) : "%r" (m0), "r" (m1));      \
1131     (pl) = __m0 * __m1;                                                 \
1132   } while (0)
1133 #define UMUL_TIME 15
1134 #define smul_ppmm(ph, pl, m0, m1) \
1135   do {                                                                  \
1136     DItype __m0 = (m0), __m1 = (m1);                                    \
1137     __asm__ ("mulhd %0,%1,%2" : "=r" (ph) : "%r" (m0), "r" (m1));       \
1138     (pl) = __m0 * __m1;                                                 \
1139   } while (0)
1140 #define SMUL_TIME 14  /* ??? */
1141 #define UDIV_TIME 120 /* ??? */
1142 #endif /* 64-bit PowerPC.  */
1143 #endif /* if 0 */
1144
1145 /***************************************
1146  **************  PYR  ******************
1147  ***************************************/
1148 #if defined (__pyr__) && W_TYPE_SIZE == 32
1149 # define add_ssaaaa(sh, sl, ah, al, bh, bl) \
1150   __asm__ ("addw        %5,%1 \n" \
1151            "addwc       %3,%0"                                          \
1152            : "=r" ((USItype)(sh)),                                      \
1153              "=&r" ((USItype)(sl))                                      \
1154            : "%0" ((USItype)(ah)),                                      \
1155              "g" ((USItype)(bh)),                                       \
1156              "%1" ((USItype)(al)),                                      \
1157              "g" ((USItype)(bl)))
1158 # define sub_ddmmss(sh, sl, ah, al, bh, bl) \
1159   __asm__ ("subw        %5,%1 \n" \
1160            "subwb       %3,%0"                                          \
1161            : "=r" ((USItype)(sh)),                                      \
1162              "=&r" ((USItype)(sl))                                      \
1163            : "0" ((USItype)(ah)),                                       \
1164              "g" ((USItype)(bh)),                                       \
1165              "1" ((USItype)(al)),                                       \
1166              "g" ((USItype)(bl)))
1167 /* This insn works on Pyramids with AP, XP, or MI CPUs, but not with SP.  */
1168 # define umul_ppmm(w1, w0, u, v) \
1169   ({union {UDItype __ll;                                                \
1170            struct {USItype __h, __l;} __i;                              \
1171           } __xx;                                                       \
1172   __asm__ ("movw %1,%R0 \n" \
1173            "uemul %2,%0"                                                \
1174            : "=&r" (__xx.__ll)                                          \
1175            : "g" ((USItype) (u)),                                       \
1176              "g" ((USItype)(v)));                                       \
1177   (w1) = __xx.__i.__h; (w0) = __xx.__i.__l;})
1178 #endif /* __pyr__ */
1179
1180
1181 /***************************************
1182  **************  RT/ROMP  **************
1183  ***************************************/
1184 #if defined (__ibm032__) /* RT/ROMP */  && W_TYPE_SIZE == 32
1185 # define add_ssaaaa(sh, sl, ah, al, bh, bl) \
1186   __asm__ ("a %1,%5 \n" \
1187            "ae %0,%3"                                                   \
1188            : "=r" ((USItype)(sh)),                                      \
1189              "=&r" ((USItype)(sl))                                      \
1190            : "%0" ((USItype)(ah)),                                      \
1191              "r" ((USItype)(bh)),                                       \
1192              "%1" ((USItype)(al)),                                      \
1193              "r" ((USItype)(bl)))
1194 # define sub_ddmmss(sh, sl, ah, al, bh, bl) \
1195   __asm__ ("s %1,%5\n" \
1196            "se %0,%3"                                                   \
1197            : "=r" ((USItype)(sh)),                                      \
1198              "=&r" ((USItype)(sl))                                      \
1199            : "0" ((USItype)(ah)),                                       \
1200              "r" ((USItype)(bh)),                                       \
1201              "1" ((USItype)(al)),                                       \
1202              "r" ((USItype)(bl)))
1203 # define umul_ppmm(ph, pl, m0, m1) \
1204   do {                                                                  \
1205     USItype __m0 = (m0), __m1 = (m1);                                   \
1206     __asm__ (                                                           \
1207        "s       r2,r2    \n" \
1208        "mts     r10,%2   \n" \
1209        "m       r2,%3    \n" \
1210        "m       r2,%3    \n" \
1211        "m       r2,%3    \n" \
1212        "m       r2,%3    \n" \
1213        "m       r2,%3    \n" \
1214        "m       r2,%3    \n" \
1215        "m       r2,%3    \n" \
1216        "m       r2,%3    \n" \
1217        "m       r2,%3    \n" \
1218        "m       r2,%3    \n" \
1219        "m       r2,%3    \n" \
1220        "m       r2,%3    \n" \
1221        "m       r2,%3    \n" \
1222        "m       r2,%3    \n" \
1223        "m       r2,%3    \n" \
1224        "m       r2,%3    \n" \
1225        "cas     %0,r2,r0 \n" \
1226        "mfs     r10,%1"                                                 \
1227              : "=r" ((USItype)(ph)),                                    \
1228                "=r" ((USItype)(pl))                                     \
1229              : "%r" (__m0),                                             \
1230                 "r" (__m1)                                              \
1231              : "r2");                                                   \
1232     (ph) += ((((SItype) __m0 >> 31) & __m1)                             \
1233              + (((SItype) __m1 >> 31) & __m0));                         \
1234   } while (0)
1235 # define UMUL_TIME 20
1236 # define UDIV_TIME 200
1237 # define count_leading_zeros(count, x) \
1238   do {                                                                  \
1239     if ((x) >= 0x10000)                                                 \
1240       __asm__ ("clz     %0,%1"                                          \
1241                : "=r" ((USItype)(count))                                \
1242                : "r" ((USItype)(x) >> 16));                             \
1243     else                                                                \
1244       {                                                                 \
1245         __asm__ ("clz   %0,%1"                                          \
1246                  : "=r" ((USItype)(count))                              \
1247                  : "r" ((USItype)(x)));                                 \
1248         (count) += 16;                                                  \
1249       }                                                                 \
1250   } while (0)
1251 #endif /* RT/ROMP */
1252
1253
1254 /***************************************
1255  **************  SH2  ******************
1256  ***************************************/
1257 #if (defined (__sh2__) || defined(__sh3__) || defined(__SH4__) ) \
1258     && W_TYPE_SIZE == 32
1259 # define umul_ppmm(w1, w0, u, v) \
1260   __asm__ (                                                             \
1261         "dmulu.l %2,%3\n"  \
1262         "sts    macl,%1\n" \
1263         "sts    mach,%0"                                                \
1264            : "=r" ((USItype)(w1)),                                      \
1265              "=r" ((USItype)(w0))                                       \
1266            : "r" ((USItype)(u)),                                        \
1267              "r" ((USItype)(v))                                         \
1268            : "macl", "mach")
1269 # define UMUL_TIME 5
1270 #endif
1271
1272 /***************************************
1273  **************  SPARC  ****************
1274  ***************************************/
1275 #if defined (__sparc__) && W_TYPE_SIZE == 32
1276 # define add_ssaaaa(sh, sl, ah, al, bh, bl) \
1277   __asm__ ("addcc %r4,%5,%1\n" \
1278            "addx %r2,%3,%0"                                             \
1279            : "=r" ((USItype)(sh)),                                      \
1280              "=&r" ((USItype)(sl))                                      \
1281            : "%rJ" ((USItype)(ah)),                                     \
1282              "rI" ((USItype)(bh)),                                      \
1283              "%rJ" ((USItype)(al)),                                     \
1284              "rI" ((USItype)(bl))                                       \
1285            __CLOBBER_CC)
1286 # define sub_ddmmss(sh, sl, ah, al, bh, bl) \
1287   __asm__ ("subcc %r4,%5,%1\n" \
1288            "subx %r2,%3,%0"                                             \
1289            : "=r" ((USItype)(sh)),                                      \
1290              "=&r" ((USItype)(sl))                                      \
1291            : "rJ" ((USItype)(ah)),                                      \
1292              "rI" ((USItype)(bh)),                                      \
1293              "rJ" ((USItype)(al)),                                      \
1294              "rI" ((USItype)(bl))                                       \
1295            __CLOBBER_CC)
1296 # if defined (__sparc_v8__)
1297 /* Don't match immediate range because, 1) it is not often useful,
1298    2) the 'I' flag thinks of the range as a 13 bit signed interval,
1299    while we want to match a 13 bit interval, sign extended to 32 bits,
1300    but INTERPRETED AS UNSIGNED.  */
1301 #  define umul_ppmm(w1, w0, u, v) \
1302   __asm__ ("umul %2,%3,%1;rd %%y,%0"                                    \
1303            : "=r" ((USItype)(w1)),                                      \
1304              "=r" ((USItype)(w0))                                       \
1305            : "r" ((USItype)(u)),                                        \
1306              "r" ((USItype)(v)))
1307 #  define UMUL_TIME 5
1308 #  ifndef SUPERSPARC    /* SuperSPARC's udiv only handles 53 bit dividends */
1309 #   define udiv_qrnnd(q, r, n1, n0, d) \
1310   do {                                                                  \
1311     USItype __q;                                                        \
1312     __asm__ ("mov %1,%%y;nop;nop;nop;udiv %2,%3,%0"                     \
1313              : "=r" ((USItype)(__q))                                    \
1314              : "r" ((USItype)(n1)),                                     \
1315                "r" ((USItype)(n0)),                                     \
1316                "r" ((USItype)(d)));                                     \
1317     (r) = (n0) - __q * (d);                                             \
1318     (q) = __q;                                                          \
1319   } while (0)
1320 #   define UDIV_TIME 25
1321 #  endif /*!SUPERSPARC */
1322 # else /* ! __sparc_v8__ */
1323 #  if defined (__sparclite__)
1324 /* This has hardware multiply but not divide.  It also has two additional
1325    instructions scan (ffs from high bit) and divscc.  */
1326 #   define umul_ppmm(w1, w0, u, v) \
1327   __asm__ ("umul %2,%3,%1;rd %%y,%0"                                    \
1328            : "=r" ((USItype)(w1)),                                      \
1329              "=r" ((USItype)(w0))                                       \
1330            : "r" ((USItype)(u)),                                        \
1331              "r" ((USItype)(v)))
1332 #   define UMUL_TIME 5
1333 #   define udiv_qrnnd(q, r, n1, n0, d) \
1334   __asm__ ("! Inlined udiv_qrnnd                                     \n" \
1335  "        wr    %%g0,%2,%%y     ! Not a delayed write for sparclite  \n" \
1336  "        tst   %%g0                                                 \n" \
1337  "        divscc        %3,%4,%%g1                                   \n" \
1338  "        divscc        %%g1,%4,%%g1                                 \n" \
1339  "        divscc        %%g1,%4,%%g1                                 \n" \
1340  "        divscc        %%g1,%4,%%g1                                 \n" \
1341  "        divscc        %%g1,%4,%%g1                                 \n" \
1342  "        divscc        %%g1,%4,%%g1                                 \n" \
1343  "        divscc        %%g1,%4,%%g1                                 \n" \
1344  "        divscc        %%g1,%4,%%g1                                 \n" \
1345  "        divscc        %%g1,%4,%%g1                                 \n" \
1346  "        divscc        %%g1,%4,%%g1                                 \n" \
1347  "        divscc        %%g1,%4,%%g1                                 \n" \
1348  "        divscc        %%g1,%4,%%g1                                 \n" \
1349  "        divscc        %%g1,%4,%%g1                                 \n" \
1350  "        divscc        %%g1,%4,%%g1                                 \n" \
1351  "        divscc        %%g1,%4,%%g1                                 \n" \
1352  "        divscc        %%g1,%4,%%g1                                 \n" \
1353  "        divscc        %%g1,%4,%%g1                                 \n" \
1354  "        divscc        %%g1,%4,%%g1                                 \n" \
1355  "        divscc        %%g1,%4,%%g1                                 \n" \
1356  "        divscc        %%g1,%4,%%g1                                 \n" \
1357  "        divscc        %%g1,%4,%%g1                                 \n" \
1358  "        divscc        %%g1,%4,%%g1                                 \n" \
1359  "        divscc        %%g1,%4,%%g1                                 \n" \
1360  "        divscc        %%g1,%4,%%g1                                 \n" \
1361  "        divscc        %%g1,%4,%%g1                                 \n" \
1362  "        divscc        %%g1,%4,%%g1                                 \n" \
1363  "        divscc        %%g1,%4,%%g1                                 \n" \
1364  "        divscc        %%g1,%4,%%g1                                 \n" \
1365  "        divscc        %%g1,%4,%%g1                                 \n" \
1366  "        divscc        %%g1,%4,%%g1                                 \n" \
1367  "        divscc        %%g1,%4,%%g1                                 \n" \
1368  "        divscc        %%g1,%4,%0                                   \n" \
1369  "        rd    %%y,%1                                               \n" \
1370  "        bl,a 1f                                                    \n" \
1371  "        add   %1,%4,%1                                             \n" \
1372  "1:    ! End of inline udiv_qrnnd"                                     \
1373            : "=r" ((USItype)(q)),                                       \
1374              "=r" ((USItype)(r))                                        \
1375            : "r" ((USItype)(n1)),                                       \
1376              "r" ((USItype)(n0)),                                       \
1377              "rI" ((USItype)(d))                                        \
1378            : "%g1" __AND_CLOBBER_CC)
1379 #   define UDIV_TIME 37
1380 #   define count_leading_zeros(count, x) \
1381   __asm__ ("scan %1,0,%0"                                               \
1382            : "=r" ((USItype)(x))                                        \
1383            : "r" ((USItype)(count)))
1384 /* Early sparclites return 63 for an argument of 0, but they warn that future
1385    implementations might change this.  Therefore, leave COUNT_LEADING_ZEROS_0
1386    undefined.  */
1387 #  endif /* !__sparclite__ */
1388 # endif /* !__sparc_v8__ */
1389 /* Default to sparc v7 versions of umul_ppmm and udiv_qrnnd.  */
1390 # ifndef umul_ppmm
1391 #  define umul_ppmm(w1, w0, u, v) \
1392   __asm__ ("! Inlined umul_ppmm                                        \n" \
1393  "        wr    %%g0,%2,%%y     ! SPARC has 0-3 delay insn after a wr  \n" \
1394  "        sra   %3,31,%%g2      ! Don't move this insn                 \n" \
1395  "        and   %2,%%g2,%%g2    ! Don't move this insn                 \n" \
1396  "        andcc %%g0,0,%%g1     ! Don't move this insn                 \n" \
1397  "        mulscc        %%g1,%3,%%g1                                   \n" \
1398  "        mulscc        %%g1,%3,%%g1                                   \n" \
1399  "        mulscc        %%g1,%3,%%g1                                   \n" \
1400  "        mulscc        %%g1,%3,%%g1                                   \n" \
1401  "        mulscc        %%g1,%3,%%g1                                   \n" \
1402  "        mulscc        %%g1,%3,%%g1                                   \n" \
1403  "        mulscc        %%g1,%3,%%g1                                   \n" \
1404  "        mulscc        %%g1,%3,%%g1                                   \n" \
1405  "        mulscc        %%g1,%3,%%g1                                   \n" \
1406  "        mulscc        %%g1,%3,%%g1                                   \n" \
1407  "        mulscc        %%g1,%3,%%g1                                   \n" \
1408  "        mulscc        %%g1,%3,%%g1                                   \n" \
1409  "        mulscc        %%g1,%3,%%g1                                   \n" \
1410  "        mulscc        %%g1,%3,%%g1                                   \n" \
1411  "        mulscc        %%g1,%3,%%g1                                   \n" \
1412  "        mulscc        %%g1,%3,%%g1                                   \n" \
1413  "        mulscc        %%g1,%3,%%g1                                   \n" \
1414  "        mulscc        %%g1,%3,%%g1                                   \n" \
1415  "        mulscc        %%g1,%3,%%g1                                   \n" \
1416  "        mulscc        %%g1,%3,%%g1                                   \n" \
1417  "        mulscc        %%g1,%3,%%g1                                   \n" \
1418  "        mulscc        %%g1,%3,%%g1                                   \n" \
1419  "        mulscc        %%g1,%3,%%g1                                   \n" \
1420  "        mulscc        %%g1,%3,%%g1                                   \n" \
1421  "        mulscc        %%g1,%3,%%g1                                   \n" \
1422  "        mulscc        %%g1,%3,%%g1                                   \n" \
1423  "        mulscc        %%g1,%3,%%g1                                   \n" \
1424  "        mulscc        %%g1,%3,%%g1                                   \n" \
1425  "        mulscc        %%g1,%3,%%g1                                   \n" \
1426  "        mulscc        %%g1,%3,%%g1                                   \n" \
1427  "        mulscc        %%g1,%3,%%g1                                   \n" \
1428  "        mulscc        %%g1,%3,%%g1                                   \n" \
1429  "        mulscc        %%g1,0,%%g1                                    \n" \
1430  "        add   %%g1,%%g2,%0                                           \n" \
1431  "        rd    %%y,%1"                                                 \
1432            : "=r" ((USItype)(w1)),                                      \
1433              "=r" ((USItype)(w0))                                       \
1434            : "%rI" ((USItype)(u)),                                      \
1435              "r" ((USItype)(v))                                         \
1436            : "%g1", "%g2" __AND_CLOBBER_CC)
1437 #  define UMUL_TIME 39          /* 39 instructions */
1438 # endif /* umul_ppmm */
1439 # ifndef udiv_qrnnd
1440 #  ifndef LONGLONG_STANDALONE
1441 #   define udiv_qrnnd(q, r, n1, n0, d) \
1442   do { USItype __r;                                                     \
1443     (q) = __udiv_qrnnd (&__r, (n1), (n0), (d));                         \
1444     (r) = __r;                                                          \
1445   } while (0)
1446 extern USItype __udiv_qrnnd ();
1447 #   define UDIV_TIME 140
1448 #  endif /* LONGLONG_STANDALONE */
1449 # endif /* udiv_qrnnd */
1450 #endif /* __sparc__ */
1451
1452
1453 /***************************************
1454  **************  VAX  ******************
1455  ***************************************/
1456 #if defined (__vax__) && W_TYPE_SIZE == 32
1457 # define add_ssaaaa(sh, sl, ah, al, bh, bl) \
1458   __asm__ ("addl2 %5,%1\n" \
1459            "adwc %3,%0"                                                 \
1460            : "=g" ((USItype)(sh)),                                      \
1461              "=&g" ((USItype)(sl))                                      \
1462            : "%0" ((USItype)(ah)),                                      \
1463              "g" ((USItype)(bh)),                                       \
1464              "%1" ((USItype)(al)),                                      \
1465              "g" ((USItype)(bl)))
1466 # define sub_ddmmss(sh, sl, ah, al, bh, bl) \
1467   __asm__ ("subl2 %5,%1\n" \
1468            "sbwc %3,%0"                                                 \
1469            : "=g" ((USItype)(sh)),                                      \
1470              "=&g" ((USItype)(sl))                                      \
1471            : "0" ((USItype)(ah)),                                       \
1472              "g" ((USItype)(bh)),                                       \
1473              "1" ((USItype)(al)),                                       \
1474              "g" ((USItype)(bl)))
1475 # define umul_ppmm(xh, xl, m0, m1) \
1476   do {                                                                  \
1477     union {UDItype __ll;                                                \
1478            struct {USItype __l, __h;} __i;                              \
1479           } __xx;                                                       \
1480     USItype __m0 = (m0), __m1 = (m1);                                   \
1481     __asm__ ("emul %1,%2,$0,%0"                                         \
1482              : "=g" (__xx.__ll)                                         \
1483              : "g" (__m0),                                              \
1484                "g" (__m1));                                             \
1485     (xh) = __xx.__i.__h; (xl) = __xx.__i.__l;                           \
1486     (xh) += ((((SItype) __m0 >> 31) & __m1)                             \
1487              + (((SItype) __m1 >> 31) & __m0));                         \
1488   } while (0)
1489 # define sdiv_qrnnd(q, r, n1, n0, d) \
1490   do {                                                                  \
1491     union {DItype __ll;                                                 \
1492            struct {SItype __l, __h;} __i;                               \
1493           } __xx;                                                       \
1494     __xx.__i.__h = n1; __xx.__i.__l = n0;                               \
1495     __asm__ ("ediv %3,%2,%0,%1"                                         \
1496              : "=g" (q), "=g" (r)                                       \
1497              : "g" (__xx.__ll), "g" (d));                               \
1498   } while (0)
1499 #endif /* __vax__ */
1500
1501
1502 /***************************************
1503  **************  Z8000  ****************
1504  ***************************************/
1505 #if defined (__z8000__) && W_TYPE_SIZE == 16
1506 # define add_ssaaaa(sh, sl, ah, al, bh, bl) \
1507   __asm__ ("add %H1,%H5\n\tadc  %H0,%H3"                                \
1508            : "=r" ((unsigned int)(sh)),                                 \
1509              "=&r" ((unsigned int)(sl))                                 \
1510            : "%0" ((unsigned int)(ah)),                                 \
1511              "r" ((unsigned int)(bh)),                                  \
1512              "%1" ((unsigned int)(al)),                                 \
1513              "rQR" ((unsigned int)(bl)))
1514 # define sub_ddmmss(sh, sl, ah, al, bh, bl) \
1515   __asm__ ("sub %H1,%H5\n\tsbc  %H0,%H3"                                \
1516            : "=r" ((unsigned int)(sh)),                                 \
1517              "=&r" ((unsigned int)(sl))                                 \
1518            : "0" ((unsigned int)(ah)),                                  \
1519              "r" ((unsigned int)(bh)),                                  \
1520              "1" ((unsigned int)(al)),                                  \
1521              "rQR" ((unsigned int)(bl)))
1522 # define umul_ppmm(xh, xl, m0, m1) \
1523   do {                                                                  \
1524     union {long int __ll;                                               \
1525            struct {unsigned int __h, __l;} __i;                         \
1526           } __xx;                                                       \
1527     unsigned int __m0 = (m0), __m1 = (m1);                              \
1528     __asm__ ("mult      %S0,%H3"                                        \
1529              : "=r" (__xx.__i.__h),                                     \
1530                "=r" (__xx.__i.__l)                                      \
1531              : "%1" (__m0),                                             \
1532                "rQR" (__m1));                                           \
1533     (xh) = __xx.__i.__h; (xl) = __xx.__i.__l;                           \
1534     (xh) += ((((signed int) __m0 >> 15) & __m1)                         \
1535              + (((signed int) __m1 >> 15) & __m0));                     \
1536   } while (0)
1537 #endif /* __z8000__ */
1538
1539
1540 /***************************************
1541  *****  End CPU Specific Versions  *****
1542  ***************************************/
1543
1544 #endif /* __GNUC__ */
1545 #endif /* !__riscos__ */
1546
1547
1548 /***************************************
1549  ***********  Generic Versions  ********
1550  ***************************************/
1551 #if !defined (umul_ppmm) && defined (__umulsidi3)
1552 #  define umul_ppmm(ph, pl, m0, m1) \
1553   {                                                                     \
1554     UDWtype __ll = __umulsidi3 (m0, m1);                                \
1555     ph = (UWtype) (__ll >> W_TYPE_SIZE);                                \
1556     pl = (UWtype) __ll;                                                 \
1557   }
1558 #endif
1559
1560 #if !defined (__umulsidi3)
1561 #  define __umulsidi3(u, v) \
1562   ({UWtype __hi, __lo;                                                  \
1563     umul_ppmm (__hi, __lo, u, v);                                       \
1564     ((UDWtype) __hi << W_TYPE_SIZE) | __lo; })
1565 #endif
1566
1567 /* If this machine has no inline assembler, use C macros.  */
1568
1569 #if !defined (add_ssaaaa)
1570 #  define add_ssaaaa(sh, sl, ah, al, bh, bl) \
1571   do {                                                                  \
1572     UWtype __x;                                                         \
1573     __x = (al) + (bl);                                                  \
1574     (sh) = (ah) + (bh) + (__x < (al));                                  \
1575     (sl) = __x;                                                         \
1576   } while (0)
1577 #endif
1578
1579 #if !defined (sub_ddmmss)
1580 #  define sub_ddmmss(sh, sl, ah, al, bh, bl) \
1581   do {                                                                  \
1582     UWtype __x;                                                         \
1583     __x = (al) - (bl);                                                  \
1584     (sh) = (ah) - (bh) - (__x > (al));                                  \
1585     (sl) = __x;                                                         \
1586   } while (0)
1587 #endif
1588
1589 #if !defined (umul_ppmm)
1590 #  define umul_ppmm(w1, w0, u, v)                                       \
1591   do {                                                                  \
1592     UWtype __x0, __x1, __x2, __x3;                                      \
1593     UHWtype __ul, __vl, __uh, __vh;                                     \
1594     UWtype __u = (u), __v = (v);                                        \
1595                                                                         \
1596     __ul = __ll_lowpart (__u);                                          \
1597     __uh = __ll_highpart (__u);                                         \
1598     __vl = __ll_lowpart (__v);                                          \
1599     __vh = __ll_highpart (__v);                                         \
1600                                                                         \
1601     __x0 = (UWtype) __ul * __vl;                                        \
1602     __x1 = (UWtype) __ul * __vh;                                        \
1603     __x2 = (UWtype) __uh * __vl;                                        \
1604     __x3 = (UWtype) __uh * __vh;                                        \
1605                                                                         \
1606     __x1 += __ll_highpart (__x0);/* this can't give carry */            \
1607     __x1 += __x2;               /* but this indeed can */               \
1608     if (__x1 < __x2)            /* did we get it? */                    \
1609       __x3 += __ll_B;           /* yes, add it in the proper pos. */    \
1610                                                                         \
1611     (w1) = __x3 + __ll_highpart (__x1);                                 \
1612     (w0) = (__ll_lowpart (__x1) << W_TYPE_SIZE/2) + __ll_lowpart (__x0);\
1613   } while (0)
1614 #endif
1615
1616 #if !defined (umul_ppmm)
1617 #  define smul_ppmm(w1, w0, u, v)                                       \
1618   do {                                                                  \
1619     UWtype __w1;                                                        \
1620     UWtype __m0 = (u), __m1 = (v);                                      \
1621     umul_ppmm (__w1, w0, __m0, __m1);                                   \
1622     (w1) = __w1 - (-(__m0 >> (W_TYPE_SIZE - 1)) & __m1)                 \
1623                 - (-(__m1 >> (W_TYPE_SIZE - 1)) & __m0);                \
1624   } while (0)
1625 #endif
1626
1627 /* Define this unconditionally, so it can be used for debugging.  */
1628 #define __udiv_qrnnd_c(q, r, n1, n0, d) \
1629   do {                                                                  \
1630     UWtype __d1, __d0, __q1, __q0, __r1, __r0, __m;                     \
1631     __d1 = __ll_highpart (d);                                           \
1632     __d0 = __ll_lowpart (d);                                            \
1633                                                                         \
1634     __r1 = (n1) % __d1;                                                 \
1635     __q1 = (n1) / __d1;                                                 \
1636     __m = (UWtype) __q1 * __d0;                                         \
1637     __r1 = __r1 * __ll_B | __ll_highpart (n0);                          \
1638     if (__r1 < __m)                                                     \
1639       {                                                                 \
1640         __q1--, __r1 += (d);                                            \
1641         if (__r1 >= (d)) /* i.e. we didn't get carry when adding to __r1 */\
1642           if (__r1 < __m)                                               \
1643             __q1--, __r1 += (d);                                        \
1644       }                                                                 \
1645     __r1 -= __m;                                                        \
1646                                                                         \
1647     __r0 = __r1 % __d1;                                                 \
1648     __q0 = __r1 / __d1;                                                 \
1649     __m = (UWtype) __q0 * __d0;                                         \
1650     __r0 = __r0 * __ll_B | __ll_lowpart (n0);                           \
1651     if (__r0 < __m)                                                     \
1652       {                                                                 \
1653         __q0--, __r0 += (d);                                            \
1654         if (__r0 >= (d))                                                \
1655           if (__r0 < __m)                                               \
1656             __q0--, __r0 += (d);                                        \
1657       }                                                                 \
1658     __r0 -= __m;                                                        \
1659                                                                         \
1660     (q) = (UWtype) __q1 * __ll_B | __q0;                                \
1661     (r) = __r0;                                                         \
1662   } while (0)
1663
1664 /* If the processor has no udiv_qrnnd but sdiv_qrnnd, go through
1665    __udiv_w_sdiv (defined in libgcc or elsewhere).  */
1666 #if !defined (udiv_qrnnd) && defined (sdiv_qrnnd)
1667 #  define udiv_qrnnd(q, r, nh, nl, d) \
1668   do {                                                                  \
1669     UWtype __r;                                                         \
1670     (q) = __MPN(udiv_w_sdiv) (&__r, nh, nl, d);                         \
1671     (r) = __r;                                                          \
1672   } while (0)
1673 #endif
1674
1675 /* If udiv_qrnnd was not defined for this processor, use __udiv_qrnnd_c.  */
1676 #if !defined (udiv_qrnnd)
1677 #  define UDIV_NEEDS_NORMALIZATION 1
1678 #  define udiv_qrnnd __udiv_qrnnd_c
1679 #endif
1680
1681 #if !defined (count_leading_zeros)
1682 extern
1683 #  ifdef __STDC__
1684 const
1685 #  endif
1686 unsigned char _gcry_clz_tab[];
1687 #  define MPI_INTERNAL_NEED_CLZ_TAB 1
1688 #  define count_leading_zeros(count, x) \
1689   do {                                                                  \
1690     UWtype __xr = (x);                                                  \
1691     UWtype __a;                                                         \
1692                                                                         \
1693     if (W_TYPE_SIZE <= 32)                                              \
1694       {                                                                 \
1695         __a = __xr < ((UWtype) 1 << 2*__BITS4)                          \
1696           ? (__xr < ((UWtype) 1 << __BITS4) ? 0 : __BITS4)              \
1697           : (__xr < ((UWtype) 1 << 3*__BITS4) ?  2*__BITS4 : 3*__BITS4);\
1698       }                                                                 \
1699     else                                                                \
1700       {                                                                 \
1701         for (__a = W_TYPE_SIZE - 8; __a > 0; __a -= 8)                  \
1702           if (((__xr >> __a) & 0xff) != 0)                              \
1703             break;                                                      \
1704       }                                                                 \
1705                                                                         \
1706     (count) = W_TYPE_SIZE - (_gcry_clz_tab[__xr >> __a] + __a);         \
1707   } while (0)
1708 /* This version gives a well-defined value for zero. */
1709 #  define COUNT_LEADING_ZEROS_0 W_TYPE_SIZE
1710 #endif /* !count_leading_zeros */
1711
1712 #if !defined (count_trailing_zeros)
1713 /* Define count_trailing_zeros using count_leading_zeros.  The latter might be
1714    defined in asm, but if it is not, the C version above is good enough.  */
1715 #  define count_trailing_zeros(count, x) \
1716   do {                                                                  \
1717     UWtype __ctz_x = (x);                                               \
1718     UWtype __ctz_c;                                                     \
1719     count_leading_zeros (__ctz_c, __ctz_x & -__ctz_x);                  \
1720     (count) = W_TYPE_SIZE - 1 - __ctz_c;                                \
1721   } while (0)
1722 #endif /* !count_trailing_zeros */
1723
1724 #ifndef UDIV_NEEDS_NORMALIZATION
1725 #  define UDIV_NEEDS_NORMALIZATION 0
1726 #endif
1727
1728 /***************************************
1729  ******  longlong.h ends here  *********
1730  ***************************************/