Skip to content

Comments

feat(dart): add float16 to dart#3336

Merged
chaokunyang merged 17 commits intoapache:mainfrom
ayush00git:feat/dart-float16
Feb 18, 2026
Merged

feat(dart): add float16 to dart#3336
chaokunyang merged 17 commits intoapache:mainfrom
ayush00git:feat/dart-float16

Conversation

@ayush00git
Copy link
Contributor

@ayush00git ayush00git commented Feb 13, 2026

Why?

As requested in Issue #3209, Dart implementation was missing support for IEEE 754 half-precision floating-point (Float16) types that are already available in other Fory language implementations (Rust, Go, C++, Python). This PR adds complete Float16 support for Dart.

What does this PR do?

This PR implements schema-based support for IEEE 754 Float16 types in the Fory Dart library:

Added Types

  • float16

Changes Made

1. lib/src/datatype/float16.dart

Implemented the Float16 class which wraps a 16-bit integer and handles IEEE 754 conversions (handling rounding, subnormals, signed zeros, NaN/Infinity):

final class Float16 extends FixedNum {
  final int _bits;
  // ...
  factory Float16(num value) => Float16.fromDouble(value.toDouble());
  double toDouble() => _bitsToDouble(_bits);
}

2. lib/src/memory/

Added read/write support for 16-bit floats in ByteReader and ByteWriter:

void writeFloat16(Float16 value);
Float16 readFloat16();

3. lib/src/serializer/

Registered Float16Serializer to handle serialization of Float16 types in PrimitiveTypeSerializer:

typeToTypeInfo[Float16]!.serializer = Float16Serializer.cache.getSerializer(conf);

4. lib/src/const/dart_type.dart

Added FLOAT16 to DartTypeEnum for type inference and registry.

5. test/cross_lang_test/

Updated cross-language tests to fix lint issues and build errors:

  • Renamed enum constants in xlang_test_defs.dart and xlang_test_main.dart to follow Dart's lowerCamelCase convention (e.g., VALUE_A -> valueA).

Usage Example

import 'package:fory/src/datatype/float16.dart'; 
import 'package:fory/src/memory/byte_writer.dart';

var val = Float16(1.5);
print(val.toDouble())

// Handling limits and edge cases
var max = Float16(65504.0);
var inf = Float16(65505.0);
var subnormal = Float16(0.0000000596046);

// Serialization
var bw = ByteWriter();
bw.writeFloat16(val);

Related issues

Does this PR introduce any user-facing change?

  • Does this PR introduce any public API change?

    • Yes: Adds Float16 class and corresponding NumType.float16.
    • Yes: Adds readFloat16 / writeFloat16 to memory interfaces.
    • Yes: Exports Float16 for user consumption.
  • Does this PR introduce any binary protocol compatibility change?

    • Yes: Adds support for TypeId 17 (Float16).
    • Cross-language compatible: Uses the same TypeId values as other Fory implementations.

Benchmark

N/A

@ayush00git
Copy link
Contributor Author

Hey @chaokunyang
Have a look at the implementation and let me know your reviews

@chaokunyang
Copy link
Collaborator

chaokunyang commented Feb 17, 2026

@ayush00git I removed dart/packages/fory-test/test/cross_lang_test/xlang_test_defs.dart, could you address conflict?

@ayush00git
Copy link
Contributor Author

@ayush00git I removed dart/packages/fory-test/test/cross_lang_test/xlang_test_defs.dart, could you address conflict?

Hii @chaokunyang
Have a look and let me know

Copy link
Collaborator

@chaokunyang chaokunyang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@chaokunyang chaokunyang merged commit db529f1 into apache:main Feb 18, 2026
57 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Dart] add float16 to dart

2 participants