2 /*============================================================================
4 This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
5 Package, Release 3d, by John R. Hauser.
7 Copyright 2011, 2012, 2013, 2014 The Regents of the University of California.
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
13 1. Redistributions of source code must retain the above copyright notice,
14 this list of conditions, and the following disclaimer.
16 2. Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions, and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
20 3. Neither the name of the University nor the names of its contributors may
21 be used to endorse or promote products derived from this software without
22 specific prior written permission.
24 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
27 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 =============================================================================*/
40 #include "internals.h"
41 #include "specialize.h"
42 #include "softfloat.h"
44 float64_t
f64_rem( float64_t a
, float64_t b
)
55 struct exp16_sig64 normExpSig
;
60 uint64_t altRem
, meanRem
;
65 /*------------------------------------------------------------------------
66 *------------------------------------------------------------------------*/
69 signA
= signF64UI( uiA
);
70 expA
= expF64UI( uiA
);
71 sigA
= fracF64UI( uiA
);
74 expB
= expF64UI( uiB
);
75 sigB
= fracF64UI( uiB
);
76 /*------------------------------------------------------------------------
77 *------------------------------------------------------------------------*/
78 if ( expA
== 0x7FF ) {
79 if ( sigA
|| ((expB
== 0x7FF) && sigB
) ) goto propagateNaN
;
82 if ( expB
== 0x7FF ) {
83 if ( sigB
) goto propagateNaN
;
86 /*------------------------------------------------------------------------
87 *------------------------------------------------------------------------*/
88 if ( expA
< expB
- 1 ) return a
;
89 /*------------------------------------------------------------------------
90 *------------------------------------------------------------------------*/
92 if ( ! sigB
) goto invalid
;
93 normExpSig
= softfloat_normSubnormalF64Sig( sigB
);
94 expB
= normExpSig
.exp
;
95 sigB
= normExpSig
.sig
;
98 if ( ! sigA
) return a
;
99 normExpSig
= softfloat_normSubnormalF64Sig( sigA
);
100 expA
= normExpSig
.exp
;
101 sigA
= normExpSig
.sig
;
103 /*------------------------------------------------------------------------
104 *------------------------------------------------------------------------*/
105 rem
= sigA
| UINT64_C( 0x0010000000000000 );
106 sigB
|= UINT64_C( 0x0010000000000000 );
107 expDiff
= expA
- expB
;
109 if ( expDiff
< -1 ) return a
;
117 if ( q
) rem
-= sigB
;
120 recip32
= softfloat_approxRecip32_1( sigB
>>21 );
121 /*--------------------------------------------------------------------
122 | Changing the shift of `rem' here requires also changing the initial
123 | subtraction from `expDiff'.
124 *--------------------------------------------------------------------*/
127 /*--------------------------------------------------------------------
128 | The scale of `sigB' affects how many bits are obtained during each
129 | cycle of the loop. Currently this is 29 bits per loop iteration,
130 | the maximum possible.
131 *--------------------------------------------------------------------*/
134 q64
= (uint32_t) (rem
>>32) * (uint_fast64_t) recip32
;
135 if ( expDiff
< 0 ) break;
136 q
= (q64
+ 0x80000000)>>32;
137 #ifdef SOFTFLOAT_FAST_INT64
140 rem
= (uint_fast64_t) (uint32_t) (rem
>>3)<<32;
142 rem
-= q
* (uint64_t) sigB
;
143 if ( rem
& UINT64_C( 0x8000000000000000 ) ) rem
+= sigB
;
146 /*--------------------------------------------------------------------
147 | (`expDiff' cannot be less than -29 here.)
148 *--------------------------------------------------------------------*/
149 q
= (uint32_t) (q64
>>32)>>(~expDiff
& 31);
150 rem
= (rem
<<(expDiff
+ 30)) - q
* (uint64_t) sigB
;
151 if ( rem
& UINT64_C( 0x8000000000000000 ) ) {
156 /*------------------------------------------------------------------------
157 *------------------------------------------------------------------------*/
162 } while ( ! (rem
& UINT64_C( 0x8000000000000000 )) );
164 meanRem
= rem
+ altRem
;
166 (meanRem
& UINT64_C( 0x8000000000000000 )) || (! meanRem
&& (q
& 1))
171 if ( rem
& UINT64_C( 0x8000000000000000 ) ) {
175 return softfloat_normRoundPackToF64( signRem
, expB
, rem
);
176 /*------------------------------------------------------------------------
177 *------------------------------------------------------------------------*/
179 uiZ
= softfloat_propagateNaNF64UI( uiA
, uiB
);
182 softfloat_raiseFlags( softfloat_flag_invalid
);
183 uiZ
= defaultNaNF64UI
;