/ src / ref / lroundf.c
lroundf.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  long int FN_PROTOTYPE_REF(lroundf)(float f)
 33  {
 34      UT32 u32d;
 35      UT32 u32Temp,u32result;
 36      int intexp, shift;
 37      U32 sign;
 38      long int  result;
 39  
 40      u32d.f32 = u32Temp.f32 = f;
 41      if ((u32d.u32 & 0X7F800000) == 0x7F800000)
 42      {
 43          /*else the number is infinity*/
 44  		//Raise range or domain error
 45          {
 46  		#ifdef WIN64
 47  			__amd_handle_errorf("lroundf", __amd_lround, SIGNBIT_SP32, _DOMAIN, AMD_F_NONE, EDOM, f, 0.0, 1);
 48  			return (long int)SIGNBIT_SP32;
 49  		#else
 50           if((u32d.u32 & 0x7fffffff) == 0x7f800000)
 51              return SIGNBIT_DP64;
 52           if((u32d.u32 & 0x7fffffff) >= 0x7fc00000)
 53                  __amd_handle_errorf("lround", __amd_lround, (unsigned int)SIGNBIT_DP64, _DOMAIN, AMD_F_NONE, EDOM, f, 0.0, 1);
 54           else    
 55                  __amd_handle_errorf("lround", __amd_lround, (unsigned int)SIGNBIT_DP64, _DOMAIN, AMD_F_INVALID, EDOM, f, 0.0, 1);
 56              
 57  		 return SIGNBIT_DP64; /*GCC returns this when the number is out of range*/
 58  		#endif
 59          }
 60  
 61      }
 62  
 63      u32Temp.u32 &= 0x7FFFFFFF;
 64      intexp = (u32d.u32 & 0x7F800000) >> 23;
 65      sign = u32d.u32 & 0x80000000;
 66      intexp -= 0x7F;
 67  
 68  
 69      /* 1.0 x 2^-1 is the smallest number which can be rounded to 1 */
 70      if (intexp < -1)
 71          return (0);
 72  
 73  
 74  #ifdef WIN64
 75      /* 1.0 x 2^31 is already too large */
 76      if (intexp >= 31)
 77      {
 78          result = 0x80000000;
 79  		__amd_handle_errorf("lroundf", __amd_lround, result, _DOMAIN, AMD_F_NONE, EDOM, f, 0.0, 1);
 80          return result;
 81  	}
 82  
 83  #else
 84      /* 1.0 x 2^31 (or 2^63) is already too large */
 85      if (intexp >= 63)
 86      {
 87          result = 0x8000000000000000;
 88          __amd_handle_errorf("lroundf", __amd_lround, result, _DOMAIN, AMD_F_NONE, EDOM, f, 0.0, 1);
 89  		return result;
 90      }
 91   #endif
 92  
 93      u32result.f32 = u32Temp.f32;
 94  
 95      /* >= 2^23 is already an exact integer */
 96      if (intexp < 23)
 97      {
 98          /* add 0.5, extraction below will truncate */
 99          u32result.f32 = u32Temp.f32 + 0.5F;
100      }
101      intexp = (u32result.u32 & 0x7f800000) >> 23;
102      intexp -= 0x7f;
103      u32result.u32 &= 0x7fffff;
104      u32result.u32 |= 0x00800000;
105  
106      result = u32result.u32;
107  
108      #ifdef WIN64
109      shift = intexp - 23;
110      #else
111  
112      /*Since float is only 32 bit for higher accuracy we shift the result by 32 bits
113       * In the next step we shift an extra 32 bits in the reverse direction based
114       * on the value of intexp*/
115      result = result << 32;
116      shift = intexp - 55; /*55= 23 +32*/
117      #endif
118  
119  
120  	if(shift < 0)
121  		result = result >> (-shift);
122  	if(shift > 0)
123          result = result << (shift);
124  
125      if (sign)
126          result = -result;
127      return result;
128  
129  }
130  
131  
132  
133