Skip to content

Commit

Permalink
wip: int edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
prestwich committed Apr 7, 2023
1 parent a90158f commit e4049c9
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions primitives/src/signed/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,21 @@ pub const fn const_eq<const BITS: usize, const LIMBS: usize>(

/// Compute the max value at compile time
const fn max<const BITS: usize, const LIMBS: usize>() -> Signed<BITS, LIMBS> {
if LIMBS == 0 {
return zero();
}

let mut limbs = [u64::MAX; LIMBS];
limbs[LIMBS - 1] &= Signed::<BITS, LIMBS>::MASK; // unset all high bits
limbs[LIMBS - 1] &= !Signed::<BITS, LIMBS>::SIGN_BIT; // unset the sign bit
Signed(Uint::from_limbs(limbs))
}

const fn min<const BITS: usize, const LIMBS: usize>() -> Signed<BITS, LIMBS> {
if LIMBS == 0 {
return zero();
}

let mut limbs = [0; LIMBS];
limbs[LIMBS - 1] = Signed::<BITS, LIMBS>::SIGN_BIT;
Signed(Uint::from_limbs(limbs))
Expand All @@ -67,6 +75,10 @@ const fn zero<const BITS: usize, const LIMBS: usize>() -> Signed<BITS, LIMBS> {
}

const fn one<const BITS: usize, const LIMBS: usize>() -> Signed<BITS, LIMBS> {
if LIMBS == 0 {
return zero();
}

let mut limbs = [0; LIMBS];
limbs[0] = 1;
Signed(Uint::from_limbs(limbs))
Expand Down Expand Up @@ -1744,6 +1756,7 @@ mod tests {
ParseSignedError,
};

// type U2 = Uint<2, 1>;
type I96 = Signed<96, 2>;
type U96 = Uint<96, 2>;

Expand Down Expand Up @@ -1819,6 +1832,9 @@ mod tests {
};
}

// run_test!(I0, U0);
// run_test!(I1, U1);
// run_test!(I2, U2);
run_test!(I96, U96);
run_test!(I128, U128);
run_test!(I160, U160);
Expand Down Expand Up @@ -1863,6 +1879,10 @@ mod tests {
assert_eq!(err, ParseSignedError::IntegerOverflow);
};
}

// run_test!(I0, U0);
// run_test!(I1, U1);
// run_test!(I2, U2);
run_test!(I96, U96);
run_test!(I128, U128);
run_test!(I160, U160);
Expand Down Expand Up @@ -1910,6 +1930,10 @@ mod tests {
assert!(matches!(err, ParseSignedError::IntegerOverflow));
};
}

// run_test!(I0, U0);
// run_test!(I1, U1);
// run_test!(I2, U2);
run_test!(I96, U96);
run_test!(I128, U128);
run_test!(I160, U160);
Expand Down Expand Up @@ -1953,6 +1977,10 @@ mod tests {
);
};
}

// run_test!(I0, U0);
// run_test!(I1, U1);
// run_test!(I2, U2);
run_test!(I96, U96);
run_test!(I128, U128);
run_test!(I160, U160);
Expand Down Expand Up @@ -1990,6 +2018,10 @@ mod tests {
assert!(<$i_struct>::zero().is_zero());
};
}

// run_test!(I0, U0);
// run_test!(I1, U1);
// run_test!(I2, U2);
run_test!(I96, U96);
run_test!(I128, U128);
run_test!(I160, U160);
Expand All @@ -2016,6 +2048,10 @@ mod tests {
assert_eq!(<$i_struct>::MIN.checked_abs(), None);
};
}

// run_test!(I0, U0);
// run_test!(I1, U1);
// run_test!(I2, U2);
run_test!(I96, U96);
run_test!(I128, U128);
run_test!(I160, U160);
Expand All @@ -2040,6 +2076,10 @@ mod tests {
assert_eq!(<$i_struct>::MIN.checked_neg(), None);
};
}

// run_test!(I0, U0);
// run_test!(I1, U1);
// run_test!(I2, U2);
run_test!(I96, U96);
run_test!(I128, U128);
run_test!(I160, U160);
Expand All @@ -2063,6 +2103,10 @@ mod tests {
assert_eq!(<$i_struct>::zero().bits(), 0);
};
}

// run_test!(I0, U0);
// run_test!(I1, U1);
// run_test!(I2, U2);
run_test!(I96, U96);
run_test!(I128, U128);
run_test!(I160, U160);
Expand All @@ -2084,6 +2128,10 @@ mod tests {
);
};
}

// run_test!(I0, U0);
// run_test!(I1, U1);
// run_test!(I2, U2);
run_test!(I96, U96);
run_test!(I128, U128);
run_test!(I160, U160);
Expand Down Expand Up @@ -2161,6 +2209,10 @@ mod tests {
);
};
}

