1 /* longlong.h -- definitions for mixed size 32/64 bit arithmetic.
2 Copyright (C) 1991, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2004,
3 2005 Free Software Foundation, Inc.
5 This definition file is free software; you can redistribute it
6 and/or modify it under the terms of the GNU General Public
7 License as published by the Free Software Foundation; either
8 version 2, or (at your option) any later version.
10 This definition file is distributed in the hope that it will be
11 useful, but WITHOUT ANY WARRANTY; without even the implied
12 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA. */
20 /* You have to define the following before including this file:
22 UWtype -- An unsigned type, default type for operations (typically a "word")
23 UHWtype -- An unsigned type, at least half the size of UWtype.
24 UDWtype -- An unsigned type, at least twice as large a UWtype
25 W_TYPE_SIZE -- size in bits of UWtype
27 UQItype -- Unsigned 8 bit type.
28 SItype, USItype -- Signed and unsigned 32 bit types.
29 DItype, UDItype -- Signed and unsigned 64 bit types.
31 On a 32 bit machine UWtype should typically be USItype;
32 on a 64 bit machine, UWtype should typically be UDItype. */
34 #define __BITS4 (W_TYPE_SIZE / 4)
35 #define __ll_B ((UWtype) 1 << (W_TYPE_SIZE / 2))
36 #define __ll_lowpart(t) ((UWtype) (t) & (__ll_B - 1))
37 #define __ll_highpart(t) ((UWtype) (t) >> (W_TYPE_SIZE / 2))
40 #define W_TYPE_SIZE 32
41 #define UWtype USItype
42 #define UHWtype USItype
43 #define UDWtype UDItype
46 extern const UQItype __clz_tab[256];
48 /* Define auxiliary asm macros.
50 1) umul_ppmm(high_prod, low_prod, multiplier, multiplicand) multiplies two
51 UWtype integers MULTIPLIER and MULTIPLICAND, and generates a two UWtype
52 word product in HIGH_PROD and LOW_PROD.
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.
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.
65 4) sdiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
66 denominator). Like udiv_qrnnd but the numbers are signed. The quotient
69 5) count_leading_zeros(count, x) counts the number of zero-bits from the
70 msb to the first nonzero 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.
74 6) count_trailing_zeros(count, x) like count_leading_zeros, but counts
75 from the least significant end.
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.
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,
90 If any of these macros are left undefined for a particular CPU,
93 /* The CPUs come in alphabetical order below.
95 Please add support for more CPUs here, or improve the current support
97 (E.g. WE32100, IBM360.) */
99 /* Snipped per CPU support */
101 /* If this machine has no inline assembler, use C macros. */
103 #if !defined (add_ssaaaa)
104 #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
108 (sh) = (ah) + (bh) + (__x < (al)); \
113 #if !defined (sub_ddmmss)
114 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
118 (sh) = (ah) - (bh) - (__x > (al)); \
123 /* If we lack umul_ppmm but have smul_ppmm, define umul_ppmm in terms of
125 #if !defined (umul_ppmm) && defined (smul_ppmm)
126 #define umul_ppmm(w1, w0, u, v) \
129 UWtype __xm0 = (u), __xm1 = (v); \
130 smul_ppmm (__w1, w0, __xm0, __xm1); \
131 (w1) = __w1 + (-(__xm0 >> (W_TYPE_SIZE - 1)) & __xm1) \
132 + (-(__xm1 >> (W_TYPE_SIZE - 1)) & __xm0); \
136 /* If we still don't have umul_ppmm, define it using plain C. */
137 #if !defined (umul_ppmm)
138 #define umul_ppmm(w1, w0, u, v) \
140 UWtype __x0, __x1, __x2, __x3; \
141 UHWtype __ul, __vl, __uh, __vh; \
143 __ul = __ll_lowpart (u); \
144 __uh = __ll_highpart (u); \
145 __vl = __ll_lowpart (v); \
146 __vh = __ll_highpart (v); \
148 __x0 = (UWtype) __ul * __vl; \
149 __x1 = (UWtype) __ul * __vh; \
150 __x2 = (UWtype) __uh * __vl; \
151 __x3 = (UWtype) __uh * __vh; \
153 __x1 += __ll_highpart (__x0);/* this can't give carry */ \
154 __x1 += __x2; /* but this indeed can */ \
155 if (__x1 < __x2) /* did we get it? */ \
156 __x3 += __ll_B; /* yes, add it in the proper pos. */ \
158 (w1) = __x3 + __ll_highpart (__x1); \
159 (w0) = __ll_lowpart (__x1) * __ll_B + __ll_lowpart (__x0); \
163 #if !defined (__umulsidi3)
164 #define __umulsidi3(u, v) \
166 umul_ppmm (__w.s.high, __w.s.low, u, v); \
170 /* Define this unconditionally, so it can be used for debugging. */
171 #define __udiv_qrnnd_c(q, r, n1, n0, d) \
173 UWtype __d1, __d0, __q1, __q0; \
174 UWtype __r1, __r0, __m; \
175 __d1 = __ll_highpart (d); \
176 __d0 = __ll_lowpart (d); \
178 __r1 = (n1) % __d1; \
179 __q1 = (n1) / __d1; \
180 __m = (UWtype) __q1 * __d0; \
181 __r1 = __r1 * __ll_B | __ll_highpart (n0); \
184 __q1--, __r1 += (d); \
185 if (__r1 >= (d)) /* i.e. we didn't get carry when adding to __r1 */\
187 __q1--, __r1 += (d); \
191 __r0 = __r1 % __d1; \
192 __q0 = __r1 / __d1; \
193 __m = (UWtype) __q0 * __d0; \
194 __r0 = __r0 * __ll_B | __ll_lowpart (n0); \
197 __q0--, __r0 += (d); \
200 __q0--, __r0 += (d); \
204 (q) = (UWtype) __q1 * __ll_B | __q0; \
208 /* If the processor has no udiv_qrnnd but sdiv_qrnnd, go through
209 __udiv_w_sdiv (defined in libgcc or elsewhere). */
210 #if !defined (udiv_qrnnd) && defined (sdiv_qrnnd)
211 #define udiv_qrnnd(q, r, nh, nl, d) \
214 (q) = __udiv_w_sdiv (&__r, nh, nl, d); \
219 /* If udiv_qrnnd was not defined for this processor, use __udiv_qrnnd_c. */
220 #if !defined (udiv_qrnnd)
221 #define UDIV_NEEDS_NORMALIZATION 1
222 #define udiv_qrnnd __udiv_qrnnd_c
225 #if !defined (count_leading_zeros)
226 #define count_leading_zeros(count, x) \
231 if (W_TYPE_SIZE <= 32) \
233 __a = __xr < ((UWtype)1<<2*__BITS4) \
234 ? (__xr < ((UWtype)1<<__BITS4) ? 0 : __BITS4) \
235 : (__xr < ((UWtype)1<<3*__BITS4) ? 2*__BITS4 : 3*__BITS4); \
239 for (__a = W_TYPE_SIZE - 8; __a > 0; __a -= 8) \
240 if (((__xr >> __a) & 0xff) != 0) \
244 (count) = W_TYPE_SIZE - (__clz_tab[__xr >> __a] + __a); \
246 #define COUNT_LEADING_ZEROS_0 W_TYPE_SIZE
249 #if !defined (count_trailing_zeros)
250 /* Define count_trailing_zeros using count_leading_zeros. The latter might be
251 defined in asm, but if it is not, the C version above is good enough. */
252 #define count_trailing_zeros(count, x) \
254 UWtype __ctz_x = (x); \
256 count_leading_zeros (__ctz_c, __ctz_x & -__ctz_x); \
257 (count) = W_TYPE_SIZE - 1 - __ctz_c; \
261 #ifndef UDIV_NEEDS_NORMALIZATION
262 #define UDIV_NEEDS_NORMALIZATION 0