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