authorgravatar for xtex@astrafall.orgxtex <xtex@astrafall.org> 2025-03-02 09:04:56+08:00
committergravatar for xtex@astrafall.orgxtex <xtex@astrafall.org> 2025-03-02 10:30:43+08:00
log1da909a1e25a92bf077bfe84dc3c788419972a43
treebf67bb94a001533ec612c9861faff221c48ab8d3
parent4e32193de33df13a90499bc4b678b4b69716018c
signaturelock-open Commit is signed but in an unrecognized format.

x86_64: add behavior tests for saturating shift left

Co-authored-by: Jacob Young <jacobly0@users.noreply.github.com> Signed-off-by: Bingwu Zhang <xtex@aosc.io>

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

test/behavior/bit_shifting.zig+47
......@@ -1,5 +1,6 @@
11const std = @import("std");
22const expect = std.testing.expect;
3const expectEqual = std.testing.expectEqual;
34const builtin = @import("builtin");
45
56fn ShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, comptime V: type) type {
......@@ -158,3 +159,49 @@ comptime {
158159 _ = &image;
159160 _ = @shlExact(@as(u16, image[0]), 8);
160161}
162
163test "Saturating Shift Left" {
164 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
165 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
166 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
167 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
168 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
169 if (builtin.zig_backend == .stage2_x86_64 and builtin.target.ofmt != .elf and builtin.target.ofmt != .macho) return error.SkipZigTest;
170
171 const S = struct {
172 fn shlSat(x: anytype, y: std.math.Log2Int(@TypeOf(x))) @TypeOf(x) {
173 // workaround https://github.com/ziglang/zig/issues/23033
174 @setRuntimeSafety(false);
175 return x <<| y;
176 }
177
178 fn testType(comptime T: type) !void {
179 comptime var rhs: std.math.Log2Int(T) = 0;
180 inline while (true) : (rhs += 1) {
181 comptime var lhs: T = std.math.minInt(T);
182 inline while (true) : (lhs += 1) {
183 try expectEqual(lhs <<| rhs, shlSat(lhs, rhs));
184 if (lhs == std.math.maxInt(T)) break;
185 }
186 if (rhs == @bitSizeOf(T) - 1) break;
187 }
188 }
189 };
190
191 try S.testType(u2);
192 try S.testType(i2);
193 try S.testType(u3);
194 try S.testType(i3);
195 try S.testType(u4);
196 try S.testType(i4);
197
198 try expectEqual(0xfffffffffffffff0fffffffffffffff0, S.shlSat(@as(u128, 0x0fffffffffffffff0fffffffffffffff), 4));
199 try expectEqual(0xffffffffffffffffffffffffffffffff, S.shlSat(@as(u128, 0x0fffffffffffffff0fffffffffffffff), 5));
200 try expectEqual(-0x80000000000000000000000000000000, S.shlSat(@as(i128, -0x0fffffffffffffff0fffffffffffffff), 5));
201
202 // TODO
203 // try expectEqual(51146728248377216718956089012931236753385031969422887335676427626502090568823039920051095192592252455482604439493126109519019633529459266458258243583, S.shlSat(@as(i495, 0x2fe6bc5448c55ce18252e2c9d44777505dfe63ff249a8027a6626c7d8dd9893fd5731e51474727be556f757facb586a4e04bbc0148c6c7ad692302f46fbd), 0x31));
204 try expectEqual(-57896044618658097711785492504343953926634992332820282019728792003956564819968, S.shlSat(@as(i256, -0x53d4148cee74ea43477a65b3daa7b8fdadcbf4508e793f4af113b8d8da5a7eb6), 0x91));
205 try expectEqual(170141183460469231731687303715884105727, S.shlSat(@as(i128, 0x2fe6bc5448c55ce18252e2c9d4477750), 0x31));
206 try expectEqual(0, S.shlSat(@as(i128, 0), 127));
207}