Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
extF80_roundToInt.c
Go to the documentation of this file.
1
2/*============================================================================
3
4This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
5Package, Release 3e, by John R. Hauser.
6
7Copyright 2011, 2012, 2013, 2014, 2017 The Regents of the University of
8California. All rights reserved.
9
10Redistribution and use in source and binary forms, with or without
11modification, 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
24THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
25EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
27DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
28DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31ON 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
33SOFTWARE, 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
45 extF80_roundToInt( extFloat80_t a, uint_fast8_t roundingMode, bool exact )
46{
47 union { struct extFloat80M s; extFloat80_t f; } uA;
48 uint_fast16_t uiA64, signUI64;
49 int_fast32_t exp;
50 uint_fast64_t sigA;
51 uint_fast16_t uiZ64;
52 uint_fast64_t sigZ;
53 struct exp32_sig64 normExpSig;
54 struct uint128 uiZ;
55 uint_fast64_t lastBitMask, roundBitsMask;
56 union { struct extFloat80M s; extFloat80_t f; } uZ;
57
58 /*------------------------------------------------------------------------
59 *------------------------------------------------------------------------*/
60 uA.f = a;
61 uiA64 = uA.s.signExp;
62 signUI64 = uiA64 & packToExtF80UI64( 1, 0 );
63 exp = expExtF80UI64( uiA64 );
64 sigA = uA.s.signif;
65 /*------------------------------------------------------------------------
66 *------------------------------------------------------------------------*/
67 if ( !(sigA & UINT64_C( 0x8000000000000000 )) && (exp != 0x7FFF) ) {
68 if ( !sigA ) {
69 uiZ64 = signUI64;
70 sigZ = 0;
71 goto uiZ;
72 }
73 normExpSig = softfloat_normSubnormalExtF80Sig( sigA );
74 exp += normExpSig.exp;
75 sigA = normExpSig.sig;
76 }
77 /*------------------------------------------------------------------------
78 *------------------------------------------------------------------------*/
79 if ( 0x403E <= exp ) {
80 if ( exp == 0x7FFF ) {
81 if ( sigA & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) {
82 uiZ = softfloat_propagateNaNExtF80UI( uiA64, sigA, 0, 0 );
83 uiZ64 = uiZ.v64;
84 sigZ = uiZ.v0;
85 goto uiZ;
86 }
87 sigZ = UINT64_C( 0x8000000000000000 );
88 } else {
89 sigZ = sigA;
90 }
91 uiZ64 = signUI64 | exp;
92 goto uiZ;
93 }
94 if ( exp <= 0x3FFE ) {
96 switch ( roundingMode ) {
98 if ( !(sigA & UINT64_C( 0x7FFFFFFFFFFFFFFF )) ) break;
100 if ( exp == 0x3FFE ) goto mag1;
101 break;
103 if ( signUI64 ) goto mag1;
104 break;
106 if ( !signUI64 ) goto mag1;
107 break;
108#ifdef SOFTFLOAT_ROUND_ODD
110 goto mag1;
111#endif
112 }
113 uiZ64 = signUI64;
114 sigZ = 0;
115 goto uiZ;
116 mag1:
117 uiZ64 = signUI64 | 0x3FFF;
118 sigZ = UINT64_C( 0x8000000000000000 );
119 goto uiZ;
120 }
121 /*------------------------------------------------------------------------
122 *------------------------------------------------------------------------*/
123 uiZ64 = signUI64 | exp;
124 lastBitMask = (uint_fast64_t) 1<<(0x403E - exp);
125 roundBitsMask = lastBitMask - 1;
126 sigZ = sigA;
127 if ( roundingMode == softfloat_round_near_maxMag ) {
128 sigZ += lastBitMask>>1;
129 } else if ( roundingMode == softfloat_round_near_even ) {
130 sigZ += lastBitMask>>1;
131 if ( !(sigZ & roundBitsMask) ) sigZ &= ~lastBitMask;
132 } else if (
133 roundingMode == (signUI64 ? softfloat_round_min : softfloat_round_max)
134 ) {
135 sigZ += roundBitsMask;
136 }
137 sigZ &= ~roundBitsMask;
138 if ( !sigZ ) {
139 ++uiZ64;
140 sigZ = UINT64_C( 0x8000000000000000 );
141 }
142 if ( sigZ != sigA ) {
143#ifdef SOFTFLOAT_ROUND_ODD
144 if ( roundingMode == softfloat_round_odd ) sigZ |= lastBitMask;
145#endif
147 }
148 uiZ:
149 uZ.s.signExp = uiZ64;
150 uZ.s.signif = sigZ;
151 return uZ.f;
152
153}
154
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
struct uint128 softfloat_propagateNaNExtF80UI(uint_fast16_t uiA64, uint_fast64_t uiA0, uint_fast16_t uiB64, uint_fast64_t uiB0)
extFloat80_t extF80_roundToInt(extFloat80_t a, uint_fast8_t roundingMode, bool exact)
#define packToExtF80UI64(sign, exp)
Definition internals.h:148
#define expExtF80UI64(a64)
Definition internals.h:147
@ softfloat_flag_inexact
Definition softfloat.h:85
THREAD_LOCAL uint_fast8_t softfloat_exceptionFlags
@ softfloat_round_odd
Definition softfloat.h:77
@ softfloat_round_max
Definition softfloat.h:75
@ softfloat_round_min
Definition softfloat.h:74
@ softfloat_round_near_even
Definition softfloat.h:72
@ softfloat_round_near_maxMag
Definition softfloat.h:76
struct exp32_sig64 softfloat_normSubnormalExtF80Sig(uint_fast64_t sig)
uint16_t uint_fast16_t
Definition stdint.h:155
uint64_t uint_fast64_t
Definition stdint.h:157
#define UINT64_C(val)
Definition stdint.h:284
int32_t int_fast32_t
Definition stdint.h:152
uint8_t uint_fast8_t
Definition stdint.h:154
char * s