diff options
author | Joe Ramsay <Joe.Ramsay@arm.com> | 2023-01-05 10:29:55 +0000 |
---|---|---|
committer | Joe Ramsay <joe.ramsay@arm.com> | 2023-01-05 10:29:55 +0000 |
commit | 47c03a91d990e9dad25b7e9925c4b72533a242f7 (patch) | |
tree | 79a835ca01904e09a87b937fd6cc9e17cf5eeb70 | |
parent | 2364ce531894c760f0742e17c490069ffa0032bd (diff) | |
download | arm-optimized-routines-47c03a91d990e9dad25b7e9925c4b72533a242f7.tar.gz |
pl/math: Add scalar & vector/Neon tanh
New routines use the same algorithm, reliant on a modified version of
expm1, and are accurate to 3 ULP.
-rw-r--r-- | pl/math/include/mathlib.h | 5 | ||||
-rw-r--r-- | pl/math/s_tanh_3u.c | 6 | ||||
-rw-r--r-- | pl/math/tanh_3u.c | 82 | ||||
-rw-r--r-- | pl/math/test/testcases/directed/tanh.tst | 18 | ||||
-rw-r--r-- | pl/math/v_tanh_3u.c | 94 | ||||
-rw-r--r-- | pl/math/vn_tanh_3u.c | 12 |
6 files changed, 217 insertions, 0 deletions
diff --git a/pl/math/include/mathlib.h b/pl/math/include/mathlib.h index 67c3c9d..43c5ebc 100644 --- a/pl/math/include/mathlib.h +++ b/pl/math/include/mathlib.h @@ -37,6 +37,7 @@ double expm1 (double); double log10 (double); double log1p (double); double sinh (double); +double tanh (double); float __s_asinhf (float); float __s_atanf (float); @@ -67,6 +68,7 @@ double __s_log10 (double); double __s_log1p (double); double __s_log2 (double); double __s_sinh (double); +double __s_tanh (double); #if __aarch64__ #if __GNUC__ >= 5 @@ -108,6 +110,7 @@ __f32x4_t __v_sinhf (__f32x4_t); __f64x2_t __v_sinh (__f64x2_t); __f32x4_t __v_tanf (__f32x4_t); __f32x4_t __v_tanhf (__f32x4_t); +__f64x2_t __v_tanh (__f64x2_t); #if __GNUC__ >= 9 || __clang_major__ >= 8 #define __vpcs __attribute__((__aarch64_vector_pcs__)) @@ -141,6 +144,7 @@ __vpcs __f32x4_t __vn_sinhf (__f32x4_t); __vpcs __f64x2_t __vn_sinh (__f64x2_t); __vpcs __f32x4_t __vn_tanf (__f32x4_t); __vpcs __f32x4_t __vn_tanhf (__f32x4_t); +__vpcs __f64x2_t __vn_tanh (__f64x2_t); /* Vector functions following the vector PCS using ABI names. */ __vpcs __f32x4_t _ZGVnN4v_asinhf (__f32x4_t); @@ -171,6 +175,7 @@ __vpcs __f32x4_t _ZGVnN4v_sinhf (__f32x4_t); __vpcs __f64x2_t _ZGVnN2v_sinh (__f64x2_t); __vpcs __f32x4_t _ZGVnN4v_tanf (__f32x4_t); __vpcs __f32x4_t _ZGVnN4v_tanhf (__f32x4_t); +__vpcs __f64x2_t _ZGVnN2v_tanh (__f64x2_t); #endif diff --git a/pl/math/s_tanh_3u.c b/pl/math/s_tanh_3u.c new file mode 100644 index 0000000..a4d7bce --- /dev/null +++ b/pl/math/s_tanh_3u.c @@ -0,0 +1,6 @@ +/* + * Copyright (c) 2023, Arm Limited. + * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception + */ +#define SCALAR 1 +#include "v_tanh_3u.c" diff --git a/pl/math/tanh_3u.c b/pl/math/tanh_3u.c new file mode 100644 index 0000000..46d9fb3 --- /dev/null +++ b/pl/math/tanh_3u.c @@ -0,0 +1,82 @@ +/* + * Double-precision tanh(x) function. + * + * Copyright (c) 2023, Arm Limited. + * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception + */ +#include "math_config.h" +#include "estrin.h" +#include "pl_sig.h" +#include "pl_test.h" + +#define AbsMask 0x7fffffffffffffff +#define InvLn2 0x1.71547652b82fep0 +#define Ln2hi 0x1.62e42fefa39efp-1 +#define Ln2lo 0x1.abc9e3b39803fp-56 +#define Shift 0x1.8p52 +#define C(i) __expm1_poly[i] + +#define BoringBound 0x403241bf835f9d5f /* asuint64 (0x1.241bf835f9d5fp+4). */ +#define TinyBound 0x3e40000000000000 /* asuint64 (0x1p-27). */ +#define One 0x3ff0000000000000 + +static inline double +expm1_inline (double x) +{ + /* Helper routine for calculating exp(x) - 1. Copied from expm1_2u5.c, with + several simplifications: + - No special-case handling for tiny or special values. + - Simpler combination of p and t in final stage of the algorithm. + - Use shift-and-add instead of ldexp to calculate t. */ + + /* Reduce argument: f in [-ln2/2, ln2/2], i is exact. */ + double j = fma (InvLn2, x, Shift) - Shift; + int64_t i = j; + double f = fma (j, -Ln2hi, x); + f = fma (j, -Ln2lo, f); + + /* Approximate expm1(f) using polynomial. */ + double f2 = f * f; + double f4 = f2 * f2; + double p = fma (f2, ESTRIN_10 (f, f2, f4, f4 * f4, C), f); + + /* t = 2 ^ i. */ + double t = asdouble ((uint64_t) (i + 1023) << 52); + /* expm1(x) = p * t + (t - 1). */ + return fma (p, t, t - 1); +} + +/* Approximation for double-precision tanh(x), using a simplified version of + expm1. The greatest observed error is 2.75 ULP: + tanh(-0x1.c143c3a44e087p-3) got -0x1.ba31ba4691ab7p-3 + want -0x1.ba31ba4691ab4p-3. */ +double +tanh (double x) +{ + uint64_t ix = asuint64 (x); + uint64_t ia = ix & AbsMask; + uint64_t sign = ix & ~AbsMask; + + if (unlikely (ia > BoringBound)) + { + if (ia > 0x7ff0000000000000) + return __math_invalid (x); + return asdouble (One | sign); + } + + if (unlikely (ia < TinyBound)) + return x; + + /* tanh(x) = (e^2x - 1) / (e^2x + 1). */ + double q = expm1_inline (2 * x); + return q / (q + 2); +} + +PL_SIG (S, D, 1, tanh, -10.0, 10.0) +PL_TEST_ULP (tanh, 2.26) +PL_TEST_INTERVAL (tanh, 0, TinyBound, 1000) +PL_TEST_INTERVAL (tanh, -0, -TinyBound, 1000) +PL_TEST_INTERVAL (tanh, TinyBound, BoringBound, 100000) +PL_TEST_INTERVAL (tanh, -TinyBound, -BoringBound, 100000) +PL_TEST_INTERVAL (tanh, BoringBound, inf, 1000) +PL_TEST_INTERVAL (tanh, -BoringBound, -inf, 1000) diff --git a/pl/math/test/testcases/directed/tanh.tst b/pl/math/test/testcases/directed/tanh.tst new file mode 100644 index 0000000..4a02c55 --- /dev/null +++ b/pl/math/test/testcases/directed/tanh.tst @@ -0,0 +1,18 @@ +; tanh.tst +; +; Copyright 1999-2023, Arm Limited. +; SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception + +func=tanh op1=7ff80000.00000001 result=7ff80000.00000001 errno=0 +func=tanh op1=fff80000.00000001 result=7ff80000.00000001 errno=0 +func=tanh op1=7ff00000.00000001 result=7ff80000.00000001 errno=0 status=i +func=tanh op1=fff00000.00000001 result=7ff80000.00000001 errno=0 status=i +func=tanh op1=7ff00000.00000000 result=3ff00000.00000000 errno=0 +func=tanh op1=fff00000.00000000 result=bff00000.00000000 errno=0 +func=tanh op1=00000000.00000000 result=00000000.00000000 errno=0 +func=tanh op1=80000000.00000000 result=80000000.00000000 errno=0 +; No exception is raised with certain versions of glibc. Functions +; approximated by x near zero may not generate/implement flops and +; thus may not raise exceptions. +func=tanh op1=00000000.00000001 result=00000000.00000001 errno=0 maybestatus=ux +func=tanh op1=80000000.00000001 result=80000000.00000001 errno=0 maybestatus=ux diff --git a/pl/math/v_tanh_3u.c b/pl/math/v_tanh_3u.c new file mode 100644 index 0000000..c8b6c25 --- /dev/null +++ b/pl/math/v_tanh_3u.c @@ -0,0 +1,94 @@ +/* + * Double-precision vector tanh(x) function. + * Copyright (c) 2023, Arm Limited. + * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception + */ + +#include "v_math.h" +#include "estrin.h" +#include "mathlib.h" +#include "pl_sig.h" +#include "pl_test.h" + +#if V_SUPPORTED + +#define AbsMask v_u64 (0x7fffffffffffffff) +#define InvLn2 v_f64 (0x1.71547652b82fep0) +#define MLn2hi v_f64 (-0x1.62e42fefa39efp-1) +#define MLn2lo v_f64 (-0x1.abc9e3b39803fp-56) +#define Shift v_f64 (0x1.8p52) +#define C(i) v_f64 (__expm1_poly[i]) + +#define BoringBound 0x403241bf835f9d5f /* asuint64 (0x1.241bf835f9d5fp+4). */ +#define TinyBound 0x3e40000000000000 /* asuint64 (0x1p-27). */ +#define One v_u64 (0x3ff0000000000000) + +static inline v_f64_t +expm1_inline (v_f64_t x) +{ + /* Helper routine for calculating exp(x) - 1. Vector port of the helper from + the scalar variant of tanh. */ + + /* Reduce argument: f in [-ln2/2, ln2/2], i is exact. */ + v_f64_t j = v_fma_f64 (InvLn2, x, Shift) - Shift; + v_s64_t i = v_to_s64_f64 (j); + v_f64_t f = v_fma_f64 (j, MLn2hi, x); + f = v_fma_f64 (j, MLn2lo, f); + + /* Approximate expm1(f) using polynomial. */ + v_f64_t f2 = f * f; + v_f64_t f4 = f2 * f2; + v_f64_t p = v_fma_f64 (f2, ESTRIN_10 (f, f2, f4, f4 * f4, C), f); + + /* t = 2 ^ i. */ + v_f64_t t = v_as_f64_u64 (v_as_u64_s64 (i << 52) + One); + /* expm1(x) = p * t + (t - 1). */ + return v_fma_f64 (p, t, t - 1); +} + +static NOINLINE v_f64_t +special_case (v_f64_t x, v_f64_t y, v_u64_t special) +{ + return v_call_f64 (tanh, x, y, special); +} + +/* Vector approximation for double-precision tanh(x), using a simplified + version of expm1. The greatest observed error is 2.75 ULP: + __v_tanh(-0x1.c143c3a44e087p-3) got -0x1.ba31ba4691ab7p-3 + want -0x1.ba31ba4691ab4p-3. */ +VPCS_ATTR v_f64_t V_NAME (tanh) (v_f64_t x) +{ + v_u64_t ix = v_as_u64_f64 (x); + v_u64_t ia = ix & AbsMask; + + /* Trigger special-cases for tiny, boring and infinity/NaN. */ + v_u64_t special = v_cond_u64 ((ia - TinyBound) > (BoringBound - TinyBound)); + v_f64_t u; + + /* To trigger fp exceptions correctly, set special lanes to a neutral value. + They will be fixed up later by the special-case handler. */ + if (unlikely (v_any_u64 (special))) + u = v_sel_f64 (special, v_f64 (1), x) * 2; + else + u = x * 2; + + /* tanh(x) = (e^2x - 1) / (e^2x + 1). */ + v_f64_t q = expm1_inline (u); + v_f64_t y = q / (q + 2); + + if (unlikely (v_any_u64 (special))) + return special_case (x, y, special); + return y; +} +VPCS_ALIAS + +PL_SIG (V, D, 1, tanh, -10.0, 10.0) +PL_TEST_ULP (V_NAME (tanh), 2.26) +PL_TEST_EXPECT_FENV_ALWAYS (V_NAME (tanh)) +PL_TEST_INTERVAL (V_NAME (tanh), 0, TinyBound, 1000) +PL_TEST_INTERVAL (V_NAME (tanh), -0, -TinyBound, 1000) +PL_TEST_INTERVAL (V_NAME (tanh), TinyBound, BoringBound, 100000) +PL_TEST_INTERVAL (V_NAME (tanh), -TinyBound, -BoringBound, 100000) +PL_TEST_INTERVAL (V_NAME (tanh), BoringBound, inf, 1000) +PL_TEST_INTERVAL (V_NAME (tanh), -BoringBound, -inf, 1000) +#endif diff --git a/pl/math/vn_tanh_3u.c b/pl/math/vn_tanh_3u.c new file mode 100644 index 0000000..cb2746c --- /dev/null +++ b/pl/math/vn_tanh_3u.c @@ -0,0 +1,12 @@ +/* + * AdvSIMD vector PCS variant of __v_tanh. + * + * Copyright (c) 2023, Arm Limited. + * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception + */ +#include "include/mathlib.h" +#ifdef __vpcs +#define VPCS 1 +#define VPCS_ALIAS PL_ALIAS (__vn_tanh, _ZGVnN2v_tanh) +#include "v_tanh_3u.c" +#endif |