authorgravatar for pivoc@protonmail.comPivok <pivoc@protonmail.com> 2026-02-09 07:59:40+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-02-09 07:59:40+01:00
logf1b255402377b41e47cec65b4dbc2633e1dd7b83
treeb0de668b854d02982424b7b079102ea02aec2959
parentfb18f2096aaff3a26c309da635472a2fb8b303cd

libzigc: hypotf, hypotl (#31150)

Implements hypotf and hypotl for libzigc #30978. Continuation of #31104 Commands i run: ``` $ stage3/bin/zig build -p stage4 -Denable-llvm -Dno-lib $ stage4/bin/zig build test-libc -Dlibc-test-path=../../libc-test -Dtest-filter=hypotf -fqemu -fwasmtime --summary line Build Summary: 365/369 steps succeeded (4 skipped) $ stage4/bin/zig build test-modules -Dtest-target-filter=windows -Dtest-filter=hypotf --summary line Build Summary: 53/101 steps succeeded (48 skipped) ``` Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31150 Reviewed-by: Andrew Kelley <andrew@ziglang.org> Co-authored-by: Pivok <pivoc@protonmail.com> Co-committed-by: Pivok <pivoc@protonmail.com>

9 files changed, 10 insertions(+), 255 deletions(-)

lib/c/math.zig+10
...@@ -29,6 +29,8 @@ comptime {...@@ -29,6 +29,8 @@ comptime {
29 }29 }
3030
31 if (builtin.target.isMinGW() or builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) {31 if (builtin.target.isMinGW() or builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) {
32 @export(&hypotf, .{ .name = "hypotf", .linkage = common.linkage, .visibility = common.visibility });
33 @export(&hypotl, .{ .name = "hypotl", .linkage = common.linkage, .visibility = common.visibility });
32 @export(&nan, .{ .name = "nan", .linkage = common.linkage, .visibility = common.visibility });34 @export(&nan, .{ .name = "nan", .linkage = common.linkage, .visibility = common.visibility });
33 @export(&nanf, .{ .name = "nanf", .linkage = common.linkage, .visibility = common.visibility });35 @export(&nanf, .{ .name = "nanf", .linkage = common.linkage, .visibility = common.visibility });
34 @export(&nanl, .{ .name = "nanl", .linkage = common.linkage, .visibility = common.visibility });36 @export(&nanl, .{ .name = "nanl", .linkage = common.linkage, .visibility = common.visibility });
...@@ -123,6 +125,14 @@ fn hypot(x: f64, y: f64) callconv(.c) f64 {...@@ -123,6 +125,14 @@ fn hypot(x: f64, y: f64) callconv(.c) f64 {
123 return math.hypot(x, y);125 return math.hypot(x, y);
124}126}
125127
128fn hypotf(x: f32, y: f32) callconv(.c) f32 {
129 return math.hypot(x, y);
130}
131
132fn hypotl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {
133 return math.hypot(x, y);
134}
135
126fn pow(x: f64, y: f64) callconv(.c) f64 {136fn pow(x: f64, y: f64) callconv(.c) f64 {
127 return math.pow(f64, x, y);137 return math.pow(f64, x, y);
128}138}
lib/libc/mingw/math/hypotf.c deleted-23
...@@ -1,23 +0,0 @@
1/**
2 * This file is part of the mingw-w64 runtime package.
3 * No warranty is given; refer to the file DISCLAIMER within this package.
4 */
5#define _NEW_COMPLEX_FLOAT 1
6
7#include "../complex/complex_internal.h"
8#include <errno.h>
9#include <math.h>
10
11float hypotf (float x, float y)
12{
13 int x_class = fpclassify (x);
14 int y_class = fpclassify (y);
15
16 if (x_class == FP_INFINITE || y_class == FP_INFINITE)
17 return __FLT_HUGE_VAL;
18 else if (x_class == FP_NAN || y_class == FP_NAN)
19 return __FLT_NAN;
20
21 return (float) _hypot (x, y);
22}
23
lib/libc/mingw/math/hypotl.c deleted-82
...@@ -1,82 +0,0 @@
1/**
2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the mingw-w64 runtime package.
4 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5 */
6#include <math.h>
7#include <float.h>
8#include <errno.h>
9
10/*
11This implementation is based largely on Cephes library
12function cabsl (cmplxl.c), which bears the following notice:
13
14Cephes Math Library Release 2.1: January, 1989
15Copyright 1984, 1987, 1989 by Stephen L. Moshier
16Direct inquiries to 30 Frost Street, Cambridge, MA 02140
17*/
18
19/*
20 Modified for use in libmingwex.a
21 02 Sept 2002 Danny Smith <dannysmith@users.sourceforege.net>
22 Calls to ldexpl replaced by logbl and calls to frexpl replaced
23 by scalbnl to avoid duplicated range checks.
24*/
25
26#define PRECL 32
27
28long double
29hypotl (long double x, long double y)
30{
31 int exx;
32 int eyy;
33 int scale;
34 long double xx =fabsl(x);
35 long double yy =fabsl(y);
36 if (!isfinite(xx) || !isfinite(yy))
37 {
38 /* Annex F.9.4.3, hypot returns +infinity if
39 either component is an infinity, even when the
40 other component is NaN. */
41 return (isinf(xx) || isinf(yy)) ? INFINITY : NAN;
42 }
43
44 if (xx == 0.0L)
45 return yy;
46 if (yy == 0.0L)
47 return xx;
48
49 /* Get exponents */
50 exx = logbl (xx);
51 eyy = logbl (yy);
52
53 /* Check if large differences in scale */
54 scale = exx - eyy;
55 if ( scale > PRECL)
56 return xx;
57 if ( scale < -PRECL)
58 return yy;
59
60 /* Exponent of approximate geometric mean (x 2) */
61 scale = (exx + eyy) >> 1;
62
63 /* Rescale: Geometric mean is now about 2 */
64 x = scalbnl(xx, -scale);
65 y = scalbnl(yy, -scale);
66
67 xx = sqrtl(x * x + y * y);
68
69 /* Check for overflow and underflow */
70 exx = logbl(xx);
71 exx += scale;
72 if (exx > LDBL_MAX_EXP)
73 {
74 errno = ERANGE;
75 return INFINITY;
76 }
77 if (exx < LDBL_MIN_EXP)
78 return 0.0L;
79
80 /* Undo scaling */
81 return (scalbnl (xx, scale));
82}
lib/libc/musl/src/math/hypotf.c deleted-35
...@@ -1,35 +0,0 @@
1#include <math.h>
2#include <stdint.h>
3
4float hypotf(float x, float y)
5{
6 union {float f; uint32_t i;} ux = {x}, uy = {y}, ut;
7 float_t z;
8
9 ux.i &= -1U>>1;
10 uy.i &= -1U>>1;
11 if (ux.i < uy.i) {
12 ut = ux;
13 ux = uy;
14 uy = ut;
15 }
16
17 x = ux.f;
18 y = uy.f;
19 if (uy.i == 0xff<<23)
20 return y;
21 if (ux.i >= 0xff<<23 || uy.i == 0 || ux.i - uy.i >= 25<<23)
22 return x + y;
23
24 z = 1;
25 if (ux.i >= (0x7f+60)<<23) {
26 z = 0x1p90f;
27 x *= 0x1p-90f;
28 y *= 0x1p-90f;
29 } else if (uy.i < (0x7f-60)<<23) {
30 z = 0x1p-90f;
31 x *= 0x1p90f;
32 y *= 0x1p90f;
33 }
34 return z*sqrtf((double)x*x + (double)y*y);
35}
lib/libc/musl/src/math/hypotl.c deleted-66
...@@ -1,66 +0,0 @@
1#include "libm.h"
2
3#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
4long double hypotl(long double x, long double y)
5{
6 return hypot(x, y);
7}
8#elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
9#if LDBL_MANT_DIG == 64
10#define SPLIT (0x1p32L+1)
11#elif LDBL_MANT_DIG == 113
12#define SPLIT (0x1p57L+1)
13#endif
14
15static void sq(long double *hi, long double *lo, long double x)
16{
17 long double xh, xl, xc;
18 xc = x*SPLIT;
19 xh = x - xc + xc;
20 xl = x - xh;
21 *hi = x*x;
22 *lo = xh*xh - *hi + 2*xh*xl + xl*xl;
23}
24
25long double hypotl(long double x, long double y)
26{
27 union ldshape ux = {x}, uy = {y};
28 int ex, ey;
29 long double hx, lx, hy, ly, z;
30
31 ux.i.se &= 0x7fff;
32 uy.i.se &= 0x7fff;
33 if (ux.i.se < uy.i.se) {
34 ex = uy.i.se;
35 ey = ux.i.se;
36 x = uy.f;
37 y = ux.f;
38 } else {
39 ex = ux.i.se;
40 ey = uy.i.se;
41 x = ux.f;
42 y = uy.f;
43 }
44
45 if (ex == 0x7fff && isinf(y))
46 return y;
47 if (ex == 0x7fff || y == 0)
48 return x;
49 if (ex - ey > LDBL_MANT_DIG)
50 return x + y;
51
52 z = 1;
53 if (ex > 0x3fff+8000) {
54 z = 0x1p10000L;
55 x *= 0x1p-10000L;
56 y *= 0x1p-10000L;
57 } else if (ey < 0x3fff-8000) {
58 z = 0x1p-10000L;
59 x *= 0x1p10000L;
60 y *= 0x1p10000L;
61 }
62 sq(&hx, &lx, x);
63 sq(&hy, &ly, y);
64 return z*sqrtl(ly+lx+hy+hx);
65}
66#endif
lib/libc/musl/src/math/i386/hypotf.s deleted-42
...@@ -1,42 +0,0 @@
1.global hypotf
2.type hypotf,@function
3hypotf:
4 mov 4(%esp),%eax
5 mov 8(%esp),%ecx
6 add %eax,%eax
7 add %ecx,%ecx
8 and %eax,%ecx
9 cmp $0xff000000,%ecx
10 jae 2f
11 test %eax,%eax
12 jnz 1f
13 flds 8(%esp)
14 fabs
15 ret
161: mov 8(%esp),%eax
17 add %eax,%eax
18 jnz 1f
19 flds 4(%esp)
20 fabs
21 ret
221: flds 4(%esp)
23 fld %st(0)
24 fmulp
25 flds 8(%esp)
26 fld %st(0)
27 fmulp
28 faddp
29 fsqrt
30 ret
312: cmp $0xff000000,%eax
32 jnz 1f
33 flds 4(%esp)
34 fabs
35 ret
361: mov 8(%esp),%eax
37 add %eax,%eax
38 cmp $0xff000000,%eax
39 flds 8(%esp)
40 jnz 1f
41 fabs
421: ret
src/libs/mingw.zig-2
...@@ -615,8 +615,6 @@ const mingw32_generic_src = [_][]const u8{...@@ -615,8 +615,6 @@ const mingw32_generic_src = [_][]const u8{
615 "math" ++ path.sep_str ++ "fpclassifyl.c",615 "math" ++ path.sep_str ++ "fpclassifyl.c",
616 "math" ++ path.sep_str ++ "frexpf.c",616 "math" ++ path.sep_str ++ "frexpf.c",
617 "math" ++ path.sep_str ++ "frexpl.c",617 "math" ++ path.sep_str ++ "frexpl.c",
618 "math" ++ path.sep_str ++ "hypotf.c",
619 "math" ++ path.sep_str ++ "hypotl.c",
620 "math" ++ path.sep_str ++ "ldexpf.c",618 "math" ++ path.sep_str ++ "ldexpf.c",
621 "math" ++ path.sep_str ++ "lgamma.c",619 "math" ++ path.sep_str ++ "lgamma.c",
622 "math" ++ path.sep_str ++ "lgammaf.c",620 "math" ++ path.sep_str ++ "lgammaf.c",
src/libs/musl.zig-3
...@@ -874,8 +874,6 @@ const src_files = [_][]const u8{...@@ -874,8 +874,6 @@ const src_files = [_][]const u8{
874 "musl/src/math/frexp.c",874 "musl/src/math/frexp.c",
875 "musl/src/math/frexpf.c",875 "musl/src/math/frexpf.c",
876 "musl/src/math/frexpl.c",876 "musl/src/math/frexpl.c",
877 "musl/src/math/hypotf.c",
878 "musl/src/math/hypotl.c",
879 "musl/src/math/i386/acosf.s",877 "musl/src/math/i386/acosf.s",
880 "musl/src/math/i386/acosl.s",878 "musl/src/math/i386/acosl.s",
881 "musl/src/math/i386/asinf.s",879 "musl/src/math/i386/asinf.s",
...@@ -888,7 +886,6 @@ const src_files = [_][]const u8{...@@ -888,7 +886,6 @@ const src_files = [_][]const u8{
888 "musl/src/math/i386/exp_ld.s",886 "musl/src/math/i386/exp_ld.s",
889 "musl/src/math/i386/expl.s",887 "musl/src/math/i386/expl.s",
890 "musl/src/math/i386/expm1l.s",888 "musl/src/math/i386/expm1l.s",
891 "musl/src/math/i386/hypotf.s",
892 "musl/src/math/i386/__invtrigl.s",889 "musl/src/math/i386/__invtrigl.s",
893 "musl/src/math/i386/ldexpf.s",890 "musl/src/math/i386/ldexpf.s",
894 "musl/src/math/i386/ldexpl.s",891 "musl/src/math/i386/ldexpl.s",
src/libs/wasi_libc.zig-2
...@@ -730,8 +730,6 @@ const libc_top_half_src_files = [_][]const u8{...@@ -730,8 +730,6 @@ const libc_top_half_src_files = [_][]const u8{
730 "musl/src/math/frexp.c",730 "musl/src/math/frexp.c",
731 "musl/src/math/frexpf.c",731 "musl/src/math/frexpf.c",
732 "musl/src/math/frexpl.c",732 "musl/src/math/frexpl.c",
733 "musl/src/math/hypotf.c",
734 "musl/src/math/hypotl.c",
735 "musl/src/math/ilogb.c",733 "musl/src/math/ilogb.c",
736 "musl/src/math/ilogbf.c",734 "musl/src/math/ilogbf.c",
737 "musl/src/math/ilogbl.c",735 "musl/src/math/ilogbl.c",