1/* 2 * Copyright (c) 2014,2015 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 acospi(float x) { 28 // Computes arccos(x). 29 // The argument is first reduced by noting that arccos(x) 30 // is invalid for abs(x) > 1. For denormal and small 31 // arguments arccos(x) = pi/2 to machine accuracy. 32 // Remaining argument ranges are handled as follows. 33 // For abs(x) <= 0.5 use 34 // arccos(x) = pi/2 - arcsin(x) 35 // = pi/2 - (x + x^3*R(x^2)) 36 // where R(x^2) is a rational minimax approximation to 37 // (arcsin(x) - x)/x^3. 38 // For abs(x) > 0.5 exploit the identity: 39 // arccos(x) = pi - 2*arcsin(sqrt(1-x)/2) 40 // together with the above rational approximation, and 41 // reconstruct the terms carefully. 42 43 44 // Some constants and split constants. 45 const float pi = 3.1415926535897933e+00f; 46 const float piby2_head = 1.5707963267948965580e+00f; /* 0x3ff921fb54442d18 */ 47 const float piby2_tail = 6.12323399573676603587e-17f; /* 0x3c91a62633145c07 */ 48 49 uint ux = as_uint(x); 50 uint aux = ux & ~SIGNBIT_SP32; 51 int xneg = ux != aux; 52 int xexp = (int)(aux >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32; 53 54 float y = as_float(aux); 55 56 // transform if |x| >= 0.5 57 int transform = xexp >= -1; 58 59 float y2 = y * y; 60 float yt = 0.5f * (1.0f - y); 61 float r = transform ? yt : y2; 62 63 // Use a rational approximation for [0.0, 0.5] 64 float a = mad(r, mad(r, mad(r, -0.00396137437848476485201154797087F, -0.0133819288943925804214011424456F), 65 -0.0565298683201845211985026327361F), 66 0.184161606965100694821398249421F); 67 float b = mad(r, -0.836411276854206731913362287293F, 1.10496961524520294485512696706F); 68 float u = r * MATH_DIVIDE(a, b); 69 70 float s = MATH_SQRT(r); 71 y = s; 72 float s1 = as_float(as_uint(s) & 0xffff0000); 73 float c = MATH_DIVIDE(r - s1 * s1, s + s1); 74 // float rettn = 1.0f - MATH_DIVIDE(2.0f * (s + (y * u - piby2_tail)), pi); 75 float rettn = 1.0f - MATH_DIVIDE(2.0f * (s + mad(y, u, -piby2_tail)), pi); 76 // float rettp = MATH_DIVIDE(2.0F * s1 + (2.0F * c + 2.0F * y * u), pi); 77 float rettp = MATH_DIVIDE(2.0f*(s1 + mad(y, u, c)), pi); 78 float rett = xneg ? rettn : rettp; 79 // float ret = MATH_DIVIDE(piby2_head - (x - (piby2_tail - x * u)), pi); 80 float ret = MATH_DIVIDE(piby2_head - (x - mad(x, -u, piby2_tail)), pi); 81 82 ret = transform ? rett : ret; 83 ret = aux > 0x3f800000U ? as_float(QNANBITPATT_SP32) : ret; 84 ret = ux == 0x3f800000U ? 0.0f : ret; 85 ret = ux == 0xbf800000U ? 1.0f : ret; 86 ret = xexp < -26 ? 0.5f : ret; 87 return ret; 88} 89 90_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, acospi, float) 91 92#ifdef cl_khr_fp64 93#pragma OPENCL EXTENSION cl_khr_fp64 : enable 94 95_CLC_OVERLOAD _CLC_DEF double acospi(double x) { 96 // Computes arccos(x). 97 // The argument is first reduced by noting that arccos(x) 98 // is invalid for abs(x) > 1. For denormal and small 99 // arguments arccos(x) = pi/2 to machine accuracy. 100 // Remaining argument ranges are handled as follows. 101 // For abs(x) <= 0.5 use 102 // arccos(x) = pi/2 - arcsin(x) 103 // = pi/2 - (x + x^3*R(x^2)) 104 // where R(x^2) is a rational minimax approximation to 105 // (arcsin(x) - x)/x^3. 106 // For abs(x) > 0.5 exploit the identity: 107 // arccos(x) = pi - 2*arcsin(sqrt(1-x)/2) 108 // together with the above rational approximation, and 109 // reconstruct the terms carefully. 110 111 const double pi = 0x1.921fb54442d18p+1; 112 const double piby2_tail = 6.12323399573676603587e-17; /* 0x3c91a62633145c07 */ 113 114 double y = fabs(x); 115 int xneg = as_int2(x).hi < 0; 116 int xexp = (as_int2(y).hi >> 20) - EXPBIAS_DP64; 117 118 // abs(x) >= 0.5 119 int transform = xexp >= -1; 120 121 // Transform y into the range [0,0.5) 122 double r1 = 0.5 * (1.0 - y); 123 double s = sqrt(r1); 124 double r = y * y; 125 r = transform ? r1 : r; 126 y = transform ? s : y; 127 128 // Use a rational approximation for [0.0, 0.5] 129 double un = fma(r, 130 fma(r, 131 fma(r, 132 fma(r, 133 fma(r, 0.0000482901920344786991880522822991, 134 0.00109242697235074662306043804220), 135 -0.0549989809235685841612020091328), 136 0.275558175256937652532686256258), 137 -0.445017216867635649900123110649), 138 0.227485835556935010735943483075); 139 140 double ud = fma(r, 141 fma(r, 142 fma(r, 143 fma(r, 0.105869422087204370341222318533, 144 -0.943639137032492685763471240072), 145 2.76568859157270989520376345954), 146 -3.28431505720958658909889444194), 147 1.36491501334161032038194214209); 148 149 double u = r * MATH_DIVIDE(un, ud); 150 151 // Reconstruct acos carefully in transformed region 152 double res1 = fma(-2.0, MATH_DIVIDE(s + fma(y, u, -piby2_tail), pi), 1.0); 153 double s1 = as_double(as_ulong(s) & 0xffffffff00000000UL); 154 double c = MATH_DIVIDE(fma(-s1, s1, r), s + s1); 155 double res2 = MATH_DIVIDE(fma(2.0, s1, fma(2.0, c, 2.0 * y * u)), pi); 156 res1 = xneg ? res1 : res2; 157 res2 = 0.5 - fma(x, u, x) / pi; 158 res1 = transform ? res1 : res2; 159 160 const double qnan = as_double(QNANBITPATT_DP64); 161 res2 = x == 1.0 ? 0.0 : qnan; 162 res2 = x == -1.0 ? 1.0 : res2; 163 res1 = xexp >= 0 ? res2 : res1; 164 res1 = xexp < -56 ? 0.5 : res1; 165 166 return res1; 167} 168 169_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, acospi, double) 170 171#endif 172 173#ifdef cl_khr_fp16 174 175#pragma OPENCL EXTENSION cl_khr_fp16 : enable 176 177_CLC_DEFINE_UNARY_BUILTIN_FP16(acospi) 178 179#endif 180