add BoolOrIntType::copy_bits_from_bigint_wrapping and take BigInt arguments by reference
This commit is contained in:
		
							parent
							
								
									9f42cab471
								
							
						
					
					
						commit
						e504cfebfe
					
				
					 2 changed files with 20 additions and 17 deletions
				
			
		|  | @ -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)), | ||||||
|                         ) |                         ) | ||||||
|                     }), |                     }), | ||||||
|                 } |                 } | ||||||
|  |  | ||||||
|  | @ -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) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue