Skip to content

Commit 6417026

Browse files
committed
Refactor stack consumption and syntax
1 parent f462c0e commit 6417026

12 files changed

Lines changed: 54 additions & 77 deletions

File tree

examples/chapter11_07a/src/app/pi_spigot/pi_spigot.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ namespace local
6868
constexpr auto pi_spigot_input_start_address = static_cast<mcal_sram_uintptr_t>(UINT8_C(0));
6969

7070
using pi_spigot_input_container_type = mcal::memory::sram::array<std::uint32_t,
71-
pi_spigot_type::input_static_size,
71+
pi_spigot_type::get_input_static_size(),
7272
pi_spigot_input_start_address>;
7373

7474
auto pi_spigot_input() -> pi_spigot_input_container_type&;

examples/chapter11_07a/src/math/checksums/hash/hash_base.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
#ifndef HASH_BASE_2013_09_05_H
99
#define HASH_BASE_2013_09_05_H
1010

11-
#include <limits>
12-
1311
#include <math/checksums/hash/hash_detail.h>
1412
#include <math/checksums/hash/hash_stream_base.h>
1513

14+
#include <array>
15+
#include <cstddef>
16+
#include <cstdint>
17+
#include <limits>
18+
1619
namespace math { namespace checksums { namespace hash {
1720

1821
template<const std::uint16_t ResultBitCount,

examples/chapter11_07a/src/math/checksums/hash/hash_detail.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <cstddef>
1111
#include <cstdint>
12+
#include <limits>
1213

1314
namespace math { namespace checksums { namespace hash { namespace detail {
1415

examples/chapter11_07a/src/math/checksums/hash/hash_sha1.h

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010

1111
// See also: https://en.wikipedia.org/wiki/SHA-1
1212

13+
#include <math/checksums/hash/hash_base.h>
14+
1315
#include <algorithm>
14-
#include <array>
1516
#include <functional>
1617

17-
#include <math/checksums/hash/hash_base.h>
18-
1918
namespace math { namespace checksums { namespace hash {
2019

2120
class hash_sha1 : public hash_base<static_cast<std::uint16_t>(UINT8_C(160)),
@@ -31,6 +30,8 @@
3130
"Error: The message buffer size must exactly equal 64");
3231

3332
public:
33+
using transform_function_type = std::uint32_t(*)(const std::uint32_t*);
34+
3435
hash_sha1() = default;
3536

3637
hash_sha1(const hash_sha1&) = default;
@@ -53,9 +54,20 @@
5354
}
5455

5556
private:
57+
static const std::array<transform_function_type, static_cast<std::size_t>(UINT8_C(4))> transform_functions;
58+
5659
auto perform_algorithm() -> void override;
5760
};
5861

62+
const std::array<hash_sha1::transform_function_type, static_cast<std::size_t>(UINT8_C(4))>
63+
hash_sha1::transform_functions
64+
{
65+
[](const std::uint32_t* p) -> std::uint32_t { return (static_cast<std::uint32_t>( p[1U] & p[2U]) | static_cast<std::uint32_t>(static_cast<std::uint32_t>(~p[1U]) & p[3U])); },
66+
[](const std::uint32_t* p) -> std::uint32_t { return static_cast<std::uint32_t>(static_cast<std::uint32_t>(p[1U] ^ p[2U]) ^ p[3U]); },
67+
[](const std::uint32_t* p) -> std::uint32_t { return static_cast<std::uint32_t>(static_cast<std::uint32_t>(p[1U] & p[2U]) | static_cast<std::uint32_t>(p[1U] & p[3U]) | static_cast<std::uint32_t>(p[2U] & p[3U])); },
68+
[](const std::uint32_t* p) -> std::uint32_t { return (static_cast<std::uint32_t>( p[1U] ^ p[2U]) ^ p[3U]); }
69+
};
70+
5971
auto hash_sha1::perform_algorithm() -> void
6072
{
6173
// Apply the hash transformation algorithm to a full data block.
@@ -81,17 +93,7 @@
8193
transform_block.data()
8294
);
8395

84-
using transform_function_type = std::uint32_t(*)(const std::uint32_t*);
85-
86-
const std::array<transform_function_type, static_cast<std::size_t>(UINT8_C(4))> transform_functions
87-
{
88-
[](const std::uint32_t* p) -> std::uint32_t { return (static_cast<std::uint32_t>( p[1U] & p[2U]) | static_cast<std::uint32_t>(static_cast<std::uint32_t>(~p[1U]) & p[3U])); },
89-
[](const std::uint32_t* p) -> std::uint32_t { return static_cast<std::uint32_t>(static_cast<std::uint32_t>(p[1U] ^ p[2U]) ^ p[3U]); },
90-
[](const std::uint32_t* p) -> std::uint32_t { return static_cast<std::uint32_t>(static_cast<std::uint32_t>(p[1U] & p[2U]) | static_cast<std::uint32_t>(p[1U] & p[3U]) | static_cast<std::uint32_t>(p[2U] & p[3U])); },
91-
[](const std::uint32_t* p) -> std::uint32_t { return (static_cast<std::uint32_t>( p[1U] ^ p[2U]) ^ p[3U]); }
92-
};
93-
94-
auto hash_tmp = base_class_type::transform_context;
96+
context_type hash_tmp { base_class_type::transform_context };
9597

9698
auto loop_index = static_cast<std::uint8_t>(UINT8_C(0));
9799

@@ -110,7 +112,7 @@
110112
^ transform_block[static_cast<std::size_t>( loop_counter & static_cast<std::uint8_t>(UINT8_C(0x0F)))]
111113
);
112114

113-
const auto transform_block_index = static_cast<std::size_t>(loop_counter & std::uint8_t(UINT8_C(0x0F)));
115+
const auto transform_block_index { static_cast<std::size_t>(loop_counter & std::uint8_t(UINT8_C(0x0F))) };
114116

115117
transform_block[transform_block_index] = detail::circular_left_shift<static_cast<unsigned>(UINT8_C(1))>(the_dword);
116118

examples/chapter11_07a/src/math/checksums/hash/hash_sha256.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010

1111
// See also: https://en.wikipedia.org/wiki/SHA-2
1212

13+
#include <math/checksums/hash/hash_base.h>
14+
1315
#include <algorithm>
14-
#include <array>
1516
#include <functional>
1617

17-
#include <math/checksums/hash/hash_base.h>
18-
1918
namespace math { namespace checksums { namespace hash {
2019

2120
template<typename CountType>

examples/chapter11_07a/src/math/pi_spigot/pi_spigot.h

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
#include <algorithm>
1717
#include <array>
18-
#include <functional>
1918
#include <iterator>
2019
#include <type_traits>
2120

@@ -45,28 +44,15 @@
4544
class pi_spigot
4645
{
4746
public:
48-
static constexpr auto result_digit() -> std::uint32_t { return ResultDigit; }
49-
5047
using unsigned_small_type = UnsignedSmallType;
5148
using unsigned_large_type = UnsignedLargeType;
5249

5350
private:
54-
static constexpr auto loop_digit() -> std::uint32_t { return LoopDigit; }
55-
56-
static_assert(loop_digit() <= static_cast<std::uint32_t>(std::numeric_limits<unsigned_small_type>::digits10),
57-
"Error: loop_digit exceeds the number of base-10 digits in its constituent unsigned_small_type");
58-
59-
static_assert(result_digit() <= static_cast<std::uint32_t>(UINT32_C(100001)),
60-
"Error: result_digit exceeds its limit of 100,001");
61-
62-
static_assert(std::numeric_limits<unsigned_small_type>::digits * 2 == std::numeric_limits<unsigned_large_type>::digits,
63-
"Error: unsigned_large_type must be exactly twice as wide as unsigned_small_type");
51+
using callback_type = void(*)(const std::uint32_t);
6452

65-
static_assert((!std::numeric_limits<unsigned_small_type>::is_signed),
66-
"Error: unsigned_small_type must be unsigned");
53+
static constexpr auto result_digit() -> std::uint32_t { return ResultDigit; }
6754

68-
static_assert((!std::numeric_limits<unsigned_large_type>::is_signed),
69-
"Error: unsigned_large_type must be unsigned");
55+
static constexpr auto loop_digit() -> std::uint32_t { return LoopDigit; }
7056

7157
static constexpr auto input_scale(std::uint32_t x) -> std::uint32_t
7258
{
@@ -83,9 +69,7 @@
8369
}
8470

8571
public:
86-
static constexpr std::uint32_t input_static_size { input_scale(result_digit()) };
87-
88-
static constexpr auto get_input_static_size() -> std::uint32_t { return input_static_size; }
72+
static constexpr auto get_input_static_size() -> std::uint32_t { return input_scale(result_digit()); }
8973

9074
constexpr pi_spigot() = default; // LCOV_EXCL_LINE
9175

@@ -104,9 +88,6 @@
10488
return my_operation_count;
10589
}
10690

107-
using callback_type = void(*)(const std::uint32_t);
108-
109-
11091
template<typename InputIteratorType>
11192
auto calculate(InputIteratorType my_pi_in,
11293
callback_type pfn_callback_to_report_digits10 = nullptr,
@@ -126,13 +107,13 @@
126107
}
127108

128109
using local_input_iterator_type = InputIteratorType;
129-
using local_input_value_type = typename std::iterator_traits<local_input_iterator_type>::value_type;
110+
using local_input_value_type = typename std::iterator_traits<local_input_iterator_type>::value_type;
130111

131112
// Invalidate the input container values at the first 32 indices.
132113
const std::uint32_t
133114
invalidate_size
134115
{
135-
(std::min)(static_cast<std::uint32_t>(UINT8_C(32)), input_static_size)
116+
(std::min)(std::uint32_t { UINT8_C(32) }, get_input_static_size())
136117
};
137118

138119
std::fill(my_pi_in,
@@ -339,8 +320,22 @@
339320
pow10(loop_digit()) / static_cast<unsigned>(UINT8_C(5))
340321
);
341322
}
342-
};
343323

324+
static_assert(loop_digit() <= static_cast<std::uint32_t>(std::numeric_limits<unsigned_small_type>::digits10),
325+
"Error: loop_digit exceeds the number of base-10 digits in its constituent unsigned_small_type");
326+
327+
static_assert(result_digit() <= static_cast<std::uint32_t>(UINT32_C(100001)),
328+
"Error: result_digit exceeds its limit of 100,001");
329+
330+
static_assert(std::numeric_limits<unsigned_small_type>::digits * 2 == std::numeric_limits<unsigned_large_type>::digits,
331+
"Error: unsigned_large_type must be exactly twice as wide as unsigned_small_type");
332+
333+
static_assert((!std::numeric_limits<unsigned_small_type>::is_signed),
334+
"Error: unsigned_small_type must be unsigned");
335+
336+
static_assert((!std::numeric_limits<unsigned_large_type>::is_signed),
337+
"Error: unsigned_large_type must be unsigned");
338+
};
344339

345340
template<const std::uint32_t ResultDigit,
346341
const std::uint32_t LoopDigit,

examples/chapter11_07a/src/mcal/avr/mcal_led.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,3 @@ auto mcal::led::led0() -> led_base&
2121

2222
return l0;
2323
}
24-
25-
auto mcal::led::led1() -> led_base&
26-
{
27-
using led1_port_type = mcal::port::port_pin<std::uint8_t,
28-
std::uint8_t,
29-
mcal::reg::portd,
30-
UINT8_C(3)>;
31-
32-
using led1_led_type = mcal::led::led_port<led1_port_type>;
33-
34-
static led1_led_type l1;
35-
36-
return l1;
37-
}

examples/chapter11_07a/src/mcal/avr/mcal_led.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
namespace led
1616
{
1717
auto led0() -> led_base&;
18-
auto led1() -> led_base&;
1918
}
2019
}
2120

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
///////////////////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 2007 - 2021.
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)
@@ -14,10 +14,3 @@ mcal::led::led_base& mcal::led::led0()
1414

1515
return l0;
1616
}
17-
18-
mcal::led::led_base& mcal::led::led1()
19-
{
20-
static mcal::led::led_console l1(1U);
21-
22-
return l1;
23-
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
///////////////////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 2007 - 2021.
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)
66
//
77

8-
#ifndef MCAL_LED_2010_09_14_H_
9-
#define MCAL_LED_2010_09_14_H_
8+
#ifndef MCAL_LED_2010_09_14_H
9+
#define MCAL_LED_2010_09_14_H
1010

1111
#include <mcal_led/mcal_led_base.h>
1212

@@ -15,8 +15,7 @@
1515
namespace led
1616
{
1717
led_base& led0();
18-
led_base& led1();
1918
}
2019
}
2120

22-
#endif // MCAL_LED_2010_09_14_H_
21+
#endif // MCAL_LED_2010_09_14_H

0 commit comments

Comments
 (0)