diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-10-17 21:59:10 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-17 21:59:10 -0400 |
| commit | ad17108bddc3bc198190407ab5b00820b2c17cd5 (patch) | |
| tree | b59e6657f6350aefe8a67631d8398ba584a9717e /src/value.zig | |
| parent | e9d1e5e533d12abe14582736d90e4cb173addc56 (diff) | |
| parent | 15a0b30d8e905a7b46fa97175d9bdba2bd5a8403 (diff) | |
| download | zig-ad17108bddc3bc198190407ab5b00820b2c17cd5.tar.gz zig-ad17108bddc3bc198190407ab5b00820b2c17cd5.zip | |
Merge pull request #9960 from Snektron/bit-not
Some not and vector stuff
Diffstat (limited to 'src/value.zig')
| -rw-r--r-- | src/value.zig | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/value.zig b/src/value.zig index 127c6c27b4..030ee8f783 100644 --- a/src/value.zig +++ b/src/value.zig @@ -2081,6 +2081,32 @@ pub const Value = extern union { }; } + /// operands must be integers; handles undefined. + pub fn bitwiseNot(val: Value, ty: Type, arena: *Allocator, target: Target) !Value { + if (val.isUndef()) return Value.initTag(.undef); + + const info = ty.intInfo(target); + + // TODO is this a performance issue? maybe we should try the operation without + // resorting to BigInt first. + var val_space: Value.BigIntSpace = undefined; + const val_bigint = val.toBigInt(&val_space); + 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.bitNotWrap(val_bigint, 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); + } + } + /// operands must be integers; handles undefined. pub fn bitwiseAnd(lhs: Value, rhs: Value, arena: *Allocator) !Value { if (lhs.isUndef() or rhs.isUndef()) return Value.initTag(.undef); |
