Skip to content

Commit 144d69b

Browse files
committed
test: add comptime memoization tests for bit-for-bit float equality
1 parent 8eefc4c commit 144d69b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

test/behavior/floatop.zig

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,3 +1802,33 @@ test "optimized float mode" {
18021802
try expect(S.optimized(small) == small);
18031803
try expect(S.strict(small) == tiny);
18041804
}
1805+
1806+
fn MakeType(comptime x: anytype) type {
1807+
return struct {
1808+
fn get() @TypeOf(x) {
1809+
return x;
1810+
}
1811+
};
1812+
}
1813+
1814+
const nan_a: f32 = @bitCast(@as(u32, 0xffc00000));
1815+
const nan_b: f32 = @bitCast(@as(u32, 0xffe00000));
1816+
1817+
fn 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+
1823+
fn 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+
1831+
test "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+
}

0 commit comments

Comments
 (0)