Trying to multiply.

This commit is contained in:
Rhinemann 2023-10-02 15:07:58 +03:00
parent aef2dc3760
commit 4c994a8fef
3 changed files with 97 additions and 43 deletions

View File

@ -5,8 +5,12 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[profile.dev.package."*"] # +
opt-level = "z" # Optimize library for size
[profile.release]
opt-level = 'z' # Optimize for size
#opt-level = 'z' # Optimize for size
opt-level = 3 # Optimize for speed
lto = true # Enable link-time optimization
codegen-units = 1 # Reduce number of codegen units to increase optimizations
panic = 'abort' # Abort on panic
@ -14,5 +18,4 @@ strip = true # Strip symbols from binary*
[dependencies]
text_io = "0.1.12"
prettytable-rs = "0.10.0"
tabled = "0.12.2"
tabled = "0.14.0"

View File

@ -1,4 +1,3 @@
// use prettytable::{Cell, Row, Table};
use std::collections::VecDeque;
use std::{cmp, fmt};
@ -11,9 +10,9 @@ fn capitalise(s: &str) -> String {
}
/// A basic register capable of storing binary data.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct BasicRegister {
/// A [Vec] that stores the binary data of the register.
/// A [VecDeque] that stores the binary data of the register.
memory: VecDeque<bool>,
}
@ -43,39 +42,27 @@ impl BasicRegister {
}
/// Shifts the register to the left by a specified number of steps, shifting in the provided values.
pub fn left_shift(
&mut self,
shift_in_value_default: Option<bool>,
bits_shifted_default: Option<i32>,
) -> VecDeque<bool> {
let shift_in_value: bool = shift_in_value_default.unwrap_or(false);
let bits_shifted: usize = bits_shifted_default.unwrap_or(1) as usize;
pub fn left_shifted(&self, shift_in_value: bool, bits_shifted: usize) -> Self {
let mut shifted_memory = self.memory.clone();
let mut shifted_bits: VecDeque<bool> = [].into();
for _i in 0..bits_shifted {
shifted_bits.push_back(self.memory.pop_front().unwrap());
self.memory.push_back(shift_in_value)
for _ in 0..bits_shifted {
shifted_memory.pop_front();
shifted_memory.push_back(shift_in_value)
}
shifted_bits
BasicRegister::new(shifted_memory)
}
/// Shifts the register to the left by a specified number of steps, shifting in the provided values.
pub fn right_shift(
&mut self,
shift_in_value_default: Option<bool>,
bits_shifted_default: Option<i32>,
) -> VecDeque<bool> {
let shift_in_value: bool = shift_in_value_default.unwrap_or(false);
let bits_shifted: usize = bits_shifted_default.unwrap_or(1) as usize;
pub fn right_shifted(&self, shift_in_value: bool, bits_shifted: i32) -> Self {
let mut shifted_memory = self.memory.clone();
let mut shifted_bits: VecDeque<bool> = [].into();
for _i in 0..bits_shifted {
shifted_bits.push_back(self.memory.pop_back().unwrap());
self.memory.push_front(shift_in_value)
shifted_memory.pop_back();
shifted_memory.push_front(shift_in_value)
}
shifted_bits
BasicRegister::new(shifted_memory)
}
/// Constructs a new BasicRegister from a given VecDeque<bool> as memory.
@ -104,6 +91,20 @@ impl BasicRegister {
builder.build().with(Style::modern()).to_string()
}
fn decrement(&mut self) {
let one = BasicRegister::new([true].into()).adjusted_by_size(self.len());
self.memory = binary_subtraction(self, &one).memory;
}
fn increment(&mut self) {
let one = BasicRegister::new([true].into()).adjusted_by_size(self.len());
self.memory = binary_sum(self, &one).memory;
}
fn non_zero(&mut self) -> bool {
self.memory.iter().any(|i| *i)
}
}
impl Default for BasicRegister {
@ -234,3 +235,32 @@ pub fn binary_subtraction(minuend: &BasicRegister, subtrahend: &BasicRegister) -
false => difference.negated(),
}
}
/// Multiplies the first term by the second term.
pub fn binary_multiplication_method_1(
first_term: &BasicRegister,
second_term: &BasicRegister,
) -> BasicRegister {
let n = first_term.len();
let mut rg1: BasicRegister = BasicRegister::new(vec![false; n].into());
let mut rg2: &BasicRegister = first_term;
let rg3: &BasicRegister = second_term;
let mut counter: BasicRegister = BasicRegister::new(char_to_bool_vecdeque(format!("{n:b}").chars().collect()));
let _i = 0;
while counter.non_zero() {
let value_to_check = rg2.memory[n - 1].clone();
if value_to_check {
rg1 = binary_sum(&rg1, rg3);
}
let new_rg2 = rg2.right_shifted(rg1.memory[n - 1], 1).clone();
rg2 = &rg2.right_shifted(rg1.memory[n - 1], 1);
rg1 = rg1.right_shifted(false, 1);
counter.decrement();
}
rg1.memory.append(&mut rg2.memory.clone());
rg1
}

View File

@ -1,15 +1,19 @@
#[allow(dead_code)]
#![allow(dead_code)]
mod bit_utilities;
use bit_utilities as bu;
use text_io::read;
fn input_handler(first_register: bu::BasicRegister, second_register: bu::BasicRegister) {
let (first_register, second_register) = bu::align_registers(first_register, second_register);
fn input_handler() {
let (mut first_register, mut second_register) = bu::align_registers(
bu::BasicRegister::new(bu::get_memory("first number")),
bu::BasicRegister::new(bu::get_memory("second number")),
);
println!();
println!("First register:\n{}", first_register.table());
println!("Second register:\n{}", second_register.table());
// println!();
// println!("First register:\n{}", first_register.table());
// println!("Second register:\n{}", second_register.table());
loop {
print!("\nChoose the operation:\n[a]ddition, [s]ubtraction, [m]ultiplication, [d]ivision, [q]uit\n>>> ");
@ -20,10 +24,24 @@ fn input_handler(first_register: bu::BasicRegister, second_register: bu::BasicRe
println!("Sum:\n{}\nResult (to copy): {sum:b}", sum.table())
}
"s" => {
let difference: bu::BasicRegister = bu::binary_subtraction(&first_register, &second_register);
println!("Difference:\n{}\nResult (to copy): {difference:b}", difference.table())
}
"m" => (),
let difference: bu::BasicRegister =
bu::binary_subtraction(&first_register, &second_register);
println!(
"Difference:\n{}\nResult (to copy): {difference:b}",
difference.table()
)
},
"m" => loop {
print!("\nChoose method to use (1-4): ");
let method_input: String = read!();
match method_input.as_str() {
"1" => {
println!("{}", bu::binary_multiplication_method_1(&first_register, &second_register));
break;
},
_ => println!("Not an available operation, try again."),
}
},
"d" => (),
"q" => std::process::exit(0x0100),
_ => println!("Not an available operation, try again."),
@ -32,8 +50,11 @@ fn input_handler(first_register: bu::BasicRegister, second_register: bu::BasicRe
}
fn main() {
let reg = bu::BasicRegister::new(bu::get_memory("your number"));
let reg2 = bu::BasicRegister::new(bu::get_memory("your number"));
input_handler(reg, reg2);
// let mut reg = bu::BasicRegister::new(bu::get_memory("your number"));
// let mut reg2 = bu::BasicRegister::new(bu::get_memory("your number"));
//
// println!("{reg}");
// println!("{reg2}");
// println!("{}", bu::binary_multiplication_method_1(reg, reg2))
input_handler();
}