package optint
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=774901af130eacc08e30ae43e9a18c926f284c22e3c66c2ff95e74648fbedf26
sha512=fe5762ee7a1a08c7b389fbef9188f8a59c40bb8e19535cec1661a789f973338d5b6d4b30c1869e089282242efa63bfc86c1fb15224b47c05cb5277971aa14a35
README.md.html
Optint - Abstract integer types between x64 and x86 architectures
This library provides two new integer types, Optint.t
and Int63.t
, which guarantee efficient representation on x64 architectures and provide a best-effort boxed representation on x86 architectures.
Goal
The standard Int32.t
and Int64.t
types provided by the standard library have the same heap-allocated representation on all architectures. This consistent representation has costs in both memory and run-time performance.
On 64-bit architectures, it's often more efficient to use the native Int.t
directly, and fallback to the boxed representations on 32-bit architectures. This library provides types to do exactly this:
Optint.t
: an integer containing at least 32 bits. On 64-bit machines, this is an immediate integer; on 32-bit machines, it is a boxed 32-bit value. The overflow behaviour is platform-dependent.Int63.t
: an integer containing exactly 63 bits. On 64-bit machines, this is an immediate integer; on 32-bit machines, it is a boxed 64-bit integer that is wrapped to provide 63-bit two's complement semantics. The two implementations are observationally equivalent, modulo use ofMarshal
andObj
.
In summary:
Integer type | x86 representation | x64 representation | Semantics |
---|---|---|---|
Stdlib.Int.t |
31-bit immediate ✅ | 63-bit immediate ✅ | Always immediate |
Stdlib.Nativeint.t |
64-bit boxed ❌ | 32-bit boxed ❌ | Exactly word size |
Stdlib.Int32.t |
32-bit boxed ❌ | 32-bit boxed ❌ | Exactly 32 bits |
Stdlib.Int64.t |
64-bit boxed ❌ | 64-bit boxed ❌ | Exactly 64 bits |
Optint.t (new) |
32-bit boxed ❌ | 63-bit immediate ✅ | At least 32 bits |
Int63.t (new) |
64-bit boxed ❌ | 63-bit immediate ✅ | Exactly 63 bits |
These new types are safe and well-tested, but their architecture-dependent implementation makes them unsuitable for use with the Marshal
module. Use the provided encode and decode functions instead.