1 2 /******************************************************************************* 3 MIT License 4 ----------- 5 6 Copyright (c) 2002-2019 Advanced Micro Devices, Inc. 7 8 Permission is hereby granted, free of charge, to any person obtaining a copy 9 of this Software and associated documentaon files (the "Software"), to deal 10 in the Software without restriction, including without limitation the rights 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 copies of the Software, and to permit persons to whom the Software is 13 furnished to do so, subject to the following conditions: 14 15 The above copyright notice and this permission notice shall be included in 16 all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 THE SOFTWARE. 25 *******************************************************************************/ 26 27 #include "libm.h" 28 #include "libm_util.h" 29 30 #define USE_VALF_WITH_FLAGS 31 #define USE_NANF_WITH_FLAGS 32 #define USE_HANDLE_ERRORF 33 #include "libm_inlines.h" 34 #undef USE_NANF_WITH_FLAGS 35 #undef USE_VALF_WITH_FLAGS 36 #undef USE_HANDLE_ERRORF 37 38 #include "libm_errno.h" 39 40 #ifdef _MSC_VER 41 // Disable "C4163: not available as intrinsic function" warning that older 42 // compilers may issue here. 43 #pragma warning(disable:4163) 44 #pragma function(asinf) 45 #endif 46 47 float FN_PROTOTYPE(asinf)(float x) 48 { 49 /* Computes arcsin(x). 50 The argument is first reduced by noting that arcsin(x) 51 is invalid for abs(x) > 1 and arcsin(-x) = -arcsin(x). 52 For denormal and small arguments arcsin(x) = x to machine 53 accuracy. Remaining argument ranges are handled as follows. 54 For abs(x) <= 0.5 use 55 arcsin(x) = x + x^3*R(x^2) 56 where R(x^2) is a rational minimax approximation to 57 (arcsin(x) - x)/x^3. 58 For abs(x) > 0.5 exploit the identity: 59 arcsin(x) = pi/2 - 2*arcsin(sqrt(1-x)/2) 60 together with the above rational approximation, and 61 reconstruct the terms carefully. 62 */ 63 64 /* Some constants and split constants. */ 65 66 static const float 67 piby2_tail = 7.5497894159e-08F, /* 0x33a22168 */ 68 hpiby2_head = 7.8539812565e-01F, /* 0x3f490fda */ 69 piby2 = 1.5707963705e+00F; /* 0x3fc90fdb */ 70 float u, v, y, s = 0.0F, r; 71 int xexp, xnan, transform = 0; 72 73 unsigned int ux, aux, xneg; 74 GET_BITS_SP32(x, ux); 75 aux = ux & ~SIGNBIT_SP32; 76 xneg = (ux & SIGNBIT_SP32); 77 xnan = (aux > PINFBITPATT_SP32); 78 xexp = (int)((ux & EXPBITS_SP32) >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32; 79 80 /* Special cases */ 81 82 if (xnan) 83 { 84 return _handle_errorf("asinf", OP_ASIN, ux|0x00400000, _DOMAIN, 0, 85 EDOM, x, 0.0F, 1); 86 } 87 else if (xexp < -14) 88 /* y small enough that arcsin(x) = x */ 89 return valf_with_flags(x, AMD_F_INEXACT); 90 else if (xexp >= 0) 91 { 92 /* abs(x) >= 1.0 */ 93 if (x == 1.0F) 94 return valf_with_flags(piby2, AMD_F_INEXACT); 95 else if (x == -1.0F) 96 return valf_with_flags(-piby2, AMD_F_INEXACT); 97 else 98 return _handle_errorf("asinf", OP_ASIN, INDEFBITPATT_SP32, _DOMAIN, 99 AMD_F_INVALID, EDOM, x, 0.0F, 1); 100 } 101 102 if (xneg) y = -x; 103 else y = x; 104 105 transform = (xexp >= -1); /* abs(x) >= 0.5 */ 106 107 if (transform) 108 { /* Transform y into the range [0,0.5) */ 109 r = 0.5F*(1.0F - y); 110 /* VC++ intrinsic call */ 111 _mm_store_ss(&s, _mm_sqrt_ss(_mm_load_ss(&r))); 112 y = s; 113 } 114 else 115 r = y*y; 116 117 /* Use a rational approximation for [0.0, 0.5] */ 118 119 u=r*(0.184161606965100694821398249421F + 120 (-0.0565298683201845211985026327361F + 121 (-0.0133819288943925804214011424456F - 122 0.00396137437848476485201154797087F*r)*r)*r)/ 123 (1.10496961524520294485512696706F - 124 0.836411276854206731913362287293F*r); 125 126 if (transform) 127 { 128 /* Reconstruct asin carefully in transformed region */ 129 float c, s1, p, q; 130 unsigned int us; 131 GET_BITS_SP32(s, us); 132 PUT_BITS_SP32(0xffff0000 & us, s1); 133 c = (r-s1*s1)/(s+s1); 134 p = 2.0F*s*u - (piby2_tail-2.0F*c); 135 q = hpiby2_head - 2.0F*s1; 136 v = hpiby2_head - (p-q); 137 } 138 else 139 { 140 /* Use a temporary variable to prevent VC++ rearranging 141 y + y*u 142 into 143 y * (1 + u) 144 and getting an incorrectly rounded result */ 145 float tmp; 146 tmp = y * u; 147 v = y + tmp; 148 } 149 150 if (xneg) return -v; 151 else return v; 152 } 153