[analyzer][NFC] Factor out SymbolManager::get<*> (#121781)
[llvm-project.git] / libclc / generic / lib / math / sincosD_piby4.h
blobc98488b33ed0c19dc3e5096e98ca5e90a80bbaa0
1 /*
2 * Copyright (c) 2014 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
23 #pragma OPENCL EXTENSION cl_khr_fp64 : enable
25 _CLC_INLINE double2
26 __libclc__sincos_piby4(double x, double xx)
28 // Taylor series for sin(x) is x - x^3/3! + x^5/5! - x^7/7! ...
29 // = x * (1 - x^2/3! + x^4/5! - x^6/7! ...
30 // = x * f(w)
31 // where w = x*x and f(w) = (1 - w/3! + w^2/5! - w^3/7! ...
32 // We use a minimax approximation of (f(w) - 1) / w
33 // because this produces an expansion in even powers of x.
34 // If xx (the tail of x) is non-zero, we add a correction
35 // term g(x,xx) = (1-x*x/2)*xx to the result, where g(x,xx)
36 // is an approximation to cos(x)*sin(xx) valid because
37 // xx is tiny relative to x.
39 // Taylor series for cos(x) is 1 - x^2/2! + x^4/4! - x^6/6! ...
40 // = f(w)
41 // where w = x*x and f(w) = (1 - w/2! + w^2/4! - w^3/6! ...
42 // We use a minimax approximation of (f(w) - 1 + w/2) / (w*w)
43 // because this produces an expansion in even powers of x.
44 // If xx (the tail of x) is non-zero, we subtract a correction
45 // term g(x,xx) = x*xx to the result, where g(x,xx)
46 // is an approximation to sin(x)*sin(xx) valid because
47 // xx is tiny relative to x.
49 const double sc1 = -0.166666666666666646259241729;
50 const double sc2 = 0.833333333333095043065222816e-2;
51 const double sc3 = -0.19841269836761125688538679e-3;
52 const double sc4 = 0.275573161037288022676895908448e-5;
53 const double sc5 = -0.25051132068021699772257377197e-7;
54 const double sc6 = 0.159181443044859136852668200e-9;
56 const double cc1 = 0.41666666666666665390037e-1;
57 const double cc2 = -0.13888888888887398280412e-2;
58 const double cc3 = 0.248015872987670414957399e-4;
59 const double cc4 = -0.275573172723441909470836e-6;
60 const double cc5 = 0.208761463822329611076335e-8;
61 const double cc6 = -0.113826398067944859590880e-10;
63 double x2 = x * x;
64 double x3 = x2 * x;
65 double r = 0.5 * x2;
66 double t = 1.0 - r;
68 double sp = fma(fma(fma(fma(sc6, x2, sc5), x2, sc4), x2, sc3), x2, sc2);
70 double cp = t + fma(fma(fma(fma(fma(fma(cc6, x2, cc5), x2, cc4), x2, cc3), x2, cc2), x2, cc1),
71 x2*x2, fma(x, xx, (1.0 - t) - r));
73 double2 ret;
74 ret.lo = x - fma(-x3, sc1, fma(fma(-x3, sp, 0.5*xx), x2, -xx));
75 ret.hi = cp;
77 return ret;
80 _CLC_INLINE double2
81 __clc_tan_piby4(double x, double xx)
83 const double piby4_lead = 7.85398163397448278999e-01; // 0x3fe921fb54442d18
84 const double piby4_tail = 3.06161699786838240164e-17; // 0x3c81a62633145c06
86 // In order to maintain relative precision transform using the identity:
87 // tan(pi/4-x) = (1-tan(x))/(1+tan(x)) for arguments close to pi/4.
88 // Similarly use tan(x-pi/4) = (tan(x)-1)/(tan(x)+1) close to -pi/4.
90 int ca = x > 0.68;
91 int cb = x < -0.68;
92 double transform = ca ? 1.0 : 0.0;
93 transform = cb ? -1.0 : transform;
95 double tx = fma(-transform, x, piby4_lead) + fma(-transform, xx, piby4_tail);
96 int c = ca | cb;
97 x = c ? tx : x;
98 xx = c ? 0.0 : xx;
100 // Core Remez [2,3] approximation to tan(x+xx) on the interval [0,0.68].
101 double t1 = x;
102 double r = fma(2.0, x*xx, x*x);
104 double a = fma(r,
105 fma(r, 0.224044448537022097264602535574e-3, -0.229345080057565662883358588111e-1),
106 0.372379159759792203640806338901e0);
108 double b = fma(r,
109 fma(r,
110 fma(r, -0.232371494088563558304549252913e-3, 0.260656620398645407524064091208e-1),
111 -0.515658515729031149329237816945e0),
112 0.111713747927937668539901657944e1);
114 double t2 = fma(MATH_DIVIDE(a, b), x*r, xx);
116 double tp = t1 + t2;
118 // Compute -1.0/(t1 + t2) accurately
119 double z1 = as_double(as_long(tp) & 0xffffffff00000000L);
120 double z2 = t2 - (z1 - t1);
121 double trec = -MATH_RECIP(tp);
122 double trec_top = as_double(as_long(trec) & 0xffffffff00000000L);
124 double tpr = fma(fma(trec_top, z2, fma(trec_top, z1, 1.0)), trec, trec_top);
126 double tpt = transform * (1.0 - MATH_DIVIDE(2.0*tp, 1.0 + tp));
127 double tptr = transform * (MATH_DIVIDE(2.0*tp, tp - 1.0) - 1.0);
129 double2 ret;
130 ret.lo = c ? tpt : tp;
131 ret.hi = c ? tptr : tpr;
132 return ret;