dart-lang / dart-lang/language
Primitive types are too large, signed, and lack precision guarantees
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
Both `dart:ffi` and `dart:typed_data` expose differently sized storage for numeric types. However precision is only lost when transition from/to these storage areas; there is no way to maintain this imprecision within a Dart program. This results in bugs & unexpected behaviors. It also regresses runtime performance and memory usage because VM integers are assumed to be 64-bit, even where 32-bit or 16-bit operations would be more beneficial.
Having types that match the naming scheme of `typed_data` would help. To avoid accidental type punning the types could expose `toInt/toInt{prec}` and `toDouble/toDouble{prec}` methods, and `fromInt/fromInt{prec}` and `fromDouble/fromDouble{prec}` constructors. This would also mean these precise types do not extend `num`.
For floating point operations, the implementation could keep the 64-bit precision throughout all operations, but then store unboxed as the requested precision. This matches how Clang implements `__fp16` precision. Truncation between operations may be [prohibitively expensive](https://github.com/llvm/llvm-project/blob/e83e8e271169f06e78cd1b6c2bab5e2926f29ff1/compiler-rt/lib/builtins/fp_trunc_impl.inc#L41) (as is done with `_Float16`).
For integer types, the implementation could avoid truncation until observed. `int64` may end up using `BigInt` for JavaScript compilation so it should probably be truncated after each operation, but this is fast with `BigInt.asIntN/asUintN`. Unsigned integer types could be an extension type around the normal signed type.
This will also mean the discrepancy between the VM's 64-bit ints and JS's 52-bit ints would (mostly) disappear.
Contributor guide
Research direction
The issue names no file, test, or entry point. Start by reviewing the Dart language specification and the VM and JavaScript compilation concerns described here; done would require an agreed design for precise integer and floating-point types, conversions, storage, and operation semantics.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100