authorgravatar for jeamsblue@gmail.comKoki Ueha <jeamsblue@gmail.com> 2025-06-18 14:15:51+00:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-06-20 10:17:24+02:00
log9e340b3005975e87563566a223d2f98e070f12cb
tree24bd9ac8339ebeafdb8ec79a37cf883e660bde8b
parent14ad8378a19dbbf84e0be491292019356b9e8752

This commit enables fmax and fmin to differentiate between 0.0 and

-0.0, making it compatible with musl.

2 files changed, 12 insertions(+), 0 deletions(-)

lib/compiler_rt/fmax.zig+6
......@@ -54,12 +54,15 @@ inline fn generic_fmax(comptime T: type, x: T, y: T) T {
5454 return y;
5555 if (math.isNan(y))
5656 return x;
57 if (math.signbit(x) != math.signbit(y))
58 return if (math.signbit(x)) y else x;
5759 return if (x < y) y else x;
5860}
5961
6062test "generic_fmax" {
6163 inline for ([_]type{ f32, f64, c_longdouble, f80, f128 }) |T| {
6264 const nan_val = math.nan(T);
65 const Int = std.meta.Int(.unsigned, @bitSizeOf(T));
6366
6467 try std.testing.expect(math.isNan(generic_fmax(T, nan_val, nan_val)));
6568 try std.testing.expectEqual(@as(T, 1.0), generic_fmax(T, nan_val, 1.0));
......@@ -67,5 +70,8 @@ test "generic_fmax" {
6770
6871 try std.testing.expectEqual(@as(T, 10.0), generic_fmax(T, 1.0, 10.0));
6972 try std.testing.expectEqual(@as(T, 1.0), generic_fmax(T, 1.0, -1.0));
73
74 try std.testing.expectEqual(@as(Int, @bitCast(@as(T, 0.0))), @as(Int, @bitCast(generic_fmax(T, 0.0, -0.0))));
75 try std.testing.expectEqual(@as(Int, @bitCast(@as(T, 0.0))), @as(Int, @bitCast(generic_fmax(T, -0.0, 0.0))));
7076 }
7177}
lib/compiler_rt/fmin.zig+6
......@@ -54,12 +54,15 @@ inline fn generic_fmin(comptime T: type, x: T, y: T) T {
5454 return y;
5555 if (math.isNan(y))
5656 return x;
57 if (math.signbit(x) != math.signbit(y))
58 return if (math.signbit(x)) x else y;
5759 return if (x < y) x else y;
5860}
5961
6062test "generic_fmin" {
6163 inline for ([_]type{ f32, f64, c_longdouble, f80, f128 }) |T| {
6264 const nan_val = math.nan(T);
65 const Int = std.meta.Int(.unsigned, @bitSizeOf(T));
6366
6467 try std.testing.expect(math.isNan(generic_fmin(T, nan_val, nan_val)));
6568 try std.testing.expectEqual(@as(T, 1.0), generic_fmin(T, nan_val, 1.0));
......@@ -67,5 +70,8 @@ test "generic_fmin" {
6770
6871 try std.testing.expectEqual(@as(T, 1.0), generic_fmin(T, 1.0, 10.0));
6972 try std.testing.expectEqual(@as(T, -1.0), generic_fmin(T, 1.0, -1.0));
73
74 try std.testing.expectEqual(@as(Int, @bitCast(@as(T, -0.0))), @as(Int, @bitCast(generic_fmin(T, 0.0, -0.0))));
75 try std.testing.expectEqual(@as(Int, @bitCast(@as(T, -0.0))), @as(Int, @bitCast(generic_fmin(T, -0.0, 0.0))));
7076 }
7177}