Open up the Barrett number construction / debugging a bit.

This commit is contained in:
2018-11-06 21:54:32 -08:00
parent de5ff87f9e
commit 1a2f3aaa7a

View File

@@ -1,5 +1,6 @@
macro_rules! barrett_impl { macro_rules! barrett_impl {
($bar: ident, $name: ident, $name64: ident, $dbl: ident, $dbl64: ident) => { ($bar: ident, $name: ident, $name64: ident, $dbl: ident, $dbl64: ident) => {
#[derive(PartialEq)]
pub struct $bar { pub struct $bar {
pub(crate) k: usize, pub(crate) k: usize,
pub(crate) m: $name64, pub(crate) m: $name64,
@@ -31,8 +32,17 @@ macro_rules! barrett_impl {
$bar { k: k, m: resm, mu: mu } $bar { k: k, m: resm, mu: mu }
} }
/// Generate a new Barrett number from its component parts. You
/// probably don't want to use it; it's mostly here for debugging
/// purposes, so that tests won't have to compute this all the
/// time.
pub fn from_components(k: usize, m: $name64, mu: $name64) -> $bar {
$bar { k: k, m: m, mu: mu }
}
// Reduce the input by this value; in other words, perform a mod // Reduce the input by this value; in other words, perform a mod
// operation. // operation.
#[inline(always)]
pub fn reduce(&self, x: &$dbl) -> $name { pub fn reduce(&self, x: &$dbl) -> $name {
// 1. q1←⌊x/bk1⌋, q2←q1 · μ, q3←⌊q2/bk+1⌋. // 1. q1←⌊x/bk1⌋, q2←q1 · μ, q3←⌊q2/bk+1⌋.
let q1: $name64 = $name64::from(x >> ((self.k - 1) * 64)); let q1: $name64 = $name64::from(x >> ((self.k - 1) * 64));
@@ -73,6 +83,16 @@ macro_rules! barrett_impl {
$bar{ k: k, m: m, mu: mu } $bar{ k: k, m: m, mu: mu }
} }
} }
impl fmt::Debug for $bar {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct(stringify!($name))
.field("k", &self.k)
.field("m", &self.m)
.field("mu", &self.mu)
.finish()
}
}
}; };
} }