1
(* $Id: netnumber.mli 1558 2011-03-04 17:15:46Z gerd $ *)
3
(** Binary encodings of numbers *)
5
(** This is the successor of the {!Rtypes} module *)
7
(** {2 Numeric types} *)
10
(* 4- and 8-bytes representation of signed integers *)
13
(** 32 bit signed integer *)
16
(** 64 bit signed integer *)
18
(* 4- and 8-bytes representation of non-negative integers *)
21
(** 32 bit unsigned integer *)
24
(** 64 bit unsigned integer *)
26
(* Floating-point numbers of single and double precision according to IEEE *)
29
(** single precision float (IEEE "float") *)
32
(** double precision float (IEEE "double") *)
34
exception Cannot_represent of string
35
(** raised if a conversion can't be done *)
37
exception Out_of_range
38
(** raised if string position out of range *)
42
(** {2 Basic encoding/decoding functions} *)
44
val mk_int4 : char * char * char * char -> int4
45
val mk_int8 : char * char * char * char * char * char * char * char -> int8
46
val mk_uint4 : char * char * char * char -> uint4
47
val mk_uint8 : char * char * char * char * char * char * char * char -> uint8
48
(** [mk_]<t> create integer values from character tuples. In these tuples
49
* the MSB is the first component and the LSB the last.
53
(* destroy integers and get tuples *)
55
val dest_int4 : int4 -> char * char * char * char
56
val dest_int8 : int8 -> char * char * char * char * char * char * char * char
57
val dest_uint4 : uint4 -> char * char * char * char
58
val dest_uint8 : uint8 -> char * char * char * char * char * char * char * char
59
(** [dest_]<t> destroy integer values and returns the corresponding char
63
val mk_fp4 : char * char * char * char -> fp4
64
val mk_fp8 : char * char * char * char * char * char * char * char -> fp8
65
val dest_fp4 : fp4 -> char * char * char * char
66
val dest_fp8 : fp8 -> char * char * char * char * char * char * char * char
69
(** {2 Conversions} *)
71
(** Conversions from int to (u)int and vice versa.
72
* On 32-bit computers, the type [int] can hold 31-bit signed integers
73
* (including the sign, i.e. one bit cannot be used).
74
* On 64-bit computers, the type [int] can hold 63-bit signed integers
75
* (including the sign, i.e. one bit cannot be used).
76
* The [int_of_xxx] functions raise [Cannot_represent] if the number to
77
* convert is too big (or too small) to be represented as [int]. Note
78
* that this depends on the word size of your architecture.
81
val int_of_int4 : int4 -> int
82
val int_of_uint4 : uint4 -> int
83
val int_of_int8 : int8 -> int
84
val int_of_uint8 : uint8 -> int
86
val int4_of_int : int -> int4
87
val uint4_of_int : int -> uint4
88
val int8_of_int : int -> int8
89
val uint8_of_int : int -> uint8
91
val int32_of_int4 : int4 -> int32
92
val int32_of_uint4 : uint4 -> int32
93
val int32_of_int8 : int8 -> int32
94
val int32_of_uint8 : uint8 -> int32
96
val int4_of_int32 : int32 -> int4
97
val uint4_of_int32 : int32 -> uint4
98
val int8_of_int32 : int32 -> int8
99
val uint8_of_int32 : int32 -> uint8
101
val int64_of_int4 : int4 -> int64
102
val int64_of_uint4 : uint4 -> int64
103
val int64_of_int8 : int8 -> int64
104
val int64_of_uint8 : uint8 -> int64
106
val int4_of_int64 : int64 -> int4
107
val uint4_of_int64 : int64 -> uint4
108
val int8_of_int64 : int64 -> int8
109
val uint8_of_int64 : int64 -> uint8
111
(** Casts from [uint4]/[uint8] to [int32]/[int64]. Here, the sign is ignored and
112
* simply considered as a bit.
115
val logical_uint4_of_int32 : int32 -> uint4
116
val logical_int32_of_uint4 : uint4 -> int32
117
val logical_uint8_of_int64 : int64 -> uint8
118
val logical_int64_of_uint8 : uint8 -> int64
120
val fp8_of_fp4 : fp4 -> fp8
121
val fp4_of_fp8 : fp8 -> fp4
122
(** Note [fp4_of_fp8]: This conversion is not exact. It is quite
123
* normal that precision is lost. Numbers too small or too large
124
* for fp4 are converted to the "infinity" value.
127
val float_of_fp4 : fp4 -> float
128
val float_of_fp8 : fp8 -> float
129
val fp4_of_float : float -> fp4
130
val fp8_of_float : float -> fp8
131
(** Note fp4_of_float: The same problems as in fp4_of_fp8 may arise *)
133
(** {2 Comparisons} *)
135
(** The comparisons "=" and "<>" work for all numbers.
137
For signed integers, the operators "<", "<=", ">", and ">=" work, too.
138
The unsigned integer type use representation that are not compatible
139
with these operators, and the following functions need to be called.
141
For [fp4] and [fp8] there are no comparison functions - convert to
142
[float] first and compare then.
145
val lt_uint4 : uint4 -> uint4 -> bool
146
(** [lt_uint4] is true iff the first value is less than the second value
149
val le_uint4 : uint4 -> uint4 -> bool
150
val gt_uint4 : uint4 -> uint4 -> bool
151
val ge_uint4 : uint4 -> uint4 -> bool
152
(** Other comparisons *)
154
val lt_uint8 : uint8 -> uint8 -> bool
155
(** [lt_uint8] is true iff the first value is less than the second value
158
val le_uint8 : uint8 -> uint8 -> bool
159
val gt_uint8 : uint8 -> uint8 -> bool
160
val ge_uint8 : uint8 -> uint8 -> bool
161
(** Other comparisons *)
164
module type ENCDEC = sig
165
(** Encode/decode numbers as strings. These functions exist in two
167
- {!Netnumber.BE} implements network byte order (big endian)
168
- {!Netnumber.LE} implements little endian
171
val read_int4 : string -> int -> int4
172
val read_int8 : string -> int -> int8
173
val read_uint4 : string -> int -> uint4
174
val read_uint8 : string -> int -> uint8
175
(** [read_]<t> create integer values from the characters found at a
176
certain position in the string. Raises [Out_of_range] if the position
180
val read_int4_unsafe : string -> int -> int4
181
val read_int8_unsafe : string -> int -> int8
182
val read_uint4_unsafe : string -> int -> uint4
183
val read_uint8_unsafe : string -> int -> uint8
184
(** Same, but no index check *)
186
val write_int4 : string -> int -> int4 -> unit
187
val write_int8 : string -> int -> int8 -> unit
188
val write_uint4 : string -> int -> uint4 -> unit
189
val write_uint8 : string -> int -> uint8 -> unit
190
(** [write_]<t> copies the characters corresponding to the integer values
191
into the string at the given positions. Raises [Out_of_range] if the
195
val write_int4_unsafe : string -> int -> int4 -> unit
196
val write_int8_unsafe : string -> int -> int8 -> unit
197
val write_uint4_unsafe : string -> int -> uint4 -> unit
198
val write_uint8_unsafe : string -> int -> uint8 -> unit
199
(** [write_]<t>[_unsafe]: Same, but no index check. *)
202
val int4_as_string : int4 -> string
203
val int8_as_string : int8 -> string
204
val uint4_as_string : uint4 -> string
205
val uint8_as_string : uint8 -> string
206
(** <t>[_as_string]: Returns the corresponding string for an integer value
209
val fp4_as_string : fp4 -> string
210
val fp8_as_string : fp8 -> string
212
val read_fp4 : string -> int -> fp4
213
val read_fp8 : string -> int -> fp8
219
(** Encoders/decoders for big endian - network byte order *)
222
(** Encoders/decoders for little endian *)
225
(** Encoders/decoders for host byte order - which is either little
226
endian or big endian, depending on the CPU (or CPU mode)