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 
45 #ifdef SOFTFLOAT_FAST_INT64
46 
47 void
extF80M_rem(const extFloat80_t * aPtr,const extFloat80_t * bPtr,extFloat80_t * zPtr)48  extF80M_rem(
49      const extFloat80_t *aPtr, const extFloat80_t *bPtr, extFloat80_t *zPtr )
50 {
51 
52     *zPtr = extF80_rem( *aPtr, *bPtr );
53 
54 }
55 
56 #else
57 
58 void
extF80M_rem(const extFloat80_t * aPtr,const extFloat80_t * bPtr,extFloat80_t * zPtr)59  extF80M_rem(
60      const extFloat80_t *aPtr, const extFloat80_t *bPtr, extFloat80_t *zPtr )
61 {
62     const struct extFloat80M *aSPtr, *bSPtr;
63     struct extFloat80M *zSPtr;
64     uint_fast16_t uiA64;
65     int32_t expA, expB;
66     uint64_t x64;
67     bool signRem;
68     uint64_t sigA;
69     int32_t expDiff;
70     uint32_t rem[3], x[3], sig32B, q, recip32, rem2[3], *remPtr, *altRemPtr;
71     uint32_t *newRemPtr, wordMeanRem;
72 
73     /*------------------------------------------------------------------------
74     *------------------------------------------------------------------------*/
75     aSPtr = (const struct extFloat80M *) aPtr;
76     bSPtr = (const struct extFloat80M *) bPtr;
77     zSPtr = (struct extFloat80M *) zPtr;
78     /*------------------------------------------------------------------------
79     *------------------------------------------------------------------------*/
80     uiA64 = aSPtr->signExp;
81     expA = expExtF80UI64( uiA64 );
82     expB = expExtF80UI64( bSPtr->signExp );
83     /*------------------------------------------------------------------------
84     *------------------------------------------------------------------------*/
85     if ( (expA == 0x7FFF) || (expB == 0x7FFF) ) {
86         if ( softfloat_tryPropagateNaNExtF80M( aSPtr, bSPtr, zSPtr ) ) return;
87         if ( expA == 0x7FFF ) goto invalid;
88         /*--------------------------------------------------------------------
89         | If we get here, then argument b is an infinity and `expB' is 0x7FFF;
90         | Doubling `expB' is an easy way to ensure that `expDiff' later is
91         | less than -1, which will result in returning a canonicalized version
92         | of argument a.
93         *--------------------------------------------------------------------*/
94         expB += expB;
95     }
96     /*------------------------------------------------------------------------
97     *------------------------------------------------------------------------*/
98     if ( ! expB ) expB = 1;
99     x64 = bSPtr->signif;
100     if ( ! (x64 & UINT64_C( 0x8000000000000000 )) ) {
101         if ( ! x64 ) goto invalid;
102         expB += softfloat_normExtF80SigM( &x64 );
103     }
104     signRem = signExtF80UI64( uiA64 );
105     if ( ! expA ) expA = 1;
106     sigA = aSPtr->signif;
107     if ( ! (sigA & UINT64_C( 0x8000000000000000 )) ) {
108         if ( ! sigA ) {
109             expA = 0;
110             goto copyA;
111         }
112         expA += softfloat_normExtF80SigM( &sigA );
113     }
114     /*------------------------------------------------------------------------
115     *------------------------------------------------------------------------*/
116     expDiff = expA - expB;
117     if ( expDiff < -1 ) goto copyA;
118     rem[indexWord( 3, 2 )] = sigA>>34;
119     rem[indexWord( 3, 1 )] = sigA>>2;
120     rem[indexWord( 3, 0 )] = (uint32_t) sigA<<30;
121     x[indexWord( 3, 0 )] = (uint32_t) x64<<30;
122     sig32B = x64>>32;
123     x64 >>= 2;
124     x[indexWord( 3, 2 )] = x64>>32;
125     x[indexWord( 3, 1 )] = x64;
126     if ( expDiff < 1 ) {
127         if ( expDiff ) {
128             --expB;
129             softfloat_add96M( x, x, x );
130             q = 0;
131         } else {
132             q = (softfloat_compare96M( x, rem ) <= 0);
133             if ( q ) softfloat_sub96M( rem, x, rem );
134         }
135     } else {
136         recip32 = softfloat_approxRecip32_1( sig32B );
137         expDiff -= 30;
138         for (;;) {
139             x64 = (uint64_t) rem[indexWordHi( 3 )] * recip32;
140             if ( expDiff < 0 ) break;
141             q = (x64 + 0x80000000)>>32;
142             softfloat_remStep96MBy32( rem, 29, x, q, rem );
143             if ( rem[indexWordHi( 3 )] & 0x80000000 ) {
144                 softfloat_add96M( rem, x, rem );
145             }
146             expDiff -= 29;
147         }
148         /*--------------------------------------------------------------------
149         | (`expDiff' cannot be less than -29 here.)
150         *--------------------------------------------------------------------*/
151         q = (uint32_t) (x64>>32)>>(~expDiff & 31);
152         softfloat_remStep96MBy32( rem, expDiff + 30, x, q, rem );
153         if ( rem[indexWordHi( 3 )] & 0x80000000 ) {
154             remPtr = rem;
155             altRemPtr = rem2;
156             softfloat_add96M( remPtr, x, altRemPtr );
157             goto selectRem;
158         }
159     }
160     /*------------------------------------------------------------------------
161     *------------------------------------------------------------------------*/
162     remPtr = rem;
163     altRemPtr = rem2;
164     do {
165         ++q;
166         newRemPtr = altRemPtr;
167         softfloat_sub96M( remPtr, x, newRemPtr );
168         altRemPtr = remPtr;
169         remPtr = newRemPtr;
170     } while ( ! (remPtr[indexWordHi( 3 )] & 0x80000000) );
171  selectRem:
172     softfloat_add96M( remPtr, altRemPtr, x );
173     wordMeanRem = x[indexWordHi( 3 )];
174     if (
175         (wordMeanRem & 0x80000000)
176             || (! wordMeanRem && (q & 1) && ! x[indexWord( 3, 0 )]
177                     && ! x[indexWord( 3, 1 )])
178     ) {
179         remPtr = altRemPtr;
180     }
181     if ( remPtr[indexWordHi( 3 )] & 0x80000000 ) {
182         signRem = ! signRem;
183         softfloat_negX96M( remPtr );
184     }
185     softfloat_normRoundPackMToExtF80M( signRem, expB + 2, remPtr, 80, zSPtr );
186     return;
187     /*------------------------------------------------------------------------
188     *------------------------------------------------------------------------*/
189  invalid:
190     softfloat_invalidExtF80M( zSPtr );
191     return;
192     /*------------------------------------------------------------------------
193     *------------------------------------------------------------------------*/
194  copyA:
195     if ( expA < 1 ) {
196         sigA >>= 1 - expA;
197         expA = 0;
198     }
199     zSPtr->signExp = packToExtF80UI64( signRem, expA );
200     zSPtr->signif = sigA;
201 
202 }
203 
204 #endif
205 
206