authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2020-11-02 23:47:14+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-11-05 17:20:54-05:00
log2e354c387e5b5f9a033c29e622ea4a4dbb04bccb
tree75c55eb90a98b716f0b244f6d2730807476c5d6f
parent73aef46f7dd0b34b8b9f4248e592b58eeb219dd4

math.shl/math.shr: add support for vectors


2 files changed, 35 insertions(+), 9 deletions(-)

lib/std/crypto/blake2.zig+4-4
......@@ -261,7 +261,7 @@ test "blake2s224 streaming" {
261261
262262test "comptime blake2s224" {
263263 comptime {
264 @setEvalBranchQuota(6000);
264 @setEvalBranchQuota(10000);
265265 var block = [_]u8{0} ** Blake2s224.block_length;
266266 var out: [Blake2s224.digest_length]u8 = undefined;
267267
......@@ -353,7 +353,7 @@ test "blake2s256 keyed" {
353353
354354test "comptime blake2s256" {
355355 comptime {
356 @setEvalBranchQuota(6000);
356 @setEvalBranchQuota(10000);
357357 var block = [_]u8{0} ** Blake2s256.block_length;
358358 var out: [Blake2s256.digest_length]u8 = undefined;
359359
......@@ -611,7 +611,7 @@ test "blake2b384 streaming" {
611611
612612test "comptime blake2b384" {
613613 comptime {
614 @setEvalBranchQuota(7000);
614 @setEvalBranchQuota(10000);
615615 var block = [_]u8{0} ** Blake2b384.block_length;
616616 var out: [Blake2b384.digest_length]u8 = undefined;
617617
......@@ -703,7 +703,7 @@ test "blake2b512 keyed" {
703703
704704test "comptime blake2b512" {
705705 comptime {
706 @setEvalBranchQuota(8000);
706 @setEvalBranchQuota(10000);
707707 var block = [_]u8{0} ** Blake2b512.block_length;
708708 var out: [Blake2b512.digest_length]u8 = undefined;
709709
lib/std/math.zig+31-5
......@@ -352,7 +352,18 @@ pub fn shlExact(comptime T: type, a: T, shift_amt: Log2Int(T)) !T {
352352/// A negative shift amount results in a right shift.
353353pub fn shl(comptime T: type, a: T, shift_amt: anytype) T {
354354 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 };
356367
357368 if (@TypeOf(shift_amt) == comptime_int or @typeInfo(@TypeOf(shift_amt)).Int.is_signed) {
358369 if (shift_amt < 0) {
......@@ -372,18 +383,30 @@ test "math.shl" {
372383 testing.expect(shl(u8, 0b11111111, 8) == 0);
373384 testing.expect(shl(u8, 0b11111111, 9) == 0);
374385 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);
375389}
376390
377391/// Shifts right. Overflowed bits are truncated.
378392/// A negative shift amount results in a left shift.
379393pub fn shr(comptime T: type, a: T, shift_amt: anytype) T {
380394 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);
382395
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));
386402 } 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) {
387410 return a << casted_shift_amt;
388411 }
389412 }
......@@ -400,6 +423,9 @@ test "math.shr" {
400423 testing.expect(shr(u8, 0b11111111, 8) == 0);
401424 testing.expect(shr(u8, 0b11111111, 9) == 0);
402425 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);
403429}
404430
405431/// Rotates right. Only unsigned values can be rotated.