Skip to content

Commit a895b5f

Browse files
authored
Merge pull request #704 from ckormanyos/examples_tuning
Examples tuning
2 parents 9df3ec8 + 7d93cec commit a895b5f

File tree

111 files changed

+532
-517
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+532
-517
lines changed

code_snippets/chapter03/chapter03_22-001_mersenne_twister_19937.cpp

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,79 @@
77

88
// chapter03_22-001_mersenne_twister_19937.cpp
99

10+
// See also: https://godbolt.org/z/azW71ahfK
11+
1012
#include <cstdint>
11-
#include <ctime>
1213
#include <iomanip>
1314
#include <iostream>
1415
#include <random>
16+
#include <sstream>
1517

18+
template<typename EngineType>
1619
auto do_something() -> void
1720
{
18-
using local_result_type = typename std::random_device::result_type;
21+
using local_result_type = typename EngineType::result_type;
22+
23+
using dist_type = std::uniform_int_distribution<local_result_type>;
24+
25+
std::random_device dev { };
1926

20-
std::random_device device;
21-
const local_result_type seed(device());
22-
std::mt19937 gtor(seed);
23-
std::uniform_int_distribution<local_result_type> distribution
24-
{
25-
static_cast<local_result_type>(UINT16_C(1)),
26-
static_cast<local_result_type>(UINT16_C(1023))
27-
};
27+
const local_result_type seed(dev());
28+
EngineType eng { seed };
29+
30+
dist_type
31+
distribution
32+
{
33+
static_cast<local_result_type>(UINT16_C(1)),
34+
static_cast<local_result_type>(UINT16_C(1023))
35+
};
36+
37+
std::stringstream strm { };
2838

2939
// Print the seed.
30-
std::cout << "Seed is: 0x"
31-
<< std::hex
32-
<< std::setw(8)
33-
<< std::setfill('0')
34-
<< std::uppercase
35-
<< seed
36-
<< ". ";
40+
strm << "Seed is: 0x"
41+
<< std::hex
42+
<< std::setw(8)
43+
<< std::setfill('0')
44+
<< std::uppercase
45+
<< seed
46+
<< ". ";
3747

3848
// Generate 3 pseudo-random numbers in [1, 1023].
39-
const unsigned random_numbers[3U] =
49+
const unsigned random_numbers[std::size_t { UINT8_C(3) }] =
4050
{
41-
distribution(gtor),
42-
distribution(gtor),
43-
distribution(gtor)
51+
static_cast<unsigned>(distribution(eng)),
52+
static_cast<unsigned>(distribution(eng)),
53+
static_cast<unsigned>(distribution(eng))
4454
};
4555

46-
std::cout << "Random numbers in [1, 1023]: ";
47-
std::cout << std::dec << std::setw(5) << std::setfill(char(' ')) << random_numbers[0U] << ", ";
48-
std::cout << std::dec << std::setw(5) << std::setfill(char(' ')) << random_numbers[1U] << ", ";
49-
std::cout << std::dec << std::setw(5) << std::setfill(char(' ')) << random_numbers[2U] << std::endl;
56+
strm << "Random numbers in [1, 1023]: ";
57+
strm << std::dec << std::setw(5) << std::setfill(char(' ')) << random_numbers[0U] << ", ";
58+
strm << std::dec << std::setw(5) << std::setfill(char(' ')) << random_numbers[1U] << ", ";
59+
strm << std::dec << std::setw(5) << std::setfill(char(' ')) << random_numbers[2U];
60+
61+
std::cout << strm.str() << std::endl;
5062
}
5163

5264
auto main() -> int
5365
{
54-
// Generate 20 sequences of 3 pseudo-random numbers.
55-
for(std::uint_fast8_t i = 0U; i < 20U; ++i)
66+
// Generate 8 sequences of 3 pseudo-random numbers.
67+
for(std::uint_fast8_t i = { UINT8_C(0) }; i < std::uint_fast8_t { UINT8_C(8) }; ++i)
5668
{
57-
do_something();
69+
// For std::mt19937.
70+
using eng32_type = std::mt19937;
71+
72+
do_something<eng32_type>();
73+
}
74+
75+
std::cout << std::endl;
76+
77+
// Generate 8 sequences of 3 pseudo-random numbers.
78+
for(std::uint_fast8_t i = { UINT8_C(0) }; i < std::uint_fast8_t { UINT8_C(8) }; ++i)
79+
{
80+
// For std::mt19937_64.
81+
using eng64_type = std::mt19937_64;
82+
83+
do_something<eng64_type>();
5884
}
5985
}

