forked from msgpack/msgpack-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathint.ts
More file actions
28 lines (24 loc) · 984 Bytes
/
int.ts
File metadata and controls
28 lines (24 loc) · 984 Bytes
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
// 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) {
const high = view.getInt32(offset);
const low = view.getUint32(offset + 4);
return high * 0x1_0000_0000 + low;
}
export function getUint64(view: DataView, offset: number) {
const high = view.getUint32(offset);
const low = view.getUint32(offset + 4);
return high * 0x1_0000_0000 + low;
}