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 | extF80M_roundToInt(
|
---|
48 | const extFloat80_t *aPtr,
|
---|
49 | uint_fast8_t roundingMode,
|
---|
50 | bool exact,
|
---|
51 | extFloat80_t *zPtr
|
---|
52 | SOFTFLOAT_STATE_DECL_COMMA
|
---|
53 | )
|
---|
54 | {
|
---|
55 |
|
---|
56 | *zPtr = extF80_roundToInt( *aPtr, roundingMode, exact SOFTFLOAT_STATE_ARG_COMMA );
|
---|
57 |
|
---|
58 | }
|
---|
59 |
|
---|
60 | #else
|
---|
61 |
|
---|
62 | void
|
---|
63 | extF80M_roundToInt(
|
---|
64 | const extFloat80_t *aPtr,
|
---|
65 | uint_fast8_t roundingMode,
|
---|
66 | bool exact,
|
---|
67 | extFloat80_t *zPtr
|
---|
68 | SOFTFLOAT_STATE_DECL_COMMA
|
---|
69 | )
|
---|
70 | {
|
---|
71 | const struct extFloat80M *aSPtr;
|
---|
72 | struct extFloat80M *zSPtr;
|
---|
73 | uint_fast16_t uiA64, signUI64;
|
---|
74 | int32_t exp;
|
---|
75 | uint64_t sigA;
|
---|
76 | uint_fast16_t uiZ64;
|
---|
77 | uint64_t sigZ, lastBitMask, roundBitsMask;
|
---|
78 |
|
---|
79 | /*------------------------------------------------------------------------
|
---|
80 | *------------------------------------------------------------------------*/
|
---|
81 | aSPtr = (const struct extFloat80M *) aPtr;
|
---|
82 | zSPtr = (struct extFloat80M *) zPtr;
|
---|
83 | /*------------------------------------------------------------------------
|
---|
84 | *------------------------------------------------------------------------*/
|
---|
85 | uiA64 = aSPtr->signExp;
|
---|
86 | signUI64 = uiA64 & packToExtF80UI64( 1, 0 );
|
---|
87 | exp = expExtF80UI64( uiA64 );
|
---|
88 | sigA = aSPtr->signif;
|
---|
89 | /*------------------------------------------------------------------------
|
---|
90 | *------------------------------------------------------------------------*/
|
---|
91 | if ( !(sigA & UINT64_C( 0x8000000000000000 )) && (exp != 0x7FFF) ) {
|
---|
92 | if ( !sigA ) {
|
---|
93 | uiZ64 = signUI64;
|
---|
94 | sigZ = 0;
|
---|
95 | goto uiZ;
|
---|
96 | }
|
---|
97 | exp += softfloat_normExtF80SigM( &sigA );
|
---|
98 | }
|
---|
99 | /*------------------------------------------------------------------------
|
---|
100 | *------------------------------------------------------------------------*/
|
---|
101 | if ( exp <= 0x3FFE ) {
|
---|
102 | if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;
|
---|
103 | switch ( roundingMode ) {
|
---|
104 | case softfloat_round_near_even:
|
---|
105 | if ( !(sigA & UINT64_C( 0x7FFFFFFFFFFFFFFF )) ) break;
|
---|
106 | case softfloat_round_near_maxMag:
|
---|
107 | if ( exp == 0x3FFE ) goto mag1;
|
---|
108 | break;
|
---|
109 | case softfloat_round_min:
|
---|
110 | if ( signUI64 ) goto mag1;
|
---|
111 | break;
|
---|
112 | case softfloat_round_max:
|
---|
113 | if ( !signUI64 ) goto mag1;
|
---|
114 | break;
|
---|
115 | #ifdef SOFTFLOAT_ROUND_ODD
|
---|
116 | case softfloat_round_odd:
|
---|
117 | goto mag1;
|
---|
118 | #endif
|
---|
119 | }
|
---|
120 | uiZ64 = signUI64;
|
---|
121 | sigZ = 0;
|
---|
122 | goto uiZ;
|
---|
123 | mag1:
|
---|
124 | uiZ64 = signUI64 | 0x3FFF;
|
---|
125 | sigZ = UINT64_C( 0x8000000000000000 );
|
---|
126 | goto uiZ;
|
---|
127 | }
|
---|
128 | /*------------------------------------------------------------------------
|
---|
129 | *------------------------------------------------------------------------*/
|
---|
130 | if ( 0x403E <= exp ) {
|
---|
131 | if ( exp == 0x7FFF ) {
|
---|
132 | if ( sigA & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) {
|
---|
133 | softfloat_propagateNaNExtF80M( aSPtr, 0, zSPtr SOFTFLOAT_STATE_ARG_COMMA );
|
---|
134 | return;
|
---|
135 | }
|
---|
136 | sigZ = UINT64_C( 0x8000000000000000 );
|
---|
137 | } else {
|
---|
138 | sigZ = sigA;
|
---|
139 | }
|
---|
140 | uiZ64 = signUI64 | exp;
|
---|
141 | goto uiZ;
|
---|
142 | }
|
---|
143 | /*------------------------------------------------------------------------
|
---|
144 | *------------------------------------------------------------------------*/
|
---|
145 | uiZ64 = signUI64 | exp;
|
---|
146 | lastBitMask = (uint64_t) 1<<(0x403E - exp);
|
---|
147 | roundBitsMask = lastBitMask - 1;
|
---|
148 | sigZ = sigA;
|
---|
149 | if ( roundingMode == softfloat_round_near_maxMag ) {
|
---|
150 | sigZ += lastBitMask>>1;
|
---|
151 | } else if ( roundingMode == softfloat_round_near_even ) {
|
---|
152 | sigZ += lastBitMask>>1;
|
---|
153 | if ( !(sigZ & roundBitsMask) ) sigZ &= ~lastBitMask;
|
---|
154 | } else if (
|
---|
155 | roundingMode == (signUI64 ? softfloat_round_min : softfloat_round_max)
|
---|
156 | ) {
|
---|
157 | sigZ += roundBitsMask;
|
---|
158 | }
|
---|
159 | sigZ &= ~roundBitsMask;
|
---|
160 | if ( !sigZ ) {
|
---|
161 | ++uiZ64;
|
---|
162 | sigZ = UINT64_C( 0x8000000000000000 );
|
---|
163 | }
|
---|
164 | if ( sigZ != sigA ) {
|
---|
165 | #ifdef SOFTFLOAT_ROUND_ODD
|
---|
166 | if ( roundingMode == softfloat_round_odd ) sigZ |= lastBitMask;
|
---|
167 | #endif
|
---|
168 | if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;
|
---|
169 | }
|
---|
170 | uiZ:
|
---|
171 | zSPtr->signExp = uiZ64;
|
---|
172 | zSPtr->signif = sigZ;
|
---|
173 | return;
|
---|
174 |
|
---|
175 | }
|
---|
176 |
|
---|
177 | #endif
|
---|
178 |
|
---|