Skip to content

Commit aa5f962

Browse files
committed
rewrite cmov c,0,-1 to sbb
1 parent 8377bca commit aa5f962

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/Rewriter/Rules.v

+9-1
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,13 @@ Definition arith_with_casts_rewrite_rulesT (adc_no_carry_to_add : bool) : list (
395395
y ∈ ry -> y = Z.ones (Z.succ (Z.log2 y))
396396
-> cstZ rv (cstZ ry ('y) &' cstZ rx x) = cstZ rx x)
397397
]%Z%zrange
398+
; mymap
399+
do_again
400+
[ (forall c M rv r0 rM, 0 ∈ r0 -> M ∈ rM -> M ∈ rv -> 2^Z.log2 (M+1) = M + 1 -> 1 <= M ->
401+
cstZ rv (Z.zselect (cstZ r[0~>1] c) (cstZ r0 ('0)) (cstZ rM ('M)))
402+
= (dlet vc := cstZZ rv r[0~>1] (Z.sub_with_get_borrow_full ('(M+1)) (cstZ r[0~>1] c) 0 0) in
403+
cstZ rv (fst vc)))
404+
]
398405
; mymap
399406
do_again
400407
[ (* [do_again], so that we can trigger add/sub rules on the output *)
@@ -1213,7 +1220,8 @@ Section with_bitwidth.
12131220
:= Eval cbv [myapp mymap myflatten] in
12141221
mymap
12151222
dont_do_again
1216-
[(* no-op rule to prevent firing on selects between 0 and mask (since
1223+
[
1224+
(* no-op rule to prevent firing on selects between 0 and mask (since
12171225
these can be succinctly expressed as 0-c *)
12181226
(forall rc c,
12191227
singlewidth (Z.zselect (cstZ rc c)

src/Rewriter/RulesProofs.v

+5
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,11 @@ Proof using Type.
571571
all: repeat interp_good_t_step_arith.
572572
all: remove_casts; try fin_with_nia.
573573
all: try (reflect_hyps; lia).
574+
575+
{ (* cmov c 0 -1 -> sbb 0 0 c *)
576+
enough (- c mod (M + 1) = M) as E by (rewrite E; remove_casts; trivial).
577+
match goal with H5 : _ |- _ => apply unfold_is_bounded_by_bool in H5; cbn in H5 end.
578+
rewrite Z.mod_opp_l_nz; rewrite ?Z.mod_small; nia. }
574579
Qed.
575580

576581
Lemma strip_literal_casts_rewrite_rules_proofs

0 commit comments

Comments
 (0)