package mopsa

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file intBound.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
(****************************************************************************)
(*                                                                          *)
(* This file is part of MOPSA, a Modular Open Platform for Static Analysis. *)
(*                                                                          *)
(* Copyright (C) 2017-2019 The MOPSA Project.                               *)
(*                                                                          *)
(* This program is free software: you can redistribute it and/or modify     *)
(* it under the terms of the GNU Lesser General Public License as published *)
(* by the Free Software Foundation, either version 3 of the License, or     *)
(* (at your option) any later version.                                      *)
(*                                                                          *)
(* This program is distributed in the hope that it will be useful,          *)
(* but WITHOUT ANY WARRANTY; without even the implied warranty of           *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *)
(* GNU Lesser General Public License for more details.                      *)
(*                                                                          *)
(* You should have received a copy of the GNU Lesser General Public License *)
(* along with this program.  If not, see <http://www.gnu.org/licenses/>.    *)
(*                                                                          *)
(****************************************************************************)

(**
  IntBound - Enriches arbitrary precision integers with +∞ and -∞.

  Useful as interval bounds.
 *)


(** {2 Types} *)


type t = Finite of Z.t | MINF (** -∞ *) | PINF (** +∞ *)


(** {2 Internal utilities} *)



(** {2 Constructors} *)

let zero : t = Finite Z.zero
let one : t = Finite Z.one
let minus_one : t = Finite Z.minus_one
(** Useful constants *)

let of_int (x:int) : t = Finite (Z.of_int x)
let of_int64 (x:int64) : t = Finite (Z.of_int64 x)
(** Exact conversion from machine integers. *)

let of_float (x:float) : t =
  match classify_float x with
  | FP_infinite -> if x >= 0. then PINF else MINF
  | FP_nan -> invalid_arg "IntBound.of_float"
  | _ -> Finite (Z.of_float x)
(** Conversion with truncation from floats. Handles infinites. Raises an exception on NaNs. *)

let infinite (sign:int) : t =
  if sign > 0 then PINF
  else if sign < 0 then MINF
  else zero
(** Constructs an infinity with the given sign. Zero maps to zero. *)

let pow2 (nb:int) : t =
  Finite (Z.shift_left Z.one nb)
(** A power of two. The argument must be positive. *)



(** {2 Predicates} *)

let sign (x:t) : int =
  match x with MINF -> -1 | PINF -> 1 | Finite x -> Z.sign x
(** Sign of x: -1, 0, or 1. *)

let is_finite (x:t) : bool =
  match x with
  | Finite _ -> true
  | _ -> false
(** Whether x is finite or not. *)

let equal (x:t) (y:t) : bool =
  match x,y with
  | Finite a, Finite b -> Z.equal a b
  | MINF,MINF | PINF,PINF -> true
  | _ -> false
(** Equality comparison. = also works. *)

let compare (x:t) (y:t) : int =
  match x,y with
  | PINF,PINF | MINF,MINF -> 0
  | MINF,_ | _,PINF -> -1
  | PINF,_ | _,MINF -> 1
  | Finite x, Finite y -> Z.compare x y
(** Total order. Returns -1 (strictly smaller), 0 (equal), or 1 (strictly greater). *)

let leq (x:t) (y:t) : bool = compare x y <= 0
let geq (x:t) (y:t) : bool = compare x y >= 0
let lt  (x:t) (y:t) : bool = compare x y < 0
let gt  (x:t) (y:t) : bool = compare x y > 0
let eq  (x:t) (y:t) : bool = equal x y
let neq (x:t) (y:t) : bool = not (equal x y)
(** Comparison predicates. *)

let min (x:t) (y:t) : t = if leq x y then x else y
let max (x:t) (y:t) : t = if leq x y then y else x
(** Minimum and maximum. *)

let is_zero (x:t) : bool = sign x = 0
let is_nonzero (x:t) : bool = sign x <> 0
let is_positive (x:t) : bool = sign x >= 0
let is_negative (x:t) : bool = sign x <= 0
let is_positive_strict (x:t) : bool = sign x > 0
let is_negative_strict (x:t) : bool = sign x < 0
(** Sign predicates. *)

let hash (a:t) : int =
  match a with PINF -> 1 | MINF -> -1 | Finite x -> Z.hash x
(** Hashing function. *)


(** {2 Printing} *)

let to_string (x:t) : string =
  match x with
  | PINF -> "+∞"
  | MINF -> "-∞"
  | Finite x -> Z.to_string x

let print ch (x:t) = output_string ch (to_string x)
let fprint ch (x:t) = Format.pp_print_string ch (to_string x)
let bprint ch (x:t) = Buffer.add_string ch (to_string x)


(** {2 Operators} *)

let succ (a:t) : t =
  match a with Finite x -> Finite (Z.succ x) | _ -> a
(** +1. Infinities are left unchanged. *)

let pred (a:t) :t =
  match a with Finite x -> Finite (Z.pred x) | _ -> a
(** -1. Infinities are left unchanged. *)

let neg (a:t) : t =
  match a with MINF -> PINF | PINF -> MINF | Finite x -> Finite (Z.neg x)
(** Negation. *)

let abs (a:t) : t =
  match a with MINF | PINF -> PINF | Finite x -> Finite (Z.abs x)
(** Absolute value. *)

let add (a:t) (b:t) : t =
  match a, b with
  | PINF,MINF | MINF,PINF-> invalid_arg "IntBound.add"
  | PINF,_ | _,PINF -> PINF
  | MINF,_ | _,MINF -> MINF
  | Finite x, Finite y -> Finite (Z.add x y)
(** Addition. +∞ + -∞ is undefined (invalid argument exception). *)

let sub (a:t) (b:t) : t =
  match a, b with
  | PINF,PINF | MINF,MINF-> invalid_arg "IntBound.sub"
  | PINF,_ | _,MINF -> PINF
  | MINF,_ | _,PINF -> MINF
  | Finite x, Finite y -> Finite (Z.sub x y)
(** Subtraction. +∞ - +∞ is undefined (invalid argument exception). *)

let mul (a:t) (b:t) =
  match a, b with
  | Finite x, Finite y -> Finite (Z.mul x y)
  | _ -> infinite (sign a * sign b)
(** Multiplication. Always defined: +∞ * 0 = 0 *)

let div (a:t) (b:t) :t =
  match b with
  | PINF | MINF -> zero
  | Finite y ->
     match a with
     | PINF -> infinite (Z.sign y)
     | MINF -> infinite (-(Z.sign y))
     | Finite x ->
        if y = Z.zero then infinite (Z.sign x)
        else Finite (Z.div x y)
(** Division. Always defined: 0 / 0 = 0, x / +∞ = 0, x / 0 = sign(x) * ∞ *)

let ediv (a:t) (b:t) :t =
  match b with
  | PINF | MINF -> zero
  | Finite y ->
     match a with
     | PINF -> infinite (Z.sign y)
     | MINF -> infinite (-(Z.sign y))
     | Finite x ->
        if y = Z.zero then infinite (Z.sign x)
        else Finite (Z.ediv x y)
(** Euclidian division. Always defined: 0 / 0 = 0, x / +∞ = 0, x / 0 = sign(x) * ∞ *)

let rem (a:t) (b:t) : t =
  match a with
  | PINF | MINF -> invalid_arg "IntBound.rem INF"
  | Finite x ->
     match b with
     | PINF | MINF -> a
     | Finite y ->
        if y = Z.zero then invalid_arg "IntBound.rem ZERO"
        else Finite (Z.rem x y)
(** Remainder. rem x y has the sign of x, rem x (-y) = rem x y, and rem x +∞ = x.
    rem +∞ y and rem x 0 are undefined (invalid argument exception).
*)

let erem (a:t) (b:t) : t =
  match a with
  | PINF | MINF -> invalid_arg "IntBound.rem"
  | Finite x ->
     match b with
     | PINF | MINF -> a
     | Finite y ->
        if y = Z.zero then invalid_arg "IntBound.rem"
        else Finite (Z.erem x y)
(** Euclidian remainder. erem x y >= 0, rem x y < |b| and a = b * ediv a b + erem a b.
    rem +∞ y and rem x 0 are undefined (invalid argument exception).
*)

let shift_left (a:t) (b:t) : t =
  match a with
  | PINF -> PINF | MINF -> MINF
  | Finite x ->
     match b with
     | PINF -> infinite (sign a)
     | Finite y when Z.geq y Z.zero ->
       (try
          if Z.geq y (Z.of_int 2048) then
            (* let's avoid allocating xx GB memory of Z.t *)
            raise Z.Overflow
          else 
          let r = (Z.shift_left x (Z.to_int y)) in
          Finite r
        with Z.Overflow -> infinite (sign a)
       )
     | _ -> invalid_arg "IntBound.shift_left"
(** Left bitshift.
    Undefined if the second argument is negative (invalid argument exception).
    Returns an infinity if the second argument is too large.
 *)

let shift_right (a:t) (b:t) :t =
  match a with
  | PINF -> PINF | MINF -> MINF
  | Finite x ->
     match b with
     | PINF -> zero
     | Finite y when Z.geq y Z.zero ->
        (try Finite (Z.shift_right x (Z.to_int y))
         with Z.Overflow -> zero)
     | _ -> invalid_arg "IntBound.shift_right"
(** Right bitshift, rounding towards -∞.
    Undefined if the second argument is negative (invalid argument exception).
    Returns zero if the second argument is too large.
 *)

let shift_right_trunc (a:t) (b:t) : t =
  match a with
  | PINF -> PINF | MINF -> MINF
  | Finite x ->
     match b with
     | PINF -> zero
     | Finite y when Z.geq y Z.zero ->
        (try Finite (Z.shift_right_trunc x (Z.to_int y))
         with Z.Overflow -> zero)
     | _ -> invalid_arg "IntBound.shift_right_trunc"
(** Right bitshift, rounding towards 0 (truncation).
    Undefined if the second argument is negative (invalid argument exception).
    Returns zero if the second argument is too large.
*)

let only_finite msg op a b =
  match a,b with
  | Finite x, Finite y -> Finite (op x y)
  | _ -> invalid_arg msg

let bit_or : t -> t -> t = only_finite "IntBound.bit_or"  Z.logor
let bit_xor : t -> t -> t = only_finite "IntBound.bit_xor" Z.logxor
let bit_and : t -> t -> t = only_finite "IntBound.bit_and" Z.logand
(** Bitwise operations. Undefined for infinites (invalid argument exception). *)

let pow (a:t) (b:t) : t =
  match a, b with
  | Finite x, Finite y when Z.geq y Z.zero ->
    (try
       if Z.geq y (Z.of_int 2048) then
         (* let's avoid allocating xx GB memory of Z.t *)
         raise Z.Overflow
       else 
         Finite (Z.pow x (Z.to_int y))
     with Z.Overflow -> invalid_arg "IntBound.pow")
  | _ -> invalid_arg "IntBound.pow"
(** Power. Undefined if the second argument is negative or too large. *)
OCaml

Innovation. Community. Security.