| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | |
| 3 | var GetIntrinsic = require('get-intrinsic'); |
| 4 | |
| 5 | var $TypeError = GetIntrinsic('%TypeError%'); |
| 6 | // var $BigInt = GetIntrinsic('%BigInt%', true); |
| 7 | // var $pow = GetIntrinsic('%Math.pow%'); |
| 8 | |
| 9 | // var BinaryAnd = require('./BinaryAnd'); |
| 10 | // var BinaryOr = require('./BinaryOr'); |
| 11 | // var BinaryXor = require('./BinaryXor'); |
| 12 | var Type = require('./Type'); |
| 13 | // var modulo = require('./modulo'); |
| 14 | |
| 15 | // var zero = $BigInt && $BigInt(0); |
| 16 | // var negOne = $BigInt && $BigInt(-1); |
| 17 | // var two = $BigInt && $BigInt(2); |
| 18 | |
| 19 | // https://262.ecma-international.org/11.0/#sec-bigintbitwiseop |
| 20 | |
| 21 | module.exports = function BigIntBitwiseOp(op, x, y) { |
| 22 | if (op !== '&' && op !== '|' && op !== '^') { |
| 23 | throw new $TypeError('Assertion failed: `op` must be `&`, `|`, or `^`'); |
| 24 | } |
| 25 | if (Type(x) !== 'BigInt' || Type(y) !== 'BigInt') { |
| 26 | throw new $TypeError('`x` and `y` must be BigInts'); |
| 27 | } |
| 28 | |
| 29 | if (op === '&') { |
| 30 | return x & y; |
| 31 | } |
| 32 | if (op === '|') { |
| 33 | return x | y; |
| 34 | } |
| 35 | return x ^ y; |
| 36 | /* |
| 37 | var result = zero; |
| 38 | var shift = 0; |
| 39 | while (x !== zero && x !== negOne && y !== zero && y !== negOne) { |
| 40 | var xDigit = modulo(x, two); |
| 41 | var yDigit = modulo(y, two); |
| 42 | if (op === '&') { |
| 43 | result += $pow(2, shift) * BinaryAnd(xDigit, yDigit); |
| 44 | } else if (op === '|') { |
| 45 | result += $pow(2, shift) * BinaryOr(xDigit, yDigit); |
| 46 | } else if (op === '^') { |
| 47 | result += $pow(2, shift) * BinaryXor(xDigit, yDigit); |
| 48 | } |
| 49 | shift += 1; |
| 50 | x = (x - xDigit) / two; |
| 51 | y = (y - yDigit) / two; |
| 52 | } |
| 53 | var tmp; |
| 54 | if (op === '&') { |
| 55 | tmp = BinaryAnd(modulo(x, two), modulo(y, two)); |
| 56 | } else if (op === '|') { |
| 57 | tmp = BinaryAnd(modulo(x, two), modulo(y, two)); |
| 58 | } else { |
| 59 | tmp = BinaryXor(modulo(x, two), modulo(y, two)); |
| 60 | } |
| 61 | if (tmp !== 0) { |
| 62 | result -= $pow(2, shift); |
| 63 | } |
| 64 | return result; |
| 65 | */ |
| 66 | }; |