/ src / ref / ilogbf.c
ilogbf.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 "libm_amd.h"
 29  #include "libm_util_amd.h"
 30  #include "libm_special.h"
 31  
 32  int FN_PROTOTYPE_REF(ilogbf)(float x)
 33  {
 34  
 35      /* Check for input range */
 36      UT32 checkbits;
 37      int expbits;
 38      U32 manbits;
 39      U32 zerovalue;
 40      checkbits.f32=x;
 41  
 42      /* Clear the sign bit and check if the value is zero nan or inf.*/
 43      zerovalue = (checkbits.u32 & ~SIGNBIT_SP32);
 44  
 45      if(zerovalue == 0)
 46      {
 47          /* Raise domain error as the number zero*/
 48          __amd_handle_errorf("ilogbf", __amd_log, (unsigned int)INT_MIN, _SING, AMD_F_DIVBYZERO, ERANGE, x, 0.0, 1);
 49          return INT_MIN;
 50      }
 51  
 52      if( zerovalue == EXPBITS_SP32 )
 53      {
 54          /* Raise domain error as the number is inf */
 55          //if negative inf raise an exception
 56          //if positive inf don't raise and exception
 57          if (x<0.0)
 58                  __amd_handle_errorf("ilogbf", __amd_log, (unsigned int)INT_MAX, _DOMAIN, AMD_F_INVALID, EDOM, x, 0.0, 1);
 59          else
 60                  __amd_handle_errorf("ilogbf", __amd_log, (unsigned int)INT_MAX, 0, AMD_F_NONE, 0, x, 0.0, 1);
 61          return INT_MAX;
 62      }
 63  
 64      if( zerovalue > EXPBITS_SP32 )
 65      {
 66          /* Raise exception as the number is inf */
 67  #ifdef WINDOWS
 68          __amd_handle_errorf("ilogbf", __amd_log, (unsigned int)INT_MIN, _DOMAIN, AMD_F_NONE, EDOM, x, 0.0, 1);
 69          return INT_MIN;
 70  #else
 71          //x = x+x;
 72          //x+x is not sufficient here since we return an integer and in 
 73          //optimization mode the compiler tends to optimize out the 
 74          //x+x operation if done.
 75          if (zerovalue >= 0x7fC00000)
 76                  __amd_handle_errorf("ilogbf", __amd_log, (unsigned int)INT_MIN, _DOMAIN, AMD_F_NONE, EDOM, x, 0.0, 1);
 77          else    
 78                  __amd_handle_errorf("ilogbf", __amd_log, (unsigned int)INT_MIN, _DOMAIN, AMD_F_INVALID, EDOM, x, 0.0, 1);
 79          return INT_MIN;
 80  #endif
 81      }
 82  
 83      expbits = (int) (( checkbits.u32 << 1) >> 24);
 84  
 85      if(expbits == 0 && (checkbits.u32 & MANTBITS_SP32 )!= 0)
 86      {
 87          /* the value is denormalized */
 88        manbits = checkbits.u32 & MANTBITS_SP32;
 89        expbits = EMIN_SP32;
 90        while (manbits < IMPBIT_SP32)
 91          {
 92            manbits <<= 1;
 93            expbits--;
 94          }
 95      }
 96      else
 97      {
 98          expbits-=EXPBIAS_SP32;
 99      }
100  
101      return expbits;
102  }
103