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
f32_rem(float32_t a,float32_t b)45 float32_t f32_rem( float32_t a, float32_t b )
46 {
47 union ui32_f32 uA;
48 uint_fast32_t uiA;
49 bool signA;
50 int_fast16_t expA;
51 uint_fast32_t sigA;
52 union ui32_f32 uB;
53 uint_fast32_t uiB;
54 int_fast16_t expB;
55 uint_fast32_t sigB;
56 struct exp16_sig32 normExpSig;
57 uint32_t rem;
58 int_fast16_t expDiff;
59 uint32_t q, recip32, altRem, meanRem;
60 bool signRem;
61 uint_fast32_t uiZ;
62 union ui32_f32 uZ;
63
64 /*------------------------------------------------------------------------
65 *------------------------------------------------------------------------*/
66 uA.f = a;
67 uiA = uA.ui;
68 signA = signF32UI( uiA );
69 expA = expF32UI( uiA );
70 sigA = fracF32UI( uiA );
71 uB.f = b;
72 uiB = uB.ui;
73 expB = expF32UI( uiB );
74 sigB = fracF32UI( uiB );
75 /*------------------------------------------------------------------------
76 *------------------------------------------------------------------------*/
77 if ( expA == 0xFF ) {
78 if ( sigA || ((expB == 0xFF) && sigB) ) goto propagateNaN;
79 goto invalid;
80 }
81 if ( expB == 0xFF ) {
82 if ( sigB ) goto propagateNaN;
83 return a;
84 }
85 /*------------------------------------------------------------------------
86 *------------------------------------------------------------------------*/
87 if ( ! expB ) {
88 if ( ! sigB ) goto invalid;
89 normExpSig = softfloat_normSubnormalF32Sig( sigB );
90 expB = normExpSig.exp;
91 sigB = normExpSig.sig;
92 }
93 if ( ! expA ) {
94 if ( ! sigA ) return a;
95 normExpSig = softfloat_normSubnormalF32Sig( sigA );
96 expA = normExpSig.exp;
97 sigA = normExpSig.sig;
98 }
99 /*------------------------------------------------------------------------
100 *------------------------------------------------------------------------*/
101 rem = sigA | 0x00800000;
102 sigB |= 0x00800000;
103 expDiff = expA - expB;
104 if ( expDiff < 1 ) {
105 if ( expDiff < -1 ) return a;
106 sigB <<= 6;
107 if ( expDiff ) {
108 rem <<= 5;
109 q = 0;
110 } else {
111 rem <<= 6;
112 q = (sigB <= rem);
113 if ( q ) rem -= sigB;
114 }
115 } else {
116 recip32 = softfloat_approxRecip32_1( sigB<<8 );
117 /*--------------------------------------------------------------------
118 | Changing the shift of `rem' here requires also changing the initial
119 | subtraction from `expDiff'.
120 *--------------------------------------------------------------------*/
121 rem <<= 7;
122 expDiff -= 31;
123 /*--------------------------------------------------------------------
124 | The scale of `sigB' affects how many bits are obtained during each
125 | cycle of the loop. Currently this is 29 bits per loop iteration,
126 | which is believed to be the maximum possible.
127 *--------------------------------------------------------------------*/
128 sigB <<= 6;
129 for (;;) {
130 q = (rem * (uint_fast64_t) recip32)>>32;
131 if ( expDiff < 0 ) break;
132 rem = -(q * (uint32_t) sigB);
133 expDiff -= 29;
134 }
135 /*--------------------------------------------------------------------
136 | (`expDiff' cannot be less than -30 here.)
137 *--------------------------------------------------------------------*/
138 q >>= ~expDiff & 31;
139 rem = (rem<<(expDiff + 30)) - q * (uint32_t) sigB;
140 }
141 /*------------------------------------------------------------------------
142 *------------------------------------------------------------------------*/
143 do {
144 altRem = rem;
145 ++q;
146 rem -= sigB;
147 } while ( ! (rem & 0x80000000) );
148 meanRem = rem + altRem;
149 if ( (meanRem & 0x80000000) || (! meanRem && (q & 1)) ) rem = altRem;
150 signRem = signA;
151 if ( 0x80000000 <= rem ) {
152 signRem = ! signRem;
153 rem = -rem;
154 }
155 return softfloat_normRoundPackToF32( signRem, expB, rem );
156 /*------------------------------------------------------------------------
157 *------------------------------------------------------------------------*/
158 propagateNaN:
159 uiZ = softfloat_propagateNaNF32UI( uiA, uiB );
160 goto uiZ;
161 invalid:
162 softfloat_raiseFlags( softfloat_flag_invalid );
163 uiZ = defaultNaNF32UI;
164 uiZ:
165 uZ.ui = uiZ;
166 return uZ.f;
167
168 }
169
170