commit dbfe34167de6b2a3a46ee4aff8f016c5afda7ccf (tree)
parent 4ea99049230b5be00ca5a09d3a3fd78ecba30594
Author: Andrew Kelley <andrew@ziglang.org>
Date: Fri, 13 Feb 2026 11:31:15 -0800
Merge branch 'jeffective-jeff/libzigc-acosf'
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30996
Diffstat:
7 files changed, 13 insertions(+), 94 deletions(-)
diff --git a/lib/c/math.zig b/lib/c/math.zig
@@ -51,6 +51,7 @@ comptime {
symbol(&pow, "pow");
symbol(&pow10, "pow10");
symbol(&pow10f, "pow10f");
+ symbol(&acosf, "acosf");
}
if (builtin.target.isMuslLibC()) {
@@ -84,6 +85,10 @@ fn atanl(x: c_longdouble) callconv(.c) c_longdouble {
};
}
+fn acosf(x: f32) callconv(.c) f32 {
+ return std.math.acos(x);
+}
+
fn isnan(x: f64) callconv(.c) c_int {
return if (math.isNan(x)) 1 else 0;
}
diff --git a/lib/libc/musl/src/math/acosf.c b/lib/libc/musl/src/math/acosf.c
@@ -1,71 +0,0 @@
-/* origin: FreeBSD /usr/src/lib/msun/src/e_acosf.c */
-/*
- * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
- */
-/*
- * ====================================================
- * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
- *
- * Developed at SunPro, a Sun Microsystems, Inc. business.
- * Permission to use, copy, modify, and distribute this
- * software is freely granted, provided that this notice
- * is preserved.
- * ====================================================
- */
-
-#include "libm.h"
-
-static const float
-pio2_hi = 1.5707962513e+00, /* 0x3fc90fda */
-pio2_lo = 7.5497894159e-08, /* 0x33a22168 */
-pS0 = 1.6666586697e-01,
-pS1 = -4.2743422091e-02,
-pS2 = -8.6563630030e-03,
-qS1 = -7.0662963390e-01;
-
-static float R(float z)
-{
- float_t p, q;
- p = z*(pS0+z*(pS1+z*pS2));
- q = 1.0f+z*qS1;
- return p/q;
-}
-
-float acosf(float x)
-{
- float z,w,s,c,df;
- uint32_t hx,ix;
-
- GET_FLOAT_WORD(hx, x);
- ix = hx & 0x7fffffff;
- /* |x| >= 1 or nan */
- if (ix >= 0x3f800000) {
- if (ix == 0x3f800000) {
- if (hx >> 31)
- return 2*pio2_hi + 0x1p-120f;
- return 0;
- }
- return 0/(x-x);
- }
- /* |x| < 0.5 */
- if (ix < 0x3f000000) {
- if (ix <= 0x32800000) /* |x| < 2**-26 */
- return pio2_hi + 0x1p-120f;
- return pio2_hi - (x - (pio2_lo-x*R(x*x)));
- }
- /* x < -0.5 */
- if (hx >> 31) {
- z = (1+x)*0.5f;
- s = sqrtf(z);
- w = R(z)*s-pio2_lo;
- return 2*(pio2_hi - (s+w));
- }
- /* x > 0.5 */
- z = (1-x)*0.5f;
- s = sqrtf(z);
- GET_FLOAT_WORD(hx,s);
- SET_FLOAT_WORD(df,hx&0xfffff000);
- c = (z-df*df)/(s+df);
- w = R(z)*s+c;
- return 2*(df+w);
-}
diff --git a/lib/libc/musl/src/math/i386/acosf.s b/lib/libc/musl/src/math/i386/acosf.s
@@ -1,16 +0,0 @@
-.global acosf
-.type acosf,@function
-acosf:
- flds 4(%esp)
- fld %st(0)
- fld1
- fsub %st(0),%st(1)
- fadd %st(2)
- fmulp
- fsqrt
- fabs # fix sign of zero (matters in downward rounding mode)
- fxch %st(1)
- fpatan
- fstps 4(%esp)
- flds 4(%esp)
- ret
diff --git a/lib/std/math/acos.zig b/lib/std/math/acos.zig
@@ -81,9 +81,13 @@ fn rationalApproxBinary32(z: f32) f32 {
const pS2: f32 = -8.6563630030e-03;
const qS1: f32 = -7.0662963390e-01;
- const p = z * (pS0 + z * (pS1 + z * pS2));
- const q = 1.0 + z * qS1;
- return p / q;
+ // f64 is used instead of f32 to avoid
+ // a vectorization on x86_64. The vectorization
+ // causes extra floating point execeptions
+ // that are prohibited by libc-test.
+ const p: f64 = @as(f64, @floatCast(z)) * (pS0 + z * (pS1 + z * pS2));
+ const q: f64 = 1.0 + z * qS1;
+ return @floatCast(p / q);
}
fn acosBinary32(x: f32) f32 {
diff --git a/src/libs/musl.zig b/src/libs/musl.zig
@@ -798,7 +798,6 @@ const src_files = [_][]const u8{
"musl/src/math/aarch64/nearbyint.c",
"musl/src/math/aarch64/nearbyintf.c",
"musl/src/math/aarch64/rintf.c",
- "musl/src/math/acosf.c",
"musl/src/math/acosh.c",
"musl/src/math/acoshf.c",
"musl/src/math/acoshl.c",
@@ -852,7 +851,6 @@ const src_files = [_][]const u8{
"musl/src/math/frexp.c",
"musl/src/math/frexpf.c",
"musl/src/math/frexpl.c",
- "musl/src/math/i386/acosf.s",
"musl/src/math/i386/acosl.s",
"musl/src/math/i386/asinf.s",
"musl/src/math/i386/asinl.s",
diff --git a/src/libs/wasi_libc.zig b/src/libs/wasi_libc.zig
@@ -664,7 +664,6 @@ const libc_top_half_src_files = [_][]const u8{
"musl/src/locale/strtod_l.c",
"musl/src/locale/wcscoll.c",
"musl/src/locale/wcsxfrm.c",
- "musl/src/math/acosf.c",
"musl/src/math/acosh.c",
"musl/src/math/acoshf.c",
"musl/src/math/acoshl.c",
diff --git a/test/libc.zig b/test/libc.zig
@@ -139,7 +139,7 @@ pub fn addCases(cases: *tests.LibcContext) void {
cases.addLibcTestCase("regression/wcsstr-false-negative.c", true, .{});
cases.addLibcTestCase("math/acos.c", true, .{});
- // cases.addLibcTestCase("math/acosf.c", true, .{});
+ cases.addLibcTestCase("math/acosf.c", true, .{});
// cases.addLibcTestCase("math/acosh.c", true, .{});
cases.addLibcTestCase("math/acoshf.c", true, .{});
// cases.addLibcTestCase("math/acoshl.c", true, .{});