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 integer0. the beginning of everything πval one : tβ The target integer1. 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. raisesDivision_by_zeroif 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 asTargetint.div, except that arguments and result are interpreted as unsigned integers. no negativity allowed β¨val rem : t -> t -> tβ Integer remainder. ifyis not zero, the result ofTargetint.rem x ysatisfies these properties:Targetint.zero <= Nativeint.rem x y < Targetint.abs yx = 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 asTargetint.rem, except that arguments and result are interpreted as unsigned integers. again: only good vibes, no signs πval succ : t -> tβ Successor.Targetint.succ xisTargetint.add x Targetint.one. one step forward, periodt.val pred : t -> tβ Predecessor.Targetint.pred xisTargetint.sub x Targetint.one. one step back, still slaying.val abs : t -> tβabs xis the absolute value ofx. BUT β and this is the plot twist π β onmin_intthis ismin_intitself 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, either2^31 - 1on a 32-bit platform, or2^63 - 1on a 64-bit platform. the ceiling, literally.val min_int : tβ The smallest representable target integer, either-2^31on a 32-bit platform, or-2^63on 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 yshiftsxto the left byybits. the result is unspecified ify < 0ory >= bitsize, wherebitsizeis32on a 32-bit platform and64on 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 yshiftsxto the right byybits. this is an arithmetic shift: the sign bit ofxis replicated and inserted in the vacated bits. the result is unspecified ify < 0ory >= bitsize. same warning, same energy πval shift_right_logical : t -> int -> tβTargetint.shift_right_logical x yshiftsxto the right byybits. this is a logical shift: zeroes are inserted in the vacated bits regardless of the sign ofx. the result is unspecified ify < 0ory >= bitsize. zeroes only, she doesn't care about your sign π§val of_int : int -> tβ Convert the given integer (typeint) to a target integer (typet), modulo the target word size. π«Άval of_int_exn : int -> tβ Convert the given integer (typeint) to a target integer (typet). 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 (typet) to an integer (typeint). 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 towards0). 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 (typeint32) to a target integer.val to_int32 : t -> int32β Convert the given target integer to a 32-bit integer (typeint32). on 64-bit platforms, the 64-bit native integer is taken modulo2^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 (typeint64) to a target integer.val to_int64 : t -> int64β Convert the given target integer to a 64-bit integer (typeint64). 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 with0x,0oor0brespectively. raisesFailure "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 typenativeint. 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 ascompare. along with the typet, this functioncompareallows the moduleTargetintto be passed as argument to the functorsSet.MakeandMap.Make. so she fits right into your data structures, bestie πval unsigned_compare : t -> t -> intβ Same asTargetint.compare, except that arguments are interpreted as unsigned integers.val equal : t -> t -> boolβ Theequalfunction for target ints. are they the same?trueorfalse, 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 modulo2^32or2^64based on your target's word size,div/remraiseDivision_by_zeroon a zero divisor, the shifts are unspecified outside0 <= y < bitsize, and the module is unstable and part ofCompiler_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
- arch rolling: ocaml 5.5.0-1 Β· LGPL2.1 AND custom: QPL-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms