Use wheel factorization for prime numbers

Certain existing units in the library require very large prime
numbers---so large, in fact, that our naive trial division hits the
_iteration limit_ for `constexpr` loops.  We don't want to force users
to provide a compiler option override, so we'd better find another way.

The solution is to use the "wheel factorization" algorithm:
https://en.wikipedia.org/wiki/Wheel_factorization

This lets us skip most composite numbers in our trial division.  The
implementation presented here is configurable in terms of the size of
the "basis" of primes we use.  Bigger bases let us skip more primes, but
at the cost of storing more numbers.  Fortunately, it turns out that N=3
was good enough for our purposes.
This commit is contained in:
Chip Hogg committed 2022-03-10 23:29:02 +00:00
1 parent 2198c8a403
commit 04b80f0827
5 files changed
+218 -21

No files matched your search

+9 -9
View File
@@ -140,6 +140,15 @@ TEST_CASE("make_ratio performs prime factorization correctly")
// The failure was due to a prime factor which is larger than 2^31.
as_magnitude<ratio(16'605'390'666'050, 10'000'000'000'000)>();
}
SECTION("Can handle prime number which would exceed GCC iteration limit")
{
// GCC 10 has a constexpr loop iteration limit of 262144. A naive algorithm, which performs trial division on 2 and
// all odd numbers up to sqrt(N), will exceed this limit for the following prime. Thus, for this test to pass, we
// need to be using a more efficient algorithm. (We could increase the limit, but we don't want users to have to
// mess with compiler flags just to compile the code.)
as_magnitude<334524384739>();
}
}
TEST_CASE("magnitude converts to numerical value")
@@ -344,15 +353,6 @@ TEST_CASE("int_power computes integer powers") {
TEST_CASE("Prime helper functions")
{
SECTION("find_first_factor()") {
CHECK(find_first_factor(1) == 1);
CHECK(find_first_factor(2) == 2);
CHECK(find_first_factor(4) == 2);
CHECK(find_first_factor(6) == 2);
CHECK(find_first_factor(15) == 3);
CHECK(find_first_factor(17) == 17);
}
SECTION("multiplicity") {
CHECK(multiplicity(2, 8) == 3);
CHECK(multiplicity(2, 1024) == 10);
+1
View File
@@ -52,6 +52,7 @@ add_library(unit_tests_static
kind_test.cpp
math_test.cpp
point_origin_test.cpp
prime_test.cpp
ratio_test.cpp
references_test.cpp
si_test.cpp
+57
View File
@@ -0,0 +1,57 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <units/bits/prime.h>
#include <type_traits>
#include <utility>
namespace units::detail
{
template<std::size_t BasisSize, std::size_t... Is>
constexpr bool check_primes(std::index_sequence<Is...>) {
return ((Is < 2 || WheelFactorizer<BasisSize>::is_prime(Is) == is_prime_by_trial_division(Is)) && ...);
}
static_assert(check_primes<2>(std::make_index_sequence<122>{}));
constexpr auto some_primes = primes_between<7, 19>(); // 7, 11, 13, 17
static_assert(std::is_same_v<std::remove_cvref_t<decltype(some_primes)>, std::array<std::size_t, 4>>);
static_assert(some_primes[0] == 7);
static_assert(some_primes[1] == 11);
static_assert(some_primes[2] == 13);
static_assert(some_primes[3] == 17);
static_assert(!WheelFactorizer<1>::is_prime(0));
static_assert(!WheelFactorizer<1>::is_prime(1));
static_assert(WheelFactorizer<1>::is_prime(2));
static_assert(!WheelFactorizer<2>::is_prime(0));
static_assert(!WheelFactorizer<2>::is_prime(1));
static_assert(WheelFactorizer<2>::is_prime(2));
static_assert(!WheelFactorizer<3>::is_prime(0));
static_assert(!WheelFactorizer<3>::is_prime(1));
static_assert(WheelFactorizer<3>::is_prime(2));
} // namespace units::detail