1 2 /*============================================================================ 3 4 This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic 5 Package, Release 3a, by John R. Hauser. 6 7 Copyright 2011, 2012, 2013, 2014 The Regents of the University of California. 8 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 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 lastBitMask, roundBitsMask; 52 bool roundNearEven; 53 union ui128_f128 uZ; 54 55 /*------------------------------------------------------------------------ 56 *------------------------------------------------------------------------*/ 57 uA.f = a; 58 uiA64 = uA.ui.v64; 59 uiA0 = uA.ui.v0; 60 exp = expF128UI64( uiA64 ); 61 /*------------------------------------------------------------------------ 62 *------------------------------------------------------------------------*/ 63 if ( 0x402F <= exp ) { 64 /*-------------------------------------------------------------------- 65 *--------------------------------------------------------------------*/ 66 if ( 0x406F <= exp ) { 67 if ( (exp == 0x7FFF) && (fracF128UI64( uiA64 ) | uiA0) ) { 68 uiZ = softfloat_propagateNaNF128UI( uiA64, uiA0, 0, 0 ); 69 goto uiZ; 70 } 71 return a; 72 } 73 /*-------------------------------------------------------------------- 74 *--------------------------------------------------------------------*/ 75 lastBitMask = (uint_fast64_t) 2<<(0x406E - exp); 76 roundBitsMask = lastBitMask - 1; 77 uiZ.v64 = uiA64; 78 uiZ.v0 = uiA0; 79 roundNearEven = (roundingMode == softfloat_round_near_even); 80 if ( roundNearEven || (roundingMode == softfloat_round_near_maxMag) ) { 81 if ( exp == 0x402F ) { 82 if ( UINT64_C( 0x8000000000000000 ) <= uiZ.v0 ) { 83 ++uiZ.v64; 84 if ( 85 roundNearEven 86 && (uiZ.v0 == UINT64_C( 0x8000000000000000 )) 87 ) { 88 uiZ.v64 &= ~1; 89 } 90 } 91 } else { 92 uiZ = softfloat_add128( uiZ.v64, uiZ.v0, 0, lastBitMask>>1 ); 93 if ( roundNearEven && ! (uiZ.v0 & roundBitsMask) ) { 94 uiZ.v0 &= ~lastBitMask; 95 } 96 } 97 } else if ( roundingMode != softfloat_round_minMag ) { 98 if ( 99 signF128UI64( uiZ.v64 ) ^ (roundingMode == softfloat_round_max) 100 ) { 101 uiZ = softfloat_add128( uiZ.v64, uiZ.v0, 0, roundBitsMask ); 102 } 103 } 104 uiZ.v0 &= ~roundBitsMask; 105 } else { 106 /*-------------------------------------------------------------------- 107 *--------------------------------------------------------------------*/ 108 if ( exp < 0x3FFF ) { 109 if ( ! ((uiA64 & UINT64_C( 0x7FFFFFFFFFFFFFFF )) | uiA0) ) { 110 return a; 111 } 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 } 128 goto uiZ; 129 } 130 /*-------------------------------------------------------------------- 131 *--------------------------------------------------------------------*/ 132 uiZ.v64 = uiA64; 133 uiZ.v0 = 0; 134 lastBitMask = (uint_fast64_t) 1<<(0x402F - exp); 135 roundBitsMask = lastBitMask - 1; 136 if ( roundingMode == softfloat_round_near_maxMag ) { 137 uiZ.v64 += lastBitMask>>1; 138 } else if ( roundingMode == softfloat_round_near_even ) { 139 uiZ.v64 += lastBitMask>>1; 140 if ( ! ((uiZ.v64 & roundBitsMask) | uiA0) ) { 141 uiZ.v64 &= ~lastBitMask; 142 } 143 } else if ( roundingMode != softfloat_round_minMag ) { 144 if ( 145 signF128UI64( uiZ.v64 ) ^ (roundingMode == softfloat_round_max) 146 ) { 147 uiZ.v64 = (uiZ.v64 | (uiA0 != 0)) + roundBitsMask; 148 } 149 } 150 uiZ.v64 &= ~roundBitsMask; 151 } 152 if ( exact && ((uiZ.v64 != uiA64) || (uiZ.v0 != uiA0)) ) { 153 softfloat_exceptionFlags |= softfloat_flag_inexact; 154 } 155 uiZ: 156 uZ.ui = uiZ; 157 return uZ.f; 158 159 } 160 161