Skip to content

Commit

Permalink
opt: for little-endian targets, use as_le_bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
prestwich committed Dec 25, 2024
1 parent 28434b8 commit 60147b0
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,18 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
"Buffer is too small to hold the bytes of the Uint"
);

let chunks = buf[..Self::BYTES].chunks_mut(8);
#[cfg(target_endian = "little")]
buf[..Self::BYTES].copy_from_slice(self.as_le_slice());

self.limbs.iter().zip(chunks).for_each(|(&limb, chunk)| {
let le = limb.to_le_bytes();
chunk.copy_from_slice(&le[..chunk.len()]);
});
#[cfg(not(target_endian = "little"))]
{
let chunks = buf[..Self::BYTES].chunks_mut(8);

self.limbs.iter().zip(chunks).for_each(|(&limb, chunk)| {
let le = limb.to_le_bytes();
chunk.copy_from_slice(&le[..chunk.len()]);
});
}

Self::BYTES
}
Expand Down

0 comments on commit 60147b0

Please sign in to comment.