Created
March 31, 2025 08:47
-
-
Save dbuenzli/e1526339ad9951796b0136cb1c1d4d2c to your computer and use it in GitHub Desktop.
OCaml integers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(*--------------------------------------------------------------------------- | |
Copyright (c) 2018 Daniel C. Bünzli. All rights reserved. | |
SPDX-License-Identifier: ISC | |
---------------------------------------------------------------------------*) | |
(** Integer types. | |
[Ints] provides types for specifying sized integer types not | |
supported by the {!Stdlib}. It also provides bitwise conversion | |
functions and safe, magnitude preserving, conversions. | |
Safe magnitude preserving functions always ensure that the {e | |
signed} magnitude of the converted integer is accurately | |
represented in the result or [None] is returned. If the conversion | |
is guaranteed to succeed, the result is not wrapped in an option. *) | |
(** {1:ints Integers types} *) | |
type sub_uint8 = int | |
(** The type for unsigned n-bit integers with [n < 8]. *) | |
type uint8 = int | |
(** The type for unsigned 8-bit integers. *) | |
type int8 = int | |
(** The type for signed 8-bit integers. *) | |
type uint16 = int | |
(** The type for unsigned 16-bit integers. *) | |
type int16 = int | |
(** The type for signed 16-bit integers. *) | |
type uint32 = int32 | |
(** The type for unsigned 32-bit integers. *) | |
type uint64 = int64 | |
(** The type for unsigned 64-bit integers. *) | |
(** {1:conversions Conversions} *) | |
(** {2:intconv To [int]} *) | |
val int_of_int32 : int32 -> int option | |
(** [int_of_int32 i] is [i] as an integer. *) | |
val int_of_uint32 : uint32 -> int option | |
(** [int_of_uint32 i] is [i] as an integer. *) | |
val int_of_int64 : int64 -> int option | |
(** [int_of_int64 i] is [i] as an integer. *) | |
val int_of_uint64 : uint64 -> int option | |
(** [int_of_uint64 i] is [i] as an integer. *) | |
(** {2:int8conv To [int8]} *) | |
val int8_of_uint8_bits : uint8 -> int8 | |
(** [int8_of_uint8_bits i] is the two complement's signed 8-bit | |
integer corresponding to the bits of the unsigned 8-bit integer | |
[i]. The function assumes [i] is in the range \[[0];[255]\] and is | |
undefined otherwise. The result is in the range | |
\[[-128];[127]\]. *) | |
(** {2:uint8conv To [uint8]} *) | |
val uint8_of_int8_bits : int8 -> uint8 | |
(** [uint8_of_int8_bits i] is the unsigned 8-bit integer corresponding | |
to the bits of the two complement's signed 8-bit integer [i]. The | |
function assumes [i] is in the range \[[-128];[127]\] and is | |
undefined otherwise. The result is in the range \[[0];[255]\]. *) | |
(** {2:int16conv To [int16]} *) | |
val int16_of_uint16_bits : uint16 -> int16 | |
(** [int16_of_uint16_bits i] is the two complement's signed 16-bit | |
integer corresponding to the bits of the unsigned 16-bit integer | |
[i]. The function assumes [i] is in the range \[[0];[65535]\] and is | |
undefined otherwise. The result is in the range | |
\[[-32768];[32767]\]. *) | |
(** {2:uint16conv To [uint16]} *) | |
val uint16_of_int16_bits : int16 -> uint16 | |
(** [uint16_of_int16_bits i] is the unsigned 16-bit integer corresponding | |
to the bits of the two complement's signed 16-bit integer [i]. The | |
function assumes [i] is in the range \[[-32768];[32767]\] and is | |
undefined otherwise. The result is in the range \[[0];[65535]\]. *) | |
(** {2:int32conv To [int32]} *) | |
val int32_of_int : int -> int32 option | |
(** [int32_of_int i] is [i] as a 32-bit integer. *) | |
val int32_of_uint32 : uint32 -> int32 option | |
(** [int32_of_uint32 i] is [i] as a 32-bit integer. *) | |
val int32_of_int64 : int64 -> int32 option | |
(** [int32_of_int64 i] is [i] as a 32-bit integer. *) | |
val int32_of_uint64 : uint64 -> int32 option | |
(** [int32_of_uint64 i] is [i] as a 32-bit integer. *) | |
(** {2:uint32conv To [uint32]} *) | |
val uint32_of_int : int -> uint32 option | |
(** [uint32_of_int i] is [i] as a 32-bit unsigned integer. *) | |
val uint32_of_int32 : int32 -> uint32 option | |
(** [uint32_of_int32 i] is [i] as a 32-bit unsigned integer. *) | |
val uint32_of_int64 : int64 -> uint32 option | |
(** [uint32_of_int64 i] is [i] as a 32-bit unsigned integer. *) | |
val uint32_of_uint64 : uint64 -> uint32 option | |
(** [uint32_of_uint64 i] is [i] as a 32-bit unsigned integer. *) | |
(** {2:int64conv To [int64]} *) | |
val int64_of_int : int -> int64 | |
(** [int64_of_int i] is [i] as a 64-bit integer. *) | |
val int64_of_int32 : int32 -> int64 | |
(** [int64_of_int32 i] is [i] as a 64-bit integer. *) | |
val int64_of_uint32 : uint32 -> int64 | |
(** [int64_of_uint64 i] is [i] as a 64-bit integer. *) | |
val int64_of_uint64 : uint64 -> int64 option | |
(** [int64_of_uint64 i] is [i] as a 64-bit integer. *) | |
(** {2:uint64conv To [uint64]} *) | |
val uint64_of_int : int -> uint64 option | |
(** [uint64_of_int i] is [i] as an unsigned 64-bit integer. *) | |
val uint64_of_int32 : int32 -> uint64 option | |
(** [uint64_of_int32 i] is [i] as an unsigned 64-bit integer. *) | |
val uint64_of_uint32 : uint32 -> uint64 | |
(** [uint64_of_uint64 i] is [i] as an unsigned 64-bit integer. *) | |
val uint64_of_int64 : int64 -> uint64 option | |
(** [uint64_of_int64 i] is [i] as an unsigned 64-bit integer. *) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment