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