authorgravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2025-03-04 23:28:49-05:00
committergravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2025-03-04 23:31:12-05:00
log144d69b571601612f3ec84f038231e924db10ca1
treec46f203421fd375836d1290bc2d9957d94919d47
parent8eefc4c5c2553648422c6d6e4f35021baf2c9683

test: add comptime memoization tests for bit-for-bit float equality


1 files changed, 30 insertions(+), 0 deletions(-)

test/behavior/floatop.zig+30
...@@ -1802,3 +1802,33 @@ test "optimized float mode" {...@@ -1802,3 +1802,33 @@ test "optimized float mode" {
1802 try expect(S.optimized(small) == small);1802 try expect(S.optimized(small) == small);
1803 try expect(S.strict(small) == tiny);1803 try expect(S.strict(small) == tiny);
1804}1804}
1805
1806fn MakeType(comptime x: anytype) type {
1807 return struct {
1808 fn get() @TypeOf(x) {
1809 return x;
1810 }
1811 };
1812}
1813
1814const nan_a: f32 = @bitCast(@as(u32, 0xffc00000));
1815const nan_b: f32 = @bitCast(@as(u32, 0xffe00000));
1816
1817fn testMemoization() !void {
1818 try expect(MakeType(nan_a) == MakeType(nan_a));
1819 try expect(MakeType(nan_b) == MakeType(nan_b));
1820 try expect(MakeType(nan_a) != MakeType(nan_b));
1821}
1822
1823fn testVectorMemoization(comptime T: type) !void {
1824 const nan_a_v: T = @splat(nan_a);
1825 const nan_b_v: T = @splat(nan_b);
1826 try expect(MakeType(nan_a_v) == MakeType(nan_a_v));
1827 try expect(MakeType(nan_b_v) == MakeType(nan_b_v));
1828 try expect(MakeType(nan_a_v) != MakeType(nan_b_v));
1829}
1830
1831test "comptime calls are only memoized when float arguments are bit-for-bit equal" {
1832 try comptime testMemoization();
1833 try comptime testVectorMemoization(@Vector(4, f32));
1834}