xref: /llvm-project/libclc/generic/lib/math/sin.cl (revision 78b5bb702fe97fe85f66d72598d0dfa7c49fe001)
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 "sincos_helpers.h"
24#include <clc/clc.h>
25#include <clc/clcmacro.h>
26#include <clc/math/math.h>
27
28_CLC_OVERLOAD _CLC_DEF float sin(float x)
29{
30    int ix = as_int(x);
31    int ax = ix & 0x7fffffff;
32    float dx = as_float(ax);
33
34    float r0, r1;
35    int regn = __clc_argReductionS(&r0, &r1, dx);
36
37    float ss = __clc_sinf_piby4(r0, r1);
38    float cc = __clc_cosf_piby4(r0, r1);
39
40    float s = (regn & 1) != 0 ? cc : ss;
41    s = as_float(as_int(s) ^ ((regn > 1) << 31) ^ (ix ^ ax));
42
43    s = ax >= PINFBITPATT_SP32 ? as_float(QNANBITPATT_SP32) : s;
44
45    //Subnormals
46    s = x == 0.0f ? x : s;
47
48    return s;
49}
50
51_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, sin, float);
52
53#ifdef cl_khr_fp64
54
55#pragma OPENCL EXTENSION cl_khr_fp64 : enable
56
57_CLC_OVERLOAD _CLC_DEF double sin(double x) {
58    double y = fabs(x);
59
60    double r, rr;
61    int regn;
62
63    if (y < 0x1.0p+47)
64        __clc_remainder_piby2_medium(y, &r, &rr, &regn);
65    else
66        __clc_remainder_piby2_large(y, &r, &rr, &regn);
67
68    double2 sc = __clc_sincos_piby4(r, rr);
69
70    int2 s = as_int2(regn & 1 ? sc.hi : sc.lo);
71    s.hi ^= ((regn > 1) << 31) ^ ((x < 0.0) << 31);
72
73    return  isinf(x) | isnan(x) ? as_double(QNANBITPATT_DP64) : as_double(s);
74}
75
76_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, sin, double);
77
78#endif
79
80#ifdef cl_khr_fp16
81
82#pragma OPENCL EXTENSION cl_khr_fp16 : enable
83
84_CLC_DEFINE_UNARY_BUILTIN_FP16(sin)
85
86#endif
87