1/* 2 * Copyright (c) 2014 Advanced Micro Devices, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a copy 5 * of this software and associated documentation files (the "Software"), to deal 6 * in the Software without restriction, including without limitation the rights 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 * copies of the Software, and to permit persons to whom the Software is 9 * furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 * THE SOFTWARE. 21 */ 22 23#include <clc/clc.h> 24#include <clc/clcmacro.h> 25#include <clc/math/math.h> 26 27_CLC_OVERLOAD _CLC_DEF float atan(float x) 28{ 29 const float piby2 = 1.5707963267948966f; // 0x3ff921fb54442d18 30 31 uint ux = as_uint(x); 32 uint aux = ux & EXSIGNBIT_SP32; 33 uint sx = ux ^ aux; 34 35 float spiby2 = as_float(sx | as_uint(piby2)); 36 37 float v = as_float(aux); 38 39 // Return for NaN 40 float ret = x; 41 42 // 2^26 <= |x| <= Inf => atan(x) is close to piby2 43 ret = aux <= PINFBITPATT_SP32 ? spiby2 : ret; 44 45 // Reduce arguments 2^-19 <= |x| < 2^26 46 47 // 39/16 <= x < 2^26 48 x = -MATH_RECIP(v); 49 float c = 1.57079632679489655800f; // atan(infinity) 50 51 // 19/16 <= x < 39/16 52 int l = aux < 0x401c0000; 53 float xx = MATH_DIVIDE(v - 1.5f, mad(v, 1.5f, 1.0f)); 54 x = l ? xx : x; 55 c = l ? 9.82793723247329054082e-01f : c; // atan(1.5) 56 57 // 11/16 <= x < 19/16 58 l = aux < 0x3f980000U; 59 xx = MATH_DIVIDE(v - 1.0f, 1.0f + v); 60 x = l ? xx : x; 61 c = l ? 7.85398163397448278999e-01f : c; // atan(1) 62 63 // 7/16 <= x < 11/16 64 l = aux < 0x3f300000; 65 xx = MATH_DIVIDE(mad(v, 2.0f, -1.0f), 2.0f + v); 66 x = l ? xx : x; 67 c = l ? 4.63647609000806093515e-01f : c; // atan(0.5) 68 69 // 2^-19 <= x < 7/16 70 l = aux < 0x3ee00000; 71 x = l ? v : x; 72 c = l ? 0.0f : c; 73 74 // Core approximation: Remez(2,2) on [-7/16,7/16] 75 76 float s = x * x; 77 float a = mad(s, 78 mad(s, 0.470677934286149214138357545549e-2f, 0.192324546402108583211697690500f), 79 0.296528598819239217902158651186f); 80 81 float b = mad(s, 82 mad(s, 0.299309699959659728404442796915f, 0.111072499995399550138837673349e1f), 83 0.889585796862432286486651434570f); 84 85 float q = x * s * MATH_DIVIDE(a, b); 86 87 float z = c - (q - x); 88 float zs = as_float(sx | as_uint(z)); 89 90 ret = aux < 0x4c800000 ? zs : ret; 91 92 // |x| < 2^-19 93 ret = aux < 0x36000000 ? as_float(ux) : ret; 94 return ret; 95} 96 97_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, atan, float); 98 99#ifdef cl_khr_fp64 100 101#pragma OPENCL EXTENSION cl_khr_fp64 : enable 102 103 104_CLC_OVERLOAD _CLC_DEF double atan(double x) 105{ 106 const double piby2 = 1.5707963267948966e+00; // 0x3ff921fb54442d18 107 108 double v = fabs(x); 109 110 // 2^56 > v > 39/16 111 double a = -1.0; 112 double b = v; 113 // (chi + clo) = arctan(infinity) 114 double chi = 1.57079632679489655800e+00; 115 double clo = 6.12323399573676480327e-17; 116 117 double ta = v - 1.5; 118 double tb = 1.0 + 1.5 * v; 119 int l = v <= 0x1.38p+1; // 39/16 > v > 19/16 120 a = l ? ta : a; 121 b = l ? tb : b; 122 // (chi + clo) = arctan(1.5) 123 chi = l ? 9.82793723247329054082e-01 : chi; 124 clo = l ? 1.39033110312309953701e-17 : clo; 125 126 ta = v - 1.0; 127 tb = 1.0 + v; 128 l = v <= 0x1.3p+0; // 19/16 > v > 11/16 129 a = l ? ta : a; 130 b = l ? tb : b; 131 // (chi + clo) = arctan(1.) 132 chi = l ? 7.85398163397448278999e-01 : chi; 133 clo = l ? 3.06161699786838240164e-17 : clo; 134 135 ta = 2.0 * v - 1.0; 136 tb = 2.0 + v; 137 l = v <= 0x1.6p-1; // 11/16 > v > 7/16 138 a = l ? ta : a; 139 b = l ? tb : b; 140 // (chi + clo) = arctan(0.5) 141 chi = l ? 4.63647609000806093515e-01 : chi; 142 clo = l ? 2.26987774529616809294e-17 : clo; 143 144 l = v <= 0x1.cp-2; // v < 7/16 145 a = l ? v : a; 146 b = l ? 1.0 : b;; 147 chi = l ? 0.0 : chi; 148 clo = l ? 0.0 : clo; 149 150 // Core approximation: Remez(4,4) on [-7/16,7/16] 151 double r = a / b; 152 double s = r * r; 153 double qn = fma(s, 154 fma(s, 155 fma(s, 156 fma(s, 0.142316903342317766e-3, 157 0.304455919504853031e-1), 158 0.220638780716667420e0), 159 0.447677206805497472e0), 160 0.268297920532545909e0); 161 162 double qd = fma(s, 163 fma(s, 164 fma(s, 165 fma(s, 0.389525873944742195e-1, 166 0.424602594203847109e0), 167 0.141254259931958921e1), 168 0.182596787737507063e1), 169 0.804893761597637733e0); 170 171 double q = r * s * qn / qd; 172 r = chi - ((q - clo) - r); 173 174 double z = isnan(x) ? x : piby2; 175 z = v <= 0x1.0p+56 ? r : z; 176 z = v < 0x1.0p-26 ? v : z; 177 return x == v ? z : -z; 178} 179 180_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, atan, double); 181 182#endif // cl_khr_fp64 183 184#ifdef cl_khr_fp16 185 186#pragma OPENCL EXTENSION cl_khr_fp16 : enable 187 188_CLC_DEFINE_UNARY_BUILTIN_FP16(atan) 189 190#endif 191