Skip to content

Commit 09ea81f

Browse files
authored
Merge pull request #442 from ckormanyos/sysntax_and_tests
Sysntax and tests
2 parents c725651 + 169235c commit 09ea81f

File tree

4 files changed

+133
-88
lines changed

4 files changed

+133
-88
lines changed

math/wide_integer/uintwide_t.h

Lines changed: 65 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
///////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 1999 - 2024. //
2+
// Copyright Christopher Kormanyos 1999 - 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) //
@@ -59,7 +59,7 @@
5959
#endif
6060

6161
#if (defined(_MSC_VER) && (!defined(__GNUC__) && !defined(__clang__)))
62-
#if (_MSC_VER >= 1900) && defined(_HAS_CXX20) && (_HAS_CXX20 != 0)
62+
#if ((_MSC_VER >= 1900) && (defined(_HAS_CXX20) && (_HAS_CXX20 != 0)))
6363
#define WIDE_INTEGER_NODISCARD [[nodiscard]] // NOLINT(cppcoreguidelines-macro-usage)
6464
#else
6565
#define WIDE_INTEGER_NODISCARD
@@ -97,11 +97,11 @@
9797

9898
namespace iterator_detail {
9999

100-
class input_iterator_tag {};
101-
class output_iterator_tag {};
102-
class forward_iterator_tag : public input_iterator_tag {};
103-
class bidirectional_iterator_tag : public forward_iterator_tag {};
104-
class random_access_iterator_tag : public bidirectional_iterator_tag {};
100+
class input_iterator_tag { };
101+
class output_iterator_tag { };
102+
class forward_iterator_tag : public input_iterator_tag { };
103+
class bidirectional_iterator_tag : public forward_iterator_tag { };
104+
class random_access_iterator_tag : public bidirectional_iterator_tag { };
105105

106106
template<typename iterator_type>
107107
class iterator_traits
@@ -166,7 +166,7 @@
166166
using reference = typename iterator_traits<iterator_type>::reference;
167167
using iterator_category = typename iterator_traits<iterator_type>::iterator_category;
168168

169-
constexpr reverse_iterator() = default;
169+
constexpr reverse_iterator() { } // NOLINT(hicpp-use-equals-default,modernize-use-equals-default)
170170

171171
explicit constexpr reverse_iterator(iterator_type x) : current(x) { }
172172

@@ -218,7 +218,7 @@
218218
// Forward declaration of:
219219
// Use a local, constexpr, unsafe implementation of the abs-function.
220220
template<typename ArithmeticType>
221-
constexpr auto abs_unsafe(ArithmeticType val) -> ArithmeticType;
221+
constexpr auto abs_unsafe(const ArithmeticType& val) -> ArithmeticType;
222222

223223
// Use a local, constexpr, unsafe implementation of the fill-function.
224224
template<typename DestinationIterator,
@@ -261,7 +261,9 @@
261261
#pragma GCC diagnostic ignored "-Wstringop-overflow"
262262
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
263263
#endif
264+
264265
*dest++ = static_cast<local_destination_value_type>(*first++);
266+
265267
#if (defined(__GNUC__) && (__GNUC__ > 9))
266268
#pragma GCC diagnostic pop
267269
#pragma GCC diagnostic pop
@@ -289,10 +291,10 @@
289291
template<typename T>
290292
constexpr auto swap_unsafe(T& left, T& right) noexcept -> void
291293
{
292-
auto tmp = static_cast<T&&>(left);
294+
T tmp { std::move(static_cast<T&&>(left)) };
293295

294-
left = static_cast<T&&>(right);
295-
right = static_cast<T&&>(tmp);
296+
left = std::move(static_cast<T&&>(right));
297+
right = std::move(static_cast<T&&>(tmp));
296298
}
297299

298300
template<typename InputIt, typename UnaryPredicate>
@@ -738,9 +740,9 @@
738740
// Constructors.
739741
constexpr dynamic_array() = delete;
740742

741-
explicit constexpr dynamic_array( size_type count_in,
742-
const_reference value_in = value_type(),
743-
const allocator_type& alloc_in = allocator_type())
743+
explicit constexpr dynamic_array(size_type count_in,
744+
const_reference value_in = value_type(),
745+
const allocator_type& alloc_in = allocator_type())
744746
: elem_count(count_in)
745747
{
746748
if(elem_count > static_cast<size_type>(UINT8_C(0)))
@@ -779,8 +781,8 @@
779781

780782
template<typename input_iterator>
781783
constexpr dynamic_array(input_iterator first,
782-
input_iterator last,
783-
const allocator_type& alloc_in = allocator_type())
784+
input_iterator last,
785+
const allocator_type& alloc_in = allocator_type())
784786
: elem_count(static_cast<size_type>(last - first))
785787
{
786788
allocator_type my_alloc(alloc_in);
@@ -799,7 +801,7 @@
799801
}
800802

801803
constexpr dynamic_array(std::initializer_list<value_type> lst,
802-
const allocator_type& alloc_in = allocator_type())
804+
const allocator_type& alloc_in = allocator_type())
803805
: elem_count(lst.size())
804806
{
805807
allocator_type my_alloc(alloc_in);
@@ -818,7 +820,7 @@
818820

819821
// Move constructor.
820822
constexpr dynamic_array(dynamic_array&& other) noexcept : elem_count(other.elem_count),
821-
elems (other.elems)
823+
elems (other.elems)
822824
{
823825
other.elem_count = static_cast<size_type>(UINT8_C(0));
824826
other.elems = nullptr;
@@ -943,10 +945,10 @@
943945

944946
constexpr auto swap(dynamic_array&& other) noexcept -> void
945947
{
946-
const auto tmp = std::move(*this);
948+
dynamic_array tmp { std::move(*this) };
947949

948-
*this = std::move(other);
949-
other = std::move(tmp);
950+
*this = std::move(static_cast<dynamic_array&&>(other));
951+
other = std::move(static_cast<dynamic_array&&>(tmp));
950952
}
951953

952954
private:
@@ -972,7 +974,7 @@
972974

973975
template<typename ValueType, typename AllocatorType>
974976
constexpr auto operator<(const dynamic_array<ValueType, AllocatorType>& lhs,
975-
const dynamic_array<ValueType, AllocatorType>& rhs) -> bool
977+
const dynamic_array<ValueType, AllocatorType>& rhs) -> bool
976978
{
977979
using size_type = typename dynamic_array<ValueType, AllocatorType>::size_type;
978980

@@ -1106,8 +1108,8 @@
11061108
using size_t = std::uint32_t;
11071109
using ptrdiff_t = std::int32_t;
11081110

1109-
static_assert(( (std::numeric_limits<size_t>::digits >= std::numeric_limits<std::uint16_t>::digits)
1110-
&& (std::numeric_limits<ptrdiff_t>::digits + 1 >= std::numeric_limits<std::uint16_t>::digits)),
1111+
static_assert(( (std::numeric_limits<size_t>::digits >= std::numeric_limits<std::uint16_t>::digits)
1112+
&& (std::numeric_limits<ptrdiff_t>::digits + 1 >= std::numeric_limits<std::uint16_t>::digits)),
11111113
"Error: size type and pointer difference type must be at least 16 bits in width (or wider)");
11121114

11131115
template<const size_t Width2> struct verify_power_of_two // NOLINT(altera-struct-pack-align)
@@ -2109,11 +2111,11 @@
21092111
static_assert(((sizeof(limb_type) * 2U) == sizeof(double_limb_type)),
21102112
"Error: Please check the characteristics of the template parameters UnsignedShortType and UnsignedLargeType");
21112113
#else
2112-
static_assert(( ( std::numeric_limits<limb_type>::is_integer)
2113-
&& ( std::numeric_limits<double_limb_type>::is_integer)
2114-
&& (!std::numeric_limits<limb_type>::is_signed)
2115-
&& (!std::numeric_limits<double_limb_type>::is_signed)
2116-
&& ((sizeof(limb_type) * 2U) == sizeof(double_limb_type))),
2114+
static_assert(( ( std::numeric_limits<limb_type>::is_integer)
2115+
&& ( std::numeric_limits<double_limb_type>::is_integer)
2116+
&& (!std::numeric_limits<limb_type>::is_signed)
2117+
&& (!std::numeric_limits<double_limb_type>::is_signed)
2118+
&& ((sizeof(limb_type) * 2U) == sizeof(double_limb_type))),
21172119
"Error: Please check the characteristics of the template parameters UnsignedShortType and UnsignedLargeType");
21182120
#endif
21192121

@@ -4963,7 +4965,7 @@
49634965

49644966
for(auto t = static_cast<double_limb_type>(u_j_j1 - static_cast<double_limb_type>(q_hat * static_cast<double_limb_type>(vv_at_vj0)));
49654967
;
4966-
--q_hat, t = static_cast<double_limb_type>(t + vv_at_vj0))
4968+
t = static_cast<double_limb_type>(t + vv_at_vj0), --q_hat)
49674969
{
49684970
if( (detail::make_hi<limb_type>(t) != static_cast<limb_type>(UINT8_C(0)))
49694971
|| ( static_cast<double_limb_type>(static_cast<double_limb_type>(vv_at_vj0_minus_one) * q_hat)
@@ -6608,15 +6610,15 @@
66086610

66096611
// Use a local, constexpr, unsafe implementation of the abs-function.
66106612
template<typename ArithmeticType>
6611-
constexpr auto abs_unsafe(ArithmeticType val) -> ArithmeticType
6613+
constexpr auto abs_unsafe(const ArithmeticType& val) -> ArithmeticType
66126614
{
66136615
if(val > static_cast<int>(INT8_C(0)))
66146616
{
66156617
return val;
66166618
}
66176619
else // NOLINT(llvm-else-after-return,readability-else-after-return)
66186620
{
6619-
ArithmeticType val_unsigned = std::move(val);
6621+
ArithmeticType val_unsigned { val };
66206622

66216623
val_unsigned.negate();
66226624

@@ -6869,8 +6871,8 @@
68696871

68706872
template<typename GeneratorType,
68716873
const int GeneratorResultBits = std::numeric_limits<typename GeneratorType::result_type>::digits>
6872-
constexpr auto operator()( GeneratorType& input_generator,
6873-
const param_type& input_params) -> result_type
6874+
constexpr auto operator()(GeneratorType& input_generator,
6875+
const param_type& input_params) -> result_type
68746876
{
68756877
return
68766878
generate<GeneratorType, GeneratorResultBits>
@@ -6885,8 +6887,8 @@
68856887

68866888
template<typename GeneratorType,
68876889
const int GeneratorResultBits = std::numeric_limits<typename GeneratorType::result_type>::digits>
6888-
constexpr auto generate( GeneratorType& input_generator,
6889-
const param_type& input_params) const -> result_type
6890+
constexpr auto generate(GeneratorType& input_generator,
6891+
const param_type& input_params) const -> result_type
68906892
{
68916893
// Generate random numbers r, where a <= r <= b.
68926894

@@ -6994,7 +6996,7 @@
69946996
// This Miller-Rabin primality test is loosely based on
69956997
// an adaptation of some code from Boost.Multiprecision.
69966998
// The Boost.Multiprecision code can be found here:
6997-
// https://www.boost.org/doc/libs/1_76_0/libs/multiprecision/doc/html/boost_multiprecision/tut/primetest.html
6999+
// https://www.boost.org/doc/libs/1_87_0/libs/multiprecision/doc/html/boost_multiprecision/tut/primetest.html
69987000

69997001
// Note: Some comments in this subroutine use the Wolfram Language(TM).
70007002
// These can be exercised at the web links to WolframAlpha(R) provided
@@ -7131,10 +7133,10 @@
71317133
}
71327134
}
71337135

7134-
const auto nm1 = static_cast<local_wide_integer_type>(np - static_cast<unsigned>(UINT8_C(1)));
7136+
const local_wide_integer_type nm1 { np - static_cast<unsigned>(UINT8_C(1)) };
71357137

71367138
auto
7137-
isone
7139+
local_functor_isone
71387140
{
71397141
[](const local_wide_integer_type& t1)
71407142
{
@@ -7155,7 +7157,7 @@
71557157

71567158
const local_wide_integer_type fn { powm(local_wide_integer_type(static_cast<local_limb_type>(228U)), nm1, np) };
71577159

7158-
if(!isone(fn))
7160+
if(!local_functor_isone(fn))
71597161
{
71607162
return false;
71617163
}
@@ -7174,45 +7176,39 @@
71747176
np - unsigned { UINT8_C(2) }
71757177
};
71767178

7177-
local_wide_integer_type x;
7178-
local_wide_integer_type y;
7179-
71807179
// Assume the test will pass, even though it usually does not pass.
7181-
bool result { true };
7182-
7183-
// Loop over the trials to perform the primality testing.
7180+
bool result_candidate_is_prime { true };
71847181

71857182
std::size_t idx { UINT8_C(0) };
71867183

7187-
do
7188-
{
7189-
x = distribution(generator, params);
7190-
y = powm(x, q, np);
7184+
using local_double_width_type = typename local_wide_integer_type::double_width_type;
71917185

7192-
using local_double_width_type = typename local_wide_integer_type::double_width_type;
7186+
const local_double_width_type np_dbl { np };
71937187

7194-
const local_double_width_type np_dbl { np };
7188+
// Loop over the trials to perform the primality testing.
7189+
do
7190+
{
7191+
local_wide_integer_type y { powm(distribution(generator, params), q, np) };
71957192

71967193
std::size_t jdx { UINT8_C(0) };
71977194

71987195
// Continue while y is not nm1, and while y is not 1,
71997196
// and while the result is true.
72007197

7201-
while((y != nm1) && (!isone(y)) && result) // NOLINT(altera-id-dependent-backward-branch)
7198+
while((y != nm1) && (!local_functor_isone(y)) && result_candidate_is_prime) // NOLINT(altera-id-dependent-backward-branch)
72027199
{
72037200
++jdx;
72047201

72057202
if(jdx == static_cast<std::size_t>(k))
72067203
{
72077204
// Mark failure if max iterations reached.
7208-
result = false;
7205+
result_candidate_is_prime = false;
72097206
}
72107207
else
72117208
{
72127209
// Continue with the next value of y.
72137210

7214-
// Manually calculate:
7215-
// y = powm(y, 2, np);
7211+
// Manually calculate: y = powm(y, 2, np);
72167212

72177213
local_double_width_type yd { y };
72187214

@@ -7224,29 +7220,28 @@
72247220
}
72257221

72267222
// Check for (y == 1) after the loop.
7227-
if(isone(y) && (jdx != std::size_t { UINT8_C(0) }))
7223+
if(local_functor_isone(y) && (jdx != std::size_t { UINT8_C(0) }))
72287224
{
72297225
// Mark failure if (y == 1) and (jdx != 0).
7230-
result = false;
7226+
result_candidate_is_prime = false;
72317227
}
72327228

72337229
++idx;
72347230
}
7235-
while((idx < number_of_trials) && result);
7231+
while((idx < number_of_trials) && result_candidate_is_prime);
72367232

7237-
return result;
7233+
return result_candidate_is_prime;
72387234
}
72397235

72407236
#if (defined(__cpp_lib_to_chars) && (__cpp_lib_to_chars >= 201611L))
72417237
template<const size_t Width2,
72427238
typename LimbType,
72437239
typename AllocatorType,
72447240
const bool IsSigned>
7245-
constexpr
7246-
auto to_chars(char* first,
7247-
char* last,
7248-
const uintwide_t<Width2, LimbType, AllocatorType, IsSigned>& x,
7249-
int base) -> std::to_chars_result
7241+
constexpr auto to_chars(char* first,
7242+
char* last,
7243+
const uintwide_t<Width2, LimbType, AllocatorType, IsSigned>& x,
7244+
int base) -> std::to_chars_result
72507245
{
72517246
using local_wide_integer_type = uintwide_t<Width2, LimbType, AllocatorType, IsSigned>;
72527247

@@ -7406,11 +7401,10 @@
74067401
typename LimbType,
74077402
typename AllocatorType,
74087403
const bool IsSigned>
7409-
constexpr
7410-
auto from_chars(const char* first,
7411-
const char* last,
7412-
uintwide_t<Width2, LimbType, AllocatorType, IsSigned>& x,
7413-
int base) -> std::from_chars_result
7404+
constexpr auto from_chars(const char* first,
7405+
const char* last,
7406+
uintwide_t<Width2, LimbType, AllocatorType, IsSigned>& x,
7407+
int base) -> std::from_chars_result
74147408
{
74157409
using local_wide_integer_type = uintwide_t<Width2, LimbType, AllocatorType, IsSigned>;
74167410

0 commit comments

Comments
 (0)