diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-01-13 00:00:33 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-01-13 00:00:33 -0500 |
| commit | a2315cfbfcfe33a2b9010994a1da6674854151e9 (patch) | |
| tree | d509b0494776da54c78aa299d4679cf4f17180e0 /std/math/index.zig | |
| parent | 3268276b58d8b65cb295b738d7c14174005bd84e (diff) | |
| parent | 51fdbf7f8c282c7b91a688c9069b363e90bedf6e (diff) | |
| download | zig-a2315cfbfcfe33a2b9010994a1da6674854151e9.tar.gz zig-a2315cfbfcfe33a2b9010994a1da6674854151e9.zip | |
Merge pull request #686 from zig-lang/md5-sha1
Add Md5 and Sha1 functions
Diffstat (limited to 'std/math/index.zig')
| -rw-r--r-- | std/math/index.zig | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/std/math/index.zig b/std/math/index.zig index 081c59a88a..d925740030 100644 --- a/std/math/index.zig +++ b/std/math/index.zig @@ -267,6 +267,45 @@ test "math.shr" { assert(shr(u8, 0b11111111, isize(-2)) == 0b11111100); } +/// Rotates right. Only unsigned values can be rotated. +/// Negative shift values results in shift modulo the bit count. +pub fn rotr(comptime T: type, x: T, r: var) -> T { + if (T.is_signed) { + @compileError("cannot rotate signed integer"); + } else { + const ar = @mod(r, T.bit_count); + return shr(T, x, ar) | shl(T, x, T.bit_count - ar); + } +} + +test "math.rotr" { + assert(rotr(u8, 0b00000001, usize(0)) == 0b00000001); + assert(rotr(u8, 0b00000001, usize(9)) == 0b10000000); + assert(rotr(u8, 0b00000001, usize(8)) == 0b00000001); + assert(rotr(u8, 0b00000001, usize(4)) == 0b00010000); + assert(rotr(u8, 0b00000001, isize(-1)) == 0b00000010); +} + +/// Rotates left. Only unsigned values can be rotated. +/// Negative shift values results in shift modulo the bit count. +pub fn rotl(comptime T: type, x: T, r: var) -> T { + if (T.is_signed) { + @compileError("cannot rotate signed integer"); + } else { + const ar = @mod(r, T.bit_count); + return shl(T, x, ar) | shr(T, x, T.bit_count - ar); + } +} + +test "math.rotl" { + assert(rotl(u8, 0b00000001, usize(0)) == 0b00000001); + assert(rotl(u8, 0b00000001, usize(9)) == 0b00000010); + assert(rotl(u8, 0b00000001, usize(8)) == 0b00000001); + assert(rotl(u8, 0b00000001, usize(4)) == 0b00010000); + assert(rotl(u8, 0b00000001, isize(-1)) == 0b10000000); +} + + pub fn Log2Int(comptime T: type) -> type { return @IntType(false, log2(T.bit_count)); } |
