1const std = @import("std");
2const expect = std.testing.expect;
3const maxInt = std.math.maxInt;
4const minInt = std.math.minInt;
5const builtin = @import("builtin");
6
7test "uint128" {
8 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
9 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
10 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
11
12 var buff: u128 = maxInt(u128);
13 try expect(buff == maxInt(u128));
14
15 const magic_const = 0x12341234123412341234123412341234;
16 buff = magic_const;
17
18 try expect(buff == magic_const);
19 try expect(magic_const == 0x12341234123412341234123412341234);
20
21 buff = 0;
22 try expect(buff == @as(u128, 0));
23}
24
25test "undefined 128 bit int" {
26 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
27 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
28 if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
29 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
30
31 @setRuntimeSafety(true);
32
33 // TODO implement @setRuntimeSafety
34 if (builtin.mode != .debug and builtin.mode != .safe) {
35 return error.SkipZigTest;
36 }
37
38 var undef: u128 = undefined;
39 var undef_signed: i128 = undefined;
40 _ = .{ &undef, &undef_signed };
41 try expect(undef == 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa and @as(u128, @bitCast(undef_signed)) == undef);
42}
43
44test "int128" {
45 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
46 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
47 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
48
49 var buff: i128 = -1;
50 try expect(buff < 0 and (buff + 1) == 0);
51 try expect(@as(i8, @intCast(buff)) == @as(i8, -1));
52
53 buff = minInt(i128);
54 try expect(buff < 0);
55
56 buff = -0x12341234123412341234123412341234;
57 try expect(-buff == 0x12341234123412341234123412341234);
58
59 const a: i128 = -170141183460469231731687303715884105728;
60 const b: i128 = -0x8000_0000_0000_0000_0000_0000_0000_0000;
61 try expect(@divFloor(b, 1_000_000) == -170141183460469231731687303715885);
62 try expect(@divCeil(b, 1_000_000) == -170141183460469231731687303715884);
63 try expect(a == b);
64}
65
66test "truncate int128" {
67 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
68 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
69 if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
70 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
71
72 {
73 var buff: u128 = maxInt(u128);
74 _ = &buff;
75 try expect(@as(u64, @truncate(buff)) == maxInt(u64));
76 try expect(@as(u90, @truncate(buff)) == maxInt(u90));
77 try expect(@as(u128, @truncate(buff)) == maxInt(u128));
78 }
79
80 {
81 var buff: i128 = maxInt(i128);
82 _ = &buff;
83 try expect(@as(i64, @truncate(buff)) == -1);
84 try expect(@as(i90, @truncate(buff)) == -1);
85 try expect(@as(i128, @truncate(buff)) == maxInt(i128));
86 }
87}
88
89test "shift int128" {
90 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
91 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
92 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
93
94 const types = .{ u128, i128 };
95 inline for (types) |t| {
96 try testShlTrunc(t, 0x8, 123);
97 try comptime testShlTrunc(t, 0x8, 123);
98
99 try testShlTrunc(t, 0x40000000_00000000, 64);
100 try comptime testShlTrunc(t, 0x40000000_00000000, 64);
101
102 try testShlTrunc(t, 0x01000000_00000000_00000000, 38);
103 try comptime testShlTrunc(t, 0x01000000_00000000_00000000, 38);
104
105 try testShlTrunc(t, 0x00000008_00000000_00000000_00000000, 27);
106 try comptime testShlTrunc(t, 0x00000008_00000000_00000000_00000000, 27);
107 }
108}
109
110fn testShlTrunc(comptime Type: type, x: Type, rhs: u7) !void {
111 const shifted = x << rhs;
112 try expect(shifted == @as(Type, 0x40000000_00000000_00000000_00000000));
113}