Skip to content

Commit c177f8e

Browse files
authored
Add ZRange.bitwidth, ZRange.of_bitwidth (#2041)
1 parent 2d55a49 commit c177f8e

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/AbstractInterpretation/ZRange.v

+34
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,28 @@ Module Compilers.
5353
| base.type.list A => list (interp A)
5454
| base.type.option A => option (interp A)
5555
end%type.
56+
Fixpoint bitwidth {t} : interp t -> binterp t
57+
:= match t with
58+
| base.type.type_base base.type.Z => fun r => ZRange.bitwidth r
59+
| base.type.type_base _ as t
60+
| base.type.unit as t
61+
=> fun x => x
62+
| base.type.prod A B => fun '(a, b) => (@bitwidth A a, @bitwidth B b)
63+
| base.type.list A => List.map (@bitwidth A)
64+
| base.type.option A => option_map (@bitwidth A)
65+
end.
66+
Fixpoint of_bitwidth (signed : bool) {t} : binterp t -> interp t
67+
:= match t with
68+
| base.type.type_base base.type.Z => fun r => ZRange.of_bitwidth signed r
69+
| base.type.type_base _ as t
70+
| base.type.unit as t
71+
=> fun x => x
72+
| base.type.prod A B => fun '(a, b) => (@of_bitwidth signed A a, @of_bitwidth signed B b)
73+
| base.type.list A => List.map (@of_bitwidth signed A)
74+
| base.type.option A => option_map (@of_bitwidth signed A)
75+
end%type.
76+
Notation of_bitwidth_unsigned := (@of_bitwidth false).
77+
Notation of_bitwidth_signed := (@of_bitwidth true).
5678
Fixpoint map_ranges (f : zrange -> zrange) {t} : interp t -> interp t
5779
:= match t with
5880
| base.type.type_base base.type.Z => f
@@ -406,6 +428,18 @@ Module Compilers.
406428
| type.base x => @base.is_bounded_by x
407429
| type.arrow s d => fun _ _ => false
408430
end.
431+
Fixpoint bitwidth (signed : bool) {t} : interp t -> einterp t
432+
:= match t with
433+
| type.base x => @base.bitwidth x
434+
| type.arrow s d => fun fr sz => @bitwidth signed d (fr (@of_bitwidth signed s sz))
435+
end
436+
with of_bitwidth (signed : bool) {t} : einterp t -> interp t
437+
:= match t with
438+
| type.base x => @base.of_bitwidth signed x
439+
| type.arrow s d => fun fr sz => @of_bitwidth signed d (fr (@bitwidth signed s sz))
440+
end.
441+
Notation of_bitwidth_unsigned := (@of_bitwidth false).
442+
Notation of_bitwidth_signed := (@of_bitwidth true).
409443
Module option.
410444
(** turn a [type] into a [Set] describing the type of optional
411445
bounds on that base type; bounds on a [Z] may be either a

src/Util/ZRange/Operations.v

+12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ Module ZRange.
1111

1212
Local Notation eta v := r[ lower v ~> upper v ].
1313

14+
Definition bitwidth (v : zrange) : Z
15+
:= let bitwidth_of z := (if (z =? 0) then 0 else if (z <? 0) then Z.log2_up (-z) else Z.log2_up (z+1))%Z in
16+
Z.max (bitwidth_of (lower v)) (bitwidth_of (upper v)).
17+
18+
Definition of_bitwidth (signed : bool) (bitwidth : Z) : zrange
19+
:= if signed
20+
then r[ -2 ^ (bitwidth - 1) ~> 2 ^ (bitwidth - 1) - 1 ]
21+
else r[ 0 ~> 2 ^ bitwidth - 1 ].
22+
23+
Notation of_bitwidth_unsigned := (of_bitwidth false).
24+
Notation of_bitwidth_signed := (of_bitwidth true).
25+
1426
Definition flip (v : zrange) : zrange
1527
:= r[ upper v ~> lower v ].
1628

0 commit comments

Comments
 (0)