Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix several edge cases with negative Int values in bit arrays on JavaScript #3784

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,16 @@
arguments were supplied in a record update.
([Surya Rose](https://github.com/GearsDatapacks))

- Fixed a bug where an incorrect bit array would be generated on JavaScript for
negative `Int` values when the segment's `size` was wider than 48 bits or when
the `Int` value was less than the minimum representable value for the segment
size.
([Richard Viney](https://github.com/richard-viney))

- Fixed a bug where an incorrect `Int` would be returned when pattern matching
to a negative value wider than 48 bits in a bit array.
([Richard Viney](https://github.com/richard-viney))

## v1.5.1 - 2024-09-26

### Bug Fixes
Expand Down
98 changes: 72 additions & 26 deletions compiler-core/templates/prelude.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export function toBitArray(segments) {
// @internal
// Derived from this answer https://stackoverflow.com/questions/8482309/converting-javascript-integer-to-byte-array-and-back
export function sizedInt(value, size, isBigEndian) {
if (size < 0) {
if (size <= 0) {
return new Uint8Array();
}
if (size % 8 != 0) {
Expand All @@ -200,22 +200,39 @@ export function sizedInt(value, size, isBigEndian) {

const byteArray = new Uint8Array(size / 8);

let byteModulus = 256;

// Convert negative number to two's complement representation
if (value < 0) {
value = 2 ** size + value;
let valueModulus;

// For output sizes larger than 48 bits BigInt is needed in order to
// maintain accuracy
if (size <= 48) {
valueModulus = 2 ** size;
} else {
valueModulus = 1n << BigInt(size);

value = BigInt(value);
byteModulus = BigInt(byteModulus);
}

value %= valueModulus;
value = valueModulus + value;
}

// The following loops work with both Number and BigInt types
if (isBigEndian) {
for (let i = byteArray.length - 1; i >= 0; i--) {
const byte = value % 256;
byteArray[i] = byte;
value = (value - byte) / 256;
const byte = value % byteModulus;
byteArray[i] = Number(byte);
value = (value - byte) / byteModulus;
}
} else {
for (let i = 0; i < byteArray.length; i++) {
const byte = value % 256;
byteArray[i] = byte;
value = (value - byte) / 256;
const byte = value % byteModulus;
byteArray[i] = Number(byte);
value = (value - byte) / byteModulus;
}
}

Expand All @@ -224,32 +241,61 @@ export function sizedInt(value, size, isBigEndian) {

// @internal
export function byteArrayToInt(byteArray, start, end, isBigEndian, isSigned) {
let value = 0;
const byteSize = end - start;

// Read bytes as an unsigned integer value
if (isBigEndian) {
for (let i = start; i < end; i++) {
value = value * 256 + byteArray[i];
// Ints wider than 48 bits are read using a BigInt, but narrower ones can
// be read with a JS number which is faster
if (byteSize <= 6) {
let value = 0;

// Read bytes as an unsigned integer value
if (isBigEndian) {
for (let i = start; i < end; i++) {
value = value * 256 + byteArray[i];
}
} else {
for (let i = end - 1; i >= start; i--) {
value = value * 256 + byteArray[i];
}
}
} else {
for (let i = end - 1; i >= start; i--) {
value = value * 256 + byteArray[i];

// For signed integers, check if the high bit is set and if so then
// reinterpret as two's complement
if (isSigned) {
const highBit = 2 ** (byteSize * 8 - 1);
if (value >= highBit) {
value -= highBit * 2;
}
}
}

if (isSigned) {
const byteSize = end - start;
return value;
} else {
let value = 0n;

const highBit = 2 ** (byteSize * 8 - 1);
// Read bytes as an unsigned integer value
if (isBigEndian) {
for (let i = start; i < end; i++) {
value = (value << 8n) + BigInt(byteArray[i]);
}
} else {
for (let i = end - 1; i >= start; i--) {
value = (value << 8n) + BigInt(byteArray[i]);
}
}

// If the high bit is set and this is a signed integer, reinterpret as
// two's complement
if (value >= highBit) {
value -= highBit * 2;
// For signed integers, check if the high bit is set and if so then
// reinterpret as two's complement
if (isSigned) {
const highBit = 1n << BigInt(byteSize * 8 - 1);
if (value >= highBit) {
value -= highBit * 2n;
}
}
}

return value;
// Convert the result into a JS number. This may cause quantizing/error on
// values outside JavaScript's safe integer range.
return Number(value);
}
}

// @internal
Expand Down
54 changes: 54 additions & 0 deletions test/javascript_prelude/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
stringBits,
toBitArray,
toList,
sizedInt,
} from "./prelude.mjs";

let failures = 0;
Expand Down Expand Up @@ -394,6 +395,8 @@ assertNotEqual(new HasCustomEquals(1, 1), new HasCustomEquals(2, 1));
assertEqual(hasEqualsField, { ...hasEqualsField });
assertNotEqual(hasEqualsField, hasEqualsField2);

// BitArray

assertEqual(new BitArray(new Uint8Array([1, 2, 3])).byteAt(0), 1);
assertEqual(new BitArray(new Uint8Array([1, 2, 3])).byteAt(2), 3);
assertEqual(new BitArray(new Uint8Array([1, 2, 3])).intFromSlice(0, 1, true, false), 1);
Expand All @@ -403,6 +406,14 @@ assertEqual(new BitArray(new Uint8Array([1, 2, 3])).intFromSlice(0, 2, false, fa
assertEqual(new BitArray(new Uint8Array([1, 160, 3])).intFromSlice(0, 2, false, true), -24575);
assertEqual(new BitArray(new Uint8Array([160, 2, 3])).intFromSlice(0, 2, true, false), 40962);
assertEqual(new BitArray(new Uint8Array([160, 2, 3])).intFromSlice(0, 2, true, true), -24574);
assertEqual(
new BitArray(new Uint8Array([255, 255, 255, 255, 255, 255, 255])).intFromSlice(0, 7, true, true),
-1,
);
assertEqual(
new BitArray(new Uint8Array([255, 255, 255, 255, 255, 255, 254])).intFromSlice(0, 7, true, false),
Number(0xFFFFFFFFFFFFFEn),
);
assertEqual(
new BitArray(new Uint8Array([63, 240, 0, 0, 0, 0, 0, 0])).floatFromSlice(0, 8, true),
1.0,
Expand All @@ -424,6 +435,49 @@ assertEqual(
new BitArray(new Uint8Array([2, 3])),
);

// sizedInt()

assertEqual(
sizedInt(100, 0, true),
new Uint8Array([]),
);
assertEqual(
sizedInt(0, 32, true),
new Uint8Array([0, 0, 0, 0]),
);
assertEqual(
sizedInt(1, 24, true),
new Uint8Array([0, 0, 1]),
);
assertEqual(
sizedInt(-1, 32, true),
new Uint8Array([255, 255, 255, 255]),
);
assertEqual(
sizedInt(80000, 16, true),
new Uint8Array([56, 128]),
);
assertEqual(
sizedInt(-80000, 16, true),
new Uint8Array([199, 128]),
);
assertEqual(
sizedInt(-489_391_639_457_909_760, 56, true),
new Uint8Array([53, 84, 229, 150, 16, 180, 0]),
);
assertEqual(
sizedInt(-1, 64, true),
new Uint8Array([255, 255, 255, 255, 255, 255, 255, 255]),
);
assertEqual(
sizedInt(Number.MAX_SAFE_INTEGER, 64, true),
new Uint8Array([0, 31, 255, 255, 255, 255, 255, 255]),
);
assertEqual(
sizedInt(Number.MIN_SAFE_INTEGER, 64, true),
new Uint8Array([255, 224, 0, 0, 0, 0, 0, 1]),
);

// Result.isOk

assertEqual(new Ok(1).isOk(), true);
Expand Down
15 changes: 15 additions & 0 deletions test/language/test/language_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,14 @@ fn bit_array_tests() -> List(Test) {
|> example(fn() { assert_equal(True, <<<<1>>:bits, 2>> == <<1, 2>>) }),
"<<1>> == <<1:int>>"
|> example(fn() { assert_equal(True, <<1>> == <<1:int>>) }),
"<<80_000:16>> == <<56, 128>>"
|> example(fn() { assert_equal(True, <<80_000:16>> == <<56, 128>>) }),
"<<-80_000:16>> == <<199, 128>>"
|> example(fn() { assert_equal(True, <<-80_000:16>> == <<199, 128>>) }),
"<<-1:64>> == <<255, 255, 255, 255, 255, 255, 255, 255>>"
|> example(fn() { assert_equal(True, <<-1:64>> == <<255, 255, 255, 255, 255, 255, 255, 255>>) }),
"<<-489_391_639_457_909_760:56>> == <<53, 84, 229, 150, 16, 180, 0>>"
|> example(fn() { assert_equal(True, <<-489_391_639_457_909_760:56>> == <<53, 84, 229, 150, 16, 180, 0>>) }),
"<<63, 240, 0, 0, 0, 0, 0, 0>> == <<1.0:float>>"
|> example(fn() {
assert_equal(True, <<63, 240, 0, 0, 0, 0, 0, 0>> == <<1.0:float>>)
Expand Down Expand Up @@ -1387,6 +1395,13 @@ fn bit_array_match_tests() {
#(a, b)
})
}),
"let <<a:64-signed>> = <<255, 255, 255, 255, 255, 255, 255, 255>>"
|> example(fn() {
assert_equal(-1, {
let assert <<a:64-signed>> = <<255, 255, 255, 255, 255, 255, 255, 255>>
a
})
}),
"let <<a:float, b:int>> = <<63,240,0,0,0,0,0,0,1>>"
|> example(fn() {
assert_equal(#(1.0, 1), {
Expand Down
Loading