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