github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/3rd_party/SoftFloat-3e/source/f16_sqrt.c (about)

     1  
     2  /*============================================================================
     3  
     4  This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
     5  Package, Release 3e, by John R. Hauser.
     6  
     7  Copyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of
     8  California.  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  extern const uint16_t softfloat_approxRecipSqrt_1k0s[];
    45  extern const uint16_t softfloat_approxRecipSqrt_1k1s[];
    46  
    47  float16_t f16_sqrt( float16_t a )
    48  {
    49      union ui16_f16 uA;
    50      uint_fast16_t uiA;
    51      bool signA;
    52      int_fast8_t expA;
    53      uint_fast16_t sigA, uiZ;
    54      struct exp8_sig16 normExpSig;
    55      int_fast8_t expZ;
    56      int index;
    57      uint_fast16_t r0;
    58      uint_fast32_t ESqrR0;
    59      uint16_t sigma0;
    60      uint_fast16_t recipSqrt16, sigZ, shiftedSigZ;
    61      uint16_t negRem;
    62      union ui16_f16 uZ;
    63  
    64      /*------------------------------------------------------------------------
    65      *------------------------------------------------------------------------*/
    66      uA.f = a;
    67      uiA = uA.ui;
    68      signA = signF16UI( uiA );
    69      expA  = expF16UI( uiA );
    70      sigA  = fracF16UI( uiA );
    71      /*------------------------------------------------------------------------
    72      *------------------------------------------------------------------------*/
    73      if ( expA == 0x1F ) {
    74          if ( sigA ) {
    75              uiZ = softfloat_propagateNaNF16UI( uiA, 0 );
    76              goto uiZ;
    77          }
    78          if ( ! signA ) return a;
    79          goto invalid;
    80      }
    81      /*------------------------------------------------------------------------
    82      *------------------------------------------------------------------------*/
    83      if ( signA ) {
    84          if ( ! (expA | sigA) ) return a;
    85          goto invalid;
    86      }
    87      /*------------------------------------------------------------------------
    88      *------------------------------------------------------------------------*/
    89      if ( ! expA ) {
    90          if ( ! sigA ) return a;
    91          normExpSig = softfloat_normSubnormalF16Sig( sigA );
    92          expA = normExpSig.exp;
    93          sigA = normExpSig.sig;
    94      }
    95      /*------------------------------------------------------------------------
    96      *------------------------------------------------------------------------*/
    97      expZ = ((expA - 0xF)>>1) + 0xE;
    98      expA &= 1;
    99      sigA |= 0x0400;
   100      index = (sigA>>6 & 0xE) + expA;
   101      r0 = softfloat_approxRecipSqrt_1k0s[index]
   102               - (((uint_fast32_t) softfloat_approxRecipSqrt_1k1s[index]
   103                       * (sigA & 0x7F))
   104                      >>11);
   105      ESqrR0 = ((uint_fast32_t) r0 * r0)>>1;
   106      if ( expA ) ESqrR0 >>= 1;
   107      sigma0 = ~(uint_fast16_t) ((ESqrR0 * sigA)>>16);
   108      recipSqrt16 = r0 + (((uint_fast32_t) r0 * sigma0)>>25);
   109      if ( ! (recipSqrt16 & 0x8000) ) recipSqrt16 = 0x8000;
   110      sigZ = ((uint_fast32_t) (sigA<<5) * recipSqrt16)>>16;
   111      if ( expA ) sigZ >>= 1;
   112      /*------------------------------------------------------------------------
   113      *------------------------------------------------------------------------*/
   114      ++sigZ;
   115      if ( ! (sigZ & 7) ) {
   116          shiftedSigZ = sigZ>>1;
   117          negRem = shiftedSigZ * shiftedSigZ;
   118          sigZ &= ~1;
   119          if ( negRem & 0x8000 ) {
   120              sigZ |= 1;
   121          } else {
   122              if ( negRem ) --sigZ;
   123          }
   124      }
   125      return softfloat_roundPackToF16( 0, expZ, sigZ );
   126      /*------------------------------------------------------------------------
   127      *------------------------------------------------------------------------*/
   128   invalid:
   129      softfloat_raiseFlags( softfloat_flag_invalid );
   130      uiZ = defaultNaNF16UI;
   131   uiZ:
   132      uZ.ui = uiZ;
   133      return uZ.f;
   134  
   135  }
   136