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