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#include <clc/clc.h> 23 24#include "math.h" 25#include "sincos_helpers.h" 26#include "../clcmacro.h" 27#include "tables.h" 28 29_CLC_DEF _CLC_OVERLOAD float __clc_tanpi(float x) 30{ 31 int ix = as_int(x); 32 int xsgn = ix & 0x80000000; 33 int xnsgn = xsgn ^ 0x80000000; 34 ix ^= xsgn; 35 float ax = as_float(ix); 36 int iax = (int)ax; 37 float r = ax - iax; 38 int xodd = xsgn ^ (iax & 0x1 ? 0x80000000 : 0); 39 40 // Initialize with return for +-Inf and NaN 41 int ir = 0x7fc00000; 42 43 // 2^24 <= |x| < Inf, the result is always even integer 44 ir = ix < 0x7f800000 ? xsgn : ir; 45 46 // 2^23 <= |x| < 2^24, the result is always integer 47 ir = ix < 0x4b800000 ? xodd : ir; 48 49 // 0x1.0p-7 <= |x| < 2^23, result depends on which 0.25 interval 50 51 // r < 1.0 52 float a = 1.0f - r; 53 int e = 0; 54 int s = xnsgn; 55 56 // r <= 0.75 57 int c = r <= 0.75f; 58 a = c ? r - 0.5f : a; 59 e = c ? 1 : e; 60 s = c ? xsgn : s; 61 62 // r < 0.5 63 c = r < 0.5f; 64 a = c ? 0.5f - r : a; 65 s = c ? xnsgn : s; 66 67 // 0 < r <= 0.25 68 c = r <= 0.25f; 69 a = c ? r : a; 70 e = c ? 0 : e; 71 s = c ? xsgn : s; 72 73 float t = __clc_tanf_piby4(a * M_PI_F, 0); 74 float tr = -native_recip(t); 75 int jr = s ^ as_int(e ? tr : t); 76 77 jr = r == 0.5f ? xodd | 0x7f800000 : jr; 78 79 ir = ix < 0x4b000000 ? jr : ir; 80 81 return as_float(ir); 82} 83_CLC_UNARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, float, __clc_tanpi, float); 84 85#ifdef cl_khr_fp64 86#include "sincosD_piby4.h" 87 88_CLC_DEF _CLC_OVERLOAD double __clc_tanpi(double x) 89{ 90 long ix = as_long(x); 91 long xsgn = ix & 0x8000000000000000L; 92 long xnsgn = xsgn ^ 0x8000000000000000L; 93 ix ^= xsgn; 94 double ax = as_double(ix); 95 long iax = (long)ax; 96 double r = ax - iax; 97 long xodd = xsgn ^ (iax & 0x1 ? 0x8000000000000000L : 0L); 98 99 // Initialize with return for +-Inf and NaN 100 long ir = 0x7ff8000000000000L; 101 102 // 2^53 <= |x| < Inf, the result is always even integer 103 ir = ix < 0x7ff0000000000000L ? xsgn : ir; 104 105 // 2^52 <= |x| < 2^53, the result is always integer 106 ir = ix < 0x4340000000000000L ? xodd : ir; 107 108 // 0x1.0p-14 <= |x| < 2^53, result depends on which 0.25 interval 109 110 // r < 1.0 111 double a = 1.0 - r; 112 int e = 0; 113 long s = xnsgn; 114 115 // r <= 0.75 116 int c = r <= 0.75; 117 double t = r - 0.5; 118 a = c ? t : a; 119 e = c ? 1 : e; 120 s = c ? xsgn : s; 121 122 // r < 0.5 123 c = r < 0.5; 124 t = 0.5 - r; 125 a = c ? t : a; 126 s = c ? xnsgn : s; 127 128 // r <= 0.25 129 c = r <= 0.25; 130 a = c ? r : a; 131 e = c ? 0 : e; 132 s = c ? xsgn : s; 133 134 double api = a * M_PI; 135 double2 tt = __clc_tan_piby4(api, 0.0); 136 long jr = s ^ as_long(e ? tt.hi : tt.lo); 137 138 long si = xodd | 0x7ff0000000000000L; 139 jr = r == 0.5 ? si : jr; 140 141 ir = ix < 0x4330000000000000L ? jr : ir; 142 143 return as_double(ir); 144} 145_CLC_UNARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, double, __clc_tanpi, double); 146#endif 147