Unroll u8aToBn loops (#1535)

* Unroll u8aToBn loops

* Adjust with additional xor shortcuts

* Adjust mult -> shift (where below u32)

* assertUnreachable

* Add u8aToNumber
This commit is contained in:
Jaco
2022-06-16 11:29:23 +03:00
committed by GitHub
parent cfb5d24a87
commit fff30601a0
9 changed files with 619 additions and 140 deletions
+4
View File
@@ -1,6 +1,10 @@
// Copyright 2017-2022 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
/**
* @name arrayZip
* @description Combines 2 distinct key/value arrays into a single [K, V] array
*/
export function arrayZip <K, V> (keys: K[], values: V[]): [K, V][] {
const result = new Array<[K, V]>(keys.length);
+62 -24
View File
@@ -38,30 +38,6 @@ describe('bnToU8a', (): void => {
).toEqual(new Uint8Array([0x56, 0x34, 0x12, 0x00]));
});
it('converts negative numbers', (): void => {
expect(
bnToU8a(new BN(-1234), { isNegative: true })
).toEqual(new Uint8Array([46, 251]));
});
it('converts negative numbers (BE)', (): void => {
expect(
bnToU8a(new BN(-1234), { isLe: false, isNegative: true })
).toEqual(new Uint8Array([251, 46]));
});
it('converts negative numbers (LE)', (): void => {
expect(
bnToU8a(new BN(-1234), { bitLength: 32, isNegative: true })
).toEqual(new Uint8Array([46, 251, 255, 255]));
});
it('converts negative numbers (LE, bitLength)', (): void => {
expect(
bnToU8a(new BN(-123456), { bitLength: 32, isNegative: true })
).toEqual(new Uint8Array([192, 29, 254, 255]));
});
it('handles backwards compatibility', (): void => {
expect(
bnToU8a(new BN(1234), 32, false)
@@ -70,5 +46,67 @@ describe('bnToU8a', (): void => {
);
});
describe('signed', (): void => {
it('converts negative numbers (BE)', (): void => {
expect(
bnToU8a(new BN(-1234), { isLe: false, isNegative: true })
).toEqual(new Uint8Array([251, 46]));
});
it('converts negative numbers (LE, i8)', (): void => {
expect(
bnToU8a(new BN(-12), { isNegative: true })
).toEqual(new Uint8Array([244]));
});
it('converts negative numbers (LE, i16)', (): void => {
expect(
bnToU8a(new BN(-1234), { isNegative: true })
).toEqual(new Uint8Array([46, 251]));
});
it('converts negative numbers (LE, i24)', (): void => {
expect(
bnToU8a(new BN(-123456), { isNegative: true })
).toEqual(new Uint8Array([192, 29, 254]));
});
it('converts negative numbers (LE, i32)', (): void => {
expect(
bnToU8a(new BN(-123456789), { isNegative: true })
).toEqual(new Uint8Array([235, 50, 164, 248]));
});
it('converts negative numbers (LE, i40)', (): void => {
expect(
bnToU8a(new BN(-5678999999), { isNegative: true })
).toEqual(new Uint8Array([65, 86, 129, 173, 254]));
});
it('converts negative numbers (LE, i48)', (): void => {
expect(
bnToU8a(new BN(-9999999999999), { isNegative: true })
).toEqual(new Uint8Array([1, 96, 141, 177, 231, 246]));
});
it('converts negative numbers (LE, i64)', (): void => {
expect(
bnToU8a(new BN('-999999999999999999'), { isNegative: true })
).toEqual(new Uint8Array([1, 0, 156, 88, 76, 73, 31, 242]));
});
it('converts negative numbers (LE, bitLength)', (): void => {
expect(
bnToU8a(new BN(-1234), { bitLength: 32, isNegative: true })
).toEqual(new Uint8Array([46, 251, 255, 255]));
});
it('converts negative numbers (LE, bitLength)', (): void => {
expect(
bnToU8a(new BN(-123456), { bitLength: 32, isNegative: true })
).toEqual(new Uint8Array([192, 29, 254, 255]));
});
});
performance('bnToU8a', 250000, ptest, bnToU8a);
});
+45 -43
View File
@@ -30,35 +30,38 @@ export function compactFromU8a (input: U8aLike): [number, BN] {
return [1, new BN(u8a[0] >>> 2)];
case 0b01:
return [2, new BN((u8a[0] + (u8a[1] * 0x1_00)) >>> 2)];
return [2, new BN((u8a[0] + (u8a[1] << 8)) >>> 2)];
case 0b10:
return [4, new BN((u8a[0] + (u8a[1] * 0x1_00) + (u8a[2] * 0x1_00_00) + (u8a[3] * 0x1_00_00_00)) >>> 2)];
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
// 32-bit, in the case where the top-most bit is set this yields a negative value
return [4, new BN((u8a[0] + (u8a[1] << 8) + (u8a[2] << 16) + (u8a[3] * 0x1_00_00_00)) >>> 2)];
// 0b11
default:
break;
}
default: {
// add 5 to shifted (4 for base length, 1 for this byte)
const offset = (u8a[0] >>> 2) + 5;
// add 5 to shifted (4 for base length, 1 for this byte)
const offset = (u8a[0] >>> 2) + 5;
// we unroll the loop
switch (offset) {
// there still could be 4 bytes data, similar to 0b10 above (with offsets)
case 5:
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
// 32-bit, in the case where the top-most bit is set this yields a negative value
return [5, new BN(u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + (u8a[4] * 0x1_00_00_00))];
// we unroll the loop
switch (offset) {
// there still could be 4 bytes data, similar to 0b10 above (with offsets)
case 5:
return [5, new BN(u8a[1] + (u8a[2] * 0x1_00) + (u8a[3] * 0x1_00_00) + (u8a[4] * 0x1_00_00_00))];
case 6:
return [6, new BN(u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + ((u8a[4] + (u8a[5] << 8)) * 0x1_00_00_00))];
case 6:
return [6, new BN(u8a[1] + (u8a[2] * 0x1_00) + (u8a[3] * 0x1_00_00) + (u8a[4] * 0x1_00_00_00) + (u8a[5] * 0x1_00_00_00_00))];
// 6 bytes data is the maximum, 48 bits (56 would overflow)
case 7:
return [7, new BN(u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + ((u8a[4] + (u8a[5] << 8) + (u8a[6] << 16)) * 0x1_00_00_00))];
// 6 bytes data is the maximum, 48 bits (56 would overflow)
case 7:
return [7, new BN(u8a[1] + (u8a[2] * 0x1_00) + (u8a[3] * 0x1_00_00) + (u8a[4] * 0x1_00_00_00) + (u8a[5] * 0x1_00_00_00_00) + (u8a[6] * 0x1_00_00_00_00_00))];
// for anything else, use the non-unrolled version
default:
return [offset, u8aToBn(u8a.subarray(1, offset))];
// for anything else, use the non-unrolled version
default:
return [offset, u8aToBn(u8a.subarray(1, offset))];
}
}
}
}
@@ -74,34 +77,33 @@ export function compactFromU8aLim (u8a: Uint8Array): [number, number] {
return [1, u8a[0] >>> 2];
case 0b01:
return [2, (u8a[0] + (u8a[1] * 0x1_00)) >>> 2];
return [2, (u8a[0] + (u8a[1] << 8)) >>> 2];
case 0b10:
return [4, (u8a[0] + (u8a[1] * 0x1_00) + (u8a[2] * 0x1_00_00) + (u8a[3] * 0x1_00_00_00)) >>> 2];
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
// 32-bit, in the case where the top-most bit is set this yields a negative value
return [4, (u8a[0] + (u8a[1] << 8) + (u8a[2] << 16) + (u8a[3] * 0x1_00_00_00)) >>> 2];
// 0b11
default:
break;
}
default: {
// add 5 to shifted (4 for base length, 1 for this byte)
// we unroll the loop
switch ((u8a[0] >>> 2) + 5) {
// there still could be 4 bytes data, similar to 0b10 above (with offsets)
case 5:
return [5, u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + (u8a[4] * 0x1_00_00_00)];
// add 5 to shifted (4 for base length, 1 for this byte)
const offset = (u8a[0] >>> 2) + 5;
case 6:
return [6, u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + ((u8a[4] + (u8a[5] << 8)) * 0x1_00_00_00)];
// we unroll the loop
switch (offset) {
// there still could be 4 bytes data, similar to 0b10 above (with offsets)
case 5:
return [5, u8a[1] + (u8a[2] * 0x1_00) + (u8a[3] * 0x1_00_00) + (u8a[4] * 0x1_00_00_00)];
// 6 bytes data is the maximum, 48 bits (56 would overflow)
case 7:
return [7, u8a[1] + (u8a[2] << 8) + (u8a[3] << 16) + ((u8a[4] + (u8a[5] << 8) + (u8a[6] << 16)) * 0x1_00_00_00)];
case 6:
return [6, u8a[1] + (u8a[2] * 0x1_00) + (u8a[3] * 0x1_00_00) + (u8a[4] * 0x1_00_00_00) + (u8a[5] * 0x1_00_00_00_00)];
// 6 bytes data is the maximum, 48 bits (56 would overflow)
case 7:
return [7, u8a[1] + (u8a[2] * 0x1_00) + (u8a[3] * 0x1_00_00) + (u8a[4] * 0x1_00_00_00) + (u8a[5] * 0x1_00_00_00_00) + (u8a[6] * 0x1_00_00_00_00_00)];
// for anything else, we are above the actual MAX_SAFE_INTEGER - bail out
default:
throw new Error('Compact input is > Number.MAX_SAFE_INTEGER');
// for anything else, we are above the actual MAX_SAFE_INTEGER - bail out
default:
throw new Error('Compact input is > Number.MAX_SAFE_INTEGER');
}
}
}
}
+1 -1
View File
@@ -40,7 +40,7 @@ export interface ToBnOptions {
*/
isLe?: boolean;
/**
* @description Number is negative, apply two's complement
* @description Number is signed, apply two's complement
*/
isNegative?: boolean;
}
+1
View File
@@ -15,6 +15,7 @@ export { u8aToBigInt } from './toBigInt';
export { u8aToBn } from './toBn';
export { u8aToBuffer } from './toBuffer';
export { u8aToHex } from './toHex';
export { u8aToNumber } from './toNumber';
export { u8aToString } from './toString';
export { u8aToU8a } from './toU8a';
export { U8A_WRAP_ETHEREUM, U8A_WRAP_POSTFIX, U8A_WRAP_PREFIX, u8aIsWrapped, u8aWrapBytes, u8aUnwrapBytes } from './wrap';
+192 -55
View File
@@ -28,49 +28,183 @@ describe('u8aToBn', (): void => {
).toBe('3412');
});
it('converts values (big-endian)', (): void => {
expect(
u8aToBn(
new Uint8Array([0x12, 0x34]),
{ isLe: false }
).toString(16)
).toBe('1234');
describe('LE', (): void => {
describe('unsigned', (): void => {
it('converts values (u8)', (): void => {
expect(
u8aToBn(
new Uint8Array([0x12]),
{ isLe: true }
).toString(16)
).toBe('12');
});
it('converts values (u16)', (): void => {
expect(
u8aToBn(
new Uint8Array([0x12, 0x34]),
{ isLe: true }
).toString(16)
).toBe('3412');
});
it('converts values (u24)', (): void => {
expect(
u8aToBn(
new Uint8Array([0x12, 0x34, 0x56]),
{ isLe: true }
).toString(16)
).toBe('563412');
});
it('converts values (u32)', (): void => {
expect(
u8aToBn(
new Uint8Array([0x12, 0x34, 0x56, 0x78]),
{ isLe: true }
).toString(16)
).toBe('78563412');
});
it('converts values (u40)', (): void => {
expect(
u8aToBn(
new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x9a]),
{ isLe: true }
).toString(16)
).toBe('9a78563412');
});
it('converts values (u48)', (): void => {
expect(
u8aToBn(
new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc]),
{ isLe: true }
).toString(16)
).toBe('bc9a78563412');
});
});
describe('signed', (): void => {
it('converts values (i8)', (): void => {
expect(
u8aToBn(
new Uint8Array([244]),
{ isLe: true, isNegative: true }
).toString()
).toBe('-12');
});
it('converts values (i16)', (): void => {
expect(
u8aToBn(
new Uint8Array([46, 251]),
{ isLe: true, isNegative: true }
).toString()
).toBe('-1234');
});
it('converts values (i24)', (): void => {
expect(
u8aToBn(
new Uint8Array([192, 29, 254]),
{ isLe: true, isNegative: true }
).toString()
).toBe('-123456');
});
it('converts values (i32)', (): void => {
expect(
u8aToBn(
new Uint8Array([235, 50, 164, 248]),
{ isLe: true, isNegative: true }
).toString()
).toBe('-123456789');
});
it('converts values (i40)', (): void => {
expect(
u8aToBn(
new Uint8Array([65, 86, 129, 173, 254]),
{ isLe: true, isNegative: true }
).toString()
).toBe('-5678999999');
});
it('converts values (i48)', (): void => {
expect(
u8aToBn(
new Uint8Array([1, 96, 141, 177, 231, 246]),
{ isLe: true, isNegative: true }
).toString()
).toBe('-9999999999999');
});
it('converts values (i64)', (): void => {
expect(
u8aToBn(
new Uint8Array([1, 0, 156, 88, 76, 73, 31, 242]),
{ isLe: true, isNegative: true }
).toString()
).toBe('-999999999999999999');
});
});
});
it('converts values (little-endian, u16)', (): void => {
expect(
u8aToBn(
new Uint8Array([0x12, 0x34]),
{ isLe: true }
).toString(16)
).toBe('3412');
});
describe('BE', (): void => {
it('converts values (u8)', (): void => {
expect(
u8aToBn(
new Uint8Array([0x12]),
{ isLe: false }
).toString(16)
).toBe('12');
});
it('converts values (little-endian, u32)', (): void => {
expect(
u8aToBn(
new Uint8Array([0x12, 0x34, 0x56, 0x78]),
{ isLe: true }
).toString(16)
).toBe('78563412');
});
it('converts values (u16)', (): void => {
expect(
u8aToBn(
new Uint8Array([0x12, 0x34]),
{ isLe: false }
).toString(16)
).toBe('1234');
});
it('converts values (big-endian, u16)', (): void => {
expect(
u8aToBn(
new Uint8Array([0x12, 0x34]),
{ isLe: false }
).toString(16)
).toBe('1234');
});
it('converts values (u24)', (): void => {
expect(
u8aToBn(
new Uint8Array([0x12, 0x34, 0x56]),
{ isLe: false }
).toString(16)
).toBe('123456');
});
it('converts values (big-endian, u32)', (): void => {
expect(
u8aToBn(
new Uint8Array([0x12, 0x34, 0x56, 0x78]),
{ isLe: false }
).toString(16)
).toBe('12345678');
it('converts values (u32)', (): void => {
expect(
u8aToBn(
new Uint8Array([0x12, 0x34, 0x56, 0x78]),
{ isLe: false }
).toString(16)
).toBe('12345678');
});
it('converts values (u40)', (): void => {
expect(
u8aToBn(
new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x9a]),
{ isLe: false }
).toString(16)
).toBe('123456789a');
});
it('converts values (u48)', (): void => {
expect(
u8aToBn(
new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc]),
{ isLe: false }
).toString(16)
).toBe('123456789abc');
});
});
describe('empty creation', (): void => {
@@ -111,22 +245,24 @@ describe('u8aToBn', (): void => {
});
});
it('handles negative numbers (little-endian)', (): void => {
expect(
u8aToBn(
new Uint8Array([46, 251]),
{ isLe: true, isNegative: true }
).toNumber()
).toBe(-1234);
});
describe('negative', (): void => {
it('handles negative numbers (little-endian)', (): void => {
expect(
u8aToBn(
new Uint8Array([46, 251]),
{ isLe: true, isNegative: true }
).toNumber()
).toBe(-1234);
});
it('handles negative numbers (big-endian)', (): void => {
expect(
u8aToBn(
new Uint8Array([251, 46]),
{ isLe: false, isNegative: true }
).toNumber()
).toBe(-1234);
it('handles negative numbers (big-endian)', (): void => {
expect(
u8aToBn(
new Uint8Array([251, 46]),
{ isLe: false, isNegative: true }
).toNumber()
).toBe(-1234);
});
});
it('handles overflows correctly (little-endian)', (): void => {
@@ -163,6 +299,7 @@ describe('u8aToBn', (): void => {
).toBe(true);
});
performance('u8aToBn (u32)', 500000, [[new Uint8Array([0x68, 0x65, 0x6c, 0x6c])]], u8aToBn);
performance('u8aToBn (u64)', 500000, [[new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x68, 0x65, 0x6c, 0x6c])]], u8aToBn);
performance('u8aToBn (u32)', 1_000_000, [[new Uint8Array([0x68, 0x65, 0x6c, 0x6c])]], u8aToBn);
performance('u8aToBn (i32)', 1_000_000, [[new Uint8Array([0x68, 0x65, 0x6c, 0x6c])]], (v: Uint8Array) => u8aToBn(v, { isNegative: true }));
performance('u8aToBn (u64)', 500_000, [[new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x68, 0x65, 0x6c, 0x6c])]], u8aToBn);
});
+94 -17
View File
@@ -3,6 +3,7 @@
import type { ToBnOptions } from '../types';
import { assertUnreachable } from '../assert';
import { BN } from '../bn/bn';
import { isBoolean } from '../is/boolean';
@@ -27,7 +28,7 @@ import { isBoolean } from '../is/boolean';
function u8aToBn (value: Uint8Array, options?: ToBnOptions): BN;
/** @deprecated Use u8aToBn(value: Uint8Array, options?: ToBnOptions) */
function u8aToBn (value: Uint8Array, options?: boolean): BN;
/** @deprecated Use hexToBn (value?: string | null, options?: ToBnOptions) */
/** @deprecated Use u8aToBn (value?: string | null, options?: ToBnOptions) */
function u8aToBn (value: Uint8Array, options: ToBnOptions | boolean = {}): BN {
// NOTE: This is the same process as followed in the hexToBn conversion
// For Uint8Array, default to LE
@@ -35,30 +36,106 @@ function u8aToBn (value: Uint8Array, options: ToBnOptions | boolean = {}): BN {
? { isLe: options }
: options;
const count = value.length;
let bn: BN;
// shortcut for <= u48 values - in this case the manual conversion
// here seems to be more efficient than passing the full array
// (and yes, it can be u48, e.g. in compacts with variable lengths)
if (count <= 6) {
let result = 0;
if (isNegative) {
let result = 0;
for (let i = 0; i < count; i++) {
result = (result * 0x1_00) + (
isLe
? value[count - 1 - i]
: value[i]
);
if (isLe) {
// Most common case i{8, 16, 32} default LE SCALE-encoded
// For <= 32, we also optimize the xor to a single op
// (see the comments around unrolling in the next section)
switch (count) {
case 0:
return new BN(0);
case 1:
result = value[0] ^ 0x0000_00ff;
break;
case 2:
result = (value[0] + (value[1] << 8)) ^ 0x0000_ffff;
break;
case 3:
result = (value[0] + (value[1] << 8) + (value[2] << 16)) ^ 0x00ff_ffff;
break;
case 4:
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
// 32-bit, in the case where the top-most bit is set this yields a negative value
result = (value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00)) ^ 0xffff_ffff;
break;
case 5:
result = ((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00)) ^ 0xffff_ffff) + ((value[4] ^ 0xff) * 0x1_00_00_00_00);
break;
case 6:
result = ((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00)) ^ 0xffff_ffff) + (((value[4] + (value[5] << 8)) ^ 0x0000_ffff) * 0x1_00_00_00_00);
break;
default:
assertUnreachable(count as never);
}
} else {
for (let i = 0; i < count; i++) {
result = (result * 0x1_00) + (value[i] ^ 0xff);
}
}
return count
? new BN((result * -1) - 1)
: new BN(0);
} else if (isLe) {
// Most common case - u{8, 16, 32} default LE SCALE-encoded
//
// There are some slight benefits in unrolling this specific loop,
// however it comes with diminishing returns since here the actual
// `new BN` does seem to take up the bulk of the time
switch (count) {
case 0:
return new BN(0);
case 1:
return new BN(value[0]);
case 2:
return new BN(value[0] + (value[1] << 8));
case 3:
return new BN(value[0] + (value[1] << 8) + (value[2] << 16));
case 4:
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
// 32-bit, in the case where the top-most bit is set this yields a negative value
return new BN(value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00));
case 5:
return new BN(value[0] + (value[1] << 8) + (value[2] << 16) + ((value[3] + (value[4] << 8)) * 0x1_00_00_00));
case 6:
return new BN(value[0] + (value[1] << 8) + (value[2] << 16) + ((value[3] + (value[4] << 8) + (value[5] << 16)) * 0x1_00_00_00));
default:
assertUnreachable(count as never);
}
} else {
let result = 0;
for (let i = 0; i < count; i++) {
result = (result * 0x1_00) + value[i];
}
return new BN(result);
}
bn = new BN(result);
} else {
bn = new BN(value, isLe ? 'le' : 'be');
}
return isNegative && value.length
? bn.fromTwos(value.length * 8)
: bn;
return isNegative
? new BN(value, isLe ? 'le' : 'be').fromTwos(value.length * 8)
: new BN(value, isLe ? 'le' : 'be');
}
export { u8aToBn };
+135
View File
@@ -0,0 +1,135 @@
// Copyright 2017-2022 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { performance } from '../test/performance';
import { u8aToNumber } from '.';
describe('u8aToNumber', (): void => {
describe('unsigned', (): void => {
it('converts values (u8)', (): void => {
expect(
u8aToNumber(
new Uint8Array([0x12])
)
).toBe(18);
});
it('converts values (u16)', (): void => {
expect(
u8aToNumber(
new Uint8Array([0x12, 0x34])
)
).toBe(13330);
});
it('converts values (u24)', (): void => {
expect(
u8aToNumber(
new Uint8Array([0x12, 0x34, 0x56])
)
).toBe(5649426);
});
it('converts values (u32)', (): void => {
expect(
u8aToNumber(
new Uint8Array([0x12, 0x34, 0x56, 0x78])
)
).toBe(2018915346);
});
it('converts values (u40)', (): void => {
expect(
u8aToNumber(
new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x9a])
)
).toBe(663443878930);
});
it('converts values (u48)', (): void => {
expect(
u8aToNumber(
new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc])
)
).toBe(207371629900818);
});
});
describe('signed', (): void => {
it('converts values (i8)', (): void => {
expect(
u8aToNumber(
new Uint8Array([244]),
{ isNegative: true }
)
).toBe(-12);
});
it('converts values (i16)', (): void => {
expect(
u8aToNumber(
new Uint8Array([46, 251]),
{ isNegative: true }
)
).toBe(-1234);
});
it('converts values (i24)', (): void => {
expect(
u8aToNumber(
new Uint8Array([192, 29, 254]),
{ isNegative: true }
)
).toBe(-123456);
});
it('converts values (i32)', (): void => {
expect(
u8aToNumber(
new Uint8Array([235, 50, 164, 248]),
{ isNegative: true }
)
).toBe(-123456789);
});
it('converts values (i40)', (): void => {
expect(
u8aToNumber(
new Uint8Array([65, 86, 129, 173, 254]),
{ isNegative: true }
)
).toBe(-5678999999);
});
it('converts values (i48)', (): void => {
expect(
u8aToNumber(
new Uint8Array([1, 96, 141, 177, 231, 246]),
{ isNegative: true }
)
).toBe(-9999999999999);
});
});
describe('empty creation', (): void => {
it('handles unsigned (le)', (): void => {
expect(
u8aToNumber(
new Uint8Array()
)
).toBe(0);
});
it('handles signed (le)', (): void => {
expect(
u8aToNumber(
new Uint8Array(),
{ isNegative: true }
)
).toBe(0);
});
});
performance('u8aToNumber (u32)', 1_000_000, [[new Uint8Array([0x68, 0x65, 0x6c, 0x6c])]], u8aToNumber);
performance('u8aToNumber (i32)', 1_000_000, [[new Uint8Array([0x68, 0x65, 0x6c, 0x6c])]], (v: Uint8Array) => u8aToNumber(v, { isNegative: true }));
});
+85
View File
@@ -0,0 +1,85 @@
// Copyright 2017-2022 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
interface ToNumberOptions {
/**
* @description Number is signed, apply two's complement
*/
isNegative?: boolean;
}
/**
* @name u8aToNumber
* @summary Creates a number from a Uint8Array object.
*/
export function u8aToNumber (value: Uint8Array, { isNegative = false }: ToNumberOptions = {}): number {
const count = value.length;
if (isNegative) {
let result = 0;
switch (count) {
case 0:
return 0;
case 1:
result = value[0] ^ 0x0000_00ff;
break;
case 2:
result = (value[0] + (value[1] << 8)) ^ 0x0000_ffff;
break;
case 3:
result = (value[0] + (value[1] << 8) + (value[2] << 16)) ^ 0x00ff_ffff;
break;
case 4:
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
// 32-bit, in the case where the top-most bit is set this yields a negative value
result = (value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00)) ^ 0xffff_ffff;
break;
case 5:
result = ((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00)) ^ 0xffff_ffff) + ((value[4] ^ 0xff) * 0x1_00_00_00_00);
break;
case 6:
result = ((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00)) ^ 0xffff_ffff) + (((value[4] + (value[5] << 8)) ^ 0x0000_ffff) * 0x1_00_00_00_00);
break;
default:
throw new Error('Value more than 48-bits cannot be reliably converted');
}
return (result * -1) - 1;
}
switch (count) {
case 0:
return 0;
case 1:
return value[0];
case 2:
return value[0] + (value[1] << 8);
case 3:
return value[0] + (value[1] << 8) + (value[2] << 16);
case 4:
// for the 3rd byte, we don't << 24 - since JS converts all bitwise operators to
// 32-bit, in the case where the top-most bit is set this yields a negative value
return value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00);
case 5:
return value[0] + (value[1] << 8) + (value[2] << 16) + ((value[3] + (value[4] << 8)) * 0x1_00_00_00);
case 6:
return value[0] + (value[1] << 8) + (value[2] << 16) + ((value[3] + (value[4] << 8) + (value[5] << 16)) * 0x1_00_00_00);
default:
throw new Error('Value more than 48-bits cannot be reliably converted');
}
}