pax_global_header00006660000000000000000000000064134320447140014514gustar00rootroot0000000000000052 comment=41846f424b13af552200939228492d29bd06a495 ocaml-integers-0.3.0/000077500000000000000000000000001343204471400144255ustar00rootroot00000000000000ocaml-integers-0.3.0/.gitignore000066400000000000000000000000431343204471400164120ustar00rootroot00000000000000*~ _build integers.install .merlin ocaml-integers-0.3.0/.travis.yml000066400000000000000000000010561343204471400165400ustar00rootroot00000000000000language: c sudo: required install: wget https://raw.githubusercontent.com/ocaml/ocaml-ci-scripts/master/.travis-opam.sh script: bash -ex .travis-opam.sh env: - OCAML_VERSION=4.02 - OCAML_VERSION=4.03 - OCAML_VERSION=4.04 - OCAML_VERSION=4.05 - OCAML_VERSION=4.06 - OCAML_VERSION=4.07 os: - linux - osx matrix: exclude: - os: osx env: OCAML_VERSION=4.02 - os: osx env: OCAML_VERSION=4.03 - os: osx env: OCAML_VERSION=4.04 - os: osx env: OCAML_VERSION=4.05 - os: osx env: OCAML_VERSION=4.06 ocaml-integers-0.3.0/CHANGES.md000066400000000000000000000013021343204471400160130ustar00rootroot00000000000000v0.3.0 2019-02-15 ----------------- * Drop 4.01 support (@yallop, #23) * Add equal and pp to the output signatures (@emillon, #22) * Dune port (@rgrinberg, #21) * Add UInt64.(of|to)_uint32 (@yallop, #17) v0.2.2 2016-12-19 ----------------- * Fix truncation in UInt64.of_int; remove other uses of `Val_int`/`Int_val` v0.2.1 2016-11-14 ----------------- * Register the custom deserializers v0.2.0 2016-10-04 ----------------- * Expose from_byte_size functions in Unsigned and Signed * Support for platforms where standard integer types are macros * Add 'max' and 'min' functions to Unsigned.S. * Expose private types for UChar, UInt8, UInt16. 0.1.0 2016-09-26 ---------------- * Initial public release ocaml-integers-0.3.0/LICENSE.md000066400000000000000000000020451343204471400160320ustar00rootroot00000000000000Copyright (c) 2013-2016 Jeremy Yallop Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.ocaml-integers-0.3.0/Makefile000066400000000000000000000001551343204471400160660ustar00rootroot00000000000000.PHONY: all clean test doc all: dune build test: dune runtest doc: dune build @doc clean: dune clean ocaml-integers-0.3.0/README.md000066400000000000000000000037161343204471400157130ustar00rootroot00000000000000# ocaml-integers The `ocaml-integers` library provides a number of 8-, 16-, 32- and 64-bit signed and unsigned integer types, together with aliases such as `long` and `size_t` whose sizes depend on the host platform. ### Features * The interfaces follow the pattern of the signatures of the [`Int32`][int32], [`Int64`][int64], and [`Nativeint`][nativeint] modules in the OCaml standard library. The behaviour also follows the standard library; for example, conversions such as `of_int` truncate, and operations are "modulo" in general: ```ocaml # Unsigned.UInt8.(pred zero);; - : Unsigned.UInt8.t = ``` * Top-level printers for each type are included ```ocaml # Unsigned.UInt32.[of_int 103; one; of_string "1000"];; - : Unsigned.UInt32.t list = [; ; ] ``` * Infix operators are available: ```ocaml # Unsigned.UInt32.(Infix.(one + one));; - : Unsigned.UInt32.t = ``` * Polymorphic operations such as comparison behave correctly: ```ocaml # open Unsigned.UInt32 # zero < one;; - : bool = true # max_int < zero;; - : bool = false ``` * Integers 32 bits and above are boxed; integers below 32 bits are unboxed. ```ocaml # Obj.(tag (repr Unsigned.UInt32.zero));; - : int = 255 # Obj.(tag (repr Unsigned.UInt16.zero));; - : int = 1000 ``` [![Travis build Status](https://travis-ci.org/ocamllabs/ocaml-integers.svg?branch=master)](https://travis-ci.org/ocamllabs/ocaml-integers) [![AppVeyor build status](https://ci.appveyor.com/api/projects/status/4l1vout6fl581hgq?svg=true)](https://ci.appveyor.com/project/yallop/ocaml-integers/branch/master) [API documentation][doc] [int32]: http://caml.inria.fr/pub/docs/manual-ocaml/libref/Int32.html [int64]: http://caml.inria.fr/pub/docs/manual-ocaml/libref/Int64.html [nativeint]: http://caml.inria.fr/pub/docs/manual-ocaml/libref/Nativeint.html [doc]: https://ocamllabs.github.io/ocaml-integers/api.docdir/ ocaml-integers-0.3.0/appveyor.yml000066400000000000000000000005521343204471400170170ustar00rootroot00000000000000platform: - x86 environment: FORK_USER: ocaml FORK_BRANCH: master CYG_ROOT: C:\cygwin64 install: - ps: iex ((new-object net.webclient).DownloadString("https://raw.githubusercontent.com/$env:FORK_USER/ocaml-ci-scripts/$env:FORK_BRANCH/appveyor-install.ps1")) build_script: - call %CYG_ROOT%\bin\bash.exe -l %APPVEYOR_BUILD_FOLDER%\appveyor-opam.sh ocaml-integers-0.3.0/dune-project000066400000000000000000000000371343204471400167470ustar00rootroot00000000000000(lang dune 1.0) (name integers)ocaml-integers-0.3.0/integers.opam000066400000000000000000000011711343204471400171230ustar00rootroot00000000000000opam-version: "2.0" maintainer: "yallop@gmail.com" authors: ["Jeremy Yallop" "Demi Obenour" "Stephane Glondu" "Andreas Hauptmann"] homepage: "https://github.com/ocamllabs/ocaml-integers" bug-reports: "https://github.com/ocamllabs/ocaml-integers/issues" dev-repo: "git+https://github.com/ocamllabs/ocaml-integers.git" license: "MIT" build: [ ["dune" "subst"] {pinned} ["dune" "build" "-p" name "-j" jobs] ["dune" "runtest" "-p" name "-j" jobs] {with-test} ] depends: [ "ocaml" {>= "4.02"} "dune" {build} ] synopsis: "Various signed and unsigned integer types for OCaml" description: """ """ ocaml-integers-0.3.0/src/000077500000000000000000000000001343204471400152145ustar00rootroot00000000000000ocaml-integers-0.3.0/src/dune000066400000000000000000000002761343204471400160770ustar00rootroot00000000000000(library (name integers) (public_name integers) (wrapped false) (install_c_headers ocaml_integers) (c_names unsigned_stubs) (synopsis "Signed and unsigned integers of various sizes")) ocaml-integers-0.3.0/src/ocaml_integers.h000066400000000000000000000075571343204471400203760ustar00rootroot00000000000000/* * Copyright (c) 2013 Jeremy Yallop. * * This file is distributed under the terms of the MIT License. * See the file LICENSE for details. */ #ifndef INTEGERS_UNSIGNED_STUBS_H #define INTEGERS_UNSIGNED_STUBS_H #include #include #define UINT_DECLS(BITS) \ extern value integers_copy_uint ## BITS(uint ## BITS ## _t u); \ /* uintX_add : t -> t -> t */ \ extern value integers_uint ## BITS ## _ ## add(value a, value b); \ /* uintX_sub : t -> t -> t */ \ extern value integers_uint ## BITS ## _ ## sub(value a, value b); \ /* uintX_mul : t -> t -> t */ \ extern value integers_uint ## BITS ## _ ## mul(value a, value b); \ /* uintX_div : t -> t -> t */ \ extern value integers_uint ## BITS ## _ ## div(value a, value b); \ /* uintX_rem : t -> t -> t */ \ extern value integers_uint ## BITS ## _ ## rem(value a, value b); \ /* uintX_logand : t -> t -> t */ \ extern value integers_uint ## BITS ## _ ## logand(value a, value b); \ /* uintX_logor : t -> t -> t */ \ extern value integers_uint ## BITS ## _ ## logor(value a, value b); \ /* uintX_logxor : t -> t -> t */ \ extern value integers_uint ## BITS ## _ ## logxor(value a, value b); \ /* uintX_shift_left : t -> t -> t */ \ extern value integers_uint ## BITS ## _ ## shift_left(value a, value b); \ /* uintX_shift_right : t -> t -> t */ \ extern value integers_uint ## BITS ## _ ## shift_right(value a, value b); \ /* of_int : int -> t */ \ extern value integers_uint ## BITS ## _of_int(value a); \ /* to_int : t -> int */ \ extern value integers_uint ## BITS ## _to_int(value a); \ /* of_string : string -> t */ \ extern value integers_uint ## BITS ## _of_string(value a); \ /* to_string : t -> string */ \ extern value integers_uint ## BITS ## _to_string(value a); \ /* max : unit -> t */ \ extern value integers_uint ## BITS ## _max(value a); #define UINT_SMALL_DECLS(BITS) \ /* of_string : string -> t */ \ extern value integers_uint ## BITS ## _of_string(value a); \ /* to_string : t -> string */ \ extern value integers_uint ## BITS ## _to_string(value a); \ /* max : unit -> t */ \ extern value integers_uint ## BITS ## _max(value a); UINT_SMALL_DECLS(8) UINT_SMALL_DECLS(16) UINT_DECLS(32) UINT_DECLS(64) /* X_size : unit -> int */ extern value integers_size_t_size (value _); extern value integers_ushort_size (value _); extern value integers_uint_size (value _); extern value integers_ulong_size (value _); extern value integers_ulonglong_size (value _); #define Integers_val_uint8(t) ((Val_int((uint8_t)t))) #define Integers_val_uint16(t) ((Val_int((uint16_t)t))) #define Uint8_val(V) ((uint8_t)(Int_val(V))) #define Uint16_val(V) ((uint16_t)(Int_val(V))) #define Uint32_val(V) (*((uint32_t *) Data_custom_val(V))) #define Uint64_val(V) (*((uint64_t *) Data_custom_val(V))) #endif /* INTEGERS_UNSIGNED_STUBS_H */ ocaml-integers-0.3.0/src/signed.ml000066400000000000000000000064201343204471400170210ustar00rootroot00000000000000(* * Copyright (c) 2013 Jeremy Yallop. * * This file is distributed under the terms of the MIT License. * See the file LICENSE for details. *) module type S = sig include Unsigned.S val neg : t -> t val abs : t -> t val minus_one : t val min_int : t val shift_right_logical : t -> int -> t val of_nativeint : nativeint -> t val to_nativeint : t -> nativeint val of_int64 : int64 -> t val to_int64 : t -> int64 end module type Basics = sig type t val add : t -> t -> t val sub : t -> t -> t val mul : t -> t -> t val div : t -> t -> t val rem : t -> t -> t val logand : t -> t -> t val logor : t -> t -> t val logxor : t -> t -> t val shift_left : t -> int -> t val shift_right : t -> int -> t val shift_right_logical : t -> int -> t end module MakeInfix(S : Basics) = struct open S let (+) = add let (-) = sub let ( * ) = mul let (/) = div let (mod) = rem let (land) = logand let (lor) = logor let (lxor) = logxor let (lsl) = shift_left let (lsr) = shift_right_logical let (asr) = shift_right end module Int = struct module Basics = struct type t = int let add = ( + ) let sub = ( - ) let mul = ( * ) let div = ( / ) let rem = ( mod ) let max_int = Pervasives.max_int let min_int = Pervasives.min_int let logand = ( land ) let logor = ( lor ) let logxor = ( lxor ) let shift_left = ( lsl ) let shift_right = ( asr ) let shift_right_logical = ( lsr ) let of_int x = x let to_int x = x let of_string = int_of_string let to_string = string_of_int let zero = 0 let one = 1 let minus_one = -1 let lognot = lnot let succ = Pervasives.succ let pred = Pervasives.pred let compare = Pervasives.compare let equal = Pervasives.(=) let max = Pervasives.max let min = Pervasives.min end include Basics module Infix = MakeInfix(Basics) let to_int64 = Int64.of_int let of_int64 = Int64.to_int let to_nativeint = Nativeint.of_int let of_nativeint = Nativeint.to_int let abs = Pervasives.abs let neg x = -x let pp fmt n = Format.fprintf fmt "%d" n end module Int32 = struct let equal (x:int32) (y:int32) = x = y include Int32 module Infix = MakeInfix(Int32) let of_nativeint = Nativeint.to_int32 let to_nativeint = Nativeint.of_int32 let of_int64 = Int64.to_int32 let to_int64 = Int64.of_int32 let max = Pervasives.max let min = Pervasives.min let pp fmt n = Format.fprintf fmt "%ld" n end module Int64 = struct let equal (x:int64) (y:int64) = x = y include Int64 module Infix = MakeInfix(Int64) let of_int64 x = x let to_int64 x = x let max = Pervasives.max let min = Pervasives.min let pp fmt n = Format.fprintf fmt "%Ld" n end (* C guarantees that sizeof(t) == sizeof(unsigned t) *) external int_size : unit -> int = "integers_uint_size" external long_size : unit -> int = "integers_ulong_size" external llong_size : unit -> int = "integers_ulonglong_size" let of_byte_size : int -> (module S) = function | 4 -> (module Int32) | 8 -> (module Int64) | _ -> invalid_arg "Signed.of_byte_size" module SInt = (val of_byte_size (int_size ())) module Long = (val of_byte_size (long_size ())) module LLong = (val of_byte_size (llong_size ())) type sint = SInt.t type long = Long.t type llong = LLong.t ocaml-integers-0.3.0/src/signed.mli000066400000000000000000000035231343204471400171730ustar00rootroot00000000000000(* * Copyright (c) 2013 Jeremy Yallop. * * This file is distributed under the terms of the MIT License. * See the file LICENSE for details. *) (** Types and operations for signed integers. *) module type S = sig include Unsigned.S val neg : t -> t (** Unary negation. *) val abs : t -> t (** Return the absolute value of its argument. *) val minus_one : t (** The value -1 *) val min_int : t (** The smallest representable integer. *) val shift_right_logical : t -> int -> t (** {!shift_right_logical} [x] [y] shifts [x] to the right by [y] bits. See {!Int32.shift_right_logical}. *) val of_nativeint : nativeint -> t (** Convert the given nativeint value to a signed integer. *) val to_nativeint : t -> nativeint (** Convert the given signed integer to a nativeint value. *) val of_int64 : int64 -> t (** Convert the given int64 value to a signed integer. *) val to_int64 : t -> int64 (** Convert the given signed integer to an int64 value. *) end (** Signed integer operations *) module Int : S with type t = int (** Signed integer type and operations. *) module Int32 : S with type t = int32 (** Signed 32-bit integer type and operations. *) module Int64 : S with type t = int64 (** Signed 64-bit integer type and operations. *) module SInt : S (** C's signed integer type and operations. *) module Long : S (** The signed long integer type and operations. *) module LLong : S (** The signed long long integer type and operations. *) type sint = SInt.t (** C's signed integer type. *) type long = Long.t (** The signed long integer type. *) type llong = LLong.t (** The signed long long integer type. *) val of_byte_size : int -> (module S) (** [of_byte_size b] is a module of type S that implements a signed type with [b] bytes. Raise [Invalid_argument] if no suitable type is available. *) ocaml-integers-0.3.0/src/unsigned.ml000066400000000000000000000202611343204471400173630ustar00rootroot00000000000000(* * Copyright (c) 2013 Jeremy Yallop. * * This file is distributed under the terms of the MIT License. * See the file LICENSE for details. *) external init : unit -> unit = "integers_unsigned_init" let () = init () (* Boxed unsigned types *) module type Basics = sig type t val add : t -> t -> t val sub : t -> t -> t val mul : t -> t -> t val div : t -> t -> t val rem : t -> t -> t val max_int : t val logand : t -> t -> t val logor : t -> t -> t val logxor : t -> t -> t val shift_left : t -> int -> t val shift_right : t -> int -> t val of_int : int -> t val to_int : t -> int val of_int64 : int64 -> t val to_int64 : t -> int64 val of_string : string -> t val to_string : t -> string end module type Extras = sig type t val zero : t val one : t val lognot : t -> t val succ : t -> t val pred : t -> t val compare : t -> t -> int val equal : t -> t -> bool val max : t -> t -> t val min : t -> t -> t val pp : Format.formatter -> t -> unit end module type Infix = sig type t val (+) : t -> t -> t val (-) : t -> t -> t val ( * ) : t -> t -> t val (/) : t -> t -> t val (mod) : t -> t -> t val (land) : t -> t -> t val (lor) : t -> t -> t val (lxor) : t -> t -> t val (lsl) : t -> int -> t val (lsr) : t -> int -> t end module type S = sig include Basics include Extras with type t := t module Infix : Infix with type t := t end module MakeInfix (B : Basics) = struct open B let (+) = add let (-) = sub let ( * ) = mul let (/) = div let (mod) = rem let (land) = logand let (lor) = logor let (lxor) = logxor let (lsl) = shift_left let (lsr) = shift_right end module Extras(Basics : Basics) : Extras with type t := Basics.t = struct open Basics let zero = of_int 0 let one = of_int 1 let succ n = add n one let pred n = sub n one let lognot n = logxor n max_int let compare (x : t) (y : t) = Pervasives.compare x y let equal (x : t) (y : t) = Pervasives.(=) x y let max (x : t) (y : t) = Pervasives.max x y let min (x : t) (y : t) = Pervasives.min x y let pp fmt x = Format.fprintf fmt "%s" (to_string x) end module UInt8 : S with type t = private int = struct module B = struct type t = int let max_int = 255 let add : t -> t -> t = fun x y -> (x + y) land max_int let sub : t -> t -> t = fun x y -> (x - y) land max_int let mul : t -> t -> t = fun x y -> (x * y) land max_int let div : t -> t -> t = (/) let rem : t -> t -> t = (mod) let logand: t -> t -> t = (land) let logor: t -> t -> t = (lor) let logxor : t -> t -> t = (lxor) let shift_left : t -> int -> t = fun x y -> (x lsl y) land max_int let shift_right : t -> int -> t = (lsr) let of_int (x: int): t = (* For backwards compatibility, this wraps *) x land max_int external to_int : t -> int = "%identity" let of_int64 : int64 -> t = fun x -> of_int (Int64.to_int x) let to_int64 : t -> int64 = fun x -> Int64.of_int (to_int x) external of_string : string -> t = "integers_uint8_of_string" let to_string : t -> string = string_of_int end include B include Extras(B) module Infix = MakeInfix(B) end module UInt16 : S with type t = private int = struct module B = struct type t = int let max_int = 65535 let add : t -> t -> t = fun x y -> (x + y) land max_int let sub : t -> t -> t = fun x y -> (x - y) land max_int let mul : t -> t -> t = fun x y -> (x * y) land max_int let div : t -> t -> t = (/) let rem : t -> t -> t = (mod) let logand: t -> t -> t = (land) let logor: t -> t -> t = (lor) let logxor : t -> t -> t = (lxor) let shift_left : t -> int -> t = fun x y -> (x lsl y) land max_int let shift_right : t -> int -> t = (lsr) let of_int (x: int): t = (* For backwards compatibility, this wraps *) x land max_int external to_int : t -> int = "%identity" let of_int64 : int64 -> t = fun x -> Int64.to_int x |> of_int let to_int64 : t -> int64 = fun x -> to_int x |> Int64.of_int external of_string : string -> t = "integers_uint16_of_string" let to_string : t -> string = string_of_int end include B include Extras(B) module Infix = MakeInfix(B) end module UInt32 : sig include S external of_int32 : int32 -> t = "integers_uint32_of_int32" external to_int32 : t -> int32 = "integers_int32_of_uint32" end = struct module B = struct type t external add : t -> t -> t = "integers_uint32_add" external sub : t -> t -> t = "integers_uint32_sub" external mul : t -> t -> t = "integers_uint32_mul" external div : t -> t -> t = "integers_uint32_div" external rem : t -> t -> t = "integers_uint32_rem" external logand : t -> t -> t = "integers_uint32_logand" external logor : t -> t -> t = "integers_uint32_logor" external logxor : t -> t -> t = "integers_uint32_logxor" external shift_left : t -> int -> t = "integers_uint32_shift_left" external shift_right : t -> int -> t = "integers_uint32_shift_right" external of_int : int -> t = "integers_uint32_of_int" external to_int : t -> int = "integers_uint32_to_int" external of_int64 : int64 -> t = "integers_uint32_of_int64" external to_int64 : t -> int64 = "integers_uint32_to_int64" external of_string : string -> t = "integers_uint32_of_string" external to_string : t -> string = "integers_uint32_to_string" external _max_int : unit -> t = "integers_uint32_max" let max_int = _max_int () end include B include Extras(B) module Infix = MakeInfix(B) external of_int32 : int32 -> t = "integers_uint32_of_int32" external to_int32 : t -> int32 = "integers_int32_of_uint32" end module UInt64 : sig include S external of_int64 : int64 -> t = "integers_uint64_of_int64" external to_int64 : t -> int64 = "integers_uint64_to_int64" external of_uint32 : UInt32.t -> t = "integers_uint64_of_uint32" external to_uint32 : t -> UInt32.t = "integers_uint32_of_uint64" end = struct module B = struct type t external add : t -> t -> t = "integers_uint64_add" external sub : t -> t -> t = "integers_uint64_sub" external mul : t -> t -> t = "integers_uint64_mul" external div : t -> t -> t = "integers_uint64_div" external rem : t -> t -> t = "integers_uint64_rem" external logand : t -> t -> t = "integers_uint64_logand" external logor : t -> t -> t = "integers_uint64_logor" external logxor : t -> t -> t = "integers_uint64_logxor" external shift_left : t -> int -> t = "integers_uint64_shift_left" external shift_right : t -> int -> t = "integers_uint64_shift_right" external of_int : int -> t = "integers_uint64_of_int" external to_int : t -> int = "integers_uint64_to_int" external of_int64 : int64 -> t = "integers_uint64_of_int64" external to_int64 : t -> int64 = "integers_uint64_to_int64" external of_uint32 : UInt32.t -> t = "integers_uint64_of_uint32" external to_uint32 : t -> UInt32.t = "integers_uint32_of_uint64" external of_string : string -> t = "integers_uint64_of_string" external to_string : t -> string = "integers_uint64_to_string" external _max_int : unit -> t = "integers_uint64_max" let max_int = _max_int () end include B include Extras(B) module Infix = MakeInfix(B) end let of_byte_size : int -> (module S) = function | 1 -> (module UInt8) | 2 -> (module UInt16) | 4 -> (module UInt32) | 8 -> (module UInt64) | _ -> invalid_arg "Unsigned.of_byte_size" external size_t_size : unit -> int = "integers_size_t_size" external ushort_size : unit -> int = "integers_ushort_size" external uint_size : unit -> int = "integers_uint_size" external ulong_size : unit -> int = "integers_ulong_size" external ulonglong_size : unit -> int = "integers_ulonglong_size" module Size_t : S = (val of_byte_size (size_t_size ())) module UChar = UInt8 module UShort : S = (val of_byte_size (ushort_size ())) module UInt : S = (val of_byte_size (uint_size ())) module ULong : S = (val of_byte_size (ulong_size ())) module ULLong : S = (val of_byte_size (ulonglong_size ())) type uchar = UChar.t type uint8 = UInt8.t type uint16 = UInt16.t type uint32 = UInt32.t type uint64 = UInt64.t type size_t = Size_t.t type ushort = UShort.t type uint = UInt.t type ulong = ULong.t type ullong = ULLong.t ocaml-integers-0.3.0/src/unsigned.mli000066400000000000000000000124571343204471400175440ustar00rootroot00000000000000(* * Copyright (c) 2013 Jeremy Yallop. * * This file is distributed under the terms of the MIT License. * See the file LICENSE for details. *) (** Types and operations for unsigned integers. *) module type S = sig type t val add : t -> t -> t (** Addition. *) val sub : t -> t -> t (** Subtraction. *) val mul : t -> t -> t (** Multiplication. *) val div : t -> t -> t (** Division. Raise {!Division_by_zero} if the second argument is zero. *) val rem : t -> t -> t (** Integer remainder. Raise {!Division_by_zero} if the second argument is zero. *) val max_int : t (** The greatest representable integer. *) 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. *) val shift_left : t -> int -> t (** {!shift_left} [x] [y] shifts [x] to the left by [y] bits. *) val shift_right : t -> int -> t (** {!shift_right} [x] [y] shifts [x] to the right by [y] bits. *) val of_int : int -> t (** Convert the given int value to an unsigned integer. *) val to_int : t -> int (** Convert the given unsigned integer value to an int. *) val of_int64 : int64 -> t (** Convert the given int64 value to an unsigned integer. *) val to_int64 : t -> int64 (** Convert the given unsigned integer value to an int64. *) val of_string : string -> t (** Convert the given string to an unsigned integer. Raise {!Failure} ["int_of_string"] if the given string is not a valid representation of an unsigned integer. *) val to_string : t -> string (** Return the string representation of its argument. *) val zero : t (** The integer 0. *) val one : t (** The integer 1. *) val lognot : t -> t (** Bitwise logical negation. *) val succ : t -> t (** Successor. *) val pred : t -> t (** Predecessor. *) val compare : t -> t -> int (** The comparison function for unsigned integers, with the same specification as {!Pervasives.compare}. *) val equal : t -> t -> bool (** Tests for equality, with the same specification as {!Pervasives.(=)}. *) val max : t -> t -> t (** [max x y] is the greater of [x] and [y] *) val min : t -> t -> t (** [min x y] is the lesser of [x] and [y] *) val pp : Format.formatter -> t -> unit (** Output the result of {!to_string} on a formatter. *) module Infix : sig val (+) : t -> t -> t (** Addition. See {!add}. *) val (-) : t -> t -> t (** Subtraction. See {!sub}.*) val ( * ) : t -> t -> t (** Multiplication. See {!mul}.*) val (/) : t -> t -> t (** Division. See {!div}.*) val (mod) : t -> t -> t (** Integer remainder. See {!rem}. *) val (land) : t -> t -> t (** Bitwise logical and. See {!logand}. *) val (lor) : t -> t -> t (** Bitwise logical or. See {!logor}. *) val (lxor) : t -> t -> t (** Bitwise logical exclusive or. See {!logxor}. *) val (lsl) : t -> int -> t (** [x lsl y] shifts [x] to the left by [y] bits. See {!shift_left}. *) val (lsr) : t -> int -> t (** [x lsr y] shifts [x] to the right by [y] bits. See {!shift_right}. *) end (** Infix names for the unsigned integer operations. *) end (** Unsigned integer operations. *) module UChar : S with type t = private int (** Unsigned char type and operations. *) module UInt8 : S with type t = private int (** Unsigned 8-bit integer type and operations. *) module UInt16 : S with type t = private int (** Unsigned 16-bit integer type and operations. *) module UInt32 : sig include S val of_int32 : int32 -> t val to_int32 : t -> int32 end (** Unsigned 32-bit integer type and operations. *) module UInt64 : sig include S val of_int64 : int64 -> t val to_int64 : t -> int64 val of_uint32 : UInt32.t -> t (** Convert the given 32-bit unsigned integer to a 64-bit unsigned integer. *) val to_uint32 : t -> UInt32.t (** Convert the given 64-bit unsigned integer to a 32-bit unsigned integer. The 64-bit unsigned integer is taken modulo 2{^32}, i.e. the top 32 bits are lost during the conversion. *) end (** Unsigned 64-bit integer type and operations. *) module Size_t : S (** The size_t unsigned integer type and operations. *) module UShort : S (** The unsigned short integer type and operations. *) module UInt : S (** The unsigned int type and operations. *) module ULong : S (** The unsigned long integer type and operations. *) module ULLong : S (** The unsigned long long integer type and operations. *) type uchar = UChar.t (** The unsigned char type. *) type uint8 = UInt8.t (** Unsigned 8-bit integer type. *) type uint16 = UInt16.t (** Unsigned 16-bit integer type. *) type uint32 = UInt32.t (** Unsigned 32-bit integer type. *) type uint64 = UInt64.t (** Unsigned 64-bit integer type. *) type size_t = Size_t.t (** The size_t unsigned integer type. *) type ushort = UShort.t (** The unsigned short unsigned integer type. *) type uint = UInt.t (** The unsigned int type. *) type ulong = ULong.t (** The unsigned long integer type. *) type ullong = ULLong.t (** The unsigned long long integer type. *) val of_byte_size : int -> (module S) (** [of_byte_size b] is a module of type S that implements an unsigned type with [b] bytes. Raise [Invalid_argument] if no suitable type is available. *) ocaml-integers-0.3.0/src/unsigned_stubs.c000066400000000000000000000351051343204471400204200ustar00rootroot00000000000000/* * Copyright (c) 2013 Jeremy Yallop. * * This file is distributed under the terms of the MIT License. * See the file LICENSE for details. */ #if !__USE_MINGW_ANSI_STDIO && (defined(__MINGW32__) || defined(__MINGW64__)) #define __USE_MINGW_ANSI_STDIO 1 #endif #include #include #include #include #include #include #include #include #include #include "ocaml_integers.h" #define Uint_custom_val(SIZE, V) Uint_custom_val_(SIZE, V) #define Uint_custom_val_(SIZE, V) \ (*(uint ## SIZE ## _t *)(Data_custom_val(V))) #define TYPE(SIZE) uint ## SIZE ## _t #define BUF_SIZE(TYPE) ((sizeof(TYPE) * CHAR_BIT + 2) / 3 + 1) #define UINT_PRIMOP(NAME, SIZE, OP) \ /* OP : t -> t -> t */ \ value integers_uint ## SIZE ## _ ## NAME(value a, value b) \ { \ return integers_copy_uint ## SIZE(Uint_custom_val(SIZE, a) \ OP Uint_custom_val(SIZE, b)); \ } #define UINT_DEFS(BITS, BYTES) \ static int uint ## BITS ## _cmp(value v1, value v2) \ { \ TYPE(BITS) u1 = Uint_custom_val(BITS, v1); \ TYPE(BITS) u2 = Uint_custom_val(BITS, v2); \ return (u1 > u2) - (u1 < u2); \ } \ \ static intnat uint ## BITS ## _hash(value v) \ { \ return Uint_custom_val(BITS, v); \ } \ \ static void uint ## BITS ## _serialize(value v, \ uintnat *wsize_32, \ uintnat *wsize_64) \ { \ caml_serialize_int_ ## BYTES(Uint_custom_val(BITS, v)); \ *wsize_32 = *wsize_64 = BYTES; \ } \ \ static uintnat uint ## BITS ## _deserialize(void *dst) \ { \ *(TYPE(BITS) *)dst = caml_deserialize_uint_ ## BYTES(); \ return BYTES; \ } \ \ static struct custom_operations caml_uint ## BITS ## _ops = { \ "integers:uint" #BITS, \ custom_finalize_default, \ uint ## BITS ## _cmp, \ uint ## BITS ## _hash, \ uint ## BITS ## _serialize, \ uint ## BITS ## _deserialize, \ custom_compare_ext_default \ }; \ \ value integers_copy_uint ## BITS(TYPE(BITS) u) \ { \ value res = caml_alloc_custom(&caml_uint ## BITS ## _ops, BYTES, 0, 1); \ Uint_custom_val(BITS, res) = u; \ return res; \ } \ UINT_PRIMOP(add, BITS, +) \ UINT_PRIMOP(sub, BITS, -) \ UINT_PRIMOP(mul, BITS, *) \ UINT_PRIMOP(logand, BITS, &) \ UINT_PRIMOP(logor, BITS, |) \ UINT_PRIMOP(logxor, BITS, ^) \ \ /* div : t -> t -> t */ \ value integers_uint ## BITS ## _div(value n_, value d_) \ { \ TYPE(BITS) n = Uint_custom_val(BITS, n_); \ TYPE(BITS) d = Uint_custom_val(BITS, d_); \ if (d == (TYPE(BITS)) 0) \ caml_raise_zero_divide(); \ return integers_copy_uint ## BITS (n / d); \ } \ \ /* rem : t -> t -> t */ \ value integers_uint ## BITS ## _rem(value n_, value d_) \ { \ TYPE(BITS) n = Uint_custom_val(BITS, n_); \ TYPE(BITS) d = Uint_custom_val(BITS, d_); \ if (d == (TYPE(BITS)) 0) \ caml_raise_zero_divide(); \ return integers_copy_uint ## BITS (n % d); \ } \ \ /* shift_left : t -> int -> t */ \ value integers_uint ## BITS ## _shift_left(value a, value b) \ { \ return integers_copy_uint ## BITS(Uint_custom_val(BITS, a) \ << Long_val(b)); \ } \ \ /* shift_right : t -> int -> t */ \ value integers_uint ## BITS ## _shift_right(value a, value b) \ { \ return integers_copy_uint ## BITS(Uint_custom_val(BITS, a) \ >> Long_val(b)); \ } \ \ /* of_int : int -> t */ \ value integers_uint ## BITS ## _of_int(value a) \ { \ return integers_copy_uint ## BITS (Long_val(a)); \ } \ \ /* to_int : t -> int */ \ value integers_uint ## BITS ## _to_int(value a) \ { \ return Val_long(Uint_custom_val(BITS, a)); \ } \ \ /* of_int64 : int64 -> t */ \ value integers_uint ## BITS ## _of_int64(value a) \ { \ return integers_copy_uint ## BITS(Int64_val(a)); \ } \ \ /* to_int64 : t -> int64 */ \ value integers_uint ## BITS ## _to_int64(value a) \ { \ return caml_copy_int64(Uint_custom_val(BITS, a)); \ } \ \ /* of_string : string -> t */ \ value integers_uint ## BITS ## _of_string(value a) \ { \ TYPE(BITS) u; \ if (sscanf(String_val(a), "%" SCNu ## BITS , &u) != 1) \ caml_failwith("int_of_string"); \ else \ return integers_copy_uint ## BITS (u); \ } \ \ /* to_string : t -> string */ \ value integers_uint ## BITS ## _to_string(value a) \ { \ char buf[BUF_SIZE(TYPE(BITS))]; \ if (sprintf(buf, "%" PRIu ## BITS , Uint_custom_val(BITS, a)) < 0) \ caml_failwith("string_of_int"); \ else \ return caml_copy_string(buf); \ } \ \ /* max : unit -> t */ \ value integers_uint ## BITS ## _max(value a) \ { \ return integers_copy_uint ## BITS ((TYPE(BITS))(-1)); \ } #define UINT_SMALL_DEFS(BITS, BYTES) \ /* of_string : string -> t */ \ value integers_uint ## BITS ## _of_string(value a) \ { \ TYPE(BITS) u; \ if (sscanf(String_val(a), "%" SCNu ## BITS , &u) != 1) \ caml_failwith("int_of_string"); \ else \ return Integers_val_uint ## BITS(u); \ } \ \ /* to_string : t -> string */ \ value integers_uint ## BITS ## _to_string(value a) \ { \ char buf[BUF_SIZE(TYPE(BITS))]; \ if (sprintf(buf, "%" PRIu ## BITS , Uint ## BITS ##_val(a)) < 0) \ caml_failwith("string_of_int"); \ else \ return caml_copy_string(buf); \ } \ \ /* max : unit -> t */ \ value integers_uint ## BITS ## _max(value unit) \ { \ return Integers_val_uint ## BITS((TYPE(BITS))(-1)); \ } UINT_SMALL_DEFS(8, 1) UINT_SMALL_DEFS(16, 2) UINT_DEFS(32, 4) UINT_DEFS(64, 8) value integers_size_t_size (value _) { return Val_long(sizeof (size_t)); } value integers_ushort_size (value _) { return Val_long(sizeof (unsigned short)); } value integers_uint_size (value _) { return Val_long(sizeof (unsigned int)); } value integers_ulong_size (value _) { return Val_long(sizeof (unsigned long)); } value integers_ulonglong_size (value _) { return Val_long(sizeof (unsigned long long)); } value integers_uint32_of_int32 (value i) { return integers_copy_uint32(Int32_val(i)); } value integers_int32_of_uint32 (value u) { return caml_copy_int32(Uint_custom_val(32, u)); } value integers_uintptr_t_size (value _) { return Val_long(sizeof (uintptr_t)); } value integers_intptr_t_size (value _) { return Val_long(sizeof (intptr_t)); } value integers_ptrdiff_t_size (value _) { return Val_long(sizeof (ptrdiff_t)); } value integers_uint32_of_uint64 (value u) { return integers_copy_uint32(Uint_custom_val(64,u)); } value integers_uint64_of_uint32 (value u) { return integers_copy_uint64(Uint_custom_val(32,u)); } value integers_unsigned_init(value unit) { caml_register_custom_operations(&caml_uint32_ops); caml_register_custom_operations(&caml_uint64_ops); return Val_unit; } ocaml-integers-0.3.0/top/000077500000000000000000000000001343204471400152275ustar00rootroot00000000000000ocaml-integers-0.3.0/top/dune000066400000000000000000000002451343204471400161060ustar00rootroot00000000000000(library (name integers_top) (public_name integers.top) (modes byte) (wrapped false) (synopsis "toplevel pretty printers") (libraries integers compiler-libs)) ocaml-integers-0.3.0/top/install_integer_printers.ml000066400000000000000000000023521343204471400226740ustar00rootroot00000000000000(* Adapted from Anil Madhavapeddy's ocaml-uri package. *) let _ = Integer_printers.format_sint let printers = [ "Integer_printers.format_sint"; "Integer_printers.format_long"; "Integer_printers.format_llong"; "Integer_printers.format_uchar"; "Integer_printers.format_uint8"; "Integer_printers.format_uint16"; "Integer_printers.format_uint32"; "Integer_printers.format_uint64"; "Integer_printers.format_ushort"; "Integer_printers.format_uint"; "Integer_printers.format_ulong"; "Integer_printers.format_ullong";] let eval_string ?(print_outcome = false) ?(err_formatter = Format.err_formatter) str = let lexbuf = Lexing.from_string str in let phrase = !Toploop.parse_toplevel_phrase lexbuf in Toploop.execute_phrase print_outcome err_formatter phrase let rec install_printers = function | [] -> true | printer :: printers -> let cmd = Printf.sprintf "#install_printer %s;;" printer in eval_string cmd && install_printers printers let () = if not (install_printers printers) then Format.eprintf "Problem installing integer-printers@." ocaml-integers-0.3.0/top/install_integer_printers.mli000066400000000000000000000000641343204471400230430ustar00rootroot00000000000000(* This file is intentionally left almost blank. *) ocaml-integers-0.3.0/top/integer_printers.ml000066400000000000000000000022651343204471400211510ustar00rootroot00000000000000(* * Copyright (c) 2013 Jeremy Yallop. * * This file is distributed under the terms of the MIT License. * See the file LICENSE for details. *) let format_sint fmt v = Format.fprintf fmt "" (Signed.SInt.to_string v) let format_long fmt v = Format.fprintf fmt "" (Signed.Long.to_string v) let format_llong fmt v = Format.fprintf fmt "" (Signed.LLong.to_string v) let format_uchar fmt v = Format.fprintf fmt "" (Unsigned.UChar.to_string v) let format_uint8 fmt v = Format.fprintf fmt "" (Unsigned.UInt8.to_string v) let format_uint16 fmt v = Format.fprintf fmt "" (Unsigned.UInt16.to_string v) let format_uint32 fmt v = Format.fprintf fmt "" (Unsigned.UInt32.to_string v) let format_uint64 fmt v = Format.fprintf fmt "" (Unsigned.UInt64.to_string v) let format_ushort fmt v = Format.fprintf fmt "" (Unsigned.UShort.to_string v) let format_uint fmt v = Format.fprintf fmt "" (Unsigned.UInt.to_string v) let format_ulong fmt v = Format.fprintf fmt "" (Unsigned.ULong.to_string v) let format_ullong fmt v = Format.fprintf fmt "" (Unsigned.ULLong.to_string v) ocaml-integers-0.3.0/top/integer_printers.mli000066400000000000000000000015141343204471400213160ustar00rootroot00000000000000(* * Copyright (c) 2013 Jeremy Yallop. * * This file is distributed under the terms of the MIT License. * See the file LICENSE for details. *) open Format val format_sint : formatter -> Signed.SInt.t -> unit val format_long : formatter -> Signed.Long.t -> unit val format_llong : formatter -> Signed.LLong.t -> unit val format_uchar : formatter -> Unsigned.UChar.t -> unit val format_uint8 : formatter -> Unsigned.UInt8.t -> unit val format_uint16 : formatter -> Unsigned.UInt16.t -> unit val format_uint32 : formatter -> Unsigned.UInt32.t -> unit val format_uint64 : formatter -> Unsigned.UInt64.t -> unit val format_ushort : formatter -> Unsigned.UShort.t -> unit val format_uint : formatter -> Unsigned.UInt.t -> unit val format_ulong : formatter -> Unsigned.ULong.t -> unit val format_ullong : formatter -> Unsigned.ULLong.t -> unit