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 #ifdef SOFTFLOAT_FAST_INT64
46 
47 void
f128M_roundToInt(const float128_t * aPtr,uint_fast8_t roundingMode,bool exact,float128_t * zPtr)48  f128M_roundToInt(
49      const float128_t *aPtr,
50      uint_fast8_t roundingMode,
51      bool exact,
52      float128_t *zPtr
53  )
54 {
55 
56     *zPtr = f128_roundToInt( *aPtr, roundingMode, exact );
57 
58 }
59 
60 #else
61 
62 void
f128M_roundToInt(const float128_t * aPtr,uint_fast8_t roundingMode,bool exact,float128_t * zPtr)63  f128M_roundToInt(
64      const float128_t *aPtr,
65      uint_fast8_t roundingMode,
66      bool exact,
67      float128_t *zPtr
68  )
69 {
70     const uint32_t *aWPtr;
71     uint32_t *zWPtr;
72     uint32_t ui96;
73     int32_t exp;
74     uint32_t sigExtra;
75     bool sign;
76     uint_fast8_t bitPos;
77     bool roundNear;
78     unsigned int index, lastIndex;
79     bool extra;
80     uint32_t wordA, bit, wordZ;
81     uint_fast8_t carry;
82     uint32_t extrasMask;
83 
84     /*------------------------------------------------------------------------
85     *------------------------------------------------------------------------*/
86     aWPtr = (const uint32_t *) aPtr;
87     zWPtr = (uint32_t *) zPtr;
88     /*------------------------------------------------------------------------
89     *------------------------------------------------------------------------*/
90     ui96 = aWPtr[indexWordHi( 4 )];
91     exp = expF128UI96( ui96 );
92     /*------------------------------------------------------------------------
93     *------------------------------------------------------------------------*/
94     if ( exp < 0x3FFF ) {
95         zWPtr[indexWord( 4, 2 )] = 0;
96         zWPtr[indexWord( 4, 1 )] = 0;
97         zWPtr[indexWord( 4, 0 )] = 0;
98         sigExtra = aWPtr[indexWord( 4, 2 )];
99         if ( ! sigExtra ) {
100             sigExtra = aWPtr[indexWord( 4, 1 )] | aWPtr[indexWord( 4, 0 )];
101         }
102         if ( ! sigExtra && ! (ui96 & 0x7FFFFFFF) ) goto ui96;
103         if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;
104         sign = signF128UI96( ui96 );
105         switch ( roundingMode ) {
106          case softfloat_round_near_even:
107             if ( ! fracF128UI96( ui96 ) && ! sigExtra ) break;
108          case softfloat_round_near_maxMag:
109             if ( exp == 0x3FFE ) goto mag1;
110             break;
111          case softfloat_round_min:
112             if ( sign ) goto mag1;
113             break;
114          case softfloat_round_max:
115             if ( ! sign ) goto mag1;
116             break;
117         }
118         ui96 = packToF128UI96( sign, 0, 0 );
119         goto ui96;
120      mag1:
121         ui96 = packToF128UI96( sign, 0x3FFF, 0 );
122         goto ui96;
123     }
124     /*------------------------------------------------------------------------
125     *------------------------------------------------------------------------*/
126     if ( 0x406F <= exp ) {
127         if (
128             (exp == 0x7FFF)
129                 && (fracF128UI96( ui96 )
130                         || (aWPtr[indexWord( 4, 2 )] | aWPtr[indexWord( 4, 1 )]
131                                 | aWPtr[indexWord( 4, 0 )]))
132         ) {
133             softfloat_propagateNaNF128M( aWPtr, 0, zWPtr );
134             return;
135         }
136         zWPtr[indexWord( 4, 2 )] = aWPtr[indexWord( 4, 2 )];
137         zWPtr[indexWord( 4, 1 )] = aWPtr[indexWord( 4, 1 )];
138         zWPtr[indexWord( 4, 0 )] = aWPtr[indexWord( 4, 0 )];
139         goto ui96;
140     }
141     /*------------------------------------------------------------------------
142     *------------------------------------------------------------------------*/
143     bitPos = 0x406F - exp;
144     roundNear =
145            (roundingMode == softfloat_round_near_maxMag)
146         || (roundingMode == softfloat_round_near_even);
147     bitPos -= roundNear;
148     index = indexWordLo( 4 );
149     lastIndex = indexWordHi( 4 );
150     extra = 0;
151     for (;;) {
152         wordA = aWPtr[index];
153         if ( bitPos < 32 ) break;
154         if ( wordA ) extra = 1;
155         zWPtr[index] = 0;
156         index += wordIncr;
157         bitPos -= 32;
158     }
159     bit = (uint32_t) 1<<bitPos;
160     if ( roundNear ) {
161         wordZ = wordA + bit;
162         carry = (wordZ < wordA);
163         bit <<= 1;
164         extrasMask = bit - 1;
165         if (
166             (roundingMode == softfloat_round_near_even)
167                 && ! extra && ! (wordZ & extrasMask)
168         ) {
169             if ( ! bit ) {
170                 zWPtr[index] = wordZ;
171                 index += wordIncr;
172                 wordZ = aWPtr[index] + carry;
173                 carry &= ! wordZ;
174                 zWPtr[index] = wordZ & ~1;
175                 goto propagateCarry;
176             }
177             wordZ &= ~bit;
178         }
179     } else {
180         extrasMask = bit - 1;
181         wordZ = wordA;
182         carry = 0;
183         if (
184                (roundingMode != softfloat_round_minMag)
185             && (signF128UI96( ui96 ) ^ (roundingMode == softfloat_round_max))
186         ) {
187             if ( extra || (wordA & extrasMask) ) {
188                 wordZ += bit;
189                 carry = (wordZ < wordA);
190             }
191         }
192     }
193     wordZ &= ~extrasMask;
194     zWPtr[index] = wordZ;
195  propagateCarry:
196     while ( index != lastIndex ) {
197         index += wordIncr;
198         wordZ = aWPtr[index] + carry;
199         zWPtr[index] = wordZ;
200         carry &= ! wordZ;
201     }
202     /*------------------------------------------------------------------------
203     *------------------------------------------------------------------------*/
204     if ( exact && (softfloat_compare128M( aWPtr, zWPtr ) != 0) ) {
205         softfloat_exceptionFlags |= softfloat_flag_inexact;
206     }
207     return;
208     /*------------------------------------------------------------------------
209     *------------------------------------------------------------------------*/
210  ui96:
211     zWPtr[indexWordHi( 4 )] = ui96;
212 
213 }
214 
215 #endif
216 
217