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 
f128_rem(float128_t a,float128_t b)45 float128_t f128_rem( float128_t a, float128_t b )
46 {
47     union ui128_f128 uA;
48     uint_fast64_t uiA64, uiA0;
49     bool signA;
50     int_fast32_t expA;
51     struct uint128 sigA;
52     union ui128_f128 uB;
53     uint_fast64_t uiB64, uiB0;
54     bool signB;
55     int_fast32_t expB;
56     struct uint128 sigB;
57     struct exp32_sig128 normExpSig;
58     struct uint128 rem;
59     int_fast32_t expDiff;
60     uint_fast32_t q, recip32;
61     uint_fast64_t q64;
62     struct uint128 term, altRem, meanRem;
63     bool signRem;
64     struct uint128 uiZ;
65     union ui128_f128 uZ;
66 
67     /*------------------------------------------------------------------------
68     *------------------------------------------------------------------------*/
69     uA.f = a;
70     uiA64 = uA.ui.v64;
71     uiA0  = uA.ui.v0;
72     signA = signF128UI64( uiA64 );
73     expA  = expF128UI64( uiA64 );
74     sigA.v64 = fracF128UI64( uiA64 );
75     sigA.v0  = uiA0;
76     uB.f = b;
77     uiB64 = uB.ui.v64;
78     uiB0  = uB.ui.v0;
79     signB = signF128UI64( uiB64 );
80     expB  = expF128UI64( uiB64 );
81     sigB.v64 = fracF128UI64( uiB64 );
82     sigB.v0  = uiB0;
83     /*------------------------------------------------------------------------
84     *------------------------------------------------------------------------*/
85     if ( expA == 0x7FFF ) {
86         if (
87             (sigA.v64 | sigA.v0) || ((expB == 0x7FFF) && (sigB.v64 | sigB.v0))
88         ) {
89             goto propagateNaN;
90         }
91         goto invalid;
92     }
93     if ( expB == 0x7FFF ) {
94         if ( sigB.v64 | sigB.v0 ) goto propagateNaN;
95         return a;
96     }
97     /*------------------------------------------------------------------------
98     *------------------------------------------------------------------------*/
99     if ( ! expB ) {
100         if ( ! (sigB.v64 | sigB.v0) ) goto invalid;
101         normExpSig = softfloat_normSubnormalF128Sig( sigB.v64, sigB.v0 );
102         expB = normExpSig.exp;
103         sigB = normExpSig.sig;
104     }
105     if ( ! expA ) {
106         if ( ! (sigA.v64 | sigA.v0) ) return a;
107         normExpSig = softfloat_normSubnormalF128Sig( sigA.v64, sigA.v0 );
108         expA = normExpSig.exp;
109         sigA = normExpSig.sig;
110     }
111     /*------------------------------------------------------------------------
112     *------------------------------------------------------------------------*/
113     sigA.v64 |= UINT64_C( 0x0001000000000000 );
114     sigB.v64 |= UINT64_C( 0x0001000000000000 );
115     rem = sigA;
116     expDiff = expA - expB;
117     if ( expDiff < 1 ) {
118         if ( expDiff < -1 ) return a;
119         if ( expDiff ) {
120             --expB;
121             sigB = softfloat_add128( sigB.v64, sigB.v0, sigB.v64, sigB.v0 );
122             q = 0;
123         } else {
124             q = softfloat_le128( sigB.v64, sigB.v0, rem.v64, rem.v0 );
125             if ( q ) {
126                 rem = softfloat_sub128( rem.v64, rem.v0, sigB.v64, sigB.v0 );
127             }
128         }
129     } else {
130         recip32 = softfloat_approxRecip32_1( sigB.v64>>17 );
131         expDiff -= 30;
132         for (;;) {
133             q64 = (uint_fast64_t) (uint32_t) (rem.v64>>19) * recip32;
134             if ( expDiff < 0 ) break;
135             q = (q64 + 0x80000000)>>32;
136             rem = softfloat_shortShiftLeft128( rem.v64, rem.v0, 29 );
137             term = softfloat_mul128By32( sigB.v64, sigB.v0, q );
138             rem = softfloat_sub128( rem.v64, rem.v0, term.v64, term.v0 );
139             if ( rem.v64 & UINT64_C( 0x8000000000000000 ) ) {
140                 rem = softfloat_add128( rem.v64, rem.v0, sigB.v64, sigB.v0 );
141             }
142             expDiff -= 29;
143         }
144         /*--------------------------------------------------------------------
145         | (`expDiff' cannot be less than -29 here.)
146         *--------------------------------------------------------------------*/
147         q = (uint32_t) (q64>>32)>>(~expDiff & 31);
148         rem = softfloat_shortShiftLeft128( rem.v64, rem.v0, expDiff + 30 );
149         term = softfloat_mul128By32( sigB.v64, sigB.v0, q );
150         rem = softfloat_sub128( rem.v64, rem.v0, term.v64, term.v0 );
151         if ( rem.v64 & UINT64_C( 0x8000000000000000 ) ) {
152             altRem = softfloat_add128( rem.v64, rem.v0, sigB.v64, sigB.v0 );
153             goto selectRem;
154         }
155     }
156     /*------------------------------------------------------------------------
157     *------------------------------------------------------------------------*/
158     do {
159         altRem = rem;
160         ++q;
161         rem = softfloat_sub128( rem.v64, rem.v0, sigB.v64, sigB.v0 );
162     } while ( ! (rem.v64 & UINT64_C( 0x8000000000000000 )) );
163  selectRem:
164     meanRem = softfloat_add128( rem.v64, rem.v0, altRem.v64, altRem.v0 );
165     if (
166         (meanRem.v64 & UINT64_C( 0x8000000000000000 ))
167             || (! (meanRem.v64 | meanRem.v0) && (q & 1))
168     ) {
169         rem = altRem;
170     }
171     signRem = signA;
172     if ( rem.v64 & UINT64_C( 0x8000000000000000 ) ) {
173         signRem = ! signRem;
174         rem = softfloat_sub128( 0, 0, rem.v64, rem.v0 );
175     }
176     return softfloat_normRoundPackToF128( signRem, expB - 1, rem.v64, rem.v0 );
177     /*------------------------------------------------------------------------
178     *------------------------------------------------------------------------*/
179  propagateNaN:
180     uiZ = softfloat_propagateNaNF128UI( uiA64, uiA0, uiB64, uiB0 );
181     goto uiZ;
182     /*------------------------------------------------------------------------
183     *------------------------------------------------------------------------*/
184  invalid:
185     softfloat_raiseFlags( softfloat_flag_invalid );
186     uiZ.v64 = defaultNaNF128UI64;
187     uiZ.v0  = defaultNaNF128UI0;
188  uiZ:
189     uZ.ui = uiZ;
190     return uZ.f;
191 
192 }
193 
194