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" {...@@ -261,7 +261,7 @@ test "blake2s224 streaming" {
261261
262test "comptime blake2s224" {262test "comptime blake2s224" {
263 comptime {263 comptime {
264 @setEvalBranchQuota(6000);264 @setEvalBranchQuota(10000);
265 var block = [_]u8{0} ** Blake2s224.block_length;265 var block = [_]u8{0} ** Blake2s224.block_length;
266 var out: [Blake2s224.digest_length]u8 = undefined;266 var out: [Blake2s224.digest_length]u8 = undefined;
267267
...@@ -353,7 +353,7 @@ test "blake2s256 keyed" {...@@ -353,7 +353,7 @@ test "blake2s256 keyed" {
353353
354test "comptime blake2s256" {354test "comptime blake2s256" {
355 comptime {355 comptime {
356 @setEvalBranchQuota(6000);356 @setEvalBranchQuota(10000);
357 var block = [_]u8{0} ** Blake2s256.block_length;357 var block = [_]u8{0} ** Blake2s256.block_length;
358 var out: [Blake2s256.digest_length]u8 = undefined;358 var out: [Blake2s256.digest_length]u8 = undefined;
359359
...@@ -611,7 +611,7 @@ test "blake2b384 streaming" {...@@ -611,7 +611,7 @@ test "blake2b384 streaming" {
611611
612test "comptime blake2b384" {612test "comptime blake2b384" {
613 comptime {613 comptime {
614 @setEvalBranchQuota(7000);614 @setEvalBranchQuota(10000);
615 var block = [_]u8{0} ** Blake2b384.block_length;615 var block = [_]u8{0} ** Blake2b384.block_length;
616 var out: [Blake2b384.digest_length]u8 = undefined;616 var out: [Blake2b384.digest_length]u8 = undefined;
617617
...@@ -703,7 +703,7 @@ test "blake2b512 keyed" {...@@ -703,7 +703,7 @@ test "blake2b512 keyed" {
703703
704test "comptime blake2b512" {704test "comptime blake2b512" {
705 comptime {705 comptime {
706 @setEvalBranchQuota(8000);706 @setEvalBranchQuota(10000);
707 var block = [_]u8{0} ** Blake2b512.block_length;707 var block = [_]u8{0} ** Blake2b512.block_length;
708 var out: [Blake2b512.digest_length]u8 = undefined;708 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 {...@@ -352,7 +352,18 @@ pub fn shlExact(comptime T: type, a: T, shift_amt: Log2Int(T)) !T {
352/// A negative shift amount results in a right shift.352/// A negative shift amount results in a right shift.
353pub fn shl(comptime T: type, a: T, shift_amt: anytype) T {353pub fn shl(comptime T: type, a: T, shift_amt: anytype) T {
354 const abs_shift_amt = absCast(shift_amt);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 };
356367
357 if (@TypeOf(shift_amt) == comptime_int or @typeInfo(@TypeOf(shift_amt)).Int.is_signed) {368 if (@TypeOf(shift_amt) == comptime_int or @typeInfo(@TypeOf(shift_amt)).Int.is_signed) {
358 if (shift_amt < 0) {369 if (shift_amt < 0) {
...@@ -372,18 +383,30 @@ test "math.shl" {...@@ -372,18 +383,30 @@ test "math.shl" {
372 testing.expect(shl(u8, 0b11111111, 8) == 0);383 testing.expect(shl(u8, 0b11111111, 8) == 0);
373 testing.expect(shl(u8, 0b11111111, 9) == 0);384 testing.expect(shl(u8, 0b11111111, 9) == 0);
374 testing.expect(shl(u8, 0b11111111, -2) == 0b00111111);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}
376390
377/// Shifts right. Overflowed bits are truncated.391/// Shifts right. Overflowed bits are truncated.
378/// A negative shift amount results in a left shift.392/// A negative shift amount results in a left shift.
379pub fn shr(comptime T: type, a: T, shift_amt: anytype) T {393pub fn shr(comptime T: type, a: T, shift_amt: anytype) T {
380 const abs_shift_amt = absCast(shift_amt);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);
382395
383 if (@TypeOf(shift_amt) == comptime_int or @typeInfo(@TypeOf(shift_amt)).Int.is_signed) {396 const casted_shift_amt = blk: {
384 if (shift_amt >= 0) {397 if (@typeInfo(T) == .Vector) {
385 return a >> casted_shift_amt;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 } else {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 return a << casted_shift_amt;410 return a << casted_shift_amt;
388 }411 }
389 }412 }
...@@ -400,6 +423,9 @@ test "math.shr" {...@@ -400,6 +423,9 @@ test "math.shr" {
400 testing.expect(shr(u8, 0b11111111, 8) == 0);423 testing.expect(shr(u8, 0b11111111, 8) == 0);
401 testing.expect(shr(u8, 0b11111111, 9) == 0);424 testing.expect(shr(u8, 0b11111111, 9) == 0);
402 testing.expect(shr(u8, 0b11111111, -2) == 0b11111100);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}
404430
405/// Rotates right. Only unsigned values can be rotated.431/// Rotates right. Only unsigned values can be rotated.