Module Base.Nativeint

Processor-native integers.

type t = nativeint
val globalize : t @ local -> t
include Int.S with type t := t
include Sexplib0.Sexpable.S__stack with type t := t
include Sexplib0.Sexpable.Of_sexp with type t := t
include Sexplib0.Sexpable.Sexp_of__stack with type t := t
val t_sexp_grammar : t Sexplib0.Sexp_grammar.t @@ portable
include Floatable.S_local_input with type t := t
val of_float : float @ local -> t
val to_float : t @ local -> float
include Intable.S with type t := t
val of_int_exn : int -> t
val to_int_exn : t @ local -> int
include Identifiable.S__local__portable with type t := t
include Ppx_hash_lib.Hashable.S_any with type t := t
val hash_fold_t : t Ppx_hash_lib.hash_fold
include Sexplib0.Sexpable.S with type t := t
include Sexplib0.Sexpable.Of_sexp with type t := t
val t_of_sexp : Sexplib0.Sexp.t -> t
include Sexplib0.Sexpable.Sexp_of with type t := t
val sexp_of_t : t -> Sexplib0.Sexp.t
include Stringable.S with type t := t
include Comparable.S__local__portable with type t := t
include Comparisons.S__local with type t := t
include Comparisons.Infix with type t := t
val between : t -> low:t -> high:t -> bool @@ portable

between t ~low ~high means low <= t <= high

val clamp_exn : t -> min:t -> max:t -> t @@ portable

clamp_exn t ~min ~max returns t', the closest value to t such that between t' ~low:min ~high:max is true.

Raises if not (min <= max).

val clamp : t -> min:t -> max:t -> t Or_error.t @@ portable
include Comparator.S__portable with type t := t
type comparator_witness
include Pretty_printer.S with type t := t
val pp : Formatter.t -> t -> unit
val hashable : t Hashable.t
include Stringable.S_local_input with type t := t
val of_string : string @ local -> t
val to_string : t @ local -> string
include Comparable.With_zero with type t := t
val is_positive : t -> bool
val is_non_negative : t -> bool
val is_negative : t -> bool
val is_non_positive : t -> bool
val sign : t -> Base__.Sign0.t

Returns Neg, Zero, or Pos in a way consistent with the above functions.

include Invariant.S with type t := t
val invariant : t -> unit
module Hex : sig ... end
module Binary : sig ... end
val of_string_opt : string @ local -> t option
val to_string_hum : ?delimiter:char -> t @ local -> string

delimiter is an underscore by default.

Infix operators and constants

val zero : t
val one : t
val minus_one : t
val (+) : t @ local -> (t @ local -> t) @ local
val (-) : t @ local -> (t @ local -> t) @ local
val (*) : t @ local -> (t @ local -> t) @ local
val (**) : t @ local -> (t @ local -> t) @ local

Integer exponentiation

Negation

val neg : t @ local -> t
val (~-) : t @ local -> t

There are two pairs of integer division and remainder functions, /% and %, and / and rem. They both satisfy the same equation relating the quotient and the remainder:

  x = (x /% y * y) + (x % y);
  x = (x / y * y) + rem x y

The functions return the same values if x and y are positive. They all raise if y = 0.

The functions differ if x < 0 or y < 0.

If y < 0, then % and /% raise, whereas / and rem do not.

x % y always returns a value between 0 and y - 1, even when x < 0. On the other hand, rem x y returns a negative value if and only if x < 0; that value satisfies abs (rem x y) <= abs y - 1.

