Skip to content

Commit

Permalink
Undo typed stride as that introduces a runtime branch. Use suggestion…
Browse files Browse the repository at this point in the history
… by @JASory in #84 (#87)

* Undo typed stride as that introduces a runtime branch. Use suggestion by @JASory in #84

* Add debug assert
  • Loading branch information
JSorngard authored Nov 29, 2024
1 parent 411ef5c commit 6fc4e9f
Showing 1 changed file with 5 additions and 19 deletions.
24 changes: 5 additions & 19 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::is_prime;
/// Generalised function for nearest search by incrementing/decrementing by 1
/// Any attempt at optimising this would be largely pointless since the largest prime gap under 2^64 is only 1550
/// And is_prime's trial division already eliminates most of those
const fn bounded_search(mut n: u64, stride: Stride) -> Option<u64> {
let stride = stride.into_u64();
const fn bounded_search(mut n: u64, stride: u64) -> Option<u64> {
debug_assert!(stride == 1 || stride == u64::MAX);

loop {
// Addition over Z/2^64, aka regular addition under optimisation flags
Expand Down Expand Up @@ -46,7 +46,8 @@ const fn bounded_search(mut n: u64, stride: Stride) -> Option<u64> {
/// ```
#[must_use = "the function only returns a new value and does not modify its input"]
pub const fn previous_prime(n: u64) -> Option<u64> {
bounded_search(n, Stride::Down)
// Adding by 2^64-1 over Z/2^64 is equivalent to subtracting by 1
bounded_search(n, u64::MAX)
}

/// Returns the smallest prime greater than `n` if there is one that
Expand All @@ -73,20 +74,5 @@ pub const fn previous_prime(n: u64) -> Option<u64> {
/// ```
#[must_use = "the function only returns a new value and does not modify its input"]
pub const fn next_prime(n: u64) -> Option<u64> {
bounded_search(n, Stride::Up)
}

enum Stride {
Up,
Down,
}

impl Stride {
const fn into_u64(self) -> u64 {
match self {
Self::Up => 1,
// Adding by 2^64-1 over Z/2^64 is equivalent to subtracting by 1
Self::Down => u64::MAX,
}
}
bounded_search(n, 1)
}

0 comments on commit 6fc4e9f

Please sign in to comment.