Skip to content

[libclc] Move sinh, cosh & tanh to the CLC library #134063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions libclc/clc/include/clc/math/clc_cosh.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef __CLC_MATH_CLC_COSH_H__
#define __CLC_MATH_CLC_COSH_H__

#define __CLC_BODY <clc/math/unary_decl.inc>
#define __CLC_FUNCTION __clc_cosh

#include <clc/math/gentype.inc>

#undef __CLC_BODY
#undef __CLC_FUNCTION

#endif // __CLC_MATH_CLC_COSH_H__
20 changes: 20 additions & 0 deletions libclc/clc/include/clc/math/clc_sinh.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef __CLC_MATH_CLC_SINH_H__
#define __CLC_MATH_CLC_SINH_H__

#define __CLC_BODY <clc/math/unary_decl.inc>
#define __CLC_FUNCTION __clc_sinh

#include <clc/math/gentype.inc>

#undef __CLC_BODY
#undef __CLC_FUNCTION

#endif // __CLC_MATH_CLC_SINH_H__
20 changes: 20 additions & 0 deletions libclc/clc/include/clc/math/clc_tanh.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef __CLC_MATH_CLC_TANH_H__
#define __CLC_MATH_CLC_TANH_H__

#define __CLC_BODY <clc/math/unary_decl.inc>
#define __CLC_FUNCTION __clc_tanh

#include <clc/math/gentype.inc>

#undef __CLC_BODY
#undef __CLC_FUNCTION

#endif // __CLC_MATH_CLC_TANH_H__
9 changes: 6 additions & 3 deletions libclc/clc/include/clc/math/tables.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
TABLE_FUNCTION_DECL(float2, log2_tbl);
TABLE_FUNCTION_DECL(float2, log10_tbl);
TABLE_FUNCTION_DECL(uint4, pibits_tbl);
TABLE_FUNCTION_DECL(float2, sinhcosh_tbl);

CLC_TABLE_FUNCTION_DECL(float, log_inv_tbl_ep_head);
CLC_TABLE_FUNCTION_DECL(float, log_inv_tbl_ep_tail);
Expand All @@ -74,6 +73,8 @@ CLC_TABLE_FUNCTION_DECL(float, exp_tbl_ep_head);
CLC_TABLE_FUNCTION_DECL(float, exp_tbl_ep_tail);
CLC_TABLE_FUNCTION_DECL(float, cbrt_tbl_head);
CLC_TABLE_FUNCTION_DECL(float, cbrt_tbl_tail);
CLC_TABLE_FUNCTION_DECL(float, sinhcosh_tbl_head);
CLC_TABLE_FUNCTION_DECL(float, sinhcosh_tbl_tail);

#ifdef cl_khr_fp64

