| ... | ... | @@ -288,10 +288,8 @@ pub fn shl(comptime T: type, a: T, shift_amt: var) T { |
| 288 | 288 | const abs_shift_amt = absCast(shift_amt); |
| 289 | 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) { |
| 292 | | if (shift_amt >= 0) { |
| 293 | | return a << casted_shift_amt; |
| 294 | | } else { |
| 291 | if (@typeOf(shift_amt) == comptime_int or @typeOf(shift_amt).is_signed) { |
| 292 | if (shift_amt < 0) { |
| 295 | 293 | return a >> casted_shift_amt; |
| 296 | 294 | } |
| 297 | 295 | } |
| ... | ... | @@ -304,6 +302,10 @@ test "math.shl" { |
| 304 | 302 | testing.expect(shl(u8, 0b11111111, usize(8)) == 0); |
| 305 | 303 | testing.expect(shl(u8, 0b11111111, usize(9)) == 0); |
| 306 | 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 | 311 | /// Shifts right. Overflowed bits are truncated. |
| ... | ... | @@ -312,7 +314,7 @@ pub fn shr(comptime T: type, a: T, shift_amt: var) T { |
| 312 | 314 | const abs_shift_amt = absCast(shift_amt); |
| 313 | 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 | 318 | if (shift_amt >= 0) { |
| 317 | 319 | return a >> casted_shift_amt; |
| 318 | 320 | } else { |
| ... | ... | @@ -328,6 +330,10 @@ test "math.shr" { |
| 328 | 330 | testing.expect(shr(u8, 0b11111111, usize(8)) == 0); |
| 329 | 331 | testing.expect(shr(u8, 0b11111111, usize(9)) == 0); |
| 330 | 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 | 339 | /// Rotates right. Only unsigned values can be rotated. |