Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[opt] Optimizing Shoup's MulMod in Apple M1/M2 chip. #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
98 changes: 98 additions & 0 deletions src/prime64/less_than_62bit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,53 @@ pub(crate) fn fwd_last_butterfly_avx2(
)
}

#[cfg(any(target_arch = "aarch64"))]
#[inline(always)]
pub(crate) fn fwd_butterfly_scalar(
z0: u64,
z1: u64,
w: u64,
w_shoup: u64,
p: u64,
neg_p: u64,
two_p: u64,
) -> (u64, u64) {
let _ = p;
let z0 = z0.min(z0.wrapping_sub(two_p));

let shoup_q_u128 = (z1 as u128 * w_shoup as u128) >> 64;
let t = ((z1 as u128 * w as u128) + (shoup_q_u128 * neg_p as u128)) as u64;

(z0.wrapping_add(t), z0.wrapping_sub(t).wrapping_add(two_p))
}

#[cfg(any(target_arch = "aarch64"))]
#[inline(always)]
pub(crate) fn fwd_last_butterfly_scalar(
z0: u64,
z1: u64,
w: u64,
w_shoup: u64,
p: u64,
neg_p: u64,
two_p: u64,
) -> (u64, u64) {
let _ = p;
let z0 = z0.min(z0.wrapping_sub(two_p));
let z0 = z0.min(z0.wrapping_sub(p));

let shoup_q_u128 = (z1 as u128 * w_shoup as u128) >> 64;
let t = ((z1 as u128 * w as u128) + (shoup_q_u128 * neg_p as u128)) as u64;

let t = t.min(t.wrapping_sub(p));
let res = (z0.wrapping_add(t), z0.wrapping_sub(t).wrapping_add(p));
(
res.0.min(res.0.wrapping_sub(p)),
res.1.min(res.1.wrapping_sub(p)),
)
}

