Linux-2.6.12-rc2
[linux-2.6.git] / arch / arm26 / lib / longlong.h
1 /* longlong.h -- based on code from gcc-2.95.3
2
3    definitions for mixed size 32/64 bit arithmetic.
4    Copyright (C) 1991, 92, 94, 95, 96, 1997, 1998 Free Software Foundation, Inc.
5
6    This definition file is free software; you can redistribute it
7    and/or modify it under the terms of the GNU General Public
8    License as published by the Free Software Foundation; either
9    version 2, or (at your option) any later version.
10
11    This definition file is distributed in the hope that it will be
12    useful, but WITHOUT ANY WARRANTY; without even the implied
13    warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14    See the GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 /* Borrowed from GCC 2.95.3, I Molton 29/07/01 */
22
23 #ifndef SI_TYPE_SIZE
24 #define SI_TYPE_SIZE 32
25 #endif
26
27 #define __BITS4 (SI_TYPE_SIZE / 4)
28 #define __ll_B (1L << (SI_TYPE_SIZE / 2))
29 #define __ll_lowpart(t) ((USItype) (t) % __ll_B)
30 #define __ll_highpart(t) ((USItype) (t) / __ll_B)
31
32 /* Define auxiliary asm macros.
33
34    1) umul_ppmm(high_prod, low_prod, multipler, multiplicand)
35    multiplies two USItype integers MULTIPLER and MULTIPLICAND,
36    and generates a two-part USItype product in HIGH_PROD and
37    LOW_PROD.
38
39    2) __umulsidi3(a,b) multiplies two USItype integers A and B,
40    and returns a UDItype product.  This is just a variant of umul_ppmm.
41
42    3) udiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
43    denominator) divides a two-word unsigned integer, composed by the
44    integers HIGH_NUMERATOR and LOW_NUMERATOR, by DENOMINATOR and
45    places the quotient in QUOTIENT and the remainder in REMAINDER.
46    HIGH_NUMERATOR must be less than DENOMINATOR for correct operation.
47    If, in addition, the most significant bit of DENOMINATOR must be 1,
48    then the pre-processor symbol UDIV_NEEDS_NORMALIZATION is defined to 1.
49
50    4) sdiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
51    denominator).  Like udiv_qrnnd but the numbers are signed.  The
52    quotient is rounded towards 0.
53
54    5) count_leading_zeros(count, x) counts the number of zero-bits from
55    the msb to the first non-zero bit.  This is the number of steps X
56    needs to be shifted left to set the msb.  Undefined for X == 0.
57
58    6) add_ssaaaa(high_sum, low_sum, high_addend_1, low_addend_1,
59    high_addend_2, low_addend_2) adds two two-word unsigned integers,
60    composed by HIGH_ADDEND_1 and LOW_ADDEND_1, and HIGH_ADDEND_2 and
61    LOW_ADDEND_2 respectively.  The result is placed in HIGH_SUM and
62    LOW_SUM.  Overflow (i.e. carry out) is not stored anywhere, and is
63    lost.
64
65    7) sub_ddmmss(high_difference, low_difference, high_minuend,
66    low_minuend, high_subtrahend, low_subtrahend) subtracts two
67    two-word unsigned integers, composed by HIGH_MINUEND_1 and
68    LOW_MINUEND_1, and HIGH_SUBTRAHEND_2 and LOW_SUBTRAHEND_2
69    respectively.  The result is placed in HIGH_DIFFERENCE and
70    LOW_DIFFERENCE.  Overflow (i.e. carry out) is not stored anywhere,
71    and is lost.
72
73    If any of these macros are left undefined for a particular CPU,
74    C macros are used.  */
75
76 #if defined (__arm__)
77 #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
78   __asm__ ("adds        %1, %4, %5                                      \n\
79         adc     %0, %2, %3"                                             \
80            : "=r" ((USItype) (sh)),                                     \
81              "=&r" ((USItype) (sl))                                     \
82            : "%r" ((USItype) (ah)),                                     \
83              "rI" ((USItype) (bh)),                                     \
84              "%r" ((USItype) (al)),                                     \
85              "rI" ((USItype) (bl)))
86 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
87   __asm__ ("subs        %1, %4, %5                                      \n\
88         sbc     %0, %2, %3"                                             \
89            : "=r" ((USItype) (sh)),                                     \
90              "=&r" ((USItype) (sl))                                     \
91            : "r" ((USItype) (ah)),                                      \
92              "rI" ((USItype) (bh)),                                     \
93              "r" ((USItype) (al)),                                      \
94              "rI" ((USItype) (bl)))
95 #define umul_ppmm(xh, xl, a, b) \
96 {register USItype __t0, __t1, __t2;                                     \
97   __asm__ ("%@ Inlined umul_ppmm                                        \n\
98         mov     %2, %5, lsr #16                                         \n\
99         mov     %0, %6, lsr #16                                         \n\
100         bic     %3, %5, %2, lsl #16                                     \n\
101         bic     %4, %6, %0, lsl #16                                     \n\
102         mul     %1, %3, %4                                              \n\
103         mul     %4, %2, %4                                              \n\
104         mul     %3, %0, %3                                              \n\
105         mul     %0, %2, %0                                              \n\
106         adds    %3, %4, %3                                              \n\
107         addcs   %0, %0, #65536                                          \n\
108         adds    %1, %1, %3, lsl #16                                     \n\
109         adc     %0, %0, %3, lsr #16"                                    \
110            : "=&r" ((USItype) (xh)),                                    \
111              "=r" ((USItype) (xl)),                                     \
112              "=&r" (__t0), "=&r" (__t1), "=r" (__t2)                    \
113            : "r" ((USItype) (a)),                                       \
114              "r" ((USItype) (b)));}
115 #define UMUL_TIME 20
116 #define UDIV_TIME 100
117 #endif /* __arm__ */
118
119 #define __umulsidi3(u, v) \
120   ({DIunion __w;                                                        \
121     umul_ppmm (__w.s.high, __w.s.low, u, v);                            \
122     __w.ll; })
123
124 #define __udiv_qrnnd_c(q, r, n1, n0, d) \
125   do {                                                                  \
126     USItype __d1, __d0, __q1, __q0;                                     \
127     USItype __r1, __r0, __m;                                            \
128     __d1 = __ll_highpart (d);                                           \
129     __d0 = __ll_lowpart (d);                                            \
130                                                                         \
131     __r1 = (n1) % __d1;                                                 \
132     __q1 = (n1) / __d1;                                                 \
133     __m = (USItype) __q1 * __d0;                                        \
134     __r1 = __r1 * __ll_B | __ll_highpart (n0);                          \
135     if (__r1 < __m)                                                     \
136       {                                                                 \
137         __q1--, __r1 += (d);                                            \
138         if (__r1 >= (d)) /* i.e. we didn't get carry when adding to __r1 */\
139           if (__r1 < __m)                                               \
140             __q1--, __r1 += (d);                                        \
141       }                                                                 \
142     __r1 -= __m;                                                        \
143                                                                         \
144     __r0 = __r1 % __d1;                                                 \
145     __q0 = __r1 / __d1;                                                 \
146     __m = (USItype) __q0 * __d0;                                        \
147     __r0 = __r0 * __ll_B | __ll_lowpart (n0);                           \
148     if (__r0 < __m)                                                     \
149       {                                                                 \
150         __q0--, __r0 += (d);                                            \
151         if (__r0 >= (d))                                                \
152           if (__r0 < __m)                                               \
153             __q0--, __r0 += (d);                                        \
154       }                                                                 \
155     __r0 -= __m;                                                        \
156                                                                         \
157     (q) = (USItype) __q1 * __ll_B | __q0;                               \
158     (r) = __r0;                                                         \
159   } while (0)
160
161 #define UDIV_NEEDS_NORMALIZATION 1
162 #define udiv_qrnnd __udiv_qrnnd_c
163
164 extern const UQItype __clz_tab[];
165 #define count_leading_zeros(count, x) \
166   do {                                                                  \
167     USItype __xr = (x);                                                 \
168     USItype __a;                                                        \
169                                                                         \
170     if (SI_TYPE_SIZE <= 32)                                             \
171       {                                                                 \
172         __a = __xr < ((USItype)1<<2*__BITS4)                            \
173           ? (__xr < ((USItype)1<<__BITS4) ? 0 : __BITS4)                \
174           : (__xr < ((USItype)1<<3*__BITS4) ?  2*__BITS4 : 3*__BITS4);  \
175       }                                                                 \
176     else                                                                \
177       {                                                                 \
178         for (__a = SI_TYPE_SIZE - 8; __a > 0; __a -= 8)                 \
179           if (((__xr >> __a) & 0xff) != 0)                              \
180             break;                                                      \
181       }                                                                 \
182                                                                         \
183     (count) = SI_TYPE_SIZE - (__clz_tab[__xr >> __a] + __a);            \
184   } while (0)