Skip to content

Commit 49da1e9

Browse files
committed
Update and simplify from embedded port
1 parent 9daeb53 commit 49da1e9

13 files changed

+173
-97
lines changed

.gcov/make/make_gcov_01_generic.gmk

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# ------------------------------------------------------------------------------
2-
# Copyright Christopher Kormanyos 2022.
2+
# Copyright Christopher Kormanyos 2022 - 2024.
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)

.gcov/make/make_gcov_03_flags.gmk

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# ------------------------------------------------------------------------------
2-
# Copyright Christopher Kormanyos 2022.
2+
# Copyright Christopher Kormanyos 2022 - 2024.
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)
66
# ------------------------------------------------------------------------------
77

8-
BOOST_ROOT_FOR_GCOV = /mnt/c/boost/boost_1_78_0
8+
BOOST_ROOT_FOR_GCOV = /mnt/c/boost/boost_1_85_0
99
CC = g++
1010
STD = c++14
1111
ALL_COV = 0

examples/example007_random_generator.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace local_random
4545
// Purosely use the default seed.
4646
random_engine_type generator; // NOLINT(cert-msc32-c,cert-msc51-cpp)
4747

48-
distribution_type distribution;
48+
distribution_type distribution { };
4949

5050
const wide_integer_type n = distribution(generator);
5151

examples/example009_timed_mul.cpp

+10-18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
// or copy at http://www.boost.org/LICENSE_1_0.txt) //
66
///////////////////////////////////////////////////////////////////
77

8+
#include <examples/example_uintwide_t.h>
9+
#include <math/wide_integer/uintwide_t.h>
10+
#include <test/stopwatch.h>
11+
12+
#include <util/utility/util_pseudorandom_time_point_seed.h>
13+
814
#include <algorithm>
915
#include <cstddef>
1016
#include <cstdint>
@@ -14,11 +20,6 @@
1420
#include <random>
1521
#include <vector>
1622

