1 // SPDX-License-Identifier: BSD-3-Clause
2 
3 /*============================================================================
4 
5 This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
6 Package, Release 3a, by John R. Hauser.
7 
8 Copyright 2011, 2012, 2013, 2014 The Regents of the University of California.
9 All rights reserved.
10 
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions are met:
13 
14  1. Redistributions of source code must retain the above copyright notice,
15     this list of conditions, and the following disclaimer.
16 
17  2. Redistributions in binary form must reproduce the above copyright notice,
18     this list of conditions, and the following disclaimer in the documentation
19     and/or other materials provided with the distribution.
20 
21  3. Neither the name of the University nor the names of its contributors may
22     be used to endorse or promote products derived from this software without
23     specific prior written permission.
24 
25 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
26 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
28 DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
29 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 
36 =============================================================================*/
37 
38 #include <stdbool.h>
39 #include <stdint.h>
40 #include "platform.h"
41 #include "internals.h"
42 #include "specialize.h"
43 #include "softfloat.h"
44 
extF80_div(extFloat80_t a,extFloat80_t b)45 extFloat80_t extF80_div( extFloat80_t a, extFloat80_t b )
46 {
47     union { struct extFloat80M s; extFloat80_t f; } uA;
48     uint_fast16_t uiA64;
49     uint_fast64_t uiA0;
50     bool signA;
51     int_fast32_t expA;
52     uint_fast64_t sigA;
53     union { struct extFloat80M s; extFloat80_t f; } uB;
54     uint_fast16_t uiB64;
55     uint_fast64_t uiB0;
56     bool signB;
57     int_fast32_t expB;
58     uint_fast64_t sigB;
59     bool signZ;
60     struct exp32_sig64 normExpSig;
61     int_fast32_t expZ;
62     struct uint128 rem;
63     uint_fast32_t recip32;
64     uint_fast64_t sigZ;
65     int ix;
66     uint_fast64_t q64;
67     uint_fast32_t q;
68     struct uint128 term;
69     uint_fast64_t sigZExtra;
70     struct uint128 uiZ;
71     uint_fast16_t uiZ64;
72     uint_fast64_t uiZ0;
73     union { struct extFloat80M s; extFloat80_t f; } uZ;
74 
75     /*------------------------------------------------------------------------
76     *------------------------------------------------------------------------*/
77     uA.f = a;
78     uiA64 = uA.s.signExp;
79     uiA0  = uA.s.signif;
80     signA = signExtF80UI64( uiA64 );
81     expA  = expExtF80UI64( uiA64 );
82     sigA  = uiA0;
83     uB.f = b;
84     uiB64 = uB.s.signExp;
85     uiB0  = uB.s.signif;
86     signB = signExtF80UI64( uiB64 );
87     expB  = expExtF80UI64( uiB64 );
88     sigB  = uiB0;
89     signZ = signA ^ signB;
90     /*------------------------------------------------------------------------
91     *------------------------------------------------------------------------*/
92     if ( expA == 0x7FFF ) {
93         if ( sigA & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) goto propagateNaN;
94         if ( expB == 0x7FFF ) {
95             if ( sigB & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) goto propagateNaN;
96             goto invalid;
97         }
98         goto infinity;
99     }
100     if ( expB == 0x7FFF ) {
101         if ( sigB & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) goto propagateNaN;
102         goto zero;
103     }
104     /*------------------------------------------------------------------------
105     *------------------------------------------------------------------------*/
106     if ( ! expB ) expB = 1;
107     if ( ! (sigB & UINT64_C( 0x8000000000000000 )) ) {
108         if ( ! sigB ) {
109             if ( ! sigA ) goto invalid;
110             softfloat_raiseFlags( softfloat_flag_infinite );
111             goto infinity;
112         }
113         normExpSig = softfloat_normSubnormalExtF80Sig( sigB );
114         expB += normExpSig.exp;
115         sigB = normExpSig.sig;
116     }
117     if ( ! expA ) expA = 1;
118     if ( ! (sigA & UINT64_C( 0x8000000000000000 )) ) {
119         if ( ! sigA ) goto zero;
120         normExpSig = softfloat_normSubnormalExtF80Sig( sigA );
121         expA += normExpSig.exp;
122         sigA = normExpSig.sig;
123     }
124     /*------------------------------------------------------------------------
125     *------------------------------------------------------------------------*/
126     expZ = expA - expB + 0x3FFF;
127     if ( sigA < sigB ) {
128         --expZ;
129         rem = softfloat_shortShiftLeft128( 0, sigA, 32 );
130     } else {
131         rem = softfloat_shortShiftLeft128( 0, sigA, 31 );
132     }
133     recip32 = softfloat_approxRecip32_1( sigB>>32 );
134     sigZ = 0;
135     ix = 2;
136     for (;;) {
137         q64 = (uint_fast64_t) (uint32_t) (rem.v64>>2) * recip32;
138         q = (q64 + 0x80000000)>>32;
139         --ix;
140         if ( ix < 0 ) break;
141         rem = softfloat_shortShiftLeft128( rem.v64, rem.v0, 29 );
142         term = softfloat_mul64ByShifted32To128( sigB, q );
143         rem = softfloat_sub128( rem.v64, rem.v0, term.v64, term.v0 );
144         if ( rem.v64 & UINT64_C( 0x8000000000000000 ) ) {
145             --q;
146             rem = softfloat_add128( rem.v64, rem.v0, sigB>>32, sigB<<32 );
147         }
148         sigZ = (sigZ<<29) + q;
149     }
150     /*------------------------------------------------------------------------
151     *------------------------------------------------------------------------*/
152     if ( ((q + 1) & 0x3FFFFF) < 2 ) {
153         rem = softfloat_shortShiftLeft128( rem.v64, rem.v0, 29 );
154         term = softfloat_mul64ByShifted32To128( sigB, q );
155         rem = softfloat_sub128( rem.v64, rem.v0, term.v64, term.v0 );
156         term = softfloat_shortShiftLeft128( 0, sigB, 32 );
157         if ( rem.v64 & UINT64_C( 0x8000000000000000 ) ) {
158             --q;
159             rem = softfloat_add128( rem.v64, rem.v0, term.v64, term.v0 );
160         } else if ( softfloat_le128( term.v64, term.v0, rem.v64, rem.v0 ) ) {
161             ++q;
162             rem = softfloat_sub128( rem.v64, rem.v0, term.v64, term.v0 );
163         }
164         if ( rem.v64 | rem.v0 ) q |= 1;
165     }
166     /*------------------------------------------------------------------------
167     *------------------------------------------------------------------------*/
168     sigZ = (sigZ<<6) + (q>>23);
169     sigZExtra = (uint64_t) ((uint_fast64_t) q<<41);
170     return
171         softfloat_roundPackToExtF80(
172             signZ, expZ, sigZ, sigZExtra, extF80_roundingPrecision );
173     /*------------------------------------------------------------------------
174     *------------------------------------------------------------------------*/
175  propagateNaN:
176     uiZ = softfloat_propagateNaNExtF80UI( uiA64, uiA0, uiB64, uiB0 );
177     uiZ64 = uiZ.v64;
178     uiZ0  = uiZ.v0;
179     goto uiZ;
180     /*------------------------------------------------------------------------
181     *------------------------------------------------------------------------*/
182  invalid:
183     softfloat_raiseFlags( softfloat_flag_invalid );
184     uiZ64 = defaultNaNExtF80UI64;
185     uiZ0  = defaultNaNExtF80UI0;
186     goto uiZ;
187     /*------------------------------------------------------------------------
188     *------------------------------------------------------------------------*/
189  infinity:
190     uiZ64 = packToExtF80UI64( signZ, 0x7FFF );
191     uiZ0  = UINT64_C( 0x8000000000000000 );
192     goto uiZ;
193     /*------------------------------------------------------------------------
194     *------------------------------------------------------------------------*/
195  zero:
196     uiZ64 = packToExtF80UI64( signZ, 0 );
197     uiZ0  = 0;
198  uiZ:
199     uZ.s.signExp = uiZ64;
200     uZ.s.signif  = uiZ0;
201     return uZ.f;
202 
203 }
204 
205