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 
f64_div(float64_t a,float64_t b)45 float64_t f64_div( float64_t a, float64_t b )
46 {
47     union ui64_f64 uA;
48     uint_fast64_t uiA;
49     bool signA;
50     int_fast16_t expA;
51     uint_fast64_t sigA;
52     union ui64_f64 uB;
53     uint_fast64_t uiB;
54     bool signB;
55     int_fast16_t expB;
56     uint_fast64_t sigB;
57     bool signZ;
58     struct exp16_sig64 normExpSig;
59     int_fast16_t expZ;
60     uint32_t recip32, sig32Z, doubleTerm;
61     uint_fast64_t rem;
62     uint32_t q;
63     uint_fast64_t sigZ;
64     uint_fast64_t uiZ;
65     union ui64_f64 uZ;
66 
67     /*------------------------------------------------------------------------
68     *------------------------------------------------------------------------*/
69     uA.f = a;
70     uiA = uA.ui;
71     signA = signF64UI( uiA );
72     expA  = expF64UI( uiA );
73     sigA  = fracF64UI( uiA );
74     uB.f = b;
75     uiB = uB.ui;
76     signB = signF64UI( uiB );
77     expB  = expF64UI( uiB );
78     sigB  = fracF64UI( uiB );
79     signZ = signA ^ signB;
80     /*------------------------------------------------------------------------
81     *------------------------------------------------------------------------*/
82     if ( expA == 0x7FF ) {
83         if ( sigA ) goto propagateNaN;
84         if ( expB == 0x7FF ) {
85             if ( sigB ) goto propagateNaN;
86             goto invalid;
87         }
88         goto infinity;
89     }
90     if ( expB == 0x7FF ) {
91         if ( sigB ) goto propagateNaN;
92         goto zero;
93     }
94     /*------------------------------------------------------------------------
95     *------------------------------------------------------------------------*/
96     if ( ! expB ) {
97         if ( ! sigB ) {
98             if ( ! (expA | sigA) ) goto invalid;
99             softfloat_raiseFlags( softfloat_flag_infinite );
100             goto infinity;
101         }
102         normExpSig = softfloat_normSubnormalF64Sig( sigB );
103         expB = normExpSig.exp;
104         sigB = normExpSig.sig;
105     }
106     if ( ! expA ) {
107         if ( ! sigA ) goto zero;
108         normExpSig = softfloat_normSubnormalF64Sig( sigA );
109         expA = normExpSig.exp;
110         sigA = normExpSig.sig;
111     }
112     /*------------------------------------------------------------------------
113     *------------------------------------------------------------------------*/
114     expZ = expA - expB + 0x3FE;
115     sigA |= UINT64_C( 0x0010000000000000 );
116     sigB |= UINT64_C( 0x0010000000000000 );
117     if ( sigA < sigB ) {
118         --expZ;
119         sigA <<= 11;
120     } else {
121         sigA <<= 10;
122     }
123     sigB <<= 11;
124     recip32 = softfloat_approxRecip32_1( sigB>>32 ) - 2;
125     sig32Z = ((uint32_t) (sigA>>32) * (uint_fast64_t) recip32)>>32;
126     doubleTerm = sig32Z<<1;
127     rem =
128         ((sigA - (uint_fast64_t) doubleTerm * (uint32_t) (sigB>>32))<<28)
129             - (uint_fast64_t) doubleTerm * ((uint32_t) sigB>>4);
130     q = (((uint32_t) (rem>>32) * (uint_fast64_t) recip32)>>32) + 4;
131     sigZ = ((uint_fast64_t) sig32Z<<32) + ((uint_fast64_t) q<<4);
132     /*------------------------------------------------------------------------
133     *------------------------------------------------------------------------*/
134     if ( (sigZ & 0x1FF) < 4<<4 ) {
135         q &= ~7;
136         sigZ &= ~(uint_fast64_t) 0x7F;
137         doubleTerm = q<<1;
138         rem =
139             ((rem - (uint_fast64_t) doubleTerm * (uint32_t) (sigB>>32))<<28)
140                 - (uint_fast64_t) doubleTerm * ((uint32_t) sigB>>4);
141         if ( rem & UINT64_C( 0x8000000000000000 ) ) {
142             sigZ -= 1<<7;
143         } else {
144             if ( rem ) sigZ |= 1;
145         }
146     }
147     return softfloat_roundPackToF64( signZ, expZ, sigZ );
148     /*------------------------------------------------------------------------
149     *------------------------------------------------------------------------*/
150  propagateNaN:
151     uiZ = softfloat_propagateNaNF64UI( uiA, uiB );
152     goto uiZ;
153     /*------------------------------------------------------------------------
154     *------------------------------------------------------------------------*/
155  invalid:
156     softfloat_raiseFlags( softfloat_flag_invalid );
157     uiZ = defaultNaNF64UI;
158     goto uiZ;
159     /*------------------------------------------------------------------------
160     *------------------------------------------------------------------------*/
161  infinity:
162     uiZ = packToF64UI( signZ, 0x7FF, 0 );
163     goto uiZ;
164     /*------------------------------------------------------------------------
165     *------------------------------------------------------------------------*/
166  zero:
167     uiZ = packToF64UI( signZ, 0, 0 );
168  uiZ:
169     uZ.ui = uiZ;
170     return uZ.f;
171 
172 }
173 
174