From f36d0573cd0f80b271b9f07a7215fa237d14ae9a Mon Sep 17 00:00:00 2001 From: kineye <27766253+JanikkinaJ@users.noreply.github.com.> Date: Sat, 7 Feb 2026 16:09:29 +0100 Subject: [PATCH] zig libc: add rint --- lib/c/math.zig | 57 +++++++++++++++++++++++++++++ lib/libc/musl/src/math/rint.c | 28 -------------- lib/libc/musl/src/math/s390x/rint.c | 15 -------- src/libs/musl.zig | 4 -- 4 files changed, 57 insertions(+), 47 deletions(-) delete mode 100644 lib/libc/musl/src/math/rint.c delete mode 100644 lib/libc/musl/src/math/s390x/rint.c diff --git a/lib/c/math.zig b/lib/c/math.zig index 85b871c695ef5c4233ba9c6375d41f6347605687..2c02597bb2271bd9a83cf6f4cf35b8cd8afdf9fc 100644 --- a/lib/c/math.zig +++ b/lib/c/math.zig @@ -56,6 +56,7 @@ comptime { if (builtin.target.isMuslLibC()) { symbol(©signf, "copysignf"); symbol(©sign, "copysign"); + symbol(&rint, "rint"); } symbol(©signl, "copysignl"); } @@ -158,3 +159,59 @@ fn pow10(x: f64) callconv(.c) f64 { fn pow10f(x: f32) callconv(.c) f32 { return exp10f(x); } + +fn rint(x: f64) callconv(.c) f64 { + const toint: f64 = 1.0 / @as(f64, std.math.floatEps(f64)); + const a: u64 = @bitCast(x); + const e = a >> 52 & 0x7ff; + const s = a >> 63; + var y: f64 = undefined; + + if (e >= 0x3ff + 52) { + return x; + } + if (s == 1) { + y = x - toint + toint; + } else { + y = x + toint - toint; + } + if (y == 0) { + return if (s == 1) -0.0 else 0; + } + return y; +} + +test "rint" { + // Positive numbers round correctly + try std.testing.expectEqual(@as(f64, 42.0), rint(42.2)); + try std.testing.expectEqual(@as(f64, 42.0), rint(41.8)); + + // Negative numbers round correctly + try std.testing.expectEqual(@as(f64, -6.0), rint(-5.9)); + try std.testing.expectEqual(@as(f64, -6.0), rint(-6.1)); + + // No rounding needed test + try std.testing.expectEqual(@as(f64, 5.0), rint(5.0)); + try std.testing.expectEqual(@as(f64, -10.0), rint(-10.0)); + try std.testing.expectEqual(@as(f64, 0.0), rint(0.0)); + + // Very large numbers return unchanged + const large: f64 = 9007199254740992.0; // 2^53 + try std.testing.expectEqual(large, rint(large)); + try std.testing.expectEqual(-large, rint(-large)); + + // Small positive numbers round to zero + const pos_result = rint(0.3); + try std.testing.expectEqual(@as(f64, 0.0), pos_result); + try std.testing.expect(@as(u64, @bitCast(pos_result)) == 0); + + // Small negative numbers round to negative zero + const neg_result = rint(-0.3); + try std.testing.expectEqual(@as(f64, 0.0), neg_result); + const bits: u64 = @bitCast(neg_result); + try std.testing.expect((bits >> 63) == 1); + + // Exact half rounds to nearest even (banker's rounding) + try std.testing.expectEqual(@as(f64, 2.0), rint(2.5)); + try std.testing.expectEqual(@as(f64, 4.0), rint(3.5)); +} diff --git a/lib/libc/musl/src/math/rint.c b/lib/libc/musl/src/math/rint.c deleted file mode 100644 index fbba390e7d723774d41ba2a9da74957c20643d63..0000000000000000000000000000000000000000 --- a/lib/libc/musl/src/math/rint.c +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include -#include - -#if FLT_EVAL_METHOD==0 || FLT_EVAL_METHOD==1 -#define EPS DBL_EPSILON -#elif FLT_EVAL_METHOD==2 -#define EPS LDBL_EPSILON -#endif -static const double_t toint = 1/EPS; - -double rint(double x) -{ - union {double f; uint64_t i;} u = {x}; - int e = u.i>>52 & 0x7ff; - int s = u.i>>63; - double_t y; - - if (e >= 0x3ff+52) - return x; - if (s) - y = x - toint + toint; - else - y = x + toint - toint; - if (y == 0) - return s ? -0.0 : 0; - return y; -} diff --git a/lib/libc/musl/src/math/s390x/rint.c b/lib/libc/musl/src/math/s390x/rint.c deleted file mode 100644 index b02f09eb0645ab577fe14471d76bc27f205823ba..0000000000000000000000000000000000000000 --- a/lib/libc/musl/src/math/s390x/rint.c +++ /dev/null @@ -1,15 +0,0 @@ -#include - -#if defined(__HTM__) || __ARCH__ >= 9 - -double rint(double x) -{ - __asm__ ("fidbr %0, 0, %1" : "=f"(x) : "f"(x)); - return x; -} - -#else - -#include "../rint.c" - -#endif diff --git a/src/libs/musl.zig b/src/libs/musl.zig index b201c1b7ff32e0ad63d15729618959f3fa543140..3f2739ec714d9d879bf73b340f55ac2b4d62e055 100644 --- a/src/libs/musl.zig +++ b/src/libs/musl.zig @@ -797,7 +797,6 @@ const src_files = [_][]const u8{ "musl/src/math/aarch64/lroundf.c", "musl/src/math/aarch64/nearbyint.c", "musl/src/math/aarch64/nearbyintf.c", - "musl/src/math/aarch64/rint.c", "musl/src/math/aarch64/rintf.c", "musl/src/math/acosf.c", "musl/src/math/acosh.c", @@ -887,7 +886,6 @@ const src_files = [_][]const u8{ "musl/src/math/i386/remquof.s", "musl/src/math/i386/remquol.s", "musl/src/math/i386/remquo.s", - "musl/src/math/i386/rint.c", "musl/src/math/i386/rintf.c", "musl/src/math/i386/rintl.c", "musl/src/math/i386/scalblnf.s", @@ -981,7 +979,6 @@ const src_files = [_][]const u8{ "musl/src/math/remquo.c", "musl/src/math/remquof.c", "musl/src/math/remquol.c", - "musl/src/math/rint.c", "musl/src/math/rintf.c", "musl/src/math/rintl.c", "musl/src/math/riscv32/fma.c", @@ -993,7 +990,6 @@ const src_files = [_][]const u8{ "musl/src/math/s390x/nearbyint.c", "musl/src/math/s390x/nearbyintf.c", "musl/src/math/s390x/nearbyintl.c", - "musl/src/math/s390x/rint.c", "musl/src/math/s390x/rintf.c", "musl/src/math/s390x/rintl.c", "musl/src/math/scalb.c", -- 2.54.0