Implement cosh builtin

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

llvm-svn: 276496
This commit is contained in:
Tom Stellard 2016-07-22 23:45:13 +00:00
parent ff13926a60
commit 9cb070f96a
7 changed files with 370 additions and 0 deletions

View File

@ -46,6 +46,7 @@
#include <clc/math/atanpi.h>
#include <clc/math/copysign.h>
#include <clc/math/cos.h>
#include <clc/math/cosh.h>
#include <clc/math/cospi.h>
#include <clc/math/ceil.h>
#include <clc/math/erf.h>

View File

@ -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/cosh.inc>
#include <clc/math/gentype.inc>

View File

@ -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 cosh(__CLC_GENTYPE x);

View File

@ -75,6 +75,7 @@ math/atanh.cl
math/atanpi.cl
math/copysign.cl
math/cos.cl
math/cosh.cl
math/cospi.cl
math/ep_log.cl
math/erf.cl

View File

@ -0,0 +1,192 @@
/*
* 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.
*/
#include <clc/clc.h>
#include "math.h"
#include "tables.h"
#include "../clcmacro.h"
_CLC_OVERLOAD _CLC_DEF float cosh(float x) {
// After dealing with special cases the computation is split into regions as follows.
// abs(x) >= max_cosh_arg:
// cosh(x) = sign(x)*Inf
// abs(x) >= small_threshold:
// cosh(x) = sign(x)*exp(abs(x))/2 computed using the
// splitexp and scaleDouble functions as for exp_amd().
// abs(x) < small_threshold:
// compute p = exp(y) - 1 and then z = 0.5*(p+(p/(p+1.0)))
// cosh(x) is then z.
const float max_cosh_arg = 0x1.65a9fap+6f;
const float small_threshold = 0x1.0a2b24p+3f;
uint ux = as_uint(x);
uint aux = ux & EXSIGNBIT_SP32;
float y = as_float(aux);
// Find the integer part y0 of y and the increment dy = y - y0. We then compute
// z = sinh(y) = sinh(y0)cosh(dy) + cosh(y0)sinh(dy)
// z = cosh(y) = cosh(y0)cosh(dy) + sinh(y0)sinh(dy)
// where sinh(y0) and cosh(y0) are tabulated above.
int ind = (int)y;
ind = (uint)ind > 36U ? 0 : ind;
float dy = y - ind;
float dy2 = dy * dy;
float sdy = mad(dy2,
mad(dy2,
mad(dy2,
mad(dy2,
mad(dy2,
mad(dy2, 0.7746188980094184251527126e-12f, 0.160576793121939886190847e-9f),
0.250521176994133472333666e-7f),
0.275573191913636406057211e-5f),
0.198412698413242405162014e-3f),
0.833333333333329931873097e-2f),
0.166666666666666667013899e0f);
sdy = mad(sdy, dy*dy2, dy);
float cdy = mad(dy2,
mad(dy2,
mad(dy2,
mad(dy2,
mad(dy2,
mad(dy2, 0.1163921388172173692062032e-10f, 0.208744349831471353536305e-8f),
0.275573350756016588011357e-6f),
0.248015872460622433115785e-4f),
0.138888888889814854814536e-2f),
0.416666666666660876512776e-1f),
0.500000000000000005911074e0f);
cdy = mad(cdy, dy2, 1.0f);
float2 tv = USE_TABLE(sinhcosh_tbl, ind);
float z = mad(tv.s0, sdy, tv.s1 * cdy);
// When exp(-x) is insignificant compared to exp(x), return exp(x)/2
float t = exp(y - 0x1.62e500p-1f);
float zsmall = mad(0x1.a0210ep-18f, t, t);
z = y >= small_threshold ? zsmall : z;
// Corner cases
z = y >= max_cosh_arg ? as_float(PINFBITPATT_SP32) : z;
z = aux > PINFBITPATT_SP32 ? as_float(QNANBITPATT_SP32) : z;
z = aux < 0x38800000 ? 1.0f : z;
return z;
}
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, cosh, float);
#ifdef cl_khr_fp64
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
_CLC_OVERLOAD _CLC_DEF double cosh(double x) {
// After dealing with special cases the computation is split into
// regions as follows:
//
// abs(x) >= max_cosh_arg:
// cosh(x) = sign(x)*Inf
//
// abs(x) >= small_threshold:
// cosh(x) = sign(x)*exp(abs(x))/2 computed using the
// splitexp and scaleDouble functions as for exp_amd().
//
// abs(x) < small_threshold:
// compute p = exp(y) - 1 and then z = 0.5*(p+(p/(p+1.0)))
// cosh(x) is then sign(x)*z.
// This is ln(2^1025)
const double max_cosh_arg = 7.10475860073943977113e+02; // 0x408633ce8fb9f87e
// This is where exp(-x) is insignificant compared to exp(x) = ln(2^27)
const double small_threshold = 0x1.2b708872320e2p+4;
double y = fabs(x);
// In this range we find the integer part y0 of y
// and the increment dy = y - y0. We then compute
// z = cosh(y) = cosh(y0)cosh(dy) + sinh(y0)sinh(dy)
// where sinh(y0) and cosh(y0) are tabulated above.
int ind = min((int)y, 36);
double dy = y - ind;
double dy2 = dy * dy;
double sdy = dy * dy2 *
fma(dy2,
fma(dy2,
fma(dy2,
fma(dy2,
fma(dy2,
fma(dy2, 0.7746188980094184251527126e-12, 0.160576793121939886190847e-9),
0.250521176994133472333666e-7),
0.275573191913636406057211e-5),
0.198412698413242405162014e-3),
0.833333333333329931873097e-2),
0.166666666666666667013899e0);
double cdy = dy2 * fma(dy2,
fma(dy2,
fma(dy2,
fma(dy2,
fma(dy2,
fma(dy2, 0.1163921388172173692062032e-10, 0.208744349831471353536305e-8),
0.275573350756016588011357e-6),
0.248015872460622433115785e-4),
0.138888888889814854814536e-2),
0.416666666666660876512776e-1),
0.500000000000000005911074e0);
// At this point sinh(dy) is approximated by dy + sdy,
// and cosh(dy) is approximated by 1 + cdy.
double2 tv = USE_TABLE(cosh_tbl, ind);
double cl = tv.s0;
double ct = tv.s1;
tv = USE_TABLE(sinh_tbl, ind);
double sl = tv.s0;
double st = tv.s1;
double z = fma(sl, dy, fma(sl, sdy, fma(cl, cdy, fma(st, dy, fma(st, sdy, ct*cdy)) + ct))) + cl;
// Other cases
z = y < 0x1.0p-28 ? 1.0 : z;
double t = exp(y - 0x1.62e42fefa3800p-1);
t = fma(t, -0x1.ef35793c76641p-45, t);
z = y >= small_threshold ? t : z;
z = y >= max_cosh_arg ? as_double(PINFBITPATT_DP64) : z;
z = isinf(x) | isnan(x) ? y : z;
return z;
}
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, cosh, double)
#endif

