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