17-
#include <examples/example_uintwide_t.h>
18-
#include <math/wide_integer/uintwide_t.h>
19-
20-
#include <util/utility/util_pseudorandom_time_point_seed.h>
21-
2223
namespace local_timed_mul
2324
{
2425
constexpr std::uint32_t wide_integer_test9_digits2 = static_cast<std::uint32_t>(1ULL << 15U);
@@ -89,33 +90,24 @@ auto ::math::wide_integer::example009_timed_mul() -> bool
8990
std::uint64_t count = 0U;
9091
std::size_t index = 0U;
9192

92-
float total_time { };
93+
using stopwatch_type = concurrency::stopwatch;
9394

94-
const auto begin = std::clock();
95+
stopwatch_type my_stopwatch { };
9596

96-
for(;;)
97+
while(stopwatch_type::elapsed_time<float>(my_stopwatch) < static_cast<float>(6.0L))
9798
{
9899
local_timed_mul::local_a().at(index) * local_timed_mul::local_b().at(index);
99100

100-
const auto end = std::clock();
101-
102-
total_time = static_cast<float>(static_cast<float>(end - begin) / CLOCKS_PER_SEC);
103-
104101
++count;
105102
++index;
106103

107104
if(index >= local_timed_mul::local_a().size())
108105
{
109106
index = 0U;
110107
}
111-
112-
if(total_time > static_cast<float>(6.0L)) // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
113-
{
114-
break;
115-
}
116108
}
117109

118-
const float kops_per_sec = static_cast<float>(count) / static_cast<float>(static_cast<float>(total_time * 1000.0F));
110+
const float kops_per_sec = static_cast<float>(count) / static_cast<float>(static_cast<float>(stopwatch_type::elapsed_time<float>(my_stopwatch) * 1000.0F));
119111

120112
{
121113
const auto flg = std::cout.flags();

examples/example009a_timed_mul_4_by_4.cpp

+10-18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
// or copy at http://www.boost.org/LICENSE_1_0.txt) //
66
///////////////////////////////////////////////////////////////////
77

8+
#include <examples/example_uintwide_t.h>
9+
#include <math/wide_integer/uintwide_t.h>
10+
#include <test/stopwatch.h>
11+
12+
#include <util/utility/util_pseudorandom_time_point_seed.h>
13+
814
#include <algorithm>
915
#include <cstddef>
1016
#include <cstdint>
@@ -14,11 +20,6 @@
1420
#include <random>
1521
#include <vector>
1622

17-
#include <examples/example_uintwide_t.h>
18-
#include <math/wide_integer/uintwide_t.h>
19-
20-
#include <util/utility/util_pseudorandom_time_point_seed.h>
21-
2223
namespace local_timed_mul_4_by_4
2324
{
2425
template<typename UnsignedIntegralIteratorType,
@@ -87,36 +88,27 @@ auto ::math::wide_integer::example009a_timed_mul_4_by_4() -> bool
8788
std::uint64_t count = 0U;
8889
std::size_t index = 0U;
8990

90-
float total_time { };
91+
using stopwatch_type = concurrency::stopwatch;
9192

92-
const auto begin = std::clock();
93+
stopwatch_type my_stopwatch { };
9394

94-
for(;;)
95+
while(stopwatch_type::elapsed_time<float>(my_stopwatch) < static_cast<float>(6.0L))
9596
{
9697
local_timed_mul_4_by_4::local_a().at(index + 0U) * local_timed_mul_4_by_4::local_b().at(index + 0U);
9798
local_timed_mul_4_by_4::local_a().at(index + 1U) * local_timed_mul_4_by_4::local_b().at(index + 1U);
9899
local_timed_mul_4_by_4::local_a().at(index + 2U) * local_timed_mul_4_by_4::local_b().at(index + 2U);
99100
local_timed_mul_4_by_4::local_a().at(index + 3U) * local_timed_mul_4_by_4::local_b().at(index + 3U);
100101

101-
const auto end = std::clock();
102-
103-
total_time = static_cast<float>(static_cast<float>(end - begin) / CLOCKS_PER_SEC);
104-
105102
count += 4U;
106103
index += 4U;
107104

108105
if(index >= local_timed_mul_4_by_4::local_a().size())
109106
{
110107
index = 0U;
111108
}
112-
113-
if(total_time > static_cast<float>(6.0L)) // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
114-
{
115-
break;
116-
}
117109
}
118110

119-
const float kops_per_sec = static_cast<float>(count) / static_cast<float>(static_cast<float>(total_time * 1000.0F));
111+
const float kops_per_sec = static_cast<float>(count) / static_cast<float>(static_cast<float>(stopwatch_type::elapsed_time<float>(my_stopwatch) * 1000.0F));
120112

121113
{
122114
const auto flg = std::cout.flags();

examples/example009b_timed_mul_8_by_8.cpp

+10-18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
// or copy at http://www.boost.org/LICENSE_1_0.txt) //
66
///////////////////////////////////////////////////////////////////
77

8+
#include <examples/example_uintwide_t.h>
9+
#include <math/wide_integer/uintwide_t.h>
10+
#include <test/stopwatch.h>
11+
12+
#include <util/utility/util_pseudorandom_time_point_seed.h>
13+
814
#include <algorithm>
915
#include <cstddef>
1016
#include <cstdint>
@@ -14,11 +20,6 @@
1420
#include <random>
1521
#include <vector>
1622

17-
#include <examples/example_uintwide_t.h>
18-
#include <math/wide_integer/uintwide_t.h>
19-
20-
#include <util/utility/util_pseudorandom_time_point_seed.h>
21-
2223
namespace local_timed_mul_8_by_8
2324
{
2425
template<typename UnsignedIntegralIteratorType,
@@ -87,36 +88,27 @@ auto ::math::wide_integer::example009b_timed_mul_8_by_8() -> bool
8788
std::uint64_t count = 0U;
8889
std::size_t index = 0U;
8990

90-
float total_time { };
91+
using stopwatch_type = concurrency::stopwatch;
9192

92-
const auto begin = std::clock();
93+
stopwatch_type my_stopwatch { };
9394

94-
for(;;)
95+
while(stopwatch_type::elapsed_time<float>(my_stopwatch) < static_cast<float>(6.0L))
9596
{
9697
local_timed_mul_8_by_8::local_a().at(index + 0U) * local_timed_mul_8_by_8::local_b().at(index + 0U);
9798
local_timed_mul_8_by_8::local_a().at(index + 1U) * local_timed_mul_8_by_8::local_b().at(index + 1U);
9899
local_timed_mul_8_by_8::local_a().at(index + 2U) * local_timed_mul_8_by_8::local_b().at(index + 2U);
99100
local_timed_mul_8_by_8::local_a().at(index + 3U) * local_timed_mul_8_by_8::local_b().at(index + 3U);
100101

101-
const auto end = std::clock();
102-
103-
total_time = static_cast<float>(static_cast<float>(end - begin) / CLOCKS_PER_SEC);
104-
105102
count += 4U;
106103
index += 4U;
107104

108105
if(index >= local_timed_mul_8_by_8::local_a().size())
109106
{
110107
index = 0U;
111108
}
112-
113-
if(total_time > static_cast<float>(6.0L)) // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
114-
{
115-
break;
116-
}
117109
}
118110

119-
const float kops_per_sec = static_cast<float>(count) / static_cast<float>(static_cast<float>(total_time * 1000.0F));
111+
const float kops_per_sec = static_cast<float>(count) / static_cast<float>(static_cast<float>(stopwatch_type::elapsed_time<float>(my_stopwatch) * 1000.0F));
120112

121113
{
122114
const auto flg = std::cout.flags();

examples/example013_ecdsa_sign_verify.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,8 @@ namespace example013_ecdsa
605605
}
606606

607607
template<typename UnknownWideUintType>
608-
static auto get_pseudo_random_uint() -> UnknownWideUintType
608+
static auto get_pseudo_random_uint(const UnknownWideUintType& a = (std::numeric_limits<UnknownWideUintType>::min)(),
609+
const UnknownWideUintType& b = (std::numeric_limits<UnknownWideUintType>::max)()) -> UnknownWideUintType
609610
{
610611
using local_wide_unsigned_integer_type = UnknownWideUintType;
611612

@@ -624,7 +625,7 @@ namespace example013_ecdsa
624625

625626
local_random_engine_type generator(seed_value);
626627

627-
local_distribution_type dist;
628+
local_distribution_type dist { a, b };
628629

629630
const auto unsigned_pseudo_random_value = dist(generator);
630631

math/wide_integer/uintwide_t.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -6757,9 +6757,11 @@
67576757
struct param_type
67586758
{
67596759
public:
6760+
constexpr param_type() { }
6761+
67606762
explicit constexpr param_type(const result_type& p_a, const result_type& p_b) // NOLINT(modernize-pass-by-value)
6761-
: param_a(p_a),
6762-
param_b(p_b) { }
6763+
: param_a { p_a },
6764+
param_b { p_b } { }
67636765

67646766
constexpr param_type(const param_type& other) : param_a(other.param_a),
67656767
param_b(other.param_b) { }

test/stopwatch.h

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
///////////////////////////////////////////////////////////////////////////////
2+
// Copyright Christopher Kormanyos 2013 - 2024.
3+
// Distributed under the Boost Software License,
4+
// Version 1.0. (See accompanying file LICENSE_1_0.txt
5+
// or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
8+
#ifndef STOPWATCH_2024_03_28_H // NOLINT(llvm-header-guard)
9+
#define STOPWATCH_2024_03_28_H
10+
11+
#include <cstdint>
12+
#include <ctime>
13+
14+
#if defined(_MSC_VER) && !defined(__GNUC__)
15+
#define STOPWATCH_NODISCARD
16+
#else
17+
#if (defined(__cplusplus) && (__cplusplus >= 201703L))
18+
#define STOPWATCH_NODISCARD [[nodiscard]] // NOLINT(cppcoreguidelines-macro-usage)
19+
#else
20+
#define STOPWATCH_NODISCARD
21+
#endif
22+
#endif
23+
24+
// See also: https://godbolt.org/z/37a4n9f4Y
25+
26+
namespace concurrency {
27+
28+
struct stopwatch
29+
{
30+
public:
31+
using time_point_type = std::uintmax_t;
32+
33+
stopwatch()
34+
{
35+
reset();
36+
}
37+
38+
auto reset() -> void
39+
{
40+
timespec ts { };
41+
42+
timespec_get(&ts, TIME_UTC);
43+
44+
m_start =
45+
static_cast<time_point_type>
46+
(
47+
static_cast<time_point_type>(static_cast<time_point_type>(ts.tv_sec) * UINTMAX_C(1000000000))
48+
+ static_cast<time_point_type>(ts.tv_nsec)
49+
);
50+
}
51+
52+
template<typename RepresentationRequestedTimeType>
53+
static auto elapsed_time(const stopwatch& my_stopwatch) noexcept -> RepresentationRequestedTimeType
54+
{
55+
using local_time_type = RepresentationRequestedTimeType;
56+
57+
return
58+
local_time_type
59+
{
60+
static_cast<local_time_type>(my_stopwatch.elapsed())
61+
/ local_time_type { UINTMAX_C(1000000000) }
62+
};
63+
}
64+
65+
private:
66+
time_point_type m_start { }; // NOLINT(readability-identifier-naming)
67+
68+
STOPWATCH_NODISCARD auto elapsed() const -> time_point_type
69+
{
70+
timespec ts { };
71+
72+
timespec_get(&ts, TIME_UTC);
73+
74+
time_point_type
75+
stop
76+
{
77+
static_cast<time_point_type>
78+
(
79+
static_cast<time_point_type>(static_cast<time_point_type>(ts.tv_sec) * UINTMAX_C(1000000000))
80+
+ static_cast<time_point_type>(ts.tv_nsec)
81+
)
82+
};
83+
84+
const std::uintmax_t
85+
elapsed_ns
86+
{
87+
static_cast<time_point_type>
88+
(
89+
stop - m_start
90+
)
91+
};
92+
93+
return elapsed_ns;
94+
}
95+
};
96+
97+
} // namespace concurrency
98+
99+
#endif // STOPWATCH_2024_03_28_H

0 commit comments

Comments
 (0)