#[cfg(not(target_arch = "aarch64"))]
#[inline(always)]
pub(crate) fn fwd_butterfly_scalar(
z0: u64,
Expand All @@ -130,6 +177,7 @@ pub(crate) fn fwd_butterfly_scalar(
(z0.wrapping_add(t), z0.wrapping_sub(t).wrapping_add(two_p))
}

#[cfg(not(target_arch = "aarch64"))]
#[inline(always)]
pub(crate) fn fwd_last_butterfly_scalar(
z0: u64,
Expand Down Expand Up @@ -267,6 +315,55 @@ pub(crate) fn inv_last_butterfly_avx2(
(y0, y1)
}

#[cfg(any(target_arch = "aarch64"))]
#[inline(always)]
pub(crate) fn inv_butterfly_scalar(
z0: u64,
z1: u64,
w: u64,
w_shoup: u64,
p: u64,
neg_p: u64,
two_p: u64,
) -> (u64, u64) {
let _ = p;

let y0 = z0.wrapping_add(z1);
let y0 = y0.min(y0.wrapping_sub(two_p));
let t = z0.wrapping_sub(z1).wrapping_add(two_p);

let shoup_q_u128 = (t as u128 * w_shoup as u128) >> 64;
let y1 = ((t as u128 * w as u128) + shoup_q_u128 * neg_p as u128) as u64;

(y0, y1)
}

#[cfg(any(target_arch = "aarch64"))]
#[inline(always)]
pub(crate) fn inv_last_butterfly_scalar(
z0: u64,
z1: u64,
w: u64,
w_shoup: u64,
p: u64,
neg_p: u64,
two_p: u64,
) -> (u64, u64) {
let _ = p;

let y0 = z0.wrapping_add(z1);
let y0 = y0.min(y0.wrapping_sub(two_p));
let y0 = y0.min(y0.wrapping_sub(p));
let t = z0.wrapping_sub(z1).wrapping_add(two_p);

let shoup_q_u128 = (t as u128 * w_shoup as u128) >> 64;
let y1 = ((t as u128 * w as u128) + shoup_q_u128 * neg_p as u128) as u64;

let y1 = y1.min(y1.wrapping_sub(p));
(y0, y1)
}

#[cfg(not(target_arch = "aarch64"))]
#[inline(always)]
pub(crate) fn inv_butterfly_scalar(
z0: u64,
Expand All @@ -287,6 +384,7 @@ pub(crate) fn inv_butterfly_scalar(
(y0, y1)
}

#[cfg(not(target_arch = "aarch64"))]
#[inline(always)]
pub(crate) fn inv_last_butterfly_scalar(
z0: u64,
Expand Down
73 changes: 73 additions & 0 deletions src/prime64/less_than_63bit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pub(crate) fn fwd_last_butterfly_avx2(
)
}

#[cfg(not(target_arch = "aarch64"))]
#[inline(always)]
pub(crate) fn fwd_butterfly_scalar(
z0: u64,
Expand All @@ -131,6 +132,7 @@ pub(crate) fn fwd_butterfly_scalar(
(z0.wrapping_add(t), z0.wrapping_sub(t).wrapping_add(p))
}

#[cfg(not(target_arch = "aarch64"))]
#[inline(always)]
pub(crate) fn fwd_last_butterfly_scalar(
z0: u64,
Expand All @@ -153,6 +155,52 @@ pub(crate) fn fwd_last_butterfly_scalar(
)
}

#[cfg(any(target_arch = "aarch64"))]
#[inline(always)]
pub(crate) fn fwd_butterfly_scalar(
z0: u64,
z1: u64,
w: u64,
w_shoup: u64,
p: u64,
neg_p: u64,
two_p: u64,
) -> (u64, u64) {
let _ = two_p;
let z0 = z0.min(z0.wrapping_sub(p));

let shoup_q_u128 = (z1 as u128 * w_shoup as u128) >> 64;
let t = ((z1 as u128 * w as u128) + (shoup_q_u128 * neg_p as u128)) as u64;

let t = t.min(t.wrapping_sub(p));
(z0.wrapping_add(t), z0.wrapping_sub(t).wrapping_add(p))
}

#[cfg(any(target_arch = "aarch64"))]
#[inline(always)]
pub(crate) fn fwd_last_butterfly_scalar(
z0: u64,
z1: u64,
w: u64,
w_shoup: u64,
p: u64,
neg_p: u64,
two_p: u64,
) -> (u64, u64) {
let _ = two_p;
let z0 = z0.min(z0.wrapping_sub(p));

let shoup_q_u128 = (z1 as u128 * w_shoup as u128) >> 64;
let t = ((z1 as u128 * w as u128) + (shoup_q_u128 * neg_p as u128)) as u64;

let t = t.min(t.wrapping_sub(p));
let res = (z0.wrapping_add(t), z0.wrapping_sub(t).wrapping_add(p));
(
res.0.min(res.0.wrapping_sub(p)),
res.1.min(res.1.wrapping_sub(p)),
)
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[cfg(feature = "nightly")]
#[inline(always)]
Expand Down Expand Up @@ -210,6 +258,31 @@ pub(crate) fn inv_butterfly_avx2(
(y0, y1)
}

#[cfg(any(target_arch = "aarch64"))]
#[inline(always)]
pub(crate) fn inv_butterfly_scalar(
z0: u64,
z1: u64,
w: u64,
w_shoup: u64,
p: u64,
neg_p: u64,
two_p: u64,
) -> (u64, u64) {
let _ = two_p;

let y0 = z0.wrapping_add(z1);
let y0 = y0.min(y0.wrapping_sub(p));
let t = z0.wrapping_sub(z1).wrapping_add(p);

let shoup_q_u128 = (t as u128 * w_shoup as u128) >> 64;
let y1 = ((t as u128 * w as u128) + shoup_q_u128 * neg_p as u128) as u64;

let y1 = y1.min(y1.wrapping_sub(p));
(y0, y1)
}

#[cfg(not(target_arch = "aarch64"))]
#[inline(always)]
pub(crate) fn inv_butterfly_scalar(
z0: u64,
Expand Down