val (/%) : t @ local -> (t @ local -> t) @ local
val (%) : t @ local -> (t @ local -> t) @ local
val (/) : t @ local -> (t @ local -> t) @ local
val rem : t @ local -> (t @ local -> t) @ local
val (//) : t @ local -> (t @ local -> float) @ local

Float division of integers.

val (land) : t @ local -> (t @ local -> t) @ local

Same as bit_and.

val (lor) : t @ local -> (t @ local -> t) @ local

Same as bit_or.

val (lxor) : t @ local -> (t @ local -> t) @ local

Same as bit_xor.

val lnot : t @ local -> t

Same as bit_not.

val (lsl) : t @ local -> (int -> t) @ local

Same as shift_left.

val (asr) : t @ local -> (int -> t) @ local

Same as shift_right.

Other common functions

round rounds an int to a multiple of a given to_multiple_of argument, according to a direction dir, with default dir being `Nearest. round will raise if to_multiple_of <= 0. If the result overflows (too far positive or too far negative), round returns an incorrect result.

| `Down    | rounds toward Int.neg_infinity                          |
| `Up      | rounds toward Int.infinity                              |
| `Nearest | rounds to the nearest multiple, or `Up in case of a tie |
| `Zero    | rounds toward zero                                      |

Here are some examples for round ~to_multiple_of:10 for each direction:

| `Down    | {10 .. 19} --> 10 | { 0 ... 9} --> 0 | {-10 ... -1} --> -10 |
| `Up      | { 1 .. 10} --> 10 | {-9 ... 0} --> 0 | {-19 .. -10} --> -10 |
| `Zero    | {10 .. 19} --> 10 | {-9 ... 9} --> 0 | {-19 .. -10} --> -10 |
| `Nearest | { 5 .. 14} --> 10 | {-5 ... 4} --> 0 | {-15 ... -6} --> -10 |

For convenience and performance, there are variants of round with dir hard-coded. If you are writing performance-critical code you should use these.

val round : ?dir:[ `Zero | `Nearest | `Up | `Down ] @ local -> (t @ local -> (to_multiple_of:t @ local -> t) @ local) @ local
val round_towards_zero : t @ local -> (to_multiple_of:t @ local -> t) @ local
val round_down : t @ local -> (to_multiple_of:t @ local -> t) @ local
val round_up : t @ local -> (to_multiple_of:t @ local -> t) @ local
val round_nearest : t @ local -> (to_multiple_of:t @ local -> t) @ local
val abs : t -> t

Returns the absolute value of the argument. May be negative if the input is min_value.

val abs_local : t @ local -> t @ local

Like abs, but for locally-allocated values. If the argument is already positive, then this is the identity (as is abs itself).

Successor and predecessor functions

val succ : t @ local -> t
val pred : t @ local -> t

Exponentiation

val pow : t @ local -> (t @ local -> t) @ local

pow base exponent returns base raised to the power of exponent. It is OK if base <= 0. pow raises if exponent < 0, or an integer overflow would occur.

Bit-wise logical operations

val bit_and : t @ local -> (t @ local -> t) @ local

These are identical to land, lor, etc. except they're not infix and have different names.

val bit_or : t @ local -> (t @ local -> t) @ local
val bit_xor : t @ local -> (t @ local -> t) @ local
val bit_not : t @ local -> t
val popcount : t @ local -> t

Returns the number of 1 bits in the binary representation of the input.

Bit-shifting operations

The results are unspecified for negative shifts and shifts >= num_bits.

val shift_left : t @ local -> (int -> t) @ local

Shifts left, filling in with zeroes.

val shift_right : t @ local -> (int -> t) @ local

Shifts right, preserving the sign of the input.

Increment and decrement functions for integer references

val decr : t ref @ local -> unit
val incr : t ref @ local -> unit
val of_int32_exn : int32 -> t
val to_int32_exn : t -> int32
val of_int64_exn : int64 -> t
val to_int64 : t -> int64
val of_nativeint_exn : nativeint -> t
val to_nativeint_exn : t -> nativeint
val of_local_int32_exn : int32 @ local -> t @ local
val to_local_int32_exn : t @ local -> int32 @ local
val of_local_int64_exn : int64 @ local -> t @ local
val to_local_int64 : t @ local -> int64 @ local
val of_local_nativeint_exn : nativeint @ local -> t @ local
val to_local_nativeint_exn : t @ local -> nativeint @ local
val of_float_unchecked : float @ local -> t

of_float_unchecked truncates the given floating point number to an integer, rounding towards zero. The result is unspecified if the argument is nan or falls outside the range of representable integers.

module Summable : sig ... end
val num_bits : t

The number of bits available in this integer type. Note that the integer representations are signed.

val max_value : t

The largest representable integer.

val min_value : t

The smallest representable integer.

val (lsr) : t @ local -> (int -> t) @ local

Same as shift_right_logical.

val shift_right_logical : t @ local -> (int -> t) @ local

Shifts right, filling in with zeroes, which will not preserve the sign of the input.

val ceil_pow2 : t @ local -> t

ceil_pow2 x returns the smallest power of 2 that is greater than or equal to x. The implementation may only be called for x > 0. Example: ceil_pow2 17 = 32

val floor_pow2 : t @ local -> t

floor_pow2 x returns the largest power of 2 that is less than or equal to x. The implementation may only be called for x > 0. Example: floor_pow2 17 = 16

val ceil_log2 : t @ local -> t

ceil_log2 x returns the ceiling of log-base-2 of x, and raises if x <= 0.

val floor_log2 : t @ local -> t

floor_log2 x returns the floor of log-base-2 of x, and raises if x <= 0.

val is_pow2 : t @ local -> bool

is_pow2 x returns true iff x is a power of 2. is_pow2 raises if x <= 0.

val clz : t @ local -> t

Returns the number of leading zeros in the binary representation of the input, as an integer between 0 and one less than num_bits.

The results are unspecified for t = 0.

val ctz : t @ local -> t

Returns the number of trailing zeros in the binary representation of the input, as an integer between 0 and one less than num_bits.

The results are unspecified for t = 0.

module O : sig ... end

A sub-module designed to be opened to make working with ints more convenient.

val (=) : t -> t -> bool @@ portable
val (<>) : t -> t -> bool @@ portable
val (<) : t -> t -> bool @@ portable
val (>) : t -> t -> bool @@ portable
val (<=) : t -> t -> bool @@ portable
val (>=) : t -> t -> bool @@ portable
val compare : t -> t -> int @@ portable
val compare__local : t -> t -> int @@ portable
val equal : t -> t -> bool @@ portable
val equal__local : t -> t -> bool @@ portable
val ascending : t -> t -> int @@ portable
val descending : t -> t -> int @@ portable
val max : t -> t -> t @@ portable
val min : t -> t -> t @@ portable

Conversion functions

val of_int : int -> t @@ portable
val to_int : t @ local -> int option @@ portable
val of_int32 : int32 @ local -> t @@ portable
val to_int32 : t @ local -> int32 option @@ portable
val of_nativeint : nativeint -> t @@ portable
val to_nativeint : t -> nativeint @@ portable
val of_int64 : int64 @ local -> t option @@ portable

Truncating conversions

These functions return the least-significant bits of the input. In cases where optional conversions return Some x, truncating conversions return x.

val to_int_trunc : t @ local -> int @@ portable
val to_int32_trunc : t @ local -> int32 @@ portable
val of_int64_trunc : int64 @ local -> t @@ portable

Byte swap functions

See Int's byte swap section for a description of Base's approach to exposing byte swap primitives.

val bswap : t @ local -> t @@ portable