Compare commits

...

2 commits

Author SHA1 Message Date
e504cfebfe
add BoolOrIntType::copy_bits_from_bigint_wrapping and take BigInt arguments by reference
All checks were successful
/ deps (push) Successful in 17s
/ test (push) Successful in 5m21s
/ deps (pull_request) Successful in 14s
/ test (pull_request) Successful in 5m19s
2024-12-05 20:32:15 -08:00
9f42cab471
change to version 0.3.0 for breaking change 2024-12-05 20:26:28 -08:00
4 changed files with 28 additions and 25 deletions

8
Cargo.lock generated
View file

@ -301,7 +301,7 @@ checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5"
[[package]] [[package]]
name = "fayalite" name = "fayalite"
version = "0.2.1" version = "0.3.0"
dependencies = [ dependencies = [
"bitvec", "bitvec",
"blake3", "blake3",
@ -326,14 +326,14 @@ dependencies = [
[[package]] [[package]]
name = "fayalite-proc-macros" name = "fayalite-proc-macros"
version = "0.2.1" version = "0.3.0"
dependencies = [ dependencies = [
"fayalite-proc-macros-impl", "fayalite-proc-macros-impl",
] ]
[[package]] [[package]]
name = "fayalite-proc-macros-impl" name = "fayalite-proc-macros-impl"
version = "0.2.1" version = "0.3.0"
dependencies = [ dependencies = [
"base16ct", "base16ct",
"num-bigint", "num-bigint",
@ -347,7 +347,7 @@ dependencies = [
[[package]] [[package]]
name = "fayalite-visit-gen" name = "fayalite-visit-gen"
version = "0.2.1" version = "0.3.0"
dependencies = [ dependencies = [
"indexmap", "indexmap",
"prettyplease", "prettyplease",

View file

@ -5,7 +5,7 @@ resolver = "2"
members = ["crates/*"] members = ["crates/*"]
[workspace.package] [workspace.package]
version = "0.2.1" version = "0.3.0"
license = "LGPL-3.0-or-later" license = "LGPL-3.0-or-later"
edition = "2021" edition = "2021"
repository = "https://git.libre-chip.org/libre-chip/fayalite" repository = "https://git.libre-chip.org/libre-chip/fayalite"
@ -14,9 +14,9 @@ categories = ["simulation", "development-tools", "compilers"]
rust-version = "1.82.0" rust-version = "1.82.0"
[workspace.dependencies] [workspace.dependencies]
fayalite-proc-macros = { version = "=0.2.1", path = "crates/fayalite-proc-macros" } fayalite-proc-macros = { version = "=0.3.0", path = "crates/fayalite-proc-macros" }
fayalite-proc-macros-impl = { version = "=0.2.1", path = "crates/fayalite-proc-macros-impl" } fayalite-proc-macros-impl = { version = "=0.3.0", path = "crates/fayalite-proc-macros-impl" }
fayalite-visit-gen = { version = "=0.2.1", path = "crates/fayalite-visit-gen" } fayalite-visit-gen = { version = "=0.3.0", path = "crates/fayalite-visit-gen" }
base16ct = "0.2.0" base16ct = "0.2.0"
bitvec = { version = "1.0.1", features = ["serde"] } bitvec = { version = "1.0.1", features = ["serde"] }
blake3 = { version = "1.5.4", features = ["serde"] } blake3 = { version = "1.5.4", features = ["serde"] }

View file

@ -265,7 +265,7 @@ impl Neg {
}; };
let result_ty = retval.ty(); let result_ty = retval.ty();
retval.literal_bits = arg.to_literal_bits().map(|bits| { retval.literal_bits = arg.to_literal_bits().map(|bits| {
Intern::intern_owned(result_ty.bits_from_bigint_wrapping(-SInt::bits_to_bigint(&bits))) Intern::intern_owned(result_ty.bits_from_bigint_wrapping(&-SInt::bits_to_bigint(&bits)))
}); });
retval retval
} }
@ -372,7 +372,7 @@ fn binary_op_literal_bits<ResultTy: BoolOrIntType, Lhs: BoolOrIntType, Rhs: Bool
let rhs = Rhs::bits_to_bigint(&rhs); let rhs = Rhs::bits_to_bigint(&rhs);
let result = f(lhs, rhs)?; let result = f(lhs, rhs)?;
Ok(Intern::intern_owned( Ok(Intern::intern_owned(
result_ty.bits_from_bigint_wrapping(result), result_ty.bits_from_bigint_wrapping(&result),
)) ))
} }
@ -1347,7 +1347,7 @@ macro_rules! binary_op_fixed_shift {
literal_bits: Err(NotALiteralExpr), literal_bits: Err(NotALiteralExpr),
}; };
retval.literal_bits = lhs.to_literal_bits().map(|bits| { retval.literal_bits = lhs.to_literal_bits().map(|bits| {
Intern::intern_owned(retval.ty().bits_from_bigint_wrapping($Trait::$method( Intern::intern_owned(retval.ty().bits_from_bigint_wrapping(&$Trait::$method(
$ty::bits_to_bigint(&bits), $ty::bits_to_bigint(&bits),
rhs, rhs,
))) )))
@ -1624,7 +1624,7 @@ macro_rules! impl_cast_int_op {
ty, ty,
literal_bits: arg.to_literal_bits().map(|bits| { literal_bits: arg.to_literal_bits().map(|bits| {
Intern::intern_owned( Intern::intern_owned(
ty.bits_from_bigint_wrapping($from::bits_to_bigint(&bits)), ty.bits_from_bigint_wrapping(&$from::bits_to_bigint(&bits)),
) )
}), }),
} }

View file

@ -202,17 +202,17 @@ macro_rules! impl_int {
bit_width: self.width(), bit_width: self.width(),
} }
} }
pub fn bits_from_bigint_wrapping(self, v: BigInt) -> BitVec { pub fn bits_from_bigint_wrapping(self, v: &BigInt) -> BitVec {
BoolOrIntType::bits_from_bigint_wrapping(self, v) BoolOrIntType::bits_from_bigint_wrapping(self, v)
} }
pub fn from_bigint_wrapping(self, v: BigInt) -> $value<Width> { pub fn from_bigint_wrapping(self, v: &BigInt) -> $value<Width> {
$value { $value {
bits: Arc::new(self.bits_from_bigint_wrapping(v)), bits: Arc::new(self.bits_from_bigint_wrapping(v)),
_phantom: PhantomData, _phantom: PhantomData,
} }
} }
pub fn from_int_wrapping(self, v: impl Into<BigInt>) -> $value<Width> { pub fn from_int_wrapping(self, v: impl Into<BigInt>) -> $value<Width> {
self.from_bigint_wrapping(v.into()) self.from_bigint_wrapping(&v.into())
} }
pub fn zero(self) -> $value<Width> { pub fn zero(self) -> $value<Width> {
self.from_int_wrapping(0u8) self.from_int_wrapping(0u8)
@ -234,7 +234,7 @@ macro_rules! impl_int {
fn new(width: Width::SizeType) -> Self { fn new(width: Width::SizeType) -> Self {
$name { width } $name { width }
} }
fn value_from_bigint_wrapping(self, v: BigInt) -> Self::Value { fn value_from_bigint_wrapping(self, v: &BigInt) -> Self::Value {
$value::<Width>::from_bigint_wrapping(self, v) $value::<Width>::from_bigint_wrapping(self, v)
} }
fn bits_to_value(bits: Cow<'_, BitSlice>) -> Self::Value { fn bits_to_value(bits: Cow<'_, BitSlice>) -> Self::Value {
@ -378,7 +378,7 @@ macro_rules! impl_int {
self.bits.len() self.bits.len()
} }
} }
pub fn from_bigint_wrapping(ty: $name<Width>, v: BigInt) -> $value<Width> { pub fn from_bigint_wrapping(ty: $name<Width>, v: &BigInt) -> $value<Width> {
ty.from_bigint_wrapping(v) ty.from_bigint_wrapping(v)
} }
pub fn to_bigint(&self) -> BigInt { pub fn to_bigint(&self) -> BigInt {
@ -603,20 +603,23 @@ pub trait BoolOrIntType: Type + sealed::BoolOrIntTypeSealed {
UIntType::new(Self::Width::from_usize(self.width())) UIntType::new(Self::Width::from_usize(self.width()))
} }
fn value_from_int_wrapping(self, v: impl Into<BigInt>) -> Self::Value { fn value_from_int_wrapping(self, v: impl Into<BigInt>) -> Self::Value {
self.value_from_bigint_wrapping(v.into()) self.value_from_bigint_wrapping(&v.into())
} }
fn value_from_bigint_wrapping(self, v: BigInt) -> Self::Value; fn value_from_bigint_wrapping(self, v: &BigInt) -> Self::Value;
fn bits_from_bigint_wrapping(self, v: BigInt) -> BitVec { fn bits_from_bigint_wrapping(self, v: &BigInt) -> BitVec {
let width = self.width(); let mut bits = BitVec::repeat(false, self.width());
Self::copy_bits_from_bigint_wrapping(v, &mut bits);
bits
}
fn copy_bits_from_bigint_wrapping(v: &BigInt, bits: &mut BitSlice) {
let width = bits.len();
let mut bytes = v.to_signed_bytes_le(); let mut bytes = v.to_signed_bytes_le();
bytes.resize( bytes.resize(
width.div_ceil(u8::BITS as usize), width.div_ceil(u8::BITS as usize),
if v.is_negative() { 0xFF } else { 0 }, if v.is_negative() { 0xFF } else { 0 },
); );
let bitslice = &BitSlice::<u8, Lsb0>::from_slice(&bytes)[..width]; let bitslice = &BitSlice::<u8, Lsb0>::from_slice(&bytes)[..width];
let mut bits = BitVec::new(); bits.clone_from_bitslice(bitslice);
bits.extend_from_bitslice(bitslice);
bits
} }
fn bits_to_bigint(bits: &BitSlice) -> BigInt { fn bits_to_bigint(bits: &BitSlice) -> BigInt {
let sign_byte = if Self::Signed::VALUE && bits.last().as_deref().copied().unwrap_or(false) { let sign_byte = if Self::Signed::VALUE && bits.last().as_deref().copied().unwrap_or(false) {
@ -712,7 +715,7 @@ impl BoolOrIntType for Bool {
Bool Bool
} }
fn value_from_bigint_wrapping(self, v: BigInt) -> Self::Value { fn value_from_bigint_wrapping(self, v: &BigInt) -> Self::Value {
v.bit(0) v.bit(0)
} }