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 extF80_roundToInt( extFloat80_t a
, uint_fast8_t roundingMode
, bool exact
)
47 union { struct extFloat80M s
; extFloat80_t f
; } uA
;
48 uint_fast16_t uiA64
, signUI64
;
53 struct exp32_sig64 normExpSig
;
55 uint_fast64_t lastBitMask
, roundBitsMask
;
56 union { struct extFloat80M s
; extFloat80_t f
; } uZ
;
58 /*------------------------------------------------------------------------
59 *------------------------------------------------------------------------*/
62 signUI64
= uiA64
& packToExtF80UI64( 1, 0 );
63 exp
= expExtF80UI64( uiA64
);
65 /*------------------------------------------------------------------------
66 *------------------------------------------------------------------------*/
67 if ( !(sigA
& UINT64_C( 0x8000000000000000 )) && (exp
!= 0x7FFF) ) {
73 normExpSig
= softfloat_normSubnormalExtF80Sig( sigA
);
74 exp
+= normExpSig
.exp
;
75 sigA
= normExpSig
.sig
;
77 /*------------------------------------------------------------------------
78 *------------------------------------------------------------------------*/
79 if ( 0x403E <= exp
) {
80 if ( exp
== 0x7FFF ) {
81 if ( sigA
& UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) {
82 uiZ
= softfloat_propagateNaNExtF80UI( uiA64
, sigA
, 0, 0 );
87 sigZ
= UINT64_C( 0x8000000000000000 );
91 uiZ64
= signUI64
| exp
;
94 if ( exp
<= 0x3FFE ) {
95 if ( exact
) softfloat_exceptionFlags
|= softfloat_flag_inexact
;
96 switch ( roundingMode
) {
97 case softfloat_round_near_even
:
98 if ( !(sigA
& UINT64_C( 0x7FFFFFFFFFFFFFFF )) ) break;
99 case softfloat_round_near_maxMag
:
100 if ( exp
== 0x3FFE ) goto mag1
;
102 case softfloat_round_min
:
103 if ( signUI64
) goto mag1
;
105 case softfloat_round_max
:
106 if ( !signUI64
) goto mag1
;
108 #ifdef SOFTFLOAT_ROUND_ODD
109 case softfloat_round_odd
:
117 uiZ64
= signUI64
| 0x3FFF;
118 sigZ
= UINT64_C( 0x8000000000000000 );
121 /*------------------------------------------------------------------------
122 *------------------------------------------------------------------------*/
123 uiZ64
= signUI64
| exp
;
124 lastBitMask
= (uint_fast64_t) 1<<(0x403E - exp
);
125 roundBitsMask
= lastBitMask
- 1;
127 if ( roundingMode
== softfloat_round_near_maxMag
) {
128 sigZ
+= lastBitMask
>>1;
129 } else if ( roundingMode
== softfloat_round_near_even
) {
130 sigZ
+= lastBitMask
>>1;
131 if ( !(sigZ
& roundBitsMask
) ) sigZ
&= ~lastBitMask
;
133 roundingMode
== (signUI64
? softfloat_round_min
: softfloat_round_max
)
135 sigZ
+= roundBitsMask
;
137 sigZ
&= ~roundBitsMask
;
140 sigZ
= UINT64_C( 0x8000000000000000 );
142 if ( sigZ
!= sigA
) {
143 #ifdef SOFTFLOAT_ROUND_ODD
144 if ( roundingMode
== softfloat_round_odd
) sigZ
|= lastBitMask
;
146 if ( exact
) softfloat_exceptionFlags
|= softfloat_flag_inexact
;
149 uZ
.s
.signExp
= uiZ64
;