Skip to content
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: 7 additions & 3 deletions javascript/packages/core/lib/writer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ import { toBFloat16Bits } from "../types/bfloat16";

const MAX_POOL_SIZE = 1024 * 1024 * 3; // 3MB
const MAX_SAFE_BIGINT = BigInt(Number.MAX_SAFE_INTEGER);
const MIN_SAFE_BIGINT = BigInt(Number.MIN_SAFE_INTEGER);
// Zigzag in doubles needs -v * 2 - 1 to stay exact. Below -(2^52) that value is
// an odd integer above 2^53, which rounds to an even neighbor and flips the
// decoded sign, so those values must take the bigint zigzag path.
const MIN_VAR_INT64_NUMBER = -4503599627370496; // -(2^52)
const MIN_VAR_INT64_BIGINT = BigInt(MIN_VAR_INT64_NUMBER);

function getInternalStringDetector() {
try {
Expand Down Expand Up @@ -455,12 +459,12 @@ export class BinaryWriter {

writeVarInt64(v: bigint | number) {
if (typeof v !== "bigint") {
if (Number.isSafeInteger(v)) {
if (Number.isSafeInteger(v) && v >= MIN_VAR_INT64_NUMBER) {
this.writeVarUInt64Number(v >= 0 ? v * 2 : -v * 2 - 1);
return;
}
v = BigInt(v);
} else if (v >= MIN_SAFE_BIGINT && v <= MAX_SAFE_BIGINT) {
} else if (v >= MIN_VAR_INT64_BIGINT && v <= MAX_SAFE_BIGINT) {
const value = Number(v);
this.writeVarUInt64Number(value >= 0 ? value * 2 : -value * 2 - 1);
return;
Expand Down
31 changes: 31 additions & 0 deletions javascript/test/writer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import { OwnershipError } from "../packages/core/lib/error";
import { BinaryReader } from "../packages/core/lib/reader";
import { BinaryWriter } from "../packages/core/lib/writer";
import { describe, expect, test } from "@jest/globals";

Expand Down Expand Up @@ -63,4 +64,34 @@ describe("writer", () => {
}
throw new Error("unreachable code");
});

test("varint64 round-trips negative values beyond 2^52", () => {
// The number fast path computes zigzag as -v * 2 - 1 in doubles; below
// -(2^52) that intermediate rounds to an even neighbor and flips the
// decoded sign, so such values must use the exact bigint zigzag path.
const values: (bigint | number)[] = [
-(2n ** 52n),
-(2n ** 52n + 1n),
-(2n ** 53n - 1n),
-4503599627370497,
2n ** 53n - 1n,
-1n,
0,
];
for (const value of values) {
const writer = new BinaryWriter({});
writer.writeVarInt64(value);
const reader = new BinaryReader({});
reader.reset(writer.dump());
expect(reader.readVarInt64()).toBe(BigInt(value));
}
});

test("sliInt64 round-trips negative values beyond 2^52", () => {
const writer = new BinaryWriter({});
writer.writeSliInt64(-(2n ** 52n + 1n));
const reader = new BinaryReader({});
reader.reset(writer.dump());
expect(reader.readSliInt64()).toBe(-(2n ** 52n + 1n));
});
});
Loading