@swaptoshi/dex-module
Version:
Klayr decentralized exchange (dex) on-chain module
98 lines • 2.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.mostSignificantBit = mostSignificantBit;
exports.leastSignificantBit = leastSignificantBit;
const int_1 = require("../int");
function mostSignificantBit(value) {
let x = BigInt(value);
if (x <= BigInt(0)) {
throw new Error('value must be greater than 0');
}
let r = 0;
if (x >= BigInt('0x100000000000000000000000000000000')) {
x >>= BigInt(128);
r += 128;
}
if (x >= BigInt('0x10000000000000000')) {
x >>= BigInt(64);
r += 64;
}
if (x >= BigInt('0x100000000')) {
x >>= BigInt(32);
r += 32;
}
if (x >= BigInt('0x10000')) {
x >>= BigInt(16);
r += 16;
}
if (x >= BigInt('0x100')) {
x >>= BigInt(8);
r += 8;
}
if (x >= BigInt('0x10')) {
x >>= BigInt(4);
r += 4;
}
if (x >= BigInt('0x4')) {
x >>= BigInt(2);
r += 2;
}
if (x >= BigInt('0x2')) {
r += 1;
}
return r.toString();
}
function leastSignificantBit(value) {
let x = BigInt(value);
if (x <= BigInt(0)) {
throw new Error('value must be greater than 0');
}
let r = 255;
if ((x & BigInt(int_1.Uint128.MAX)) > BigInt(0)) {
r -= 128;
}
else {
x >>= BigInt(128);
}
if ((x & BigInt(int_1.Uint64.MAX)) > BigInt(0)) {
r -= 64;
}
else {
x >>= BigInt(64);
}
if ((x & BigInt(int_1.Uint32.MAX)) > BigInt(0)) {
r -= 32;
}
else {
x >>= BigInt(32);
}
if ((x & BigInt(int_1.Uint16.MAX)) > BigInt(0)) {
r -= 16;
}
else {
x >>= BigInt(16);
}
if ((x & BigInt(int_1.Uint8.MAX)) > BigInt(0)) {
r -= 8;
}
else {
x >>= BigInt(8);
}
if ((x & BigInt(15)) > BigInt(0)) {
r -= 4;
}
else {
x >>= BigInt(4);
}
if ((x & BigInt(3)) > BigInt(0)) {
r -= 2;
}
else {
x >>= BigInt(2);
}
if ((x & BigInt(1)) > BigInt(0)) {
r -= 1;
}
return r.toString();
}
//# sourceMappingURL=bit_math.js.map