1 
2 /*============================================================================
3 
4 This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
5 Package, Release 3e, by John R. Hauser.
6 
7 Copyright 2011, 2012, 2013, 2014 The Regents of the University of California.
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, 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 
24 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
27 DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 ON 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
33 SOFTWARE, 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 
extF80_div(extFloat80_t a,extFloat80_t b)44 extFloat80_t extF80_div( extFloat80_t a, extFloat80_t b )
45 {
46     union { struct extFloat80M s; extFloat80_t f; } uA;
47     uint_fast16_t uiA64;
48     uint_fast64_t uiA0;
49     bool signA;
50     int_fast32_t expA;
51     uint_fast64_t sigA;
52     union { struct extFloat80M s; extFloat80_t f; } uB;
53     uint_fast16_t uiB64;
54     uint_fast64_t uiB0;
55     bool signB;
56     int_fast32_t expB;
57     uint_fast64_t sigB;
58     bool signZ;
59     struct exp32_sig64 normExpSig;
60     int_fast32_t expZ;
61     struct uint128 rem;
62     uint_fast32_t recip32;
63     uint_fast64_t sigZ;
64     int ix;
65     uint_fast64_t q64;
66     uint_fast32_t q;
67     struct uint128 term;
68     uint_fast64_t sigZExtra;
69     struct uint128 uiZ;
70     uint_fast16_t uiZ64;
71     uint_fast64_t uiZ0;
72     union { struct extFloat80M s; extFloat80_t f; } uZ;
73 
74     /*------------------------------------------------------------------------
75     *------------------------------------------------------------------------*/
76     uA.f = a;
77     uiA64 = uA.s.signExp;
78     uiA0  = uA.s.signif;
79     signA = signExtF80UI64( uiA64 );
80     expA  = expExtF80UI64( uiA64 );
81     sigA  = uiA0;
82     uB.f = b;
83     uiB64 = uB.s.signExp;
84     uiB0  = uB.s.signif;
85     signB = signExtF80UI64( uiB64 );
86     expB  = expExtF80UI64( uiB64 );
87     sigB  = uiB0;
88     signZ = signA ^ signB;
89     /*------------------------------------------------------------------------
90     *------------------------------------------------------------------------*/
91     if ( expA == 0x7FFF ) {
92         if ( sigA & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) goto propagateNaN;
93         if ( expB == 0x7FFF ) {
94             if ( sigB & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) goto propagateNaN;
95             goto invalid;
96         }
97         goto infinity;
98     }
99     if ( expB == 0x7FFF ) {
100         if ( sigB & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) goto propagateNaN;
101         goto zero;
102     }
103     /*------------------------------------------------------------------------
104     *------------------------------------------------------------------------*/
105     if ( ! expB ) expB = 1;
106     if ( ! (sigB & UINT64_C( 0x8000000000000000 )) ) {
107         if ( ! sigB ) {
108             if ( ! sigA ) goto invalid;
109             softfloat_raiseFlags( softfloat_flag_infinite );
110             goto infinity;
111         }
112         normExpSig = softfloat_normSubnormalExtF80Sig( sigB );
113         expB += normExpSig.exp;
114         sigB = normExpSig.sig;
115     }
116     if ( ! expA ) expA = 1;
117     if ( ! (sigA & UINT64_C( 0x8000000000000000 )) ) {
118         if ( ! sigA ) goto zero;
119         normExpSig = softfloat_normSubnormalExtF80Sig( sigA );
120         expA += normExpSig.exp;
121         sigA = normExpSig.sig;
122     }
123     /*------------------------------------------------------------------------
124     *------------------------------------------------------------------------*/
125     expZ = expA - expB + 0x3FFF;
126     if ( sigA < sigB ) {
127         --expZ;
128         rem = softfloat_shortShiftLeft128( 0, sigA, 32 );
129     } else {
130         rem = softfloat_shortShiftLeft128( 0, sigA, 31 );
131     }
132     recip32 = softfloat_approxRecip32_1( sigB>>32 );
133     sigZ = 0;
134     ix = 2;
135     for (;;) {
136         q64 = (uint_fast64_t) (uint32_t) (rem.v64>>2) * recip32;
137         q = (q64 + 0x80000000)>>32;
138         --ix;
139         if ( ix < 0 ) break;
140         rem = softfloat_shortShiftLeft128( rem.v64, rem.v0, 29 );
141         term = softfloat_mul64ByShifted32To128( sigB, q );
142         rem = softfloat_sub128( rem.v64, rem.v0, term.v64, term.v0 );
143         if ( rem.v64 & UINT64_C( 0x8000000000000000 ) ) {
144             --q;
145             rem = softfloat_add128( rem.v64, rem.v0, sigB>>32, sigB<<32 );
146         }
147         sigZ = (sigZ<<29) + q;
148     }
149     /*------------------------------------------------------------------------
150     *------------------------------------------------------------------------*/
151     if ( ((q + 1) & 0x3FFFFF) < 2 ) {
152         rem = softfloat_shortShiftLeft128( rem.v64, rem.v0, 29 );
153         term = softfloat_mul64ByShifted32To128( sigB, q );
154         rem = softfloat_sub128( rem.v64, rem.v0, term.v64, term.v0 );
155         term = softfloat_shortShiftLeft128( 0, sigB, 32 );
156         if ( rem.v64 & UINT64_C( 0x8000000000000000 ) ) {
157             --q;
158             rem = softfloat_add128( rem.v64, rem.v0, term.v64, term.v0 );
159         } else if ( softfloat_le128( term.v64, term.v0, rem.v64, rem.v0 ) ) {
160             ++q;
161             rem = softfloat_sub128( rem.v64, rem.v0, term.v64, term.v0 );
162         }
163         if ( rem.v64 | rem.v0 ) q |= 1;
164     }
165     /*------------------------------------------------------------------------
166     *------------------------------------------------------------------------*/
167     sigZ = (sigZ<<6) + (q>>23);
168     sigZExtra = (uint64_t) ((uint_fast64_t) q<<41);
169     return
170         softfloat_roundPackToExtF80(
171             signZ, expZ, sigZ, sigZExtra, extF80_roundingPrecision );
172     /*------------------------------------------------------------------------
173     *------------------------------------------------------------------------*/
174  propagateNaN:
175     uiZ = softfloat_propagateNaNExtF80UI( uiA64, uiA0, uiB64, uiB0 );
176     uiZ64 = uiZ.v64;
177     uiZ0  = uiZ.v0;
178     goto uiZ;
179     /*------------------------------------------------------------------------
180     *------------------------------------------------------------------------*/
181  invalid:
182     softfloat_raiseFlags( softfloat_flag_invalid );
183     uiZ64 = defaultNaNExtF80UI64;
184     uiZ0  = defaultNaNExtF80UI0;
185     goto uiZ;
186     /*------------------------------------------------------------------------
187     *------------------------------------------------------------------------*/
188  infinity:
189     uiZ64 = packToExtF80UI64( signZ, 0x7FFF );
190     uiZ0  = UINT64_C( 0x8000000000000000 );
191     goto uiZ;
192     /*------------------------------------------------------------------------
193     *------------------------------------------------------------------------*/
194  zero:
195     uiZ64 = packToExtF80UI64( signZ, 0 );
196     uiZ0  = 0;
197  uiZ:
198     uZ.s.signExp = uiZ64;
199     uZ.s.signif  = uiZ0;
200     return uZ.f;
201 
202 }
203 
204