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_rem(float64_t a,float64_t b)45 float64_t f64_rem( 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 int_fast16_t expB;
55 uint_fast64_t sigB;
56 struct exp16_sig64 normExpSig;
57 uint64_t rem;
58 int_fast16_t expDiff;
59 uint32_t q, recip32;
60 uint_fast64_t q64;
61 uint64_t altRem, meanRem;
62 bool signRem;
63 uint_fast64_t uiZ;
64 union ui64_f64 uZ;
65
66 /*------------------------------------------------------------------------
67 *------------------------------------------------------------------------*/
68 uA.f = a;
69 uiA = uA.ui;
70 signA = signF64UI( uiA );
71 expA = expF64UI( uiA );
72 sigA = fracF64UI( uiA );
73 uB.f = b;
74 uiB = uB.ui;
75 expB = expF64UI( uiB );
76 sigB = fracF64UI( uiB );
77 /*------------------------------------------------------------------------
78 *------------------------------------------------------------------------*/
79 if ( expA == 0x7FF ) {
80 if ( sigA || ((expB == 0x7FF) && sigB) ) goto propagateNaN;
81 goto invalid;
82 }
83 if ( expB == 0x7FF ) {
84 if ( sigB ) goto propagateNaN;
85 return a;
86 }
87 /*------------------------------------------------------------------------
88 *------------------------------------------------------------------------*/
89 if ( expA < expB - 1 ) return a;
90 /*------------------------------------------------------------------------
91 *------------------------------------------------------------------------*/
92 if ( ! expB ) {
93 if ( ! sigB ) goto invalid;
94 normExpSig = softfloat_normSubnormalF64Sig( sigB );
95 expB = normExpSig.exp;
96 sigB = normExpSig.sig;
97 }
98 if ( ! expA ) {
99 if ( ! sigA ) return a;
100 normExpSig = softfloat_normSubnormalF64Sig( sigA );
101 expA = normExpSig.exp;
102 sigA = normExpSig.sig;
103 }
104 /*------------------------------------------------------------------------
105 *------------------------------------------------------------------------*/
106 rem = sigA | UINT64_C( 0x0010000000000000 );
107 sigB |= UINT64_C( 0x0010000000000000 );
108 expDiff = expA - expB;
109 if ( expDiff < 1 ) {
110 if ( expDiff < -1 ) return a;
111 sigB <<= 9;
112 if ( expDiff ) {
113 rem <<= 8;
114 q = 0;
115 } else {
116 rem <<= 9;
117 q = (sigB <= rem);
118 if ( q ) rem -= sigB;
119 }
120 } else {
121 recip32 = softfloat_approxRecip32_1( sigB>>21 );
122 /*--------------------------------------------------------------------
123 | Changing the shift of `rem' here requires also changing the initial
124 | subtraction from `expDiff'.
125 *--------------------------------------------------------------------*/
126 rem <<= 9;
127 expDiff -= 30;
128 /*--------------------------------------------------------------------
129 | The scale of `sigB' affects how many bits are obtained during each
130 | cycle of the loop. Currently this is 29 bits per loop iteration,
131 | the maximum possible.
132 *--------------------------------------------------------------------*/
133 sigB <<= 9;
134 for (;;) {
135 q64 = (uint32_t) (rem>>32) * (uint_fast64_t) recip32;
136 if ( expDiff < 0 ) break;
137 q = (q64 + 0x80000000)>>32;
138 #ifdef SOFTFLOAT_FAST_INT64
139 rem <<= 29;
140 #else
141 rem = (uint_fast64_t) (uint32_t) (rem>>3)<<32;
142 #endif
143 rem -= q * (uint64_t) sigB;
144 if ( rem & UINT64_C( 0x8000000000000000 ) ) rem += sigB;
145 expDiff -= 29;
146 }
147 /*--------------------------------------------------------------------
148 | (`expDiff' cannot be less than -29 here.)
149 *--------------------------------------------------------------------*/
150 q = (uint32_t) (q64>>32)>>(~expDiff & 31);
151 rem = (rem<<(expDiff + 30)) - q * (uint64_t) sigB;
152 if ( rem & UINT64_C( 0x8000000000000000 ) ) {
153 altRem = rem + sigB;
154 goto selectRem;
155 }
156 }
157 /*------------------------------------------------------------------------
158 *------------------------------------------------------------------------*/
159 do {
160 altRem = rem;
161 ++q;
162 rem -= sigB;
163 } while ( ! (rem & UINT64_C( 0x8000000000000000 )) );
164 selectRem:
165 meanRem = rem + altRem;
166 if (
167 (meanRem & UINT64_C( 0x8000000000000000 )) || (! meanRem && (q & 1))
168 ) {
169 rem = altRem;
170 }
171 signRem = signA;
172 if ( rem & UINT64_C( 0x8000000000000000 ) ) {
173 signRem = ! signRem;
174 rem = -rem;
175 }
176 return softfloat_normRoundPackToF64( signRem, expB, rem );
177 /*------------------------------------------------------------------------
178 *------------------------------------------------------------------------*/
179 propagateNaN:
180 uiZ = softfloat_propagateNaNF64UI( uiA, uiB );
181 goto uiZ;
182 invalid:
183 softfloat_raiseFlags( softfloat_flag_invalid );
184 uiZ = defaultNaNF64UI;
185 uiZ:
186 uZ.ui = uiZ;
187 return uZ.f;
188
189 }
190
191