// run_test!(I0, U0);
// run_test!(I1, U1);
// run_test!(I2, U2);
run_test!(I96, U96);
run_test!(I128, U128);
run_test!(I160, U160);
Expand Down Expand Up @@ -2215,6 +2267,10 @@ mod tests {
assert_eq!(value.asl(1024), expected_result, "0 << anything was not 0");
};
}

// run_test!(I0, U0);
// run_test!(I1, U1);
// run_test!(I2, U2);
run_test!(I96, U96);
run_test!(I128, U128);
run_test!(I160, U160);
Expand Down Expand Up @@ -2268,6 +2324,10 @@ mod tests {
);
};
}

// run_test!(I0, U0);
// run_test!(I1, U1);
// run_test!(I2, U2);
run_test!(I96, U96);
run_test!(I128, U128);
run_test!(I160, U160);
Expand Down Expand Up @@ -2324,6 +2384,9 @@ mod tests {
};
}

// run_test!(I0, U0);
// run_test!(I1, U1);
// run_test!(I2, U2);
run_test!(I96, U96);
run_test!(I128, U128);
run_test!(I160, U160);
Expand Down Expand Up @@ -2389,6 +2452,9 @@ mod tests {
};
}

// run_test!(I0, U0);
// run_test!(I1, U1);
// run_test!(I2, U2);
run_test!(I96, U96);
run_test!(I128, U128);
run_test!(I160, U160);
Expand Down Expand Up @@ -2437,6 +2503,10 @@ mod tests {
);
};
}

// run_test!(I0, U0);
// run_test!(I1, U1);
// run_test!(I2, U2);
run_test!(I96, U96);
run_test!(I128, U128);
run_test!(I160, U160);
Expand All @@ -2453,6 +2523,9 @@ mod tests {
};
}

// run_test!(I0, U0);
// run_test!(I1, U1);
// run_test!(I2, U2);
run_test!(I96, U96);
run_test!(I128, U128);
run_test!(I160, U160);
Expand Down Expand Up @@ -2494,6 +2567,9 @@ mod tests {
};
}

// run_test!(I0, U0);
// run_test!(I1, U1);
// run_test!(I2, U2);
run_test!(I96, U96);
run_test!(I128, U128);
run_test!(I160, U160);
Expand Down Expand Up @@ -2548,6 +2624,9 @@ mod tests {
};
}

// run_test!(I0, U0);
// run_test!(I1, U1);
// run_test!(I2, U2);
run_test!(I96, U96);
run_test!(I128, U128);
run_test!(I160, U160);
Expand All @@ -2567,6 +2646,10 @@ mod tests {
);
};
}

// run_test!(I0, U0);
// run_test!(I1, U1);
// run_test!(I2, U2);
run_test!(I96, U96);
run_test!(I128, U128);
run_test!(I160, U160);
Expand All @@ -2582,6 +2665,10 @@ mod tests {
let _ = <$i_struct>::MIN.div_euclid(<$i_struct>::minus_one());
};
}

// run_test!(I0, U0);
// run_test!(I1, U1);
// run_test!(I2, U2);
run_test!(I96, U96);
run_test!(I128, U128);
run_test!(I160, U160);
Expand All @@ -2597,6 +2684,10 @@ mod tests {
let _ = <$i_struct>::one() % <$i_struct>::zero();
};
}

// run_test!(I0, U0);
// run_test!(I1, U1);
// run_test!(I2, U2);
run_test!(I96, U96);
run_test!(I128, U128);
run_test!(I160, U160);
Expand Down Expand Up @@ -2640,6 +2731,10 @@ mod tests {
);
};
}

// run_test!(I0, U0);
// run_test!(I1, U1);
// run_test!(I2, U2);
run_test!(I96, U96);
run_test!(I128, U128);
run_test!(I160, U160);
Expand Down Expand Up @@ -2673,6 +2768,10 @@ mod tests {
assert_eq!(<$i_struct>::exp10(18).to_string(), "1000000000000000000");
};
}

// run_test!(I0, U0);
// run_test!(I1, U1);
// run_test!(I2, U2);
run_test!(I96, U96);
run_test!(I128, U128);
run_test!(I160, U160);
Expand Down Expand Up @@ -2700,6 +2799,10 @@ mod tests {
);
};
}

// run_test!(I0, U0);
// run_test!(I1, U1);
// run_test!(I2, U2);
run_test!(I96, U96);
run_test!(I128, U128);
run_test!(I160, U160);
Expand Down Expand Up @@ -2751,6 +2854,10 @@ mod tests {
assert_twos_complement!($i_struct, $u_struct, isize, usize);
};
}

// run_test!(I0, U0);
// run_test!(I1, U1);
// run_test!(I2, U2);
run_test!(I96, U96);
run_test!(I128, U128);
run_test!(I160, U160);
Expand Down

0 comments on commit e4049c9

Please sign in to comment.