include "cerco/BitVector.ma". include "cerco/Util.ma". definition nat_of_bool ≝ λb: bool. match b with [ false ⇒ O | true ⇒ S O ]. definition add_n_with_carry: ∀n: nat. ∀b, c: BitVector n. ∀carry: bool. (BitVector n) × (BitVector 3) ≝ λn: nat. λb: BitVector n. λc: BitVector n. λcarry: bool. let b_as_nat ≝ nat_of_bitvector n b in let c_as_nat ≝ nat_of_bitvector n c in let carry_as_nat ≝ nat_of_bool carry in let result_old ≝ b_as_nat + c_as_nat + carry_as_nat in let ac_flag ≝ geb ((modulus b_as_nat (2 * n)) + (modulus c_as_nat (2 * n)) + c_as_nat) (2 * n) in let bit_xxx ≝ geb ((modulus b_as_nat (2^(n - 1))) + (modulus c_as_nat (2^(n - 1))) + c_as_nat) (2^(n - 1)) in let result ≝ modulus result_old (2^n) in let cy_flag ≝ geb result_old (2^n) in let ov_flag ≝ exclusive_disjunction cy_flag bit_xxx in pair ? ? (bitvector_of_nat n result) ([[ cy_flag ; ac_flag ; ov_flag ]]). definition sub_n_with_carry: ∀n: nat. ∀b,c: BitVector n. ∀carry: bool. (BitVector n) × (BitVector 3) ≝ λn: nat. λb: BitVector n. λc: BitVector n. λcarry: bool. let b_as_nat ≝ nat_of_bitvector n b in let c_as_nat ≝ nat_of_bitvector n c in let carry_as_nat ≝ nat_of_bool carry in let temporary ≝ (b_as_nat mod (2 * n)) - (c_as_nat mod (2 * n)) in let ac_flag ≝ ltb (b_as_nat mod (2 * n)) ((c_as_nat mod (2 * n)) + carry_as_nat) in let bit_six ≝ ltb (b_as_nat mod (2^(n - 1))) ((c_as_nat mod (2^(n - 1))) + carry_as_nat) in let 〈b',cy_flag〉 ≝ if geb b_as_nat (c_as_nat + carry_as_nat) then 〈b_as_nat, false〉 else 〈b_as_nat + (2^n), true〉 in let ov_flag ≝ exclusive_disjunction cy_flag bit_six in 〈bitvector_of_nat n ((b' - c_as_nat) - carry_as_nat), [[ cy_flag; ac_flag; ov_flag ]]〉. definition add_8_with_carry ≝ add_n_with_carry 8. definition add_16_with_carry ≝ add_n_with_carry 16. definition sub_8_with_carry ≝ sub_n_with_carry 8. definition sub_16_with_carry ≝ sub_n_with_carry 16. definition increment ≝ λn: nat. λb: BitVector n. let b_as_nat ≝ (nat_of_bitvector n b) + 1 in let overflow ≝ geb b_as_nat 2^n in match overflow with [ false ⇒ bitvector_of_nat n b_as_nat | true ⇒ zero n ]. definition decrement ≝ λn: nat. λb: BitVector n. let b_as_nat ≝ nat_of_bitvector n b in match b_as_nat with [ O ⇒ maximum n | S o ⇒ bitvector_of_nat n o ]. definition two_complement_negation ≝ λn: nat. λb: BitVector n. let new_b ≝ negation_bv n b in increment n new_b. definition addition_n ≝ λn: nat. λb, c: BitVector n. let 〈res,flags〉 ≝ add_n_with_carry n b c false in res. definition subtraction ≝ λn: nat. λb, c: BitVector n. addition_n n b (two_complement_negation n c). definition multiplication ≝ λn: nat. λb, c: BitVector n. let b_nat ≝ nat_of_bitvector ? b in let c_nat ≝ nat_of_bitvector ? c in let result ≝ b_nat * c_nat in bitvector_of_nat (n + n) result. definition division_u ≝ λn: nat. λb, c: BitVector n. let b_nat ≝ nat_of_bitvector ? b in let c_nat ≝ nat_of_bitvector ? c in match c_nat with [ O ⇒ None ? | _ ⇒ let result ≝ b_nat ÷ c_nat in Some ? (bitvector_of_nat n result) ]. definition division_s: ∀n. ∀b, c: BitVector n. option (BitVector n) ≝ λn. match n with [ O ⇒ λb, c. None ? | S p ⇒ λb, c: BitVector (S p). let b_sign_bit ≝ get_index_v ? ? b O ? in let c_sign_bit ≝ get_index_v ? ? c O ? in let b_as_nat ≝ nat_of_bitvector ? b in let c_as_nat ≝ nat_of_bitvector ? c in match c_as_nat with [ O ⇒ None ? | S o ⇒ match b_sign_bit with [ true ⇒ let temp_b ≝ b_as_nat - (2^p) in match c_sign_bit with [ true ⇒ let temp_c ≝ c_as_nat - (2^p) in Some ? (bitvector_of_nat ? (temp_b ÷ temp_c)) | false ⇒ let result ≝ (temp_b ÷ c_as_nat) + (2^p) in Some ? (bitvector_of_nat ? result) ] | false ⇒ match c_sign_bit with [ true ⇒ let temp_c ≝ c_as_nat - (2^p) in let result ≝ (b_as_nat ÷ temp_c) + (2^p) in Some ? (bitvector_of_nat ? result) | false ⇒ Some ? (bitvector_of_nat ? (b_as_nat ÷ c_as_nat)) ] ] ] ]. // qed. definition modulus_u ≝ λn. λb, c: BitVector n. let b_nat ≝ nat_of_bitvector ? b in let c_nat ≝ nat_of_bitvector ? c in let result ≝ modulus b_nat c_nat in bitvector_of_nat (n + n) result. definition modulus_s ≝ λn. λb, c: BitVector n. match division_s n b c with [ None ⇒ None ? | Some result ⇒ let 〈high_bits, low_bits〉 ≝ split bool ? n (multiplication n result c) in Some ? (subtraction n b low_bits) ]. definition lt_u ≝ λn. λb, c: BitVector n. let b_nat ≝ nat_of_bitvector ? b in let c_nat ≝ nat_of_bitvector ? c in ltb b_nat c_nat. definition gt_u ≝ λn, b, c. lt_u n c b. definition lte_u ≝ λn, b, c. ¬(gt_u n b c). definition gte_u ≝ λn, b, c. ¬(lt_u n b c). definition lt_s ≝ λn. λb, c: BitVector n. let 〈result, flags〉 ≝ sub_n_with_carry n b c false in let ov_flag ≝ get_index_v ? ? flags 2 ? in if ov_flag then true else ((match n return λn'.BitVector n' → bool with [ O ⇒ λ_.false | S o ⇒ λresult'.(get_index_v ? ? result' O ?) ]) result). // qed. definition gt_s ≝ λn,b,c. lt_s n c b. definition lte_s ≝ λn,b,c. ¬(gt_s n b c). definition gte_s ≝ λn. λb, c. ¬(lt_s n b c). alias symbol "greater_than_or_equal" (instance 1) = "nat greater than or equal prop". definition bitvector_of_bool: ∀n: nat. ∀b: bool. BitVector (S n) ≝ λn: nat. λb: bool. (pad n 1 [[b]])⌈n + 1 ↦ S n⌉. // qed. definition full_add ≝ λn: nat. λb, c: BitVector n. λd: Bit. fold_right2_i ? ? ? ( λn. λb1, b2: bool. λd: Bit × (BitVector n). let 〈c1,r〉 ≝ d in 〈(b1 ∧ b2) ∨ (c1 ∧ (b1 ∨ b2)), (exclusive_disjunction (exclusive_disjunction b1 b2) c1) ::: r〉) 〈d, [[ ]]〉 ? b c. definition half_add ≝ λn: nat. λb, c: BitVector n. full_add n b c false.