Expand All @@ -85,8 +86,10 @@ CLC_TABLE_FUNCTION_DECL(double, atan_jby256_tbl_head);
CLC_TABLE_FUNCTION_DECL(double, atan_jby256_tbl_tail);
CLC_TABLE_FUNCTION_DECL(double, two_to_jby64_ep_tbl_head);
CLC_TABLE_FUNCTION_DECL(double, two_to_jby64_ep_tbl_tail);
TABLE_FUNCTION_DECL(double2, sinh_tbl);
TABLE_FUNCTION_DECL(double2, cosh_tbl);
CLC_TABLE_FUNCTION_DECL(double, sinh_tbl_head);
CLC_TABLE_FUNCTION_DECL(double, sinh_tbl_tail);
CLC_TABLE_FUNCTION_DECL(double, cosh_tbl_head);
CLC_TABLE_FUNCTION_DECL(double, cosh_tbl_tail);
CLC_TABLE_FUNCTION_DECL(double, cbrt_inv_tbl);
CLC_TABLE_FUNCTION_DECL(double, cbrt_dbl_tbl_head);
CLC_TABLE_FUNCTION_DECL(double, cbrt_dbl_tbl_tail);
Expand Down
3 changes: 3 additions & 0 deletions libclc/clc/lib/generic/SOURCES
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ math/clc_atanpi.cl
math/clc_cbrt.cl
math/clc_ceil.cl
math/clc_copysign.cl
math/clc_cosh.cl
math/clc_cospi.cl
math/clc_ep_log.cl
math/clc_exp.cl
Expand Down Expand Up @@ -74,10 +75,12 @@ math/clc_rootn.cl
math/clc_round.cl
math/clc_rsqrt.cl
math/clc_sincos_helpers.cl
math/clc_sinh.cl
math/clc_sinpi.cl
math/clc_sqrt.cl
math/clc_sw_fma.cl
math/clc_tables.cl
math/clc_tanh.cl
math/clc_tanpi.cl
math/clc_trunc.cl
relational/clc_all.cl
Expand Down
24 changes: 24 additions & 0 deletions libclc/clc/lib/generic/math/clc_cosh.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include <clc/clc_convert.h>
#include <clc/float/definitions.h>
#include <clc/internal/clc.h>
#include <clc/math/clc_copysign.h>
#include <clc/math/clc_exp.h>
#include <clc/math/clc_fabs.h>
#include <clc/math/clc_fma.h>
#include <clc/math/clc_mad.h>
#include <clc/math/math.h>
#include <clc/math/tables.h>
#include <clc/relational/clc_isinf.h>
#include <clc/relational/clc_isnan.h>
#include <clc/shared/clc_min.h>

#define __CLC_BODY <clc_cosh.inc>
#include <clc/math/gentype.inc>
199 changes: 199 additions & 0 deletions libclc/clc/lib/generic/math/clc_cosh.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#if __CLC_FPSIZE == 32

_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_cosh(__CLC_GENTYPE 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 __CLC_GENTYPE max_cosh_arg = 0x1.65a9fap+6f;
const __CLC_GENTYPE small_threshold = 0x1.0a2b24p+3f;

__CLC_UINTN ux = __CLC_AS_UINTN(x);
__CLC_GENTYPE y = __clc_fabs(x);
__CLC_UINTN aux = __CLC_AS_UINTN(y);

// 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.

__CLC_INTN ind = __CLC_CONVERT_INTN(y);
ind = __CLC_CONVERT_UINTN(ind) > 36U ? 0 : ind;

__CLC_GENTYPE dy = y - __CLC_CONVERT_GENTYPE(ind);
__CLC_GENTYPE dy2 = dy * dy;

__CLC_GENTYPE sdy = __clc_mad(
dy2,
__clc_mad(
dy2,
__clc_mad(
dy2,
__clc_mad(
dy2,
__clc_mad(dy2,
__clc_mad(dy2, 0.7746188980094184251527126e-12f,
0.160576793121939886190847e-9f),
0.250521176994133472333666e-7f),
0.275573191913636406057211e-5f),
0.198412698413242405162014e-3f),
0.833333333333329931873097e-2f),
0.166666666666666667013899e0f);
sdy = __clc_mad(sdy, dy * dy2, dy);

__CLC_GENTYPE cdy = __clc_mad(
dy2,
__clc_mad(
dy2,
__clc_mad(
dy2,
__clc_mad(
dy2,
__clc_mad(dy2,
__clc_mad(dy2, 0.1163921388172173692062032e-10f,
0.208744349831471353536305e-8f),
0.275573350756016588011357e-6f),
0.248015872460622433115785e-4f),
0.138888888889814854814536e-2f),
0.416666666666660876512776e-1f),
0.500000000000000005911074e0f);
cdy = __clc_mad(cdy, dy2, 1.0f);

__CLC_GENTYPE sinhcoshh = USE_TABLE(sinhcosh_tbl_head, ind);
__CLC_GENTYPE sinhcosht = USE_TABLE(sinhcosh_tbl_tail, ind);
__CLC_GENTYPE z = __clc_mad(sinhcoshh, sdy, sinhcosht * cdy);

// When exp(-x) is insignificant compared to exp(x), return exp(x)/2
__CLC_GENTYPE t = __clc_exp(y - 0x1.62e500p-1f);
__CLC_GENTYPE zsmall = __clc_mad(0x1.a0210ep-18f, t, t);
z = y >= small_threshold ? zsmall : z;

// Corner cases
z = y >= max_cosh_arg ? __CLC_AS_GENTYPE((__CLC_UINTN)PINFBITPATT_SP32) : z;
z = aux > PINFBITPATT_SP32 ? __CLC_GENTYPE_NAN : z;
z = aux < 0x38800000 ? 1.0f : z;

return z;
}

