authorgravatar for hi@mihaelm.commihael <hi@mihaelm.com> 2026-03-15 23:13:24+01:00
committergravatar for hi@mihaelm.commihael <hi@mihaelm.com> 2026-03-17 23:33:10+01:00
logfd680ba5fbe99dde71b6ec28ecae1cddf4fed71b
treea5b1fc432fb8ae0c8e0b4718070bc297ff46ba16
parent4cad6f5372f6f17e249c24fba4d23466fa853fc0
signaturebadge-check Signed by SSH key SHA256:aoFoShdYLdrqMichqKXSSieTKUfACUIDJHsKc4V2tQg

`libzigc`: Implement `tanh`

The changes were tested by running following commands: ``` $ ./build/stage3/bin/zig build -p stage4 -Denable-llvm -Dno-lib $ stage4/bin/zig build test-libc -Dlibc-test-path=<LIBC-TEST-PATH> -Dtest-filter=tanh -fqemu -fwasmtime --summary line Build Summary: 1289/1289 steps succeeded ```

4 files changed, 5 insertions(+), 47 deletions(-)

lib/c/math.zig+5
...@@ -63,6 +63,7 @@ comptime {...@@ -63,6 +63,7 @@ comptime {
63 symbol(&pow, "pow");63 symbol(&pow, "pow");
64 symbol(&pow10, "pow10");64 symbol(&pow10, "pow10");
65 symbol(&pow10f, "pow10f");65 symbol(&pow10f, "pow10f");
66 symbol(&tanh, "tanh");
66 }67 }
6768
68 if (builtin.target.isMuslLibC()) {69 if (builtin.target.isMuslLibC()) {
...@@ -338,3 +339,7 @@ test "rint" {...@@ -338,3 +339,7 @@ test "rint" {
338 try expectEqual(@as(f64, 2.0), rint(2.5));339 try expectEqual(@as(f64, 2.0), rint(2.5));
339 try expectEqual(@as(f64, 4.0), rint(3.5));340 try expectEqual(@as(f64, 4.0), rint(3.5));
340}341}
342
343fn tanh(x: f64) callconv(.c) f64 {
344 return math.tanh(x);
345}
lib/libc/musl/src/math/tanh.c deleted-45
...@@ -1,45 +0,0 @@
1#include "libm.h"
2
3/* tanh(x) = (exp(x) - exp(-x))/(exp(x) + exp(-x))
4 * = (exp(2*x) - 1)/(exp(2*x) - 1 + 2)
5 * = (1 - exp(-2*x))/(exp(-2*x) - 1 + 2)
6 */
7double tanh(double x)
8{
9 union {double f; uint64_t i;} u = {.f = x};
10 uint32_t w;
11 int sign;
12 double_t t;
13
14 /* x = |x| */
15 sign = u.i >> 63;
16 u.i &= (uint64_t)-1/2;
17 x = u.f;
18 w = u.i >> 32;
19
20 if (w > 0x3fe193ea) {
21 /* |x| > log(3)/2 ~= 0.5493 or nan */
22 if (w > 0x40340000) {
23 /* |x| > 20 or nan */
24 /* note: this branch avoids raising overflow */
25 t = 1 - 0/x;
26 } else {
27 t = expm1(2*x);
28 t = 1 - 2/(t+2);
29 }
30 } else if (w > 0x3fd058ae) {
31 /* |x| > log(5/3)/2 ~= 0.2554 */
32 t = expm1(2*x);
33 t = t/(t+2);
34 } else if (w >= 0x00100000) {
35 /* |x| >= 0x1p-1022, up to 2ulp error in [0.1,0.2554] */
36 t = expm1(-2*x);
37 t = -t/(t+2);
38 } else {
39 /* |x| is subnormal */
40 /* note: the branch above would not raise underflow in [0x1p-1023,0x1p-1022) */
41 FORCE_EVAL((float)x);
42 t = x;
43 }
44 return sign ? -t : t;
45}
src/libs/musl.zig-1
...@@ -1003,7 +1003,6 @@ const src_files = [_][]const u8{...@@ -1003,7 +1003,6 @@ const src_files = [_][]const u8{
1003 "musl/src/math/sinl.c",1003 "musl/src/math/sinl.c",
1004 "musl/src/math/__tan.c",1004 "musl/src/math/__tan.c",
1005 "musl/src/math/__tandf.c",1005 "musl/src/math/__tandf.c",
1006 "musl/src/math/tanh.c",
1007 "musl/src/math/tanhf.c",1006 "musl/src/math/tanhf.c",
1008 "musl/src/math/tanhl.c",1007 "musl/src/math/tanhl.c",
1009 "musl/src/math/__tanl.c",1008 "musl/src/math/__tanl.c",
src/libs/wasi_libc.zig-1
...@@ -793,7 +793,6 @@ const libc_top_half_src_files = [_][]const u8{...@@ -793,7 +793,6 @@ const libc_top_half_src_files = [_][]const u8{
793 "musl/src/math/sinl.c",793 "musl/src/math/sinl.c",
794 "musl/src/math/__tan.c",794 "musl/src/math/__tan.c",
795 "musl/src/math/__tandf.c",795 "musl/src/math/__tandf.c",
796 "musl/src/math/tanh.c",
797 "musl/src/math/tanhf.c",796 "musl/src/math/tanhf.c",
798 "musl/src/math/tanhl.c",797 "musl/src/math/tanhl.c",
799 "musl/src/math/__tanl.c",798 "musl/src/math/__tanl.c",