Modulo the math, which I cannot really check, this looks good to me. Jeroen
> On 18 Dec 2014, at 01:35, Aaron Watry <[email protected]> wrote: > > Ported from the libclc/amd-builtins branch > > v2: Rename sincos_f_piby4 to __libclc__sincosf_piby4 > Add cospi(double) implementation instead of using llvm.cos > > Notes: > The sincosD_piby4.h file is mostly the same as the builtin implementation > released by AMD. The inline attribute declaration is changed, and M_PI is > used instead of a constant double. Otherwise, the only difference is that > the header explicitly enables the fp64 pragma. > > Piglit lacks the built-in capability to test double-precision CL builtins > at the moment. I hacked the cospi(float) tests to use cospi(double) to > verify that the implementation runs and produces reasonable results, but > I have NOT added new test cases to piglit which exercise the additional > precision of the double-precision data type for cospi. > > Signed-off-by: Aaron Watry <[email protected]> > CC: Tom Stellard <[email protected]> > CC: Jeroen Ketema <[email protected]> > CC: Matt Arsenault <[email protected]> > --- > generic/include/clc/clc.h | 1 + > generic/include/clc/math/cospi.h | 3 + > generic/include/clc/math/cospi.inc | 1 + > generic/lib/SOURCES | 1 + > generic/lib/math/cospi.cl | 136 +++++++++++++++++++++++++++++++++++++ > generic/lib/math/sincosD_piby4.h | 78 +++++++++++++++++++++ > generic/lib/math/sincospiF_piby4.h | 56 +++++++++++++++ > 7 files changed, 276 insertions(+) > create mode 100644 generic/include/clc/math/cospi.h > create mode 100644 generic/include/clc/math/cospi.inc > create mode 100644 generic/lib/math/cospi.cl > create mode 100644 generic/lib/math/sincosD_piby4.h > create mode 100644 generic/lib/math/sincospiF_piby4.h > > diff --git a/generic/include/clc/clc.h b/generic/include/clc/clc.h > index bd92fdb..bff6ddd 100644 > --- a/generic/include/clc/clc.h > +++ b/generic/include/clc/clc.h > @@ -38,6 +38,7 @@ > #include <clc/math/atan2.h> > #include <clc/math/copysign.h> > #include <clc/math/cos.h> > +#include <clc/math/cospi.h> > #include <clc/math/ceil.h> > #include <clc/math/exp.h> > #include <clc/math/exp10.h> > diff --git a/generic/include/clc/math/cospi.h > b/generic/include/clc/math/cospi.h > new file mode 100644 > index 0000000..427733b > --- /dev/null > +++ b/generic/include/clc/math/cospi.h > @@ -0,0 +1,3 @@ > +#define __CLC_BODY <clc/math/cospi.inc> > +#include <clc/math/gentype.inc> > +#undef __CLC_BODY > diff --git a/generic/include/clc/math/cospi.inc > b/generic/include/clc/math/cospi.inc > new file mode 100644 > index 0000000..1e786cf > --- /dev/null > +++ b/generic/include/clc/math/cospi.inc > @@ -0,0 +1 @@ > +_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE cospi(__CLC_GENTYPE a); > diff --git a/generic/lib/SOURCES b/generic/lib/SOURCES > index b76fec9..2b9426e 100644 > --- a/generic/lib/SOURCES > +++ b/generic/lib/SOURCES > @@ -57,6 +57,7 @@ math/atan.cl > math/atan2.cl > math/copysign.cl > math/cos.cl > +math/cospi.cl > math/exp.cl > math/exp10.cl > math/fmax.cl > diff --git a/generic/lib/math/cospi.cl b/generic/lib/math/cospi.cl > new file mode 100644 > index 0000000..108b637 > --- /dev/null > +++ b/generic/lib/math/cospi.cl > @@ -0,0 +1,136 @@ > +/* > + * Copyright (c) 2014 Advanced Micro Devices, Inc. > + * > + * Permission is hereby granted, free of charge, to any person obtaining a > copy > + * of this software and associated documentation files (the "Software"), to > deal > + * in the Software without restriction, including without limitation the > rights > + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell > + * copies of the Software, and to permit persons to whom the Software is > + * furnished to do so, subject to the following conditions: > + * > + * The above copyright notice and this permission notice shall be included in > + * all copies or substantial portions of the Software. > + * > + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR > + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, > + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL > THE > + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER > + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING > FROM, > + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN > + * THE SOFTWARE. > + */ > + > +#include <clc/clc.h> > + > +#include "math.h" > +#include "sincos_helpers.h" > +#include "sincospiF_piby4.h" > +#include "../clcmacro.h" > +#ifdef cl_khr_fp64 > +#include "sincosD_piby4.h" > +#endif > + > +_CLC_OVERLOAD _CLC_DEF float cospi(float x) > +{ > + int ix = as_int(x) & 0x7fffffff; > + float ax = as_float(ix); > + int iax = (int)ax; > + float r = ax - iax; > + int xodd = iax & 0x1 ? 0x80000000 : 0; > + > + // Initialize with return for +-Inf and NaN > + int ir = 0x7fc00000; > + > + // 2^24 <= |x| < Inf, the result is always even integer > + ir = ix < 0x7f800000 ? 0x3f800000 : ir; > + > + // 2^23 <= |x| < 2^24, the result is always integer > + ir = ix < 0x4b800000 ? xodd | 0x3f800000 : ir; > + > + // 0x1.0p-7 <= |x| < 2^23, result depends on which 0.25 interval > + > + // r < 1.0 > + float a = 1.0f - r; > + int e = 1; > + int s = xodd ^ 0x80000000; > + > + // r <= 0.75 > + int c = r <= 0.75f; > + a = c ? r - 0.5f : a; > + e = c ? 0 : e; > + > + // r < 0.5 > + c = r < 0.5f; > + a = c ? 0.5f - r : a; > + s = c ? xodd : s; > + > + // r <= 0.25 > + c = r <= 0.25f; > + a = c ? r : a; > + e = c ? 1 : e; > + > + float2 t = __libclc__sincosf_piby4(a * M_PI_F); > + int jr = s ^ as_int(e ? t.hi : t.lo); > + > + ir = ix < 0x4b000000 ? jr : ir; > + > + return as_float(ir); > +} > + > + > +_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, cospi, float); > + > +#ifdef cl_khr_fp64 > + > +#pragma OPENCL EXTENSION cl_khr_fp64 : enable > + > +_CLC_OVERLOAD _CLC_DEF double cospi(double x) { > + > + long ix = as_long(x) & 0x7fffffffffffffffL; > + double ax = as_double(ix); > + long iax = (long)ax; > + double r = ax - (double)iax; > + long xodd = iax & 0x1L ? 0x8000000000000000L : 0L; > + > + // Initialize with return for +-Inf and NaN > + long ir = 0x7ff8000000000000L; > + > + // 2^53 <= |x| < Inf, the result is always even integer > + ir = ix < 0x7ff0000000000000 ? 0x3ff0000000000000L : ir; > + > + // 2^52 <= |x| < 2^53, the result is always integer > + ir = ax < 0x1.0p+53 ? xodd | 0x3ff0000000000000L : ir; > + > + // 0x1.0p-7 <= |x| < 2^52, result depends on which 0.25 interval > + > + // r < 1.0 > + double a = 1.0 - r; > + int e = 1; > + long s = xodd ^ 0x8000000000000000L; > + > + // r <= 0.75 > + int c = r <= 0.75; > + double t = r - 0.5; > + a = c ? t : a; > + e = c ? 0 : e; > + > + // r < 0.5 > + c = r < 0.5; > + t = 0.5 - r; > + a = c ? t : a; > + s = c ? xodd : s; > + > + // r <= 0.25 > + c = r <= 0.25; > + a = c ? r : a; > + e = c ? 1 : e; > + > + double2 sc = __libclc__sincos_piby4(a * M_PI, 0.0); > + long jr = s ^ as_long(e ? sc.hi : sc.lo); > + > + ir = ax < 0x1.0p+52 ? jr : ir; > + > + return as_double(ir); > +} > +_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, cospi, double); > +#endif > diff --git a/generic/lib/math/sincosD_piby4.h > b/generic/lib/math/sincosD_piby4.h > new file mode 100644 > index 0000000..a00db85 > --- /dev/null > +++ b/generic/lib/math/sincosD_piby4.h > @@ -0,0 +1,78 @@ > +/* > + * Copyright (c) 2014 Advanced Micro Devices, Inc. > + * > + * Permission is hereby granted, free of charge, to any person obtaining a > copy > + * of this software and associated documentation files (the "Software"), to > deal > + * in the Software without restriction, including without limitation the > rights > + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell > + * copies of the Software, and to permit persons to whom the Software is > + * furnished to do so, subject to the following conditions: > + * > + * The above copyright notice and this permission notice shall be included in > + * all copies or substantial portions of the Software. > + * > + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR > + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, > + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL > THE > + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER > + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING > FROM, > + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN > + * THE SOFTWARE. > + */ > + > +#pragma OPENCL EXTENSION cl_khr_fp64 : enable > + > +_CLC_INLINE double2 > +__libclc__sincos_piby4(double x, double xx) > +{ > + // Taylor series for sin(x) is x - x^3/3! + x^5/5! - x^7/7! ... > + // = x * (1 - x^2/3! + x^4/5! - x^6/7! ... > + // = x * f(w) > + // where w = x*x and f(w) = (1 - w/3! + w^2/5! - w^3/7! ... > + // We use a minimax approximation of (f(w) - 1) / w > + // because this produces an expansion in even powers of x. > + // If xx (the tail of x) is non-zero, we add a correction > + // term g(x,xx) = (1-x*x/2)*xx to the result, where g(x,xx) > + // is an approximation to cos(x)*sin(xx) valid because > + // xx is tiny relative to x. > + > + // Taylor series for cos(x) is 1 - x^2/2! + x^4/4! - x^6/6! ... > + // = f(w) > + // where w = x*x and f(w) = (1 - w/2! + w^2/4! - w^3/6! ... > + // We use a minimax approximation of (f(w) - 1 + w/2) / (w*w) > + // because this produces an expansion in even powers of x. > + // If xx (the tail of x) is non-zero, we subtract a correction > + // term g(x,xx) = x*xx to the result, where g(x,xx) > + // is an approximation to sin(x)*sin(xx) valid because > + // xx is tiny relative to x. > + > + const double sc1 = -0.166666666666666646259241729; > + const double sc2 = 0.833333333333095043065222816e-2; > + const double sc3 = -0.19841269836761125688538679e-3; > + const double sc4 = 0.275573161037288022676895908448e-5; > + const double sc5 = -0.25051132068021699772257377197e-7; > + const double sc6 = 0.159181443044859136852668200e-9; > + > + const double cc1 = 0.41666666666666665390037e-1; > + const double cc2 = -0.13888888888887398280412e-2; > + const double cc3 = 0.248015872987670414957399e-4; > + const double cc4 = -0.275573172723441909470836e-6; > + const double cc5 = 0.208761463822329611076335e-8; > + const double cc6 = -0.113826398067944859590880e-10; > + > + double x2 = x * x; > + double x3 = x2 * x; > + double r = 0.5 * x2; > + double t = 1.0 - r; > + > + double sp = fma(fma(fma(fma(sc6, x2, sc5), x2, sc4), x2, sc3), x2, sc2); > + > + double cp = t + fma(fma(fma(fma(fma(fma(cc6, x2, cc5), x2, cc4), x2, > cc3), x2, cc2), x2, cc1), > + x2*x2, fma(x, xx, (1.0 - t) - r)); > + > + double2 ret; > + ret.lo = x - fma(-x3, sc1, fma(fma(-x3, sp, 0.5*xx), x2, -xx)); > + ret.hi = cp; > + > + return ret; > +} > diff --git a/generic/lib/math/sincospiF_piby4.h > b/generic/lib/math/sincospiF_piby4.h > new file mode 100644 > index 0000000..90ecb1d > --- /dev/null > +++ b/generic/lib/math/sincospiF_piby4.h > @@ -0,0 +1,56 @@ > +/* > + * Copyright (c) 2014 Advanced Micro Devices, Inc. > + * > + * Permission is hereby granted, free of charge, to any person obtaining a > copy > + * of this software and associated documentation files (the "Software"), to > deal > + * in the Software without restriction, including without limitation the > rights > + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell > + * copies of the Software, and to permit persons to whom the Software is > + * furnished to do so, subject to the following conditions: > + * > + * The above copyright notice and this permission notice shall be included in > + * all copies or substantial portions of the Software. > + * > + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR > + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, > + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL > THE > + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER > + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING > FROM, > + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN > + * THE SOFTWARE. > + */ > + > +// Evaluate single precisions in and cos of value in interval [-pi/4, pi/4] > +_CLC_INLINE float2 > +__libclc__sincosf_piby4(float x) > +{ > + // Taylor series for sin(x) is x - x^3/3! + x^5/5! - x^7/7! ... > + // = x * (1 - x^2/3! + x^4/5! - x^6/7! ... > + // = x * f(w) > + // where w = x*x and f(w) = (1 - w/3! + w^2/5! - w^3/7! ... > + // We use a minimax approximation of (f(w) - 1) / w > + // because this produces an expansion in even powers of x. > + > + // Taylor series for cos(x) is 1 - x^2/2! + x^4/4! - x^6/6! ... > + // = f(w) > + // where w = x*x and f(w) = (1 - w/2! + w^2/4! - w^3/6! ... > + // We use a minimax approximation of (f(w) - 1 + w/2) / (w*w) > + // because this produces an expansion in even powers of x. > + > + const float sc1 = -0.166666666638608441788607926e0F; > + const float sc2 = 0.833333187633086262120839299e-2F; > + const float sc3 = -0.198400874359527693921333720e-3F; > + const float sc4 = 0.272500015145584081596826911e-5F; > + > + const float cc1 = 0.41666666664325175238031e-1F; > + const float cc2 = -0.13888887673175665567647e-2F; > + const float cc3 = 0.24800600878112441958053e-4F; > + const float cc4 = -0.27301013343179832472841e-6F; > + > + float x2 = x * x; > + > + float2 ret; > + ret.x = mad(x*x2, mad(x2, mad(x2, mad(x2, sc4, sc3), sc2), sc1), x); > + ret.y = mad(x2*x2, mad(x2, mad(x2, mad(x2, cc4, cc3), cc2), cc1), > mad(x2, -0.5f, 1.0f)); > + return ret; > +} > -- > 2.1.0 > _______________________________________________ cfe-commits mailing list [email protected] http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
