[analyzer][NFC] Factor out SymbolManager::get<*> (#121781)
[llvm-project.git] / libclc / generic / lib / math / sinpi.cl
blob520bba5415c7c0e1a9e9a89fc636dc62a8beaa88
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 #include <clc/clc.h>
24 #include <clc/clcmacro.h>
26 #include "math.h"
27 #include "sincospiF_piby4.h"
28 #ifdef cl_khr_fp64
29 #include "sincosD_piby4.h"
30 #endif
32 _CLC_OVERLOAD _CLC_DEF float sinpi(float x)
34 int ix = as_int(x);
35 int xsgn = ix & 0x80000000;
36 ix ^= xsgn;
37 float ax = as_float(ix);
38 int iax = (int)ax;
39 float r = ax - iax;
40 int xodd = xsgn ^ (iax & 0x1 ? 0x80000000 : 0);
42 // Initialize with return for +-Inf and NaN
43 int ir = 0x7fc00000;
45 // 2^23 <= |x| < Inf, the result is always integer
46 ir = ix < 0x7f800000 ? xsgn : ir;
48 // 0x1.0p-7 <= |x| < 2^23, result depends on which 0.25 interval
50 // r < 1.0
51 float a = 1.0f - r;
52 int e = 0;
54 // r <= 0.75
55 int c = r <= 0.75f;
56 a = c ? r - 0.5f : a;
57 e = c ? 1 : e;
59 // r < 0.5
60 c = r < 0.5f;
61 a = c ? 0.5f - r : a;
63 // 0 < r <= 0.25
64 c = r <= 0.25f;
65 a = c ? r : a;
66 e = c ? 0 : e;
68 float2 t = __libclc__sincosf_piby4(a * M_PI_F);
69 int jr = xodd ^ as_int(e ? t.hi : t.lo);
71 ir = ix < 0x4b000000 ? jr : ir;
73 return as_float(ir);
76 _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, sinpi, float);
78 #ifdef cl_khr_fp64
80 #pragma OPENCL EXTENSION cl_khr_fp64 : enable
82 _CLC_OVERLOAD _CLC_DEF double sinpi(double x)
84 long ix = as_long(x);
85 long xsgn = ix & 0x8000000000000000L;
86 ix ^= xsgn;
87 double ax = as_double(ix);
88 long iax = (long)ax;
89 double r = ax - (double)iax;
90 long xodd = xsgn ^ (iax & 0x1L ? 0x8000000000000000L : 0L);
92 // Initialize with return for +-Inf and NaN
93 long ir = 0x7ff8000000000000L;
95 // 2^23 <= |x| < Inf, the result is always integer
96 ir = ix < 0x7ff0000000000000 ? xsgn : ir;
98 // 0x1.0p-7 <= |x| < 2^23, result depends on which 0.25 interval
100 // r < 1.0
101 double a = 1.0 - r;
102 int e = 0;
104 // r <= 0.75
105 int c = r <= 0.75;
106 double t = r - 0.5;
107 a = c ? t : a;
108 e = c ? 1 : e;
110 // r < 0.5
111 c = r < 0.5;
112 t = 0.5 - r;
113 a = c ? t : a;
115 // r <= 0.25
116 c = r <= 0.25;
117 a = c ? r : a;
118 e = c ? 0 : e;
120 double api = a * M_PI;
121 double2 sc = __libclc__sincos_piby4(api, 0.0);
122 long jr = xodd ^ as_long(e ? sc.hi : sc.lo);
124 ir = ax < 0x1.0p+52 ? jr : ir;
126 return as_double(ir);
129 _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, sinpi, double)
131 #endif
133 _CLC_DEFINE_UNARY_BUILTIN_FP16(sinpi)