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