Skip to content

Commit 829b2bd

Browse files
committed
CongruenceDomain: Implement sub idependently of add.
1 parent 98977b1 commit 829b2bd

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

src/cdomain/value/cdomains/int/congruenceDomain.ml

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -320,21 +320,24 @@ struct
320320
res
321321

322322
let sub ?(no_ov=false) ik x y =
323-
match y with
324-
| None -> raise (ArithmeticOnIntegerBot (Printf.sprintf "%s op %s" (show x) (show y)))
325-
| Some (c, m) ->
326-
let y =
327-
if m =: Z.zero then
328-
if no_ov then
329-
let c = Z.neg c in
330-
Some (c, m)
331-
else
332-
top_of ik
333-
else
334-
let m = m -: c in
335-
Some (c, m)
336-
in
337-
add ~no_ov ik x y
323+
let no_ov_case (c1, m1) (c2, m2) =
324+
c1 -: c2, Z.gcd m1 m2
325+
in
326+
match (x, y) with
327+
| None, None -> bot ()
328+
| None, _
329+
| _, None ->
330+
raise (ArithmeticOnIntegerBot (Printf.sprintf "%s op %s" (show x) (show y)))
331+
| Some a, Some b when no_ov ->
332+
normalize ik (Some (no_ov_case a b))
333+
| Some (c1, m1), Some (c2, m2) when m1 =: Z.zero && m2 =: Z.zero && not (Cil.isSigned ik) ->
334+
let _, max_ik = range ik in
335+
let m_ikind = max_ik +: Z.one in
336+
let c = (c1 -: c2 +: m_ikind) %: m_ikind in
337+
Some(c, Z.zero)
338+
| Some a, Some b when not (Cil.isSigned ik) ->
339+
handle_overflow ik (no_ov_case a b)
340+
| _ -> top ()
338341

339342
let sub ?no_ov ik x y =
340343
let res = sub ?no_ov ik x y in

0 commit comments

Comments
 (0)