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 "softfloat.h"
|
---|
42 |
|
---|
43 | void
|
---|
44 | softfloat_roundPackMToF128M(
|
---|
45 | bool sign, int32_t exp, uint32_t *extSigPtr, uint32_t *zWPtr SOFTFLOAT_STATE_DECL_COMMA )
|
---|
46 | {
|
---|
47 | uint_fast8_t roundingMode;
|
---|
48 | bool roundNearEven;
|
---|
49 | uint32_t sigExtra;
|
---|
50 | bool doIncrement, isTiny;
|
---|
51 | static const uint32_t maxSig[4] =
|
---|
52 | INIT_UINTM4( 0x0001FFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF );
|
---|
53 | uint32_t ui, uj;
|
---|
54 |
|
---|
55 | /*------------------------------------------------------------------------
|
---|
56 | *------------------------------------------------------------------------*/
|
---|
57 | roundingMode = softfloat_roundingMode;
|
---|
58 | roundNearEven = (roundingMode == softfloat_round_near_even);
|
---|
59 | sigExtra = extSigPtr[indexWordLo( 5 )];
|
---|
60 | doIncrement = (0x80000000 <= sigExtra);
|
---|
61 | if ( ! roundNearEven && (roundingMode != softfloat_round_near_maxMag) ) {
|
---|
62 | doIncrement =
|
---|
63 | (roundingMode
|
---|
64 | == (sign ? softfloat_round_min : softfloat_round_max))
|
---|
65 | && sigExtra;
|
---|
66 | }
|
---|
67 | /*------------------------------------------------------------------------
|
---|
68 | *------------------------------------------------------------------------*/
|
---|
69 | if ( 0x7FFD <= (uint32_t) exp ) {
|
---|
70 | if ( exp < 0 ) {
|
---|
71 | /*----------------------------------------------------------------
|
---|
72 | *----------------------------------------------------------------*/
|
---|
73 | isTiny =
|
---|
74 | (softfloat_detectTininess
|
---|
75 | == softfloat_tininess_beforeRounding)
|
---|
76 | || (exp < -1)
|
---|
77 | || ! doIncrement
|
---|
78 | || (softfloat_compare128M(
|
---|
79 | extSigPtr + indexMultiwordHi( 5, 4 ), maxSig )
|
---|
80 | < 0);
|
---|
81 | softfloat_shiftRightJam160M( extSigPtr, -exp, extSigPtr );
|
---|
82 | exp = 0;
|
---|
83 | sigExtra = extSigPtr[indexWordLo( 5 )];
|
---|
84 | if ( isTiny && sigExtra ) {
|
---|
85 | softfloat_raiseFlags( softfloat_flag_underflow SOFTFLOAT_STATE_ARG_COMMA );
|
---|
86 | }
|
---|
87 | doIncrement = (0x80000000 <= sigExtra);
|
---|
88 | if (
|
---|
89 | ! roundNearEven
|
---|
90 | && (roundingMode != softfloat_round_near_maxMag)
|
---|
91 | ) {
|
---|
92 | doIncrement =
|
---|
93 | (roundingMode
|
---|
94 | == (sign ? softfloat_round_min : softfloat_round_max))
|
---|
95 | && sigExtra;
|
---|
96 | }
|
---|
97 | } else if (
|
---|
98 | (0x7FFD < exp)
|
---|
99 | || ((exp == 0x7FFD) && doIncrement
|
---|
100 | && (softfloat_compare128M(
|
---|
101 | extSigPtr + indexMultiwordHi( 5, 4 ), maxSig )
|
---|
102 | == 0))
|
---|
103 | ) {
|
---|
104 | /*----------------------------------------------------------------
|
---|
105 | *----------------------------------------------------------------*/
|
---|
106 | softfloat_raiseFlags(
|
---|
107 | softfloat_flag_overflow | softfloat_flag_inexact
|
---|
108 | SOFTFLOAT_STATE_ARG_COMMA );
|
---|
109 | if (
|
---|
110 | roundNearEven
|
---|
111 | || (roundingMode == softfloat_round_near_maxMag)
|
---|
112 | || (roundingMode
|
---|
113 | == (sign ? softfloat_round_min : softfloat_round_max))
|
---|
114 | ) {
|
---|
115 | ui = packToF128UI96( sign, 0x7FFF, 0 );
|
---|
116 | uj = 0;
|
---|
117 | } else {
|
---|
118 | ui = packToF128UI96( sign, 0x7FFE, 0x0000FFFF );
|
---|
119 | uj = 0xFFFFFFFF;
|
---|
120 | }
|
---|
121 | zWPtr[indexWordHi( 4 )] = ui;
|
---|
122 | zWPtr[indexWord( 4, 2 )] = uj;
|
---|
123 | zWPtr[indexWord( 4, 1 )] = uj;
|
---|
124 | zWPtr[indexWord( 4, 0 )] = uj;
|
---|
125 | return;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | /*------------------------------------------------------------------------
|
---|
129 | *------------------------------------------------------------------------*/
|
---|
130 | uj = extSigPtr[indexWord( 5, 1 )];
|
---|
131 | if ( sigExtra ) {
|
---|
132 | softfloat_exceptionFlags |= softfloat_flag_inexact;
|
---|
133 | #ifdef SOFTFLOAT_ROUND_ODD
|
---|
134 | if ( roundingMode == softfloat_round_odd ) {
|
---|
135 | uj |= 1;
|
---|
136 | goto noIncrementPackReturn;
|
---|
137 | }
|
---|
138 | #endif
|
---|
139 | }
|
---|
140 | if ( doIncrement ) {
|
---|
141 | ++uj;
|
---|
142 | if ( uj ) {
|
---|
143 | if ( ! (sigExtra & 0x7FFFFFFF) && roundNearEven ) uj &= ~1;
|
---|
144 | zWPtr[indexWord( 4, 2 )] = extSigPtr[indexWord( 5, 3 )];
|
---|
145 | zWPtr[indexWord( 4, 1 )] = extSigPtr[indexWord( 5, 2 )];
|
---|
146 | zWPtr[indexWord( 4, 0 )] = uj;
|
---|
147 | ui = extSigPtr[indexWordHi( 5 )];
|
---|
148 | } else {
|
---|
149 | zWPtr[indexWord( 4, 0 )] = uj;
|
---|
150 | ui = extSigPtr[indexWord( 5, 2 )] + 1;
|
---|
151 | zWPtr[indexWord( 4, 1 )] = ui;
|
---|
152 | uj = extSigPtr[indexWord( 5, 3 )];
|
---|
153 | if ( ui ) {
|
---|
154 | zWPtr[indexWord( 4, 2 )] = uj;
|
---|
155 | ui = extSigPtr[indexWordHi( 5 )];
|
---|
156 | } else {
|
---|
157 | ++uj;
|
---|
158 | zWPtr[indexWord( 4, 2 )] = uj;
|
---|
159 | ui = extSigPtr[indexWordHi( 5 )];
|
---|
160 | if ( ! uj ) ++ui;
|
---|
161 | }
|
---|
162 | }
|
---|
163 | } else {
|
---|
164 | noIncrementPackReturn:
|
---|
165 | zWPtr[indexWord( 4, 0 )] = uj;
|
---|
166 | ui = extSigPtr[indexWord( 5, 2 )];
|
---|
167 | zWPtr[indexWord( 4, 1 )] = ui;
|
---|
168 | uj |= ui;
|
---|
169 | ui = extSigPtr[indexWord( 5, 3 )];
|
---|
170 | zWPtr[indexWord( 4, 2 )] = ui;
|
---|
171 | uj |= ui;
|
---|
172 | ui = extSigPtr[indexWordHi( 5 )];
|
---|
173 | uj |= ui;
|
---|
174 | if ( ! uj ) exp = 0;
|
---|
175 | }
|
---|
176 | zWPtr[indexWordHi( 4 )] = packToF128UI96( sign, exp, ui );
|
---|
177 |
|
---|
178 | }
|
---|
179 |
|
---|