src/HOL/Library/Code_Bit_Shifts_for_Arithmetic.thy
author haftmann
Fri, 27 Jun 2025 08:09:26 +0200
changeset 82775 61c39a9e5415
parent 81813 8df58b532ecb
permissions -rw-r--r--
typo
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
81813
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/Library/Code_Bit_Shifts_for_Arithmetic.thy
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
     3
*)
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
     4
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
     5
section \<open>Rewrite arithmetic operations to bit-shifts if feasible\<close>
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
     6
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
     7
theory Code_Bit_Shifts_for_Arithmetic
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
     8
  imports Main
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
     9
begin
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    10
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    11
context semiring_bit_operations
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    12
begin
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    13
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    14
context
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    15
  includes bit_operations_syntax
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    16
begin
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    17
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    18
lemma [code_unfold]:
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    19
  \<open>of_bool (odd a) = a AND 1\<close>
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    20
  by (simp add: and_one_eq mod2_eq_if)
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    21
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    22
lemma [code_unfold]:
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    23
  \<open>even a \<longleftrightarrow> a AND 1 = 0\<close>
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    24
  by (simp add: and_one_eq mod2_eq_if)
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    25
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    26
lemma [code_unfold]:
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    27
  \<open>2 * a = push_bit 1 a\<close>
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    28
  by (simp add: ac_simps)
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    29
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    30
lemma [code_unfold]:
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    31
  \<open>a * 2 = push_bit 1 a\<close>
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    32
  by simp
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    33
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    34
lemma [code_unfold]:
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    35
  \<open>2 ^ n * a = push_bit n a\<close>
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    36
  by (simp add: push_bit_eq_mult ac_simps)
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    37
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    38
lemma [code_unfold]:
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    39
  \<open>a * 2 ^ n = push_bit n a\<close>
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    40
  by (simp add: push_bit_eq_mult)
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    41
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    42
lemma [code_unfold]:
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    43
  \<open>a div 2 = drop_bit 1 a\<close>
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    44
  by (simp add: drop_bit_eq_div)
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    45
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    46
lemma [code_unfold]:
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    47
  \<open>a div 2 ^ n = drop_bit n a\<close>
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    48
  by (simp add: drop_bit_eq_div)
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    49
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    50
lemma [code_unfold]:
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    51
  \<open>a mod 2 = take_bit 1 a\<close>
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    52
  by (simp add: take_bit_eq_mod)
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    53
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    54
lemma [code_unfold]:
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    55
  \<open>a mod 2 ^ n  = take_bit n a\<close>
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    56
  by (simp add: take_bit_eq_mod)
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    57
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    58
end
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    59
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    60
end
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    61
8df58b532ecb theory to rewrite arithmetic operations to bit shifts
haftmann
parents:
diff changeset
    62
end