| ... | @@ -288,10 +288,8 @@ pub fn shl(comptime T: type, a: T, shift_amt: var) T { | ... | @@ -288,10 +288,8 @@ pub fn shl(comptime T: type, a: T, shift_amt: var) T { |
| 288 | const abs_shift_amt = absCast(shift_amt); | 288 | const abs_shift_amt = absCast(shift_amt); |
| 289 | const casted_shift_amt = if (abs_shift_amt >= T.bit_count) return 0 else @intCast(Log2Int(T), abs_shift_amt); | 289 | const casted_shift_amt = if (abs_shift_amt >= T.bit_count) return 0 else @intCast(Log2Int(T), abs_shift_amt); |
| 290 | | 290 | |
| 291 | if (@typeOf(shift_amt).is_signed) { | 291 | if (@typeOf(shift_amt) == comptime_int or @typeOf(shift_amt).is_signed) { |
| 292 | if (shift_amt >= 0) { | 292 | if (shift_amt < 0) { |
| 293 | return a << casted_shift_amt; | | |
| 294 | } else { | | |
| 295 | return a >> casted_shift_amt; | 293 | return a >> casted_shift_amt; |
| 296 | } | 294 | } |
| 297 | } | 295 | } |
| ... | @@ -304,6 +302,10 @@ test "math.shl" { | ... | @@ -304,6 +302,10 @@ test "math.shl" { |
| 304 | testing.expect(shl(u8, 0b11111111, usize(8)) == 0); | 302 | testing.expect(shl(u8, 0b11111111, usize(8)) == 0); |
| 305 | testing.expect(shl(u8, 0b11111111, usize(9)) == 0); | 303 | testing.expect(shl(u8, 0b11111111, usize(9)) == 0); |
| 306 | testing.expect(shl(u8, 0b11111111, isize(-2)) == 0b00111111); | 304 | testing.expect(shl(u8, 0b11111111, isize(-2)) == 0b00111111); |
| | 305 | testing.expect(shl(u8, 0b11111111, 3) == 0b11111000); |
| | 306 | testing.expect(shl(u8, 0b11111111, 8) == 0); |
| | 307 | testing.expect(shl(u8, 0b11111111, 9) == 0); |
| | 308 | testing.expect(shl(u8, 0b11111111, -2) == 0b00111111); |
| 307 | } | 309 | } |
| 308 | | 310 | |
| 309 | /// Shifts right. Overflowed bits are truncated. | 311 | /// Shifts right. Overflowed bits are truncated. |
| ... | @@ -312,7 +314,7 @@ pub fn shr(comptime T: type, a: T, shift_amt: var) T { | ... | @@ -312,7 +314,7 @@ pub fn shr(comptime T: type, a: T, shift_amt: var) T { |
| 312 | const abs_shift_amt = absCast(shift_amt); | 314 | const abs_shift_amt = absCast(shift_amt); |
| 313 | const casted_shift_amt = if (abs_shift_amt >= T.bit_count) return 0 else @intCast(Log2Int(T), abs_shift_amt); | 315 | const casted_shift_amt = if (abs_shift_amt >= T.bit_count) return 0 else @intCast(Log2Int(T), abs_shift_amt); |
| 314 | | 316 | |
| 315 | if (@typeOf(shift_amt).is_signed) { | 317 | if (@typeOf(shift_amt) == comptime_int or @typeOf(shift_amt).is_signed) { |
| 316 | if (shift_amt >= 0) { | 318 | if (shift_amt >= 0) { |
| 317 | return a >> casted_shift_amt; | 319 | return a >> casted_shift_amt; |
| 318 | } else { | 320 | } else { |
| ... | @@ -328,6 +330,10 @@ test "math.shr" { | ... | @@ -328,6 +330,10 @@ test "math.shr" { |
| 328 | testing.expect(shr(u8, 0b11111111, usize(8)) == 0); | 330 | testing.expect(shr(u8, 0b11111111, usize(8)) == 0); |
| 329 | testing.expect(shr(u8, 0b11111111, usize(9)) == 0); | 331 | testing.expect(shr(u8, 0b11111111, usize(9)) == 0); |
| 330 | testing.expect(shr(u8, 0b11111111, isize(-2)) == 0b11111100); | 332 | testing.expect(shr(u8, 0b11111111, isize(-2)) == 0b11111100); |
| | 333 | testing.expect(shr(u8, 0b11111111, 3) == 0b00011111); |
| | 334 | testing.expect(shr(u8, 0b11111111, 8) == 0); |
| | 335 | testing.expect(shr(u8, 0b11111111, 9) == 0); |
| | 336 | testing.expect(shr(u8, 0b11111111, -2) == 0b11111100); |
| 331 | } | 337 | } |
| 332 | | 338 | |
| 333 | /// Rotates right. Only unsigned values can be rotated. | 339 | /// Rotates right. Only unsigned values can be rotated. |