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