commit f36d0573cd0f80b271b9f07a7215fa237d14ae9a (tree)
parent 5132d78e8385cbadc888275761258a1fca27b6f0
Author: kineye <27766253+JanikkinaJ@users.noreply.github.com.>
Date: Sat, 7 Feb 2026 16:09:29 +0100
zig libc: add rint
Diffstat:
4 files changed, 57 insertions(+), 47 deletions(-)
diff --git 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
@@ -1,28 +0,0 @@
-#include <float.h>
-#include <math.h>
-#include <stdint.h>
-
-#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
@@ -1,15 +0,0 @@
-#include <math.h>
-
-#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
@@ -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",