76249
|
1 |
theory Note_on_signed_division_on_words
|
77884
|
2 |
imports "HOL-Library.Word" "HOL-Library.Centered_Division"
|
76249
|
3 |
begin
|
|
4 |
|
|
5 |
unbundle bit_operations_syntax
|
|
6 |
|
|
7 |
context semiring_bit_operations
|
|
8 |
begin
|
77568
|
9 |
|
76249
|
10 |
lemma take_bit_Suc_from_most:
|
|
11 |
\<open>take_bit (Suc n) a = 2 ^ n * of_bool (bit a n) OR take_bit n a\<close>
|
|
12 |
by (rule bit_eqI) (auto simp add: bit_simps less_Suc_eq)
|
77568
|
13 |
|
76249
|
14 |
end
|
77568
|
15 |
|
76249
|
16 |
context ring_bit_operations
|
|
17 |
begin
|
77568
|
18 |
|
76249
|
19 |
lemma signed_take_bit_exp_eq_int:
|
|
20 |
\<open>signed_take_bit m (2 ^ n) =
|
|
21 |
(if n < m then 2 ^ n else if n = m then - (2 ^ n) else 0)\<close>
|
|
22 |
by (rule bit_eqI) (auto simp add: bit_simps simp flip: mask_eq_exp_minus_1)
|
77568
|
23 |
|
76249
|
24 |
end
|
|
25 |
|
|
26 |
lift_definition signed_divide_word :: \<open>'a::len word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> (infixl \<open>wdiv\<close> 70)
|
77884
|
27 |
is \<open>\<lambda>a b. signed_take_bit (LENGTH('a) - Suc 0) a cdiv signed_take_bit (LENGTH('a) - Suc 0) b\<close>
|
76249
|
28 |
by (simp flip: signed_take_bit_decr_length_iff)
|
|
29 |
|
|
30 |
lift_definition signed_modulo_word :: \<open>'a::len word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> (infixl \<open>wmod\<close> 70)
|
77884
|
31 |
is \<open>\<lambda>a b. signed_take_bit (LENGTH('a) - Suc 0) a cmod signed_take_bit (LENGTH('a) - Suc 0) b\<close>
|
76249
|
32 |
by (simp flip: signed_take_bit_decr_length_iff)
|
|
33 |
|
|
34 |
lemma signed_take_bit_eq_wmod:
|
|
35 |
\<open>signed_take_bit n w = w wmod (2 ^ Suc n)\<close>
|
|
36 |
proof (transfer fixing: n)
|
|
37 |
show \<open>take_bit LENGTH('a) (signed_take_bit n (take_bit LENGTH('a) k)) =
|
77884
|
38 |
take_bit LENGTH('a) (signed_take_bit (LENGTH('a) - Suc 0) k cmod signed_take_bit (LENGTH('a) - Suc 0) (2 ^ Suc n))\<close> for k :: int
|
76249
|
39 |
proof (cases \<open>LENGTH('a) = Suc (Suc n)\<close>)
|
|
40 |
case True
|
|
41 |
then show ?thesis
|
77884
|
42 |
by (simp add: signed_take_bit_exp_eq_int signed_take_bit_take_bit cmod_minus_eq flip: power_Suc signed_take_bit_eq_cmod)
|
76249
|
43 |
next
|
|
44 |
case False
|
|
45 |
then show ?thesis
|
77884
|
46 |
by (auto simp add: signed_take_bit_exp_eq_int signed_take_bit_take_bit take_bit_signed_take_bit simp flip: power_Suc signed_take_bit_eq_cmod)
|
76249
|
47 |
qed
|
|
48 |
qed
|
77568
|
49 |
|
76249
|
50 |
end
|