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