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
25#include "math.h"
26#include "sincospiF_piby4.h"
27#include "../clcmacro.h"
28#ifdef cl_khr_fp64
29#include "sincosD_piby4.h"
30#endif
31
32_CLC_OVERLOAD _CLC_DEF float sinpi(float x)
33{
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);
41
42    // Initialize with return for +-Inf and NaN
43    int ir = 0x7fc00000;
44
45    // 2^23 <= |x| < Inf, the result is always integer
46    ir = ix < 0x7f800000 ? xsgn : ir;
47
48    // 0x1.0p-7 <= |x| < 2^23, result depends on which 0.25 interval
49
50    // r < 1.0
51    float a = 1.0f - r;
52    int e = 0;
53
54    // r <= 0.75
55    int c = r <= 0.75f;
56    a = c ? r - 0.5f : a;
57    e = c ? 1 : e;
58
59    // r < 0.5
60    c = r < 0.5f;
61    a = c ? 0.5f - r : a;
62
63    // 0 < r <= 0.25
64    c = r <= 0.25f;
65    a = c ? r : a;
66    e = c ? 0 : e;
67
68    float2 t = __libclc__sincosf_piby4(a * M_PI_F);
69    int jr = xodd ^ as_int(e ? t.hi : t.lo);
70
71    ir = ix < 0x4b000000 ? jr : ir;
72
73    return as_float(ir);
74}
75
76_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, sinpi, float);
77
78#ifdef cl_khr_fp64
79
80#pragma OPENCL EXTENSION cl_khr_fp64 : enable
81
82_CLC_OVERLOAD _CLC_DEF double sinpi(double x)
83{
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);
91
92    // Initialize with return for +-Inf and NaN
93    long ir = 0x7ff8000000000000L;
94
95    // 2^23 <= |x| < Inf, the result is always integer
96    ir = ix < 0x7ff0000000000000 ? xsgn : ir;
97
98    // 0x1.0p-7 <= |x| < 2^23, result depends on which 0.25 interval
99
100    // r < 1.0
101    double a = 1.0 - r;
102    int e = 0;
103
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;
109
110    // r < 0.5
111    c = r < 0.5;
112    t = 0.5 - r;
113    a = c ? t : a;
114
115    // r <= 0.25
116    c = r <= 0.25;
117    a = c ? r : a;
118    e = c ? 0 : e;
119
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);
123
124    ir = ax < 0x1.0p+52 ? jr : ir;
125
126    return as_double(ir);
127}
128
129_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, sinpi, double)
130
131#endif
132