Skip to content

Commit

Permalink
Merge pull request #436 from ckormanyos/update_from_embedded
Browse files Browse the repository at this point in the history
Update from embedded
  • Loading branch information
ckormanyos authored Oct 14, 2024
2 parents 9daeb53 + 61dc0f1 commit 62bc751
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .gcov/make/make_gcov_01_generic.gmk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# ------------------------------------------------------------------------------
# Copyright Christopher Kormanyos 2022.
# Copyright Christopher Kormanyos 2022 - 2024.
# Distributed under the Boost Software License,
# Version 1.0. (See accompanying file LICENSE_1_0.txt
# or copy at http://www.boost.org/LICENSE_1_0.txt)
Expand Down
4 changes: 2 additions & 2 deletions .gcov/make/make_gcov_03_flags.gmk
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# ------------------------------------------------------------------------------
# Copyright Christopher Kormanyos 2022.
# Copyright Christopher Kormanyos 2022 - 2024.
# Distributed under the Boost Software License,
# Version 1.0. (See accompanying file LICENSE_1_0.txt
# or copy at http://www.boost.org/LICENSE_1_0.txt)
# ------------------------------------------------------------------------------

BOOST_ROOT_FOR_GCOV = /mnt/c/boost/boost_1_78_0
BOOST_ROOT_FOR_GCOV = /mnt/c/boost/boost_1_85_0
CC = g++
STD = c++14
ALL_COV = 0
Expand Down
2 changes: 1 addition & 1 deletion examples/example007_random_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace local_random
// Purosely use the default seed.
random_engine_type generator; // NOLINT(cert-msc32-c,cert-msc51-cpp)

distribution_type distribution;
distribution_type distribution { };

const wide_integer_type n = distribution(generator);

Expand Down
28 changes: 10 additions & 18 deletions examples/example009_timed_mul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
// or copy at http://www.boost.org/LICENSE_1_0.txt) //
///////////////////////////////////////////////////////////////////

#include <examples/example_uintwide_t.h>
#include <math/wide_integer/uintwide_t.h>
#include <test/stopwatch.h>

#include <util/utility/util_pseudorandom_time_point_seed.h>

#include <algorithm>
#include <cstddef>
#include <cstdint>
Expand All @@ -14,11 +20,6 @@
#include <random>
#include <vector>

#include <examples/example_uintwide_t.h>
#include <math/wide_integer/uintwide_t.h>

#include <util/utility/util_pseudorandom_time_point_seed.h>