examples/chapter09_08a/build.sh

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
#
2424
# Usage example A (from *nix shell)
2525
# cd /usr/local/real-time-cpp/examples/chapter09_08a
26-
# ./build.sh /usr/local/real-time-cpp/examples/chapter09_08a/tools/Util/MinGW/msys/1.0/local/gcc-9.2.0-avr/bin avr
26+
# ./build.sh /usr/local/real-time-cpp/examples/chapter09_08a/tools/Util/msys64/usr/local/gcc-15.1.0-avr/bin avr
2727

2828
# Usage example B (from Win* shell such as in Git for Win*)
2929
# cd C:/Users/ckorm/Documents/Ks/uC_Software/Boards/real-time-cpp/examples/chapter09_08a
30-
# ./build.sh C:/Users/User/Documents/Ks/uC_Software/Boards/real-time-cpp/examples/chapter09_08a/tools/Util/MinGW/msys/1.0/local/gcc-9.2.0-avr/bin avr
30+
# ./build.sh C:/Users/ckorm/Documents/Ks/uC_Software/Boards/real-time-cpp/examples/chapter09_08a/tools/Util/msys64/usr/local/gcc-15.1.0-avr/bin avr
3131

3232
if [[ $# == 0 ]]; then ## $# is the number of arguments
3333
if [[ -n "$(which avr-g++)" ]]; then ## -n tests if string is not empty
@@ -47,9 +47,9 @@ else
4747
TOOL_PREFIX="$2"
4848
fi
4949

50-
CFLAGS="-Wall -Wextra -pedantic -mmcu=atmega328p -fsigned-char -O2 -fno-exceptions -gdwarf-2 -ffunction-sections -fdata-sections"
50+
CFLAGS="-Wall -Wextra -Wpedantic -O2 -mmcu=atmega328p -fsigned-char -finline-functions -finline-limit=64 -fno-exceptions -gdwarf-2 -ffunction-sections -fdata-sections"
5151
CPPFLAGS="-std=c++14 -fno-rtti -fstrict-enums -fno-use-cxa-atexit -fno-use-cxa-get-exception-ptr -fno-nonansi-builtins -fno-threadsafe-statics -fno-enforce-eh-specs"
52-
CINCLUDES="-Isrc/util/STL_C++XX_stdfloat -Isrc/util/STL -Isrc -Isrc/mcal/avr"
52+
CINCLUDES="-Isrc/util/STL_C++XX_stdfloat -Isrc/util/STL -Isrc/mcal/avr -Isrc"
5353

5454
echo
5555
echo "Building with : build.sh"
@@ -76,6 +76,12 @@ $TOOL_PATH/$TOOL_PREFIX-g++ -x c++ $CFLAGS $CPPFLAGS $CINCLUDES -c src/mcal/avr/
7676
echo "Compile : mcal_eep.cpp to bin/mcal_eep.o"
7777
$TOOL_PATH/$TOOL_PREFIX-g++ -x c++ $CFLAGS $CPPFLAGS $CINCLUDES -c src/mcal/avr/mcal_eep.cpp -o bin/mcal_eep.o
7878

79+
echo "Compile : mcal_gpt.cpp to bin/mcal_gpt.o"
80+
$TOOL_PATH/$TOOL_PREFIX-g++ -x c++ $CFLAGS $CPPFLAGS $CINCLUDES -c src/mcal/avr/mcal_gpt.cpp -o bin/mcal_gpt.o
81+
82+
echo "Compile : mcal_irq.cpp to bin/mcal_irq.o"
83+
$TOOL_PATH/$TOOL_PREFIX-g++ -x c++ $CFLAGS $CPPFLAGS $CINCLUDES -c src/mcal/avr/mcal_irq.cpp -o bin/mcal_irq.o
84+
7985
echo "Compile : mcal_led.cpp to bin/mcal_led.o"
8086
$TOOL_PATH/$TOOL_PREFIX-g++ -x c++ $CFLAGS $CPPFLAGS $CINCLUDES -c src/mcal/avr/mcal_led.cpp -o bin/mcal_led.o
8187

@@ -85,12 +91,6 @@ $TOOL_PATH/$TOOL_PREFIX-g++ -x c++ $CFLAGS $CPPFLAGS $CINCLUDES -c src/mcal/avr/
8591
echo "Compile : mcal_led_sys_start_interface.cpp to bin/mcal_led_sys_start_interface.o"
8692
$TOOL_PATH/$TOOL_PREFIX-g++ -x c++ $CFLAGS $CPPFLAGS $CINCLUDES -c src/mcal/avr/mcal_led_sys_start_interface.cpp -o bin/mcal_led_sys_start_interface.o
8793

88-
echo "Compile : mcal_gpt.cpp to bin/mcal_gpt.o"
89-
$TOOL_PATH/$TOOL_PREFIX-g++ -x c++ $CFLAGS $CPPFLAGS $CINCLUDES -c src/mcal/avr/mcal_gpt.cpp -o bin/mcal_gpt.o
90-
91-
echo "Compile : mcal_irq.cpp to bin/mcal_irq.o"
92-
$TOOL_PATH/$TOOL_PREFIX-g++ -x c++ $CFLAGS $CPPFLAGS $CINCLUDES -c src/mcal/avr/mcal_irq.cpp -o bin/mcal_irq.o
93-
9494
echo "Compile : mcal_osc.cpp to bin/mcal_osc.o"
9595
$TOOL_PATH/$TOOL_PREFIX-g++ -x c++ $CFLAGS $CPPFLAGS $CINCLUDES -c src/mcal/avr/mcal_osc.cpp -o bin/mcal_osc.o
9696

examples/chapter09_08a/src/app/led/app_led.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ auto app::led::task_func() -> void
9898
{
9999
const auto delta_bump_point = std::abs(static_cast<int>(rgb_hue_sum) - static_cast<int>(INT16_C(255)));
100100

101-
if (delta_bump_point == static_cast<int>(INT8_C(0))) { color_persist_time = static_cast<unsigned>(UINT8_C(600)); }
102-
else if(delta_bump_point == static_cast<int>(INT8_C(1))) { color_persist_time = static_cast<unsigned>(UINT8_C(500)); }
103-
else if(delta_bump_point == static_cast<int>(INT8_C(2))) { color_persist_time = static_cast<unsigned>(UINT8_C(400)); }
104-
else if(delta_bump_point == static_cast<int>(INT8_C(3))) { color_persist_time = static_cast<unsigned>(UINT8_C(300)); }
105-
else if(delta_bump_point == static_cast<int>(INT8_C(4))) { color_persist_time = static_cast<unsigned>(UINT8_C(200)); }
106-
else { color_persist_time = static_cast<unsigned>(UINT8_C(100)); }
101+
if (delta_bump_point == static_cast<int>(INT8_C(0))) { color_persist_time = static_cast<unsigned>(UINT16_C(600)); }
102+
else if(delta_bump_point == static_cast<int>(INT8_C(1))) { color_persist_time = static_cast<unsigned>(UINT16_C(500)); }
103+
else if(delta_bump_point == static_cast<int>(INT8_C(2))) { color_persist_time = static_cast<unsigned>(UINT16_C(400)); }
104+
else if(delta_bump_point == static_cast<int>(INT8_C(3))) { color_persist_time = static_cast<unsigned>(UINT16_C(300)); }
105+
else if(delta_bump_point == static_cast<int>(INT8_C(4))) { color_persist_time = static_cast<unsigned>(UINT16_C(200)); }
106+
else { color_persist_time = static_cast<unsigned>(UINT16_C(100)); }
107107
}
108108

109109
// Make a smooth color transition and increment the color transition state

examples/chapter09_08a/src/mcal/avr/mcal_cpu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
inline auto post_init() -> void { }
2020

21-
inline auto nop() noexcept -> void { asm volatile("nop"); }
21+
inline auto nop() -> void { asm volatile("nop"); }
2222
}
2323
}
2424

