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_NAN_WITH_FLAGS 32 #define USE_HANDLE_ERRORF 33 #include "libm_inlines.h" 34 #undef USE_VALF_WITH_FLAGS 35 #undef USE_NAN_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(atanf) 45 #endif 46 47 float FN_PROTOTYPE(atanf)(float fx) 48 { 49 50 /* Some constants and split constants. */ 51 52 static double piby2 = 1.5707963267948966e+00; /* 0x3ff921fb54442d18 */ 53 54 double c, v, s, q, z; 55 unsigned int xnan; 56 57 double x = fx; 58 59 /* Find properties of argument fx. */ 60 61 unsigned long long ux, aux, xneg; 62 63 GET_BITS_DP64(x, ux); 64 aux = ux & ~SIGNBIT_DP64; 65 xneg = ux & SIGNBIT_DP64; 66 67 v = x; 68 if (xneg) v = -x; 69 70 /* Argument reduction to range [-7/16,7/16] */ 71 72 if (aux < 0x3fdc000000000000) /* v < 7./16. */ 73 { 74 x = v; 75 c = 0.0; 76 } 77 else if (aux < 0x3fe6000000000000) /* v < 11./16. */ 78 { 79 x = (2.0*v-1.0)/(2.0+v); 80 /* c = arctan(0.5) */ 81 c = 4.63647609000806093515e-01; /* 0x3fddac670561bb4f */ 82 } 83 else if (aux < 0x3ff3000000000000) /* v < 19./16. */ 84 { 85 x = (v-1.0)/(1.0+v); 86 /* c = arctan(1.) */ 87 c = 7.85398163397448278999e-01; /* 0x3fe921fb54442d18 */ 88 } 89 else if (aux < 0x4003800000000000) /* v < 39./16. */ 90 { 91 x = (v-1.5)/(1.0+1.5*v); 92 /* c = arctan(1.5) */ 93 c = 9.82793723247329054082e-01; /* 0x3fef730bd281f69b */ 94 } 95 else 96 { 97 98 xnan = (aux > PINFBITPATT_DP64); 99 100 if (xnan) 101 { 102 /* x is NaN */ 103 unsigned int uhx; 104 GET_BITS_SP32(fx, uhx); 105 return _handle_errorf("atanf", OP_ATAN, uhx|0x00400000, _DOMAIN, 106 0, EDOM, fx, 0.0F, 1); 107 } 108 else if (v > 0x4c80000000000000) 109 { /* abs(x) > 2^26 => arctan(1/x) is 110 insignificant compared to piby2 */ 111 if (xneg) 112 return valf_with_flags((float)-piby2, AMD_F_INEXACT); 113 else 114 return valf_with_flags((float)piby2, AMD_F_INEXACT); 115 } 116 117 x = -1.0/v; 118 /* c = arctan(infinity) */ 119 c = 1.57079632679489655800e+00; /* 0x3ff921fb54442d18 */ 120 } 121 122 /* Core approximation: Remez(2,2) on [-7/16,7/16] */ 123 124 s = x*x; 125 q = x*s* 126 (0.296528598819239217902158651186e0 + 127 (0.192324546402108583211697690500e0 + 128 0.470677934286149214138357545549e-2*s)*s)/ 129 (0.889585796862432286486651434570e0 + 130 (0.111072499995399550138837673349e1 + 131 0.299309699959659728404442796915e0*s)*s); 132 133 z = c - (q - x); 134 135 if (xneg) z = -z; 136 return (float)z; 137 } 138