2 /*============================================================================
4 This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
5 Package, Release 3e, by John R. Hauser.
7 Copyright 2011, 2012, 2013, 2014, 2017 The Regents of the University of
8 California. All rights reserved.
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 #ifdef SOFTFLOAT_FAST_INT64
48 const float128_t
*aPtr
,
49 uint_fast8_t roundingMode
,
55 *zPtr
= f128_roundToInt( *aPtr
, roundingMode
, exact
);
63 const float128_t
*aPtr
,
64 uint_fast8_t roundingMode
,
69 const uint32_t *aWPtr
;
77 unsigned int index
, lastIndex
;
79 uint32_t wordA
, bit
, wordZ
;
83 /*------------------------------------------------------------------------
84 *------------------------------------------------------------------------*/
85 aWPtr
= (const uint32_t *) aPtr
;
86 zWPtr
= (uint32_t *) zPtr
;
87 /*------------------------------------------------------------------------
88 *------------------------------------------------------------------------*/
89 ui96
= aWPtr
[indexWordHi( 4 )];
90 exp
= expF128UI96( ui96
);
91 /*------------------------------------------------------------------------
92 *------------------------------------------------------------------------*/
94 zWPtr
[indexWord( 4, 2 )] = 0;
95 zWPtr
[indexWord( 4, 1 )] = 0;
96 zWPtr
[indexWord( 4, 0 )] = 0;
97 sigExtra
= aWPtr
[indexWord( 4, 2 )];
99 sigExtra
= aWPtr
[indexWord( 4, 1 )] | aWPtr
[indexWord( 4, 0 )];
101 if ( !sigExtra
&& !(ui96
& 0x7FFFFFFF) ) goto ui96
;
102 if ( exact
) softfloat_exceptionFlags
|= softfloat_flag_inexact
;
103 sign
= signF128UI96( ui96
);
104 switch ( roundingMode
) {
105 case softfloat_round_near_even
:
106 if ( !fracF128UI96( ui96
) && !sigExtra
) break;
107 case softfloat_round_near_maxMag
:
108 if ( exp
== 0x3FFE ) goto mag1
;
110 case softfloat_round_min
:
111 if ( sign
) goto mag1
;
113 case softfloat_round_max
:
114 if ( !sign
) goto mag1
;
116 #ifdef SOFTFLOAT_ROUND_ODD
117 case softfloat_round_odd
:
121 ui96
= packToF128UI96( sign
, 0, 0 );
124 ui96
= packToF128UI96( sign
, 0x3FFF, 0 );
127 /*------------------------------------------------------------------------
128 *------------------------------------------------------------------------*/
129 if ( 0x406F <= exp
) {
132 && (fracF128UI96( ui96
)
133 || (aWPtr
[indexWord( 4, 2 )] | aWPtr
[indexWord( 4, 1 )]
134 | aWPtr
[indexWord( 4, 0 )]))
136 softfloat_propagateNaNF128M( aWPtr
, 0, zWPtr
);
139 zWPtr
[indexWord( 4, 2 )] = aWPtr
[indexWord( 4, 2 )];
140 zWPtr
[indexWord( 4, 1 )] = aWPtr
[indexWord( 4, 1 )];
141 zWPtr
[indexWord( 4, 0 )] = aWPtr
[indexWord( 4, 0 )];
144 /*------------------------------------------------------------------------
145 *------------------------------------------------------------------------*/
146 bitPos
= 0x406F - exp
;
148 (roundingMode
== softfloat_round_near_maxMag
)
149 || (roundingMode
== softfloat_round_near_even
);
151 index
= indexWordLo( 4 );
152 lastIndex
= indexWordHi( 4 );
155 wordA
= aWPtr
[index
];
156 if ( bitPos
< 32 ) break;
157 if ( wordA
) extra
= 1;
162 bit
= (uint32_t) 1<<bitPos
;
165 carry
= (wordZ
< wordA
);
167 extrasMask
= bit
- 1;
168 if ( exact
&& (extra
|| (wordA
& extrasMask
)) ) {
169 softfloat_exceptionFlags
|= softfloat_flag_inexact
;
172 (roundingMode
== softfloat_round_near_even
)
173 && !extra
&& !(wordZ
& extrasMask
)
176 zWPtr
[index
] = wordZ
;
178 wordZ
= aWPtr
[index
] + carry
;
180 zWPtr
[index
] = wordZ
& ~1;
188 extrasMask
= bit
- 1;
189 if ( extra
|| (wordA
& extrasMask
) ) {
190 if ( exact
) softfloat_exceptionFlags
|= softfloat_flag_inexact
;
193 == (signF128UI96( ui96
) ? softfloat_round_min
194 : softfloat_round_max
)
197 carry
= (wordZ
< wordA
);
198 #ifdef SOFTFLOAT_ROUND_ODD
199 } else if ( roundingMode
== softfloat_round_odd
) {
205 wordZ
&= ~extrasMask
;
206 zWPtr
[index
] = wordZ
;
208 while ( index
!= lastIndex
) {
210 wordZ
= aWPtr
[index
] + carry
;
211 zWPtr
[index
] = wordZ
;
215 /*------------------------------------------------------------------------
216 *------------------------------------------------------------------------*/
218 zWPtr
[indexWordHi( 4 )] = ui96
;