[libclc] r234323 - Implement acosh builtin

Tom Stellard thomas.stellard at amd.com
Tue Apr 7 09:20:20 PDT 2015


Author: tstellar
Date: Tue Apr  7 11:20:20 2015
New Revision: 234323

URL: http://llvm.org/viewvc/llvm-project?rev=234323&view=rev
Log:
Implement acosh builtin

This implementation was ported from the AMD builtin library
and has been tested with piglit, OpenCV, and the ocl conformance tests.

Added:
    libclc/trunk/generic/include/clc/math/acosh.h
    libclc/trunk/generic/include/clc/math/acosh.inc
    libclc/trunk/generic/lib/math/acosh.cl
Modified:
    libclc/trunk/generic/include/clc/clc.h
    libclc/trunk/generic/lib/SOURCES

Modified: libclc/trunk/generic/include/clc/clc.h
URL: http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/include/clc/clc.h?rev=234323&r1=234322&r2=234323&view=diff
==============================================================================
--- libclc/trunk/generic/include/clc/clc.h (original)
+++ libclc/trunk/generic/include/clc/clc.h Tue Apr  7 11:20:20 2015
@@ -33,6 +33,7 @@
 
 /* 6.11.2 Math Functions */
 #include <clc/math/acos.h>
+#include <clc/math/acosh.h>
 #include <clc/math/acospi.h>
 #include <clc/math/asin.h>
 #include <clc/math/asinh.h>

Added: libclc/trunk/generic/include/clc/math/acosh.h
URL: http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/include/clc/math/acosh.h?rev=234323&view=auto
==============================================================================
--- libclc/trunk/generic/include/clc/math/acosh.h (added)
+++ libclc/trunk/generic/include/clc/math/acosh.h Tue Apr  7 11:20:20 2015
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2014, 2015 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/acosh.inc>
+#include <clc/math/gentype.inc>

Added: libclc/trunk/generic/include/clc/math/acosh.inc
URL: http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/include/clc/math/acosh.inc?rev=234323&view=auto
==============================================================================
--- libclc/trunk/generic/include/clc/math/acosh.inc (added)
+++ libclc/trunk/generic/include/clc/math/acosh.inc Tue Apr  7 11:20:20 2015
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2014, 2015 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 acosh(__CLC_GENTYPE x);

Modified: libclc/trunk/generic/lib/SOURCES
URL: http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/lib/SOURCES?rev=234323&r1=234322&r2=234323&view=diff
==============================================================================
--- libclc/trunk/generic/lib/SOURCES (original)
+++ libclc/trunk/generic/lib/SOURCES Tue Apr  7 11:20:20 2015
@@ -60,6 +60,7 @@ integer/sub_sat_if.ll
 integer/sub_sat_impl.ll
 integer/upsample.cl
 math/acos.cl
+math/acosh.cl
 math/acospi.cl
 math/asin.cl
 math/asinh.cl

Added: libclc/trunk/generic/lib/math/acosh.cl
URL: http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/lib/math/acosh.cl?rev=234323&view=auto
==============================================================================
--- libclc/trunk/generic/lib/math/acosh.cl (added)
+++ libclc/trunk/generic/lib/math/acosh.cl Tue Apr  7 11:20:20 2015
@@ -0,0 +1,127 @@
+/*
+ * 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 "ep_log.h"
+#include "math.h"
+#include "../clcmacro.h"
+
+_CLC_OVERLOAD _CLC_DEF  float acosh(float x) {
+    uint ux = as_uint(x);
+
+    // Arguments greater than 1/sqrt(epsilon) in magnitude are
+    // approximated by acosh(x) = ln(2) + ln(x)
+    // For 2.0 <= x <= 1/sqrt(epsilon) the approximation is
+    // acosh(x) = ln(x + sqrt(x*x-1)) */
+    int high = ux > 0x46000000U;
+    int med = ux > 0x40000000U;
+
+    float w = x - 1.0f;
+    float s = w*w + 2.0f*w;
+    float t = x*x - 1.0f;
+    float r = sqrt(med ? t : s) + (med ? x : w);
+    float v = (high ? x : r) - (med ? 1.0f : 0.0f);
+    float z = log1p(v) + (high ? 0x1.62e430p-1f : 0.0f);
+
+    z = ux >= PINFBITPATT_SP32 ? x : z;
+    z = x < 1.0f ? as_float(QNANBITPATT_SP32) : z;
+
+    return z;
+}
+
+_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, acosh, float)
+
+#ifdef cl_khr_fp64
+#pragma OPENCL EXTENSION cl_khr_fp64 : enable
+
+_CLC_OVERLOAD _CLC_DEF double acosh(double x) {
+    const double recrteps = 0x1.6a09e667f3bcdp+26;	// 1/sqrt(eps) = 9.49062656242515593767e+07
+    //log2_lead and log2_tail sum to an extra-precise version of log(2)
+    const double log2_lead = 0x1.62e42ep-1;
+    const double log2_tail = 0x1.efa39ef35793cp-25;
+
+    // Handle x >= 128 here
+    int xlarge = x > recrteps;
+    double r = x + sqrt(fma(x, x, -1.0));
+    r = xlarge ? x : r;
+
+    int xexp;
+    double r1, r2;
+    __clc_ep_log(r, &xexp, &r1, &r2);
+
+    double dxexp = xexp + xlarge;
+    r1 = fma(dxexp, log2_lead, r1);
+    r2 = fma(dxexp, log2_tail, r2);
+
+    double ret1 = r1 + r2;
+
+    // Handle 1 < x < 128 here
+    // We compute the value
+    // t = x - 1.0 + sqrt(2.0*(x - 1.0) + (x - 1.0)*(x - 1.0))
+    // using simulated quad precision.
+    double t = x - 1.0;
+    double u1 = t * 2.0;
+
+    // (t,0) * (t,0) -> (v1, v2)
+    double v1 = t * t;
+    double v2 = fma(t, t, -v1);
+
+    // (u1,0) + (v1,v2) -> (w1,w2)
+    r = u1 + v1;
+    double s = (((u1 - r) + v1) + v2);
+    double w1 = r + s;
+    double w2 = (r - w1) + s;
+
+    // sqrt(w1,w2) -> (u1,u2)
+    double p1 = sqrt(w1);
+    double a1 = p1*p1;
+    double a2 = fma(p1, p1, -a1);
+    double temp = (((w1 - a1) - a2) + w2);
+    double p2 = MATH_DIVIDE(temp * 0.5, p1);
+    u1 = p1 + p2;
+    double u2 = (p1 - u1) + p2;
+
+    // (u1,u2) + (t,0) -> (r1,r2)
+    r = u1 + t;
+    s = ((u1 - r) + t) + u2;
+    // r1 = r + s;
+    // r2 = (r - r1) + s;
+    // t = r1 + r2;
+    t = r + s;
+
+    // For arguments 1.13 <= x <= 1.5 the log1p function is good enough
+    double ret2 = log1p(t);
+
+    ulong ux = as_ulong(x);
+    double ret = x >= 128.0 ? ret1 : ret2;
+
+    ret = ux >= 0x7FF0000000000000 ? x : ret;
+    ret = x == 1.0 ? 0.0 : ret;
+    ret = (ux & SIGNBIT_DP64) != 0UL | x < 1.0 ? as_double(QNANBITPATT_DP64) : ret;
+
+    return ret;
+}
+
+_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, acosh, double)
+
+#endif





More information about the cfe-commits mailing list