use super::{
const_is_one, const_is_one_ct, const_is_zero, const_is_zero_ct, FixedUInt, MachineWord,
};
use crate::const_numtraits::{ConstBounded, ConstOne, ConstZero};
use crate::machineword::ConstMachineWord;
use crate::personality::{Personality, PersonalityTag};
c0nst::c0nst! {
impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> c0nst ConstZero for FixedUInt<T, N, P> {
fn zero() -> Self {
FixedUInt::from_array([T::zero(); N])
}
fn is_zero(&self) -> bool {
match P::TAG {
PersonalityTag::Nct => const_is_zero(&self.array),
PersonalityTag::Ct => const_is_zero_ct(&self.array),
}
}
fn set_zero(&mut self) {
let mut i = 0;
while i < N {
self.array[i].set_zero();
i += 1;
}
}
}
impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> c0nst ConstOne for FixedUInt<T, N, P> {
fn one() -> Self {
let mut ret = <Self as ConstZero>::zero();
if N > 0 {
ret.array[0] = T::one();
}
ret
}
fn is_one(&self) -> bool {
match P::TAG {
PersonalityTag::Nct => const_is_one(&self.array),
PersonalityTag::Ct => const_is_one_ct(&self.array),
}
}
fn set_one(&mut self) {
<Self as ConstZero>::set_zero(self);
if N > 0 {
self.array[0].set_one();
}
}
}
impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> c0nst ConstBounded for FixedUInt<T, N, P> {
fn min_value() -> Self {
<Self as ConstZero>::zero()
}
fn max_value() -> Self {
FixedUInt::from_array([T::max_value(); N])
}
}
}
impl<T: MachineWord, const N: usize, P: Personality> num_traits::Zero for FixedUInt<T, N, P> {
fn zero() -> Self {
<Self as ConstZero>::zero()
}
fn is_zero(&self) -> bool {
<Self as ConstZero>::is_zero(self)
}
}
impl<T: MachineWord, const N: usize, P: Personality> num_traits::One for FixedUInt<T, N, P> {
fn one() -> Self {
<Self as ConstOne>::one()
}
}
impl<T: MachineWord, const N: usize, P: Personality> num_traits::Bounded for FixedUInt<T, N, P> {
fn min_value() -> Self {
<Self as ConstBounded>::min_value()
}
fn max_value() -> Self {
<Self as ConstBounded>::max_value()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::personality::Nct;
c0nst::c0nst! {
c0nst fn const_zero<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality>() -> FixedUInt<T, N, P> {
<FixedUInt<T, N, P> as ConstZero>::zero()
}
c0nst fn const_one<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality>() -> FixedUInt<T, N, P> {
<FixedUInt<T, N, P> as ConstOne>::one()
}
c0nst fn const_is_zero<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality>(v: &FixedUInt<T, N, P>) -> bool {
<FixedUInt<T, N, P> as ConstZero>::is_zero(v)
}
c0nst fn const_is_one<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality>(v: &FixedUInt<T, N, P>) -> bool {
<FixedUInt<T, N, P> as ConstOne>::is_one(v)
}
c0nst fn const_min_value<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality>() -> FixedUInt<T, N, P> {
<FixedUInt<T, N, P> as ConstBounded>::min_value()
}
c0nst fn const_max_value<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality>() -> FixedUInt<T, N, P> {
<FixedUInt<T, N, P> as ConstBounded>::max_value()
}
c0nst fn const_set_zero<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality>(v: &mut FixedUInt<T, N, P>) {
<FixedUInt<T, N, P> as ConstZero>::set_zero(v)
}
c0nst fn const_set_one<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality>(v: &mut FixedUInt<T, N, P>) {
<FixedUInt<T, N, P> as ConstOne>::set_one(v)
}
}
#[test]
fn test_const_identity_traits() {
type TestInt = FixedUInt<u8, 2>;
let zero = const_zero::<u8, 2, Nct>();
assert!(const_is_zero(&zero));
assert!(!const_is_one(&zero));
let one = const_one::<u8, 2, Nct>();
assert!(!const_is_zero(&one));
assert!(const_is_one(&one));
let min = const_min_value::<u8, 2, Nct>();
let max = const_max_value::<u8, 2, Nct>();
assert!(const_is_zero(&min));
assert_eq!(max.array, [255, 255]);
let mut val = TestInt::from(42u8);
const_set_zero(&mut val);
assert!(const_is_zero(&val));
const_set_one(&mut val);
assert!(const_is_one(&val));
#[cfg(feature = "nightly")]
{
const ZERO: TestInt = const_zero::<u8, 2, Nct>();
const ONE: TestInt = const_one::<u8, 2, Nct>();
const IS_ZERO_TRUE: bool = const_is_zero(&ZERO);
const IS_ZERO_FALSE: bool = const_is_zero(&ONE);
const IS_ONE_TRUE: bool = const_is_one(&ONE);
const IS_ONE_FALSE: bool = const_is_one(&ZERO);
const MIN: TestInt = const_min_value::<u8, 2, Nct>();
const MAX: TestInt = const_max_value::<u8, 2, Nct>();
assert_eq!(ZERO.array, [0, 0]);
assert_eq!(ONE.array, [1, 0]);
assert!(IS_ZERO_TRUE);
assert!(!IS_ZERO_FALSE);
assert!(IS_ONE_TRUE);
assert!(!IS_ONE_FALSE);
assert_eq!(MIN.array, [0, 0]);
assert_eq!(MAX.array, [255, 255]);
const SET_ZERO_RES: TestInt = {
let mut v = FixedUInt::from_array([42, 0]);
const_set_zero(&mut v);
v
};
const SET_ONE_RES: TestInt = {
let mut v = FixedUInt::from_array([0, 0]);
const_set_one(&mut v);
v
};
assert_eq!(SET_ZERO_RES.array, [0, 0]);
assert_eq!(SET_ONE_RES.array, [1, 0]);
}
}
}