ldexp.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 double FN_PROTOTYPE_REF(ldexp)(double x, int n) 33 { 34 UT64 val,val_x; 35 unsigned int sign; 36 int exponent; 37 val.f64 = x; 38 val_x.f64 = x; 39 sign = val.u32[1] & 0x80000000; 40 val.u32[1] = val.u32[1] & 0x7fffffff; /* remove the sign bit */ 41 42 if (val.u64 > 0x7ff0000000000000) /* x is NaN */ 43 #ifdef WINDOWS 44 return __amd_handle_error("ldexp", __amd_ldexp, val_x.u64|0x0008000000000000, _DOMAIN, 0, EDOM, x, n, 2); 45 #else 46 { 47 if(!(val.u64 & 0x0008000000000000))// x is snan 48 return __amd_handle_error("ldexp", __amd_ldexp, val_x.u64|0x0008000000000000, _DOMAIN, AMD_F_INVALID, EDOM, x, n, 2); 49 else 50 return x; 51 } 52 #endif 53 54 if(val.u64 == 0x7ff0000000000000)/* x = +-inf*/ 55 return x; 56 57 if((val.u64 == 0x0000000000000000) || (n==0)) 58 return x; /* x= +-0 or n= 0*/ 59 60 exponent = val.u32[1] >> 20; /* get the exponent */ 61 62 if(exponent == 0)/*x is denormal*/ 63 { 64 val.f64 = val.f64 * VAL_2PMULTIPLIER_DP;/*multiply by 2^53 to bring it to the normal range*/ 65 exponent = val.u32[1] >> 20; /* get the exponent */ 66 exponent = exponent + n - MULTIPLIER_DP; 67 if(exponent < -MULTIPLIER_DP)/*underflow*/ 68 { 69 val.u32[1] = sign | 0x00000000; 70 val.u32[0] = 0x00000000; 71 return __amd_handle_error("ldexp", __amd_ldexp, val.u64, _UNDERFLOW, AMD_F_INEXACT|AMD_F_UNDERFLOW, ERANGE, x, (double)n, 2); 72 } 73 if(exponent > 2046)/*overflow*/ 74 { 75 val.u32[1] = sign | 0x7ff00000; 76 val.u32[0] = 0x00000000; 77 return __amd_handle_error("ldexp", __amd_ldexp, val.u64, _OVERFLOW, AMD_F_INEXACT|AMD_F_OVERFLOW, ERANGE ,x, (double)n, 2); 78 } 79 80 exponent += MULTIPLIER_DP; 81 val.u32[1] = sign | (exponent << 20) | (val.u32[1] & 0x000fffff); 82 val.f64 = val.f64 * VAL_2PMMULTIPLIER_DP; 83 return val.f64; 84 } 85 86 exponent += n; 87 88 if(exponent < -MULTIPLIER_DP)/*underflow*/ 89 { 90 val.u32[1] = sign | 0x00000000; 91 val.u32[0] = 0x00000000; 92 return __amd_handle_error("ldexp", __amd_ldexp, val.u64, _UNDERFLOW, AMD_F_INEXACT|AMD_F_UNDERFLOW, ERANGE, x, (double)n, 2); 93 } 94 95 if(exponent < 1)/*x is normal but output is debnormal*/ 96 { 97 exponent += MULTIPLIER_DP; 98 val.u32[1] = sign | (exponent << 20) | (val.u32[1] & 0x000fffff); 99 val.f64 = val.f64 * VAL_2PMMULTIPLIER_DP; 100 return val.f64; 101 } 102 103 if(exponent > 2046)/*overflow*/ 104 { 105 val.u32[1] = sign | 0x7ff00000; 106 val.u32[0] = 0x00000000; 107 return __amd_handle_error("ldexp", __amd_ldexp, val.u64, _OVERFLOW, AMD_F_INEXACT|AMD_F_OVERFLOW, ERANGE ,x, (double)n, 2); 108 } 109 110 val.u32[1] = sign | (exponent << 20) | (val.u32[1] & 0x000fffff); 111 return val.f64; 112 } 113 114 115 116