View File

@ -435,6 +435,47 @@ DECLARE_TABLE(uchar, PIBITS_TBL, ) = {
230, 139, 2, 0, 0, 0, 0, 0, 0, 0
};
// Tabulated values of sinh(i) and cosh(i) for i = 0,...,36.
DECLARE_TABLE(float2, SINHCOSH_TBL, 37) = {
(float2)(0x0.000000p+0f, 0x1.000000p+0f),
(float2)(0x1.2cd9fcp+0f, 0x1.8b0756p+0f),
(float2)(0x1.d03cf6p+1f, 0x1.e18fa0p+1f),
(float2)(0x1.40926ep+3f, 0x1.422a4ap+3f),
(float2)(0x1.b4a380p+4f, 0x1.b4ee86p+4f),
(float2)(0x1.28d016p+6f, 0x1.28d6fcp+6f),
(float2)(0x1.936d22p+7f, 0x1.936e68p+7f),
(float2)(0x1.122876p+9f, 0x1.122894p+9f),
(float2)(0x1.749ea6p+10f, 0x1.749eaap+10f),
(float2)(0x1.fa7158p+11f, 0x1.fa7158p+11f),
(float2)(0x1.5829dcp+13f, 0x1.5829dep+13f),
(float2)(0x1.d3c448p+14f, 0x1.d3c448p+14f),
(float2)(0x1.3de166p+16f, 0x1.3de166p+16f),
(float2)(0x1.b00b5ap+17f, 0x1.b00b5ap+17f),
(float2)(0x1.259ac4p+19f, 0x1.259ac4p+19f),
(float2)(0x1.8f0ccap+20f, 0x1.8f0ccap+20f),
(float2)(0x1.0f2ebep+22f, 0x1.0f2ebep+22f),
(float2)(0x1.709348p+23f, 0x1.709348p+23f),
(float2)(0x1.f4f220p+24f, 0x1.f4f220p+24f),
(float2)(0x1.546d90p+26f, 0x1.546d90p+26f),
(float2)(0x1.ceb088p+27f, 0x1.ceb088p+27f),
(float2)(0x1.3a6e20p+29f, 0x1.3a6e20p+29f),
(float2)(0x1.ab5adcp+30f, 0x1.ab5adcp+30f),
(float2)(0x1.226af4p+32f, 0x1.226af4p+32f),
(float2)(0x1.8ab7fcp+33f, 0x1.8ab7fcp+33f),
(float2)(0x1.0c3d3ap+35f, 0x1.0c3d3ap+35f),
(float2)(0x1.6c9326p+36f, 0x1.6c9326p+36f),
(float2)(0x1.ef8230p+37f, 0x1.ef8230p+37f),
(float2)(0x1.50bba4p+39f, 0x1.50bba4p+39f),
(float2)(0x1.c9aae4p+40f, 0x1.c9aae4p+40f),
(float2)(0x1.370470p+42f, 0x1.370470p+42f),
(float2)(0x1.a6b766p+43f, 0x1.a6b766p+43f),
(float2)(0x1.1f43fcp+45f, 0x1.1f43fcp+45f),
(float2)(0x1.866f34p+46f, 0x1.866f34p+46f),
(float2)(0x1.0953e2p+48f, 0x1.0953e2p+48f),
(float2)(0x1.689e22p+49f, 0x1.689e22p+49f),
(float2)(0x1.ea215ap+50f, 0x1.ea215ap+50f)
};
TABLE_FUNCTION(float2, LOGE_TBL, loge_tbl);
TABLE_FUNCTION(float, LOG_INV_TBL, log_inv_tbl);
TABLE_FUNCTION(float2, LOG2_TBL, log2_tbl);
@ -443,6 +484,8 @@ uint4 TABLE_MANGLE(pibits_tbl)(size_t idx) {
return *(__constant uint4 *)(PIBITS_TBL + idx);
}
TABLE_FUNCTION(float2, SINHCOSH_TBL, sinhcosh_tbl);
#ifdef cl_khr_fp64
DECLARE_TABLE(double2, LN_TBL, 65) = {
@ -835,7 +878,89 @@ DECLARE_TABLE(double2, TWO_TO_JBY64_EP, 64) = {
};
DECLARE_TABLE(double2, SINH_TBL, 37) = {
(double2)(0x0.0000000000000p+0, 0x0.0000000000000p+0),
(double2)(0x1.2cd9fc0000000p+0, 0x1.13ae6096a0092p-26),
(double2)(0x1.d03cf60000000p+1, 0x1.db70cfb79a640p-26),
(double2)(0x1.40926e0000000p+3, 0x1.c2526b66dc067p-23),
(double2)(0x1.b4a3800000000p+4, 0x1.b81b18647f380p-23),
(double2)(0x1.28d0160000000p+6, 0x1.bc1cdd1e1eb08p-20),
(double2)(0x1.936d228000000p+7, 0x1.d9f201534fb09p-19),
(double2)(0x1.1228768000000p+9, 0x1.d1c064a4e9954p-18),
(double2)(0x1.749ea50000000p+10, 0x1.4eca65d06ea74p-18),
(double2)(0x1.fa71570000000p+11, 0x1.0c259bcc0ecc5p-15),
(double2)(0x1.5829dc8000000p+13, 0x1.b5a6647cf9016p-13),
(double2)(0x1.d3c4488000000p+14, 0x1.9691adefb0870p-15),
(double2)(0x1.3de1650000000p+16, 0x1.3410fc29cde38p-10),
(double2)(0x1.b00b590000000p+17, 0x1.6a31a50b6fb3cp-11),
(double2)(0x1.259ac48000000p+19, 0x1.7defc71805c40p-10),
(double2)(0x1.8f0cca8000000p+20, 0x1.eb49fd80e0babp-6),
(double2)(0x1.0f2ebd0000000p+22, 0x1.4fffc7bcd5920p-7),
(double2)(0x1.7093488000000p+23, 0x1.03a93b6c63435p-3),
(double2)(0x1.f4f2208000000p+24, 0x1.1940bb255fd1cp-4),
(double2)(0x1.546d8f8000000p+26, 0x1.ed26e14260b50p-2),
(double2)(0x1.ceb0888000000p+27, 0x1.b47401fc9f2a2p+0),
(double2)(0x1.3a6e1f8000000p+29, 0x1.67bb3f55634f1p+3),
(double2)(0x1.ab5adb8000000p+30, 0x1.c435ff8194ddcp+2),
(double2)(0x1.226af30000000p+32, 0x1.d8fee052ba63ap+5),
(double2)(0x1.8ab7fb0000000p+33, 0x1.51d7edccde3f6p+7),
(double2)(0x1.0c3d390000000p+35, 0x1.04b1644557d1ap+8),
(double2)(0x1.6c93268000000p+36, 0x1.6a6b5ca0a9dc4p+8),
(double2)(0x1.ef822f0000000p+37, 0x1.fd9cc72249abap+11),
(double2)(0x1.50bba30000000p+39, 0x1.e58de693edab5p+13),
(double2)(0x1.c9aae40000000p+40, 0x1.8c70158ac6363p+14),
(double2)(0x1.3704708000000p+42, 0x1.7614764f43e20p+15),
(double2)(0x1.a6b7658000000p+43, 0x1.6337db36fc718p+17),
(double2)(0x1.1f43fc8000000p+45, 0x1.12d98b1f611e2p+19),
(double2)(0x1.866f348000000p+46, 0x1.392bc108b37ccp+19),
(double2)(0x1.0953e28000000p+48, 0x1.ce87bdc3473dcp+22),
(double2)(0x1.689e220000000p+49, 0x1.bc8d5ae99ad14p+21),
(double2)(0x1.ea215a0000000p+50, 0x1.d20d76744835cp+22),
};
DECLARE_TABLE(double2, COSH_TBL, 37) = {
(double2)(0x1.0000000000000p+0, 0x0.0000000000000p+0),
(double2)(0x1.8b07550000000p+0, 0x1.d9f5504c2bd28p-28),
(double2)(0x1.e18fa08000000p+1, 0x1.7cb66f0a4c9fdp-25),
(double2)(0x1.422a490000000p+3, 0x1.f58617928e588p-23),
(double2)(0x1.b4ee858000000p+4, 0x1.bc7d000c38d48p-25),
(double2)(0x1.28d6fc8000000p+6, 0x1.f7f9d4e329998p-21),
(double2)(0x1.936e678000000p+7, 0x1.6e6e464885269p-19),
(double2)(0x1.1228948000000p+9, 0x1.ba3a8b946c154p-19),
(double2)(0x1.749eaa8000000p+10, 0x1.3f4e76110d5a4p-18),
(double2)(0x1.fa71580000000p+11, 0x1.17622515a3e2bp-15),
(double2)(0x1.5829dd0000000p+13, 0x1.4dc4b528af3d0p-17),
(double2)(0x1.d3c4488000000p+14, 0x1.1156278615e10p-14),
(double2)(0x1.3de1650000000p+16, 0x1.35ad50ed821f5p-10),
(double2)(0x1.b00b590000000p+17, 0x1.6b61055f2935cp-11),
(double2)(0x1.259ac48000000p+19, 0x1.7e2794a601240p-10),
(double2)(0x1.8f0cca8000000p+20, 0x1.eb4b45f6aadd3p-6),
(double2)(0x1.0f2ebd0000000p+22, 0x1.5000b967b3698p-7),
(double2)(0x1.7093488000000p+23, 0x1.03a940fadc092p-3),
(double2)(0x1.f4f2208000000p+24, 0x1.1940bf3bf874cp-4),
(double2)(0x1.546d8f8000000p+26, 0x1.ed26e1a2a2110p-2),
(double2)(0x1.ceb0888000000p+27, 0x1.b4740205796d6p+0),
(double2)(0x1.3a6e1f8000000p+29, 0x1.67bb3f55cb85dp+3),
(double2)(0x1.ab5adb8000000p+30, 0x1.c435ff81e18acp+2),
(double2)(0x1.226af30000000p+32, 0x1.d8fee052bdea4p+5),
(double2)(0x1.8ab7fb0000000p+33, 0x1.51d7edccde926p+7),
(double2)(0x1.0c3d390000000p+35, 0x1.04b1644557e0ep+8),
(double2)(0x1.6c93268000000p+36, 0x1.6a6b5ca0a9e1cp+8),
(double2)(0x1.ef822f0000000p+37, 0x1.fd9cc72249abep+11),
(double2)(0x1.50bba30000000p+39, 0x1.e58de693edab5p+13),
(double2)(0x1.c9aae40000000p+40, 0x1.8c70158ac6364p+14),
(double2)(0x1.3704708000000p+42, 0x1.7614764f43e20p+15),
(double2)(0x1.a6b7658000000p+43, 0x1.6337db36fc718p+17),
(double2)(0x1.1f43fc8000000p+45, 0x1.12d98b1f611e2p+19),
(double2)(0x1.866f348000000p+46, 0x1.392bc108b37ccp+19),
(double2)(0x1.0953e28000000p+48, 0x1.ce87bdc3473dcp+22),
(double2)(0x1.689e220000000p+49, 0x1.bc8d5ae99ad14p+21),
(double2)(0x1.ea215a0000000p+50, 0x1.d20d76744835cp+22)
};
TABLE_FUNCTION(double2, ATAN_JBY256_TBL, atan_jby256_tbl);
TABLE_FUNCTION(double2, TWO_TO_JBY64_EP, two_to_jby64_ep_tbl);
TABLE_FUNCTION(double2, SINH_TBL, sinh_tbl);
TABLE_FUNCTION(double2, COSH_TBL, cosh_tbl);
#endif // cl_khr_fp64

View File

@ -42,6 +42,7 @@ TABLE_FUNCTION_DECL(float2, loge_tbl);
TABLE_FUNCTION_DECL(float, log_inv_tbl);
TABLE_FUNCTION_DECL(float2, log2_tbl);
TABLE_FUNCTION_DECL(uint4, pibits_tbl);
TABLE_FUNCTION_DECL(float2, sinhcosh_tbl);
#ifdef cl_khr_fp64
@ -50,4 +51,7 @@ TABLE_FUNCTION_DECL(uint4, pibits_tbl);
TABLE_FUNCTION_DECL(double2, ln_tbl);
TABLE_FUNCTION_DECL(double2, atan_jby256_tbl);
TABLE_FUNCTION_DECL(double2, two_to_jby64_ep_tbl);
TABLE_FUNCTION_DECL(double2, sinh_tbl);
TABLE_FUNCTION_DECL(double2, cosh_tbl);
#endif // cl_khr_fp64