-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathint.ts
32 lines (26 loc) · 1.04 KB
/
int.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Integer Utility
export const UINT32_MAX = 0xffff_ffff;
// DataView extension to handle int64 / uint64,
// where the actual range is 53-bits integer (a.k.a. safe integer)
export function setUint64(view: DataView, offset: number, value: number): void {
const high = value / 0x1_0000_0000;
const low = value; // high bits are truncated by DataView
view.setUint32(offset, high);
view.setUint32(offset + 4, low);
}
export function setInt64(view: DataView, offset: number, value: number): void {
const high = Math.floor(value / 0x1_0000_0000);
const low = value; // high bits are truncated by DataView
view.setUint32(offset, high);
view.setUint32(offset + 4, low);
}
export function getInt64(view: DataView, offset: number): number {
const high = view.getInt32(offset);
const low = view.getUint32(offset + 4);
return high * 0x1_0000_0000 + low;
}
export function getUint64(view: DataView, offset: number): number {
const high = view.getUint32(offset);
const low = view.getUint32(offset + 4);
return high * 0x1_0000_0000 + low;
}