diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-10-16 15:06:13 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-16 15:06:13 -0400 |
| commit | 82ec56e47e004176cc380cc69764602c4a8d0768 (patch) | |
| tree | 311a130761e959a49953d57bbef4d0eff40b025b /src/value.zig | |
| parent | 6f30c8c098fcbf52f4a78e662c89508997945e8a (diff) | |
| parent | 1e09157b53441d06cd1f49b9c3917a58ee244cb1 (diff) | |
| download | zig-82ec56e47e004176cc380cc69764602c4a8d0768.tar.gz zig-82ec56e47e004176cc380cc69764602c4a8d0768.zip | |
Merge pull request #9954 from Snektron/shifts
Big int saturating left shift
Diffstat (limited to 'src/value.zig')
| -rw-r--r-- | src/value.zig | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/value.zig b/src/value.zig index 264525a204..127c6c27b4 100644 --- a/src/value.zig +++ b/src/value.zig @@ -2404,6 +2404,39 @@ pub const Value = extern union { } } + pub fn shlSat( + lhs: Value, + rhs: Value, + ty: Type, + arena: *Allocator, + target: Target, + ) !Value { + // TODO is this a performance issue? maybe we should try the operation without + // resorting to BigInt first. + const info = ty.intInfo(target); + + var lhs_space: Value.BigIntSpace = undefined; + const lhs_bigint = lhs.toBigInt(&lhs_space); + const shift = rhs.toUnsignedInt(); + const limbs = try arena.alloc( + std.math.big.Limb, + std.math.big.int.calcTwosCompLimbCount(info.bits), + ); + var result_bigint = BigIntMutable{ + .limbs = limbs, + .positive = undefined, + .len = undefined, + }; + result_bigint.shiftLeftSat(lhs_bigint, shift, info.signedness, info.bits); + const result_limbs = result_bigint.limbs[0..result_bigint.len]; + + if (result_bigint.positive) { + return Value.Tag.int_big_positive.create(arena, result_limbs); + } else { + return Value.Tag.int_big_negative.create(arena, result_limbs); + } + } + pub fn shr(lhs: Value, rhs: Value, allocator: *Allocator) !Value { // TODO is this a performance issue? maybe we should try the operation without // resorting to BigInt first. |
