chore: new branch with some things from the bumpalo branch
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use std::{collections::HashMap, rc::Rc};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use pallas_primitives::babbage::Language;
|
||||
|
||||
@@ -975,7 +975,7 @@ impl Default for BuiltinCosts {
|
||||
}
|
||||
|
||||
impl BuiltinCosts {
|
||||
pub fn to_ex_budget_v2(&self, fun: DefaultFunction, args: &[Rc<Value>]) -> ExBudget {
|
||||
pub fn to_ex_budget_v2(&self, fun: DefaultFunction, args: &[Value]) -> ExBudget {
|
||||
match fun {
|
||||
DefaultFunction::AddInteger => ExBudget {
|
||||
mem: self
|
||||
@@ -1402,7 +1402,7 @@ impl BuiltinCosts {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_ex_budget_v1(&self, fun: DefaultFunction, args: &[Rc<Value>]) -> ExBudget {
|
||||
pub fn to_ex_budget_v1(&self, fun: DefaultFunction, args: &[Value]) -> ExBudget {
|
||||
match fun {
|
||||
DefaultFunction::AddInteger => ExBudget {
|
||||
mem: self
|
||||
|
||||
148
crates/uplc/src/machine/discharge.rs
Normal file
148
crates/uplc/src/machine/discharge.rs
Normal file
@@ -0,0 +1,148 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::ast::{NamedDeBruijn, Term};
|
||||
|
||||
use super::value::{Env, Value};
|
||||
|
||||
#[derive(Clone)]
|
||||
enum PartialTerm {
|
||||
Delay,
|
||||
Lambda(Rc<NamedDeBruijn>),
|
||||
Apply,
|
||||
Force,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
enum DischargeStep {
|
||||
DischargeValue(Value),
|
||||
DischargeValueEnv(usize, Env, Rc<Term<NamedDeBruijn>>),
|
||||
PopArgStack(PartialTerm),
|
||||
}
|
||||
|
||||
pub(super) fn value_as_term(value: Value) -> Rc<Term<NamedDeBruijn>> {
|
||||
let mut stack = vec![DischargeStep::DischargeValue(value)];
|
||||
|
||||
let mut arg_stack = vec![];
|
||||
|
||||
while let Some(stack_frame) = stack.pop() {
|
||||
match stack_frame {
|
||||
DischargeStep::DischargeValue(value) => match value {
|
||||
Value::Con(x) => arg_stack.push(Term::Constant(x.clone()).into()),
|
||||
Value::Builtin { term, .. } => arg_stack.push(term.clone()),
|
||||
Value::Delay(body, env) => {
|
||||
stack.push(DischargeStep::DischargeValueEnv(
|
||||
0,
|
||||
env.clone(),
|
||||
Term::Delay(body.clone()).into(),
|
||||
));
|
||||
}
|
||||
Value::Lambda {
|
||||
parameter_name,
|
||||
body,
|
||||
env,
|
||||
} => {
|
||||
stack.push(DischargeStep::DischargeValueEnv(
|
||||
0,
|
||||
env.clone(),
|
||||
Term::Lambda {
|
||||
parameter_name: parameter_name.clone(),
|
||||
body: body.clone(),
|
||||
}
|
||||
.into(),
|
||||
));
|
||||
}
|
||||
},
|
||||
DischargeStep::DischargeValueEnv(lam_cnt, env, term) => match term.as_ref() {
|
||||
Term::Var(name) => {
|
||||
let index: usize = name.index.into();
|
||||
|
||||
if lam_cnt >= index {
|
||||
arg_stack.push(Rc::new(Term::Var(name.clone())));
|
||||
} else {
|
||||
let env = env.get::<usize>(env.len() - (index - lam_cnt)).cloned();
|
||||
|
||||
if let Some(v) = env {
|
||||
stack.push(DischargeStep::DischargeValue(v));
|
||||
} else {
|
||||
arg_stack.push(Rc::new(Term::Var(name.clone())));
|
||||
}
|
||||
}
|
||||
}
|
||||
Term::Lambda {
|
||||
parameter_name,
|
||||
body,
|
||||
} => {
|
||||
stack.push(DischargeStep::PopArgStack(PartialTerm::Lambda(
|
||||
parameter_name.to_owned(),
|
||||
)));
|
||||
stack.push(DischargeStep::DischargeValueEnv(
|
||||
lam_cnt + 1,
|
||||
env,
|
||||
body.to_owned(),
|
||||
));
|
||||
}
|
||||
Term::Apply { function, argument } => {
|
||||
stack.push(DischargeStep::PopArgStack(PartialTerm::Apply));
|
||||
stack.push(DischargeStep::DischargeValueEnv(
|
||||
lam_cnt,
|
||||
env.clone(),
|
||||
argument.to_owned(),
|
||||
));
|
||||
stack.push(DischargeStep::DischargeValueEnv(
|
||||
lam_cnt,
|
||||
env,
|
||||
function.to_owned(),
|
||||
));
|
||||
}
|
||||
Term::Delay(body) => {
|
||||
stack.push(DischargeStep::PopArgStack(PartialTerm::Delay));
|
||||
stack.push(DischargeStep::DischargeValueEnv(
|
||||
lam_cnt,
|
||||
env.clone(),
|
||||
body.to_owned(),
|
||||
));
|
||||
}
|
||||
|
||||
Term::Force(body) => {
|
||||
stack.push(DischargeStep::PopArgStack(PartialTerm::Force));
|
||||
stack.push(DischargeStep::DischargeValueEnv(
|
||||
lam_cnt,
|
||||
env.clone(),
|
||||
body.to_owned(),
|
||||
));
|
||||
}
|
||||
rest => {
|
||||
arg_stack.push(rest.to_owned().into());
|
||||
}
|
||||
},
|
||||
DischargeStep::PopArgStack(term) => match term {
|
||||
PartialTerm::Delay => {
|
||||
let body = arg_stack.pop().unwrap();
|
||||
arg_stack.push(Term::Delay(body).into())
|
||||
}
|
||||
PartialTerm::Lambda(parameter_name) => {
|
||||
let body = arg_stack.pop().unwrap();
|
||||
arg_stack.push(
|
||||
Term::Lambda {
|
||||
parameter_name,
|
||||
body,
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
}
|
||||
PartialTerm::Apply => {
|
||||
let argument = arg_stack.pop().unwrap();
|
||||
let function = arg_stack.pop().unwrap();
|
||||
|
||||
arg_stack.push(Term::Apply { function, argument }.into());
|
||||
}
|
||||
PartialTerm::Force => {
|
||||
let body = arg_stack.pop().unwrap();
|
||||
arg_stack.push(Term::Force(body).into())
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
arg_stack.pop().unwrap()
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
490
crates/uplc/src/machine/value.rs
Normal file
490
crates/uplc/src/machine/value.rs
Normal file
@@ -0,0 +1,490 @@
|
||||
use std::{collections::VecDeque, ops::Deref, rc::Rc};
|
||||
|
||||
use num_bigint::BigInt;
|
||||
use num_traits::Signed;
|
||||
use pallas_primitives::babbage::{self as pallas, PlutusData};
|
||||
|
||||
use crate::{
|
||||
ast::{Constant, NamedDeBruijn, Term, Type},
|
||||
builtins::DefaultFunction,
|
||||
};
|
||||
|
||||
use super::{runtime::BuiltinRuntime, Error};
|
||||
|
||||
pub(super) type Env = Rc<Vec<Value>>;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Value {
|
||||
Con(Rc<Constant>),
|
||||
Delay(Rc<Term<NamedDeBruijn>>, Env),
|
||||
Lambda {
|
||||
parameter_name: Rc<NamedDeBruijn>,
|
||||
body: Rc<Term<NamedDeBruijn>>,
|
||||
env: Env,
|
||||
},
|
||||
Builtin {
|
||||
fun: DefaultFunction,
|
||||
term: Rc<Term<NamedDeBruijn>>,
|
||||
runtime: BuiltinRuntime,
|
||||
},
|
||||
}
|
||||
|
||||
impl Value {
|
||||
pub fn integer(n: BigInt) -> Self {
|
||||
let constant = Constant::Integer(n);
|
||||
|
||||
Value::Con(constant.into())
|
||||
}
|
||||
|
||||
pub fn bool(n: bool) -> Self {
|
||||
let constant = Constant::Bool(n);
|
||||
|
||||
Value::Con(constant.into())
|
||||
}
|
||||
|
||||
pub fn byte_string(n: Vec<u8>) -> Self {
|
||||
let constant = Constant::ByteString(n);
|
||||
|
||||
Value::Con(constant.into())
|
||||
}
|
||||
|
||||
pub fn string(n: String) -> Self {
|
||||
let constant = Constant::String(n);
|
||||
|
||||
Value::Con(constant.into())
|
||||
}
|
||||
|
||||
pub fn list(typ: Type, n: Vec<Constant>) -> Self {
|
||||
let constant = Constant::ProtoList(typ, n);
|
||||
|
||||
Value::Con(constant.into())
|
||||
}
|
||||
|
||||
pub fn data(d: PlutusData) -> Self {
|
||||
let constant = Constant::Data(d);
|
||||
|
||||
Value::Con(constant.into())
|
||||
}
|
||||
|
||||
pub(super) fn unwrap_integer(&self) -> &BigInt {
|
||||
let Value::Con(inner) = self else {unreachable!()};
|
||||
let Constant::Integer(integer) = inner.as_ref() else {unreachable!()};
|
||||
|
||||
integer
|
||||
}
|
||||
|
||||
pub(super) fn unwrap_byte_string(&self) -> &Vec<u8> {
|
||||
let Value::Con(inner) = self else {unreachable!()};
|
||||
let Constant::ByteString(byte_string) = inner.as_ref() else {unreachable!()};
|
||||
|
||||
byte_string
|
||||
}
|
||||
|
||||
pub(super) fn unwrap_string(&self) -> &String {
|
||||
let Value::Con(inner) = self else {unreachable!()};
|
||||
let Constant::String(string) = inner.as_ref() else {unreachable!()};
|
||||
|
||||
string
|
||||
}
|
||||
|
||||
pub(super) fn unwrap_bool(&self) -> &bool {
|
||||
let Value::Con(inner) = self else {unreachable!()};
|
||||
let Constant::Bool(condition) = inner.as_ref() else {unreachable!()};
|
||||
|
||||
condition
|
||||
}
|
||||
|
||||
pub(super) fn unwrap_pair(&self) -> (&Type, &Type, &Rc<Constant>, &Rc<Constant>) {
|
||||
let Value::Con(inner) = self else {unreachable!()};
|
||||
let Constant::ProtoPair(t1, t2, first, second) = inner.as_ref() else {unreachable!()};
|
||||
|
||||
(t1, t2, first, second)
|
||||
}
|
||||
|
||||
pub(super) fn unwrap_list(&self) -> (&Type, &Vec<Constant>) {
|
||||
let Value::Con(inner) = self else {unreachable!()};
|
||||
let Constant::ProtoList(t, list) = inner.as_ref() else {unreachable!()};
|
||||
|
||||
(t, list)
|
||||
}
|
||||
|
||||
pub(super) fn unwrap_constant(&self) -> &Constant {
|
||||
let Value::Con(item) = self else {unreachable!()};
|
||||
|
||||
item.as_ref()
|
||||
}
|
||||
|
||||
pub(super) fn unwrap_data_list(&self) -> &Vec<Constant> {
|
||||
let Value::Con(inner) = self else {unreachable!()};
|
||||
let Constant::ProtoList(Type::Data, list) = inner.as_ref() else {unreachable!()};
|
||||
|
||||
list
|
||||
}
|
||||
|
||||
pub fn is_integer(&self) -> bool {
|
||||
matches!(self, Value::Con(i) if matches!(i.as_ref(), Constant::Integer(_)))
|
||||
}
|
||||
|
||||
pub fn is_bool(&self) -> bool {
|
||||
matches!(self, Value::Con(b) if matches!(b.as_ref(), Constant::Bool(_)))
|
||||
}
|
||||
|
||||
// TODO: Make this to_ex_mem not recursive.
|
||||
pub fn to_ex_mem(&self) -> i64 {
|
||||
match self {
|
||||
Value::Con(c) => match c.as_ref() {
|
||||
Constant::Integer(i) => {
|
||||
if *i == 0.into() {
|
||||
1
|
||||
} else {
|
||||
(integer_log2(i.abs()) / 64) + 1
|
||||
}
|
||||
}
|
||||
Constant::ByteString(b) => {
|
||||
if b.is_empty() {
|
||||
1
|
||||
} else {
|
||||
((b.len() as i64 - 1) / 8) + 1
|
||||
}
|
||||
}
|
||||
Constant::String(s) => s.chars().count() as i64,
|
||||
Constant::Unit => 1,
|
||||
Constant::Bool(_) => 1,
|
||||
Constant::ProtoList(_, items) => items.iter().fold(0, |acc, constant| {
|
||||
acc + Value::Con(constant.clone().into()).to_ex_mem()
|
||||
}),
|
||||
Constant::ProtoPair(_, _, l, r) => {
|
||||
Value::Con(l.clone()).to_ex_mem() + Value::Con(r.clone()).to_ex_mem()
|
||||
}
|
||||
Constant::Data(item) => self.data_to_ex_mem(item),
|
||||
},
|
||||
Value::Delay(_, _) => 1,
|
||||
Value::Lambda { .. } => 1,
|
||||
Value::Builtin { .. } => 1,
|
||||
}
|
||||
}
|
||||
|
||||
// I made data not recursive since data tends to be deeply nested
|
||||
// thus causing a significant hit on performance
|
||||
pub fn data_to_ex_mem(&self, data: &PlutusData) -> i64 {
|
||||
let mut stack: VecDeque<&PlutusData> = VecDeque::new();
|
||||
let mut total = 0;
|
||||
stack.push_front(data);
|
||||
while let Some(item) = stack.pop_front() {
|
||||
// each time we deconstruct a data we add 4 memory units
|
||||
total += 4;
|
||||
match item {
|
||||
PlutusData::Constr(c) => {
|
||||
// note currently tag is not factored into cost of memory
|
||||
// create new stack with of items from the list of data
|
||||
let mut new_stack: VecDeque<&PlutusData> =
|
||||
VecDeque::from_iter(c.fields.deref().iter());
|
||||
// Append old stack to the back of the new stack
|
||||
new_stack.append(&mut stack);
|
||||
stack = new_stack;
|
||||
}
|
||||
PlutusData::Map(m) => {
|
||||
let mut new_stack: VecDeque<&PlutusData>;
|
||||
// create new stack with of items from the list of pairs of data
|
||||
new_stack = m.iter().fold(VecDeque::new(), |mut acc, d| {
|
||||
acc.push_back(&d.0);
|
||||
acc.push_back(&d.1);
|
||||
acc
|
||||
});
|
||||
// Append old stack to the back of the new stack
|
||||
new_stack.append(&mut stack);
|
||||
stack = new_stack;
|
||||
}
|
||||
PlutusData::BigInt(i) => {
|
||||
let i = from_pallas_bigint(i);
|
||||
|
||||
total += Value::Con(Constant::Integer(i).into()).to_ex_mem();
|
||||
}
|
||||
PlutusData::BoundedBytes(b) => {
|
||||
let byte_string: Vec<u8> = b.deref().clone();
|
||||
total += Value::Con(Constant::ByteString(byte_string).into()).to_ex_mem();
|
||||
}
|
||||
PlutusData::Array(a) => {
|
||||
// create new stack with of items from the list of data
|
||||
let mut new_stack: VecDeque<&PlutusData> =
|
||||
VecDeque::from_iter(a.deref().iter());
|
||||
// Append old stack to the back of the new stack
|
||||
new_stack.append(&mut stack);
|
||||
stack = new_stack;
|
||||
}
|
||||
}
|
||||
}
|
||||
total
|
||||
}
|
||||
|
||||
pub fn expect_type(&self, r#type: Type) -> Result<(), Error> {
|
||||
let constant: Constant = self.clone().try_into()?;
|
||||
|
||||
let constant_type = Type::from(&constant);
|
||||
|
||||
if constant_type == r#type {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::TypeMismatch(r#type, constant_type))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expect_list(&self) -> Result<(), Error> {
|
||||
let constant: Constant = self.clone().try_into()?;
|
||||
|
||||
let constant_type = Type::from(&constant);
|
||||
|
||||
if matches!(constant_type, Type::List(_)) {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::ListTypeMismatch(constant_type))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expect_pair(&self) -> Result<(), Error> {
|
||||
let constant: Constant = self.clone().try_into()?;
|
||||
|
||||
let constant_type = Type::from(&constant);
|
||||
|
||||
if matches!(constant_type, Type::Pair(_, _)) {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::PairTypeMismatch(constant_type))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Value> for Type {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(value: Value) -> Result<Self, Self::Error> {
|
||||
let constant: Constant = value.try_into()?;
|
||||
|
||||
let constant_type = Type::from(&constant);
|
||||
|
||||
Ok(constant_type)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&Value> for Type {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(value: &Value) -> Result<Self, Self::Error> {
|
||||
let constant: Constant = value.try_into()?;
|
||||
|
||||
let constant_type = Type::from(&constant);
|
||||
|
||||
Ok(constant_type)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Value> for Constant {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(value: Value) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
Value::Con(constant) => Ok(constant.as_ref().clone()),
|
||||
rest => Err(Error::NotAConstant(rest)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&Value> for Constant {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(value: &Value) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
Value::Con(constant) => Ok(constant.as_ref().clone()),
|
||||
rest => Err(Error::NotAConstant(rest.clone())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn integer_log2(i: BigInt) -> i64 {
|
||||
let (_, bytes) = i.to_bytes_be();
|
||||
match bytes.first() {
|
||||
None => unreachable!("empty number?"),
|
||||
Some(u) => (8 - u.leading_zeros() - 1) as i64 + 8 * (bytes.len() - 1) as i64,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_pallas_bigint(n: &pallas::BigInt) -> BigInt {
|
||||
match n {
|
||||
pallas::BigInt::Int(i) => i128::from(*i).into(),
|
||||
pallas::BigInt::BigUInt(bytes) => BigInt::from_bytes_be(num_bigint::Sign::Plus, bytes),
|
||||
pallas::BigInt::BigNInt(bytes) => BigInt::from_bytes_be(num_bigint::Sign::Minus, bytes),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_pallas_bigint(n: &BigInt) -> pallas::BigInt {
|
||||
if n.bits() <= 64 {
|
||||
let regular_int: i64 = n.try_into().unwrap();
|
||||
let pallas_int: pallas_codec::utils::Int = regular_int.into();
|
||||
|
||||
pallas::BigInt::Int(pallas_int)
|
||||
} else if n.is_positive() {
|
||||
let (_, bytes) = n.to_bytes_be();
|
||||
pallas::BigInt::BigUInt(bytes.into())
|
||||
} else {
|
||||
let (_, bytes) = n.to_bytes_be();
|
||||
pallas::BigInt::BigNInt(bytes.into())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use num_bigint::BigInt;
|
||||
|
||||
use crate::{
|
||||
ast::Constant,
|
||||
machine::value::{integer_log2, Value},
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn to_ex_mem_bigint() {
|
||||
let value = Value::Con(Constant::Integer(1.into()).into());
|
||||
|
||||
assert_eq!(value.to_ex_mem(), 1);
|
||||
|
||||
let value = Value::Con(Constant::Integer(42.into()).into());
|
||||
|
||||
assert_eq!(value.to_ex_mem(), 1);
|
||||
|
||||
let value = Value::Con(
|
||||
Constant::Integer(BigInt::parse_bytes("18446744073709551615".as_bytes(), 10).unwrap())
|
||||
.into(),
|
||||
);
|
||||
|
||||
assert_eq!(value.to_ex_mem(), 1);
|
||||
|
||||
let value = Value::Con(
|
||||
Constant::Integer(
|
||||
BigInt::parse_bytes("999999999999999999999999999999".as_bytes(), 10).unwrap(),
|
||||
)
|
||||
.into(),
|
||||
);
|
||||
|
||||
assert_eq!(value.to_ex_mem(), 2);
|
||||
|
||||
let value = Value::Con(
|
||||
Constant::Integer(
|
||||
BigInt::parse_bytes("170141183460469231731687303715884105726".as_bytes(), 10)
|
||||
.unwrap(),
|
||||
)
|
||||
.into(),
|
||||
);
|
||||
|
||||
assert_eq!(value.to_ex_mem(), 2);
|
||||
|
||||
let value = Value::Con(
|
||||
Constant::Integer(
|
||||
BigInt::parse_bytes("170141183460469231731687303715884105727".as_bytes(), 10)
|
||||
.unwrap(),
|
||||
)
|
||||
.into(),
|
||||
);
|
||||
|
||||
assert_eq!(value.to_ex_mem(), 2);
|
||||
|
||||
let value = Value::Con(
|
||||
Constant::Integer(
|
||||
BigInt::parse_bytes("170141183460469231731687303715884105728".as_bytes(), 10)
|
||||
.unwrap(),
|
||||
)
|
||||
.into(),
|
||||
);
|
||||
|
||||
assert_eq!(value.to_ex_mem(), 2);
|
||||
|
||||
let value = Value::Con(
|
||||
Constant::Integer(
|
||||
BigInt::parse_bytes("170141183460469231731687303715884105729".as_bytes(), 10)
|
||||
.unwrap(),
|
||||
)
|
||||
.into(),
|
||||
);
|
||||
|
||||
assert_eq!(value.to_ex_mem(), 2);
|
||||
|
||||
let value = Value::Con(
|
||||
Constant::Integer(
|
||||
BigInt::parse_bytes("340282366920938463463374607431768211458".as_bytes(), 10)
|
||||
.unwrap(),
|
||||
)
|
||||
.into(),
|
||||
);
|
||||
|
||||
assert_eq!(value.to_ex_mem(), 3);
|
||||
|
||||
let value = Value::Con(
|
||||
Constant::Integer(
|
||||
BigInt::parse_bytes("999999999999999999999999999999999999999999".as_bytes(), 10)
|
||||
.unwrap(),
|
||||
)
|
||||
.into(),
|
||||
);
|
||||
|
||||
assert_eq!(value.to_ex_mem(), 3);
|
||||
|
||||
let value =
|
||||
Value::Con(Constant::Integer(BigInt::parse_bytes("999999999999999999999999999999999999999999999999999999999999999999999999999999999999".as_bytes(), 10).unwrap()).into());
|
||||
|
||||
assert_eq!(value.to_ex_mem(), 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn integer_log2_oracle() {
|
||||
// Values come from the Haskell implementation
|
||||
assert_eq!(integer_log2(1.into()), 0);
|
||||
assert_eq!(integer_log2(42.into()), 5);
|
||||
assert_eq!(
|
||||
integer_log2(BigInt::parse_bytes("18446744073709551615".as_bytes(), 10).unwrap()),
|
||||
63
|
||||
);
|
||||
assert_eq!(
|
||||
integer_log2(
|
||||
BigInt::parse_bytes("999999999999999999999999999999".as_bytes(), 10).unwrap()
|
||||
),
|
||||
99
|
||||
);
|
||||
assert_eq!(
|
||||
integer_log2(
|
||||
BigInt::parse_bytes("170141183460469231731687303715884105726".as_bytes(), 10)
|
||||
.unwrap()
|
||||
),
|
||||
126
|
||||
);
|
||||
assert_eq!(
|
||||
integer_log2(
|
||||
BigInt::parse_bytes("170141183460469231731687303715884105727".as_bytes(), 10)
|
||||
.unwrap()
|
||||
),
|
||||
126
|
||||
);
|
||||
assert_eq!(
|
||||
integer_log2(
|
||||
BigInt::parse_bytes("170141183460469231731687303715884105728".as_bytes(), 10)
|
||||
.unwrap()
|
||||
),
|
||||
127
|
||||
);
|
||||
assert_eq!(
|
||||
integer_log2(
|
||||
BigInt::parse_bytes("340282366920938463463374607431768211458".as_bytes(), 10)
|
||||
.unwrap()
|
||||
),
|
||||
128
|
||||
);
|
||||
assert_eq!(
|
||||
integer_log2(
|
||||
BigInt::parse_bytes("999999999999999999999999999999999999999999".as_bytes(), 10)
|
||||
.unwrap()
|
||||
),
|
||||
139
|
||||
);
|
||||
assert_eq!(
|
||||
integer_log2(BigInt::parse_bytes("999999999999999999999999999999999999999999999999999999999999999999999999999999999999".as_bytes(), 10).unwrap()),
|
||||
279
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user