From 70ecd5f92e887185fd3e09f603e4c5e1a06ef4f4 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Thu, 1 Jan 1970 00:00:00 +0000 Subject: [PATCH] Added some more common logical operators. --- lib/int63.mli | 47 +++++++++++++++++++++++++++++++++++------------ lib/int63_on_32.ml | 8 ++++++-- lib/int63_on_64.ml | 8 ++++++-- 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/lib/int63.mli b/lib/int63.mli index 3209d08..69fbce1 100644 --- a/lib/int63.mli +++ b/lib/int63.mli @@ -51,29 +51,52 @@ type t (** {2 Operators} - It is recommended to do [open Int63.Operators] in your code - so that you get the operators [+^], [-^] .. and the type [int63] - directly, and can still use the less frequent functions - as [Int63.logand] etc. + It is recommended to do: + +{[ +open Int63.Operators +]} + + in your code so that you get the operators [+^], [-^] .. and the + type [int63] directly, and can still use the less frequent + functions as {!Int63.logand} etc. *) module Operators : sig type int63 = t + (* For the gory details of the rules of operators, see: + * http://caml.inria.fr/pub/docs/manual-ocaml/lex.html + *) + val ( +^ ) : t -> t -> t val ( -^ ) : t -> t -> t val ( *^ ) : t -> t -> t val ( /^ ) : t -> t -> t - (** Arithmetic operators. *) + val ( %^ ) : t -> t -> t + (** Infix arithmetic operators. eg. [a +^ b -^ c] *) + + val ( <^< ) : t -> int -> t + val ( >^> ) : t -> int -> t + (** Infix shift left and logical shift right. + eg. [~^1 <^< 62] + + NB: We cannot use two less-than signs or two greater-than signs + in a row because that clashes with the symbols used for + camlp4 quotations. *) - val ( <<^ ) : t -> int -> t - val ( >>^ ) : t -> int -> t - (** Shift left and logical shift right. *) + val ( &&^ ) : t -> t -> t + val ( ||^ ) : t -> t -> t + val ( ^^^ ) : t -> t -> t + (** Infix logical and, or and xor operators. + eg. [bits &&^ mask] *) val ( ~^ ) : int -> t - (** Constant, eg. [~^0] is the constant zero. *) + (** Small integer constants, + eg. [~^0] is the constant zero and [~^1] is the constant one. *) val ( ~^~ ) : int -> t - (** Negative constant, eg. [~^~1] is the constant minus one. *) + (** Small negative integer constants, + eg. [~^~1] is the constant minus one. *) end (** {2 Functions} @@ -106,9 +129,9 @@ val abs : t -> t (** Absolute value. *) val max_int : t - (** The constant [2{^62}-1]. *) + (** The constant 2{^62}-1. *) val min_int : t - (** The constant [-2{^62}]. *) + (** The constant -2{^62}. *) val logand : t -> t -> t val logor : t -> t -> t diff --git a/lib/int63_on_32.ml b/lib/int63_on_32.ml index 37349ad..0b23d49 100644 --- a/lib/int63_on_32.ml +++ b/lib/int63_on_32.ml @@ -32,8 +32,12 @@ module Operators = struct external ( -^ ) : int64 -> int64 -> int64 = "%int64_sub" external ( *^ ) : int64 -> int64 -> int64 = "%int64_mul" external ( /^ ) : int64 -> int64 -> int64 = "%int64_div" - external ( <<^ ) : int64 -> int -> int64 = "%int64_lsl" - external ( >>^ ) : int64 -> int -> int64 = "%int64_lsr" + external ( %^ ) : int64 -> int64 -> int64 = "%int64_mod" + external ( <^< ) : int64 -> int -> int64 = "%int64_lsl" + external ( >^> ) : int64 -> int -> int64 = "%int64_lsr" + external ( &&^ ) : int64 -> int64 -> int64 = "%int64_and" + external ( ||^ ) : int64 -> int64 -> int64 = "%int64_or" + external ( ^^^ ) : int64 -> int64 -> int64 = "%int64_xor" external ( ~^ ) : int -> int64 = "%int64_of_int" let ( ~^~ ) i = Int64.neg (Int64.of_int i) end diff --git a/lib/int63_on_64.ml b/lib/int63_on_64.ml index 5679294..a1c9487 100644 --- a/lib/int63_on_64.ml +++ b/lib/int63_on_64.ml @@ -28,8 +28,12 @@ module Operators = struct external ( -^ ) : int -> int -> int = "%subint" external ( *^ ) : int -> int -> int = "%mulint" external ( /^ ) : int -> int -> int = "%divint" - external ( <<^ ) : int -> int -> int = "%lslint" - external ( >>^ ) : int -> int -> int = "%lsrint" + external ( %^ ) : int -> int -> int = "%modint" + external ( <^< ) : int -> int -> int = "%lslint" + external ( >^> ) : int -> int -> int = "%lsrint" + external ( &&^ ) : int -> int -> int = "%andint" + external ( ||^ ) : int -> int -> int = "%orint" + external ( ^^^ ) : int -> int -> int = "%xorint" external ( ~^ ) : int -> int = "%identity" external ( ~^~ ) : int -> int = "%negint" end -- 1.8.3.1