examples/chapter09_08a/src/mcal/avr/mcal_irq.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
auto init(const config_type*) -> void;
1818

19-
inline auto enable_all () noexcept -> void { asm volatile("sei"); }
20-
inline auto disable_all() noexcept -> void { asm volatile("cli"); }
19+
inline auto enable_all () -> void { asm volatile("sei"); }
20+
inline auto disable_all() -> void { asm volatile("cli"); }
2121
}
2222
}
2323

examples/chapter09_08a/src/mcal/avr/mcal_led_rgb_ws2812.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#endif
3232
auto push_color() -> void;
3333

34-
static constexpr auto led_count() noexcept -> unsigned { return LedCount; }
34+
static constexpr auto led_count() -> unsigned { return LedCount; }
3535

3636
public:
3737
led_rgb_ws2812() { port_pin_type::set_direction_output(); }

examples/chapter09_08a/src/mcal/mcal_gcc_cxx_completion.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ void* operator new(std::size_t size)
4444
return static_cast<void*>(const_cast<std::uint8_t*>(p));
4545
}
4646

47-
void operator delete(void*) noexcept { }
47+
void operator delete(void*) { }
4848
#if (defined(__GNUC__) && (__GNUC__ >= 12))
4949
#else
50-
void operator delete(void*, void*) noexcept { }
50+
void operator delete(void*, void*) { }
5151
#endif
5252
#if(__cplusplus >= 201400L)
53-
void operator delete(void*, std::size_t) noexcept { }
53+
void operator delete(void*, std::size_t) { }
5454
#endif
5555

