VirtualBox

source: vbox/trunk/src/libs/softfloat-3e/source/f128_roundToInt.c@ 106877

Last change on this file since 106877 was 94558, checked in by vboxsync, 3 years ago

VMM/IEM,libs/softfloat: Don't use global variables in SoftFloat, pass in a state pointer to (almost) all functions instead. Started on fsqrt instruction implementation. bugref:9898

  • Property svn:eol-style set to native
File size: 7.0 KB
Line 
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#include <iprt/cdefs.h> /* VBox: for RT_FALL_THROUGH */
44
45float128_t
46 f128_roundToInt( float128_t a, uint_fast8_t roundingMode, bool exact SOFTFLOAT_STATE_DECL_COMMA )
47{
48 union ui128_f128 uA;
49 uint_fast64_t uiA64, uiA0;
50 int_fast32_t exp;
51 struct uint128 uiZ;
52 uint_fast64_t lastBitMask0, roundBitsMask;
53 bool roundNearEven;
54 uint_fast64_t lastBitMask64;
55 union ui128_f128 uZ;
56
57 /*------------------------------------------------------------------------
58 *------------------------------------------------------------------------*/
59 uA.f = a;
60 uiA64 = uA.ui.v64;
61 uiA0 = uA.ui.v0;
62 exp = expF128UI64( uiA64 );
63 /*------------------------------------------------------------------------
64 *------------------------------------------------------------------------*/
65 if ( 0x402F <= exp ) {
66 /*--------------------------------------------------------------------
67 *--------------------------------------------------------------------*/
68 if ( 0x406F <= exp ) {
69 if ( (exp == 0x7FFF) && (fracF128UI64( uiA64 ) | uiA0) ) {
70 uiZ = softfloat_propagateNaNF128UI( uiA64, uiA0, 0, 0 SOFTFLOAT_STATE_ARG_COMMA );
71 goto uiZ;
72 }
73 return a;
74 }
75 /*--------------------------------------------------------------------
76 *--------------------------------------------------------------------*/
77 lastBitMask0 = (uint_fast64_t) 2<<(0x406E - exp);
78 roundBitsMask = lastBitMask0 - 1;
79 uiZ.v64 = uiA64;
80 uiZ.v0 = uiA0;
81 roundNearEven = (roundingMode == softfloat_round_near_even);
82 if ( roundNearEven || (roundingMode == softfloat_round_near_maxMag) ) {
83 if ( exp == 0x402F ) {
84 if ( UINT64_C( 0x8000000000000000 ) <= uiZ.v0 ) {
85 ++uiZ.v64;
86 if (
87 roundNearEven
88 && (uiZ.v0 == UINT64_C( 0x8000000000000000 ))
89 ) {
90 uiZ.v64 &= ~1;
91 }
92 }
93 } else {
94 uiZ = softfloat_add128( uiZ.v64, uiZ.v0, 0, lastBitMask0>>1 );
95 if ( roundNearEven && !(uiZ.v0 & roundBitsMask) ) {
96 uiZ.v0 &= ~lastBitMask0;
97 }
98 }
99 } else if (
100 roundingMode
101 == (signF128UI64( uiZ.v64 ) ? softfloat_round_min
102 : softfloat_round_max)
103 ) {
104 uiZ = softfloat_add128( uiZ.v64, uiZ.v0, 0, roundBitsMask );
105 }
106 uiZ.v0 &= ~roundBitsMask;
107 lastBitMask64 = !lastBitMask0;
108 } else {
109 /*--------------------------------------------------------------------
110 *--------------------------------------------------------------------*/
111 if ( exp < 0x3FFF ) {
112 if ( !((uiA64 & UINT64_C( 0x7FFFFFFFFFFFFFFF )) | uiA0) ) return a;
113 if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;
114 uiZ.v64 = uiA64 & packToF128UI64( 1, 0, 0 );
115 uiZ.v0 = 0;
116 switch ( roundingMode ) {
117 case softfloat_round_near_even:
118 if ( !(fracF128UI64( uiA64 ) | uiA0) ) break;
119 RT_FALL_THROUGH(); /* VBox */
120 case softfloat_round_near_maxMag:
121 if ( exp == 0x3FFE ) uiZ.v64 |= packToF128UI64( 0, 0x3FFF, 0 );
122 break;
123 case softfloat_round_min:
124 if ( uiZ.v64 ) uiZ.v64 = packToF128UI64( 1, 0x3FFF, 0 );
125 break;
126 case softfloat_round_max:
127 if ( !uiZ.v64 ) uiZ.v64 = packToF128UI64( 0, 0x3FFF, 0 );
128 break;
129#ifdef SOFTFLOAT_ROUND_ODD
130 case softfloat_round_odd:
131 uiZ.v64 |= packToF128UI64( 0, 0x3FFF, 0 );
132 break;
133#endif
134 }
135 goto uiZ;
136 }
137 /*--------------------------------------------------------------------
138 *--------------------------------------------------------------------*/
139 uiZ.v64 = uiA64;
140 uiZ.v0 = 0;
141 lastBitMask64 = (uint_fast64_t) 1<<(0x402F - exp);
142 roundBitsMask = lastBitMask64 - 1;
143 if ( roundingMode == softfloat_round_near_maxMag ) {
144 uiZ.v64 += lastBitMask64>>1;
145 } else if ( roundingMode == softfloat_round_near_even ) {
146 uiZ.v64 += lastBitMask64>>1;
147 if ( !((uiZ.v64 & roundBitsMask) | uiA0) ) {
148 uiZ.v64 &= ~lastBitMask64;
149 }
150 } else if (
151 roundingMode
152 == (signF128UI64( uiZ.v64 ) ? softfloat_round_min
153 : softfloat_round_max)
154 ) {
155 uiZ.v64 = (uiZ.v64 | (uiA0 != 0)) + roundBitsMask;
156 }
157 uiZ.v64 &= ~roundBitsMask;
158 lastBitMask0 = 0;
159 }
160 if ( (uiZ.v64 != uiA64) || (uiZ.v0 != uiA0) ) {
161#ifdef SOFTFLOAT_ROUND_ODD
162 if ( roundingMode == softfloat_round_odd ) {
163 uiZ.v64 |= lastBitMask64;
164 uiZ.v0 |= lastBitMask0;
165 }
166#endif
167 if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;
168 }
169 uiZ:
170 uZ.ui = uiZ;
171 return uZ.f;
172
173}
174
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette