github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/3rd_party/SoftFloat-3e/source/f16_mul.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 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  float16_t f16_mul( float16_t a, float16_t b )
    45  {
    46      union ui16_f16 uA;
    47      uint_fast16_t uiA;
    48      bool signA;
    49      int_fast8_t expA;
    50      uint_fast16_t sigA;
    51      union ui16_f16 uB;
    52      uint_fast16_t uiB;
    53      bool signB;
    54      int_fast8_t expB;
    55      uint_fast16_t sigB;
    56      bool signZ;
    57      uint_fast16_t magBits;
    58      struct exp8_sig16 normExpSig;
    59      int_fast8_t expZ;
    60      uint_fast32_t sig32Z;
    61      uint_fast16_t sigZ, uiZ;
    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      uB.f = b;
    72      uiB = uB.ui;
    73      signB = signF16UI( uiB );
    74      expB  = expF16UI( uiB );
    75      sigB  = fracF16UI( uiB );
    76      signZ = signA ^ signB;
    77      /*------------------------------------------------------------------------
    78      *------------------------------------------------------------------------*/
    79      if ( expA == 0x1F ) {
    80          if ( sigA || ((expB == 0x1F) && sigB) ) goto propagateNaN;
    81          magBits = expB | sigB;
    82          goto infArg;
    83      }
    84      if ( expB == 0x1F ) {
    85          if ( sigB ) goto propagateNaN;
    86          magBits = expA | sigA;
    87          goto infArg;
    88      }
    89      /*------------------------------------------------------------------------
    90      *------------------------------------------------------------------------*/
    91      if ( ! expA ) {
    92          if ( ! sigA ) goto zero;
    93          normExpSig = softfloat_normSubnormalF16Sig( sigA );
    94          expA = normExpSig.exp;
    95          sigA = normExpSig.sig;
    96      }
    97      if ( ! expB ) {
    98          if ( ! sigB ) goto zero;
    99          normExpSig = softfloat_normSubnormalF16Sig( sigB );
   100          expB = normExpSig.exp;
   101          sigB = normExpSig.sig;
   102      }
   103      /*------------------------------------------------------------------------
   104      *------------------------------------------------------------------------*/
   105      expZ = expA + expB - 0xF;
   106      sigA = (sigA | 0x0400)<<4;
   107      sigB = (sigB | 0x0400)<<5;
   108      sig32Z = (uint_fast32_t) sigA * sigB;
   109      sigZ = sig32Z>>16;
   110      if ( sig32Z & 0xFFFF ) sigZ |= 1;
   111      if ( sigZ < 0x4000 ) {
   112          --expZ;
   113          sigZ <<= 1;
   114      }
   115      return softfloat_roundPackToF16( signZ, expZ, sigZ );
   116      /*------------------------------------------------------------------------
   117      *------------------------------------------------------------------------*/
   118   propagateNaN:
   119      uiZ = softfloat_propagateNaNF16UI( uiA, uiB );
   120      goto uiZ;
   121      /*------------------------------------------------------------------------
   122      *------------------------------------------------------------------------*/
   123   infArg:
   124      if ( ! magBits ) {
   125          softfloat_raiseFlags( softfloat_flag_invalid );
   126          uiZ = defaultNaNF16UI;
   127      } else {
   128          uiZ = packToF16UI( signZ, 0x1F, 0 );
   129      }
   130      goto uiZ;
   131      /*------------------------------------------------------------------------
   132      *------------------------------------------------------------------------*/
   133   zero:
   134      uiZ = packToF16UI( signZ, 0, 0 );
   135   uiZ:
   136      uZ.ui = uiZ;
   137      return uZ.f;
   138  
   139  }
   140