github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/3rd_party/SoftFloat-3e/source/f128_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, 2017 The Regents of the
     8  University of 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  float128_t f128_sqrt( float128_t a )
    45  {
    46      union ui128_f128 uA;
    47      uint_fast64_t uiA64, uiA0;
    48      bool signA;
    49      int_fast32_t expA;
    50      struct uint128 sigA, uiZ;
    51      struct exp32_sig128 normExpSig;
    52      int_fast32_t expZ;
    53      uint_fast32_t sig32A, recipSqrt32, sig32Z;
    54      struct uint128 rem;
    55      uint32_t qs[3];
    56      uint_fast32_t q;
    57      uint_fast64_t x64, sig64Z;
    58      struct uint128 y, term;
    59      uint_fast64_t sigZExtra;
    60      struct uint128 sigZ;
    61      union ui128_f128 uZ;
    62  
    63      /*------------------------------------------------------------------------
    64      *------------------------------------------------------------------------*/
    65      uA.f = a;
    66      uiA64 = uA.ui.v64;
    67      uiA0  = uA.ui.v0;
    68      signA = signF128UI64( uiA64 );
    69      expA  = expF128UI64( uiA64 );
    70      sigA.v64 = fracF128UI64( uiA64 );
    71      sigA.v0  = uiA0;
    72      /*------------------------------------------------------------------------
    73      *------------------------------------------------------------------------*/
    74      if ( expA == 0x7FFF ) {
    75          if ( sigA.v64 | sigA.v0 ) {
    76              uiZ = softfloat_propagateNaNF128UI( uiA64, uiA0, 0, 0 );
    77              goto uiZ;
    78          }
    79          if ( ! signA ) return a;
    80          goto invalid;
    81      }
    82      /*------------------------------------------------------------------------
    83      *------------------------------------------------------------------------*/
    84      if ( signA ) {
    85          if ( ! (expA | sigA.v64 | sigA.v0) ) return a;
    86          goto invalid;
    87      }
    88      /*------------------------------------------------------------------------
    89      *------------------------------------------------------------------------*/
    90      if ( ! expA ) {
    91          if ( ! (sigA.v64 | sigA.v0) ) return a;
    92          normExpSig = softfloat_normSubnormalF128Sig( sigA.v64, sigA.v0 );
    93          expA = normExpSig.exp;
    94          sigA = normExpSig.sig;
    95      }
    96      /*------------------------------------------------------------------------
    97      | (`sig32Z' is guaranteed to be a lower bound on the square root of
    98      | `sig32A', which makes `sig32Z' also a lower bound on the square root of
    99      | `sigA'.)
   100      *------------------------------------------------------------------------*/
   101      expZ = ((expA - 0x3FFF)>>1) + 0x3FFE;
   102      expA &= 1;
   103      sigA.v64 |= UINT64_C( 0x0001000000000000 );
   104      sig32A = sigA.v64>>17;
   105      recipSqrt32 = softfloat_approxRecipSqrt32_1( expA, sig32A );
   106      sig32Z = ((uint_fast64_t) sig32A * recipSqrt32)>>32;
   107      if ( expA ) {
   108          sig32Z >>= 1;
   109          rem = softfloat_shortShiftLeft128( sigA.v64, sigA.v0, 12 );
   110      } else {
   111          rem = softfloat_shortShiftLeft128( sigA.v64, sigA.v0, 13 );
   112      }
   113      qs[2] = sig32Z;
   114      rem.v64 -= (uint_fast64_t) sig32Z * sig32Z;
   115      /*------------------------------------------------------------------------
   116      *------------------------------------------------------------------------*/
   117      q = ((uint32_t) (rem.v64>>2) * (uint_fast64_t) recipSqrt32)>>32;
   118      x64 = (uint_fast64_t) sig32Z<<32;
   119      sig64Z = x64 + ((uint_fast64_t) q<<3);
   120      y = softfloat_shortShiftLeft128( rem.v64, rem.v0, 29 );
   121      /*------------------------------------------------------------------------
   122      | (Repeating this loop is a rare occurrence.)
   123      *------------------------------------------------------------------------*/
   124      for (;;) {
   125          term = softfloat_mul64ByShifted32To128( x64 + sig64Z, q );
   126          rem = softfloat_sub128( y.v64, y.v0, term.v64, term.v0 );
   127          if ( ! (rem.v64 & UINT64_C( 0x8000000000000000 )) ) break;
   128          --q;
   129          sig64Z -= 1<<3;
   130      }
   131      qs[1] = q;
   132      /*------------------------------------------------------------------------
   133      *------------------------------------------------------------------------*/
   134      q = ((rem.v64>>2) * recipSqrt32)>>32;
   135      y = softfloat_shortShiftLeft128( rem.v64, rem.v0, 29 );
   136      sig64Z <<= 1;
   137      /*------------------------------------------------------------------------
   138      | (Repeating this loop is a rare occurrence.)
   139      *------------------------------------------------------------------------*/
   140      for (;;) {
   141          term = softfloat_shortShiftLeft128( 0, sig64Z, 32 );
   142          term = softfloat_add128( term.v64, term.v0, 0, (uint_fast64_t) q<<6 );
   143          term = softfloat_mul128By32( term.v64, term.v0, q );
   144          rem = softfloat_sub128( y.v64, y.v0, term.v64, term.v0 );
   145          if ( ! (rem.v64 & UINT64_C( 0x8000000000000000 )) ) break;
   146          --q;
   147      }
   148      qs[0] = q;
   149      /*------------------------------------------------------------------------
   150      *------------------------------------------------------------------------*/
   151      q = (((rem.v64>>2) * recipSqrt32)>>32) + 2;
   152      sigZExtra = (uint64_t) ((uint_fast64_t) q<<59);
   153      term = softfloat_shortShiftLeft128( 0, qs[1], 53 );
   154      sigZ =
   155          softfloat_add128(
   156              (uint_fast64_t) qs[2]<<18, ((uint_fast64_t) qs[0]<<24) + (q>>5),
   157              term.v64, term.v0
   158          );
   159      /*------------------------------------------------------------------------
   160      *------------------------------------------------------------------------*/
   161      if ( (q & 0xF) <= 2 ) {
   162          q &= ~3;
   163          sigZExtra = (uint64_t) ((uint_fast64_t) q<<59);
   164          y = softfloat_shortShiftLeft128( sigZ.v64, sigZ.v0, 6 );
   165          y.v0 |= sigZExtra>>58;
   166          term = softfloat_sub128( y.v64, y.v0, 0, q );
   167          y    = softfloat_mul64ByShifted32To128( term.v0,  q );
   168          term = softfloat_mul64ByShifted32To128( term.v64, q );
   169          term = softfloat_add128( term.v64, term.v0, 0, y.v64 );
   170          rem = softfloat_shortShiftLeft128( rem.v64, rem.v0, 20 );
   171          term = softfloat_sub128( term.v64, term.v0, rem.v64, rem.v0 );
   172          /*--------------------------------------------------------------------
   173          | The concatenation of `term' and `y.v0' is now the negative remainder
   174          | (3 words altogether).
   175          *--------------------------------------------------------------------*/
   176          if ( term.v64 & UINT64_C( 0x8000000000000000 ) ) {
   177              sigZExtra |= 1;
   178          } else {
   179              if ( term.v64 | term.v0 | y.v0 ) {
   180                  if ( sigZExtra ) {
   181                      --sigZExtra;
   182                  } else {
   183                      sigZ = softfloat_sub128( sigZ.v64, sigZ.v0, 0, 1 );
   184                      sigZExtra = ~0;
   185                  }
   186              }
   187          }
   188      }
   189      return softfloat_roundPackToF128( 0, expZ, sigZ.v64, sigZ.v0, sigZExtra );
   190      /*------------------------------------------------------------------------
   191      *------------------------------------------------------------------------*/
   192   invalid:
   193      softfloat_raiseFlags( softfloat_flag_invalid );
   194      uiZ.v64 = defaultNaNF128UI64;
   195      uiZ.v0  = defaultNaNF128UI0;
   196   uiZ:
   197      uZ.ui = uiZ;
   198      return uZ.f;
   199  
   200  }
   201