authorgravatar for stefansu28@gmail.comStefan Su <stefansu28@gmail.com> 2023-12-22 09:51:41-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-12-22 14:51:41+00:00
log42ddf592dd610dda3371cae2eba63ac3e8502c64
treea7605d9ac2c122635dc1fa2e00549d758ba14071
parentfd98fc1c5f83224cf1f128df7ba1cd24e4d40808
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

use `casted_rhs` instead of `rhs` so `icmp` works correctly for `airShlSat`


2 files changed, 44 insertions(+), 1 deletions(-)

src/codegen/llvm.zig+1-1
......@@ -8433,7 +8433,7 @@ pub const FuncGen = struct {
84338433 llvm_lhs_ty,
84348434 try o.builder.intConst(llvm_lhs_scalar_ty, -1),
84358435 );
8436 const in_range = try self.wip.icmp(.ult, rhs, bits, "");
8436 const in_range = try self.wip.icmp(.ult, casted_rhs, bits, "");
84378437 return self.wip.select(.normal, in_range, result, lhs_max, "");
84388438 }
84398439
test/behavior/bit_shifting.zig+43
......@@ -109,3 +109,46 @@ test "comptime shr of BigInt" {
109109test "comptime shift safety check" {
110110 _ = @as(usize, 42) << @sizeOf(usize);
111111}
112
113test "Saturating Shift Left where lhs is of a computed type" {
114 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
115 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
116 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
117 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
118 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
119
120 const S = struct {
121 fn getIntShiftType(comptime T: type) type {
122 var unsigned_shift_type = @typeInfo(std.math.Log2Int(T)).Int;
123 unsigned_shift_type.signedness = .signed;
124
125 return @Type(.{
126 .Int = unsigned_shift_type,
127 });
128 }
129
130 pub fn FixedPoint(comptime value_type: type) type {
131 return struct {
132 value: value_type,
133 exponent: ShiftType,
134
135 const ShiftType: type = getIntShiftType(value_type);
136
137 pub fn shiftExponent(self: @This(), shift: ShiftType) @This() {
138 const shiftAbs = @abs(shift);
139 return .{ .value = if (shift >= 0) self.value >> shiftAbs else self.value <<| shiftAbs, .exponent = self.exponent + shift };
140 }
141 };
142 }
143 };
144
145 const FP = S.FixedPoint(i32);
146
147 const value = (FP{
148 .value = 1,
149 .exponent = 1,
150 }).shiftExponent(-1);
151
152 try expect(value.value == 2);
153 try expect(value.exponent == 0);
154}