1 | import { write, read } from "@xtuc/ieee754";
|
---|
2 | /**
|
---|
3 | * According to https://webassembly.github.io/spec/binary/values.html#binary-float
|
---|
4 | * n = 32/8
|
---|
5 | */
|
---|
6 |
|
---|
7 | export var NUMBER_OF_BYTE_F32 = 4;
|
---|
8 | /**
|
---|
9 | * According to https://webassembly.github.io/spec/binary/values.html#binary-float
|
---|
10 | * n = 64/8
|
---|
11 | */
|
---|
12 |
|
---|
13 | export var NUMBER_OF_BYTE_F64 = 8;
|
---|
14 | export var SINGLE_PRECISION_MANTISSA = 23;
|
---|
15 | export var DOUBLE_PRECISION_MANTISSA = 52;
|
---|
16 | export function encodeF32(v) {
|
---|
17 | var buffer = [];
|
---|
18 | write(buffer, v, 0, true, SINGLE_PRECISION_MANTISSA, NUMBER_OF_BYTE_F32);
|
---|
19 | return buffer;
|
---|
20 | }
|
---|
21 | export function encodeF64(v) {
|
---|
22 | var buffer = [];
|
---|
23 | write(buffer, v, 0, true, DOUBLE_PRECISION_MANTISSA, NUMBER_OF_BYTE_F64);
|
---|
24 | return buffer;
|
---|
25 | }
|
---|
26 | export function decodeF32(bytes) {
|
---|
27 | var buffer = Buffer.from(bytes);
|
---|
28 | return read(buffer, 0, true, SINGLE_PRECISION_MANTISSA, NUMBER_OF_BYTE_F32);
|
---|
29 | }
|
---|
30 | export function decodeF64(bytes) {
|
---|
31 | var buffer = Buffer.from(bytes);
|
---|
32 | return read(buffer, 0, true, DOUBLE_PRECISION_MANTISSA, NUMBER_OF_BYTE_F64);
|
---|
33 | } |
---|