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, 2015, 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 "softfloat.h"
|
---|
42 |
|
---|
43 | void
|
---|
44 | softfloat_roundPackMToExtF80M(
|
---|
45 | bool sign,
|
---|
46 | int32_t exp,
|
---|
47 | uint32_t *extSigPtr,
|
---|
48 | uint_fast8_t roundingPrecision,
|
---|
49 | struct extFloat80M *zSPtr
|
---|
50 | SOFTFLOAT_STATE_DECL_COMMA
|
---|
51 | )
|
---|
52 | {
|
---|
53 | uint_fast8_t roundingMode;
|
---|
54 | bool roundNearEven;
|
---|
55 | uint64_t sig, roundIncrement, roundMask, roundBits;
|
---|
56 | bool isTiny;
|
---|
57 | uint32_t sigExtra;
|
---|
58 | bool doIncrement;
|
---|
59 |
|
---|
60 | /*------------------------------------------------------------------------
|
---|
61 | *------------------------------------------------------------------------*/
|
---|
62 | roundingMode = softfloat_roundingMode;
|
---|
63 | roundNearEven = (roundingMode == softfloat_round_near_even);
|
---|
64 | sig =
|
---|
65 | (uint64_t) extSigPtr[indexWord( 3, 2 )]<<32
|
---|
66 | | extSigPtr[indexWord( 3, 1 )];
|
---|
67 | if ( roundingPrecision == 80 ) goto precision80;
|
---|
68 | if ( roundingPrecision == 64 ) {
|
---|
69 | roundIncrement = UINT64_C( 0x0000000000000400 );
|
---|
70 | roundMask = UINT64_C( 0x00000000000007FF );
|
---|
71 | } else if ( roundingPrecision == 32 ) {
|
---|
72 | roundIncrement = UINT64_C( 0x0000008000000000 );
|
---|
73 | roundMask = UINT64_C( 0x000000FFFFFFFFFF );
|
---|
74 | } else {
|
---|
75 | goto precision80;
|
---|
76 | }
|
---|
77 | /*------------------------------------------------------------------------
|
---|
78 | *------------------------------------------------------------------------*/
|
---|
79 | if ( extSigPtr[indexWordLo( 3 )] ) sig |= 1;
|
---|
80 | if ( ! roundNearEven && (roundingMode != softfloat_round_near_maxMag) ) {
|
---|
81 | roundIncrement =
|
---|
82 | (roundingMode
|
---|
83 | == (sign ? softfloat_round_min : softfloat_round_max))
|
---|
84 | ? roundMask
|
---|
85 | : 0;
|
---|
86 | }
|
---|
87 | roundBits = sig & roundMask;
|
---|
88 | /*------------------------------------------------------------------------
|
---|
89 | *------------------------------------------------------------------------*/
|
---|
90 | if ( 0x7FFD <= (uint32_t) (exp - 1) ) {
|
---|
91 | if ( exp <= 0 ) {
|
---|
92 | /*----------------------------------------------------------------
|
---|
93 | *----------------------------------------------------------------*/
|
---|
94 | isTiny =
|
---|
95 | (softfloat_detectTininess
|
---|
96 | == softfloat_tininess_beforeRounding)
|
---|
97 | || (exp < 0)
|
---|
98 | || (sig <= (uint64_t) (sig + roundIncrement));
|
---|
99 | sig = softfloat_shiftRightJam64( sig, 1 - exp );
|
---|
100 | roundBits = sig & roundMask;
|
---|
101 | if ( roundBits ) {
|
---|
102 | if ( isTiny ) softfloat_raiseFlags( softfloat_flag_underflow SOFTFLOAT_STATE_ARG_COMMA );
|
---|
103 | softfloat_exceptionFlags |= softfloat_flag_inexact;
|
---|
104 | #ifdef SOFTFLOAT_ROUND_ODD
|
---|
105 | if ( roundingMode == softfloat_round_odd ) {
|
---|
106 | sig |= roundMask + 1;
|
---|
107 | }
|
---|
108 | #endif
|
---|
109 | }
|
---|
110 | sig += roundIncrement;
|
---|
111 | exp = ((sig & UINT64_C( 0x8000000000000000 )) != 0);
|
---|
112 | roundIncrement = roundMask + 1;
|
---|
113 | if ( roundNearEven && (roundBits<<1 == roundIncrement) ) {
|
---|
114 | roundMask |= roundIncrement;
|
---|
115 | }
|
---|
116 | sig &= ~roundMask;
|
---|
117 | goto packReturn;
|
---|
118 | }
|
---|
119 | if (
|
---|
120 | (0x7FFE < exp)
|
---|
121 | || ((exp == 0x7FFE) && ((uint64_t) (sig + roundIncrement) < sig))
|
---|
122 | ) {
|
---|
123 | goto overflow;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | /*------------------------------------------------------------------------
|
---|
127 | *------------------------------------------------------------------------*/
|
---|
128 | if ( roundBits ) {
|
---|
129 | softfloat_exceptionFlags |= softfloat_flag_inexact;
|
---|
130 | #ifdef SOFTFLOAT_ROUND_ODD
|
---|
131 | if ( roundingMode == softfloat_round_odd ) {
|
---|
132 | sig = (sig & ~roundMask) | (roundMask + 1);
|
---|
133 | goto packReturn;
|
---|
134 | }
|
---|
135 | #endif
|
---|
136 | }
|
---|
137 | sig += roundIncrement;
|
---|
138 | if ( sig < roundIncrement ) {
|
---|
139 | ++exp;
|
---|
140 | sig = UINT64_C( 0x8000000000000000 );
|
---|
141 | }
|
---|
142 | roundIncrement = roundMask + 1;
|
---|
143 | if ( roundNearEven && (roundBits<<1 == roundIncrement) ) {
|
---|
144 | roundMask |= roundIncrement;
|
---|
145 | }
|
---|
146 | sig &= ~roundMask;
|
---|
147 | goto packReturn;
|
---|
148 | /*------------------------------------------------------------------------
|
---|
149 | *------------------------------------------------------------------------*/
|
---|
150 | precision80:
|
---|
151 | sigExtra = extSigPtr[indexWordLo( 3 )];
|
---|
152 | doIncrement = (0x80000000 <= sigExtra);
|
---|
153 | if ( ! roundNearEven && (roundingMode != softfloat_round_near_maxMag) ) {
|
---|
154 | doIncrement =
|
---|
155 | (roundingMode
|
---|
156 | == (sign ? softfloat_round_min : softfloat_round_max))
|
---|
157 | && sigExtra;
|
---|
158 | }
|
---|
159 | /*------------------------------------------------------------------------
|
---|
160 | *------------------------------------------------------------------------*/
|
---|
161 | if ( 0x7FFD <= (uint32_t) (exp - 1) ) {
|
---|
162 | if ( exp <= 0 ) {
|
---|
163 | /*----------------------------------------------------------------
|
---|
164 | *----------------------------------------------------------------*/
|
---|
165 | isTiny =
|
---|
166 | (softfloat_detectTininess
|
---|
167 | == softfloat_tininess_beforeRounding)
|
---|
168 | || (exp < 0)
|
---|
169 | || ! doIncrement
|
---|
170 | || (sig < UINT64_C( 0xFFFFFFFFFFFFFFFF ));
|
---|
171 | softfloat_shiftRightJam96M( extSigPtr, 1 - exp, extSigPtr );
|
---|
172 | exp = 0;
|
---|
173 | sig =
|
---|
174 | (uint64_t) extSigPtr[indexWord( 3, 2 )]<<32
|
---|
175 | | extSigPtr[indexWord( 3, 1 )];
|
---|
176 | sigExtra = extSigPtr[indexWordLo( 3 )];
|
---|
177 | if ( sigExtra ) {
|
---|
178 | if ( isTiny ) softfloat_raiseFlags( softfloat_flag_underflow SOFTFLOAT_STATE_ARG_COMMA );
|
---|
179 | softfloat_exceptionFlags |= softfloat_flag_inexact;
|
---|
180 | #ifdef SOFTFLOAT_ROUND_ODD
|
---|
181 | if ( roundingMode == softfloat_round_odd ) {
|
---|
182 | sig |= 1;
|
---|
183 | goto packReturn;
|
---|
184 | }
|
---|
185 | #endif
|
---|
186 | }
|
---|
187 | doIncrement = (0x80000000 <= sigExtra);
|
---|
188 | if (
|
---|
189 | ! roundNearEven
|
---|
190 | && (roundingMode != softfloat_round_near_maxMag)
|
---|
191 | ) {
|
---|
192 | doIncrement =
|
---|
193 | (roundingMode
|
---|
194 | == (sign ? softfloat_round_min : softfloat_round_max))
|
---|
195 | && sigExtra;
|
---|
196 | }
|
---|
197 | if ( doIncrement ) {
|
---|
198 | ++sig;
|
---|
199 | sig &= ~(uint64_t) (! (sigExtra & 0x7FFFFFFF) & roundNearEven);
|
---|
200 | exp = ((sig & UINT64_C( 0x8000000000000000 )) != 0);
|
---|
201 | }
|
---|
202 | goto packReturn;
|
---|
203 | }
|
---|
204 | if (
|
---|
205 | (0x7FFE < exp)
|
---|
206 | || ((exp == 0x7FFE) && (sig == UINT64_C( 0xFFFFFFFFFFFFFFFF ))
|
---|
207 | && doIncrement)
|
---|
208 | ) {
|
---|
209 | /*----------------------------------------------------------------
|
---|
210 | *----------------------------------------------------------------*/
|
---|
211 | roundMask = 0;
|
---|
212 | overflow:
|
---|
213 | softfloat_raiseFlags(
|
---|
214 | softfloat_flag_overflow | softfloat_flag_inexact
|
---|
215 | SOFTFLOAT_STATE_ARG_COMMA );
|
---|
216 | if (
|
---|
217 | roundNearEven
|
---|
218 | || (roundingMode == softfloat_round_near_maxMag)
|
---|
219 | || (roundingMode
|
---|
220 | == (sign ? softfloat_round_min : softfloat_round_max))
|
---|
221 | ) {
|
---|
222 | exp = 0x7FFF;
|
---|
223 | sig = UINT64_C( 0x8000000000000000 );
|
---|
224 | } else {
|
---|
225 | exp = 0x7FFE;
|
---|
226 | sig = ~roundMask;
|
---|
227 | }
|
---|
228 | goto packReturn;
|
---|
229 | }
|
---|
230 | }
|
---|
231 | /*------------------------------------------------------------------------
|
---|
232 | *------------------------------------------------------------------------*/
|
---|
233 | if ( sigExtra ) {
|
---|
234 | softfloat_exceptionFlags |= softfloat_flag_inexact;
|
---|
235 | #ifdef SOFTFLOAT_ROUND_ODD
|
---|
236 | if ( roundingMode == softfloat_round_odd ) {
|
---|
237 | sig |= 1;
|
---|
238 | goto packReturn;
|
---|
239 | }
|
---|
240 | #endif
|
---|
241 | }
|
---|
242 | if ( doIncrement ) {
|
---|
243 | ++sig;
|
---|
244 | if ( ! sig ) {
|
---|
245 | ++exp;
|
---|
246 | sig = UINT64_C( 0x8000000000000000 );
|
---|
247 | } else {
|
---|
248 | sig &= ~(uint64_t) (! (sigExtra & 0x7FFFFFFF) & roundNearEven);
|
---|
249 | }
|
---|
250 | }
|
---|
251 | /*------------------------------------------------------------------------
|
---|
252 | *------------------------------------------------------------------------*/
|
---|
253 | packReturn:
|
---|
254 | zSPtr->signExp = packToExtF80UI64( sign, exp );
|
---|
255 | zSPtr->signif = sig;
|
---|
256 |
|
---|
257 | }
|
---|
258 |
|
---|