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
f128M_rem(const float128_t * aPtr,const float128_t * bPtr,float128_t * zPtr)48  f128M_rem( const float128_t *aPtr, const float128_t *bPtr, float128_t *zPtr )
49 {
50 
51     *zPtr = f128_rem( *aPtr, *bPtr );
52 
53 }
54 
55 #else
56 
57 void
f128M_rem(const float128_t * aPtr,const float128_t * bPtr,float128_t * zPtr)58  f128M_rem( const float128_t *aPtr, const float128_t *bPtr, float128_t *zPtr )
59 {
60     const uint32_t *aWPtr, *bWPtr;
61     uint32_t *zWPtr, uiA96;
62     int32_t expA, expB;
63     uint32_t x[4], rem1[5], *remPtr;
64     bool signRem;
65     int32_t expDiff;
66     uint32_t q, recip32;
67     uint64_t q64;
68     uint32_t rem2[5], *altRemPtr, *newRemPtr, wordMeanRem;
69 
70     /*------------------------------------------------------------------------
71     *------------------------------------------------------------------------*/
72     aWPtr = (const uint32_t *) aPtr;
73     bWPtr = (const uint32_t *) bPtr;
74     zWPtr = (uint32_t *) zPtr;
75     /*------------------------------------------------------------------------
76     *------------------------------------------------------------------------*/
77     uiA96 = aWPtr[indexWordHi( 4 )];
78     expA = expF128UI96( uiA96 );
79     expB = expF128UI96( bWPtr[indexWordHi( 4 )] );
80     /*------------------------------------------------------------------------
81     *------------------------------------------------------------------------*/
82     if ( (expA == 0x7FFF) || (expB == 0x7FFF) ) {
83         if ( softfloat_tryPropagateNaNF128M( aWPtr, bWPtr, zWPtr ) ) return;
84         if ( expA == 0x7FFF ) goto invalid;
85         goto copyA;
86     }
87     /*------------------------------------------------------------------------
88     *------------------------------------------------------------------------*/
89     if ( expA < expB - 1 ) goto copyA;
90     /*------------------------------------------------------------------------
91     *------------------------------------------------------------------------*/
92     expB = softfloat_shiftNormSigF128M( bWPtr, 13, x );
93     if ( expB == -128 ) goto invalid;
94     remPtr = &rem1[indexMultiwordLo( 5, 4 )];
95     expA = softfloat_shiftNormSigF128M( aWPtr, 13, remPtr );
96     if ( expA == -128 ) goto copyA;
97     signRem = signF128UI96( uiA96 );
98     /*------------------------------------------------------------------------
99     *------------------------------------------------------------------------*/
100     expDiff = expA - expB;
101     if ( expDiff < 1 ) {
102         if ( expDiff < -1 ) goto copyA;
103         if ( expDiff ) {
104             --expB;
105             softfloat_add128M( x, x, x );
106             q = 0;
107         } else {
108             q = (softfloat_compare128M( x, remPtr ) <= 0);
109             if ( q ) softfloat_sub128M( remPtr, x, remPtr );
110         }
111     } else {
112         recip32 =
113             softfloat_approxRecip32_1(
114                 ((uint64_t) x[indexWord( 4, 3 )]<<32 | x[indexWord( 4, 2 )])
115                     >>30
116             );
117         expDiff -= 30;
118         for (;;) {
119             q64 = (uint64_t) remPtr[indexWordHi( 4 )] * recip32;
120             if ( expDiff < 0 ) break;
121             q = (q64 + 0x80000000)>>32;
122             softfloat_remStep128MBy32( remPtr, 29, x, q, remPtr );
123             if ( remPtr[indexWordHi( 4 )] & 0x80000000 ) {
124                 softfloat_add128M( remPtr, x, remPtr );
125             }
126             expDiff -= 29;
127         }
128         /*--------------------------------------------------------------------
129         | (`expDiff' cannot be less than -29 here.)
130         *--------------------------------------------------------------------*/
131         q = (uint32_t) (q64>>32)>>(~expDiff & 31);
132         softfloat_remStep128MBy32( remPtr, expDiff + 30, x, q, remPtr );
133         if ( remPtr[indexWordHi( 4 )] & 0x80000000 ) {
134             altRemPtr = &rem2[indexMultiwordLo( 5, 4 )];
135             softfloat_add128M( remPtr, x, altRemPtr );
136             goto selectRem;
137         }
138     }
139     /*------------------------------------------------------------------------
140     *------------------------------------------------------------------------*/
141     altRemPtr = &rem2[indexMultiwordLo( 5, 4 )];
142     do {
143         ++q;
144         newRemPtr = altRemPtr;
145         softfloat_sub128M( remPtr, x, newRemPtr );
146         altRemPtr = remPtr;
147         remPtr = newRemPtr;
148     } while ( ! (remPtr[indexWordHi( 4 )] & 0x80000000) );
149  selectRem:
150     softfloat_add128M( remPtr, altRemPtr, x );
151     wordMeanRem = x[indexWordHi( 4 )];
152     if (
153         (wordMeanRem & 0x80000000)
154             || (! wordMeanRem && (q & 1) && ! x[indexWord( 4, 0 )]
155                     && ! (x[indexWord( 4, 2 )] | x[indexWord( 4, 1 )]))
156     ) {
157         remPtr = altRemPtr;
158     }
159     if ( remPtr[indexWordHi( 4 )] & 0x80000000 ) {
160         signRem = ! signRem;
161         softfloat_negX128M( remPtr );
162     }
163     remPtr -= indexMultiwordLo( 5, 4 );
164     remPtr[indexWordHi( 5 )] = 0;
165     softfloat_normRoundPackMToF128M( signRem, expB + 18, remPtr, zWPtr );
166     return;
167     /*------------------------------------------------------------------------
168     *------------------------------------------------------------------------*/
169  invalid:
170     softfloat_invalidF128M( zWPtr );
171     return;
172     /*------------------------------------------------------------------------
173     *------------------------------------------------------------------------*/
174  copyA:
175     zWPtr[indexWordHi( 4 )] = uiA96;
176     zWPtr[indexWord( 4, 2 )] = aWPtr[indexWord( 4, 2 )];
177     zWPtr[indexWord( 4, 1 )] = aWPtr[indexWord( 4, 1 )];
178     zWPtr[indexWord( 4, 0 )] = aWPtr[indexWord( 4, 0 )];
179 
180 }
181 
182 #endif
183 
184