mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
math.shl/math.shr: add support for vectors
This commit is contained in:
parent
73aef46f7d
commit
2e354c387e
2 changed files with 35 additions and 9 deletions
|
|
@ -261,7 +261,7 @@ test "blake2s224 streaming" {
|
||||||
|
|
||||||
test "comptime blake2s224" {
|
test "comptime blake2s224" {
|
||||||
comptime {
|
comptime {
|
||||||
@setEvalBranchQuota(6000);
|
@setEvalBranchQuota(10000);
|
||||||
var block = [_]u8{0} ** Blake2s224.block_length;
|
var block = [_]u8{0} ** Blake2s224.block_length;
|
||||||
var out: [Blake2s224.digest_length]u8 = undefined;
|
var out: [Blake2s224.digest_length]u8 = undefined;
|
||||||
|
|
||||||
|
|
@ -353,7 +353,7 @@ test "blake2s256 keyed" {
|
||||||
|
|
||||||
test "comptime blake2s256" {
|
test "comptime blake2s256" {
|
||||||
comptime {
|
comptime {
|
||||||
@setEvalBranchQuota(6000);
|
@setEvalBranchQuota(10000);
|
||||||
var block = [_]u8{0} ** Blake2s256.block_length;
|
var block = [_]u8{0} ** Blake2s256.block_length;
|
||||||
var out: [Blake2s256.digest_length]u8 = undefined;
|
var out: [Blake2s256.digest_length]u8 = undefined;
|
||||||
|
|
||||||
|
|
@ -611,7 +611,7 @@ test "blake2b384 streaming" {
|
||||||
|
|
||||||
test "comptime blake2b384" {
|
test "comptime blake2b384" {
|
||||||
comptime {
|
comptime {
|
||||||
@setEvalBranchQuota(7000);
|
@setEvalBranchQuota(10000);
|
||||||
var block = [_]u8{0} ** Blake2b384.block_length;
|
var block = [_]u8{0} ** Blake2b384.block_length;
|
||||||
var out: [Blake2b384.digest_length]u8 = undefined;
|
var out: [Blake2b384.digest_length]u8 = undefined;
|
||||||
|
|
||||||
|
|
@ -703,7 +703,7 @@ test "blake2b512 keyed" {
|
||||||
|
|
||||||
test "comptime blake2b512" {
|
test "comptime blake2b512" {
|
||||||
comptime {
|
comptime {
|
||||||
@setEvalBranchQuota(8000);
|
@setEvalBranchQuota(10000);
|
||||||
var block = [_]u8{0} ** Blake2b512.block_length;
|
var block = [_]u8{0} ** Blake2b512.block_length;
|
||||||
var out: [Blake2b512.digest_length]u8 = undefined;
|
var out: [Blake2b512.digest_length]u8 = undefined;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -352,7 +352,18 @@ pub fn shlExact(comptime T: type, a: T, shift_amt: Log2Int(T)) !T {
|
||||||
/// A negative shift amount results in a right shift.
|
/// A negative shift amount results in a right shift.
|
||||||
pub fn shl(comptime T: type, a: T, shift_amt: anytype) T {
|
pub fn shl(comptime T: type, a: T, shift_amt: anytype) T {
|
||||||
const abs_shift_amt = absCast(shift_amt);
|
const abs_shift_amt = absCast(shift_amt);
|
||||||
const casted_shift_amt = if (abs_shift_amt >= @typeInfo(T).Int.bits) return 0 else @intCast(Log2Int(T), abs_shift_amt);
|
|
||||||
|
const casted_shift_amt = blk: {
|
||||||
|
if (@typeInfo(T) == .Vector) {
|
||||||
|
const C = @typeInfo(T).Vector.child;
|
||||||
|
const len = @typeInfo(T).Vector.len;
|
||||||
|
if (abs_shift_amt >= @typeInfo(C).Int.bits) return @splat(len, @as(C, 0));
|
||||||
|
break :blk @splat(len, @intCast(Log2Int(C), abs_shift_amt));
|
||||||
|
} else {
|
||||||
|
if (abs_shift_amt >= @typeInfo(T).Int.bits) return 0;
|
||||||
|
break :blk @intCast(Log2Int(T), abs_shift_amt);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if (@TypeOf(shift_amt) == comptime_int or @typeInfo(@TypeOf(shift_amt)).Int.is_signed) {
|
if (@TypeOf(shift_amt) == comptime_int or @typeInfo(@TypeOf(shift_amt)).Int.is_signed) {
|
||||||
if (shift_amt < 0) {
|
if (shift_amt < 0) {
|
||||||
|
|
@ -372,18 +383,30 @@ test "math.shl" {
|
||||||
testing.expect(shl(u8, 0b11111111, 8) == 0);
|
testing.expect(shl(u8, 0b11111111, 8) == 0);
|
||||||
testing.expect(shl(u8, 0b11111111, 9) == 0);
|
testing.expect(shl(u8, 0b11111111, 9) == 0);
|
||||||
testing.expect(shl(u8, 0b11111111, -2) == 0b00111111);
|
testing.expect(shl(u8, 0b11111111, -2) == 0b00111111);
|
||||||
|
testing.expect(shl(std.meta.Vector(1, u32), std.meta.Vector(1, u32){42}, @as(usize, 1))[0] == @as(u32, 42) << 1);
|
||||||
|
testing.expect(shl(std.meta.Vector(1, u32), std.meta.Vector(1, u32){42}, @as(isize, -1))[0] == @as(u32, 42) >> 1);
|
||||||
|
testing.expect(shl(std.meta.Vector(1, u32), std.meta.Vector(1, u32){42}, 33)[0] == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Shifts right. Overflowed bits are truncated.
|
/// Shifts right. Overflowed bits are truncated.
|
||||||
/// A negative shift amount results in a left shift.
|
/// A negative shift amount results in a left shift.
|
||||||
pub fn shr(comptime T: type, a: T, shift_amt: anytype) T {
|
pub fn shr(comptime T: type, a: T, shift_amt: anytype) T {
|
||||||
const abs_shift_amt = absCast(shift_amt);
|
const abs_shift_amt = absCast(shift_amt);
|
||||||
const casted_shift_amt = if (abs_shift_amt >= @typeInfo(T).Int.bits) return 0 else @intCast(Log2Int(T), abs_shift_amt);
|
|
||||||
|
const casted_shift_amt = blk: {
|
||||||
|
if (@typeInfo(T) == .Vector) {
|
||||||
|
const C = @typeInfo(T).Vector.child;
|
||||||
|
const len = @typeInfo(T).Vector.len;
|
||||||
|
if (abs_shift_amt >= @typeInfo(C).Int.bits) return @splat(len, @as(C, 0));
|
||||||
|
break :blk @splat(len, @intCast(Log2Int(C), abs_shift_amt));
|
||||||
|
} else {
|
||||||
|
if (abs_shift_amt >= @typeInfo(T).Int.bits) return 0;
|
||||||
|
break :blk @intCast(Log2Int(T), abs_shift_amt);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if (@TypeOf(shift_amt) == comptime_int or @typeInfo(@TypeOf(shift_amt)).Int.is_signed) {
|
if (@TypeOf(shift_amt) == comptime_int or @typeInfo(@TypeOf(shift_amt)).Int.is_signed) {
|
||||||
if (shift_amt >= 0) {
|
if (shift_amt < 0) {
|
||||||
return a >> casted_shift_amt;
|
|
||||||
} else {
|
|
||||||
return a << casted_shift_amt;
|
return a << casted_shift_amt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -400,6 +423,9 @@ test "math.shr" {
|
||||||
testing.expect(shr(u8, 0b11111111, 8) == 0);
|
testing.expect(shr(u8, 0b11111111, 8) == 0);
|
||||||
testing.expect(shr(u8, 0b11111111, 9) == 0);
|
testing.expect(shr(u8, 0b11111111, 9) == 0);
|
||||||
testing.expect(shr(u8, 0b11111111, -2) == 0b11111100);
|
testing.expect(shr(u8, 0b11111111, -2) == 0b11111100);
|
||||||
|
testing.expect(shr(std.meta.Vector(1, u32), std.meta.Vector(1, u32){42}, @as(usize, 1))[0] == @as(u32, 42) >> 1);
|
||||||
|
testing.expect(shr(std.meta.Vector(1, u32), std.meta.Vector(1, u32){42}, @as(isize, -1))[0] == @as(u32, 42) << 1);
|
||||||
|
testing.expect(shr(std.meta.Vector(1, u32), std.meta.Vector(1, u32){42}, 33)[0] == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Rotates right. Only unsigned values can be rotated.
|
/// Rotates right. Only unsigned values can be rotated.
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue