1 2/*============================================================================ 3 4This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic 5Package, Release 3e, by John R. Hauser. 6 7Copyright 2011, 2012, 2013, 2014 The Regents of the University of California. 8All rights reserved. 9 10Redistribution and use in source and binary forms, with or without 11modification, 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 24THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY 25EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE 27DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY 28DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 29(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 31ON 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 33SOFTWARE, 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 44float32_t f32_div( float32_t a, float32_t b ) 45{ 46 union ui32_f32 uA; 47 uint_fast32_t uiA; 48 bool signA; 49 int_fast16_t expA; 50 uint_fast32_t sigA; 51 union ui32_f32 uB; 52 uint_fast32_t uiB; 53 bool signB; 54 int_fast16_t expB; 55 uint_fast32_t sigB; 56 bool signZ; 57 struct exp16_sig32 normExpSig; 58 int_fast16_t expZ; 59#ifdef SOFTFLOAT_FAST_DIV64TO32 60 uint_fast64_t sig64A; 61 uint_fast32_t sigZ; 62#else 63 uint_fast32_t sigZ; 64 uint_fast64_t rem; 65#endif 66 uint_fast32_t uiZ; 67 union ui32_f32 uZ; 68 69 /*------------------------------------------------------------------------ 70 *------------------------------------------------------------------------*/ 71 uA.f = a; 72 uiA = uA.ui; 73 signA = signF32UI( uiA ); 74 expA = expF32UI( uiA ); 75 sigA = fracF32UI( uiA ); 76 uB.f = b; 77 uiB = uB.ui; 78 signB = signF32UI( uiB ); 79 expB = expF32UI( uiB ); 80 sigB = fracF32UI( uiB ); 81 signZ = signA ^ signB; 82 /*------------------------------------------------------------------------ 83 *------------------------------------------------------------------------*/ 84 if ( expA == 0xFF ) { 85 if ( sigA ) goto propagateNaN; 86 if ( expB == 0xFF ) { 87 if ( sigB ) goto propagateNaN; 88 goto invalid; 89 } 90 goto infinity; 91 } 92 if ( expB == 0xFF ) { 93 if ( sigB ) goto propagateNaN; 94 goto zero; 95 } 96 /*------------------------------------------------------------------------ 97 *------------------------------------------------------------------------*/ 98 if ( ! expB ) { 99 if ( ! sigB ) { 100 if ( ! (expA | sigA) ) goto invalid; 101 softfloat_raiseFlags( softfloat_flag_infinite ); 102 goto infinity; 103 } 104 normExpSig = softfloat_normSubnormalF32Sig( sigB ); 105 expB = normExpSig.exp; 106 sigB = normExpSig.sig; 107 } 108 if ( ! expA ) { 109 if ( ! sigA ) goto zero; 110 normExpSig = softfloat_normSubnormalF32Sig( sigA ); 111 expA = normExpSig.exp; 112 sigA = normExpSig.sig; 113 } 114 /*------------------------------------------------------------------------ 115 *------------------------------------------------------------------------*/ 116 expZ = expA - expB + 0x7E; 117 sigA |= 0x00800000; 118 sigB |= 0x00800000; 119#ifdef SOFTFLOAT_FAST_DIV64TO32 120 if ( sigA < sigB ) { 121 --expZ; 122 sig64A = (uint_fast64_t) sigA<<31; 123 } else { 124 sig64A = (uint_fast64_t) sigA<<30; 125 } 126 sigZ = sig64A / sigB; 127 if ( ! (sigZ & 0x3F) ) sigZ |= ((uint_fast64_t) sigB * sigZ != sig64A); 128#else 129 if ( sigA < sigB ) { 130 --expZ; 131 sigA <<= 8; 132 } else { 133 sigA <<= 7; 134 } 135 sigB <<= 8; 136 sigZ = ((uint_fast64_t) sigA * softfloat_approxRecip32_1( sigB ))>>32; 137 /*------------------------------------------------------------------------ 138 *------------------------------------------------------------------------*/ 139 sigZ += 2; 140 if ( (sigZ & 0x3F) < 2 ) { 141 sigZ &= ~3; 142#ifdef SOFTFLOAT_FAST_INT64 143 rem = ((uint_fast64_t) sigA<<31) - (uint_fast64_t) sigZ * sigB; 144#else 145 rem = ((uint_fast64_t) sigA<<32) - (uint_fast64_t) (sigZ<<1) * sigB; 146#endif 147 if ( rem & UINT64_C( 0x8000000000000000 ) ) { 148 sigZ -= 4; 149 } else { 150 if ( rem ) sigZ |= 1; 151 } 152 } 153#endif 154 return softfloat_roundPackToF32( signZ, expZ, sigZ ); 155 /*------------------------------------------------------------------------ 156 *------------------------------------------------------------------------*/ 157 propagateNaN: 158 uiZ = softfloat_propagateNaNF32UI( uiA, uiB ); 159 goto uiZ; 160 /*------------------------------------------------------------------------ 161 *------------------------------------------------------------------------*/ 162 invalid: 163 softfloat_raiseFlags( softfloat_flag_invalid ); 164 uiZ = defaultNaNF32UI; 165 goto uiZ; 166 /*------------------------------------------------------------------------ 167 *------------------------------------------------------------------------*/ 168 infinity: 169 uiZ = packToF32UI( signZ, 0xFF, 0 ); 170 goto uiZ; 171 /*------------------------------------------------------------------------ 172 *------------------------------------------------------------------------*/ 173 zero: 174 uiZ = packToF32UI( signZ, 0, 0 ); 175 uiZ: 176 uZ.ui = uiZ; 177 return uZ.f; 178 179} 180 181