Skip to content
Open
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
4 changes: 4 additions & 0 deletions javascript/packages/fory/lib/gen/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ class BinaryWriterBuilder {
return `${this.holder}.float32(${v})`;
}

float16(v: number | string) {
return `${this.holder}.float16(${v})`;
}

int64(v: number | string) {
return `${this.holder}.int64(${v})`;
}
Expand Down
2 changes: 1 addition & 1 deletion javascript/packages/fory/lib/gen/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ CodegenRegistry.register(TypeId.TAGGED_INT64,
);
CodegenRegistry.register(TypeId.FLOAT16,
buildNumberSerializer(
(builder, accessor) => builder.writer.float32(accessor),
(builder, accessor) => builder.writer.float16(accessor),
builder => builder.reader.float16()
)
);
Expand Down
8 changes: 3 additions & 5 deletions javascript/packages/fory/lib/writer/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ export function toFloat16(value: number) {
}

if (exponent < -14) {
return sign | 0x3ff; // returns ±max subnormal
}

if (exponent <= 0) {
return sign | ((significand | 0x800000) >> (1 - exponent + 10));
// subnormal
// shift amount = 13 - 14 - exponent = -1 - exponent
return sign | ((significand | 0x800000) >> (13 - 14 - exponent));
}

return sign | ((exponent + 15) << 10) | (significand >> 13);
Expand Down
29 changes: 24 additions & 5 deletions javascript/test/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import * as beautify from 'js-beautify';

describe('array', () => {
test('should array work', () => {


const typeinfo = Type.struct({
typeName: "example.bar"
Expand All @@ -35,9 +35,9 @@ describe('array', () => {
}))
});
const fory = new Fory({ refTracking: true, hooks: {
afterCodeGenerated: (code: string) => {
return beautify.js(code, { indent_size: 2, space_in_empty_paren: true, indent_empty_lines: true });
}
afterCodeGenerated: (code: string) => {
return beautify.js(code, { indent_size: 2, space_in_empty_paren: true, indent_empty_lines: true });
}
} });
const { serialize, deserialize } = fory.registerSerializer(typeinfo);
const o = { a: "123" };
Expand Down Expand Up @@ -82,7 +82,7 @@ describe('array', () => {
}, {
a5: Type.float32Array(),
})

const fory = new Fory({ refTracking: true }); const serialize = fory.registerSerializer(typeinfo).serializer;
const input = fory.serialize({
a5: [2.43, 654.4, 55],
Expand All @@ -94,6 +94,25 @@ describe('array', () => {
expect(result.a5[1]).toBeCloseTo(654.4)
expect(result.a5[2]).toBeCloseTo(55)
});

test('should float16Array work', () => {
const typeinfo = Type.struct({
typeName: "example.foo"
}, {
a6: Type.float16Array(),
})

const fory = new Fory({ refTracking: true }); const serialize = fory.registerSerializer(typeinfo).serializer;
const input = fory.serialize({
a6: [1.5, 2.5, -4.5],
}, serialize);
const result = fory.deserialize(
input
);
expect(result.a6[0]).toBeCloseTo(1.5, 1)
expect(result.a6[1]).toBeCloseTo(2.5, 1)
expect(result.a6[2]).toBeCloseTo(-4.5, 1)
});
});


41 changes: 28 additions & 13 deletions javascript/test/number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import { describe, expect, test } from '@jest/globals';

describe('number', () => {
test('should i8 work', () => {
const fory = new Fory({ refTracking: true });

const fory = new Fory({ refTracking: true });
const serialize = fory.registerSerializer(Type.struct({
typeName: "example.foo"
}, {
Expand All @@ -36,8 +36,8 @@ describe('number', () => {
expect(result).toEqual({ a: 1 })
});
test('should i16 work', () => {
const fory = new Fory({ refTracking: true });

const fory = new Fory({ refTracking: true });
const serialize = fory.registerSerializer(Type.struct({
typeName: "example.foo"
}, {
Expand All @@ -50,8 +50,8 @@ describe('number', () => {
expect(result).toEqual({ a: 1 })
});
test('should i32 work', () => {
const fory = new Fory({ refTracking: true });

const fory = new Fory({ refTracking: true });
const serializer = fory.registerSerializer(Type.struct({
typeName: "example.foo"
}, {
Expand All @@ -64,8 +64,8 @@ describe('number', () => {
expect(result).toEqual({ a: 1 })
});
test('should i64 work', () => {
const fory = new Fory({ refTracking: true });

const fory = new Fory({ refTracking: true });
const serializer = fory.registerSerializer(Type.struct({
typeName: "example.foo"
}, {
Expand All @@ -79,9 +79,9 @@ describe('number', () => {
expect(result).toEqual({ a: 1 })
});

test('should float work', () => {
const fory = new Fory({ refTracking: true });
test('should float32 work', () => {

const fory = new Fory({ refTracking: true });
const serializer = fory.registerSerializer(Type.struct({
typeName: "example.foo"
}, {
Expand All @@ -94,8 +94,8 @@ describe('number', () => {
expect(result.a).toBeCloseTo(1.2)
});
test('should float64 work', () => {
const fory = new Fory({ refTracking: true });

const fory = new Fory({ refTracking: true });
const serializer = fory.registerSerializer(Type.struct({
typeName: "example.foo"
}, {
Expand All @@ -108,6 +108,21 @@ describe('number', () => {
expect(result.a).toBeCloseTo(1.2)
});

test('should float16 work', () => {

const fory = new Fory({ refTracking: true });
const serializer = fory.registerSerializer(Type.struct({
typeName: "example.foo"
}, {
a: Type.float16()
})).serializer;
const input = fory.serialize({ a: 1.2 }, serializer);
const result = fory.deserialize(
input
);
expect(result.a).toBeCloseTo(1.2, 1)
});

test('should uint8 work', () => {
const fory = new Fory({ refTracking: true });
const serializer = fory.registerSerializer(Type.struct({
Expand Down
Loading