Skip to content

Commit

Permalink
avoid u128 union members in portable code
Browse files Browse the repository at this point in the history
For wasm32 compatibility. Implements #33.
  • Loading branch information
kazcw committed Aug 19, 2020
1 parent 790d7d4 commit 97e2879
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion utils-simd/ppv-lite86/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ppv-lite86"
version = "0.2.8"
version = "0.2.9"
authors = ["The CryptoCorrosion Contributors"]
edition = "2018"
license = "MIT/Apache-2.0"
Expand Down
27 changes: 17 additions & 10 deletions utils-simd/ppv-lite86/src/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::types::*;
pub union vec128_storage {
d: [u32; 4],
q: [u64; 2],
o: [u128; 1],
}
impl From<[u32; 4]> for vec128_storage {
#[inline]
Expand Down Expand Up @@ -38,14 +37,14 @@ impl From<vec128_storage> for [u64; 2] {
impl Default for vec128_storage {
#[inline]
fn default() -> Self {
Self { o: [0] }
Self { q: [0, 0] }
}
}
impl Eq for vec128_storage {}
impl PartialEq<vec128_storage> for vec128_storage {
#[inline]
fn eq(&self, rhs: &Self) -> bool {
unsafe { self.o == rhs.o }
unsafe { self.q == rhs.q }
}
}
#[derive(Clone, Copy, PartialEq, Eq, Default)]
Expand Down Expand Up @@ -152,14 +151,22 @@ where
unsafe { T::unpack(q) }
}

fn o_of_q(q: [u64; 2]) -> u128 {
u128::from(q[0]) | (u128::from(q[1]) << 64)
}

fn q_of_o(o: u128) -> [u64; 2] {
[o as u64, (o >> 64) as u64]
}

fn omap<T, F>(a: T, f: F) -> T
where
T: Store<vec128_storage> + Into<vec128_storage>,
F: Fn(u128) -> u128,
{
let a: vec128_storage = a.into();
let ao = unsafe { a.o };
let o = vec128_storage { o: [f(ao[0])] };
let ao = o_of_q(unsafe { a.q });
let o = vec128_storage { q: q_of_o(f(ao)) };
unsafe { T::unpack(o) }
}

Expand All @@ -170,10 +177,10 @@ where
{
let a: vec128_storage = a.into();
let b: vec128_storage = b.into();
let ao = unsafe { a.o };
let bo = unsafe { b.o };
let ao = o_of_q(unsafe { a.q });
let bo = o_of_q(unsafe { b.q });
let o = vec128_storage {
o: [f(ao[0], bo[0])],
q: q_of_o(f(ao, bo)),
};
unsafe { T::unpack(o) }
}
Expand Down Expand Up @@ -457,7 +464,7 @@ impl From<u64x2_generic> for vec128_storage {
impl From<u128x1_generic> for vec128_storage {
#[inline(always)]
fn from(o: u128x1_generic) -> Self {
Self { o: o.0 }
Self { q: q_of_o(o.0[0]) }
}
}

Expand All @@ -476,7 +483,7 @@ impl Store<vec128_storage> for u64x2_generic {
impl Store<vec128_storage> for u128x1_generic {
#[inline(always)]
unsafe fn unpack(s: vec128_storage) -> Self {
Self(s.o)
Self([o_of_q(s.q); 1])
}
}

Expand Down

0 comments on commit 97e2879

Please sign in to comment.