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_div(const extFloat80_t * aPtr,const extFloat80_t * bPtr,extFloat80_t * zPtr)48 extF80M_div( 49 const extFloat80_t *aPtr, const extFloat80_t *bPtr, extFloat80_t *zPtr ) 50 { 51 52 *zPtr = extF80_div( *aPtr, *bPtr ); 53 54 } 55 56 #else 57 58 void extF80M_div(const extFloat80_t * aPtr,const extFloat80_t * bPtr,extFloat80_t * zPtr)59 extF80M_div( 60 const extFloat80_t *aPtr, const extFloat80_t *bPtr, extFloat80_t *zPtr ) 61 { 62 const struct extFloat80M *aSPtr, *bSPtr; 63 struct extFloat80M *zSPtr; 64 uint_fast16_t uiA64; 65 int32_t expA; 66 uint_fast16_t uiB64; 67 int32_t expB; 68 bool signZ; 69 uint64_t sigA, x64; 70 int32_t expZ; 71 int shiftCount; 72 uint32_t y[3], recip32, sigB[3]; 73 int ix; 74 uint32_t q, qs[2]; 75 uint_fast16_t uiZ64; 76 uint64_t uiZ0; 77 78 /*------------------------------------------------------------------------ 79 *------------------------------------------------------------------------*/ 80 aSPtr = (const struct extFloat80M *) aPtr; 81 bSPtr = (const struct extFloat80M *) bPtr; 82 zSPtr = (struct extFloat80M *) zPtr; 83 /*------------------------------------------------------------------------ 84 *------------------------------------------------------------------------*/ 85 uiA64 = aSPtr->signExp; 86 expA = expExtF80UI64( uiA64 ); 87 uiB64 = bSPtr->signExp; 88 expB = expExtF80UI64( uiB64 ); 89 signZ = signExtF80UI64( uiA64 ) ^ signExtF80UI64( uiB64 ); 90 /*------------------------------------------------------------------------ 91 *------------------------------------------------------------------------*/ 92 if ( (expA == 0x7FFF) || (expB == 0x7FFF) ) { 93 if ( softfloat_tryPropagateNaNExtF80M( aSPtr, bSPtr, zSPtr ) ) return; 94 if ( expA == 0x7FFF ) { 95 if ( expB == 0x7FFF ) goto invalid; 96 goto infinity; 97 } 98 goto zero; 99 } 100 /*------------------------------------------------------------------------ 101 *------------------------------------------------------------------------*/ 102 sigA = aSPtr->signif; 103 x64 = bSPtr->signif; 104 if ( ! expB ) expB = 1; 105 if ( ! (x64 & UINT64_C( 0x8000000000000000 )) ) { 106 if ( ! x64 ) { 107 if ( ! sigA ) goto invalid; 108 softfloat_raiseFlags( softfloat_flag_infinite ); 109 goto infinity; 110 } 111 expB += softfloat_normExtF80SigM( &x64 ); 112 } 113 if ( ! expA ) expA = 1; 114 if ( ! (sigA & UINT64_C( 0x8000000000000000 )) ) { 115 if ( ! sigA ) goto zero; 116 expA += softfloat_normExtF80SigM( &sigA ); 117 } 118 /*------------------------------------------------------------------------ 119 *------------------------------------------------------------------------*/ 120 expZ = expA - expB + 0x3FFF; 121 shiftCount = 29; 122 if ( sigA < x64 ) { 123 --expZ; 124 shiftCount = 30; 125 } 126 softfloat_shortShiftLeft64To96M( sigA, shiftCount, y ); 127 recip32 = softfloat_approxRecip32_1( x64>>32 ); 128 sigB[indexWord( 3, 0 )] = (uint32_t) x64<<30; 129 x64 >>= 2; 130 sigB[indexWord( 3, 2 )] = x64>>32; 131 sigB[indexWord( 3, 1 )] = x64; 132 ix = 2; 133 for (;;) { 134 x64 = (uint64_t) y[indexWordHi( 3 )] * recip32; 135 q = (x64 + 0x80000000)>>32; 136 --ix; 137 if ( ix < 0 ) break; 138 softfloat_remStep96MBy32( y, 29, sigB, q, y ); 139 if ( y[indexWordHi( 3 )] & 0x80000000 ) { 140 --q; 141 softfloat_add96M( y, sigB, y ); 142 } 143 qs[ix] = q; 144 } 145 /*------------------------------------------------------------------------ 146 *------------------------------------------------------------------------*/ 147 if ( ((q + 1) & 0x3FFFFF) < 2 ) { 148 softfloat_remStep96MBy32( y, 29, sigB, q, y ); 149 if ( y[indexWordHi( 3 )] & 0x80000000 ) { 150 --q; 151 softfloat_add96M( y, sigB, y ); 152 } else if ( softfloat_compare96M( sigB, y ) <= 0 ) { 153 ++q; 154 softfloat_sub96M( y, sigB, y ); 155 } 156 if ( 157 y[indexWordLo( 3 )] || y[indexWord( 3, 1 )] || y[indexWord( 3, 2 )] 158 ) { 159 q |= 1; 160 } 161 } 162 /*------------------------------------------------------------------------ 163 *------------------------------------------------------------------------*/ 164 x64 = (uint64_t) q<<9; 165 y[indexWord( 3, 0 )] = x64; 166 x64 = ((uint64_t) qs[0]<<6) + (x64>>32); 167 y[indexWord( 3, 1 )] = x64; 168 y[indexWord( 3, 2 )] = (qs[1]<<3) + (x64>>32); 169 softfloat_roundPackMToExtF80M( 170 signZ, expZ, y, extF80_roundingPrecision, zSPtr ); 171 return; 172 /*------------------------------------------------------------------------ 173 *------------------------------------------------------------------------*/ 174 invalid: 175 softfloat_invalidExtF80M( zSPtr ); 176 return; 177 /*------------------------------------------------------------------------ 178 *------------------------------------------------------------------------*/ 179 infinity: 180 uiZ64 = packToExtF80UI64( signZ, 0x7FFF ); 181 uiZ0 = UINT64_C( 0x8000000000000000 ); 182 goto uiZ; 183 /*------------------------------------------------------------------------ 184 *------------------------------------------------------------------------*/ 185 zero: 186 uiZ64 = packToExtF80UI64( signZ, 0 ); 187 uiZ0 = 0; 188 uiZ: 189 zSPtr->signExp = uiZ64; 190 zSPtr->signif = uiZ0; 191 192 } 193 194 #endif 195 196