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 {...@@ -54,12 +54,15 @@ inline fn generic_fmax(comptime T: type, x: T, y: T) T {
54 return y;54 return y;
55 if (math.isNan(y))55 if (math.isNan(y))
56 return x;56 return x;
57 if (math.signbit(x) != math.signbit(y))
58 return if (math.signbit(x)) y else x;
57 return if (x < y) y else x;59 return if (x < y) y else x;
58}60}
5961
60test "generic_fmax" {62test "generic_fmax" {
61 inline for ([_]type{ f32, f64, c_longdouble, f80, f128 }) |T| {63 inline for ([_]type{ f32, f64, c_longdouble, f80, f128 }) |T| {
62 const nan_val = math.nan(T);64 const nan_val = math.nan(T);
65 const Int = std.meta.Int(.unsigned, @bitSizeOf(T));
6366
64 try std.testing.expect(math.isNan(generic_fmax(T, nan_val, nan_val)));67 try std.testing.expect(math.isNan(generic_fmax(T, nan_val, nan_val)));
65 try std.testing.expectEqual(@as(T, 1.0), generic_fmax(T, nan_val, 1.0));68 try std.testing.expectEqual(@as(T, 1.0), generic_fmax(T, nan_val, 1.0));
...@@ -67,5 +70,8 @@ test "generic_fmax" {...@@ -67,5 +70,8 @@ test "generic_fmax" {
6770
68 try std.testing.expectEqual(@as(T, 10.0), generic_fmax(T, 1.0, 10.0));71 try std.testing.expectEqual(@as(T, 10.0), generic_fmax(T, 1.0, 10.0));
69 try std.testing.expectEqual(@as(T, 1.0), generic_fmax(T, 1.0, -1.0));72 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))));
70 }76 }
71}77}
lib/compiler_rt/fmin.zig+6
...@@ -54,12 +54,15 @@ inline fn generic_fmin(comptime T: type, x: T, y: T) T {...@@ -54,12 +54,15 @@ inline fn generic_fmin(comptime T: type, x: T, y: T) T {
54 return y;54 return y;
55 if (math.isNan(y))55 if (math.isNan(y))
56 return x;56 return x;
57 if (math.signbit(x) != math.signbit(y))
58 return if (math.signbit(x)) x else y;
57 return if (x < y) x else y;59 return if (x < y) x else y;
58}60}
5961
60test "generic_fmin" {62test "generic_fmin" {
61 inline for ([_]type{ f32, f64, c_longdouble, f80, f128 }) |T| {63 inline for ([_]type{ f32, f64, c_longdouble, f80, f128 }) |T| {
62 const nan_val = math.nan(T);64 const nan_val = math.nan(T);
65 const Int = std.meta.Int(.unsigned, @bitSizeOf(T));
6366
64 try std.testing.expect(math.isNan(generic_fmin(T, nan_val, nan_val)));67 try std.testing.expect(math.isNan(generic_fmin(T, nan_val, nan_val)));
65 try std.testing.expectEqual(@as(T, 1.0), generic_fmin(T, nan_val, 1.0));68 try std.testing.expectEqual(@as(T, 1.0), generic_fmin(T, nan_val, 1.0));
...@@ -67,5 +70,8 @@ test "generic_fmin" {...@@ -67,5 +70,8 @@ test "generic_fmin" {
6770
68 try std.testing.expectEqual(@as(T, 1.0), generic_fmin(T, 1.0, 10.0));71 try std.testing.expectEqual(@as(T, 1.0), generic_fmin(T, 1.0, 10.0));
69 try std.testing.expectEqual(@as(T, -1.0), generic_fmin(T, 1.0, -1.0));72 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))));
70 }76 }
71}77}