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_REMAINDER_PIBY2F_INLINE 31 #define USE_VALF_WITH_FLAGS 32 #define USE_NANF_WITH_FLAGS 33 #define USE_HANDLE_ERRORF 34 #include "libm_inlines.h" 35 #undef USE_VALF_WITH_FLAGS 36 #undef USE_NANF_WITH_FLAGS 37 #undef USE_REMAINDER_PIBY2F_INLINE 38 #undef USE_HANDLE_ERRORF 39 40 #include "libm_errno.h" 41 42 #ifdef _MSC_VER 43 // Disable "C4163: not available as intrinsic function" warning that older 44 // compilers may issue here. 45 #pragma warning(disable:4163) 46 #pragma function(tanf) 47 #endif 48 49 /* tan(x) approximation valid on the interval [-pi/4,pi/4]. 50 If recip is true return -1/tan(x) instead. */ 51 static inline double tanf_piby4(double x, int recip) 52 { 53 double r, t; 54 55 /* Core Remez [1,2] approximation to tan(x) on the 56 interval [0,pi/4]. */ 57 r = x*x; 58 t = x + x*r* 59 (0.385296071263995406715129e0 - 60 0.172032480471481694693109e-1 * r) / 61 (0.115588821434688393452299e+1 + 62 (-0.51396505478854532132342e0 + 63 0.1844239256901656082986661e-1 * r) * r); 64 65 if (recip) 66 return -1.0 / t; 67 else 68 return t; 69 } 70 71 72 float tanf(float x) 73 { 74 double r, dx; 75 int region, xneg; 76 77 unsigned long long ux, ax; 78 79 dx = x; 80 81 GET_BITS_DP64(dx, ux); 82 ax = (ux & ~SIGNBIT_DP64); 83 84 if (ax <= 0x3fe921fb54442d18) /* abs(x) <= pi/4 */ 85 { 86 if (ax < 0x3f80000000000000) /* abs(x) < 2.0^(-7) */ 87 { 88 if (ax < 0x3f20000000000000) /* abs(x) < 2.0^(-13) */ 89 { 90 if (ax == 0x0000000000000000) 91 return x; 92 else 93 return valf_with_flags(x, AMD_F_INEXACT); 94 } 95 else 96 return (float)(dx + dx*dx*dx*0.333333333333333333); 97 } 98 else 99 return (float)tanf_piby4(x, 0); 100 } 101 else if ((ux & EXPBITS_DP64) == EXPBITS_DP64) 102 { 103 /* x is either NaN or infinity */ 104 if (ux & MANTBITS_DP64) 105 { 106 /* x is NaN */ 107 unsigned int ufx; 108 GET_BITS_SP32(x, ufx); 109 return _handle_errorf("tanf", OP_TAN, ufx|0x00400000, _DOMAIN, 0, 110 EDOM, x, 0.0F, 1); 111 } 112 else 113 { 114 /* x is infinity. Return a NaN */ 115 return _handle_errorf("tanf", OP_TAN, INDEFBITPATT_SP32, _DOMAIN, AMD_F_INVALID, 116 EDOM, x, 0.0F, 1); 117 } 118 } 119 120 xneg = (int)(ux >> 63); 121 122 if (xneg) 123 dx = -dx; 124 125 if (dx < 5.0e5) 126 { 127 /* For these size arguments we can just carefully subtract the 128 appropriate multiple of pi/2, using extra precision where 129 dx is close to an exact multiple of pi/2 */ 130 static const double 131 twobypi = 6.36619772367581382433e-01, /* 0x3fe45f306dc9c883 */ 132 piby2_1 = 1.57079632673412561417e+00, /* 0x3ff921fb54400000 */ 133 piby2_1tail = 6.07710050650619224932e-11, /* 0x3dd0b4611a626331 */ 134 piby2_2 = 6.07710050630396597660e-11, /* 0x3dd0b4611a600000 */ 135 piby2_2tail = 2.02226624879595063154e-21, /* 0x3ba3198a2e037073 */ 136 piby2_3 = 2.02226624871116645580e-21, /* 0x3ba3198a2e000000 */ 137 piby2_3tail = 8.47842766036889956997e-32; /* 0x397b839a252049c1 */ 138 double t, rhead, rtail; 139 int npi2; 140 unsigned long long uy, xexp, expdiff; 141 xexp = ax >> EXPSHIFTBITS_DP64; 142 /* How many pi/2 is dx a multiple of? */ 143 if (ax <= 0x400f6a7a2955385e) /* 5pi/4 */ 144 { 145 if (ax <= 0x4002d97c7f3321d2) /* 3pi/4 */ 146 npi2 = 1; 147 else 148 npi2 = 2; 149 } 150 else if (ax <= 0x401c463abeccb2bb) /* 9pi/4 */ 151 { 152 if (ax <= 0x4015fdbbe9bba775) /* 7pi/4 */ 153 npi2 = 3; 154 else 155 npi2 = 4; 156 } 157 else 158 npi2 = (int)(dx * twobypi + 0.5); 159 /* Subtract the multiple from dx to get an extra-precision remainder */ 160 rhead = dx - npi2 * piby2_1; 161 rtail = npi2 * piby2_1tail; 162 GET_BITS_DP64(rhead, uy); 163 expdiff = xexp - ((uy & EXPBITS_DP64) >> EXPSHIFTBITS_DP64); 164 if (expdiff > 15) 165 { 166 /* The remainder is pretty small compared with dx, which 167 implies that dx is a near multiple of pi/2 168 (dx matches the multiple to at least 15 bits) */ 169 t = rhead; 170 rtail = npi2 * piby2_2; 171 rhead = t - rtail; 172 rtail = npi2 * piby2_2tail - ((t - rhead) - rtail); 173 if (expdiff > 48) 174 { 175 /* dx matches a pi/2 multiple to at least 48 bits */ 176 t = rhead; 177 rtail = npi2 * piby2_3; 178 rhead = t - rtail; 179 rtail = npi2 * piby2_3tail - ((t - rhead) - rtail); 180 } 181 } 182 r = rhead - rtail; 183 region = npi2 & 3; 184 } 185 else 186 { 187 /* Reduce x into range [-pi/4,pi/4] */ 188 __remainder_piby2f_inline(ax, &r, ®ion); 189 } 190 191 if (xneg) 192 return (float)-tanf_piby4(r, region & 1); 193 else 194 return (float)tanf_piby4(r, region & 1); 195 } 196