package checked_oint
Install
dune-project
Dependency
Authors
Maintainers
Sources
md5=179e365805c0358880a88813772bd13a
sha512=6170bebf27fb26f88235bfc678abc2741b3da8b6ff6b8114b8491a6da86f7f618ff1eb98dd08c6813ba7205e20f65931ccb8b45d5b8bd32dbc064659cb8a59eb
Description
Added to opam-repository:
README
checked_oint
checked_oint is an OCaml library for checked integer arithmetic. We support the full set of signed and unsigned integers of bitnesses 8, 16, 32, 64, and 128. In some applications, the exact types of integers may be unknown at compile-time; we thus also provide a proper escape hatch based on existential types.
Installation
$ opam install checked_ointUsage
open Checked_oint
let () =
let x = U8.of_int_exn 50 in
let y = U8.of_int_exn 70 in
assert (U8.equal (U8.add_exn x y) (U8.of_int_exn 120));
assert (Option.is_none (U8.mul x y))You can find the API documentation here.
Polymorphic comparison
Polymorphic comparison operators (Stdlib.( = ), Stdlib.compare, etc.) can compute wrong results on checked integers, because they compare internal representations instead of the semantic values. To protect against accidental misuse, every integer carries a special "guard" value that makes polymorphic comparisons raise Invalid_argument. We recommend always using the monomorphic operations such as S.equal and S.compare, because they are both safe and fast.
The guard costs one allocation of a pair and a guard value per integer. To avoid this overhead, link your project against the checked_oint.guard-off library:
(executable
; ...
(libraries checked_oint checked_oint.guard-off))Libraries should only depend on checked_oint, leaving the choice of the guard implementation to final executables.
With this guard off, integers are represented with zero overhead, but polymorphic comparison operators silently succeed, possibly returning meaningless results.
Implementation
u8,u16,i8, andi16are represented asintinternally.u32andi32(resp.u64andi64) are represented asint32(resp.int64) internally.u128andi128are represented as{ high : int64; low : int64 }internally.- Operations on integers of 8, 16, 32, and 64 bits are implemented primarily in OCaml, save a small amount of C stub functions.
Operations on 128-bit integers are implemented solely in C.
- We heavily rely on the
__int128extension by GCC and Clang.
- We heavily rely on the
Release procedure
- Update the
versionfield indune-project. - Type
dune buildto generatechecked_oint.opam. - Update
CHANGELOG.md. Release the project in GitHub Releases.
- Generate a source code archive and include it in the release:
git archive HEAD -o checked_oint-<major>.<minor>.<patch>.tar.gz
- Generate a source code archive and include it in the release:
Type
git pull && opam publish.- Specify the correct archive URL and checksums (by running
md5sumandsha512sumon the archive).
- Specify the correct archive URL and checksums (by running
On the stability of source code archives
Including the output of git archive is needed because GitHub does not guarantee stability of source code archives. With a static asset, we will always get the same checksum, which is crucial for packaging.
Dependencies (4)
- ppx_enumerate
- ppx_deriving
-
dune
>= "3.14" -
ocaml
>= "4.13"
Used by
None
Conflicts
None