1
2/*============================================================================
3
4This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
5Package, Release 3e, by John R. Hauser.
6
7Copyright 2011, 2012, 2013, 2014 The Regents of the University of California.
8All rights reserved.
9
10Redistribution and use in source and binary forms, with or without
11modification, are permitted provided that the following conditions are met:
12
13 1. Redistributions of source code must retain the above copyright notice,
14    this list of conditions, and the following disclaimer.
15
16 2. Redistributions in binary form must reproduce the above copyright notice,
17    this list of conditions, and the following disclaimer in the documentation
18    and/or other materials provided with the distribution.
19
20 3. Neither the name of the University nor the names of its contributors may
21    be used to endorse or promote products derived from this software without
22    specific prior written permission.
23
24THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
25EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
27DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
28DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35=============================================================================*/
36
37#include <stdbool.h>
38#include <stdint.h>
39#include "platform.h"
40#include "internals.h"
41#include "specialize.h"
42#include "softfloat.h"
43
44#ifdef SOFTFLOAT_FAST_INT64
45
46void
47 f128M_div( const float128_t *aPtr, const float128_t *bPtr, float128_t *zPtr )
48{
49
50    *zPtr = f128_div( *aPtr, *bPtr );
51
52}
53
54#else
55
56void
57 f128M_div( const float128_t *aPtr, const float128_t *bPtr, float128_t *zPtr )
58{
59    const uint32_t *aWPtr, *bWPtr;
60    uint32_t *zWPtr, uiA96;
61    bool signA;
62    int32_t expA;
63    uint32_t uiB96;
64    bool signB;
65    int32_t expB;
66    bool signZ;
67    uint32_t y[5], sigB[4];
68    int32_t expZ;
69    uint32_t recip32;
70    int ix;
71    uint64_t q64;
72    uint32_t q, qs[3], uiZ96;
73
74    /*------------------------------------------------------------------------
75    *------------------------------------------------------------------------*/
76    aWPtr = (const uint32_t *) aPtr;
77    bWPtr = (const uint32_t *) bPtr;
78    zWPtr = (uint32_t *) zPtr;
79    /*------------------------------------------------------------------------
80    *------------------------------------------------------------------------*/
81    uiA96 = aWPtr[indexWordHi( 4 )];
82    signA = signF128UI96( uiA96 );
83    expA  = expF128UI96( uiA96 );
84    uiB96 = bWPtr[indexWordHi( 4 )];
85    signB = signF128UI96( uiB96 );
86    expB  = expF128UI96( uiB96 );
87    signZ = signA ^ signB;
88    /*------------------------------------------------------------------------
89    *------------------------------------------------------------------------*/
90    if ( (expA == 0x7FFF) || (expB == 0x7FFF) ) {
91        if ( softfloat_tryPropagateNaNF128M( aWPtr, bWPtr, zWPtr ) ) return;
92        if ( expA == 0x7FFF ) {
93            if ( expB == 0x7FFF ) goto invalid;
94            goto infinity;
95        }
96        goto zero;
97    }
98    /*------------------------------------------------------------------------
99    *------------------------------------------------------------------------*/
100    expA = softfloat_shiftNormSigF128M( aWPtr, 13, y );
101    expB = softfloat_shiftNormSigF128M( bWPtr, 13, sigB );
102    if ( expA == -128 ) {
103        if ( expB == -128 ) goto invalid;
104        goto zero;
105    }
106    if ( expB == -128 ) {
107        softfloat_raiseFlags( softfloat_flag_infinite );
108        goto infinity;
109    }
110    /*------------------------------------------------------------------------
111    *------------------------------------------------------------------------*/
112    expZ = expA - expB + 0x3FFE;
113    if ( softfloat_compare128M( y, sigB ) < 0 ) {
114        --expZ;
115        softfloat_add128M( y, y, y );
116    }
117    recip32 =
118        softfloat_approxRecip32_1(
119            ((uint64_t) sigB[indexWord( 4, 3 )]<<32 | sigB[indexWord( 4, 2 )])
120                >>30
121        );
122    ix = 3;
123    for (;;) {
124        q64 = (uint64_t) y[indexWordHi( 4 )] * recip32;
125        q = (q64 + 0x80000000)>>32;
126        --ix;
127        if ( ix < 0 ) break;
128        softfloat_remStep128MBy32( y, 29, sigB, q, y );
129        if ( y[indexWordHi( 4 )] & 0x80000000 ) {
130            --q;
131            softfloat_add128M( y, sigB, y );
132        }
133        qs[ix] = q;
134    }
135    /*------------------------------------------------------------------------
136    *------------------------------------------------------------------------*/
137    if ( ((q + 1) & 7) < 2 ) {
138        softfloat_remStep128MBy32( y, 29, sigB, q, y );
139        if ( y[indexWordHi( 4 )] & 0x80000000 ) {
140            --q;
141            softfloat_add128M( y, sigB, y );
142        } else if ( softfloat_compare128M( sigB, y ) <= 0 ) {
143            ++q;
144            softfloat_sub128M( y, sigB, y );
145        }
146        if (
147            y[indexWordLo( 4 )] || y[indexWord( 4, 1 )]
148                || (y[indexWord( 4, 2 )] | y[indexWord( 4, 3 )])
149        ) {
150            q |= 1;
151        }
152    }
153    /*------------------------------------------------------------------------
154    *------------------------------------------------------------------------*/
155    q64 = (uint64_t) q<<28;
156    y[indexWord( 5, 0 )] = q64;
157    q64 = ((uint64_t) qs[0]<<25) + (q64>>32);
158    y[indexWord( 5, 1 )] = q64;
159    q64 = ((uint64_t) qs[1]<<22) + (q64>>32);
160    y[indexWord( 5, 2 )] = q64;
161    q64 = ((uint64_t) qs[2]<<19) + (q64>>32);
162    y[indexWord( 5, 3 )] = q64;
163    y[indexWord( 5, 4 )] = q64>>32;
164    softfloat_roundPackMToF128M( signZ, expZ, y, zWPtr );
165    return;
166    /*------------------------------------------------------------------------
167    *------------------------------------------------------------------------*/
168 invalid:
169    softfloat_invalidF128M( zWPtr );
170    return;
171    /*------------------------------------------------------------------------
172    *------------------------------------------------------------------------*/
173 infinity:
174    uiZ96 = packToF128UI96( signZ, 0x7FFF, 0 );
175    goto uiZ96;
176 zero:
177    uiZ96 = packToF128UI96( signZ, 0, 0 );
178 uiZ96:
179    zWPtr[indexWordHi( 4 )] = uiZ96;
180    zWPtr[indexWord( 4, 2 )] = 0;
181    zWPtr[indexWord( 4, 1 )] = 0;
182    zWPtr[indexWord( 4, 0 )] = 0;
183
184}
185
186#endif
187
188