Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CodeHawk/CH/chlib/cHBounds.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
The MIT License (MIT)

Copyright (c) 2005-2019 Kestrel Technology LLC
Copyright (c) 2020-2025 Henny B. Sipma
Copyright (c) 2026 Aarno Labs LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -140,6 +142,19 @@ object (self: 'a)
in
self#mkNew r

method div_trunc (a: 'a) =
let r = match (bound, a#getBound) with
| (PLUS_INF, PLUS_INF) -> PLUS_INF
| (MINUS_INF, MINUS_INF) -> PLUS_INF
| (PLUS_INF, MINUS_INF) -> MINUS_INF
| (MINUS_INF, PLUS_INF) -> MINUS_INF
| (PLUS_INF, NUMBER x) -> if x#geq numerical_zero then PLUS_INF else MINUS_INF
| (MINUS_INF, NUMBER x) -> if x#geq numerical_zero then MINUS_INF else PLUS_INF
| (NUMBER n, NUMBER d) -> NUMBER (n#div_trunc d)
| (NUMBER _, _) -> NUMBER numerical_zero
in
self#mkNew r

method toPretty =
match bound with
| PLUS_INF -> STR "+oo"
Expand Down
9 changes: 9 additions & 0 deletions CodeHawk/CH/chlib/cHBounds.mli
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
The MIT License (MIT)

Copyright (c) 2005-2019 Kestrel Technology LLC
Copyright (c) 2020-2025 Henny B. Sipma
Copyright (c) 2026 Aarno Labs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -36,6 +38,13 @@ class bound_t :
method add : 'a -> 'a
method div_ceiling : 'a -> 'a
method div_floor : 'a -> 'a

(** [b#div_trunc d] is the quotient of [b] and [d] truncated toward zero
(C11 6.5.5p6, ARM sdiv/udiv, Java idiv; the semantics of CHIF [DIV]).
Infinite operands: [±oo / n] is [±oo] (sign of the quotient),
[n / ±oo] is [0], [±oo / ±oo] is [±oo] (sign of the quotient).
The divisor must not be zero. *)
method div_trunc : 'a -> 'a
method equal : 'a -> bool
method geq : 'a -> bool
method getBound : bounds_t
Expand Down
6 changes: 5 additions & 1 deletion CodeHawk/CH/chlib/cHConstantPropagationNoArrays.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
The MIT License (MIT)

Copyright (c) 2005-2019 Kestrel Technology LLC
Copyright (c) 2020-2025 Henny B. Sipma
Copyright (c) 2026 Aarno Labs LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -96,7 +98,9 @@ class constant_propagation_no_arrays_t system =
| ASSIGN_NUM (v, DIV (x, y)) ->
begin
match (getNumConst x, getNumConst y) with
| (Some c1, Some c2) -> ASSIGN_NUM (v, NUM (c1#div c2))
(* CHIF DIV truncates toward zero (C, ARM sdiv/udiv, Java idiv) *)
| (Some c1, Some c2) when not c2#is_zero ->
ASSIGN_NUM (v, NUM (c1#div_trunc c2))
| _ -> cmd
end
| ASSIGN_SYM (v, SYM_VAR x) ->
Expand Down
5 changes: 4 additions & 1 deletion CodeHawk/CH/chlib/cHConstants.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

Copyright (c) 2005-2019 Kestrel Technology LLC
Copyright (c) 2020-2022 Henny B. Sipma
Copyright (c) 2023 Aarno Labs LLC
Copyright (c) 2023-2026 Aarno Labs LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -129,6 +129,9 @@ object (self: 'a)
match (cst, c#getCst) with
| (NUM_BOTTOM, _) -> {< >}
| (_, NUM_BOTTOM) -> c
(* CHIF DIV truncates toward zero (C, ARM sdiv/udiv, Java idiv) *)
| (NUM_CST c1, NUM_CST c2) when not c2#is_zero ->
self#mkNew (NUM_CST (c1#div_trunc c2))
| (_, _) -> self#mkNew NUM_TOP

method toPretty =
Expand Down
16 changes: 10 additions & 6 deletions CodeHawk/CH/chlib/cHIntervals.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
The MIT License (MIT)

Copyright (c) 2005-2019 Kestrel Technology LLC
Copyright (c) 2020-2025 Henny B. Sipma
Copyright (c) 2026 Aarno Labs LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -200,12 +202,14 @@ object (self : 'a)
else
let (a, b) = (self#getMin, self#getMax) in
let (a', b') = (i#getMin, i#getMax) in
let min_l = [a#div_floor a'; a#div_floor b'; b#div_floor a'; b#div_floor b'] in
let max_l = [a#div_ceiling a'; a#div_ceiling b'; b#div_ceiling a'; b#div_ceiling b'] in
(* If we deal only with integers then min is ceiling and max is floor *)
(* let min_l = [a#div_ceiling a'; a#div_ceiling b'; b#div_ceiling a'; b#div_ceiling b'] in *)
(* let max_l = [a#div_floor a'; a#div_floor b'; b#div_floor a'; b#div_floor b'] in *)
self#mkInterval (min_in_bounds min_l) (max_in_bounds max_l)
(* DIV is division truncated toward zero (C, ARM sdiv/udiv, Java idiv).
The divisor interval does not contain zero, hence has a single sign;
trunc(x / y) is then monotone in x for fixed y and in y for fixed x,
so the extremes are attained at the corners and the result is exact.
A corner +-oo / +-oo is matched by a corner n / +-oo = 0 whenever
the dividend has a finite bound. *)
let l = [a#div_trunc a'; a#div_trunc b'; b#div_trunc a'; b#div_trunc b'] in
self#mkInterval (min_in_bounds l) (max_in_bounds l)

method singleton =
if bottom then
Expand Down
47 changes: 31 additions & 16 deletions CodeHawk/CH/chlib/cHStridedIntervals.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
The MIT License (MIT)

Copyright (c) 2005-2019 Kestrel Technology LLC
Copyright (c) 2020-2025 Henny B. Sipma
Copyright (c) 2026 Aarno Labs LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -428,23 +430,36 @@ object (self : 'a)
if i#contains numerical_zero then
self#mkTop (* Not very helpful! *)
else
let min_l =
[ min#div_floor i#getMin;
min#div_floor i#getMax;
max#div_floor i#getMin;
max#div_floor i#getMax ]
and max_l =
[ min#div_ceiling i#getMin;
min#div_ceiling i#getMax;
max#div_ceiling i#getMin;
max#div_ceiling i#getMax ] in
let new_min = min_in_bounds min_l
and new_max = max_in_bounds max_l in
(* truncating division (CHIF DIV); bounds exact, see
CHIntervals.interval_t#div *)
let l =
[ min#div_trunc i#getMin;
min#div_trunc i#getMax;
max#div_trunc i#getMin;
max#div_trunc i#getMax ] in
let new_min = min_in_bounds l
and new_max = max_in_bounds l in
(* Divisor d with |d| dividing the stride s: if all values have the
same sign, v = r + k*s maps to an arithmetic progression with step
s/|d| (trunc(v/d) is affine in k on each side of zero). Its residue
is that of the quotient of a finite bound. If the range crosses
zero the progression breaks: {-7, 1, 9} / 4 = {-1, 0, 2} (except
for |d| = 1). *)
let zero = bound_of_num numerical_zero in
let same_sign = min#geq zero || max#leq zero in
let (new_stride, new_rem) =
if (i#isSingleton && (stride#modulo i#getMin#toNumber)#is_zero) then
(stride#div i#getMin#toNumber, rem#div i#getMin#toNumber)
else
(numerical_one, numerical_zero) (* Any other useful cases ? *)
match i#singleton with
| Some d when (same_sign || d#abs#equal numerical_one)
&& (stride#modulo d#abs)#is_zero ->
let s' = stride#div d#abs in
let q =
match (new_min#getBound, new_max#getBound) with
| (NUMBER q, _) | (_, NUMBER q) -> Some q
| _ -> None in
(match q with
| Some q -> (s', q#modulo s')
| None -> (numerical_one, numerical_zero))
| _ -> (numerical_one, numerical_zero)
in
self#mkInterval new_min new_max new_stride new_rem

Expand Down
8 changes: 5 additions & 3 deletions CodeHawk/CH/chlib/cHTIntervals.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
The MIT License (MIT)

Copyright (c) 2005-2019 Kestrel Technology LLC
Copyright (c) 2020-2025 Henny B. Sipma
Copyright (c) 2026 Aarno Labs LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -212,9 +214,9 @@ object (self : 'a)
else
let (a, b) = (self#getMin, self#getMax) in
let (a', b') = (i#getMin, i#getMax) in
let min_l = [a#div_floor a'; a#div_floor b'; b#div_floor a'; b#div_floor b'] in
let max_l = [a#div_ceiling a'; a#div_ceiling b'; b#div_ceiling a'; b#div_ceiling b'] in
self#mkInterval (min_in_bounds min_l) (max_in_bounds max_l)
(* truncating division; exact, see CHIntervals.interval_t#div *)
let l = [a#div_trunc a'; a#div_trunc b'; b#div_trunc a'; b#div_trunc b'] in
self#mkInterval (min_in_bounds l) (max_in_bounds l)

method singleton =
if bottom || not_assigned then
Expand Down
Loading
Loading