1*15144b0fSOlivier Houchard/* $NetBSD: softfloat-macros,v 1.1 2002/05/21 23:51:08 bjh21 Exp $ */
2*15144b0fSOlivier Houchard/* $FreeBSD$ */
3*15144b0fSOlivier Houchard
4*15144b0fSOlivier Houchard/*
5*15144b0fSOlivier Houchard===============================================================================
6*15144b0fSOlivier Houchard
7*15144b0fSOlivier HouchardThis C source fragment is part of the SoftFloat IEC/IEEE Floating-point
8*15144b0fSOlivier HouchardArithmetic Package, Release 2a.
9*15144b0fSOlivier Houchard
10*15144b0fSOlivier HouchardWritten by John R. Hauser.  This work was made possible in part by the
11*15144b0fSOlivier HouchardInternational Computer Science Institute, located at Suite 600, 1947 Center
12*15144b0fSOlivier HouchardStreet, Berkeley, California 94704.  Funding was partially provided by the
13*15144b0fSOlivier HouchardNational Science Foundation under grant MIP-9311980.  The original version
14*15144b0fSOlivier Houchardof this code was written as part of a project to build a fixed-point vector
15*15144b0fSOlivier Houchardprocessor in collaboration with the University of California at Berkeley,
16*15144b0fSOlivier Houchardoverseen by Profs. Nelson Morgan and John Wawrzynek.  More information
17*15144b0fSOlivier Houchardis available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/
18*15144b0fSOlivier Houchardarithmetic/SoftFloat.html'.
19*15144b0fSOlivier Houchard
20*15144b0fSOlivier HouchardTHIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE.  Although reasonable effort
21*15144b0fSOlivier Houchardhas been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
22*15144b0fSOlivier HouchardTIMES RESULT IN INCORRECT BEHAVIOR.  USE OF THIS SOFTWARE IS RESTRICTED TO
23*15144b0fSOlivier HouchardPERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
24*15144b0fSOlivier HouchardAND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
25*15144b0fSOlivier Houchard
26*15144b0fSOlivier HouchardDerivative works are acceptable, even for commercial purposes, so long as
27*15144b0fSOlivier Houchard(1) they include prominent notice that the work is derivative, and (2) they
28*15144b0fSOlivier Houchardinclude prominent notice akin to these four paragraphs for those parts of
29*15144b0fSOlivier Houchardthis code that are retained.
30*15144b0fSOlivier Houchard
31*15144b0fSOlivier Houchard===============================================================================
32*15144b0fSOlivier Houchard*/
33*15144b0fSOlivier Houchard
34*15144b0fSOlivier Houchard/*
35*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
36*15144b0fSOlivier HouchardShifts `a' right by the number of bits given in `count'.  If any nonzero
37*15144b0fSOlivier Houchardbits are shifted off, they are ``jammed'' into the least significant bit of
38*15144b0fSOlivier Houchardthe result by setting the least significant bit to 1.  The value of `count'
39*15144b0fSOlivier Houchardcan be arbitrarily large; in particular, if `count' is greater than 32, the
40*15144b0fSOlivier Houchardresult will be either 0 or 1, depending on whether `a' is zero or nonzero.
41*15144b0fSOlivier HouchardThe result is stored in the location pointed to by `zPtr'.
42*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
43*15144b0fSOlivier Houchard*/
44*15144b0fSOlivier HouchardINLINE void shift32RightJamming( bits32 a, int16 count, bits32 *zPtr )
45*15144b0fSOlivier Houchard{
46*15144b0fSOlivier Houchard    bits32 z;
47*15144b0fSOlivier Houchard
48*15144b0fSOlivier Houchard    if ( count == 0 ) {
49*15144b0fSOlivier Houchard        z = a;
50*15144b0fSOlivier Houchard    }
51*15144b0fSOlivier Houchard    else if ( count < 32 ) {
52*15144b0fSOlivier Houchard        z = ( a>>count ) | ( ( a<<( ( - count ) & 31 ) ) != 0 );
53*15144b0fSOlivier Houchard    }
54*15144b0fSOlivier Houchard    else {
55*15144b0fSOlivier Houchard        z = ( a != 0 );
56*15144b0fSOlivier Houchard    }
57*15144b0fSOlivier Houchard    *zPtr = z;
58*15144b0fSOlivier Houchard
59*15144b0fSOlivier Houchard}
60*15144b0fSOlivier Houchard
61*15144b0fSOlivier Houchard/*
62*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
63*15144b0fSOlivier HouchardShifts `a' right by the number of bits given in `count'.  If any nonzero
64*15144b0fSOlivier Houchardbits are shifted off, they are ``jammed'' into the least significant bit of
65*15144b0fSOlivier Houchardthe result by setting the least significant bit to 1.  The value of `count'
66*15144b0fSOlivier Houchardcan be arbitrarily large; in particular, if `count' is greater than 64, the
67*15144b0fSOlivier Houchardresult will be either 0 or 1, depending on whether `a' is zero or nonzero.
68*15144b0fSOlivier HouchardThe result is stored in the location pointed to by `zPtr'.
69*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
70*15144b0fSOlivier Houchard*/
71*15144b0fSOlivier HouchardINLINE void shift64RightJamming( bits64 a, int16 count, bits64 *zPtr )
72*15144b0fSOlivier Houchard{
73*15144b0fSOlivier Houchard    bits64 z;
74*15144b0fSOlivier Houchard
75*15144b0fSOlivier Houchard    if ( count == 0 ) {
76*15144b0fSOlivier Houchard        z = a;
77*15144b0fSOlivier Houchard    }
78*15144b0fSOlivier Houchard    else if ( count < 64 ) {
79*15144b0fSOlivier Houchard        z = ( a>>count ) | ( ( a<<( ( - count ) & 63 ) ) != 0 );
80*15144b0fSOlivier Houchard    }
81*15144b0fSOlivier Houchard    else {
82*15144b0fSOlivier Houchard        z = ( a != 0 );
83*15144b0fSOlivier Houchard    }
84*15144b0fSOlivier Houchard    *zPtr = z;
85*15144b0fSOlivier Houchard
86*15144b0fSOlivier Houchard}
87*15144b0fSOlivier Houchard
88*15144b0fSOlivier Houchard/*
89*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
90*15144b0fSOlivier HouchardShifts the 128-bit value formed by concatenating `a0' and `a1' right by 64
91*15144b0fSOlivier Houchard_plus_ the number of bits given in `count'.  The shifted result is at most
92*15144b0fSOlivier Houchard64 nonzero bits; this is stored at the location pointed to by `z0Ptr'.  The
93*15144b0fSOlivier Houchardbits shifted off form a second 64-bit result as follows:  The _last_ bit
94*15144b0fSOlivier Houchardshifted off is the most-significant bit of the extra result, and the other
95*15144b0fSOlivier Houchard63 bits of the extra result are all zero if and only if _all_but_the_last_
96*15144b0fSOlivier Houchardbits shifted off were all zero.  This extra result is stored in the location
97*15144b0fSOlivier Houchardpointed to by `z1Ptr'.  The value of `count' can be arbitrarily large.
98*15144b0fSOlivier Houchard    (This routine makes more sense if `a0' and `a1' are considered to form a
99*15144b0fSOlivier Houchardfixed-point value with binary point between `a0' and `a1'.  This fixed-point
100*15144b0fSOlivier Houchardvalue is shifted right by the number of bits given in `count', and the
101*15144b0fSOlivier Houchardinteger part of the result is returned at the location pointed to by
102*15144b0fSOlivier Houchard`z0Ptr'.  The fractional part of the result may be slightly corrupted as
103*15144b0fSOlivier Houcharddescribed above, and is returned at the location pointed to by `z1Ptr'.)
104*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
105*15144b0fSOlivier Houchard*/
106*15144b0fSOlivier HouchardINLINE void
107*15144b0fSOlivier Houchard shift64ExtraRightJamming(
108*15144b0fSOlivier Houchard     bits64 a0, bits64 a1, int16 count, bits64 *z0Ptr, bits64 *z1Ptr )
109*15144b0fSOlivier Houchard{
110*15144b0fSOlivier Houchard    bits64 z0, z1;
111*15144b0fSOlivier Houchard    int8 negCount = ( - count ) & 63;
112*15144b0fSOlivier Houchard
113*15144b0fSOlivier Houchard    if ( count == 0 ) {
114*15144b0fSOlivier Houchard        z1 = a1;
115*15144b0fSOlivier Houchard        z0 = a0;
116*15144b0fSOlivier Houchard    }
117*15144b0fSOlivier Houchard    else if ( count < 64 ) {
118*15144b0fSOlivier Houchard        z1 = ( a0<<negCount ) | ( a1 != 0 );
119*15144b0fSOlivier Houchard        z0 = a0>>count;
120*15144b0fSOlivier Houchard    }
121*15144b0fSOlivier Houchard    else {
122*15144b0fSOlivier Houchard        if ( count == 64 ) {
123*15144b0fSOlivier Houchard            z1 = a0 | ( a1 != 0 );
124*15144b0fSOlivier Houchard        }
125*15144b0fSOlivier Houchard        else {
126*15144b0fSOlivier Houchard            z1 = ( ( a0 | a1 ) != 0 );
127*15144b0fSOlivier Houchard        }
128*15144b0fSOlivier Houchard        z0 = 0;
129*15144b0fSOlivier Houchard    }
130*15144b0fSOlivier Houchard    *z1Ptr = z1;
131*15144b0fSOlivier Houchard    *z0Ptr = z0;
132*15144b0fSOlivier Houchard
133*15144b0fSOlivier Houchard}
134*15144b0fSOlivier Houchard
135*15144b0fSOlivier Houchard/*
136*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
137*15144b0fSOlivier HouchardShifts the 128-bit value formed by concatenating `a0' and `a1' right by the
138*15144b0fSOlivier Houchardnumber of bits given in `count'.  Any bits shifted off are lost.  The value
139*15144b0fSOlivier Houchardof `count' can be arbitrarily large; in particular, if `count' is greater
140*15144b0fSOlivier Houchardthan 128, the result will be 0.  The result is broken into two 64-bit pieces
141*15144b0fSOlivier Houchardwhich are stored at the locations pointed to by `z0Ptr' and `z1Ptr'.
142*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
143*15144b0fSOlivier Houchard*/
144*15144b0fSOlivier HouchardINLINE void
145*15144b0fSOlivier Houchard shift128Right(
146*15144b0fSOlivier Houchard     bits64 a0, bits64 a1, int16 count, bits64 *z0Ptr, bits64 *z1Ptr )
147*15144b0fSOlivier Houchard{
148*15144b0fSOlivier Houchard    bits64 z0, z1;
149*15144b0fSOlivier Houchard    int8 negCount = ( - count ) & 63;
150*15144b0fSOlivier Houchard
151*15144b0fSOlivier Houchard    if ( count == 0 ) {
152*15144b0fSOlivier Houchard        z1 = a1;
153*15144b0fSOlivier Houchard        z0 = a0;
154*15144b0fSOlivier Houchard    }
155*15144b0fSOlivier Houchard    else if ( count < 64 ) {
156*15144b0fSOlivier Houchard        z1 = ( a0<<negCount ) | ( a1>>count );
157*15144b0fSOlivier Houchard        z0 = a0>>count;
158*15144b0fSOlivier Houchard    }
159*15144b0fSOlivier Houchard    else {
160*15144b0fSOlivier Houchard        z1 = ( count < 64 ) ? ( a0>>( count & 63 ) ) : 0;
161*15144b0fSOlivier Houchard        z0 = 0;
162*15144b0fSOlivier Houchard    }
163*15144b0fSOlivier Houchard    *z1Ptr = z1;
164*15144b0fSOlivier Houchard    *z0Ptr = z0;
165*15144b0fSOlivier Houchard
166*15144b0fSOlivier Houchard}
167*15144b0fSOlivier Houchard
168*15144b0fSOlivier Houchard/*
169*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
170*15144b0fSOlivier HouchardShifts the 128-bit value formed by concatenating `a0' and `a1' right by the
171*15144b0fSOlivier Houchardnumber of bits given in `count'.  If any nonzero bits are shifted off, they
172*15144b0fSOlivier Houchardare ``jammed'' into the least significant bit of the result by setting the
173*15144b0fSOlivier Houchardleast significant bit to 1.  The value of `count' can be arbitrarily large;
174*15144b0fSOlivier Houchardin particular, if `count' is greater than 128, the result will be either
175*15144b0fSOlivier Houchard0 or 1, depending on whether the concatenation of `a0' and `a1' is zero or
176*15144b0fSOlivier Houchardnonzero.  The result is broken into two 64-bit pieces which are stored at
177*15144b0fSOlivier Houchardthe locations pointed to by `z0Ptr' and `z1Ptr'.
178*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
179*15144b0fSOlivier Houchard*/
180*15144b0fSOlivier HouchardINLINE void
181*15144b0fSOlivier Houchard shift128RightJamming(
182*15144b0fSOlivier Houchard     bits64 a0, bits64 a1, int16 count, bits64 *z0Ptr, bits64 *z1Ptr )
183*15144b0fSOlivier Houchard{
184*15144b0fSOlivier Houchard    bits64 z0, z1;
185*15144b0fSOlivier Houchard    int8 negCount = ( - count ) & 63;
186*15144b0fSOlivier Houchard
187*15144b0fSOlivier Houchard    if ( count == 0 ) {
188*15144b0fSOlivier Houchard        z1 = a1;
189*15144b0fSOlivier Houchard        z0 = a0;
190*15144b0fSOlivier Houchard    }
191*15144b0fSOlivier Houchard    else if ( count < 64 ) {
192*15144b0fSOlivier Houchard        z1 = ( a0<<negCount ) | ( a1>>count ) | ( ( a1<<negCount ) != 0 );
193*15144b0fSOlivier Houchard        z0 = a0>>count;
194*15144b0fSOlivier Houchard    }
195*15144b0fSOlivier Houchard    else {
196*15144b0fSOlivier Houchard        if ( count == 64 ) {
197*15144b0fSOlivier Houchard            z1 = a0 | ( a1 != 0 );
198*15144b0fSOlivier Houchard        }
199*15144b0fSOlivier Houchard        else if ( count < 128 ) {
200*15144b0fSOlivier Houchard            z1 = ( a0>>( count & 63 ) ) | ( ( ( a0<<negCount ) | a1 ) != 0 );
201*15144b0fSOlivier Houchard        }
202*15144b0fSOlivier Houchard        else {
203*15144b0fSOlivier Houchard            z1 = ( ( a0 | a1 ) != 0 );
204*15144b0fSOlivier Houchard        }
205*15144b0fSOlivier Houchard        z0 = 0;
206*15144b0fSOlivier Houchard    }
207*15144b0fSOlivier Houchard    *z1Ptr = z1;
208*15144b0fSOlivier Houchard    *z0Ptr = z0;
209*15144b0fSOlivier Houchard
210*15144b0fSOlivier Houchard}
211*15144b0fSOlivier Houchard
212*15144b0fSOlivier Houchard/*
213*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
214*15144b0fSOlivier HouchardShifts the 192-bit value formed by concatenating `a0', `a1', and `a2' right
215*15144b0fSOlivier Houchardby 64 _plus_ the number of bits given in `count'.  The shifted result is
216*15144b0fSOlivier Houchardat most 128 nonzero bits; these are broken into two 64-bit pieces which are
217*15144b0fSOlivier Houchardstored at the locations pointed to by `z0Ptr' and `z1Ptr'.  The bits shifted
218*15144b0fSOlivier Houchardoff form a third 64-bit result as follows:  The _last_ bit shifted off is
219*15144b0fSOlivier Houchardthe most-significant bit of the extra result, and the other 63 bits of the
220*15144b0fSOlivier Houchardextra result are all zero if and only if _all_but_the_last_ bits shifted off
221*15144b0fSOlivier Houchardwere all zero.  This extra result is stored in the location pointed to by
222*15144b0fSOlivier Houchard`z2Ptr'.  The value of `count' can be arbitrarily large.
223*15144b0fSOlivier Houchard    (This routine makes more sense if `a0', `a1', and `a2' are considered
224*15144b0fSOlivier Houchardto form a fixed-point value with binary point between `a1' and `a2'.  This
225*15144b0fSOlivier Houchardfixed-point value is shifted right by the number of bits given in `count',
226*15144b0fSOlivier Houchardand the integer part of the result is returned at the locations pointed to
227*15144b0fSOlivier Houchardby `z0Ptr' and `z1Ptr'.  The fractional part of the result may be slightly
228*15144b0fSOlivier Houchardcorrupted as described above, and is returned at the location pointed to by
229*15144b0fSOlivier Houchard`z2Ptr'.)
230*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
231*15144b0fSOlivier Houchard*/
232*15144b0fSOlivier HouchardINLINE void
233*15144b0fSOlivier Houchard shift128ExtraRightJamming(
234*15144b0fSOlivier Houchard     bits64 a0,
235*15144b0fSOlivier Houchard     bits64 a1,
236*15144b0fSOlivier Houchard     bits64 a2,
237*15144b0fSOlivier Houchard     int16 count,
238*15144b0fSOlivier Houchard     bits64 *z0Ptr,
239*15144b0fSOlivier Houchard     bits64 *z1Ptr,
240*15144b0fSOlivier Houchard     bits64 *z2Ptr
241*15144b0fSOlivier Houchard )
242*15144b0fSOlivier Houchard{
243*15144b0fSOlivier Houchard    bits64 z0, z1, z2;
244*15144b0fSOlivier Houchard    int8 negCount = ( - count ) & 63;
245*15144b0fSOlivier Houchard
246*15144b0fSOlivier Houchard    if ( count == 0 ) {
247*15144b0fSOlivier Houchard        z2 = a2;
248*15144b0fSOlivier Houchard        z1 = a1;
249*15144b0fSOlivier Houchard        z0 = a0;
250*15144b0fSOlivier Houchard    }
251*15144b0fSOlivier Houchard    else {
252*15144b0fSOlivier Houchard        if ( count < 64 ) {
253*15144b0fSOlivier Houchard            z2 = a1<<negCount;
254*15144b0fSOlivier Houchard            z1 = ( a0<<negCount ) | ( a1>>count );
255*15144b0fSOlivier Houchard            z0 = a0>>count;
256*15144b0fSOlivier Houchard        }
257*15144b0fSOlivier Houchard        else {
258*15144b0fSOlivier Houchard            if ( count == 64 ) {
259*15144b0fSOlivier Houchard                z2 = a1;
260*15144b0fSOlivier Houchard                z1 = a0;
261*15144b0fSOlivier Houchard            }
262*15144b0fSOlivier Houchard            else {
263*15144b0fSOlivier Houchard                a2 |= a1;
264*15144b0fSOlivier Houchard                if ( count < 128 ) {
265*15144b0fSOlivier Houchard                    z2 = a0<<negCount;
266*15144b0fSOlivier Houchard                    z1 = a0>>( count & 63 );
267*15144b0fSOlivier Houchard                }
268*15144b0fSOlivier Houchard                else {
269*15144b0fSOlivier Houchard                    z2 = ( count == 128 ) ? a0 : ( a0 != 0 );
270*15144b0fSOlivier Houchard                    z1 = 0;
271*15144b0fSOlivier Houchard                }
272*15144b0fSOlivier Houchard            }
273*15144b0fSOlivier Houchard            z0 = 0;
274*15144b0fSOlivier Houchard        }
275*15144b0fSOlivier Houchard        z2 |= ( a2 != 0 );
276*15144b0fSOlivier Houchard    }
277*15144b0fSOlivier Houchard    *z2Ptr = z2;
278*15144b0fSOlivier Houchard    *z1Ptr = z1;
279*15144b0fSOlivier Houchard    *z0Ptr = z0;
280*15144b0fSOlivier Houchard
281*15144b0fSOlivier Houchard}
282*15144b0fSOlivier Houchard
283*15144b0fSOlivier Houchard/*
284*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
285*15144b0fSOlivier HouchardShifts the 128-bit value formed by concatenating `a0' and `a1' left by the
286*15144b0fSOlivier Houchardnumber of bits given in `count'.  Any bits shifted off are lost.  The value
287*15144b0fSOlivier Houchardof `count' must be less than 64.  The result is broken into two 64-bit
288*15144b0fSOlivier Houchardpieces which are stored at the locations pointed to by `z0Ptr' and `z1Ptr'.
289*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
290*15144b0fSOlivier Houchard*/
291*15144b0fSOlivier HouchardINLINE void
292*15144b0fSOlivier Houchard shortShift128Left(
293*15144b0fSOlivier Houchard     bits64 a0, bits64 a1, int16 count, bits64 *z0Ptr, bits64 *z1Ptr )
294*15144b0fSOlivier Houchard{
295*15144b0fSOlivier Houchard
296*15144b0fSOlivier Houchard    *z1Ptr = a1<<count;
297*15144b0fSOlivier Houchard    *z0Ptr =
298*15144b0fSOlivier Houchard        ( count == 0 ) ? a0 : ( a0<<count ) | ( a1>>( ( - count ) & 63 ) );
299*15144b0fSOlivier Houchard
300*15144b0fSOlivier Houchard}
301*15144b0fSOlivier Houchard
302*15144b0fSOlivier Houchard/*
303*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
304*15144b0fSOlivier HouchardShifts the 192-bit value formed by concatenating `a0', `a1', and `a2' left
305*15144b0fSOlivier Houchardby the number of bits given in `count'.  Any bits shifted off are lost.
306*15144b0fSOlivier HouchardThe value of `count' must be less than 64.  The result is broken into three
307*15144b0fSOlivier Houchard64-bit pieces which are stored at the locations pointed to by `z0Ptr',
308*15144b0fSOlivier Houchard`z1Ptr', and `z2Ptr'.
309*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
310*15144b0fSOlivier Houchard*/
311*15144b0fSOlivier HouchardINLINE void
312*15144b0fSOlivier Houchard shortShift192Left(
313*15144b0fSOlivier Houchard     bits64 a0,
314*15144b0fSOlivier Houchard     bits64 a1,
315*15144b0fSOlivier Houchard     bits64 a2,
316*15144b0fSOlivier Houchard     int16 count,
317*15144b0fSOlivier Houchard     bits64 *z0Ptr,
318*15144b0fSOlivier Houchard     bits64 *z1Ptr,
319*15144b0fSOlivier Houchard     bits64 *z2Ptr
320*15144b0fSOlivier Houchard )
321*15144b0fSOlivier Houchard{
322*15144b0fSOlivier Houchard    bits64 z0, z1, z2;
323*15144b0fSOlivier Houchard    int8 negCount;
324*15144b0fSOlivier Houchard
325*15144b0fSOlivier Houchard    z2 = a2<<count;
326*15144b0fSOlivier Houchard    z1 = a1<<count;
327*15144b0fSOlivier Houchard    z0 = a0<<count;
328*15144b0fSOlivier Houchard    if ( 0 < count ) {
329*15144b0fSOlivier Houchard        negCount = ( ( - count ) & 63 );
330*15144b0fSOlivier Houchard        z1 |= a2>>negCount;
331*15144b0fSOlivier Houchard        z0 |= a1>>negCount;
332*15144b0fSOlivier Houchard    }
333*15144b0fSOlivier Houchard    *z2Ptr = z2;
334*15144b0fSOlivier Houchard    *z1Ptr = z1;
335*15144b0fSOlivier Houchard    *z0Ptr = z0;
336*15144b0fSOlivier Houchard
337*15144b0fSOlivier Houchard}
338*15144b0fSOlivier Houchard
339*15144b0fSOlivier Houchard/*
340*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
341*15144b0fSOlivier HouchardAdds the 128-bit value formed by concatenating `a0' and `a1' to the 128-bit
342*15144b0fSOlivier Houchardvalue formed by concatenating `b0' and `b1'.  Addition is modulo 2^128, so
343*15144b0fSOlivier Houchardany carry out is lost.  The result is broken into two 64-bit pieces which
344*15144b0fSOlivier Houchardare stored at the locations pointed to by `z0Ptr' and `z1Ptr'.
345*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
346*15144b0fSOlivier Houchard*/
347*15144b0fSOlivier HouchardINLINE void
348*15144b0fSOlivier Houchard add128(
349*15144b0fSOlivier Houchard     bits64 a0, bits64 a1, bits64 b0, bits64 b1, bits64 *z0Ptr, bits64 *z1Ptr )
350*15144b0fSOlivier Houchard{
351*15144b0fSOlivier Houchard    bits64 z1;
352*15144b0fSOlivier Houchard
353*15144b0fSOlivier Houchard    z1 = a1 + b1;
354*15144b0fSOlivier Houchard    *z1Ptr = z1;
355*15144b0fSOlivier Houchard    *z0Ptr = a0 + b0 + ( z1 < a1 );
356*15144b0fSOlivier Houchard
357*15144b0fSOlivier Houchard}
358*15144b0fSOlivier Houchard
359*15144b0fSOlivier Houchard/*
360*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
361*15144b0fSOlivier HouchardAdds the 192-bit value formed by concatenating `a0', `a1', and `a2' to the
362*15144b0fSOlivier Houchard192-bit value formed by concatenating `b0', `b1', and `b2'.  Addition is
363*15144b0fSOlivier Houchardmodulo 2^192, so any carry out is lost.  The result is broken into three
364*15144b0fSOlivier Houchard64-bit pieces which are stored at the locations pointed to by `z0Ptr',
365*15144b0fSOlivier Houchard`z1Ptr', and `z2Ptr'.
366*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
367*15144b0fSOlivier Houchard*/
368*15144b0fSOlivier HouchardINLINE void
369*15144b0fSOlivier Houchard add192(
370*15144b0fSOlivier Houchard     bits64 a0,
371*15144b0fSOlivier Houchard     bits64 a1,
372*15144b0fSOlivier Houchard     bits64 a2,
373*15144b0fSOlivier Houchard     bits64 b0,
374*15144b0fSOlivier Houchard     bits64 b1,
375*15144b0fSOlivier Houchard     bits64 b2,
376*15144b0fSOlivier Houchard     bits64 *z0Ptr,
377*15144b0fSOlivier Houchard     bits64 *z1Ptr,
378*15144b0fSOlivier Houchard     bits64 *z2Ptr
379*15144b0fSOlivier Houchard )
380*15144b0fSOlivier Houchard{
381*15144b0fSOlivier Houchard    bits64 z0, z1, z2;
382*15144b0fSOlivier Houchard    int8 carry0, carry1;
383*15144b0fSOlivier Houchard
384*15144b0fSOlivier Houchard    z2 = a2 + b2;
385*15144b0fSOlivier Houchard    carry1 = ( z2 < a2 );
386*15144b0fSOlivier Houchard    z1 = a1 + b1;
387*15144b0fSOlivier Houchard    carry0 = ( z1 < a1 );
388*15144b0fSOlivier Houchard    z0 = a0 + b0;
389*15144b0fSOlivier Houchard    z1 += carry1;
390*15144b0fSOlivier Houchard    z0 += ( z1 < carry1 );
391*15144b0fSOlivier Houchard    z0 += carry0;
392*15144b0fSOlivier Houchard    *z2Ptr = z2;
393*15144b0fSOlivier Houchard    *z1Ptr = z1;
394*15144b0fSOlivier Houchard    *z0Ptr = z0;
395*15144b0fSOlivier Houchard
396*15144b0fSOlivier Houchard}
397*15144b0fSOlivier Houchard
398*15144b0fSOlivier Houchard/*
399*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
400*15144b0fSOlivier HouchardSubtracts the 128-bit value formed by concatenating `b0' and `b1' from the
401*15144b0fSOlivier Houchard128-bit value formed by concatenating `a0' and `a1'.  Subtraction is modulo
402*15144b0fSOlivier Houchard2^128, so any borrow out (carry out) is lost.  The result is broken into two
403*15144b0fSOlivier Houchard64-bit pieces which are stored at the locations pointed to by `z0Ptr' and
404*15144b0fSOlivier Houchard`z1Ptr'.
405*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
406*15144b0fSOlivier Houchard*/
407*15144b0fSOlivier HouchardINLINE void
408*15144b0fSOlivier Houchard sub128(
409*15144b0fSOlivier Houchard     bits64 a0, bits64 a1, bits64 b0, bits64 b1, bits64 *z0Ptr, bits64 *z1Ptr )
410*15144b0fSOlivier Houchard{
411*15144b0fSOlivier Houchard
412*15144b0fSOlivier Houchard    *z1Ptr = a1 - b1;
413*15144b0fSOlivier Houchard    *z0Ptr = a0 - b0 - ( a1 < b1 );
414*15144b0fSOlivier Houchard
415*15144b0fSOlivier Houchard}
416*15144b0fSOlivier Houchard
417*15144b0fSOlivier Houchard/*
418*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
419*15144b0fSOlivier HouchardSubtracts the 192-bit value formed by concatenating `b0', `b1', and `b2'
420*15144b0fSOlivier Houchardfrom the 192-bit value formed by concatenating `a0', `a1', and `a2'.
421*15144b0fSOlivier HouchardSubtraction is modulo 2^192, so any borrow out (carry out) is lost.  The
422*15144b0fSOlivier Houchardresult is broken into three 64-bit pieces which are stored at the locations
423*15144b0fSOlivier Houchardpointed to by `z0Ptr', `z1Ptr', and `z2Ptr'.
424*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
425*15144b0fSOlivier Houchard*/
426*15144b0fSOlivier HouchardINLINE void
427*15144b0fSOlivier Houchard sub192(
428*15144b0fSOlivier Houchard     bits64 a0,
429*15144b0fSOlivier Houchard     bits64 a1,
430*15144b0fSOlivier Houchard     bits64 a2,
431*15144b0fSOlivier Houchard     bits64 b0,
432*15144b0fSOlivier Houchard     bits64 b1,
433*15144b0fSOlivier Houchard     bits64 b2,
434*15144b0fSOlivier Houchard     bits64 *z0Ptr,
435*15144b0fSOlivier Houchard     bits64 *z1Ptr,
436*15144b0fSOlivier Houchard     bits64 *z2Ptr
437*15144b0fSOlivier Houchard )
438*15144b0fSOlivier Houchard{
439*15144b0fSOlivier Houchard    bits64 z0, z1, z2;
440*15144b0fSOlivier Houchard    int8 borrow0, borrow1;
441*15144b0fSOlivier Houchard
442*15144b0fSOlivier Houchard    z2 = a2 - b2;
443*15144b0fSOlivier Houchard    borrow1 = ( a2 < b2 );
444*15144b0fSOlivier Houchard    z1 = a1 - b1;
445*15144b0fSOlivier Houchard    borrow0 = ( a1 < b1 );
446*15144b0fSOlivier Houchard    z0 = a0 - b0;
447*15144b0fSOlivier Houchard    z0 -= ( z1 < borrow1 );
448*15144b0fSOlivier Houchard    z1 -= borrow1;
449*15144b0fSOlivier Houchard    z0 -= borrow0;
450*15144b0fSOlivier Houchard    *z2Ptr = z2;
451*15144b0fSOlivier Houchard    *z1Ptr = z1;
452*15144b0fSOlivier Houchard    *z0Ptr = z0;
453*15144b0fSOlivier Houchard
454*15144b0fSOlivier Houchard}
455*15144b0fSOlivier Houchard
456*15144b0fSOlivier Houchard/*
457*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
458*15144b0fSOlivier HouchardMultiplies `a' by `b' to obtain a 128-bit product.  The product is broken
459*15144b0fSOlivier Houchardinto two 64-bit pieces which are stored at the locations pointed to by
460*15144b0fSOlivier Houchard`z0Ptr' and `z1Ptr'.
461*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
462*15144b0fSOlivier Houchard*/
463*15144b0fSOlivier HouchardINLINE void mul64To128( bits64 a, bits64 b, bits64 *z0Ptr, bits64 *z1Ptr )
464*15144b0fSOlivier Houchard{
465*15144b0fSOlivier Houchard    bits32 aHigh, aLow, bHigh, bLow;
466*15144b0fSOlivier Houchard    bits64 z0, zMiddleA, zMiddleB, z1;
467*15144b0fSOlivier Houchard
468*15144b0fSOlivier Houchard    aLow = a;
469*15144b0fSOlivier Houchard    aHigh = a>>32;
470*15144b0fSOlivier Houchard    bLow = b;
471*15144b0fSOlivier Houchard    bHigh = b>>32;
472*15144b0fSOlivier Houchard    z1 = ( (bits64) aLow ) * bLow;
473*15144b0fSOlivier Houchard    zMiddleA = ( (bits64) aLow ) * bHigh;
474*15144b0fSOlivier Houchard    zMiddleB = ( (bits64) aHigh ) * bLow;
475*15144b0fSOlivier Houchard    z0 = ( (bits64) aHigh ) * bHigh;
476*15144b0fSOlivier Houchard    zMiddleA += zMiddleB;
477*15144b0fSOlivier Houchard    z0 += ( ( (bits64) ( zMiddleA < zMiddleB ) )<<32 ) + ( zMiddleA>>32 );
478*15144b0fSOlivier Houchard    zMiddleA <<= 32;
479*15144b0fSOlivier Houchard    z1 += zMiddleA;
480*15144b0fSOlivier Houchard    z0 += ( z1 < zMiddleA );
481*15144b0fSOlivier Houchard    *z1Ptr = z1;
482*15144b0fSOlivier Houchard    *z0Ptr = z0;
483*15144b0fSOlivier Houchard
484*15144b0fSOlivier Houchard}
485*15144b0fSOlivier Houchard
486*15144b0fSOlivier Houchard/*
487*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
488*15144b0fSOlivier HouchardMultiplies the 128-bit value formed by concatenating `a0' and `a1' by
489*15144b0fSOlivier Houchard`b' to obtain a 192-bit product.  The product is broken into three 64-bit
490*15144b0fSOlivier Houchardpieces which are stored at the locations pointed to by `z0Ptr', `z1Ptr', and
491*15144b0fSOlivier Houchard`z2Ptr'.
492*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
493*15144b0fSOlivier Houchard*/
494*15144b0fSOlivier HouchardINLINE void
495*15144b0fSOlivier Houchard mul128By64To192(
496*15144b0fSOlivier Houchard     bits64 a0,
497*15144b0fSOlivier Houchard     bits64 a1,
498*15144b0fSOlivier Houchard     bits64 b,
499*15144b0fSOlivier Houchard     bits64 *z0Ptr,
500*15144b0fSOlivier Houchard     bits64 *z1Ptr,
501*15144b0fSOlivier Houchard     bits64 *z2Ptr
502*15144b0fSOlivier Houchard )
503*15144b0fSOlivier Houchard{
504*15144b0fSOlivier Houchard    bits64 z0, z1, z2, more1;
505*15144b0fSOlivier Houchard
506*15144b0fSOlivier Houchard    mul64To128( a1, b, &z1, &z2 );
507*15144b0fSOlivier Houchard    mul64To128( a0, b, &z0, &more1 );
508*15144b0fSOlivier Houchard    add128( z0, more1, 0, z1, &z0, &z1 );
509*15144b0fSOlivier Houchard    *z2Ptr = z2;
510*15144b0fSOlivier Houchard    *z1Ptr = z1;
511*15144b0fSOlivier Houchard    *z0Ptr = z0;
512*15144b0fSOlivier Houchard
513*15144b0fSOlivier Houchard}
514*15144b0fSOlivier Houchard
515*15144b0fSOlivier Houchard/*
516*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
517*15144b0fSOlivier HouchardMultiplies the 128-bit value formed by concatenating `a0' and `a1' to the
518*15144b0fSOlivier Houchard128-bit value formed by concatenating `b0' and `b1' to obtain a 256-bit
519*15144b0fSOlivier Houchardproduct.  The product is broken into four 64-bit pieces which are stored at
520*15144b0fSOlivier Houchardthe locations pointed to by `z0Ptr', `z1Ptr', `z2Ptr', and `z3Ptr'.
521*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
522*15144b0fSOlivier Houchard*/
523*15144b0fSOlivier HouchardINLINE void
524*15144b0fSOlivier Houchard mul128To256(
525*15144b0fSOlivier Houchard     bits64 a0,
526*15144b0fSOlivier Houchard     bits64 a1,
527*15144b0fSOlivier Houchard     bits64 b0,
528*15144b0fSOlivier Houchard     bits64 b1,
529*15144b0fSOlivier Houchard     bits64 *z0Ptr,
530*15144b0fSOlivier Houchard     bits64 *z1Ptr,
531*15144b0fSOlivier Houchard     bits64 *z2Ptr,
532*15144b0fSOlivier Houchard     bits64 *z3Ptr
533*15144b0fSOlivier Houchard )
534*15144b0fSOlivier Houchard{
535*15144b0fSOlivier Houchard    bits64 z0, z1, z2, z3;
536*15144b0fSOlivier Houchard    bits64 more1, more2;
537*15144b0fSOlivier Houchard
538*15144b0fSOlivier Houchard    mul64To128( a1, b1, &z2, &z3 );
539*15144b0fSOlivier Houchard    mul64To128( a1, b0, &z1, &more2 );
540*15144b0fSOlivier Houchard    add128( z1, more2, 0, z2, &z1, &z2 );
541*15144b0fSOlivier Houchard    mul64To128( a0, b0, &z0, &more1 );
542*15144b0fSOlivier Houchard    add128( z0, more1, 0, z1, &z0, &z1 );
543*15144b0fSOlivier Houchard    mul64To128( a0, b1, &more1, &more2 );
544*15144b0fSOlivier Houchard    add128( more1, more2, 0, z2, &more1, &z2 );
545*15144b0fSOlivier Houchard    add128( z0, z1, 0, more1, &z0, &z1 );
546*15144b0fSOlivier Houchard    *z3Ptr = z3;
547*15144b0fSOlivier Houchard    *z2Ptr = z2;
548*15144b0fSOlivier Houchard    *z1Ptr = z1;
549*15144b0fSOlivier Houchard    *z0Ptr = z0;
550*15144b0fSOlivier Houchard
551*15144b0fSOlivier Houchard}
552*15144b0fSOlivier Houchard
553*15144b0fSOlivier Houchard/*
554*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
555*15144b0fSOlivier HouchardReturns an approximation to the 64-bit integer quotient obtained by dividing
556*15144b0fSOlivier Houchard`b' into the 128-bit value formed by concatenating `a0' and `a1'.  The
557*15144b0fSOlivier Houcharddivisor `b' must be at least 2^63.  If q is the exact quotient truncated
558*15144b0fSOlivier Houchardtoward zero, the approximation returned lies between q and q + 2 inclusive.
559*15144b0fSOlivier HouchardIf the exact quotient q is larger than 64 bits, the maximum positive 64-bit
560*15144b0fSOlivier Houchardunsigned integer is returned.
561*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
562*15144b0fSOlivier Houchard*/
563*15144b0fSOlivier Houchardstatic bits64 estimateDiv128To64( bits64 a0, bits64 a1, bits64 b )
564*15144b0fSOlivier Houchard{
565*15144b0fSOlivier Houchard    bits64 b0, b1;
566*15144b0fSOlivier Houchard    bits64 rem0, rem1, term0, term1;
567*15144b0fSOlivier Houchard    bits64 z;
568*15144b0fSOlivier Houchard
569*15144b0fSOlivier Houchard    if ( b <= a0 ) return LIT64( 0xFFFFFFFFFFFFFFFF );
570*15144b0fSOlivier Houchard    b0 = b>>32;
571*15144b0fSOlivier Houchard    z = ( b0<<32 <= a0 ) ? LIT64( 0xFFFFFFFF00000000 ) : ( a0 / b0 )<<32;
572*15144b0fSOlivier Houchard    mul64To128( b, z, &term0, &term1 );
573*15144b0fSOlivier Houchard    sub128( a0, a1, term0, term1, &rem0, &rem1 );
574*15144b0fSOlivier Houchard    while ( ( (sbits64) rem0 ) < 0 ) {
575*15144b0fSOlivier Houchard        z -= LIT64( 0x100000000 );
576*15144b0fSOlivier Houchard        b1 = b<<32;
577*15144b0fSOlivier Houchard        add128( rem0, rem1, b0, b1, &rem0, &rem1 );
578*15144b0fSOlivier Houchard    }
579*15144b0fSOlivier Houchard    rem0 = ( rem0<<32 ) | ( rem1>>32 );
580*15144b0fSOlivier Houchard    z |= ( b0<<32 <= rem0 ) ? 0xFFFFFFFF : rem0 / b0;
581*15144b0fSOlivier Houchard    return z;
582*15144b0fSOlivier Houchard
583*15144b0fSOlivier Houchard}
584*15144b0fSOlivier Houchard
585*15144b0fSOlivier Houchard#if !defined(SOFTFLOAT_FOR_GCC) || defined(FLOATX80) || defined(FLOAT128)
586*15144b0fSOlivier Houchard/*
587*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
588*15144b0fSOlivier HouchardReturns an approximation to the square root of the 32-bit significand given
589*15144b0fSOlivier Houchardby `a'.  Considered as an integer, `a' must be at least 2^31.  If bit 0 of
590*15144b0fSOlivier Houchard`aExp' (the least significant bit) is 1, the integer returned approximates
591*15144b0fSOlivier Houchard2^31*sqrt(`a'/2^31), where `a' is considered an integer.  If bit 0 of `aExp'
592*15144b0fSOlivier Houchardis 0, the integer returned approximates 2^31*sqrt(`a'/2^30).  In either
593*15144b0fSOlivier Houchardcase, the approximation returned lies strictly within +/-2 of the exact
594*15144b0fSOlivier Houchardvalue.
595*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
596*15144b0fSOlivier Houchard*/
597*15144b0fSOlivier Houchardstatic bits32 estimateSqrt32( int16 aExp, bits32 a )
598*15144b0fSOlivier Houchard{
599*15144b0fSOlivier Houchard    static const bits16 sqrtOddAdjustments[] = {
600*15144b0fSOlivier Houchard        0x0004, 0x0022, 0x005D, 0x00B1, 0x011D, 0x019F, 0x0236, 0x02E0,
601*15144b0fSOlivier Houchard        0x039C, 0x0468, 0x0545, 0x0631, 0x072B, 0x0832, 0x0946, 0x0A67
602*15144b0fSOlivier Houchard    };
603*15144b0fSOlivier Houchard    static const bits16 sqrtEvenAdjustments[] = {
604*15144b0fSOlivier Houchard        0x0A2D, 0x08AF, 0x075A, 0x0629, 0x051A, 0x0429, 0x0356, 0x029E,
605*15144b0fSOlivier Houchard        0x0200, 0x0179, 0x0109, 0x00AF, 0x0068, 0x0034, 0x0012, 0x0002
606*15144b0fSOlivier Houchard    };
607*15144b0fSOlivier Houchard    int8 idx;
608*15144b0fSOlivier Houchard    bits32 z;
609*15144b0fSOlivier Houchard
610*15144b0fSOlivier Houchard    idx = ( a>>27 ) & 15;
611*15144b0fSOlivier Houchard    if ( aExp & 1 ) {
612*15144b0fSOlivier Houchard        z = 0x4000 + ( a>>17 ) - sqrtOddAdjustments[ idx ];
613*15144b0fSOlivier Houchard        z = ( ( a / z )<<14 ) + ( z<<15 );
614*15144b0fSOlivier Houchard        a >>= 1;
615*15144b0fSOlivier Houchard    }
616*15144b0fSOlivier Houchard    else {
617*15144b0fSOlivier Houchard        z = 0x8000 + ( a>>17 ) - sqrtEvenAdjustments[ idx ];
618*15144b0fSOlivier Houchard        z = a / z + z;
619*15144b0fSOlivier Houchard        z = ( 0x20000 <= z ) ? 0xFFFF8000 : ( z<<15 );
620*15144b0fSOlivier Houchard        if ( z <= a ) return (bits32) ( ( (sbits32) a )>>1 );
621*15144b0fSOlivier Houchard    }
622*15144b0fSOlivier Houchard    return ( (bits32) ( ( ( (bits64) a )<<31 ) / z ) ) + ( z>>1 );
623*15144b0fSOlivier Houchard
624*15144b0fSOlivier Houchard}
625*15144b0fSOlivier Houchard#endif
626*15144b0fSOlivier Houchard
627*15144b0fSOlivier Houchard/*
628*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
629*15144b0fSOlivier HouchardReturns the number of leading 0 bits before the most-significant 1 bit of
630*15144b0fSOlivier Houchard`a'.  If `a' is zero, 32 is returned.
631*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
632*15144b0fSOlivier Houchard*/
633*15144b0fSOlivier Houchardstatic int8 countLeadingZeros32( bits32 a )
634*15144b0fSOlivier Houchard{
635*15144b0fSOlivier Houchard    static const int8 countLeadingZerosHigh[] = {
636*15144b0fSOlivier Houchard        8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
637*15144b0fSOlivier Houchard        3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
638*15144b0fSOlivier Houchard        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
639*15144b0fSOlivier Houchard        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
640*15144b0fSOlivier Houchard        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
641*15144b0fSOlivier Houchard        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
642*15144b0fSOlivier Houchard        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
643*15144b0fSOlivier Houchard        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
644*15144b0fSOlivier Houchard        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
645*15144b0fSOlivier Houchard        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
646*15144b0fSOlivier Houchard        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
647*15144b0fSOlivier Houchard        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
648*15144b0fSOlivier Houchard        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
649*15144b0fSOlivier Houchard        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
650*15144b0fSOlivier Houchard        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
651*15144b0fSOlivier Houchard        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
652*15144b0fSOlivier Houchard    };
653*15144b0fSOlivier Houchard    int8 shiftCount;
654*15144b0fSOlivier Houchard
655*15144b0fSOlivier Houchard    shiftCount = 0;
656*15144b0fSOlivier Houchard    if ( a < 0x10000 ) {
657*15144b0fSOlivier Houchard        shiftCount += 16;
658*15144b0fSOlivier Houchard        a <<= 16;
659*15144b0fSOlivier Houchard    }
660*15144b0fSOlivier Houchard    if ( a < 0x1000000 ) {
661*15144b0fSOlivier Houchard        shiftCount += 8;
662*15144b0fSOlivier Houchard        a <<= 8;
663*15144b0fSOlivier Houchard    }
664*15144b0fSOlivier Houchard    shiftCount += countLeadingZerosHigh[ a>>24 ];
665*15144b0fSOlivier Houchard    return shiftCount;
666*15144b0fSOlivier Houchard
667*15144b0fSOlivier Houchard}
668*15144b0fSOlivier Houchard
669*15144b0fSOlivier Houchard/*
670*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
671*15144b0fSOlivier HouchardReturns the number of leading 0 bits before the most-significant 1 bit of
672*15144b0fSOlivier Houchard`a'.  If `a' is zero, 64 is returned.
673*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
674*15144b0fSOlivier Houchard*/
675*15144b0fSOlivier Houchardstatic int8 countLeadingZeros64( bits64 a )
676*15144b0fSOlivier Houchard{
677*15144b0fSOlivier Houchard    int8 shiftCount;
678*15144b0fSOlivier Houchard
679*15144b0fSOlivier Houchard    shiftCount = 0;
680*15144b0fSOlivier Houchard    if ( a < ( (bits64) 1 )<<32 ) {
681*15144b0fSOlivier Houchard        shiftCount += 32;
682*15144b0fSOlivier Houchard    }
683*15144b0fSOlivier Houchard    else {
684*15144b0fSOlivier Houchard        a >>= 32;
685*15144b0fSOlivier Houchard    }
686*15144b0fSOlivier Houchard    shiftCount += countLeadingZeros32( a );
687*15144b0fSOlivier Houchard    return shiftCount;
688*15144b0fSOlivier Houchard
689*15144b0fSOlivier Houchard}
690*15144b0fSOlivier Houchard
691*15144b0fSOlivier Houchard/*
692*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
693*15144b0fSOlivier HouchardReturns 1 if the 128-bit value formed by concatenating `a0' and `a1'
694*15144b0fSOlivier Houchardis equal to the 128-bit value formed by concatenating `b0' and `b1'.
695*15144b0fSOlivier HouchardOtherwise, returns 0.
696*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
697*15144b0fSOlivier Houchard*/
698*15144b0fSOlivier HouchardINLINE flag eq128( bits64 a0, bits64 a1, bits64 b0, bits64 b1 )
699*15144b0fSOlivier Houchard{
700*15144b0fSOlivier Houchard
701*15144b0fSOlivier Houchard    return ( a0 == b0 ) && ( a1 == b1 );
702*15144b0fSOlivier Houchard
703*15144b0fSOlivier Houchard}
704*15144b0fSOlivier Houchard
705*15144b0fSOlivier Houchard/*
706*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
707*15144b0fSOlivier HouchardReturns 1 if the 128-bit value formed by concatenating `a0' and `a1' is less
708*15144b0fSOlivier Houchardthan or equal to the 128-bit value formed by concatenating `b0' and `b1'.
709*15144b0fSOlivier HouchardOtherwise, returns 0.
710*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
711*15144b0fSOlivier Houchard*/
712*15144b0fSOlivier HouchardINLINE flag le128( bits64 a0, bits64 a1, bits64 b0, bits64 b1 )
713*15144b0fSOlivier Houchard{
714*15144b0fSOlivier Houchard
715*15144b0fSOlivier Houchard    return ( a0 < b0 ) || ( ( a0 == b0 ) && ( a1 <= b1 ) );
716*15144b0fSOlivier Houchard
717*15144b0fSOlivier Houchard}
718*15144b0fSOlivier Houchard
719*15144b0fSOlivier Houchard/*
720*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
721*15144b0fSOlivier HouchardReturns 1 if the 128-bit value formed by concatenating `a0' and `a1' is less
722*15144b0fSOlivier Houchardthan the 128-bit value formed by concatenating `b0' and `b1'.  Otherwise,
723*15144b0fSOlivier Houchardreturns 0.
724*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
725*15144b0fSOlivier Houchard*/
726*15144b0fSOlivier HouchardINLINE flag lt128( bits64 a0, bits64 a1, bits64 b0, bits64 b1 )
727*15144b0fSOlivier Houchard{
728*15144b0fSOlivier Houchard
729*15144b0fSOlivier Houchard    return ( a0 < b0 ) || ( ( a0 == b0 ) && ( a1 < b1 ) );
730*15144b0fSOlivier Houchard
731*15144b0fSOlivier Houchard}
732*15144b0fSOlivier Houchard
733*15144b0fSOlivier Houchard/*
734*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
735*15144b0fSOlivier HouchardReturns 1 if the 128-bit value formed by concatenating `a0' and `a1' is
736*15144b0fSOlivier Houchardnot equal to the 128-bit value formed by concatenating `b0' and `b1'.
737*15144b0fSOlivier HouchardOtherwise, returns 0.
738*15144b0fSOlivier Houchard-------------------------------------------------------------------------------
739*15144b0fSOlivier Houchard*/
740*15144b0fSOlivier HouchardINLINE flag ne128( bits64 a0, bits64 a1, bits64 b0, bits64 b1 )
741*15144b0fSOlivier Houchard{
742*15144b0fSOlivier Houchard
743*15144b0fSOlivier Houchard    return ( a0 != b0 ) || ( a1 != b1 );
744*15144b0fSOlivier Houchard
745*15144b0fSOlivier Houchard}
746*15144b0fSOlivier Houchard
747