Targetint(3)

πŸŽ€ Targetint πŸ’… OCaml library β€” section 3 of the manual, bestie 🫢

okay so picture this: you're in the Targetint(3) page of the OCaml library docs and it's literally all about target processor-native integers. like, the ints that actually match the machine you're compiling for, not the one you're sitting at. iconic behaviour honestly ✨


✨ NAME ✨

Targetint β€” target processor-native integers, and yes that's the whole vibe πŸ’«


πŸ¦‹ Module πŸ¦‹

Module Targetint

Module Targetint
 : sig end

Target processor-native integers.

So here's the tea πŸ“ this module gives you operations on the type of signed 32-bit integers (on 32-bit target platforms) or signed 64-bit integers (on 64-bit target platforms). this integer type has exactly the same width as that of a pointer type in the C compiler β€” same width, no cap.

and every arithmetic operation over them is taken modulo 2^32 or 2^64 depending on the word size of the target architecture. so it wraps, it doesn't cry, it just wraps πŸ’…

⚠️ Warning: this module is unstable and part of Compiler_libs. ngl, unstable means unstable β€” don't build your whole personality on it πŸ™ˆ

🌸 Documentation 🌸

🧚 the types & values, one by one

  • type t β€” The type of target integers. that's her. the main character πŸ‘‘
  • val zero : t β€” The target integer 0. the beginning of everything πŸŽ€
  • val one : t β€” The target integer 1. her bestie.
  • val minus_one : t β€” The target integer -1. the edgy one πŸ’€
  • val neg : t -> t β€” Unary negation. flip the sign, flip the vibe.
  • val add : t -> t -> t β€” Addition. math slay.
  • val sub : t -> t -> t β€” Subtraction. taking things away, respectfully.
  • val mul : t -> t -> t β€” Multiplication. the girlies are multiplying πŸ”₯
  • val div : t -> t -> t β€” Integer division. raises Division_by_zero if the second argument is zero β€” so like, don't πŸ‘€. this division rounds the real quotient of its arguments towards zero, as specified for (/).
  • val unsigned_div : t -> t -> t β€” Same as Targetint.div, except that arguments and result are interpreted as unsigned integers. no negativity allowed ✨
  • val rem : t -> t -> t β€” Integer remainder. if y is not zero, the result of Targetint.rem x y satisfies these properties:
    • Targetint.zero <= Nativeint.rem x y < Targetint.abs y
    • x = Targetint.add (Targetint.mul (Targetint.div x y) y) (Targetint.rem x y)

