/ src / ref / scalbnf.c
scalbnf.c
  1  /*
  2   * Copyright (C) 2008-2020 Advanced Micro Devices, Inc. All rights reserved.
  3   *
  4   * Redistribution and use in source and binary forms, with or without modification,
  5   * are permitted provided that the following conditions are met:
  6   * 1. Redistributions of source code must retain the above copyright notice,
  7   *    this list of conditions and the following disclaimer.
  8   * 2. Redistributions in binary form must reproduce the above copyright notice,
  9   *    this list of conditions and the following disclaimer in the documentation
 10   *    and/or other materials provided with the distribution.
 11   * 3. Neither the name of the copyright holder nor the names of its contributors
 12   *    may be used to endorse or promote products derived from this software without
 13   *    specific prior written permission.
 14   *
 15   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 16   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 17   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 18   * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 19   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 20   * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 21   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 22   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 23   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 24   * POSSIBILITY OF SUCH DAMAGE.
 25   *
 26   */
 27  
 28  #include "fn_macros.h"
 29  #include "libm_util_amd.h"
 30  #include "libm_special.h"
 31  
 32  float FN_PROTOTYPE_REF(scalbnf)(float x, int n)
 33  {
 34      UT32 val,val_x;
 35      unsigned int sign;
 36      int exponent;
 37      val.f32 = x;
 38      val_x.f32 = x;
 39      sign = val.u32 & 0x80000000;
 40      val.u32 = val.u32 & 0x7fffffff;/* remove the sign bit */
 41  
 42      if(val.u32 > 0x7f800000)/* x= nan */
 43          #ifdef WINDOWS
 44          return __amd_handle_errorf("scalbnf", __amd_scalbn, val_x.u32|0x00400000, _DOMAIN, 0, EDOM, x, (float)n, 2);
 45          #else
 46          {
 47            if(!(val.u32 & 0x00400000)) //x is snan
 48                return __amd_handle_errorf("scalbnf", __amd_scalbn, val_x.u32|0x00400000, _DOMAIN, AMD_F_INVALID, EDOM, x, (float)n, 2);
 49            else
 50                return x;
 51  		}
 52          #endif
 53  
 54      if(val.u32 == 0x7f800000)/* x = +-inf*/
 55          return x;
 56  
 57      if((val.u32 == 0x00000000) || (n==0))/* x= +-0 or n= 0*/
 58          return x;
 59  
 60      exponent = val.u32 >> 23; /* get the exponent */
 61  
 62  	if(exponent == 0)/*x is denormal*/
 63  	{
 64  		val.f32 = val.f32 * VAL_2PMULTIPLIER_SP;/*multiply by 2^24 to bring it to the normal range*/
 65  		exponent = (val.u32 >> 23); /* get the exponent */
 66  		exponent = exponent + n - MULTIPLIER_SP;
 67  		if(exponent < -MULTIPLIER_SP)/*underflow*/
 68  		{
 69  			val.u32 = sign | 0x00000000;
 70              return __amd_handle_errorf("scalbnf", __amd_scalbn, val.u32, _UNDERFLOW, AMD_F_INEXACT|AMD_F_UNDERFLOW, ERANGE, x, (float)n, 2);
 71  		}
 72  
 73  		if(exponent > 254)/*overflow*/
 74  		{
 75  			val.u32 = sign | 0x7f800000;
 76              return __amd_handle_errorf("scalbnf", __amd_scalbn, val.u32, _OVERFLOW, AMD_F_INEXACT|AMD_F_OVERFLOW, ERANGE, x, (float)n, 2);
 77  		}
 78  
 79  		exponent += MULTIPLIER_SP;
 80  		val.u32 = sign | (exponent << 23) | (val.u32 & 0x007fffff);
 81  		val.f32 = val.f32 * VAL_2PMMULTIPLIER_SP;
 82          return val.f32;
 83  	}
 84  
 85      exponent += n;
 86  
 87      if(exponent < -MULTIPLIER_SP)/*underflow*/
 88  	{
 89  		val.u32 = sign | 0x00000000;
 90          return __amd_handle_errorf("scalbnf", __amd_scalbn, val.u32, _UNDERFLOW, AMD_F_INEXACT|AMD_F_UNDERFLOW, ERANGE, x, (float)n, 2);
 91  	}
 92  
 93      if(exponent < 1)/*x is normal but output is debnormal*/
 94      {
 95  		exponent += MULTIPLIER_SP;
 96  		val.u32 = sign | (exponent << 23) | (val.u32 & 0x007fffff);
 97  		val.f32 = val.f32 * VAL_2PMMULTIPLIER_SP;
 98          return val.f32;
 99      }
100  
101      if(exponent > 254)/*overflow*/
102  	{
103  		val.u32 = sign | 0x7f800000;
104          return __amd_handle_errorf("scalbnf", __amd_scalbn, val.u32, _OVERFLOW, AMD_F_INEXACT|AMD_F_OVERFLOW, ERANGE, x, (float)n, 2);
105  	}
106  
107      val.u32 = sign | (exponent << 23) | (val.u32 & 0x007fffff);/*x is normal and output is normal*/
108      return val.f32;
109  }
110  
111