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 extFloat80_t
extF80_roundToInt(extFloat80_t a,uint_fast8_t roundingMode,bool exact)46  extF80_roundToInt( extFloat80_t a, uint_fast8_t roundingMode, bool exact )
47 {
48     union { struct extFloat80M s; extFloat80_t f; } uA;
49     uint_fast16_t uiA64, signUI64;
50     int_fast32_t exp;
51     uint_fast64_t sigA;
52     uint_fast16_t uiZ64;
53     uint_fast64_t sigZ;
54     struct exp32_sig64 normExpSig;
55     struct uint128 uiZ;
56     uint_fast64_t lastBitMask, roundBitsMask;
57     union { struct extFloat80M s; extFloat80_t f; } uZ;
58 
59     /*------------------------------------------------------------------------
60     *------------------------------------------------------------------------*/
61     uA.f = a;
62     uiA64 = uA.s.signExp;
63     signUI64 = uiA64 & packToExtF80UI64( 1, 0 );
64     exp = expExtF80UI64( uiA64 );
65     sigA = uA.s.signif;
66     /*------------------------------------------------------------------------
67     *------------------------------------------------------------------------*/
68     if ( ! (sigA & UINT64_C( 0x8000000000000000 )) && (exp != 0x7FFF) ) {
69         if ( ! sigA ) {
70             uiZ64 = signUI64;
71             sigZ = 0;
72             goto uiZ;
73         }
74         normExpSig = softfloat_normSubnormalExtF80Sig( sigA );
75         exp += normExpSig.exp;
76         sigA = normExpSig.sig;
77     }
78     /*------------------------------------------------------------------------
79     *------------------------------------------------------------------------*/
80     if ( 0x403E <= exp ) {
81         if ( exp == 0x7FFF ) {
82             if ( sigA & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) {
83                 uiZ = softfloat_propagateNaNExtF80UI( uiA64, sigA, 0, 0 );
84                 uiZ64 = uiZ.v64;
85                 sigZ  = uiZ.v0;
86                 goto uiZ;
87             }
88             sigZ = UINT64_C( 0x8000000000000000 );
89         } else {
90             sigZ = sigA;
91         }
92         uiZ64 = signUI64 | exp;
93         goto uiZ;
94     }
95     if ( exp <= 0x3FFE ) {
96         if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;
97         switch ( roundingMode ) {
98          case softfloat_round_near_even:
99             if ( ! (sigA & UINT64_C( 0x7FFFFFFFFFFFFFFF )) ) break;
100          case softfloat_round_near_maxMag:
101             if ( exp == 0x3FFE ) goto mag1;
102             break;
103          case softfloat_round_min:
104             if ( signUI64 ) goto mag1;
105             break;
106          case softfloat_round_max:
107             if ( ! signUI64 ) goto mag1;
108             break;
109         }
110         uiZ64 = signUI64;
111         sigZ  = 0;
112         goto uiZ;
113      mag1:
114         uiZ64 = signUI64 | 0x3FFF;
115         sigZ  = UINT64_C( 0x8000000000000000 );
116         goto uiZ;
117     }
118     /*------------------------------------------------------------------------
119     *------------------------------------------------------------------------*/
120     uiZ64 = signUI64 | exp;
121     lastBitMask = (uint_fast64_t) 1<<(0x403E - exp);
122     roundBitsMask = lastBitMask - 1;
123     sigZ = sigA;
124     if ( roundingMode == softfloat_round_near_maxMag ) {
125         sigZ += lastBitMask>>1;
126     } else if ( roundingMode == softfloat_round_near_even ) {
127         sigZ += lastBitMask>>1;
128         if ( ! (sigZ & roundBitsMask) ) sigZ &= ~lastBitMask;
129     } else if ( roundingMode != softfloat_round_minMag ) {
130         if ( (signUI64 != 0) ^ (roundingMode == softfloat_round_max) ) {
131             sigZ += roundBitsMask;
132         }
133     }
134     sigZ &= ~roundBitsMask;
135     if ( ! sigZ ) {
136         ++uiZ64;
137         sigZ = UINT64_C( 0x8000000000000000 );
138     }
139     if ( exact && (sigZ != sigA) ) {
140         softfloat_exceptionFlags |= softfloat_flag_inexact;
141     }
142  uiZ:
143     uZ.s.signExp = uiZ64;
144     uZ.s.signif = sigZ;
145     return uZ.f;
146 
147 }
148 
149