(work-in-progress) Partially remove unsafe_target_feature

This commit is contained in:
Jan Bujak 2023-05-17 05:06:26 +00:00
parent a7df9c7918
commit c67e430cfd
No known key found for this signature in database
GPG key ID: 3B438F83D43341D4
3 changed files with 391 additions and 317 deletions

View file

@ -48,8 +48,6 @@ use crate::backend::vector::avx2::constants::{
P_TIMES_16_HI, P_TIMES_16_LO, P_TIMES_2_HI, P_TIMES_2_LO,
};
use unsafe_target_feature::unsafe_target_feature;
/// Unpack 32-bit lanes into 64-bit lanes:
/// ```ascii,no_run
/// (a0, b0, a1, b1, c0, d0, c1, d1)
@ -59,9 +57,9 @@ use unsafe_target_feature::unsafe_target_feature;
/// (a0, 0, b0, 0, c0, 0, d0, 0)
/// (a1, 0, b1, 0, c1, 0, d1, 0)
/// ```
#[unsafe_target_feature("avx2")]
#[inline(always)]
fn unpack_pair(src: u32x8) -> (u32x8, u32x8) {
#[target_feature(enable = "avx2")]
#[inline]
unsafe fn unpack_pair(src: u32x8) -> (u32x8, u32x8) {
let a: u32x8;
let b: u32x8;
let zero = u32x8::splat(0);
@ -83,9 +81,9 @@ fn unpack_pair(src: u32x8) -> (u32x8, u32x8) {
/// ```ascii,no_run
/// (a0, b0, a1, b1, c0, d0, c1, d1)
/// ```
#[unsafe_target_feature("avx2")]
#[inline(always)]
fn repack_pair(x: u32x8, y: u32x8) -> u32x8 {
#[target_feature(enable = "avx2")]
#[inline]
unsafe fn repack_pair(x: u32x8, y: u32x8) -> u32x8 {
unsafe {
use core::arch::x86_64::_mm256_blend_epi32;
use core::arch::x86_64::_mm256_shuffle_epi32;
@ -155,12 +153,18 @@ pub struct FieldElement2625x4(pub(crate) [u32x8; 5]);
use subtle::Choice;
use subtle::ConditionallySelectable;
#[unsafe_target_feature("avx2")]
impl ConditionallySelectable for FieldElement2625x4 {
#[inline(always)]
fn conditional_select(
a: &FieldElement2625x4,
b: &FieldElement2625x4,
choice: Choice,
) -> FieldElement2625x4 {
#[target_feature(enable = "avx2")]
unsafe fn inner(
a: &FieldElement2625x4,
b: &FieldElement2625x4,
choice: Choice,
) -> FieldElement2625x4 {
let mask = (-(choice.unwrap_u8() as i32)) as u32;
let mask_vec = u32x8::splat(mask);
@ -173,25 +177,38 @@ impl ConditionallySelectable for FieldElement2625x4 {
])
}
unsafe { inner(a, b, choice) }
}
#[inline(always)]
fn conditional_assign(&mut self, other: &FieldElement2625x4, choice: Choice) {
#[target_feature(enable = "avx2")]
unsafe fn inner(
itself: &mut FieldElement2625x4,
other: &FieldElement2625x4,
choice: Choice,
) {
let mask = (-(choice.unwrap_u8() as i32)) as u32;
let mask_vec = u32x8::splat(mask);
self.0[0] ^= mask_vec & (self.0[0] ^ other.0[0]);
self.0[1] ^= mask_vec & (self.0[1] ^ other.0[1]);
self.0[2] ^= mask_vec & (self.0[2] ^ other.0[2]);
self.0[3] ^= mask_vec & (self.0[3] ^ other.0[3]);
self.0[4] ^= mask_vec & (self.0[4] ^ other.0[4]);
itself.0[0] ^= mask_vec & (itself.0[0] ^ other.0[0]);
itself.0[1] ^= mask_vec & (itself.0[1] ^ other.0[1]);
itself.0[2] ^= mask_vec & (itself.0[2] ^ other.0[2]);
itself.0[3] ^= mask_vec & (itself.0[3] ^ other.0[3]);
itself.0[4] ^= mask_vec & (itself.0[4] ^ other.0[4]);
}
unsafe { inner(self, other, choice) }
}
}
#[unsafe_target_feature("avx2")]
impl FieldElement2625x4 {
pub const ZERO: FieldElement2625x4 = FieldElement2625x4([u32x8::splat_const::<0>(); 5]);
/// Split this vector into an array of four (serial) field
/// elements.
#[rustfmt::skip] // keep alignment of extracted lanes
pub fn split(&self) -> [FieldElement51; 4] {
#[target_feature(enable = "avx2")]
pub unsafe fn split(&self) -> [FieldElement51; 4] {
let mut out = [FieldElement51::ZERO; 4];
for i in 0..5 {
let a_2i = self.0[i].extract::<0>() as u64; //
@ -218,7 +235,8 @@ impl FieldElement2625x4 {
/// that when this function is inlined, LLVM is able to lower the
/// shuffle using an immediate.
#[inline]
pub fn shuffle(&self, control: Shuffle) -> FieldElement2625x4 {
#[target_feature(enable = "avx2")]
pub unsafe fn shuffle(&self, control: Shuffle) -> FieldElement2625x4 {
#[inline(always)]
fn shuffle_lanes(x: u32x8, control: Shuffle) -> u32x8 {
unsafe {
@ -258,7 +276,8 @@ impl FieldElement2625x4 {
/// that this function can be inlined and LLVM can lower it to a
/// blend instruction using an immediate.
#[inline]
pub fn blend(&self, other: FieldElement2625x4, control: Lanes) -> FieldElement2625x4 {
#[target_feature(enable = "avx2")]
pub unsafe fn blend(&self, other: FieldElement2625x4, control: Lanes) -> FieldElement2625x4 {
#[inline(always)]
fn blend_lanes(x: u32x8, y: u32x8, control: Lanes) -> u32x8 {
unsafe {
@ -322,7 +341,8 @@ impl FieldElement2625x4 {
}
/// Convenience wrapper around `new(x,x,x,x)`.
pub fn splat(x: &FieldElement51) -> FieldElement2625x4 {
#[target_feature(enable = "avx2")]
pub unsafe fn splat(x: &FieldElement51) -> FieldElement2625x4 {
FieldElement2625x4::new(x, x, x, x)
}
@ -332,7 +352,8 @@ impl FieldElement2625x4 {
///
/// The resulting `FieldElement2625x4` is bounded with \\( b < 0.0002 \\).
#[rustfmt::skip] // keep alignment of computed lanes
pub fn new(
#[target_feature(enable = "avx2")]
pub unsafe fn new(
x0: &FieldElement51,
x1: &FieldElement51,
x2: &FieldElement51,
@ -371,7 +392,8 @@ impl FieldElement2625x4 {
///
/// The coefficients of the result are bounded with \\( b < 1 \\).
#[inline]
pub fn negate_lazy(&self) -> FieldElement2625x4 {
#[target_feature(enable = "avx2")]
pub unsafe fn negate_lazy(&self) -> FieldElement2625x4 {
// The limbs of self are bounded with b < 0.999, while the
// smallest limb of 2*p is 67108845 > 2^{26+0.9999}, so
// underflows are not possible.
@ -394,7 +416,8 @@ impl FieldElement2625x4 {
///
/// The coefficients of the result are bounded with \\( b < 1.6 \\).
#[inline]
pub fn diff_sum(&self) -> FieldElement2625x4 {
#[target_feature(enable = "avx2")]
pub unsafe fn diff_sum(&self) -> FieldElement2625x4 {
// tmp1 = (B, A, D, C)
let tmp1 = self.shuffle(Shuffle::BADC);
// tmp2 = (-A, B, -C, D)
@ -409,7 +432,8 @@ impl FieldElement2625x4 {
///
/// The coefficients of the result are bounded with \\( b < 0.0002 \\).
#[inline]
pub fn reduce(&self) -> FieldElement2625x4 {
#[target_feature(enable = "avx2")]
pub unsafe fn reduce(&self) -> FieldElement2625x4 {
let shifts = u32x8::new(26, 26, 25, 25, 26, 26, 25, 25);
let masks = u32x8::new(
(1 << 26) - 1,
@ -518,7 +542,8 @@ impl FieldElement2625x4 {
/// The coefficients of the result are bounded with \\( b < 0.007 \\).
#[inline]
#[rustfmt::skip] // keep alignment of carry chain
fn reduce64(mut z: [u64x4; 10]) -> FieldElement2625x4 {
#[target_feature(enable = "avx2")]
unsafe fn reduce64(mut z: [u64x4; 10]) -> FieldElement2625x4 {
// These aren't const because splat isn't a const fn
let LOW_25_BITS: u64x4 = u64x4::splat((1 << 25) - 1);
let LOW_26_BITS: u64x4 = u64x4::splat((1 << 26) - 1);
@ -594,7 +619,8 @@ impl FieldElement2625x4 {
///
/// The coefficients of the result are bounded with \\( b < 0.007 \\).
#[rustfmt::skip] // keep alignment of z* calculations
pub fn square_and_negate_D(&self) -> FieldElement2625x4 {
#[target_feature(enable = "avx2")]
pub unsafe fn square_and_negate_D(&self) -> FieldElement2625x4 {
#[inline(always)]
fn m(x: u32x8, y: u32x8) -> u64x4 {
x.mul32(y)
@ -681,7 +707,6 @@ impl FieldElement2625x4 {
}
}
#[unsafe_target_feature("avx2")]
impl Neg for FieldElement2625x4 {
type Output = FieldElement2625x4;
@ -697,36 +722,46 @@ impl Neg for FieldElement2625x4 {
/// # Postconditions
///
/// The coefficients of the result are bounded with \\( b < 0.0002 \\).
#[inline]
#[inline(always)]
fn neg(self) -> FieldElement2625x4 {
#[inline]
#[target_feature(enable = "avx2")]
unsafe fn inner(itself: FieldElement2625x4) -> FieldElement2625x4 {
FieldElement2625x4([
P_TIMES_16_LO - self.0[0],
P_TIMES_16_HI - self.0[1],
P_TIMES_16_HI - self.0[2],
P_TIMES_16_HI - self.0[3],
P_TIMES_16_HI - self.0[4],
P_TIMES_16_LO - itself.0[0],
P_TIMES_16_HI - itself.0[1],
P_TIMES_16_HI - itself.0[2],
P_TIMES_16_HI - itself.0[3],
P_TIMES_16_HI - itself.0[4],
])
.reduce()
}
}
#[unsafe_target_feature("avx2")]
impl Add<FieldElement2625x4> for FieldElement2625x4 {
type Output = FieldElement2625x4;
/// Add two `FieldElement2625x4`s, without performing a reduction.
#[inline]
fn add(self, rhs: FieldElement2625x4) -> FieldElement2625x4 {
FieldElement2625x4([
self.0[0] + rhs.0[0],
self.0[1] + rhs.0[1],
self.0[2] + rhs.0[2],
self.0[3] + rhs.0[3],
self.0[4] + rhs.0[4],
])
unsafe { inner(self) }
}
}
impl Add<FieldElement2625x4> for FieldElement2625x4 {
type Output = FieldElement2625x4;
/// Add two `FieldElement2625x4`s, without performing a reduction.
#[inline(always)]
fn add(self, rhs: FieldElement2625x4) -> FieldElement2625x4 {
#[inline]
#[target_feature(enable = "avx2")]
unsafe fn inner(itself: FieldElement2625x4, rhs: FieldElement2625x4) -> FieldElement2625x4 {
FieldElement2625x4([
itself.0[0] + rhs.0[0],
itself.0[1] + rhs.0[1],
itself.0[2] + rhs.0[2],
itself.0[3] + rhs.0[3],
itself.0[4] + rhs.0[4],
])
}
unsafe { inner(self, rhs) }
}
}
#[unsafe_target_feature("avx2")]
impl Mul<(u32, u32, u32, u32)> for FieldElement2625x4 {
type Output = FieldElement2625x4;
/// Perform a multiplication by a vector of small constants.
@ -734,15 +769,21 @@ impl Mul<(u32, u32, u32, u32)> for FieldElement2625x4 {
/// # Postconditions
///
/// The coefficients of the result are bounded with \\( b < 0.007 \\).
#[inline]
#[inline(always)]
fn mul(self, scalars: (u32, u32, u32, u32)) -> FieldElement2625x4 {
#[inline]
#[target_feature(enable = "avx2")]
unsafe fn inner(
itself: FieldElement2625x4,
scalars: (u32, u32, u32, u32),
) -> FieldElement2625x4 {
let consts = u32x8::new(scalars.0, 0, scalars.1, 0, scalars.2, 0, scalars.3, 0);
let (b0, b1) = unpack_pair(self.0[0]);
let (b2, b3) = unpack_pair(self.0[1]);
let (b4, b5) = unpack_pair(self.0[2]);
let (b6, b7) = unpack_pair(self.0[3]);
let (b8, b9) = unpack_pair(self.0[4]);
let (b0, b1) = unpack_pair(itself.0[0]);
let (b2, b3) = unpack_pair(itself.0[1]);
let (b4, b5) = unpack_pair(itself.0[2]);
let (b6, b7) = unpack_pair(itself.0[3]);
let (b8, b9) = unpack_pair(itself.0[4]);
FieldElement2625x4::reduce64([
b0.mul32(consts),
@ -757,9 +798,11 @@ impl Mul<(u32, u32, u32, u32)> for FieldElement2625x4 {
b9.mul32(consts),
])
}
unsafe { inner(self, scalars) }
}
}
#[unsafe_target_feature("avx2")]
impl<'a, 'b> Mul<&'b FieldElement2625x4> for &'a FieldElement2625x4 {
type Output = FieldElement2625x4;
/// Multiply `self` by `rhs`.
@ -775,8 +818,11 @@ impl<'a, 'b> Mul<&'b FieldElement2625x4> for &'a FieldElement2625x4 {
/// The coefficients of the result are bounded with \\( b < 0.007 \\).
///
#[rustfmt::skip] // keep alignment of z* calculations
#[inline]
#[inline(always)]
fn mul(self, rhs: &'b FieldElement2625x4) -> FieldElement2625x4 {
#[inline]
#[target_feature(enable = "avx2")]
unsafe fn inner<'a, 'b>(itself: &'a FieldElement2625x4, rhs: &'b FieldElement2625x4) -> FieldElement2625x4 {
#[inline(always)]
fn m(x: u32x8, y: u32x8) -> u64x4 {
x.mul32(y)
@ -787,11 +833,11 @@ impl<'a, 'b> Mul<&'b FieldElement2625x4> for &'a FieldElement2625x4 {
x.mul32(y).into()
}
let (x0, x1) = unpack_pair(self.0[0]);
let (x2, x3) = unpack_pair(self.0[1]);
let (x4, x5) = unpack_pair(self.0[2]);
let (x6, x7) = unpack_pair(self.0[3]);
let (x8, x9) = unpack_pair(self.0[4]);
let (x0, x1) = unpack_pair(itself.0[0]);
let (x2, x3) = unpack_pair(itself.0[1]);
let (x4, x5) = unpack_pair(itself.0[2]);
let (x6, x7) = unpack_pair(itself.0[3]);
let (x8, x9) = unpack_pair(itself.0[4]);
let (y0, y1) = unpack_pair(rhs.0[0]);
let (y2, y3) = unpack_pair(rhs.0[1]);
@ -868,6 +914,11 @@ impl<'a, 'b> Mul<&'b FieldElement2625x4> for &'a FieldElement2625x4 {
// looser bound on b_x.
FieldElement2625x4::reduce64([z0, z1, z2, z3, z4, z5, z6, z7, z8, z9])
}
unsafe {
inner(self, rhs)
}
}
}
#[cfg(target_feature = "avx2")]

View file

@ -252,9 +252,9 @@ impl u64x4 {
}
/// Constructs a new instance.
#[unsafe_target_feature("avx2")]
#[target_feature(enable = "avx2")]
#[inline]
pub fn new(x0: u64, x1: u64, x2: u64, x3: u64) -> u64x4 {
pub unsafe fn new(x0: u64, x1: u64, x2: u64, x3: u64) -> u64x4 {
unsafe {
// _mm256_set_epi64 sets the underlying vector in reverse order of the args
u64x4(core::arch::x86_64::_mm256_set_epi64x(
@ -264,9 +264,9 @@ impl u64x4 {
}
/// Constructs a new instance with all of the elements initialized to the given value.
#[unsafe_target_feature("avx2")]
#[target_feature(enable = "avx2")]
#[inline]
pub fn splat(x: u64) -> u64x4 {
pub unsafe fn splat(x: u64) -> u64x4 {
unsafe { u64x4(core::arch::x86_64::_mm256_set1_epi64x(x as i64)) }
}
}
@ -303,9 +303,18 @@ impl u32x8 {
/// Constructs a new instance.
#[allow(clippy::too_many_arguments)]
#[unsafe_target_feature("avx2")]
#[target_feature(enable = "avx2")]
#[inline]
pub fn new(x0: u32, x1: u32, x2: u32, x3: u32, x4: u32, x5: u32, x6: u32, x7: u32) -> u32x8 {
pub unsafe fn new(
x0: u32,
x1: u32,
x2: u32,
x3: u32,
x4: u32,
x5: u32,
x6: u32,
x7: u32,
) -> u32x8 {
unsafe {
// _mm256_set_epi32 sets the underlying vector in reverse order of the args
u32x8(core::arch::x86_64::_mm256_set_epi32(
@ -316,9 +325,9 @@ impl u32x8 {
}
/// Constructs a new instance with all of the elements initialized to the given value.
#[unsafe_target_feature("avx2")]
#[target_feature(enable = "avx2")]
#[inline]
pub fn splat(x: u32) -> u32x8 {
pub unsafe fn splat(x: u32) -> u32x8 {
unsafe { u32x8(core::arch::x86_64::_mm256_set1_epi32(x as i32)) }
}
}

View file

@ -9,22 +9,15 @@
#![allow(non_snake_case)]
#[unsafe_target_feature::unsafe_target_feature_specialize(
conditional("avx2", feature = "simd_avx2"),
conditional("avx512ifma,avx512vl", all(feature = "simd_avx512", nightly))
)]
pub mod spec {
macro_rules! implement {
($module:ident, $backend_module:ident, $features:expr) => {
pub mod $module {
use alloc::vec::Vec;
use core::borrow::Borrow;
use core::cmp::Ordering;
#[for_target_feature("avx2")]
use crate::backend::vector::avx2::{CachedPoint, ExtendedPoint};
#[for_target_feature("avx512ifma")]
use crate::backend::vector::ifma::{CachedPoint, ExtendedPoint};
use crate::backend::vector::$backend_module::{CachedPoint, ExtendedPoint};
use crate::edwards::EdwardsPoint;
use crate::scalar::Scalar;
@ -38,7 +31,15 @@ pub mod spec {
impl VartimeMultiscalarMul for Pippenger {
type Point = EdwardsPoint;
#[inline(always)]
fn optional_multiscalar_mul<I, J>(scalars: I, points: J) -> Option<EdwardsPoint>
where
I: IntoIterator,
I::Item: Borrow<Scalar>,
J: IntoIterator<Item = Option<EdwardsPoint>>,
{
#[target_feature(enable = $features)]
unsafe fn inner<I, J>(scalars: I, points: J) -> Option<EdwardsPoint>
where
I: IntoIterator,
I::Item: Borrow<Scalar>,
@ -116,7 +117,8 @@ pub mod spec {
for i in (0..(buckets_count - 1)).rev() {
buckets_intermediate_sum =
&buckets_intermediate_sum + &CachedPoint::from(buckets[i]);
buckets_sum = &buckets_sum + &CachedPoint::from(buckets_intermediate_sum);
buckets_sum =
&buckets_sum + &CachedPoint::from(buckets_intermediate_sum);
}
buckets_sum
@ -134,9 +136,12 @@ pub mod spec {
.into(),
)
}
unsafe { inner(scalars, points) }
}
}
#[cfg(test)]
#[cfg(target_feature = $features)]
mod test {
#[test]
fn test_vartime_pippenger() {
@ -166,7 +171,8 @@ pub mod spec {
let points = &points[0..n].to_vec();
let control: EdwardsPoint = premultiplied[0..n].iter().sum();
let subject = Pippenger::vartime_multiscalar_mul(scalars.clone(), points.clone());
let subject =
Pippenger::vartime_multiscalar_mul(scalars.clone(), points.clone());
assert_eq!(subject.compress(), control.compress());
@ -174,4 +180,12 @@ pub mod spec {
}
}
}
}
};
}
#[cfg(feature = "simd_avx2")]
implement!(spec_avx2, avx2, "avx2");
#[cfg(all(feature = "simd_avx512", nightly))]
implement!(spec_avx512ifma_avx512vl, ifma, "avx512ifma,avx512vl");