1 |
|
---|
2 | /*============================================================================
|
---|
3 |
|
---|
4 | This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
|
---|
5 | Package, Release 3e, by John R. Hauser.
|
---|
6 |
|
---|
7 | Copyright 2011, 2012, 2013, 2014, 2017 The Regents of the University of
|
---|
8 | California. All rights reserved.
|
---|
9 |
|
---|
10 | Redistribution and use in source and binary forms, with or without
|
---|
11 | modification, are permitted provided that the following conditions are met:
|
---|
12 |
|
---|
13 | 1. Redistributions of source code must retain the above copyright notice,
|
---|
14 | this list of conditions, and the following disclaimer.
|
---|
15 |
|
---|
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.
|
---|
19 |
|
---|
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.
|
---|
23 |
|
---|
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.
|
---|
34 |
|
---|
35 | =============================================================================*/
|
---|
36 |
|
---|
37 | #include <stdbool.h>
|
---|
38 | #include <stdint.h>
|
---|
39 | #include "platform.h"
|
---|
40 | #include "internals.h"
|
---|
41 | #include "specialize.h"
|
---|
42 | #include "softfloat.h"
|
---|
43 |
|
---|
44 | #ifdef SOFTFLOAT_FAST_INT64
|
---|
45 |
|
---|
46 | void
|
---|
47 | f128M_roundToInt(
|
---|
48 | const float128_t *aPtr,
|
---|
49 | uint_fast8_t roundingMode,
|
---|
50 | bool exact,
|
---|
51 | float128_t *zPtr
|
---|
52 | SOFTFLOAT_STATE_DECL_COMMA
|
---|
53 | )
|
---|
54 | {
|
---|
55 |
|
---|
56 | *zPtr = f128_roundToInt( *aPtr, roundingMode, exact SOFTFLOAT_STATE_ARG_COMMA );
|
---|
57 |
|
---|
58 | }
|
---|
59 |
|
---|
60 | #else
|
---|
61 |
|
---|
62 | void
|
---|
63 | f128M_roundToInt(
|
---|
64 | const float128_t *aPtr,
|
---|
65 | uint_fast8_t roundingMode,
|
---|
66 | bool exact,
|
---|
67 | float128_t *zPtr
|
---|
68 | SOFTFLOAT_STATE_DECL_COMMA
|
---|
69 | )
|
---|
70 | {
|
---|
71 | const uint32_t *aWPtr;
|
---|
72 | uint32_t *zWPtr;
|
---|
73 | uint32_t ui96;
|
---|
74 | int32_t exp;
|
---|
75 | uint32_t sigExtra;
|
---|
76 | bool sign;
|
---|
77 | uint_fast8_t bitPos;
|
---|
78 | bool roundNear;
|
---|
79 | unsigned int index, lastIndex;
|
---|
80 | bool extra;
|
---|
81 | uint32_t wordA, bit, wordZ;
|
---|
82 | uint_fast8_t carry;
|
---|
83 | uint32_t extrasMask;
|
---|
84 |
|
---|
85 | /*------------------------------------------------------------------------
|
---|
86 | *------------------------------------------------------------------------*/
|
---|
87 | aWPtr = (const uint32_t *) aPtr;
|
---|
88 | zWPtr = (uint32_t *) zPtr;
|
---|
89 | /*------------------------------------------------------------------------
|
---|
90 | *------------------------------------------------------------------------*/
|
---|
91 | ui96 = aWPtr[indexWordHi( 4 )];
|
---|
92 | exp = expF128UI96( ui96 );
|
---|
93 | /*------------------------------------------------------------------------
|
---|
94 | *------------------------------------------------------------------------*/
|
---|
95 | if ( exp < 0x3FFF ) {
|
---|
96 | zWPtr[indexWord( 4, 2 )] = 0;
|
---|
97 | zWPtr[indexWord( 4, 1 )] = 0;
|
---|
98 | zWPtr[indexWord( 4, 0 )] = 0;
|
---|
99 | sigExtra = aWPtr[indexWord( 4, 2 )];
|
---|
100 | if ( !sigExtra ) {
|
---|
101 | sigExtra = aWPtr[indexWord( 4, 1 )] | aWPtr[indexWord( 4, 0 )];
|
---|
102 | }
|
---|
103 | if ( !sigExtra && !(ui96 & 0x7FFFFFFF) ) goto ui96;
|
---|
104 | if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;
|
---|
105 | sign = signF128UI96( ui96 );
|
---|
106 | switch ( roundingMode ) {
|
---|
107 | case softfloat_round_near_even:
|
---|
108 | if ( !fracF128UI96( ui96 ) && !sigExtra ) break;
|
---|
109 | case softfloat_round_near_maxMag:
|
---|
110 | if ( exp == 0x3FFE ) goto mag1;
|
---|
111 | break;
|
---|
112 | case softfloat_round_min:
|
---|
113 | if ( sign ) goto mag1;
|
---|
114 | break;
|
---|
115 | case softfloat_round_max:
|
---|
116 | if ( !sign ) goto mag1;
|
---|
117 | break;
|
---|
118 | #ifdef SOFTFLOAT_ROUND_ODD
|
---|
119 | case softfloat_round_odd:
|
---|
120 | goto mag1;
|
---|
121 | #endif
|
---|
122 | }
|
---|
123 | ui96 = packToF128UI96( sign, 0, 0 );
|
---|
124 | goto ui96;
|
---|
125 | mag1:
|
---|
126 | ui96 = packToF128UI96( sign, 0x3FFF, 0 );
|
---|
127 | goto ui96;
|
---|
128 | }
|
---|
129 | /*------------------------------------------------------------------------
|
---|
130 | *------------------------------------------------------------------------*/
|
---|
131 | if ( 0x406F <= exp ) {
|
---|
132 | if (
|
---|
133 | (exp == 0x7FFF)
|
---|
134 | && (fracF128UI96( ui96 )
|
---|
135 | || (aWPtr[indexWord( 4, 2 )] | aWPtr[indexWord( 4, 1 )]
|
---|
136 | | aWPtr[indexWord( 4, 0 )]))
|
---|
137 | ) {
|
---|
138 | softfloat_propagateNaNF128M( aWPtr, 0, zWPtr SOFTFLOAT_STATE_ARG_COMMA );
|
---|
139 | return;
|
---|
140 | }
|
---|
141 | zWPtr[indexWord( 4, 2 )] = aWPtr[indexWord( 4, 2 )];
|
---|
142 | zWPtr[indexWord( 4, 1 )] = aWPtr[indexWord( 4, 1 )];
|
---|
143 | zWPtr[indexWord( 4, 0 )] = aWPtr[indexWord( 4, 0 )];
|
---|
144 | goto ui96;
|
---|
145 | }
|
---|
146 | /*------------------------------------------------------------------------
|
---|
147 | *------------------------------------------------------------------------*/
|
---|
148 | bitPos = 0x406F - exp;
|
---|
149 | roundNear =
|
---|
150 | (roundingMode == softfloat_round_near_maxMag)
|
---|
151 | || (roundingMode == softfloat_round_near_even);
|
---|
152 | bitPos -= roundNear;
|
---|
153 | index = indexWordLo( 4 );
|
---|
154 | lastIndex = indexWordHi( 4 );
|
---|
155 | extra = 0;
|
---|
156 | for (;;) {
|
---|
157 | wordA = aWPtr[index];
|
---|
158 | if ( bitPos < 32 ) break;
|
---|
159 | if ( wordA ) extra = 1;
|
---|
160 | zWPtr[index] = 0;
|
---|
161 | index += wordIncr;
|
---|
162 | bitPos -= 32;
|
---|
163 | }
|
---|
164 | bit = (uint32_t) 1<<bitPos;
|
---|
165 | if ( roundNear ) {
|
---|
166 | wordZ = wordA + bit;
|
---|
167 | carry = (wordZ < wordA);
|
---|
168 | bit <<= 1;
|
---|
169 | extrasMask = bit - 1;
|
---|
170 | if ( exact && (extra || (wordA & extrasMask)) ) {
|
---|
171 | softfloat_exceptionFlags |= softfloat_flag_inexact;
|
---|
172 | }
|
---|
173 | if (
|
---|
174 | (roundingMode == softfloat_round_near_even)
|
---|
175 | && !extra && !(wordZ & extrasMask)
|
---|
176 | ) {
|
---|
177 | if ( !bit ) {
|
---|
178 | zWPtr[index] = wordZ;
|
---|
179 | index += wordIncr;
|
---|
180 | wordZ = aWPtr[index] + carry;
|
---|
181 | carry &= !wordZ;
|
---|
182 | zWPtr[index] = wordZ & ~1;
|
---|
183 | goto propagateCarry;
|
---|
184 | }
|
---|
185 | wordZ &= ~bit;
|
---|
186 | }
|
---|
187 | } else {
|
---|
188 | wordZ = wordA;
|
---|
189 | carry = 0;
|
---|
190 | extrasMask = bit - 1;
|
---|
191 | if ( extra || (wordA & extrasMask) ) {
|
---|
192 | if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;
|
---|
193 | if (
|
---|
194 | roundingMode
|
---|
195 | == (signF128UI96( ui96 ) ? softfloat_round_min
|
---|
196 | : softfloat_round_max)
|
---|
197 | ) {
|
---|
198 | wordZ += bit;
|
---|
199 | carry = (wordZ < wordA);
|
---|
200 | #ifdef SOFTFLOAT_ROUND_ODD
|
---|
201 | } else if ( roundingMode == softfloat_round_odd ) {
|
---|
202 | wordZ |= bit;
|
---|
203 | #endif
|
---|
204 | }
|
---|
205 | }
|
---|
206 | }
|
---|
207 | wordZ &= ~extrasMask;
|
---|
208 | zWPtr[index] = wordZ;
|
---|
209 | propagateCarry:
|
---|
210 | while ( index != lastIndex ) {
|
---|
211 | index += wordIncr;
|
---|
212 | wordZ = aWPtr[index] + carry;
|
---|
213 | zWPtr[index] = wordZ;
|
---|
214 | carry &= !wordZ;
|
---|
215 | }
|
---|
216 | return;
|
---|
217 | /*------------------------------------------------------------------------
|
---|
218 | *------------------------------------------------------------------------*/
|
---|
219 | ui96:
|
---|
220 | zWPtr[indexWordHi( 4 )] = ui96;
|
---|
221 |
|
---|
222 | }
|
---|
223 |
|
---|
224 | #endif
|
---|
225 |
|
---|