Skip to content

Commit 9f5b643

Browse files
authored
Merge pull request #638 from ckormanyos/benches_and_examples
Update math benches and example 16_08
2 parents 7dba0bf + 1e1710e commit 9f5b643

14 files changed

Lines changed: 301 additions & 351 deletions

examples/chapter16_08/build.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
@rem
3-
@rem Copyright Christopher Kormanyos 2014 - 2021.
3+
@rem Copyright Christopher Kormanyos 2014 - 2025.
44
@rem Distributed under the Boost Software License,
55
@rem Version 1.0. (See accompanying file LICENSE_1_0.txt
66
@rem or copy at http://www.boost.org/LICENSE_1_0.txt)

examples/chapter16_08/chapter16_08.vcxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@
5454
<CodeAnalysisRuleAssemblies Condition="'$(Configuration)|$(Platform)'=='Release|x64'" />
5555
</PropertyGroup>
5656
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
57-
<IncludePath>$(SolutionDir)\src;$(SolutionDir)\src\mcal\win32;C:\boost\boost_1_87_0;$(IncludePath)</IncludePath>
57+
<IncludePath>$(SolutionDir)\src;$(SolutionDir)\src\mcal\win32;C:\boost\boost_1_88_0;$(IncludePath)</IncludePath>
5858
<OutDir>$(SolutionDir)$(Configuration)\</OutDir>
5959
<IntDir>$(Configuration)\</IntDir>
6060
</PropertyGroup>
6161
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
62-
<IncludePath>$(SolutionDir)\src;$(SolutionDir)\src\mcal\win32;C:\boost\boost_1_87_0;$(IncludePath)</IncludePath>
62+
<IncludePath>$(SolutionDir)\src;$(SolutionDir)\src\mcal\win32;C:\boost\boost_1_88_0;$(IncludePath)</IncludePath>
6363
<OutDir>$(SolutionDir)$(Configuration)\</OutDir>
6464
<IntDir>$(Configuration)\</IntDir>
6565
</PropertyGroup>

examples/chapter16_08/chapter16_08.vcxproj.filters

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,9 +515,6 @@
515515
<ClInclude Include="src\math\test\math_test_independent_test_system_mathlink.h">
516516
<Filter>src\math\test</Filter>
517517
</ClInclude>
518-
<ClInclude Include="src\math\wide_integer\miller_rabin\miller_rabin_digits.h">
519-
<Filter>src\math\wide_integer\miller_rabin</Filter>
520-
</ClInclude>
521518
<ClInclude Include="src\util\STL\impl\avr\avr_atomic.h">
522519
<Filter>src\util\STL\impl\avr</Filter>
523520
</ClInclude>
@@ -542,6 +539,9 @@
542539
<ClInclude Include="src\util\STL\time.h">
543540
<Filter>src\util\STL</Filter>
544541
</ClInclude>
542+
<ClInclude Include="src\math\wide_integer\miller_rabin\miller_rabin_digits.h">
543+
<Filter>src\math\wide_integer\miller_rabin</Filter>
544+
</ClInclude>
545545
</ItemGroup>
546546
<ItemGroup>
547547
<None Include="src\util\STL_C++XX_stdfloat\cstdfloat">

examples/chapter16_08/src/math/wide_integer/miller_rabin/miller_rabin_base.h

Lines changed: 44 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
///////////////////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 2020 - 2024.
2+
// Copyright Christopher Kormanyos 2020 - 2025.
33
// Distributed under the Boost Software License,
44
// Version 1.0. (See accompanying file LICENSE_1_0.txt
55
// or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -8,7 +8,11 @@
88
#ifndef MILLER_RABIN_BASE_2020_05_30_H_
99
#define MILLER_RABIN_BASE_2020_05_30_H_
1010

11+
// Note that -DWIDE_INTEGER_NAMESPACE=ckormanyos is defined
12+
// on the compiler command line.
13+
1114
#include <math/wide_integer/miller_rabin/miller_rabin_digits.h>
15+
#include <math/wide_integer/uintwide_t.h>
1216

1317
WIDE_INTEGER_NAMESPACE_BEGIN
1418

@@ -30,27 +34,27 @@
3034

3135
virtual ~miller_rabin_base() = default;
3236

33-
virtual auto search() -> bool = 0;
37+
virtual bool search() = 0;
3438

35-
auto get_n_is_probably_prime() const -> bool
39+
bool get_n_is_probably_prime() const
3640
{
3741
return my_n_is_probably_prime;
3842
}
3943

40-
auto get_n() const -> const wide_integer_type&
44+
const wide_integer_type& get_n() const
4145
{
4246
return my_n;
4347
}
4448

45-
auto reseed1(const typename generator1_type::result_type seed1) -> void { my_generator1.seed(seed1); }
46-
auto reseed2(const typename generator2_type::result_type seed2) -> void { my_generator2.seed(seed2); }
49+
void reseed1(const typename generator1_type::result_type seed1) { my_generator1.seed(seed1); }
50+
void reseed2(const typename generator2_type::result_type seed2) { my_generator2.seed(seed2); }
4751

48-
static auto get_n_total_mul_10() -> std::uint64_t { return my_n_total_mul_10; }
52+
static std::uint64_t get_n_total_mul_10() { return my_n_total_mul_10; }
4953

5054
protected:
5155
using distribution_param_type = typename distribution_type::param_type;
5256

53-
static constexpr std::uint_fast32_t my_number_of_trials { UINT8_C(25) };
57+
static constexpr std::uint_fast32_t my_number_of_trials = UINT32_C(25);
5458

5559
wide_integer_type my_n_trial;
5660
bool my_n_trial_is_probably_prime;
@@ -59,9 +63,9 @@
5963
wide_integer_type my_n_m1;
6064
generator1_type my_generator1;
6165
generator2_type my_generator2;
62-
distribution_type my_distribution { };
63-
std::uint_fast32_t my_k { };
64-
wide_integer_type my_q { };
66+
distribution_type my_distribution;
67+
std::uint_fast32_t my_k;
68+
wide_integer_type my_q;
6569

6670
constexpr miller_rabin_base(const typename generator1_type::result_type seed1 = typename generator1_type::result_type(),
6771
const typename generator2_type::result_type seed2 = typename generator2_type::result_type())
@@ -71,9 +75,12 @@
7175
my_n_is_probably_prime (false),
7276
my_n_m1 (),
7377
my_generator1 (seed1),
74-
my_generator2 (seed2) { }
78+
my_generator2 (seed2),
79+
my_distribution (),
80+
my_k (0U),
81+
my_q () { }
7582

76-
auto set_n() -> bool
83+
bool set_n()
7784
{
7885
my_distribution.param(distribution_param_type());
7986

@@ -89,74 +96,38 @@
8996
{
9097
const limb_type lo_limb = my_n_trial.crepresentation().front();
9198

92-
const std::uint8_t
93-
lo_bit
94-
{
95-
static_cast<std::uint8_t>
96-
(
97-
static_cast<std::uint8_t>(lo_limb) & std::uint8_t { UINT8_C(1) }
98-
)
99-
};
99+
const std::uint8_t lo_bit = std::uint8_t(std::uint8_t(lo_limb) & 1U);
100100

101101
// Remove all even candidates since they are non-prime.
102-
if(lo_bit != std::uint8_t { UINT8_C(0) })
102+
if(lo_bit != 0U)
103103
{
104-
const std::uint8_t
105-
lo_digit10
106-
{
107-
static_cast<std::uint8_t>(my_n_trial % std::uint8_t { UINT8_C(10) })
108-
};
104+
const std::uint8_t lo_digit10 = std::uint8_t(my_n_trial % std::uint8_t(10U));
109105

110106
// Continue by removing candidates having trailing digit 5.
111107
// The result is all candidates have trailing digit 1,3,7,9
112-
113-
if(lo_digit10 != std::uint8_t { UINT8_C(5) })
108+
if(lo_digit10 != 5U)
114109
{
115110
// Now remove candidates having digital root 3, 6 or 9
116111
// because these are divisible by 3 and thus non-prime.
117112

118113
// To compute the digital root of n dr(n), use
119114
// dr(n) = 1 + (n - 1) % 9.
120-
wide_integer_type n_minus_one { my_n_trial };
115+
wide_integer_type n_minus_one(my_n_trial);
121116

122117
--n_minus_one;
123118

124-
const std::uint8_t
125-
digital_root
126-
{
127-
static_cast<std::uint8_t>
128-
(
129-
std::uint8_t { UINT8_C(1) }
130-
+ static_cast<std::uint8_t>(n_minus_one % std::uint8_t { UINT8_C(9) })
131-
)
132-
};
133-
134-
const bool
135-
has_digital_root_of_3_6_or_9
136-
{
137-
(digital_root == std::uint8_t { UINT8_C(3) })
138-
|| (digital_root == std::uint8_t { UINT8_C(6) })
139-
|| (digital_root == std::uint8_t { UINT8_C(9) })
140-
};
141-
142-
if(!has_digital_root_of_3_6_or_9)
143-
{
144-
// We have found a viable prime candidate. Use this prime
145-
// candidate to start a new Miller-Rabin primality test.
119+
const std::uint8_t dr =
120+
std::uint8_t(UINT8_C(1) + (std::uint8_t(n_minus_one % std::uint8_t(9U))));
146121

122+
if((dr != UINT8_C(3)) && (dr != UINT8_C(6)) && (dr != UINT8_C(9)))
123+
{
147124
set_n_is_ok = true;
148125

149-
// Reset the prime-search variables.
150-
151126
my_n_trial_is_probably_prime = false;
152127
my_n = my_n_trial;
153128
my_n_is_probably_prime = false;
154129

155-
my_n_total_mul_10 =
156-
static_cast<std::uint64_t>
157-
(
158-
my_n_total_mul_10 + std::uint_fast8_t { UINT8_C(10) }
159-
);
130+
my_n_total_mul_10 += 10U;
160131
}
161132
}
162133
}
@@ -165,18 +136,18 @@
165136
return set_n_is_ok;
166137
}
167138

168-
auto exclude_small_factors_1_0() -> bool { return do_exclude_small_factors_from_ppn({ 3U, 5U, 7U, 11U, 13U, 17U, 19U, 23U }, UINT32_C( 223092870)); }
169-
auto exclude_small_factors_2_0() -> bool { return do_exclude_small_factors_from_ppn({ 29U, 31U, 37U, 41U, 43U, 47U }, UINT32_C(2756205443)); }
170-
auto exclude_small_factors_3_0() -> bool { return do_exclude_small_factors_from_ppn({ 53U, 59U, 61U, 67U, 71U }, UINT32_C( 907383479)); }
171-
auto exclude_small_factors_4_0() -> bool { return do_exclude_small_factors_from_ppn({ 73U, 79U, 83U, 89U, 97U }, UINT32_C(4132280413)); }
172-
auto exclude_small_factors_5_0() -> bool { return do_exclude_small_factors_from_ppn({ 101U, 103U, 107U, 109U }, UINT32_C( 121330189)); }
173-
auto exclude_small_factors_5_1() -> bool { return do_exclude_small_factors_from_ppn({ 113U, 127U, 131U, 137U }, std::uint32_t(113ULL * 127ULL * 131ULL * 137ULL)); }
174-
auto exclude_small_factors_5_2() -> bool { return do_exclude_small_factors_from_ppn({ 139U, 149U, 151U, 157U }, std::uint32_t(139ULL * 149ULL * 151ULL * 157ULL)); }
175-
auto exclude_small_factors_5_3() -> bool { return do_exclude_small_factors_from_ppn({ 163U, 167U, 173U, 179U }, std::uint32_t(163ULL * 167ULL * 173ULL * 179ULL)); }
176-
auto exclude_small_factors_5_4() -> bool { return do_exclude_small_factors_from_ppn({ 181U, 191U, 193U, 197U }, std::uint32_t(181ULL * 191ULL * 193ULL * 197ULL)); }
177-
auto exclude_small_factors_5_5() -> bool { return do_exclude_small_factors_from_ppn({ 199U, 211U, 223U, 227U }, std::uint32_t(199ULL * 211ULL * 223ULL * 227ULL)); }
178-
179-
auto prepare_random_trials() -> void
139+
bool exclude_small_factors_1_0() { return do_exclude_small_factors_from_ppn({ 3U, 5U, 7U, 11U, 13U, 17U, 19U, 23U }, UINT32_C( 223092870)); }
140+
bool exclude_small_factors_2_0() { return do_exclude_small_factors_from_ppn({ 29U, 31U, 37U, 41U, 43U, 47U }, UINT32_C(2756205443)); }
141+
bool exclude_small_factors_3_0() { return do_exclude_small_factors_from_ppn({ 53U, 59U, 61U, 67U, 71U }, UINT32_C( 907383479)); }
142+
bool exclude_small_factors_4_0() { return do_exclude_small_factors_from_ppn({ 73U, 79U, 83U, 89U, 97U }, UINT32_C(4132280413)); }
143+
bool exclude_small_factors_5_0() { return do_exclude_small_factors_from_ppn({ 101U, 103U, 107U, 109U }, UINT32_C( 121330189)); }
144+
bool exclude_small_factors_5_1() { return do_exclude_small_factors_from_ppn({ 113U, 127U, 131U, 137U }, std::uint32_t(113ULL * 127ULL * 131ULL * 137ULL)); }
145+
bool exclude_small_factors_5_2() { return do_exclude_small_factors_from_ppn({ 139U, 149U, 151U, 157U }, std::uint32_t(139ULL * 149ULL * 151ULL * 157ULL)); }
146+
bool exclude_small_factors_5_3() { return do_exclude_small_factors_from_ppn({ 163U, 167U, 173U, 179U }, std::uint32_t(163ULL * 167ULL * 173ULL * 179ULL)); }
147+
bool exclude_small_factors_5_4() { return do_exclude_small_factors_from_ppn({ 181U, 191U, 193U, 197U }, std::uint32_t(181ULL * 191ULL * 193ULL * 197ULL)); }
148+
bool exclude_small_factors_5_5() { return do_exclude_small_factors_from_ppn({ 199U, 211U, 223U, 227U }, std::uint32_t(199ULL * 211ULL * 223ULL * 227ULL)); }
149+
150+
void prepare_random_trials()
180151
{
181152
my_k = lsb(my_n_m1);
182153

@@ -188,7 +159,7 @@
188159
my_n_trial_is_probably_prime = true;
189160
}
190161

191-
virtual auto execute_one_trial() -> bool
162+
virtual bool execute_one_trial()
192163
{
193164
wide_integer_type y = powm(my_distribution(my_generator2), my_q, my_n_trial);
194165

@@ -228,7 +199,7 @@
228199
private:
229200
static std::uint64_t my_n_total_mul_10;
230201

231-
auto do_exclude_small_factors_from_ppn(std::initializer_list<std::uint8_t> small_factors, const std::uint32_t& ppn) -> bool
202+
bool do_exclude_small_factors_from_ppn(std::initializer_list<std::uint8_t> small_factors, const std::uint32_t& ppn)
232203
{
233204
bool exclude_does_terminate_prime_candidate = false;
234205

0 commit comments

Comments
 (0)