1 // SPDX-License-Identifier: BSD-3-Clause 2 3 /*============================================================================ 4 5 This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic 6 Package, Release 3a, by John R. Hauser. 7 8 Copyright 2011, 2012, 2013, 2014 The Regents of the University of California. 9 All rights reserved. 10 11 Redistribution and use in source and binary forms, with or without 12 modification, are permitted provided that the following conditions are met: 13 14 1. Redistributions of source code must retain the above copyright notice, 15 this list of conditions, and the following disclaimer. 16 17 2. Redistributions in binary form must reproduce the above copyright notice, 18 this list of conditions, and the following disclaimer in the documentation 19 and/or other materials provided with the distribution. 20 21 3. Neither the name of the University nor the names of its contributors may 22 be used to endorse or promote products derived from this software without 23 specific prior written permission. 24 25 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY 26 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 27 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE 28 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY 29 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 30 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 31 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 32 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 34 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 36 =============================================================================*/ 37 38 #include <stdbool.h> 39 #include <stdint.h> 40 #include "platform.h" 41 #include "internals.h" 42 #include "specialize.h" 43 #include "softfloat.h" 44 45 #ifdef SOFTFLOAT_FAST_INT64 46 47 void extF80M_roundToInt(const extFloat80_t * aPtr,uint_fast8_t roundingMode,bool exact,extFloat80_t * zPtr)48 extF80M_roundToInt( 49 const extFloat80_t *aPtr, 50 uint_fast8_t roundingMode, 51 bool exact, 52 extFloat80_t *zPtr 53 ) 54 { 55 56 *zPtr = extF80_roundToInt( *aPtr, roundingMode, exact ); 57 58 } 59 60 #else 61 62 void extF80M_roundToInt(const extFloat80_t * aPtr,uint_fast8_t roundingMode,bool exact,extFloat80_t * zPtr)63 extF80M_roundToInt( 64 const extFloat80_t *aPtr, 65 uint_fast8_t roundingMode, 66 bool exact, 67 extFloat80_t *zPtr 68 ) 69 { 70 const struct extFloat80M *aSPtr; 71 struct extFloat80M *zSPtr; 72 uint_fast16_t uiA64, signUI64; 73 int32_t exp; 74 uint64_t sigA; 75 uint_fast16_t uiZ64; 76 uint64_t sigZ, lastBitMask, roundBitsMask; 77 78 /*------------------------------------------------------------------------ 79 *------------------------------------------------------------------------*/ 80 aSPtr = (const struct extFloat80M *) aPtr; 81 zSPtr = (struct extFloat80M *) zPtr; 82 /*------------------------------------------------------------------------ 83 *------------------------------------------------------------------------*/ 84 uiA64 = aSPtr->signExp; 85 signUI64 = uiA64 & packToExtF80UI64( 1, 0 ); 86 exp = expExtF80UI64( uiA64 ); 87 sigA = aSPtr->signif; 88 /*------------------------------------------------------------------------ 89 *------------------------------------------------------------------------*/ 90 if ( ! (sigA & UINT64_C( 0x8000000000000000 )) && (exp != 0x7FFF) ) { 91 if ( ! sigA ) { 92 uiZ64 = signUI64; 93 sigZ = 0; 94 goto uiZ; 95 } 96 exp += softfloat_normExtF80SigM( &sigA ); 97 } 98 /*------------------------------------------------------------------------ 99 *------------------------------------------------------------------------*/ 100 if ( exp <= 0x3FFE ) { 101 if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact; 102 switch ( roundingMode ) { 103 case softfloat_round_near_even: 104 if ( ! (sigA & UINT64_C( 0x7FFFFFFFFFFFFFFF )) ) break; 105 case softfloat_round_near_maxMag: 106 if ( exp == 0x3FFE ) goto mag1; 107 break; 108 case softfloat_round_min: 109 if ( signUI64 ) goto mag1; 110 break; 111 case softfloat_round_max: 112 if ( ! signUI64 ) goto mag1; 113 break; 114 } 115 uiZ64 = signUI64; 116 sigZ = 0; 117 goto uiZ; 118 mag1: 119 uiZ64 = signUI64 | 0x3FFF; 120 sigZ = UINT64_C( 0x8000000000000000 ); 121 goto uiZ; 122 } 123 /*------------------------------------------------------------------------ 124 *------------------------------------------------------------------------*/ 125 if ( 0x403E <= exp ) { 126 if ( exp == 0x7FFF ) { 127 if ( sigA & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) { 128 softfloat_propagateNaNExtF80M( aSPtr, 0, zSPtr ); 129 return; 130 } 131 sigZ = UINT64_C( 0x8000000000000000 ); 132 } else { 133 sigZ = sigA; 134 } 135 uiZ64 = signUI64 | exp; 136 goto uiZ; 137 } 138 /*------------------------------------------------------------------------ 139 *------------------------------------------------------------------------*/ 140 uiZ64 = signUI64 | exp; 141 lastBitMask = (uint64_t) 1<<(0x403E - exp); 142 roundBitsMask = lastBitMask - 1; 143 sigZ = sigA; 144 if ( roundingMode == softfloat_round_near_maxMag ) { 145 sigZ += lastBitMask>>1; 146 } else if ( roundingMode == softfloat_round_near_even ) { 147 sigZ += lastBitMask>>1; 148 if ( ! (sigZ & roundBitsMask) ) sigZ &= ~lastBitMask; 149 } else if ( roundingMode != softfloat_round_minMag ) { 150 if ( (signUI64 != 0) ^ (roundingMode == softfloat_round_max) ) { 151 sigZ += roundBitsMask; 152 } 153 } 154 sigZ &= ~roundBitsMask; 155 if ( ! sigZ ) { 156 ++uiZ64; 157 sigZ = UINT64_C( 0x8000000000000000 ); 158 } 159 if ( exact && (sigZ != sigA) ) { 160 softfloat_exceptionFlags |= softfloat_flag_inexact; 161 } 162 uiZ: 163 zSPtr->signExp = uiZ64; 164 zSPtr->signif = sigZ; 165 return; 166 167 } 168 169 #endif 170 171