5656
extern "C"

examples/chapter09_08a/src/mcal/mcal_gpt_arm_sys_tick.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
const register_value_type value = static_cast<register_value_type>(UINT8_C(0))>
4242
struct reg_access_static
4343
{
44-
static auto reg_get() noexcept -> register_value_type { volatile register_value_type* pa = reinterpret_cast<register_value_type*>(address); return *pa; }
45-
static auto reg_set() noexcept -> void { volatile register_value_type* pa = reinterpret_cast<volatile register_value_type*>(address); *pa = value; }
46-
static auto reg_or () noexcept -> void { volatile register_value_type* pa = reinterpret_cast<volatile register_value_type*>(address); *pa = *pa | value; }
44+
static auto reg_get() -> register_value_type { volatile register_value_type* pa = reinterpret_cast<register_value_type*>(address); return *pa; }
45+
static auto reg_set() -> void { volatile register_value_type* pa = reinterpret_cast<volatile register_value_type*>(address); *pa = value; }
46+
static auto reg_or () -> void { volatile register_value_type* pa = reinterpret_cast<volatile register_value_type*>(address); *pa = *pa | value; }
4747
};
4848
};
4949

@@ -66,9 +66,9 @@
6666
public:
6767
using value_type = typename base_class_type::value_type;
6868

69-
static constexpr auto sys_tick_mhz() noexcept -> std::uint32_t { return SysTickMHz; }
69+
static constexpr auto sys_tick_mhz() -> std::uint32_t { return SysTickMHz; }
7070

71-
static auto init() noexcept -> void
71+
static auto init() -> void
7272
{
7373
if(!my_is_init)
7474
{
@@ -95,7 +95,7 @@
9595
}
9696
}
9797

98-
static auto get_time_elapsed() noexcept -> value_type
98+
static auto get_time_elapsed() -> value_type
9999
{
100100
return
101101
static_cast<value_type>
@@ -108,7 +108,7 @@
108108
static volatile value_type my_sys_tick_value;
109109
static bool my_is_init;
110110

111-
static auto get_consistent_microsecond_tick() noexcept -> value_type
111+
static auto get_consistent_microsecond_tick() -> value_type
112112
{
113113
// Return the system tick using a multiple read to ensure data consistency.
114114

examples/chapter09_08a/src/mcal/mcal_helper.h

Lines changed: 5 additions & 5 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)
@@ -46,25 +46,25 @@
4646

4747
template<const bool has_disable_enable_interrupts>
4848
auto disable_all_interrupts(const bool = has_disable_enable_interrupts,
49-
const typename std::enable_if<has_disable_enable_interrupts>::type* = nullptr) noexcept -> void
49+
const typename std::enable_if<has_disable_enable_interrupts>::type* = nullptr) -> void
5050
{
5151
mcal::irq::disable_all();
5252
}
5353

5454
template<const bool has_disable_enable_interrupts>
5555
auto enable_all_interrupts(const bool = has_disable_enable_interrupts,
56-
const typename std::enable_if<has_disable_enable_interrupts>::type* = nullptr) noexcept -> void
56+
const typename std::enable_if<has_disable_enable_interrupts>::type* = nullptr) -> void
5757
{
5858
mcal::irq::enable_all();
5959
}
6060

6161
template<const bool has_disable_enable_interrupts>
6262
auto disable_all_interrupts(const bool = has_disable_enable_interrupts,
63-
const typename std::enable_if<(!has_disable_enable_interrupts)>::type* = nullptr) noexcept -> void { }
63+
const typename std::enable_if<(!has_disable_enable_interrupts)>::type* = nullptr) -> void { }
6464

6565
template<const bool has_disable_enable_interrupts>
6666
auto enable_all_interrupts(const bool = has_disable_enable_interrupts,
67-
const typename std::enable_if<(!has_disable_enable_interrupts)>::type* = nullptr) noexcept -> void { }
67+
const typename std::enable_if<(!has_disable_enable_interrupts)>::type* = nullptr) -> void { }
6868