namespace local_timed_mul
{
constexpr std::uint32_t wide_integer_test9_digits2 = static_cast<std::uint32_t>(1ULL << 15U);
Expand Down Expand Up @@ -89,33 +90,24 @@ auto ::math::wide_integer::example009_timed_mul() -> bool
std::uint64_t count = 0U;
std::size_t index = 0U;

float total_time { };
using stopwatch_type = concurrency::stopwatch;

const auto begin = std::clock();
stopwatch_type my_stopwatch { };

for(;;)
while(stopwatch_type::elapsed_time<float>(my_stopwatch) < static_cast<float>(6.0L)) // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
{
local_timed_mul::local_a().at(index) * local_timed_mul::local_b().at(index);

const auto end = std::clock();

total_time = static_cast<float>(static_cast<float>(end - begin) / CLOCKS_PER_SEC);

++count;
++index;

if(index >= local_timed_mul::local_a().size())
{
index = 0U;
}

if(total_time > static_cast<float>(6.0L)) // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
{
break;
}
}

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

{
const auto flg = std::cout.flags();
Expand Down
28 changes: 10 additions & 18 deletions examples/example009a_timed_mul_4_by_4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
// or copy at http://www.boost.org/LICENSE_1_0.txt) //
///////////////////////////////////////////////////////////////////

#include <examples/example_uintwide_t.h>
#include <math/wide_integer/uintwide_t.h>
#include <test/stopwatch.h>

#include <util/utility/util_pseudorandom_time_point_seed.h>

#include <algorithm>
#include <cstddef>
#include <cstdint>
Expand All @@ -14,11 +20,6 @@
#include <random>
#include <vector>

#include <examples/example_uintwide_t.h>
#include <math/wide_integer/uintwide_t.h>

#include <util/utility/util_pseudorandom_time_point_seed.h>

namespace local_timed_mul_4_by_4
{
template<typename UnsignedIntegralIteratorType,
Expand Down Expand Up @@ -87,36 +88,27 @@ auto ::math::wide_integer::example009a_timed_mul_4_by_4() -> bool
std::uint64_t count = 0U;
std::size_t index = 0U;

float total_time { };
using stopwatch_type = concurrency::stopwatch;

const auto begin = std::clock();
stopwatch_type my_stopwatch { };

for(;;)
while(stopwatch_type::elapsed_time<float>(my_stopwatch) < static_cast<float>(6.0L)) // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
{
local_timed_mul_4_by_4::local_a().at(index + 0U) * local_timed_mul_4_by_4::local_b().at(index + 0U);
local_timed_mul_4_by_4::local_a().at(index + 1U) * local_timed_mul_4_by_4::local_b().at(index + 1U);
local_timed_mul_4_by_4::local_a().at(index + 2U) * local_timed_mul_4_by_4::local_b().at(index + 2U);
local_timed_mul_4_by_4::local_a().at(index + 3U) * local_timed_mul_4_by_4::local_b().at(index + 3U);

const auto end = std::clock();

total_time = static_cast<float>(static_cast<float>(end - begin) / CLOCKS_PER_SEC);

count += 4U;
index += 4U;

if(index >= local_timed_mul_4_by_4::local_a().size())
{
index = 0U;
}

if(total_time > static_cast<float>(6.0L)) // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
{
break;
}
}

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

{
const auto flg = std::cout.flags();
Expand Down
28 changes: 10 additions & 18 deletions examples/example009b_timed_mul_8_by_8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
// or copy at http://www.boost.org/LICENSE_1_0.txt) //
///////////////////////////////////////////////////////////////////

#include <examples/example_uintwide_t.h>
#include <math/wide_integer/uintwide_t.h>
#include <test/stopwatch.h>

#include <util/utility/util_pseudorandom_time_point_seed.h>

#include <algorithm>
#include <cstddef>
#include <cstdint>
Expand All @@ -14,11 +20,6 @@
#include <random>
#include <vector>

#include <examples/example_uintwide_t.h>
#include <math/wide_integer/uintwide_t.h>

#include <util/utility/util_pseudorandom_time_point_seed.h>

namespace local_timed_mul_8_by_8
{
template<typename UnsignedIntegralIteratorType,
Expand Down Expand Up @@ -87,36 +88,27 @@ auto ::math::wide_integer::example009b_timed_mul_8_by_8() -> bool
std::uint64_t count = 0U;
std::size_t index = 0U;

float total_time { };
using stopwatch_type = concurrency::stopwatch;

const auto begin = std::clock();
stopwatch_type my_stopwatch { };

for(;;)
while(stopwatch_type::elapsed_time<float>(my_stopwatch) < static_cast<float>(6.0L)) // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
{
local_timed_mul_8_by_8::local_a().at(index + 0U) * local_timed_mul_8_by_8::local_b().at(index + 0U);
local_timed_mul_8_by_8::local_a().at(index + 1U) * local_timed_mul_8_by_8::local_b().at(index + 1U);
local_timed_mul_8_by_8::local_a().at(index + 2U) * local_timed_mul_8_by_8::local_b().at(index + 2U);
local_timed_mul_8_by_8::local_a().at(index + 3U) * local_timed_mul_8_by_8::local_b().at(index + 3U);

const auto end = std::clock();

total_time = static_cast<float>(static_cast<float>(end - begin) / CLOCKS_PER_SEC);

count += 4U;
index += 4U;

if(index >= local_timed_mul_8_by_8::local_a().size())
{
index = 0U;
}

if(total_time > static_cast<float>(6.0L)) // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
{
break;
}
}

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

{
const auto flg = std::cout.flags();
Expand Down
16 changes: 9 additions & 7 deletions examples/example013_ecdsa_sign_verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,8 @@ namespace example013_ecdsa
}

template<typename UnknownWideUintType>
static auto get_pseudo_random_uint() -> UnknownWideUintType
static auto get_pseudo_random_uint(const UnknownWideUintType& a = (std::numeric_limits<UnknownWideUintType>::min)(),
const UnknownWideUintType& b = (std::numeric_limits<UnknownWideUintType>::max)()) -> UnknownWideUintType
{
using local_wide_unsigned_integer_type = UnknownWideUintType;

Expand All @@ -624,7 +625,7 @@ namespace example013_ecdsa

local_random_engine_type generator(seed_value);

local_distribution_type dist;
local_distribution_type dist { a, b };

const auto unsigned_pseudo_random_value = dist(generator);

Expand All @@ -636,13 +637,14 @@ namespace example013_ecdsa
// This subroutine generate a random private-public key pair.
// The input parameter p_uint_seed can, however, be used to
// provide a fixed-input value for the private key.

// TBD: Be sure to limit to random.randrange(1, curve.n).
// Also be sure to limit to random.randrange(1, curve.n).

const auto private_key =
uint_type
(
(p_uint_seed == nullptr) ? get_pseudo_random_uint<uint_type>() : *p_uint_seed
(p_uint_seed == nullptr)
? get_pseudo_random_uint<uint_type>(uint_type { static_cast<unsigned>(UINT8_C(1)) }, curve_n())
: *p_uint_seed
);

const auto public_key = scalar_mult(private_key, { curve_gx(), curve_gy() } );
Expand All @@ -651,8 +653,8 @@ namespace example013_ecdsa
{
private_key,
{
uint_type(public_key.my_x),
uint_type(public_key.my_y)
uint_type { public_key.my_x },
uint_type { public_key.my_y }
}
};
}
Expand Down
6 changes: 4 additions & 2 deletions math/wide_integer/uintwide_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -6757,9 +6757,11 @@
struct param_type
{
public:
constexpr param_type() { } // NOLINT(hicpp-use-equals-default,modernize-use-equals-default)

explicit constexpr param_type(const result_type& p_a, const result_type& p_b) // NOLINT(modernize-pass-by-value)
: param_a(p_a),
param_b(p_b) { }
: param_a { p_a },
param_b { p_b } { }

constexpr param_type(const param_type& other) : param_a(other.param_a),
param_b(other.param_b) { }
Expand Down
111 changes: 111 additions & 0 deletions test/stopwatch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2013 - 2024.
// Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef STOPWATCH_2024_03_28_H // NOLINT(llvm-header-guard)
#define STOPWATCH_2024_03_28_H

#include <cstdint>
#include <ctime>

#if defined(_MSC_VER) && !defined(__GNUC__)
#define STOPWATCH_NODISCARD
#else
#if (defined(__cplusplus) && (__cplusplus >= 201703L))
#define STOPWATCH_NODISCARD [[nodiscard]] // NOLINT(cppcoreguidelines-macro-usage)
#else
#define STOPWATCH_NODISCARD
#endif
#endif

// See also: https://godbolt.org/z/37a4n9f4Y

namespace concurrency {

struct stopwatch
{
public:
using time_point_type = std::uintmax_t;

auto reset() -> void
{
m_start = now();
}

template<typename RepresentationRequestedTimeType>
static auto elapsed_time(const stopwatch& my_stopwatch) noexcept -> RepresentationRequestedTimeType
{
using local_time_type = RepresentationRequestedTimeType;

return
local_time_type
{
static_cast<local_time_type>(my_stopwatch.elapsed())
/ local_time_type { UINTMAX_C(1000000000) }
};
}

private:
time_point_type m_start { now() }; // NOLINT(readability-identifier-naming)

STOPWATCH_NODISCARD static auto now() -> time_point_type
{
#if defined(__CYGWIN__)

return static_cast<time_point_type>(std::clock());

#else

timespec ts { };

timespec_get(&ts, TIME_UTC);

return
static_cast<time_point_type>
(
static_cast<time_point_type>(static_cast<time_point_type>(ts.tv_sec) * UINTMAX_C(1000000000))
+ static_cast<time_point_type>(ts.tv_nsec)
);

#endif
}

STOPWATCH_NODISCARD auto elapsed() const -> time_point_type
{
const time_point_type stop { now() };

#if defined(__CYGWIN__)

const time_point_type
elapsed_ns
{
static_cast<time_point_type>
(
static_cast<time_point_type>(static_cast<time_point_type>(stop - m_start) * UINTMAX_C(1000000000))
/ static_cast<time_point_type>(CLOCKS_PER_SEC)
)
};

#else

const time_point_type
elapsed_ns
{
static_cast<time_point_type>
(
stop - m_start
)
};

#endif

return elapsed_ns;
}
};

} // namespace concurrency

#endif // STOPWATCH_2024_03_28_H
Loading

0 comments on commit 62bc751

Please sign in to comment.