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"
45 f128_roundToInt( float128_t a
, uint_fast8_t roundingMode
, bool exact
)
48 uint_fast64_t uiA64
, uiA0
;
51 uint_fast64_t lastBitMask0
, roundBitsMask
;
53 uint_fast64_t lastBitMask64
;
56 /*------------------------------------------------------------------------
57 *------------------------------------------------------------------------*/
61 exp
= expF128UI64( uiA64
);
62 /*------------------------------------------------------------------------
63 *------------------------------------------------------------------------*/
64 if ( 0x402F <= exp
) {
65 /*--------------------------------------------------------------------
66 *--------------------------------------------------------------------*/
67 if ( 0x406F <= exp
) {
68 if ( (exp
== 0x7FFF) && (fracF128UI64( uiA64
) | uiA0
) ) {
69 uiZ
= softfloat_propagateNaNF128UI( uiA64
, uiA0
, 0, 0 );
74 /*--------------------------------------------------------------------
75 *--------------------------------------------------------------------*/
76 lastBitMask0
= (uint_fast64_t) 2<<(0x406E - exp
);
77 roundBitsMask
= lastBitMask0
- 1;
80 roundNearEven
= (roundingMode
== softfloat_round_near_even
);
81 if ( roundNearEven
|| (roundingMode
== softfloat_round_near_maxMag
) ) {
82 if ( exp
== 0x402F ) {
83 if ( UINT64_C( 0x8000000000000000 ) <= uiZ
.v0
) {
87 && (uiZ
.v0
== UINT64_C( 0x8000000000000000 ))
93 uiZ
= softfloat_add128( uiZ
.v64
, uiZ
.v0
, 0, lastBitMask0
>>1 );
94 if ( roundNearEven
&& !(uiZ
.v0
& roundBitsMask
) ) {
95 uiZ
.v0
&= ~lastBitMask0
;
100 == (signF128UI64( uiZ
.v64
) ? softfloat_round_min
101 : softfloat_round_max
)
103 uiZ
= softfloat_add128( uiZ
.v64
, uiZ
.v0
, 0, roundBitsMask
);
105 uiZ
.v0
&= ~roundBitsMask
;
106 lastBitMask64
= !lastBitMask0
;
108 /*--------------------------------------------------------------------
109 *--------------------------------------------------------------------*/
110 if ( exp
< 0x3FFF ) {
111 if ( !((uiA64
& UINT64_C( 0x7FFFFFFFFFFFFFFF )) | uiA0
) ) return a
;
112 if ( exact
) softfloat_exceptionFlags
|= softfloat_flag_inexact
;
113 uiZ
.v64
= uiA64
& packToF128UI64( 1, 0, 0 );
115 switch ( roundingMode
) {
116 case softfloat_round_near_even
:
117 if ( !(fracF128UI64( uiA64
) | uiA0
) ) break;
118 case softfloat_round_near_maxMag
:
119 if ( exp
== 0x3FFE ) uiZ
.v64
|= packToF128UI64( 0, 0x3FFF, 0 );
121 case softfloat_round_min
:
122 if ( uiZ
.v64
) uiZ
.v64
= packToF128UI64( 1, 0x3FFF, 0 );
124 case softfloat_round_max
:
125 if ( !uiZ
.v64
) uiZ
.v64
= packToF128UI64( 0, 0x3FFF, 0 );
127 #ifdef SOFTFLOAT_ROUND_ODD
128 case softfloat_round_odd
:
129 uiZ
.v64
|= packToF128UI64( 0, 0x3FFF, 0 );
135 /*--------------------------------------------------------------------
136 *--------------------------------------------------------------------*/
139 lastBitMask64
= (uint_fast64_t) 1<<(0x402F - exp
);
140 roundBitsMask
= lastBitMask64
- 1;
141 if ( roundingMode
== softfloat_round_near_maxMag
) {
142 uiZ
.v64
+= lastBitMask64
>>1;
143 } else if ( roundingMode
== softfloat_round_near_even
) {
144 uiZ
.v64
+= lastBitMask64
>>1;
145 if ( !((uiZ
.v64
& roundBitsMask
) | uiA0
) ) {
146 uiZ
.v64
&= ~lastBitMask64
;
150 == (signF128UI64( uiZ
.v64
) ? softfloat_round_min
151 : softfloat_round_max
)
153 uiZ
.v64
= (uiZ
.v64
| (uiA0
!= 0)) + roundBitsMask
;
155 uiZ
.v64
&= ~roundBitsMask
;
158 if ( (uiZ
.v64
!= uiA64
) || (uiZ
.v0
!= uiA0
) ) {
159 #ifdef SOFTFLOAT_ROUND_ODD
160 if ( roundingMode
== softfloat_round_odd
) {
161 uiZ
.v64
|= lastBitMask64
;
162 uiZ
.v0
|= lastBitMask0
;
165 if ( exact
) softfloat_exceptionFlags
|= softfloat_flag_inexact
;