#elif __CLC_FPSIZE == 64

_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_cosh(__CLC_GENTYPE 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) = 0x408633ce8fb9f87e
const __CLC_GENTYPE max_cosh_arg = 7.10475860073943977113e+02;

// This is where exp(-x) is insignificant compared to exp(x) = ln(2^27)
const __CLC_GENTYPE small_threshold = 0x1.2b708872320e2p+4;

__CLC_GENTYPE y = __clc_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.

__CLC_INTN ind = __clc_min(__CLC_CONVERT_INTN(y), 36);
__CLC_GENTYPE dy = y - __CLC_CONVERT_GENTYPE(ind);
__CLC_GENTYPE dy2 = dy * dy;

__CLC_GENTYPE sdy =
dy * dy2 *
__clc_fma(
dy2,
__clc_fma(
dy2,
__clc_fma(
dy2,
__clc_fma(
dy2,
__clc_fma(dy2,
__clc_fma(dy2, 0.7746188980094184251527126e-12,
0.160576793121939886190847e-9),
0.250521176994133472333666e-7),
0.275573191913636406057211e-5),
0.198412698413242405162014e-3),
0.833333333333329931873097e-2),
0.166666666666666667013899e0);

__CLC_GENTYPE cdy =
dy2 *
__clc_fma(
dy2,
__clc_fma(
dy2,
__clc_fma(
dy2,
__clc_fma(
dy2,
__clc_fma(dy2,
__clc_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.
__CLC_GENTYPE cl = USE_TABLE(cosh_tbl_head, ind);
__CLC_GENTYPE ct = USE_TABLE(cosh_tbl_tail, ind);
__CLC_GENTYPE sl = USE_TABLE(sinh_tbl_head, ind);
__CLC_GENTYPE st = USE_TABLE(sinh_tbl_tail, ind);

__CLC_GENTYPE z =
__clc_fma(
sl, dy,
__clc_fma(sl, sdy,
__clc_fma(cl, cdy,
__clc_fma(st, dy, __clc_fma(st, sdy, ct * cdy)) +
ct))) +
cl;

// Other cases
z = y < 0x1.0p-28 ? 1.0 : z;

__CLC_GENTYPE t = __clc_exp(y - 0x1.62e42fefa3800p-1);
t = __clc_fma(t, -0x1.ef35793c76641p-45, t);
z = y >= small_threshold ? t : z;

z = y >= max_cosh_arg ? __CLC_AS_GENTYPE((__CLC_ULONGN)PINFBITPATT_DP64) : z;

z = __clc_isinf(x) || __clc_isnan(x) ? y : z;

return z;
}

#elif __CLC_FPSIZE == 16

_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_cosh(__CLC_GENTYPE x) {
return __CLC_CONVERT_GENTYPE(__clc_cosh(__CLC_CONVERT_FLOATN(x)));
}

#endif
23 changes: 23 additions & 0 deletions libclc/clc/lib/generic/math/clc_sinh.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include <clc/clc_convert.h>
#include <clc/internal/clc.h>
#include <clc/math/clc_copysign.h>
#include <clc/math/clc_exp.h>
#include <clc/math/clc_fabs.h>
#include <clc/math/clc_fma.h>
#include <clc/math/clc_mad.h>
#include <clc/math/math.h>
#include <clc/math/tables.h>
#include <clc/relational/clc_isinf.h>
#include <clc/relational/clc_isnan.h>
#include <clc/shared/clc_min.h>

#define __CLC_BODY <clc_sinh.inc>
#include <clc/math/gentype.inc>
Loading