aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/math.zig
diff options
context:
space:
mode:
authormlugg <mlugg@mlugg.co.uk>2024-09-17 11:00:38 +0100
committermlugg <mlugg@mlugg.co.uk>2024-09-17 11:00:38 +0100
commit4650e5b9fcaa74b724a51458f5cf8952f3c734de (patch)
tree3d3b16bb86bff91c115f6778102995ac1d0e40a7 /test/behavior/math.zig
parenta5c922179f99591d20e5b6b203c7e292692e0c28 (diff)
downloadzig-4650e5b9fcaa74b724a51458f5cf8952f3c734de.tar.gz
zig-4650e5b9fcaa74b724a51458f5cf8952f3c734de.zip
Sema: clean up cmpNumeric
There is one minor language change here, which is that comparisons of the form `comptime_inf < runtime_f32` have their results comptime-known. This is consistent with comparisons against comptime NaN for instance, which are always comptime known. A corresponding behavior test is added. This fixes a bug with int comparison elision which my previous commit somehow triggered. `Sema.compareIntsOnlyPossibleResult` is much cleaner now!
Diffstat (limited to 'test/behavior/math.zig')
-rw-r--r--test/behavior/math.zig62
1 files changed, 62 insertions, 0 deletions
diff --git a/test/behavior/math.zig b/test/behavior/math.zig
index 5ee07b9e98..bbf87fc834 100644
--- a/test/behavior/math.zig
+++ b/test/behavior/math.zig
@@ -1729,3 +1729,65 @@ test "@clz works on both vector and scalar inputs" {
try std.testing.expectEqual(@as(u6, 31), a);
try std.testing.expectEqual([_]u6{ 31, 31, 31, 31 }, b);
}
+
+test "runtime comparison to NaN is comptime-known" {
+ if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_x86_64 and builtin.target.ofmt != .elf and builtin.target.ofmt != .macho) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
+ if (builtin.cpu.arch.isArmOrThumb() and builtin.target.floatAbi() == .soft) return error.SkipZigTest; // https://github.com/ziglang/zig/issues/21234
+
+ const S = struct {
+ fn doTheTest(comptime F: type, x: F) void {
+ const nan = math.nan(F);
+ if (!(nan != x)) comptime unreachable;
+ if (nan == x) comptime unreachable;
+ if (nan > x) comptime unreachable;
+ if (nan < x) comptime unreachable;
+ if (nan >= x) comptime unreachable;
+ if (nan <= x) comptime unreachable;
+ }
+ };
+
+ S.doTheTest(f16, 123.0);
+ S.doTheTest(f32, 123.0);
+ S.doTheTest(f64, 123.0);
+ S.doTheTest(f128, 123.0);
+ comptime S.doTheTest(f16, 123.0);
+ comptime S.doTheTest(f32, 123.0);
+ comptime S.doTheTest(f64, 123.0);
+ comptime S.doTheTest(f128, 123.0);
+}
+
+test "runtime int comparison to inf is comptime-known" {
+ if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_x86_64 and builtin.target.ofmt != .elf and builtin.target.ofmt != .macho) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
+ if (builtin.cpu.arch.isArmOrThumb() and builtin.target.floatAbi() == .soft) return error.SkipZigTest; // https://github.com/ziglang/zig/issues/21234
+
+ const S = struct {
+ fn doTheTest(comptime F: type, x: u32) void {
+ const inf = math.inf(F);
+ if (!(inf != x)) comptime unreachable;
+ if (inf == x) comptime unreachable;
+ if (x > inf) comptime unreachable;
+ if (x >= inf) comptime unreachable;
+ if (!(x < inf)) comptime unreachable;
+ if (!(x <= inf)) comptime unreachable;
+ }
+ };
+
+ S.doTheTest(f16, 123);
+ S.doTheTest(f32, 123);
+ S.doTheTest(f64, 123);
+ S.doTheTest(f128, 123);
+ comptime S.doTheTest(f16, 123);
+ comptime S.doTheTest(f32, 123);
+ comptime S.doTheTest(f64, 123);
+ comptime S.doTheTest(f128, 123);
+}