6969
} } // namespace mcal::helper
7070

examples/chapter09_08a/src/mcal/mcal_reg_access_static.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
///////////////////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 2007 - 2024.
2+
// Copyright Christopher Kormanyos 2007 - 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)
@@ -26,28 +26,28 @@
2626
using register_value_type = RegisterValueType;
2727
using register_address_type = RegisterAddressType;
2828

29-
static auto reg_get() -> register_value_type { volatile register_value_type* pa = reinterpret_cast<register_value_type*>(address); return *pa; }
30-
static auto reg_set() -> void { volatile register_value_type* pa = reinterpret_cast<volatile register_value_type*>(address); *pa = value; }
31-
static auto reg_and() -> void { volatile register_value_type* pa = reinterpret_cast<volatile register_value_type*>(address); *pa = static_cast<register_value_type>(*pa & value); }
32-
static auto reg_or () -> void { volatile register_value_type* pa = reinterpret_cast<volatile register_value_type*>(address); *pa = static_cast<register_value_type>(*pa | value); }
33-
static auto reg_not() -> void { volatile register_value_type* pa = reinterpret_cast<volatile register_value_type*>(address); *pa = static_cast<register_value_type>(*pa & static_cast<register_value_type>(~value)); }
29+
static auto reg_get() -> register_value_type { volatile register_value_type* p_addr = reinterpret_cast<register_value_type*>(address); return *p_addr; }
30+
static auto reg_set() -> void { volatile register_value_type* p_addr = reinterpret_cast<volatile register_value_type*>(address); *p_addr = value; }
31+
static auto reg_and() -> void { volatile register_value_type* p_addr = reinterpret_cast<volatile register_value_type*>(address); *p_addr = static_cast<register_value_type>(*p_addr & value); }
32+
static auto reg_or () -> void { volatile register_value_type* p_addr = reinterpret_cast<volatile register_value_type*>(address); *p_addr = static_cast<register_value_type>(*p_addr | value); }
33+
static auto reg_not() -> void { volatile register_value_type* p_addr = reinterpret_cast<volatile register_value_type*>(address); *p_addr = static_cast<register_value_type>(*p_addr & static_cast<register_value_type>(~value)); }
3434

3535
template<const register_value_type mask_value>
3636
static auto reg_msk() -> void
3737
{
38-
volatile register_value_type* pa = reinterpret_cast<volatile register_value_type*>(address);
38+
volatile register_value_type* p_addr = reinterpret_cast<volatile register_value_type*>(address);
3939

40-
*pa =
40+
*p_addr =
4141
static_cast<register_value_type>
4242
(
4343
static_cast<register_value_type>(reg_get() & static_cast<register_value_type>(~mask_value))
4444
| value
4545
);
4646
}
4747

48-
static auto bit_set() -> void { volatile register_value_type* pa = reinterpret_cast<volatile register_value_type*>(address); *pa = static_cast<register_value_type>(*pa | static_cast<register_value_type>(1ULL << value)); }
49-
static auto bit_clr() -> void { volatile register_value_type* pa = reinterpret_cast<volatile register_value_type*>(address); *pa = static_cast<register_value_type>(*pa & static_cast<register_value_type>(~static_cast<register_value_type>(1ULL << value))); }
50-
static auto bit_not() -> void { volatile register_value_type* pa = reinterpret_cast<volatile register_value_type*>(address); *pa = static_cast<register_value_type>(*pa ^ static_cast<register_value_type>(1ULL << value)); }
48+
static auto bit_set() -> void { volatile register_value_type* p_addr = reinterpret_cast<volatile register_value_type*>(address); *p_addr = static_cast<register_value_type>(*p_addr | static_cast<register_value_type>(1ULL << value)); }
49+
static auto bit_clr() -> void { volatile register_value_type* p_addr = reinterpret_cast<volatile register_value_type*>(address); *p_addr = static_cast<register_value_type>(*p_addr & static_cast<register_value_type>(~static_cast<register_value_type>(1ULL << value))); }
50+
static auto bit_not() -> void { volatile register_value_type* p_addr = reinterpret_cast<volatile register_value_type*>(address); *p_addr = static_cast<register_value_type>(*p_addr ^ static_cast<register_value_type>(1ULL << value)); }
5151
static auto bit_get() -> bool { return (static_cast<register_value_type>(reg_get() & static_cast<register_value_type>(1ULL << value)) != static_cast<register_value_type>(0U)); }
5252
};
5353
}

0 commit comments

Comments
 (0)