if y = 0, then Targetint.rem x y raises Division_by_zero. zero is not invited to this party 🚫

  • val unsigned_rem : t -> t -> t β€” Same as Targetint.rem, except that arguments and result are interpreted as unsigned integers. again: only good vibes, no signs πŸ’œ
  • val succ : t -> t β€” Successor. Targetint.succ x is Targetint.add x Targetint.one. one step forward, periodt.
  • val pred : t -> t β€” Predecessor. Targetint.pred x is Targetint.sub x Targetint.one. one step back, still slaying.
  • val abs : t -> t β€” abs x is the absolute value of x. BUT β€” and this is the plot twist 😭 β€” on min_int this is min_int itself and thus remains negative. the one case where the glow-up doesn't happen.
  • val size : int β€” The size in bits of a target native integer. lowkey the most important number here.
  • val max_int : t β€” The greatest representable target integer, either 2^31 - 1 on a 32-bit platform, or 2^63 - 1 on a 64-bit platform. the ceiling, literally.
  • val min_int : t β€” The smallest representable target integer, either -2^31 on a 32-bit platform, or -2^63 on a 64-bit platform. the floor. she's doing her best.
  • val logand : t -> t -> t β€” Bitwise logical and.
  • val logor : t -> t -> t β€” Bitwise logical or.
  • val logxor : t -> t -> t β€” Bitwise logical exclusive or. spicy.
  • val lognot : t -> t β€” Bitwise logical negation. flipping every single bit, no thoughts πŸ’…
  • val shift_left : t -> int -> t β€” Targetint.shift_left x y shifts x to the left by y bits. the result is unspecified if y < 0 or y >= bitsize, where bitsize is 32 on a 32-bit platform and 64 on a 64-bit platform. unspecified means don't ask, you won't like the answer πŸ’€
  • val shift_right : t -> int -> t β€” Targetint.shift_right x y shifts x to the right by y bits. this is an arithmetic shift: the sign bit of x is replicated and inserted in the vacated bits. the result is unspecified if y < 0 or y >= bitsize. same warning, same energy πŸ™ˆ
  • val shift_right_logical : t -> int -> t β€” Targetint.shift_right_logical x y shifts x to the right by y bits. this is a logical shift: zeroes are inserted in the vacated bits regardless of the sign of x. the result is unspecified if y < 0 or y >= bitsize. zeroes only, she doesn't care about your sign 🧚
  • val of_int : int -> t β€” Convert the given integer (type int) to a target integer (type t), modulo the target word size. 🫢
  • val of_int_exn : int -> t β€” Convert the given integer (type int) to a target integer (type t). raises a fatal error if the conversion is not exact. no silent lies here, she will tell on you πŸ”₯
  • val to_int : t -> int β€” Convert the given target integer (type t) to an integer (type int). the high-order bit is lost during the conversion. gone. adiΓ³s. 😭
  • val of_float : float -> t β€” Convert the given floating-point number to a target integer, discarding the fractional part (truncate towards 0). the result of the conversion is undefined if, after truncation, the number is outside the range [ Targetint.min_int , Targetint.max_int ]. undefined is not a cute look, check your range first bestie.
  • val to_float : t -> float β€” Convert the given target integer to a floating-point number. glow-up into float ✨
  • val of_int32 : int32 -> t β€” Convert the given 32-bit integer (type int32) to a target integer.
  • val to_int32 : t -> int32 β€” Convert the given target integer to a 32-bit integer (type int32). on 64-bit platforms, the 64-bit native integer is taken modulo 2^32, i.e. the top 32 bits are lost πŸ’€. on 32-bit platforms, the conversion is exact. platform-dependent behaviour, iykyk.
  • val of_int64 : int64 -> t β€” Convert the given 64-bit integer (type int64) to a target integer.
  • val to_int64 : t -> int64 β€” Convert the given target integer to a 64-bit integer (type int64). big girl conversion.
  • val of_string : string -> t β€” Convert the given string to a target integer. the string is read in decimal (by default) or in hexadecimal, octal or binary if the string begins with 0x, 0o or 0b respectively. raises Failure "int_of_string" if the given string is not a valid representation of an integer, or if the integer represented exceeds the range of integers representable in type nativeint. so yeah, bad input = Failure, no mercy πŸ™…
  • val to_string : t -> string β€” Return the string representation of its argument, in decimal. always decimal, always straightforward.
  • val compare : t -> t -> int β€” The comparison function for target integers, with the same specification as compare. along with the type t, this function compare allows the module Targetint to be passed as argument to the functors Set.Make and Map.Make. so she fits right into your data structures, bestie 🌈
  • val unsigned_compare : t -> t -> int β€” Same as Targetint.compare, except that arguments are interpreted as unsigned integers.
  • val equal : t -> t -> bool β€” The equal function for target ints. are they the same? true or false, that's it πŸ’–

πŸ‘‘ the representation girlies

type repr =
 | Int32 of int32
 | Int64 of int64
  • val repr : t -> repr β€” The concrete representation of a native integer. what's actually under the hood, no mystery ⭐
  • val print : Format.formatter -> t -> unit β€” Print a target integer to a formatter. show her off 🌸

πŸ’‘ quick vibe check recap: everything arithmetic wraps modulo 2^32 or 2^64 based on your target's word size, div/rem raise Division_by_zero on a zero divisor, the shifts are unspecified outside 0 <= y < bitsize, and the module is unstable and part of Compiler_libs. that's the whole story, fr fr.

and that's the page!! πŸ¦‹ this gorgeous little document was brought to you by OCamldoc, dated 2026-06-22, serving Targetint(3) realness. mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from