@@ -2116,7 +2116,7 @@ theorem clzAux_le {x : BitVec w} {n : Nat} :
21162116 · simp [hn1]
21172117 · simp [hn1]; omega
21182118
2119- theorem clzAux_eq_iff {x : BitVec w} {n : Nat}:
2119+ theorem clzAux_eq_iff {x : BitVec w} {n : Nat} :
21202120 clzAux x n = (n + 1 ) ↔ (∀ i, i ≤ n → x.getLsbD i = false) := by
21212121 induction n
21222122 · case zero => simp [clzAux]
@@ -2144,34 +2144,62 @@ theorem clzAux_eq_iff {x : BitVec w} {n : Nat}:
21442144 apply h
21452145 omega
21462146
2147- theorem clzAux_le_of_ne_zero {x : BitVec w} (h : x ≠ 0 #w) : clzAux x n ≤ n:= by
2148- induction n generalizing x
2149- · case zero =>
2150- simp
2151-
2152- sorry
2147+ theorem clzAux_lt_iff {x : BitVec w} {n : Nat} :
2148+ clzAux x n < (n + 1 ) ↔ (∃ i, i ≤ n ∧ x.getLsbD i = true) := by
2149+ induction n
2150+ · case zero => simp [clzAux]
21532151 · case succ n ihn =>
2154- simp [clzAux]
2155- sorry
2152+ unfold clzAux
2153+ by_cases hxn: x.getLsbD (n + 1 )
2154+ · simp [hxn]
2155+ exists n + 1 , by omega
2156+ · simp [hxn]
2157+ have h1 : 1 + x.clzAux n < n + 1 + 1 ↔ x.clzAux n < n + 1 := by omega
2158+ simp [h1]
2159+ simp [ihn]
2160+ constructor
2161+ · intro h
2162+ obtain ⟨j, h2, h3⟩ := h
2163+ exists j, by omega
2164+ · intro h
2165+ obtain ⟨j, h2, h3⟩ := h
2166+ exists j
2167+ by_cases hjn : j = n + 1
2168+ · simp_all
2169+ · simp [show j ≤ n by omega]
2170+ exact h3
21562171
21572172/-- Count the number of leading zeroes. -/
2158- def clz {w : Nat} (x : BitVec w) : Nat := clzAux x (w - 1 )
2173+ def clz {w : Nat} (x : BitVec w) : Nat := if w = 0 then 0 else clzAux x (w - 1 )
21592174
21602175-- theorems about the exclusion of x = 0#w
21612176-- explain the hyoitheses
21622177-- y.clz = w ↔ y = 0#w (simp lemma)
21632178
2179+ theorem clz_le {x : BitVec w} :
2180+ clz x ≤ w := by
2181+ unfold clz
2182+ rcases w with _|w
2183+ · simp
2184+ · simp [clzAux_le]
2185+
2186+ theorem clz_zero : clz 0 #w = w := by
2187+ rcases w with _|w
2188+ · simp [clz]
2189+ · unfold clz
2190+ simp [clzAux_eq_iff]
2191+
21642192@[simp]
21652193theorem clz_eq_length_iff {x : BitVec w} :
2166- clz x = w ↔ x = 0 #w := by sorry
2167- -- by_cases hx : x = 0#w
2168- -- · unfold clz clzAux
2169- -- simp [ hx ]
2170- -- · simp [ hx ]
2171- -- unfold clz
2172- -- simp [ hx ]
2173-
2174- -- sorry
2194+ clz x = w ↔ x = 0 #w := by
2195+ constructor
2196+ · intro h
2197+ have := clz_le (x := x)
2198+
2199+
2200+ sorry
2201+ · intro h
2202+ simp [h, clz_zero]
21752203
21762204/--
21772205 preliminary overflow flag for fast umulOverflow circuit
0 commit comments