xref: /OK3568_Linux_fs/u-boot/arch/nios2/lib/longlong.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* longlong.h -- definitions for mixed size 32/64 bit arithmetic.
2*4882a593Smuzhiyun    Copyright (C) 1991, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2004,
3*4882a593Smuzhiyun    2005  Free Software Foundation, Inc.
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun /* You have to define the following before including this file:
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun    UWtype -- An unsigned type, default type for operations (typically a "word")
11*4882a593Smuzhiyun    UHWtype -- An unsigned type, at least half the size of UWtype.
12*4882a593Smuzhiyun    UDWtype -- An unsigned type, at least twice as large a UWtype
13*4882a593Smuzhiyun    W_TYPE_SIZE -- size in bits of UWtype
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun    UQItype -- Unsigned 8 bit type.
16*4882a593Smuzhiyun    SItype, USItype -- Signed and unsigned 32 bit types.
17*4882a593Smuzhiyun    DItype, UDItype -- Signed and unsigned 64 bit types.
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun    On a 32 bit machine UWtype should typically be USItype;
20*4882a593Smuzhiyun    on a 64 bit machine, UWtype should typically be UDItype.  */
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #define __BITS4 (W_TYPE_SIZE / 4)
23*4882a593Smuzhiyun #define __ll_B ((UWtype) 1 << (W_TYPE_SIZE / 2))
24*4882a593Smuzhiyun #define __ll_lowpart(t) ((UWtype) (t) & (__ll_B - 1))
25*4882a593Smuzhiyun #define __ll_highpart(t) ((UWtype) (t) >> (W_TYPE_SIZE / 2))
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #ifndef W_TYPE_SIZE
28*4882a593Smuzhiyun #define W_TYPE_SIZE	32
29*4882a593Smuzhiyun #define UWtype		USItype
30*4882a593Smuzhiyun #define UHWtype		USItype
31*4882a593Smuzhiyun #define UDWtype		UDItype
32*4882a593Smuzhiyun #endif
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun extern const UQItype __clz_tab[256];
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun /* Define auxiliary asm macros.
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun    1) umul_ppmm(high_prod, low_prod, multiplier, multiplicand) multiplies two
39*4882a593Smuzhiyun    UWtype integers MULTIPLIER and MULTIPLICAND, and generates a two UWtype
40*4882a593Smuzhiyun    word product in HIGH_PROD and LOW_PROD.
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun    2) __umulsidi3(a,b) multiplies two UWtype integers A and B, and returns a
43*4882a593Smuzhiyun    UDWtype product.  This is just a variant of umul_ppmm.
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun    3) udiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
46*4882a593Smuzhiyun    denominator) divides a UDWtype, composed by the UWtype integers
47*4882a593Smuzhiyun    HIGH_NUMERATOR and LOW_NUMERATOR, by DENOMINATOR and places the quotient
48*4882a593Smuzhiyun    in QUOTIENT and the remainder in REMAINDER.  HIGH_NUMERATOR must be less
49*4882a593Smuzhiyun    than DENOMINATOR for correct operation.  If, in addition, the most
50*4882a593Smuzhiyun    significant bit of DENOMINATOR must be 1, then the pre-processor symbol
51*4882a593Smuzhiyun    UDIV_NEEDS_NORMALIZATION is defined to 1.
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun    4) sdiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
54*4882a593Smuzhiyun    denominator).  Like udiv_qrnnd but the numbers are signed.  The quotient
55*4882a593Smuzhiyun    is rounded towards 0.
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun    5) count_leading_zeros(count, x) counts the number of zero-bits from the
58*4882a593Smuzhiyun    msb to the first nonzero bit in the UWtype X.  This is the number of
59*4882a593Smuzhiyun    steps X needs to be shifted left to set the msb.  Undefined for X == 0,
60*4882a593Smuzhiyun    unless the symbol COUNT_LEADING_ZEROS_0 is defined to some value.
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun    6) count_trailing_zeros(count, x) like count_leading_zeros, but counts
63*4882a593Smuzhiyun    from the least significant end.
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun    7) add_ssaaaa(high_sum, low_sum, high_addend_1, low_addend_1,
66*4882a593Smuzhiyun    high_addend_2, low_addend_2) adds two UWtype integers, composed by
67*4882a593Smuzhiyun    HIGH_ADDEND_1 and LOW_ADDEND_1, and HIGH_ADDEND_2 and LOW_ADDEND_2
68*4882a593Smuzhiyun    respectively.  The result is placed in HIGH_SUM and LOW_SUM.  Overflow
69*4882a593Smuzhiyun    (i.e. carry out) is not stored anywhere, and is lost.
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun    8) sub_ddmmss(high_difference, low_difference, high_minuend, low_minuend,
72*4882a593Smuzhiyun    high_subtrahend, low_subtrahend) subtracts two two-word UWtype integers,
73*4882a593Smuzhiyun    composed by HIGH_MINUEND_1 and LOW_MINUEND_1, and HIGH_SUBTRAHEND_2 and
74*4882a593Smuzhiyun    LOW_SUBTRAHEND_2 respectively.  The result is placed in HIGH_DIFFERENCE
75*4882a593Smuzhiyun    and LOW_DIFFERENCE.  Overflow (i.e. carry out) is not stored anywhere,
76*4882a593Smuzhiyun    and is lost.
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun    If any of these macros are left undefined for a particular CPU,
79*4882a593Smuzhiyun    C macros are used.  */
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun /* The CPUs come in alphabetical order below.
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun    Please add support for more CPUs here, or improve the current support
84*4882a593Smuzhiyun    for the CPUs below!
85*4882a593Smuzhiyun    (E.g. WE32100, IBM360.)  */
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun /* Snipped per CPU support */
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun /* If this machine has no inline assembler, use C macros.  */
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun #if !defined (add_ssaaaa)
92*4882a593Smuzhiyun #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
93*4882a593Smuzhiyun   do {									\
94*4882a593Smuzhiyun     UWtype __x;								\
95*4882a593Smuzhiyun     __x = (al) + (bl);							\
96*4882a593Smuzhiyun     (sh) = (ah) + (bh) + (__x < (al));					\
97*4882a593Smuzhiyun     (sl) = __x;								\
98*4882a593Smuzhiyun   } while (0)
99*4882a593Smuzhiyun #endif
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun #if !defined (sub_ddmmss)
102*4882a593Smuzhiyun #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
103*4882a593Smuzhiyun   do {									\
104*4882a593Smuzhiyun     UWtype __x;								\
105*4882a593Smuzhiyun     __x = (al) - (bl);							\
106*4882a593Smuzhiyun     (sh) = (ah) - (bh) - (__x > (al));					\
107*4882a593Smuzhiyun     (sl) = __x;								\
108*4882a593Smuzhiyun   } while (0)
109*4882a593Smuzhiyun #endif
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun /* If we lack umul_ppmm but have smul_ppmm, define umul_ppmm in terms of
112*4882a593Smuzhiyun    smul_ppmm.  */
113*4882a593Smuzhiyun #if !defined (umul_ppmm) && defined (smul_ppmm)
114*4882a593Smuzhiyun #define umul_ppmm(w1, w0, u, v)						\
115*4882a593Smuzhiyun   do {									\
116*4882a593Smuzhiyun     UWtype __w1;							\
117*4882a593Smuzhiyun     UWtype __xm0 = (u), __xm1 = (v);					\
118*4882a593Smuzhiyun     smul_ppmm (__w1, w0, __xm0, __xm1);					\
119*4882a593Smuzhiyun     (w1) = __w1 + (-(__xm0 >> (W_TYPE_SIZE - 1)) & __xm1)		\
120*4882a593Smuzhiyun 		+ (-(__xm1 >> (W_TYPE_SIZE - 1)) & __xm0);		\
121*4882a593Smuzhiyun   } while (0)
122*4882a593Smuzhiyun #endif
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun /* If we still don't have umul_ppmm, define it using plain C.  */
125*4882a593Smuzhiyun #if !defined (umul_ppmm)
126*4882a593Smuzhiyun #define umul_ppmm(w1, w0, u, v)						\
127*4882a593Smuzhiyun   do {									\
128*4882a593Smuzhiyun     UWtype __x0, __x1, __x2, __x3;					\
129*4882a593Smuzhiyun     UHWtype __ul, __vl, __uh, __vh;					\
130*4882a593Smuzhiyun 									\
131*4882a593Smuzhiyun     __ul = __ll_lowpart (u);						\
132*4882a593Smuzhiyun     __uh = __ll_highpart (u);						\
133*4882a593Smuzhiyun     __vl = __ll_lowpart (v);						\
134*4882a593Smuzhiyun     __vh = __ll_highpart (v);						\
135*4882a593Smuzhiyun 									\
136*4882a593Smuzhiyun     __x0 = (UWtype) __ul * __vl;					\
137*4882a593Smuzhiyun     __x1 = (UWtype) __ul * __vh;					\
138*4882a593Smuzhiyun     __x2 = (UWtype) __uh * __vl;					\
139*4882a593Smuzhiyun     __x3 = (UWtype) __uh * __vh;					\
140*4882a593Smuzhiyun 									\
141*4882a593Smuzhiyun     __x1 += __ll_highpart (__x0);/* this can't give carry */		\
142*4882a593Smuzhiyun     __x1 += __x2;		/* but this indeed can */		\
143*4882a593Smuzhiyun     if (__x1 < __x2)		/* did we get it? */			\
144*4882a593Smuzhiyun       __x3 += __ll_B;		/* yes, add it in the proper pos.  */	\
145*4882a593Smuzhiyun 									\
146*4882a593Smuzhiyun     (w1) = __x3 + __ll_highpart (__x1);					\
147*4882a593Smuzhiyun     (w0) = __ll_lowpart (__x1) * __ll_B + __ll_lowpart (__x0);		\
148*4882a593Smuzhiyun   } while (0)
149*4882a593Smuzhiyun #endif
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun #if !defined (__umulsidi3)
152*4882a593Smuzhiyun #define __umulsidi3(u, v) \
153*4882a593Smuzhiyun   ({DWunion __w;							\
154*4882a593Smuzhiyun     umul_ppmm (__w.s.high, __w.s.low, u, v);				\
155*4882a593Smuzhiyun     __w.ll; })
156*4882a593Smuzhiyun #endif
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun /* Define this unconditionally, so it can be used for debugging.  */
159*4882a593Smuzhiyun #define __udiv_qrnnd_c(q, r, n1, n0, d) \
160*4882a593Smuzhiyun   do {									\
161*4882a593Smuzhiyun     UWtype __d1, __d0, __q1, __q0;					\
162*4882a593Smuzhiyun     UWtype __r1, __r0, __m;						\
163*4882a593Smuzhiyun     __d1 = __ll_highpart (d);						\
164*4882a593Smuzhiyun     __d0 = __ll_lowpart (d);						\
165*4882a593Smuzhiyun 									\
166*4882a593Smuzhiyun     __r1 = (n1) % __d1;							\
167*4882a593Smuzhiyun     __q1 = (n1) / __d1;							\
168*4882a593Smuzhiyun     __m = (UWtype) __q1 * __d0;						\
169*4882a593Smuzhiyun     __r1 = __r1 * __ll_B | __ll_highpart (n0);				\
170*4882a593Smuzhiyun     if (__r1 < __m)							\
171*4882a593Smuzhiyun       {									\
172*4882a593Smuzhiyun 	__q1--, __r1 += (d);						\
173*4882a593Smuzhiyun 	if (__r1 >= (d)) /* i.e. we didn't get carry when adding to __r1 */\
174*4882a593Smuzhiyun 	  if (__r1 < __m)						\
175*4882a593Smuzhiyun 	    __q1--, __r1 += (d);					\
176*4882a593Smuzhiyun       }									\
177*4882a593Smuzhiyun     __r1 -= __m;							\
178*4882a593Smuzhiyun 									\
179*4882a593Smuzhiyun     __r0 = __r1 % __d1;							\
180*4882a593Smuzhiyun     __q0 = __r1 / __d1;							\
181*4882a593Smuzhiyun     __m = (UWtype) __q0 * __d0;						\
182*4882a593Smuzhiyun     __r0 = __r0 * __ll_B | __ll_lowpart (n0);				\
183*4882a593Smuzhiyun     if (__r0 < __m)							\
184*4882a593Smuzhiyun       {									\
185*4882a593Smuzhiyun 	__q0--, __r0 += (d);						\
186*4882a593Smuzhiyun 	if (__r0 >= (d))						\
187*4882a593Smuzhiyun 	  if (__r0 < __m)						\
188*4882a593Smuzhiyun 	    __q0--, __r0 += (d);					\
189*4882a593Smuzhiyun       }									\
190*4882a593Smuzhiyun     __r0 -= __m;							\
191*4882a593Smuzhiyun 									\
192*4882a593Smuzhiyun     (q) = (UWtype) __q1 * __ll_B | __q0;				\
193*4882a593Smuzhiyun     (r) = __r0;								\
194*4882a593Smuzhiyun   } while (0)
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun /* If the processor has no udiv_qrnnd but sdiv_qrnnd, go through
197*4882a593Smuzhiyun    __udiv_w_sdiv (defined in libgcc or elsewhere).  */
198*4882a593Smuzhiyun #if !defined (udiv_qrnnd) && defined (sdiv_qrnnd)
199*4882a593Smuzhiyun #define udiv_qrnnd(q, r, nh, nl, d) \
200*4882a593Smuzhiyun   do {									\
201*4882a593Smuzhiyun     USItype __r;							\
202*4882a593Smuzhiyun     (q) = __udiv_w_sdiv (&__r, nh, nl, d);				\
203*4882a593Smuzhiyun     (r) = __r;								\
204*4882a593Smuzhiyun   } while (0)
205*4882a593Smuzhiyun #endif
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun /* If udiv_qrnnd was not defined for this processor, use __udiv_qrnnd_c.  */
208*4882a593Smuzhiyun #if !defined (udiv_qrnnd)
209*4882a593Smuzhiyun #define UDIV_NEEDS_NORMALIZATION 1
210*4882a593Smuzhiyun #define udiv_qrnnd __udiv_qrnnd_c
211*4882a593Smuzhiyun #endif
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun #if !defined (count_leading_zeros)
214*4882a593Smuzhiyun #define count_leading_zeros(count, x) \
215*4882a593Smuzhiyun   do {									\
216*4882a593Smuzhiyun     UWtype __xr = (x);							\
217*4882a593Smuzhiyun     UWtype __a;								\
218*4882a593Smuzhiyun 									\
219*4882a593Smuzhiyun     if (W_TYPE_SIZE <= 32)						\
220*4882a593Smuzhiyun       {									\
221*4882a593Smuzhiyun 	__a = __xr < ((UWtype)1<<2*__BITS4)				\
222*4882a593Smuzhiyun 	  ? (__xr < ((UWtype)1<<__BITS4) ? 0 : __BITS4)			\
223*4882a593Smuzhiyun 	  : (__xr < ((UWtype)1<<3*__BITS4) ?  2*__BITS4 : 3*__BITS4);	\
224*4882a593Smuzhiyun       }									\
225*4882a593Smuzhiyun     else								\
226*4882a593Smuzhiyun       {									\
227*4882a593Smuzhiyun 	for (__a = W_TYPE_SIZE - 8; __a > 0; __a -= 8)			\
228*4882a593Smuzhiyun 	  if (((__xr >> __a) & 0xff) != 0)				\
229*4882a593Smuzhiyun 	    break;							\
230*4882a593Smuzhiyun       }									\
231*4882a593Smuzhiyun 									\
232*4882a593Smuzhiyun     (count) = W_TYPE_SIZE - (__clz_tab[__xr >> __a] + __a);		\
233*4882a593Smuzhiyun   } while (0)
234*4882a593Smuzhiyun #define COUNT_LEADING_ZEROS_0 W_TYPE_SIZE
235*4882a593Smuzhiyun #endif
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun #if !defined (count_trailing_zeros)
238*4882a593Smuzhiyun /* Define count_trailing_zeros using count_leading_zeros.  The latter might be
239*4882a593Smuzhiyun    defined in asm, but if it is not, the C version above is good enough.  */
240*4882a593Smuzhiyun #define count_trailing_zeros(count, x) \
241*4882a593Smuzhiyun   do {									\
242*4882a593Smuzhiyun     UWtype __ctz_x = (x);						\
243*4882a593Smuzhiyun     UWtype __ctz_c;							\
244*4882a593Smuzhiyun     count_leading_zeros (__ctz_c, __ctz_x & -__ctz_x);			\
245*4882a593Smuzhiyun     (count) = W_TYPE_SIZE - 1 - __ctz_c;				\
246*4882a593Smuzhiyun   } while (0)
247*4882a593Smuzhiyun #endif
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun #ifndef UDIV_NEEDS_NORMALIZATION
250*4882a593Smuzhiyun #define UDIV_NEEDS_NORMALIZATION 0
251*4882a593Smuzhiyun #endif
252