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 float32_t f32_div( float32_t a, float32_t b ) 46 { 47 union ui32_f32 uA; 48 uint_fast32_t uiA; 49 bool signA; 50 int_fast16_t expA; 51 uint_fast32_t sigA; 52 union ui32_f32 uB; 53 uint_fast32_t uiB; 54 bool signB; 55 int_fast16_t expB; 56 uint_fast32_t sigB; 57 bool signZ; 58 struct exp16_sig32 normExpSig; 59 int_fast16_t expZ; 60 #ifdef SOFTFLOAT_FAST_DIV64TO32 61 uint_fast64_t sig64A; 62 uint_fast32_t sigZ; 63 #else 64 uint_fast32_t sigZ; 65 uint_fast64_t rem; 66 #endif 67 uint_fast32_t uiZ; 68 union ui32_f32 uZ; 69 70 /*------------------------------------------------------------------------ 71 *------------------------------------------------------------------------*/ 72 uA.f = a; 73 uiA = uA.ui; 74 signA = signF32UI( uiA ); 75 expA = expF32UI( uiA ); 76 sigA = fracF32UI( uiA ); 77 uB.f = b; 78 uiB = uB.ui; 79 signB = signF32UI( uiB ); 80 expB = expF32UI( uiB ); 81 sigB = fracF32UI( uiB ); 82 signZ = signA ^ signB; 83 /*------------------------------------------------------------------------ 84 *------------------------------------------------------------------------*/ 85 if ( expA == 0xFF ) { 86 if ( sigA ) goto propagateNaN; 87 if ( expB == 0xFF ) { 88 if ( sigB ) goto propagateNaN; 89 goto invalid; 90 } 91 goto infinity; 92 } 93 if ( expB == 0xFF ) { 94 if ( sigB ) goto propagateNaN; 95 goto zero; 96 } 97 /*------------------------------------------------------------------------ 98 *------------------------------------------------------------------------*/ 99 if ( ! expB ) { 100 if ( ! sigB ) { 101 if ( ! (expA | sigA) ) goto invalid; 102 softfloat_raiseFlags( softfloat_flag_infinite ); 103 goto infinity; 104 } 105 normExpSig = softfloat_normSubnormalF32Sig( sigB ); 106 expB = normExpSig.exp; 107 sigB = normExpSig.sig; 108 } 109 if ( ! expA ) { 110 if ( ! sigA ) goto zero; 111 normExpSig = softfloat_normSubnormalF32Sig( sigA ); 112 expA = normExpSig.exp; 113 sigA = normExpSig.sig; 114 } 115 /*------------------------------------------------------------------------ 116 *------------------------------------------------------------------------*/ 117 expZ = expA - expB + 0x7E; 118 sigA |= 0x00800000; 119 sigB |= 0x00800000; 120 #ifdef SOFTFLOAT_FAST_DIV64TO32 121 if ( sigA < sigB ) { 122 --expZ; 123 sig64A = (uint_fast64_t) sigA<<31; 124 } else { 125 sig64A = (uint_fast64_t) sigA<<30; 126 } 127 sigZ = sig64A / sigB; 128 if ( ! (sigZ & 0x3F) ) sigZ |= ((uint_fast64_t) sigB * sigZ != sig64A); 129 #else 130 if ( sigA < sigB ) { 131 --expZ; 132 sigA <<= 8; 133 } else { 134 sigA <<= 7; 135 } 136 sigB <<= 8; 137 sigZ = ((uint_fast64_t) sigA * softfloat_approxRecip32_1( sigB ))>>32; 138 /*------------------------------------------------------------------------ 139 *------------------------------------------------------------------------*/ 140 sigZ += 2; 141 if ( (sigZ & 0x3F) < 2 ) { 142 sigZ &= ~3; 143 #ifdef SOFTFLOAT_FAST_INT64 144 rem = ((uint_fast64_t) sigA<<31) - (uint_fast64_t) sigZ * sigB; 145 #else 146 rem = ((uint_fast64_t) sigA<<32) - (uint_fast64_t) (sigZ<<1) * sigB; 147 #endif 148 if ( rem & UINT64_C( 0x8000000000000000 ) ) { 149 sigZ -= 4; 150 } else { 151 if ( rem ) sigZ |= 1; 152 } 153 } 154 #endif 155 return softfloat_roundPackToF32( signZ, expZ, sigZ ); 156 /*------------------------------------------------------------------------ 157 *------------------------------------------------------------------------*/ 158 propagateNaN: 159 uiZ = softfloat_propagateNaNF32UI( uiA, uiB ); 160 goto uiZ; 161 /*------------------------------------------------------------------------ 162 *------------------------------------------------------------------------*/ 163 invalid: 164 softfloat_raiseFlags( softfloat_flag_invalid ); 165 uiZ = defaultNaNF32UI; 166 goto uiZ; 167 /*------------------------------------------------------------------------ 168 *------------------------------------------------------------------------*/ 169 infinity: 170 uiZ = packToF32UI( signZ, 0xFF, 0 ); 171 goto uiZ; 172 /*------------------------------------------------------------------------ 173 *------------------------------------------------------------------------*/ 174 zero: 175 uiZ = packToF32UI( signZ, 0, 0 ); 176 uiZ: 177 uZ.ui = uiZ; 178 return uZ.f; 179 180 } 181 182