| ... | ... | @@ -352,7 +352,18 @@ pub fn shlExact(comptime T: type, a: T, shift_amt: Log2Int(T)) !T { |
| 352 | 352 | /// A negative shift amount results in a right shift. |
| 353 | 353 | pub fn shl(comptime T: type, a: T, shift_amt: anytype) T { |
| 354 | 354 | const abs_shift_amt = absCast(shift_amt); |
| 355 | | const casted_shift_amt = if (abs_shift_amt >= @typeInfo(T).Int.bits) return 0 else @intCast(Log2Int(T), abs_shift_amt); |
| 355 | |
| 356 | const casted_shift_amt = blk: { |
| 357 | if (@typeInfo(T) == .Vector) { |
| 358 | const C = @typeInfo(T).Vector.child; |
| 359 | const len = @typeInfo(T).Vector.len; |
| 360 | if (abs_shift_amt >= @typeInfo(C).Int.bits) return @splat(len, @as(C, 0)); |
| 361 | break :blk @splat(len, @intCast(Log2Int(C), abs_shift_amt)); |
| 362 | } else { |
| 363 | if (abs_shift_amt >= @typeInfo(T).Int.bits) return 0; |
| 364 | break :blk @intCast(Log2Int(T), abs_shift_amt); |
| 365 | } |
| 366 | }; |
| 356 | 367 | |
| 357 | 368 | if (@TypeOf(shift_amt) == comptime_int or @typeInfo(@TypeOf(shift_amt)).Int.is_signed) { |
| 358 | 369 | if (shift_amt < 0) { |
| ... | ... | @@ -372,18 +383,30 @@ test "math.shl" { |
| 372 | 383 | testing.expect(shl(u8, 0b11111111, 8) == 0); |
| 373 | 384 | testing.expect(shl(u8, 0b11111111, 9) == 0); |
| 374 | 385 | testing.expect(shl(u8, 0b11111111, -2) == 0b00111111); |
| 386 | testing.expect(shl(std.meta.Vector(1, u32), std.meta.Vector(1, u32){42}, @as(usize, 1))[0] == @as(u32, 42) << 1); |
| 387 | testing.expect(shl(std.meta.Vector(1, u32), std.meta.Vector(1, u32){42}, @as(isize, -1))[0] == @as(u32, 42) >> 1); |
| 388 | testing.expect(shl(std.meta.Vector(1, u32), std.meta.Vector(1, u32){42}, 33)[0] == 0); |
| 375 | 389 | } |
| 376 | 390 | |
| 377 | 391 | /// Shifts right. Overflowed bits are truncated. |
| 378 | 392 | /// A negative shift amount results in a left shift. |
| 379 | 393 | pub fn shr(comptime T: type, a: T, shift_amt: anytype) T { |
| 380 | 394 | const abs_shift_amt = absCast(shift_amt); |
| 381 | | const casted_shift_amt = if (abs_shift_amt >= @typeInfo(T).Int.bits) return 0 else @intCast(Log2Int(T), abs_shift_amt); |
| 382 | 395 | |
| 383 | | if (@TypeOf(shift_amt) == comptime_int or @typeInfo(@TypeOf(shift_amt)).Int.is_signed) { |
| 384 | | if (shift_amt >= 0) { |
| 385 | | return a >> casted_shift_amt; |
| 396 | const casted_shift_amt = blk: { |
| 397 | if (@typeInfo(T) == .Vector) { |
| 398 | const C = @typeInfo(T).Vector.child; |
| 399 | const len = @typeInfo(T).Vector.len; |
| 400 | if (abs_shift_amt >= @typeInfo(C).Int.bits) return @splat(len, @as(C, 0)); |
| 401 | break :blk @splat(len, @intCast(Log2Int(C), abs_shift_amt)); |
| 386 | 402 | } else { |
| 403 | if (abs_shift_amt >= @typeInfo(T).Int.bits) return 0; |
| 404 | break :blk @intCast(Log2Int(T), abs_shift_amt); |
| 405 | } |
| 406 | }; |
| 407 | |
| 408 | if (@TypeOf(shift_amt) == comptime_int or @typeInfo(@TypeOf(shift_amt)).Int.is_signed) { |
| 409 | if (shift_amt < 0) { |
| 387 | 410 | return a << casted_shift_amt; |
| 388 | 411 | } |
| 389 | 412 | } |
| ... | ... | @@ -400,6 +423,9 @@ test "math.shr" { |
| 400 | 423 | testing.expect(shr(u8, 0b11111111, 8) == 0); |
| 401 | 424 | testing.expect(shr(u8, 0b11111111, 9) == 0); |
| 402 | 425 | testing.expect(shr(u8, 0b11111111, -2) == 0b11111100); |
| 426 | testing.expect(shr(std.meta.Vector(1, u32), std.meta.Vector(1, u32){42}, @as(usize, 1))[0] == @as(u32, 42) >> 1); |
| 427 | testing.expect(shr(std.meta.Vector(1, u32), std.meta.Vector(1, u32){42}, @as(isize, -1))[0] == @as(u32, 42) << 1); |
| 428 | testing.expect(shr(std.meta.Vector(1, u32), std.meta.Vector(1, u32){42}, 33)[0] == 0); |
| 403 | 429 | } |
| 404 | 430 | |
| 405 | 431 | /// Rotates right. Only unsigned values can be rotated. |