[Libclc-dev] [PATCH 1/4] Implement atan builtin
Aaron Watry
awatry at gmail.com
Wed Jul 16 11:06:18 PDT 2014
On Wed, Jul 16, 2014 at 9:58 AM, Tom Stellard <tom at stellard.net> wrote:
> On Tue, Jul 15, 2014 at 05:31:49PM -0500, Aaron Watry wrote:
>> No objection here. I am assuming that the formulas are correct based
>> on the previous comment about them coming straight from another AMD
>> math library.
>>
>> Only question I have is if the defined macros in math.h will interfere
>> with kernels that use the same names for their own macros. The header
>> is in generic/lib/math, so it's not going to be installed in the
>> include directory, but I just wanted to make sure that those macros
>> were getting resolved before user kernels attempted to redefine them
>> (e.g. macros substituted when creating the .bc files).
>>
>
> This shouldn't be a problem as long as it stays in the lib/ subdirectory.
> Since clover links against the .bc files all the macros have already been resolved.
>
That's what I thought, just wanted to make sure.
> -Tom
>
>> --Aaron
>>
>> On Tue, Jul 15, 2014 at 10:46 AM, Tom Stellard <tom at stellard.net> wrote:
>> > On Wed, Jul 02, 2014 at 04:11:13PM -0400, Tom Stellard wrote:
>> >
>> > I'm going to commit these tomorrow if there are no objections.
>> >
>> > -Tom
>> >
>> >> ---
>> >> generic/include/clc/clc.h | 1 +
>> >> generic/include/clc/math/atan.h | 24 +++++
>> >> generic/include/clc/math/atan.inc | 23 +++++
>> >> generic/lib/SOURCES | 1 +
>> >> generic/lib/math/atan.cl | 182 ++++++++++++++++++++++++++++++++++++++
>> >> generic/lib/math/math.h | 64 ++++++++++++++
>> >> 6 files changed, 295 insertions(+)
>> >> create mode 100644 generic/include/clc/math/atan.h
>> >> create mode 100644 generic/include/clc/math/atan.inc
>> >> create mode 100644 generic/lib/math/atan.cl
>> >> create mode 100644 generic/lib/math/math.h
>> >>
>> >> diff --git a/generic/include/clc/clc.h b/generic/include/clc/clc.h
>> >> index e0cae98..af177dc 100644
>> >> --- a/generic/include/clc/clc.h
>> >> +++ b/generic/include/clc/clc.h
>> >> @@ -32,6 +32,7 @@
>> >> #include <clc/workitem/get_group_id.h>
>> >>
>> >> /* 6.11.2 Math Functions */
>> >> +#include <clc/math/atan.h>
>> >> #include <clc/math/cos.h>
>> >> #include <clc/math/ceil.h>
>> >> #include <clc/math/exp.h>
>> >> diff --git a/generic/include/clc/math/atan.h b/generic/include/clc/math/atan.h
>> >> new file mode 100644
>> >> index 0000000..d969719
>> >> --- /dev/null
>> >> +++ b/generic/include/clc/math/atan.h
>> >> @@ -0,0 +1,24 @@
>> >> +/*
>> >> + * 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.
>> >> + */
>> >> +
>> >> +#define __CLC_BODY <clc/math/atan.inc>
>> >> +#include <clc/math/gentype.inc>
>> >> diff --git a/generic/include/clc/math/atan.inc b/generic/include/clc/math/atan.inc
>> >> new file mode 100644
>> >> index 0000000..d217c95
>> >> --- /dev/null
>> >> +++ b/generic/include/clc/math/atan.inc
>> >> @@ -0,0 +1,23 @@
>> >> +/*
>> >> + * 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.
>> >> + */
>> >> +
>> >> +_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE atan(__CLC_GENTYPE a);
>> >> diff --git a/generic/lib/SOURCES b/generic/lib/SOURCES
>> >> index 02894d9..ed93084 100644
>> >> --- a/generic/lib/SOURCES
>> >> +++ b/generic/lib/SOURCES
>> >> @@ -27,6 +27,7 @@ integer/sub_sat.cl
>> >> integer/sub_sat_if.ll
>> >> integer/sub_sat_impl.ll
>> >> integer/upsample.cl
>> >> +math/atan.cl
>> >> math/exp.cl
>> >> math/fmax.cl
>> >> math/fmin.cl
>> >> diff --git a/generic/lib/math/atan.cl b/generic/lib/math/atan.cl
>> >> new file mode 100644
>> >> index 0000000..682bff4
>> >> --- /dev/null
>> >> +++ b/generic/lib/math/atan.cl
>> >> @@ -0,0 +1,182 @@
>> >> +/*
>> >> + * 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 "math.h"
>> >> +
>> >> +#include <clc/clc.h>
>> >> +
>> >> +_CLC_OVERLOAD _CLC_DEF float atan(float x)
>> >> +{
>> >> + const float piby2 = 1.5707963267948966f; // 0x3ff921fb54442d18
>> >> +
>> >> + uint ux = as_uint(x);
>> >> + uint aux = ux & EXSIGNBIT_SP32;
>> >> + uint sx = ux ^ aux;
>> >> +
>> >> + float spiby2 = as_float(sx | as_uint(piby2));
>> >> +
>> >> + float v = as_float(aux);
>> >> +
>> >> + // Return for NaN
>> >> + float ret = x;
>> >> +
>> >> + // 2^26 <= |x| <= Inf => atan(x) is close to piby2
>> >> + ret = aux <= PINFBITPATT_SP32 ? spiby2 : ret;
>> >> +
>> >> + // Reduce arguments 2^-19 <= |x| < 2^26
>> >> +
>> >> + // 39/16 <= x < 2^26
>> >> + x = -MATH_RECIP(v);
>> >> + float c = 1.57079632679489655800f; // atan(infinity)
>> >> +
>> >> + // 19/16 <= x < 39/16
>> >> + int l = aux < 0x401c0000;
>> >> + float xx = MATH_DIVIDE(v - 1.5f, mad(v, 1.5f, 1.0f));
>> >> + x = l ? xx : x;
>> >> + c = l ? 9.82793723247329054082e-01f : c; // atan(1.5)
>> >> +
>> >> + // 11/16 <= x < 19/16
>> >> + l = aux < 0x3f980000U;
>> >> + xx = MATH_DIVIDE(v - 1.0f, 1.0f + v);
>> >> + x = l ? xx : x;
>> >> + c = l ? 7.85398163397448278999e-01f : c; // atan(1)
>> >> +
>> >> + // 7/16 <= x < 11/16
>> >> + l = aux < 0x3f300000;
>> >> + xx = MATH_DIVIDE(mad(v, 2.0f, -1.0f), 2.0f + v);
>> >> + x = l ? xx : x;
>> >> + c = l ? 4.63647609000806093515e-01f : c; // atan(0.5)
>> >> +
>> >> + // 2^-19 <= x < 7/16
>> >> + l = aux < 0x3ee00000;
>> >> + x = l ? v : x;
>> >> + c = l ? 0.0f : c;
>> >> +
>> >> + // Core approximation: Remez(2,2) on [-7/16,7/16]
>> >> +
>> >> + float s = x * x;
>> >> + float a = mad(s,
>> >> + mad(s, 0.470677934286149214138357545549e-2f, 0.192324546402108583211697690500f),
>> >> + 0.296528598819239217902158651186f);
>> >> +
>> >> + float b = mad(s,
>> >> + mad(s, 0.299309699959659728404442796915f, 0.111072499995399550138837673349e1f),
>> >> + 0.889585796862432286486651434570f);
>> >> +
>> >> + float q = x * s * MATH_DIVIDE(a, b);
>> >> +
>> >> + float z = c - (q - x);
>> >> + float zs = as_float(sx | as_uint(z));
>> >> +
>> >> + ret = aux < 0x4c800000 ? zs : ret;
>> >> +
>> >> + // |x| < 2^-19
>> >> + ret = aux < 0x36000000 ? as_float(ux) : ret;
>> >> + return ret;
>> >> +}
>> >> +
>> >> +_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, atan, float);
>> >> +
>> >> +#ifdef cl_khr_fp64
>> >> +
>> >> +#pragma OPENCL EXTENSION cl_khr_fp64 : enable
>> >> +
>> >> +
>> >> +_CLC_OVERLOAD _CLC_DEF double atan(double x)
>> >> +{
>> >> + const double piby2 = 1.5707963267948966e+00; // 0x3ff921fb54442d18
>> >> +
>> >> + double v = fabs(x);
>> >> +
>> >> + // 2^56 > v > 39/16
>> >> + double a = -1.0;
>> >> + double b = v;
>> >> + // (chi + clo) = arctan(infinity)
>> >> + double chi = 1.57079632679489655800e+00;
>> >> + double clo = 6.12323399573676480327e-17;
>> >> +
>> >> + double ta = v - 1.5;
>> >> + double tb = 1.0 + 1.5 * v;
>> >> + int l = v <= 0x1.38p+1; // 39/16 > v > 19/16
>> >> + a = l ? ta : a;
>> >> + b = l ? tb : b;
>> >> + // (chi + clo) = arctan(1.5)
>> >> + chi = l ? 9.82793723247329054082e-01 : chi;
>> >> + clo = l ? 1.39033110312309953701e-17 : clo;
>> >> +
>> >> + ta = v - 1.0;
>> >> + tb = 1.0 + v;
>> >> + l = v <= 0x1.3p+0; // 19/16 > v > 11/16
>> >> + a = l ? ta : a;
>> >> + b = l ? tb : b;
>> >> + // (chi + clo) = arctan(1.)
>> >> + chi = l ? 7.85398163397448278999e-01 : chi;
>> >> + clo = l ? 3.06161699786838240164e-17 : clo;
>> >> +
>> >> + ta = 2.0 * v - 1.0;
>> >> + tb = 2.0 + v;
>> >> + l = v <= 0x1.6p-1; // 11/16 > v > 7/16
>> >> + a = l ? ta : a;
>> >> + b = l ? tb : b;
>> >> + // (chi + clo) = arctan(0.5)
>> >> + chi = l ? 4.63647609000806093515e-01 : chi;
>> >> + clo = l ? 2.26987774529616809294e-17 : clo;
>> >> +
>> >> + l = v <= 0x1.cp-2; // v < 7/16
>> >> + a = l ? v : a;
>> >> + b = l ? 1.0 : b;;
>> >> + chi = l ? 0.0 : chi;
>> >> + clo = l ? 0.0 : clo;
>> >> +
>> >> + // Core approximation: Remez(4,4) on [-7/16,7/16]
>> >> + double r = a / b;
>> >> + double s = r * r;
>> >> + double qn = fma(s,
>> >> + fma(s,
>> >> + fma(s,
>> >> + fma(s, 0.142316903342317766e-3,
>> >> + 0.304455919504853031e-1),
>> >> + 0.220638780716667420e0),
>> >> + 0.447677206805497472e0),
>> >> + 0.268297920532545909e0);
>> >> +
>> >> + double qd = fma(s,
>> >> + fma(s,
>> >> + fma(s,
>> >> + fma(s, 0.389525873944742195e-1,
>> >> + 0.424602594203847109e0),
>> >> + 0.141254259931958921e1),
>> >> + 0.182596787737507063e1),
>> >> + 0.804893761597637733e0);
>> >> +
>> >> + double q = r * s * qn / qd;
>> >> + r = chi - ((q - clo) - r);
>> >> +
>> >> + double z = isnan(x) ? x : piby2;
>> >> + z = v <= 0x1.0p+56 ? r : z;
>> >> + z = v < 0x1.0p-26 ? v : z;
>> >> + return x == v ? z : -z;
>> >> +}
>> >> +
>> >> +_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, atan, double);
>> >> +
>> >> +#endif // cl_khr_fp64
>> >> diff --git a/generic/lib/math/math.h b/generic/lib/math/math.h
>> >> new file mode 100644
>> >> index 0000000..53ed38a
>> >> --- /dev/null
>> >> +++ b/generic/lib/math/math.h
>> >> @@ -0,0 +1,64 @@
>> >> +/*
>> >> + * 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.
>> >> + */
>> >> +
>> >> +#define SNAN 0x001
>> >> +#define QNAN 0x002
>> >> +#define NINF 0x004
>> >> +#define NNOR 0x008
>> >> +#define NSUB 0x010
>> >> +#define NZER 0x020
>> >> +#define PZER 0x040
>> >> +#define PSUB 0x080
>> >> +#define PNOR 0x100
>> >> +#define PINF 0x200
>> >> +
>> >> +#define HAVE_HW_FMA32() (1)
>> >> +#define HAVE_BITALIGN() (0)
>> >> +#define HAVE_FAST_FMA32() (0)
>> >> +
>> >> +#define MATH_DIVIDE(X, Y) ((X) / (Y))
>> >> +#define MATH_RECIP(X) (1.0f / (X))
>> >> +#define MATH_SQRT(X) sqrt(X)
>> >> +
>> >> +#define SIGNBIT_SP32 0x80000000
>> >> +#define EXSIGNBIT_SP32 0x7fffffff
>> >> +#define EXPBITS_SP32 0x7f800000
>> >> +#define MANTBITS_SP32 0x007fffff
>> >> +#define ONEEXPBITS_SP32 0x3f800000
>> >> +#define TWOEXPBITS_SP32 0x40000000
>> >> +#define HALFEXPBITS_SP32 0x3f000000
>> >> +#define IMPBIT_SP32 0x00800000
>> >> +#define QNANBITPATT_SP32 0x7fc00000
>> >> +#define INDEFBITPATT_SP32 0xffc00000
>> >> +#define PINFBITPATT_SP32 0x7f800000
>> >> +#define NINFBITPATT_SP32 0xff800000
>> >> +#define EXPBIAS_SP32 127
>> >> +#define EXPSHIFTBITS_SP32 23
>> >> +#define BIASEDEMIN_SP32 1
>> >> +#define EMIN_SP32 -126
>> >> +#define BIASEDEMAX_SP32 254
>> >> +#define EMAX_SP32 127
>> >> +#define LAMBDA_SP32 1.0e30
>> >> +#define MANTLENGTH_SP32 24
>> >> +#define BASEDIGITS_SP32 7
>> >> +
>> >> +#define ALIGNED(x) __attribute__((aligned(x)))
>> >> --
>> >> 1.8.1.5
>> >>
>> >>
>> >> _______________________________________________
>> >> Libclc-dev mailing list
>> >> Libclc-dev at pcc.me.uk
>> >> http://www.pcc.me.uk/cgi-bin/mailman/listinfo/libclc-dev
>> >
>> > _______________________________________________
>> > Libclc-dev mailing list
>> > Libclc-dev at pcc.me.uk
>> > http://www.pcc.me.uk/cgi-bin/mailman/listinfo/libclc-dev
More information about the Libclc-dev
mailing list