115144b0fSOlivier Houchard
215144b0fSOlivier Houchard/*
315144b0fSOlivier Houchard===============================================================================
415144b0fSOlivier Houchard
515144b0fSOlivier HouchardThis C source fragment is part of the SoftFloat IEC/IEEE Floating-point
615144b0fSOlivier HouchardArithmetic Package, Release 2a.
715144b0fSOlivier Houchard
815144b0fSOlivier HouchardWritten by John R. Hauser.  This work was made possible in part by the
915144b0fSOlivier HouchardInternational Computer Science Institute, located at Suite 600, 1947 Center
1015144b0fSOlivier HouchardStreet, Berkeley, California 94704.  Funding was partially provided by the
1115144b0fSOlivier HouchardNational Science Foundation under grant MIP-9311980.  The original version
1215144b0fSOlivier Houchardof this code was written as part of a project to build a fixed-point vector
1315144b0fSOlivier Houchardprocessor in collaboration with the University of California at Berkeley,
1415144b0fSOlivier Houchardoverseen by Profs. Nelson Morgan and John Wawrzynek.  More information
1515144b0fSOlivier Houchardis available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/
1615144b0fSOlivier Houchardarithmetic/SoftFloat.html'.
1715144b0fSOlivier Houchard
1815144b0fSOlivier HouchardTHIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE.  Although reasonable effort
1915144b0fSOlivier Houchardhas been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
2015144b0fSOlivier HouchardTIMES RESULT IN INCORRECT BEHAVIOR.  USE OF THIS SOFTWARE IS RESTRICTED TO
2115144b0fSOlivier HouchardPERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
2215144b0fSOlivier HouchardAND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
2315144b0fSOlivier Houchard
2415144b0fSOlivier HouchardDerivative works are acceptable, even for commercial purposes, so long as
2515144b0fSOlivier Houchard(1) they include prominent notice that the work is derivative, and (2) they
2615144b0fSOlivier Houchardinclude prominent notice akin to these four paragraphs for those parts of
2715144b0fSOlivier Houchardthis code that are retained.
2815144b0fSOlivier Houchard
2915144b0fSOlivier Houchard===============================================================================
3015144b0fSOlivier Houchard*/
3115144b0fSOlivier Houchard
3215144b0fSOlivier Houchard/*
3315144b0fSOlivier Houchard-------------------------------------------------------------------------------
3415144b0fSOlivier HouchardShifts `a' right by the number of bits given in `count'.  If any nonzero
3515144b0fSOlivier Houchardbits are shifted off, they are ``jammed'' into the least significant bit of
3615144b0fSOlivier Houchardthe result by setting the least significant bit to 1.  The value of `count'
3715144b0fSOlivier Houchardcan be arbitrarily large; in particular, if `count' is greater than 32, the
3815144b0fSOlivier Houchardresult will be either 0 or 1, depending on whether `a' is zero or nonzero.
3915144b0fSOlivier HouchardThe result is stored in the location pointed to by `zPtr'.
4015144b0fSOlivier Houchard-------------------------------------------------------------------------------
4115144b0fSOlivier Houchard*/
4215144b0fSOlivier HouchardINLINE void shift32RightJamming( bits32 a, int16 count, bits32 *zPtr )
4315144b0fSOlivier Houchard{
4415144b0fSOlivier Houchard    bits32 z;
4515144b0fSOlivier Houchard
4615144b0fSOlivier Houchard    if ( count == 0 ) {
4715144b0fSOlivier Houchard        z = a;
4815144b0fSOlivier Houchard    }
4915144b0fSOlivier Houchard    else if ( count < 32 ) {
5015144b0fSOlivier Houchard        z = ( a>>count ) | ( ( a<<( ( - count ) & 31 ) ) != 0 );
5115144b0fSOlivier Houchard    }
5215144b0fSOlivier Houchard    else {
5315144b0fSOlivier Houchard        z = ( a != 0 );
5415144b0fSOlivier Houchard    }
5515144b0fSOlivier Houchard    *zPtr = z;
5615144b0fSOlivier Houchard
5715144b0fSOlivier Houchard}
5815144b0fSOlivier Houchard
5915144b0fSOlivier Houchard/*
6015144b0fSOlivier Houchard-------------------------------------------------------------------------------
6115144b0fSOlivier HouchardShifts the 64-bit value formed by concatenating `a0' and `a1' right by the
6215144b0fSOlivier Houchardnumber of bits given in `count'.  Any bits shifted off are lost.  The value
6315144b0fSOlivier Houchardof `count' can be arbitrarily large; in particular, if `count' is greater
6415144b0fSOlivier Houchardthan 64, the result will be 0.  The result is broken into two 32-bit pieces
6515144b0fSOlivier Houchardwhich are stored at the locations pointed to by `z0Ptr' and `z1Ptr'.
6615144b0fSOlivier Houchard-------------------------------------------------------------------------------
6715144b0fSOlivier Houchard*/
6815144b0fSOlivier HouchardINLINE void
6915144b0fSOlivier Houchard shift64Right(
7015144b0fSOlivier Houchard     bits32 a0, bits32 a1, int16 count, bits32 *z0Ptr, bits32 *z1Ptr )
7115144b0fSOlivier Houchard{
7215144b0fSOlivier Houchard    bits32 z0, z1;
7315144b0fSOlivier Houchard    int8 negCount = ( - count ) & 31;
7415144b0fSOlivier Houchard
7515144b0fSOlivier Houchard    if ( count == 0 ) {
7615144b0fSOlivier Houchard        z1 = a1;
7715144b0fSOlivier Houchard        z0 = a0;
7815144b0fSOlivier Houchard    }
7915144b0fSOlivier Houchard    else if ( count < 32 ) {
8015144b0fSOlivier Houchard        z1 = ( a0<<negCount ) | ( a1>>count );
8115144b0fSOlivier Houchard        z0 = a0>>count;
8215144b0fSOlivier Houchard    }
8315144b0fSOlivier Houchard    else {
8415144b0fSOlivier Houchard        z1 = ( count < 64 ) ? ( a0>>( count & 31 ) ) : 0;
8515144b0fSOlivier Houchard        z0 = 0;
8615144b0fSOlivier Houchard    }
8715144b0fSOlivier Houchard    *z1Ptr = z1;
8815144b0fSOlivier Houchard    *z0Ptr = z0;
8915144b0fSOlivier Houchard
9015144b0fSOlivier Houchard}
9115144b0fSOlivier Houchard
9215144b0fSOlivier Houchard/*
9315144b0fSOlivier Houchard-------------------------------------------------------------------------------
9415144b0fSOlivier HouchardShifts the 64-bit value formed by concatenating `a0' and `a1' right by the
9515144b0fSOlivier Houchardnumber of bits given in `count'.  If any nonzero bits are shifted off, they
9615144b0fSOlivier Houchardare ``jammed'' into the least significant bit of the result by setting the
9715144b0fSOlivier Houchardleast significant bit to 1.  The value of `count' can be arbitrarily large;
9815144b0fSOlivier Houchardin particular, if `count' is greater than 64, the result will be either 0
9915144b0fSOlivier Houchardor 1, depending on whether the concatenation of `a0' and `a1' is zero or
10015144b0fSOlivier Houchardnonzero.  The result is broken into two 32-bit pieces which are stored at
10115144b0fSOlivier Houchardthe locations pointed to by `z0Ptr' and `z1Ptr'.
10215144b0fSOlivier Houchard-------------------------------------------------------------------------------
10315144b0fSOlivier Houchard*/
10415144b0fSOlivier HouchardINLINE void
10515144b0fSOlivier Houchard shift64RightJamming(
10615144b0fSOlivier Houchard     bits32 a0, bits32 a1, int16 count, bits32 *z0Ptr, bits32 *z1Ptr )
10715144b0fSOlivier Houchard{
10815144b0fSOlivier Houchard    bits32 z0, z1;
10915144b0fSOlivier Houchard    int8 negCount = ( - count ) & 31;
11015144b0fSOlivier Houchard
11115144b0fSOlivier Houchard    if ( count == 0 ) {
11215144b0fSOlivier Houchard        z1 = a1;
11315144b0fSOlivier Houchard        z0 = a0;
11415144b0fSOlivier Houchard    }
11515144b0fSOlivier Houchard    else if ( count < 32 ) {
11615144b0fSOlivier Houchard        z1 = ( a0<<negCount ) | ( a1>>count ) | ( ( a1<<negCount ) != 0 );
11715144b0fSOlivier Houchard        z0 = a0>>count;
11815144b0fSOlivier Houchard    }
11915144b0fSOlivier Houchard    else {
12015144b0fSOlivier Houchard        if ( count == 32 ) {
12115144b0fSOlivier Houchard            z1 = a0 | ( a1 != 0 );
12215144b0fSOlivier Houchard        }
12315144b0fSOlivier Houchard        else if ( count < 64 ) {
12415144b0fSOlivier Houchard            z1 = ( a0>>( count & 31 ) ) | ( ( ( a0<<negCount ) | a1 ) != 0 );
12515144b0fSOlivier Houchard        }
12615144b0fSOlivier Houchard        else {
12715144b0fSOlivier Houchard            z1 = ( ( a0 | a1 ) != 0 );
12815144b0fSOlivier Houchard        }
12915144b0fSOlivier Houchard        z0 = 0;
13015144b0fSOlivier Houchard    }
13115144b0fSOlivier Houchard    *z1Ptr = z1;
13215144b0fSOlivier Houchard    *z0Ptr = z0;
13315144b0fSOlivier Houchard
13415144b0fSOlivier Houchard}
13515144b0fSOlivier Houchard
13615144b0fSOlivier Houchard/*
13715144b0fSOlivier Houchard-------------------------------------------------------------------------------
13815144b0fSOlivier HouchardShifts the 96-bit value formed by concatenating `a0', `a1', and `a2' right
13915144b0fSOlivier Houchardby 32 _plus_ the number of bits given in `count'.  The shifted result is
14015144b0fSOlivier Houchardat most 64 nonzero bits; these are broken into two 32-bit pieces which are
14115144b0fSOlivier Houchardstored at the locations pointed to by `z0Ptr' and `z1Ptr'.  The bits shifted
14215144b0fSOlivier Houchardoff form a third 32-bit result as follows:  The _last_ bit shifted off is
14315144b0fSOlivier Houchardthe most-significant bit of the extra result, and the other 31 bits of the
14415144b0fSOlivier Houchardextra result are all zero if and only if _all_but_the_last_ bits shifted off
14515144b0fSOlivier Houchardwere all zero.  This extra result is stored in the location pointed to by
14615144b0fSOlivier Houchard`z2Ptr'.  The value of `count' can be arbitrarily large.
14715144b0fSOlivier Houchard    (This routine makes more sense if `a0', `a1', and `a2' are considered
14815144b0fSOlivier Houchardto form a fixed-point value with binary point between `a1' and `a2'.  This
14915144b0fSOlivier Houchardfixed-point value is shifted right by the number of bits given in `count',
15015144b0fSOlivier Houchardand the integer part of the result is returned at the locations pointed to
15115144b0fSOlivier Houchardby `z0Ptr' and `z1Ptr'.  The fractional part of the result may be slightly
15215144b0fSOlivier Houchardcorrupted as described above, and is returned at the location pointed to by
15315144b0fSOlivier Houchard`z2Ptr'.)
15415144b0fSOlivier Houchard-------------------------------------------------------------------------------
15515144b0fSOlivier Houchard*/
15615144b0fSOlivier HouchardINLINE void
15715144b0fSOlivier Houchard shift64ExtraRightJamming(
15815144b0fSOlivier Houchard     bits32 a0,
15915144b0fSOlivier Houchard     bits32 a1,
16015144b0fSOlivier Houchard     bits32 a2,
16115144b0fSOlivier Houchard     int16 count,
16215144b0fSOlivier Houchard     bits32 *z0Ptr,
16315144b0fSOlivier Houchard     bits32 *z1Ptr,
16415144b0fSOlivier Houchard     bits32 *z2Ptr
16515144b0fSOlivier Houchard )
16615144b0fSOlivier Houchard{
16715144b0fSOlivier Houchard    bits32 z0, z1, z2;
16815144b0fSOlivier Houchard    int8 negCount = ( - count ) & 31;
16915144b0fSOlivier Houchard
17015144b0fSOlivier Houchard    if ( count == 0 ) {
17115144b0fSOlivier Houchard        z2 = a2;
17215144b0fSOlivier Houchard        z1 = a1;
17315144b0fSOlivier Houchard        z0 = a0;
17415144b0fSOlivier Houchard    }
17515144b0fSOlivier Houchard    else {
17615144b0fSOlivier Houchard        if ( count < 32 ) {
17715144b0fSOlivier Houchard            z2 = a1<<negCount;
17815144b0fSOlivier Houchard            z1 = ( a0<<negCount ) | ( a1>>count );
17915144b0fSOlivier Houchard            z0 = a0>>count;
18015144b0fSOlivier Houchard        }
18115144b0fSOlivier Houchard        else {
18215144b0fSOlivier Houchard            if ( count == 32 ) {
18315144b0fSOlivier Houchard                z2 = a1;
18415144b0fSOlivier Houchard                z1 = a0;
18515144b0fSOlivier Houchard            }
18615144b0fSOlivier Houchard            else {
18715144b0fSOlivier Houchard                a2 |= a1;
18815144b0fSOlivier Houchard                if ( count < 64 ) {
18915144b0fSOlivier Houchard                    z2 = a0<<negCount;
19015144b0fSOlivier Houchard                    z1 = a0>>( count & 31 );
19115144b0fSOlivier Houchard                }
19215144b0fSOlivier Houchard                else {
19315144b0fSOlivier Houchard                    z2 = ( count == 64 ) ? a0 : ( a0 != 0 );
19415144b0fSOlivier Houchard                    z1 = 0;
19515144b0fSOlivier Houchard                }
19615144b0fSOlivier Houchard            }
19715144b0fSOlivier Houchard            z0 = 0;
19815144b0fSOlivier Houchard        }
19915144b0fSOlivier Houchard        z2 |= ( a2 != 0 );
20015144b0fSOlivier Houchard    }
20115144b0fSOlivier Houchard    *z2Ptr = z2;
20215144b0fSOlivier Houchard    *z1Ptr = z1;
20315144b0fSOlivier Houchard    *z0Ptr = z0;
20415144b0fSOlivier Houchard
20515144b0fSOlivier Houchard}
20615144b0fSOlivier Houchard
20715144b0fSOlivier Houchard/*
20815144b0fSOlivier Houchard-------------------------------------------------------------------------------
20915144b0fSOlivier HouchardShifts the 64-bit value formed by concatenating `a0' and `a1' left by the
21015144b0fSOlivier Houchardnumber of bits given in `count'.  Any bits shifted off are lost.  The value
21115144b0fSOlivier Houchardof `count' must be less than 32.  The result is broken into two 32-bit
21215144b0fSOlivier Houchardpieces which are stored at the locations pointed to by `z0Ptr' and `z1Ptr'.
21315144b0fSOlivier Houchard-------------------------------------------------------------------------------
21415144b0fSOlivier Houchard*/
21515144b0fSOlivier HouchardINLINE void
21615144b0fSOlivier Houchard shortShift64Left(
21715144b0fSOlivier Houchard     bits32 a0, bits32 a1, int16 count, bits32 *z0Ptr, bits32 *z1Ptr )
21815144b0fSOlivier Houchard{
21915144b0fSOlivier Houchard
22015144b0fSOlivier Houchard    *z1Ptr = a1<<count;
22115144b0fSOlivier Houchard    *z0Ptr =
22215144b0fSOlivier Houchard        ( count == 0 ) ? a0 : ( a0<<count ) | ( a1>>( ( - count ) & 31 ) );
22315144b0fSOlivier Houchard
22415144b0fSOlivier Houchard}
22515144b0fSOlivier Houchard
22615144b0fSOlivier Houchard/*
22715144b0fSOlivier Houchard-------------------------------------------------------------------------------
22815144b0fSOlivier HouchardShifts the 96-bit value formed by concatenating `a0', `a1', and `a2' left
22915144b0fSOlivier Houchardby the number of bits given in `count'.  Any bits shifted off are lost.
23015144b0fSOlivier HouchardThe value of `count' must be less than 32.  The result is broken into three
23115144b0fSOlivier Houchard32-bit pieces which are stored at the locations pointed to by `z0Ptr',
23215144b0fSOlivier Houchard`z1Ptr', and `z2Ptr'.
23315144b0fSOlivier Houchard-------------------------------------------------------------------------------
23415144b0fSOlivier Houchard*/
23515144b0fSOlivier HouchardINLINE void
23615144b0fSOlivier Houchard shortShift96Left(
23715144b0fSOlivier Houchard     bits32 a0,
23815144b0fSOlivier Houchard     bits32 a1,
23915144b0fSOlivier Houchard     bits32 a2,
24015144b0fSOlivier Houchard     int16 count,
24115144b0fSOlivier Houchard     bits32 *z0Ptr,
24215144b0fSOlivier Houchard     bits32 *z1Ptr,
24315144b0fSOlivier Houchard     bits32 *z2Ptr
24415144b0fSOlivier Houchard )
24515144b0fSOlivier Houchard{
24615144b0fSOlivier Houchard    bits32 z0, z1, z2;
24715144b0fSOlivier Houchard    int8 negCount;
24815144b0fSOlivier Houchard
24915144b0fSOlivier Houchard    z2 = a2<<count;
25015144b0fSOlivier Houchard    z1 = a1<<count;
25115144b0fSOlivier Houchard    z0 = a0<<count;
25215144b0fSOlivier Houchard    if ( 0 < count ) {
25315144b0fSOlivier Houchard        negCount = ( ( - count ) & 31 );
25415144b0fSOlivier Houchard        z1 |= a2>>negCount;
25515144b0fSOlivier Houchard        z0 |= a1>>negCount;
25615144b0fSOlivier Houchard    }
25715144b0fSOlivier Houchard    *z2Ptr = z2;
25815144b0fSOlivier Houchard    *z1Ptr = z1;
25915144b0fSOlivier Houchard    *z0Ptr = z0;
26015144b0fSOlivier Houchard
26115144b0fSOlivier Houchard}
26215144b0fSOlivier Houchard
26315144b0fSOlivier Houchard/*
26415144b0fSOlivier Houchard-------------------------------------------------------------------------------
26515144b0fSOlivier HouchardAdds the 64-bit value formed by concatenating `a0' and `a1' to the 64-bit
26615144b0fSOlivier Houchardvalue formed by concatenating `b0' and `b1'.  Addition is modulo 2^64, so
26715144b0fSOlivier Houchardany carry out is lost.  The result is broken into two 32-bit pieces which
26815144b0fSOlivier Houchardare stored at the locations pointed to by `z0Ptr' and `z1Ptr'.
26915144b0fSOlivier Houchard-------------------------------------------------------------------------------
27015144b0fSOlivier Houchard*/
27115144b0fSOlivier HouchardINLINE void
27215144b0fSOlivier Houchard add64(
27315144b0fSOlivier Houchard     bits32 a0, bits32 a1, bits32 b0, bits32 b1, bits32 *z0Ptr, bits32 *z1Ptr )
27415144b0fSOlivier Houchard{
27515144b0fSOlivier Houchard    bits32 z1;
27615144b0fSOlivier Houchard
27715144b0fSOlivier Houchard    z1 = a1 + b1;
27815144b0fSOlivier Houchard    *z1Ptr = z1;
27915144b0fSOlivier Houchard    *z0Ptr = a0 + b0 + ( z1 < a1 );
28015144b0fSOlivier Houchard
28115144b0fSOlivier Houchard}
28215144b0fSOlivier Houchard
28315144b0fSOlivier Houchard/*
28415144b0fSOlivier Houchard-------------------------------------------------------------------------------
28515144b0fSOlivier HouchardAdds the 96-bit value formed by concatenating `a0', `a1', and `a2' to the
28615144b0fSOlivier Houchard96-bit value formed by concatenating `b0', `b1', and `b2'.  Addition is
28715144b0fSOlivier Houchardmodulo 2^96, so any carry out is lost.  The result is broken into three
28815144b0fSOlivier Houchard32-bit pieces which are stored at the locations pointed to by `z0Ptr',
28915144b0fSOlivier Houchard`z1Ptr', and `z2Ptr'.
29015144b0fSOlivier Houchard-------------------------------------------------------------------------------
29115144b0fSOlivier Houchard*/
29215144b0fSOlivier HouchardINLINE void
29315144b0fSOlivier Houchard add96(
29415144b0fSOlivier Houchard     bits32 a0,
29515144b0fSOlivier Houchard     bits32 a1,
29615144b0fSOlivier Houchard     bits32 a2,
29715144b0fSOlivier Houchard     bits32 b0,
29815144b0fSOlivier Houchard     bits32 b1,
29915144b0fSOlivier Houchard     bits32 b2,
30015144b0fSOlivier Houchard     bits32 *z0Ptr,
30115144b0fSOlivier Houchard     bits32 *z1Ptr,
30215144b0fSOlivier Houchard     bits32 *z2Ptr
30315144b0fSOlivier Houchard )
30415144b0fSOlivier Houchard{
30515144b0fSOlivier Houchard    bits32 z0, z1, z2;
30615144b0fSOlivier Houchard    int8 carry0, carry1;
30715144b0fSOlivier Houchard
30815144b0fSOlivier Houchard    z2 = a2 + b2;
30915144b0fSOlivier Houchard    carry1 = ( z2 < a2 );
31015144b0fSOlivier Houchard    z1 = a1 + b1;
31115144b0fSOlivier Houchard    carry0 = ( z1 < a1 );
31215144b0fSOlivier Houchard    z0 = a0 + b0;
31315144b0fSOlivier Houchard    z1 += carry1;
314c36abe0dSDavid Schultz    z0 += ( z1 < (bits32)carry1 );
31515144b0fSOlivier Houchard    z0 += carry0;
31615144b0fSOlivier Houchard    *z2Ptr = z2;
31715144b0fSOlivier Houchard    *z1Ptr = z1;
31815144b0fSOlivier Houchard    *z0Ptr = z0;
31915144b0fSOlivier Houchard
32015144b0fSOlivier Houchard}
32115144b0fSOlivier Houchard
32215144b0fSOlivier Houchard/*
32315144b0fSOlivier Houchard-------------------------------------------------------------------------------
32415144b0fSOlivier HouchardSubtracts the 64-bit value formed by concatenating `b0' and `b1' from the
32515144b0fSOlivier Houchard64-bit value formed by concatenating `a0' and `a1'.  Subtraction is modulo
32615144b0fSOlivier Houchard2^64, so any borrow out (carry out) is lost.  The result is broken into two
32715144b0fSOlivier Houchard32-bit pieces which are stored at the locations pointed to by `z0Ptr' and
32815144b0fSOlivier Houchard`z1Ptr'.
32915144b0fSOlivier Houchard-------------------------------------------------------------------------------
33015144b0fSOlivier Houchard*/
33115144b0fSOlivier HouchardINLINE void
33215144b0fSOlivier Houchard sub64(
33315144b0fSOlivier Houchard     bits32 a0, bits32 a1, bits32 b0, bits32 b1, bits32 *z0Ptr, bits32 *z1Ptr )
33415144b0fSOlivier Houchard{
33515144b0fSOlivier Houchard
33615144b0fSOlivier Houchard    *z1Ptr = a1 - b1;
33715144b0fSOlivier Houchard    *z0Ptr = a0 - b0 - ( a1 < b1 );
33815144b0fSOlivier Houchard
33915144b0fSOlivier Houchard}
34015144b0fSOlivier Houchard
34115144b0fSOlivier Houchard/*
34215144b0fSOlivier Houchard-------------------------------------------------------------------------------
34315144b0fSOlivier HouchardSubtracts the 96-bit value formed by concatenating `b0', `b1', and `b2' from
34415144b0fSOlivier Houchardthe 96-bit value formed by concatenating `a0', `a1', and `a2'.  Subtraction
34515144b0fSOlivier Houchardis modulo 2^96, so any borrow out (carry out) is lost.  The result is broken
34615144b0fSOlivier Houchardinto three 32-bit pieces which are stored at the locations pointed to by
34715144b0fSOlivier Houchard`z0Ptr', `z1Ptr', and `z2Ptr'.
34815144b0fSOlivier Houchard-------------------------------------------------------------------------------
34915144b0fSOlivier Houchard*/
35015144b0fSOlivier HouchardINLINE void
35115144b0fSOlivier Houchard sub96(
35215144b0fSOlivier Houchard     bits32 a0,
35315144b0fSOlivier Houchard     bits32 a1,
35415144b0fSOlivier Houchard     bits32 a2,
35515144b0fSOlivier Houchard     bits32 b0,
35615144b0fSOlivier Houchard     bits32 b1,
35715144b0fSOlivier Houchard     bits32 b2,
35815144b0fSOlivier Houchard     bits32 *z0Ptr,
35915144b0fSOlivier Houchard     bits32 *z1Ptr,
36015144b0fSOlivier Houchard     bits32 *z2Ptr
36115144b0fSOlivier Houchard )
36215144b0fSOlivier Houchard{
36315144b0fSOlivier Houchard    bits32 z0, z1, z2;
36415144b0fSOlivier Houchard    int8 borrow0, borrow1;
36515144b0fSOlivier Houchard
36615144b0fSOlivier Houchard    z2 = a2 - b2;
36715144b0fSOlivier Houchard    borrow1 = ( a2 < b2 );
36815144b0fSOlivier Houchard    z1 = a1 - b1;
36915144b0fSOlivier Houchard    borrow0 = ( a1 < b1 );
37015144b0fSOlivier Houchard    z0 = a0 - b0;
371c36abe0dSDavid Schultz    z0 -= ( z1 < (bits32)borrow1 );
37215144b0fSOlivier Houchard    z1 -= borrow1;
37315144b0fSOlivier Houchard    z0 -= borrow0;
37415144b0fSOlivier Houchard    *z2Ptr = z2;
37515144b0fSOlivier Houchard    *z1Ptr = z1;
37615144b0fSOlivier Houchard    *z0Ptr = z0;
37715144b0fSOlivier Houchard
37815144b0fSOlivier Houchard}
37915144b0fSOlivier Houchard
38015144b0fSOlivier Houchard/*
38115144b0fSOlivier Houchard-------------------------------------------------------------------------------
38215144b0fSOlivier HouchardMultiplies `a' by `b' to obtain a 64-bit product.  The product is broken
38315144b0fSOlivier Houchardinto two 32-bit pieces which are stored at the locations pointed to by
38415144b0fSOlivier Houchard`z0Ptr' and `z1Ptr'.
38515144b0fSOlivier Houchard-------------------------------------------------------------------------------
38615144b0fSOlivier Houchard*/
38715144b0fSOlivier HouchardINLINE void mul32To64( bits32 a, bits32 b, bits32 *z0Ptr, bits32 *z1Ptr )
38815144b0fSOlivier Houchard{
38915144b0fSOlivier Houchard    bits16 aHigh, aLow, bHigh, bLow;
39015144b0fSOlivier Houchard    bits32 z0, zMiddleA, zMiddleB, z1;
39115144b0fSOlivier Houchard
39215144b0fSOlivier Houchard    aLow = a;
39315144b0fSOlivier Houchard    aHigh = a>>16;
39415144b0fSOlivier Houchard    bLow = b;
39515144b0fSOlivier Houchard    bHigh = b>>16;
39615144b0fSOlivier Houchard    z1 = ( (bits32) aLow ) * bLow;
39715144b0fSOlivier Houchard    zMiddleA = ( (bits32) aLow ) * bHigh;
39815144b0fSOlivier Houchard    zMiddleB = ( (bits32) aHigh ) * bLow;
39915144b0fSOlivier Houchard    z0 = ( (bits32) aHigh ) * bHigh;
40015144b0fSOlivier Houchard    zMiddleA += zMiddleB;
40115144b0fSOlivier Houchard    z0 += ( ( (bits32) ( zMiddleA < zMiddleB ) )<<16 ) + ( zMiddleA>>16 );
40215144b0fSOlivier Houchard    zMiddleA <<= 16;
40315144b0fSOlivier Houchard    z1 += zMiddleA;
40415144b0fSOlivier Houchard    z0 += ( z1 < zMiddleA );
40515144b0fSOlivier Houchard    *z1Ptr = z1;
40615144b0fSOlivier Houchard    *z0Ptr = z0;
40715144b0fSOlivier Houchard
40815144b0fSOlivier Houchard}
40915144b0fSOlivier Houchard
41015144b0fSOlivier Houchard/*
41115144b0fSOlivier Houchard-------------------------------------------------------------------------------
41215144b0fSOlivier HouchardMultiplies the 64-bit value formed by concatenating `a0' and `a1' by `b'
41315144b0fSOlivier Houchardto obtain a 96-bit product.  The product is broken into three 32-bit pieces
41415144b0fSOlivier Houchardwhich are stored at the locations pointed to by `z0Ptr', `z1Ptr', and
41515144b0fSOlivier Houchard`z2Ptr'.
41615144b0fSOlivier Houchard-------------------------------------------------------------------------------
41715144b0fSOlivier Houchard*/
41815144b0fSOlivier HouchardINLINE void
41915144b0fSOlivier Houchard mul64By32To96(
42015144b0fSOlivier Houchard     bits32 a0,
42115144b0fSOlivier Houchard     bits32 a1,
42215144b0fSOlivier Houchard     bits32 b,
42315144b0fSOlivier Houchard     bits32 *z0Ptr,
42415144b0fSOlivier Houchard     bits32 *z1Ptr,
42515144b0fSOlivier Houchard     bits32 *z2Ptr
42615144b0fSOlivier Houchard )
42715144b0fSOlivier Houchard{
42815144b0fSOlivier Houchard    bits32 z0, z1, z2, more1;
42915144b0fSOlivier Houchard
43015144b0fSOlivier Houchard    mul32To64( a1, b, &z1, &z2 );
43115144b0fSOlivier Houchard    mul32To64( a0, b, &z0, &more1 );
43215144b0fSOlivier Houchard    add64( z0, more1, 0, z1, &z0, &z1 );
43315144b0fSOlivier Houchard    *z2Ptr = z2;
43415144b0fSOlivier Houchard    *z1Ptr = z1;
43515144b0fSOlivier Houchard    *z0Ptr = z0;
43615144b0fSOlivier Houchard
43715144b0fSOlivier Houchard}
43815144b0fSOlivier Houchard
43915144b0fSOlivier Houchard/*
44015144b0fSOlivier Houchard-------------------------------------------------------------------------------
44115144b0fSOlivier HouchardMultiplies the 64-bit value formed by concatenating `a0' and `a1' to the
44215144b0fSOlivier Houchard64-bit value formed by concatenating `b0' and `b1' to obtain a 128-bit
44315144b0fSOlivier Houchardproduct.  The product is broken into four 32-bit pieces which are stored at
44415144b0fSOlivier Houchardthe locations pointed to by `z0Ptr', `z1Ptr', `z2Ptr', and `z3Ptr'.
44515144b0fSOlivier Houchard-------------------------------------------------------------------------------
44615144b0fSOlivier Houchard*/
44715144b0fSOlivier HouchardINLINE void
44815144b0fSOlivier Houchard mul64To128(
44915144b0fSOlivier Houchard     bits32 a0,
45015144b0fSOlivier Houchard     bits32 a1,
45115144b0fSOlivier Houchard     bits32 b0,
45215144b0fSOlivier Houchard     bits32 b1,
45315144b0fSOlivier Houchard     bits32 *z0Ptr,
45415144b0fSOlivier Houchard     bits32 *z1Ptr,
45515144b0fSOlivier Houchard     bits32 *z2Ptr,
45615144b0fSOlivier Houchard     bits32 *z3Ptr
45715144b0fSOlivier Houchard )
45815144b0fSOlivier Houchard{
45915144b0fSOlivier Houchard    bits32 z0, z1, z2, z3;
46015144b0fSOlivier Houchard    bits32 more1, more2;
46115144b0fSOlivier Houchard
46215144b0fSOlivier Houchard    mul32To64( a1, b1, &z2, &z3 );
46315144b0fSOlivier Houchard    mul32To64( a1, b0, &z1, &more2 );
46415144b0fSOlivier Houchard    add64( z1, more2, 0, z2, &z1, &z2 );
46515144b0fSOlivier Houchard    mul32To64( a0, b0, &z0, &more1 );
46615144b0fSOlivier Houchard    add64( z0, more1, 0, z1, &z0, &z1 );
46715144b0fSOlivier Houchard    mul32To64( a0, b1, &more1, &more2 );
46815144b0fSOlivier Houchard    add64( more1, more2, 0, z2, &more1, &z2 );
46915144b0fSOlivier Houchard    add64( z0, z1, 0, more1, &z0, &z1 );
47015144b0fSOlivier Houchard    *z3Ptr = z3;
47115144b0fSOlivier Houchard    *z2Ptr = z2;
47215144b0fSOlivier Houchard    *z1Ptr = z1;
47315144b0fSOlivier Houchard    *z0Ptr = z0;
47415144b0fSOlivier Houchard
47515144b0fSOlivier Houchard}
47615144b0fSOlivier Houchard
47715144b0fSOlivier Houchard/*
47815144b0fSOlivier Houchard-------------------------------------------------------------------------------
47915144b0fSOlivier HouchardReturns an approximation to the 32-bit integer quotient obtained by dividing
48015144b0fSOlivier Houchard`b' into the 64-bit value formed by concatenating `a0' and `a1'.  The
48115144b0fSOlivier Houcharddivisor `b' must be at least 2^31.  If q is the exact quotient truncated
48215144b0fSOlivier Houchardtoward zero, the approximation returned lies between q and q + 2 inclusive.
48315144b0fSOlivier HouchardIf the exact quotient q is larger than 32 bits, the maximum positive 32-bit
48415144b0fSOlivier Houchardunsigned integer is returned.
48515144b0fSOlivier Houchard-------------------------------------------------------------------------------
48615144b0fSOlivier Houchard*/
48715144b0fSOlivier Houchardstatic bits32 estimateDiv64To32( bits32 a0, bits32 a1, bits32 b )
48815144b0fSOlivier Houchard{
48915144b0fSOlivier Houchard    bits32 b0, b1;
49015144b0fSOlivier Houchard    bits32 rem0, rem1, term0, term1;
49115144b0fSOlivier Houchard    bits32 z;
49215144b0fSOlivier Houchard
49315144b0fSOlivier Houchard    if ( b <= a0 ) return 0xFFFFFFFF;
49415144b0fSOlivier Houchard    b0 = b>>16;
49515144b0fSOlivier Houchard    z = ( b0<<16 <= a0 ) ? 0xFFFF0000 : ( a0 / b0 )<<16;
49615144b0fSOlivier Houchard    mul32To64( b, z, &term0, &term1 );
49715144b0fSOlivier Houchard    sub64( a0, a1, term0, term1, &rem0, &rem1 );
49815144b0fSOlivier Houchard    while ( ( (sbits32) rem0 ) < 0 ) {
49915144b0fSOlivier Houchard        z -= 0x10000;
50015144b0fSOlivier Houchard        b1 = b<<16;
50115144b0fSOlivier Houchard        add64( rem0, rem1, b0, b1, &rem0, &rem1 );
50215144b0fSOlivier Houchard    }
50315144b0fSOlivier Houchard    rem0 = ( rem0<<16 ) | ( rem1>>16 );
50415144b0fSOlivier Houchard    z |= ( b0<<16 <= rem0 ) ? 0xFFFF : rem0 / b0;
50515144b0fSOlivier Houchard    return z;
50615144b0fSOlivier Houchard
50715144b0fSOlivier Houchard}
50815144b0fSOlivier Houchard
50915144b0fSOlivier Houchard#ifndef SOFTFLOAT_FOR_GCC
51015144b0fSOlivier Houchard/*
51115144b0fSOlivier Houchard-------------------------------------------------------------------------------
51215144b0fSOlivier HouchardReturns an approximation to the square root of the 32-bit significand given
51315144b0fSOlivier Houchardby `a'.  Considered as an integer, `a' must be at least 2^31.  If bit 0 of
51415144b0fSOlivier Houchard`aExp' (the least significant bit) is 1, the integer returned approximates
51515144b0fSOlivier Houchard2^31*sqrt(`a'/2^31), where `a' is considered an integer.  If bit 0 of `aExp'
51615144b0fSOlivier Houchardis 0, the integer returned approximates 2^31*sqrt(`a'/2^30).  In either
51715144b0fSOlivier Houchardcase, the approximation returned lies strictly within +/-2 of the exact
51815144b0fSOlivier Houchardvalue.
51915144b0fSOlivier Houchard-------------------------------------------------------------------------------
52015144b0fSOlivier Houchard*/
52115144b0fSOlivier Houchardstatic bits32 estimateSqrt32( int16 aExp, bits32 a )
52215144b0fSOlivier Houchard{
52315144b0fSOlivier Houchard    static const bits16 sqrtOddAdjustments[] = {
52415144b0fSOlivier Houchard        0x0004, 0x0022, 0x005D, 0x00B1, 0x011D, 0x019F, 0x0236, 0x02E0,
52515144b0fSOlivier Houchard        0x039C, 0x0468, 0x0545, 0x0631, 0x072B, 0x0832, 0x0946, 0x0A67
52615144b0fSOlivier Houchard    };
52715144b0fSOlivier Houchard    static const bits16 sqrtEvenAdjustments[] = {
52815144b0fSOlivier Houchard        0x0A2D, 0x08AF, 0x075A, 0x0629, 0x051A, 0x0429, 0x0356, 0x029E,
52915144b0fSOlivier Houchard        0x0200, 0x0179, 0x0109, 0x00AF, 0x0068, 0x0034, 0x0012, 0x0002
53015144b0fSOlivier Houchard    };
53115144b0fSOlivier Houchard    int8 index;
53215144b0fSOlivier Houchard    bits32 z;
53315144b0fSOlivier Houchard
53415144b0fSOlivier Houchard    index = ( a>>27 ) & 15;
53515144b0fSOlivier Houchard    if ( aExp & 1 ) {
53615144b0fSOlivier Houchard        z = 0x4000 + ( a>>17 ) - sqrtOddAdjustments[ index ];
53715144b0fSOlivier Houchard        z = ( ( a / z )<<14 ) + ( z<<15 );
53815144b0fSOlivier Houchard        a >>= 1;
53915144b0fSOlivier Houchard    }
54015144b0fSOlivier Houchard    else {
54115144b0fSOlivier Houchard        z = 0x8000 + ( a>>17 ) - sqrtEvenAdjustments[ index ];
54215144b0fSOlivier Houchard        z = a / z + z;
54315144b0fSOlivier Houchard        z = ( 0x20000 <= z ) ? 0xFFFF8000 : ( z<<15 );
54415144b0fSOlivier Houchard        if ( z <= a ) return (bits32) ( ( (sbits32) a )>>1 );
54515144b0fSOlivier Houchard    }
54615144b0fSOlivier Houchard    return ( ( estimateDiv64To32( a, 0, z ) )>>1 ) + ( z>>1 );
54715144b0fSOlivier Houchard
54815144b0fSOlivier Houchard}
54915144b0fSOlivier Houchard#endif
55015144b0fSOlivier Houchard
55115144b0fSOlivier Houchard/*
55215144b0fSOlivier Houchard-------------------------------------------------------------------------------
55315144b0fSOlivier HouchardReturns the number of leading 0 bits before the most-significant 1 bit of
55415144b0fSOlivier Houchard`a'.  If `a' is zero, 32 is returned.
55515144b0fSOlivier Houchard-------------------------------------------------------------------------------
55615144b0fSOlivier Houchard*/
55715144b0fSOlivier Houchardstatic int8 countLeadingZeros32( bits32 a )
55815144b0fSOlivier Houchard{
55915144b0fSOlivier Houchard    static const int8 countLeadingZerosHigh[] = {
56015144b0fSOlivier Houchard        8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
56115144b0fSOlivier Houchard        3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
56215144b0fSOlivier Houchard        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
56315144b0fSOlivier Houchard        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
56415144b0fSOlivier Houchard        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
56515144b0fSOlivier Houchard        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
56615144b0fSOlivier Houchard        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
56715144b0fSOlivier Houchard        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
56815144b0fSOlivier Houchard        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
56915144b0fSOlivier Houchard        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57015144b0fSOlivier Houchard        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57115144b0fSOlivier Houchard        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57215144b0fSOlivier Houchard        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57315144b0fSOlivier Houchard        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57415144b0fSOlivier Houchard        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57515144b0fSOlivier Houchard        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
57615144b0fSOlivier Houchard    };
57715144b0fSOlivier Houchard    int8 shiftCount;
57815144b0fSOlivier Houchard
57915144b0fSOlivier Houchard    shiftCount = 0;
58015144b0fSOlivier Houchard    if ( a < 0x10000 ) {
58115144b0fSOlivier Houchard        shiftCount += 16;
58215144b0fSOlivier Houchard        a <<= 16;
58315144b0fSOlivier Houchard    }
58415144b0fSOlivier Houchard    if ( a < 0x1000000 ) {
58515144b0fSOlivier Houchard        shiftCount += 8;
58615144b0fSOlivier Houchard        a <<= 8;
58715144b0fSOlivier Houchard    }
58815144b0fSOlivier Houchard    shiftCount += countLeadingZerosHigh[ a>>24 ];
58915144b0fSOlivier Houchard    return shiftCount;
59015144b0fSOlivier Houchard
59115144b0fSOlivier Houchard}
59215144b0fSOlivier Houchard
59315144b0fSOlivier Houchard/*
59415144b0fSOlivier Houchard-------------------------------------------------------------------------------
59515144b0fSOlivier HouchardReturns 1 if the 64-bit value formed by concatenating `a0' and `a1' is
59615144b0fSOlivier Houchardequal to the 64-bit value formed by concatenating `b0' and `b1'.  Otherwise,
59715144b0fSOlivier Houchardreturns 0.
59815144b0fSOlivier Houchard-------------------------------------------------------------------------------
59915144b0fSOlivier Houchard*/
60015144b0fSOlivier HouchardINLINE flag eq64( bits32 a0, bits32 a1, bits32 b0, bits32 b1 )
60115144b0fSOlivier Houchard{
60215144b0fSOlivier Houchard
60315144b0fSOlivier Houchard    return ( a0 == b0 ) && ( a1 == b1 );
60415144b0fSOlivier Houchard
60515144b0fSOlivier Houchard}
60615144b0fSOlivier Houchard
60715144b0fSOlivier Houchard/*
60815144b0fSOlivier Houchard-------------------------------------------------------------------------------
60915144b0fSOlivier HouchardReturns 1 if the 64-bit value formed by concatenating `a0' and `a1' is less
61015144b0fSOlivier Houchardthan or equal to the 64-bit value formed by concatenating `b0' and `b1'.
61115144b0fSOlivier HouchardOtherwise, returns 0.
61215144b0fSOlivier Houchard-------------------------------------------------------------------------------
61315144b0fSOlivier Houchard*/
61415144b0fSOlivier HouchardINLINE flag le64( bits32 a0, bits32 a1, bits32 b0, bits32 b1 )
61515144b0fSOlivier Houchard{
61615144b0fSOlivier Houchard
61715144b0fSOlivier Houchard    return ( a0 < b0 ) || ( ( a0 == b0 ) && ( a1 <= b1 ) );
61815144b0fSOlivier Houchard
61915144b0fSOlivier Houchard}
62015144b0fSOlivier Houchard
62115144b0fSOlivier Houchard/*
62215144b0fSOlivier Houchard-------------------------------------------------------------------------------
62315144b0fSOlivier HouchardReturns 1 if the 64-bit value formed by concatenating `a0' and `a1' is less
62415144b0fSOlivier Houchardthan the 64-bit value formed by concatenating `b0' and `b1'.  Otherwise,
62515144b0fSOlivier Houchardreturns 0.
62615144b0fSOlivier Houchard-------------------------------------------------------------------------------
62715144b0fSOlivier Houchard*/
62815144b0fSOlivier HouchardINLINE flag lt64( bits32 a0, bits32 a1, bits32 b0, bits32 b1 )
62915144b0fSOlivier Houchard{
63015144b0fSOlivier Houchard
63115144b0fSOlivier Houchard    return ( a0 < b0 ) || ( ( a0 == b0 ) && ( a1 < b1 ) );
63215144b0fSOlivier Houchard
63315144b0fSOlivier Houchard}
63415144b0fSOlivier Houchard
63515144b0fSOlivier Houchard/*
63615144b0fSOlivier Houchard-------------------------------------------------------------------------------
63715144b0fSOlivier HouchardReturns 1 if the 64-bit value formed by concatenating `a0' and `a1' is not
63815144b0fSOlivier Houchardequal to the 64-bit value formed by concatenating `b0' and `b1'.  Otherwise,
63915144b0fSOlivier Houchardreturns 0.
64015144b0fSOlivier Houchard-------------------------------------------------------------------------------
64115144b0fSOlivier Houchard*/
64215144b0fSOlivier HouchardINLINE flag ne64( bits32 a0, bits32 a1, bits32 b0, bits32 b1 )
64315144b0fSOlivier Houchard{
64415144b0fSOlivier Houchard
64515144b0fSOlivier Houchard    return ( a0 != b0 ) || ( a1 != b1 );
64615144b0fSOlivier Houchard
64715144b0fSOlivier Houchard}
64815144b0fSOlivier Houchard
649