Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/uint/from.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! `From`-like conversions for [`Uint`].

use crate::{ConcatMixed, Limb, Uint, WideWord, Word, U128, U64};
use crate::{ConcatMixed, Limb, SplitMixed, Uint, WideWord, Word, U128, U64};

impl<const LIMBS: usize> Uint<LIMBS> {
/// Create a [`Uint`] from a `u8` (const-friendly)
Expand Down Expand Up @@ -214,9 +214,12 @@ where
}
}

impl<const L: usize, const H: usize, const LIMBS: usize> From<Uint<LIMBS>> for (Uint<L>, Uint<H>) {
impl<const L: usize, const H: usize, const LIMBS: usize> From<Uint<LIMBS>> for (Uint<L>, Uint<H>)
where
Uint<LIMBS>: SplitMixed<Uint<L>, Uint<H>>,
{
fn from(num: Uint<LIMBS>) -> (Uint<L>, Uint<H>) {
crate::uint::split::split_mixed(&num)
num.split_mixed()
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/uint/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ macro_rules! impl_uint_concat_split_mixed {
impl $crate::traits::SplitMixed<Uint<{ U64::LIMBS * $size }>, Uint<{ <$name>::LIMBS - U64::LIMBS * $size }>> for $name
{
fn split_mixed(&self) -> (Uint<{ U64::LIMBS * $size }>, Uint<{ <$name>::LIMBS - U64::LIMBS * $size }>) {
$crate::uint::split::split_mixed(self)
self.split_mixed()
}
}
};
Expand Down Expand Up @@ -128,7 +128,7 @@ macro_rules! impl_uint_concat_split_even {
impl $crate::traits::SplitMixed<Uint<{ <$name>::LIMBS / 2 }>, Uint<{ <$name>::LIMBS / 2 }>> for $name
{
fn split_mixed(&self) -> (Uint<{ <$name>::LIMBS / 2 }>, Uint<{ <$name>::LIMBS / 2 }>) {
$crate::uint::split::split_mixed(self)
self.split_mixed()
}
}

Expand All @@ -140,7 +140,7 @@ macro_rules! impl_uint_concat_split_even {
impl $name {
/// Split this number in half, returning its low and high components respectively.
pub const fn split(&self) -> (Uint<{ <$name>::LIMBS / 2 }>, Uint<{ <$name>::LIMBS / 2 }>) {
$crate::uint::split::split_mixed(self)
self.split_mixed()
}
}
};
Expand Down
41 changes: 22 additions & 19 deletions src/uint/split.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
use crate::{Limb, Uint};
use crate::{Limb, SplitMixed, Uint};

/// Split this number in half, returning its low and high components respectively.
#[inline]
pub(crate) const fn split_mixed<const L: usize, const H: usize, const O: usize>(
n: &Uint<O>,
) -> (Uint<L>, Uint<H>) {
let top = L + H;
let top = if top < O { top } else { O };
let mut lo = [Limb::ZERO; L];
let mut hi = [Limb::ZERO; H];
let mut i = 0;
impl<const I: usize> Uint<I> {
/// Split this number into low and high components respectively.
#[inline]
pub const fn split_mixed<const L: usize, const H: usize>(&self) -> (Uint<L>, Uint<H>)
where
Self: SplitMixed<Uint<L>, Uint<H>>,
{
let top = L + H;
let top = if top < I { top } else { I };
let mut lo = [Limb::ZERO; L];
let mut hi = [Limb::ZERO; H];
let mut i = 0;

while i < top {
if i < L {
lo[i] = n.limbs[i];
} else {
hi[i - L] = n.limbs[i];
while i < top {
if i < L {
lo[i] = self.limbs[i];
} else {
hi[i - L] = self.limbs[i];
}
i += 1;
}
i += 1;
}

(Uint { limbs: lo }, Uint { limbs: hi })
(Uint { limbs: lo }, Uint { limbs: hi })
}
}

#[cfg(test)]
Expand Down