| ... | @@ -1,5 +1,6 @@ | ... | @@ -1,5 +1,6 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const expect = std.testing.expect; | 2 | const expect = std.testing.expect; |
| | 3 | const expectEqual = std.testing.expectEqual; |
| 3 | const builtin = @import("builtin"); | 4 | const builtin = @import("builtin"); |
| 4 | | 5 | |
| 5 | fn ShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, comptime V: type) type { | 6 | fn ShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, comptime V: type) type { |
| ... | @@ -158,3 +159,49 @@ comptime { | ... | @@ -158,3 +159,49 @@ comptime { |
| 158 | _ = ℑ | 159 | _ = ℑ |
| 159 | _ = @shlExact(@as(u16, image[0]), 8); | 160 | _ = @shlExact(@as(u16, image[0]), 8); |
| 160 | } | 161 | } |
| | 162 | |
| | 163 | test "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 | } |