Clarify licensing for Debian.
[virt-df.git] / lib / int63_on_64.mli
1 (** 63 bit signed integer type *)
2 (* (C) Copyright 2008 Richard W.M. Jones, Red Hat Inc.
3
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2 of the License, or (at your option) any later version,
8    with the OCaml linking exception described in ../COPYING.LIB.
9
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with this library; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
18  *)
19
20 (** This module deals with creating an efficient 63 bit signed integer
21     type.
22
23     In OCaml, the basic [int] type is fast because it is unboxed, but
24     it has a different size on 32 and 64 bit platforms (31 and 63 bits
25     respectively).  If we want a large integer type we can use the
26     OCaml [int64] type, but that is boxed and hence operations are
27     slow, even on 64 bit platforms.
28
29     This type gives us a large integer (up to 63 bits, therefore fine
30     for things involving eg. large disk file sizes), but retains
31     efficiency.  On 64 bit platforms it degenerates into just a normal
32     [int], hence unboxed and fast.  On 32 bit platforms it degenerates
33     to a kind of [int64], which is boxed and slow but hey 32 bit
34     platforms are going the way of all things anyway.
35
36     The implementation of this type is in the files [int63_on_32.ml]
37     or [int63_on_64.ml] for 32 bit and 64 bit platforms respectively.
38     The appropriate implementation is copied by the Makefile to
39     [int63.ml].
40 *)
41
42 (** {2 Type Int63.t}
43
44     For reasons not fully understood, we need to declare this
45     type explicitly in order to get properly optimized operations.
46     This means the user of a 64 bit platform must be careful not to
47     accidentally write code which depends on [t] being an [int],
48     but should treat this as an opaque type.
49 *)
50
51 type t = int
52
53 (** {2 Operators}
54
55     It is recommended to do:
56
57 {[
58 open Int63.Operators
59 ]}
60
61     in your code so that you get the operators [+^], [-^] .. and the
62     type [int63] directly, and can still use the less frequent
63     functions such as {!Int63.abs} etc.
64 *)
65
66 module Operators : sig
67   type int63 = t
68
69   (* For the gory details of the rules of operators, see:
70    * http://caml.inria.fr/pub/docs/manual-ocaml/lex.html
71    *)
72
73   val ( +^ ) : t -> t -> t
74   val ( -^ ) : t -> t -> t
75   val ( *^ ) : t -> t -> t
76   val ( /^ ) : t -> t -> t
77   val ( %^ ) : t -> t -> t
78     (** Infix arithmetic operators. eg. [a +^ b -^ c] *)
79
80   val ( <^< ) : t -> int -> t
81   val ( >^> ) : t -> int -> t
82     (** Infix shift left and logical shift right.
83         eg. [~^1 <^< 62]
84
85         NB: We cannot use two less-than signs or two greater-than signs
86         in a row because that clashes with the symbols used for
87         camlp4 quotations. *)
88
89   val ( &^ ) : t -> t -> t
90   val ( |^ ) : t -> t -> t
91   val ( ^^ ) : t -> t -> t
92     (** Infix logical and, or and xor operators.
93         eg. [bits &^ mask] *)
94
95   val ( ~^ ) : int -> t
96     (** Small integer constants,
97         eg. [~^0] is the constant zero and [~^1] is the constant one. *)
98   val ( ~^~ ) : int -> t
99     (** Small negative integer constants,
100         eg. [~^~1] is the constant minus one. *)
101 end
102
103 (** {2 Functions}
104
105     These functions are analogous to the similarly named
106     functions available in the standard library [Int32] and
107     [Int64] modules. *)
108
109 val zero : t
110 val one : t
111 val minus_one : t
112   (** Some constants. *)
113
114 val neg : t -> t
115   (** Negate. *)
116
117 val add : t -> t -> t
118 val sub : t -> t -> t
119 val mul : t -> t -> t
120 val div : t -> t -> t
121 val rem : t -> t -> t
122   (** Arithmetic. *)
123
124 val succ : t -> t
125   (** Successor. *)
126 val pred : t -> t
127   (** Predecessor. *)
128
129 val abs : t -> t
130   (** Absolute value. *)
131
132 val max_int : t
133   (** The constant 2{^62}-1. *)
134 val min_int : t
135   (** The constant -2{^62}. *)
136
137 val logand : t -> t -> t
138 val logor : t -> t -> t
139 val logxor : t -> t -> t
140 val lognot : t -> t
141   (** Bitwise logical and, or, xor and not. *)
142
143 val shift_left : t -> int -> t
144   (** Shift the number left by the integer number of bits. *)
145 val shift_right : t -> int -> t
146   (** Arithmetic shift the number right by the integer number of bits.
147       The sign bit is replicated into the vacated bits. *)
148 val shift_right_logical : t -> int -> t
149   (** Logical shift the number right by the integer number of bits.
150       The vacated bits are filled with bit [0] regardless of sign. *)
151
152 val of_int : int -> t
153 val to_int : t -> int
154 val of_float : float -> t
155 val to_float : t -> float
156 val of_int32 : int32 -> t
157 val to_int32 : t -> int32
158 val of_int64 : int64 -> t
159 val to_int64 : t -> int64
160 val of_nativeint : nativeint -> t
161 val to_nativeint : t -> nativeint
162   (** Convert between t and various standard types. *)
163
164 val of_string : string -> t
165 val to_string : t -> string
166   (** Convert between string. *)
167
168 val compare : t -> t -> int
169   (** Compare two numbers. *)