Skip to content

Commit e56c1dc

Browse files
committed
Update wide math and LED task and utils
1 parent 6138409 commit e56c1dc

File tree

13 files changed

+206
-147
lines changed

13 files changed

+206
-147
lines changed

examples/chapter16_08/src/math/wide_integer/uintwide_t.h

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2957,7 +2957,7 @@
29572957

29582958
++dst;
29592959

2960-
tu >>= 4;
2960+
tu >>= static_cast<unsigned>(UINT8_C(4));
29612961
}
29622962

29632963
return dst;
@@ -4283,7 +4283,7 @@
42834283

42844284
const auto n = static_cast<local_uint_index_type> (number_of_limbs - v_offset);
42854285
const auto m = static_cast<local_uint_index_type>(static_cast<local_uint_index_type>(number_of_limbs - u_offset) - n);
4286-
const auto vj0 = static_cast<local_uint_index_type>(static_cast<local_uint_index_type>(number_of_limbs - 1U) - v_offset);
4286+
const auto vj0 = static_cast<local_uint_index_type>(static_cast<local_uint_index_type>(n - static_cast<local_uint_index_type>(UINT8_C(1))));
42874287

42884288
auto vv_at_vj0_it = detail::advance_and_point(vv.cbegin(), static_cast<size_t>(vj0)); // NOLINT(llvm-qualified-auto,readability-qualified-auto)
42894289

@@ -4332,7 +4332,7 @@
43324332
// Replace u[j, ... j + n] by u[j, ... j + n] - q_hat * v[1, ... n].
43334333

43344334
// Set nv = q_hat * (v[1, ... n]).
4335-
uu_array_type nv;
4335+
uu_array_type nv { };
43364336

43374337
*(nv.begin() + static_cast<size_t>(n)) = eval_multiply_1d(nv.begin(), vv.cbegin(), q_hat, n);
43384338

@@ -4342,7 +4342,6 @@
43424342
nv.cbegin(),
43434343
n + 1U);
43444344

4345-
43464345
// Step D5: Test the remainder.
43474346
// Set the result value: Set result.m_data[m - j] = q_hat.
43484347
// Use the condition (u[j] < 0), in other words if the borrow
@@ -4639,7 +4638,7 @@
46394638
{
46404639
const auto uc_oct = static_cast<std::uint8_t>(c - static_cast<char>(UINT8_C(0x30)));
46414640

4642-
operator<<=(3U);
4641+
operator<<=(static_cast<unsigned>(UINT8_C(3)));
46434642

46444643
*values.begin() = static_cast<limb_type>(*values.begin() | uc_oct);
46454644
}
@@ -4673,7 +4672,7 @@
46734672
else if(char_is_a_to_f_hi) { uc_hex = static_cast<std::uint8_t>(c - static_cast<char>(UINT8_C( 55))); }
46744673
else if(char_is_0_to_9) { uc_hex = static_cast<std::uint8_t>(c - static_cast<char>(UINT8_C(0x30))); }
46754674

4676-
operator<<=(4U);
4675+
operator<<=(static_cast<unsigned>(UINT8_C(4)));
46774676

46784677
*values.begin() = static_cast<limb_type>(*values.begin() | uc_hex);
46794678
}
@@ -4920,13 +4919,13 @@
49204919

49214920
local_wide_integer_type remainder;
49224921

4923-
local_wide_integer_type((!u_is_neg) ? u : -u).eval_divide_by_single_limb(v, 0U, &remainder);
4922+
local_wide_integer_type((!u_is_neg) ? u : -u).eval_divide_by_single_limb(v, static_cast<unsigned_fast_type>(UINT8_C(0)), &remainder);
49244923

49254924
using local_limb_type = typename local_wide_integer_type::limb_type;
49264925

49274926
auto u_rem = static_cast<local_limb_type>(remainder);
49284927

4929-
return ((!u_is_neg) ? u_rem : static_cast<local_limb_type>(static_cast<local_limb_type>(~u_rem) + 1U));
4928+
return ((!u_is_neg) ? u_rem : static_cast<local_limb_type>(static_cast<local_limb_type>(~u_rem) + static_cast<local_limb_type>(UINT8_C(1))));
49304929
}
49314930

49324931
template<typename IntegralType, const size_t Width2, typename LimbType, typename AllocatorType, const bool IsSigned>
@@ -5273,7 +5272,7 @@
52735272
// on the lowest bit position of the fundamental type.
52745273
while(static_cast<std::uint_fast8_t>(static_cast<std::uint_fast8_t>(mask) & static_cast<std::uint_fast8_t>(UINT8_C(1))) == static_cast<std::uint_fast8_t>(UINT8_C(0))) // NOLINT(hicpp-signed-bitwise,altera-id-dependent-backward-branch)
52755274
{
5276-
mask = static_cast<local_unsigned_integral_type>(mask >> 1U);
5275+
mask = static_cast<local_unsigned_integral_type>(mask >> static_cast<unsigned>(UINT8_C(1)));
52775276

52785277
++result;
52795278
}
@@ -5509,7 +5508,7 @@
55095508
{
55105509
s = u;
55115510

5512-
u = (s + (m / s)) >> 1;
5511+
u = (s + (m / s)) >> static_cast<unsigned>(UINT8_C(1));
55135512

55145513
if(u >= s) { break; } // LCOV_EXCL_LINE
55155514
}
@@ -5580,7 +5579,7 @@
55805579
m_over_s_pow_3_minus_one /= s;
55815580
}
55825581

5583-
u = ((s * three_minus_one) + m_over_s_pow_3_minus_one) / 3U;
5582+
u = ((s * three_minus_one) + m_over_s_pow_3_minus_one) / static_cast<unsigned>(UINT8_C(3));
55845583

55855584
if(u >= s) { break; }
55865585
}
@@ -5687,7 +5686,7 @@
56875686

56885687
if((p0 == static_cast<local_limb_type>(UINT8_C(0))) && (p == static_cast<OtherIntegralTypeP>(0)))
56895688
{
5690-
result = local_wide_integer_type(static_cast<std::uint8_t>(1U));
5689+
result = local_wide_integer_type(static_cast<std::uint8_t>(UINT8_C(1)));
56915690
}
56925691
else if((p0 == static_cast<local_limb_type>(UINT8_C(1))) && (p == static_cast<OtherIntegralTypeP>(1)))
56935692
{
@@ -5705,7 +5704,7 @@
57055704
local_wide_integer_type y (b);
57065705
local_wide_integer_type p_local(p);
57075706

5708-
while(((p0 = static_cast<local_limb_type>(p_local)) != 0U) || (p_local != 0U)) // NOLINT(altera-id-dependent-backward-branch)
5707+
while(((p0 = static_cast<local_limb_type>(p_local)) != static_cast<local_limb_type>(UINT8_C(0))) || (p_local != static_cast<local_wide_integer_type>(UINT8_C(0)))) // NOLINT(altera-id-dependent-backward-branch)
57095708
{
57105709
if(static_cast<unsigned_fast_type>(p0 & static_cast<local_limb_type>(UINT8_C(1))) != static_cast<unsigned_fast_type>(UINT8_C(0)))
57115710
{
@@ -5745,7 +5744,7 @@
57455744

57465745
if((p0 == static_cast<local_limb_type>(UINT8_C(0))) && (p == static_cast<OtherIntegralTypeP>(0)))
57475746
{
5748-
result = local_normal_width_type((m != 1U) ? static_cast<std::uint8_t>(1U) : static_cast<std::uint8_t>(UINT8_C(0)));
5747+
result = local_normal_width_type((m != static_cast<unsigned>(UINT8_C(1))) ? static_cast<std::uint8_t>(UINT8_C(1)) : static_cast<std::uint8_t>(UINT8_C(0)));
57495748
}
57505749
else if((p0 == static_cast<local_limb_type>(UINT8_C(1))) && (p == static_cast<OtherIntegralTypeP>(1)))
57515750
{
@@ -5760,10 +5759,12 @@
57605759
}
57615760
else
57625761
{
5763-
local_double_width_type x (static_cast<std::uint8_t>(UINT8_C(1)));
5764-
OtherIntegralTypeP p_local(p);
5762+
using local_other_integral_p_type = OtherIntegralTypeP;
5763+
5764+
local_double_width_type x (static_cast<std::uint8_t>(UINT8_C(1)));
5765+
local_other_integral_p_type p_local(p);
57655766

5766-
while(((p0 = static_cast<local_limb_type>(p_local)) != 0U) || (p_local != static_cast<OtherIntegralTypeP>(0))) // NOLINT(altera-id-dependent-backward-branch)
5767+
while(((p0 = static_cast<local_limb_type>(p_local)) != static_cast<local_limb_type>(UINT8_C(0))) || (p_local != static_cast<local_other_integral_p_type>(0))) // NOLINT(altera-id-dependent-backward-branch)
57675768
{
57685769
if(static_cast<unsigned_fast_type>(p0 & static_cast<local_limb_type>(UINT8_C(1))) != static_cast<unsigned_fast_type>(UINT8_C(0)))
57695770
{
@@ -5841,9 +5842,9 @@
58415842

58425843
v -= u;
58435844

5844-
while(static_cast<std::uint_fast8_t>(static_cast<std::uint_fast8_t>(v) & UINT8_C(1)) == UINT8_C(0)) // NOLINT(hicpp-signed-bitwise,altera-id-dependent-backward-branch)
5845+
while(static_cast<std::uint_fast8_t>(static_cast<std::uint_fast8_t>(v) & static_cast<std::uint_fast8_t>(UINT8_C(1))) == static_cast<std::uint_fast8_t>(UINT8_C(0))) // NOLINT(hicpp-signed-bitwise,altera-id-dependent-backward-branch)
58455846
{
5846-
v >>= 1U;
5847+
v >>= static_cast<unsigned>(UINT8_C(1));
58475848
}
58485849
}
58495850

@@ -6289,13 +6290,13 @@
62896290

62906291
constexpr auto digits_generator_result_type = static_cast<std::uint32_t>(GeneratorResultBits);
62916292

6292-
static_assert((digits_generator_result_type % UINT32_C(8)) == UINT32_C(0),
6293+
static_assert(static_cast<std::uint32_t>(digits_generator_result_type % static_cast<std::uint32_t>(UINT8_C(8))) == static_cast<std::uint32_t>(UINT32_C(0)),
62936294
"Error: Generator result type must have a multiple of 8 bits.");
62946295

62956296
constexpr auto digits_limb_ratio =
6296-
static_cast<std::uint32_t>(std::numeric_limits<local_limb_type>::digits / 8U);
6297+
static_cast<std::uint32_t>(std::numeric_limits<local_limb_type>::digits / static_cast<int>(INT8_C(8)));
62976298

6298-
constexpr auto digits_gtor_ratio = static_cast<std::uint32_t>(digits_generator_result_type / 8U);
6299+
constexpr auto digits_gtor_ratio = static_cast<std::uint32_t>(digits_generator_result_type / static_cast<std::uint32_t>(UINT8_C(8)));
62996300

63006301
generator_result_type value = generator_result_type();
63016302

@@ -6539,7 +6540,7 @@
65396540

65406541
const auto fn0 = static_cast<local_limb_type>(fn);
65416542

6542-
if((fn0 != 1U) && (fn != 1U))
6543+
if((fn0 != static_cast<local_limb_type>(UINT8_C(1))) && (fn != 1U))
65436544
{
65446545
return false;
65456546
}
@@ -6572,7 +6573,7 @@
65726573
{
65736574
const local_limb_type y0(y);
65746575

6575-
if((y0 == 1U) && (y == 1U))
6576+
if((y0 == static_cast<local_limb_type>(UINT8_C(1))) && (y == 1U))
65766577
{
65776578
if(j != static_cast<unsigned_fast_type>(UINT8_C(0)))
65786579
{

examples/chapter16_08/src/util/STL/type_traits

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
///////////////////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 2013 - 2022.
2+
// Copyright Christopher Kormanyos 2013 - 2023.
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)

ref_app/src/app/benchmark/app_benchmark_hash_sha256.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ auto app::benchmark::run_hash_sha256() -> bool
5757
#if defined(APP_BENCHMARK_STANDALONE_MAIN)
5858
int main()
5959
{
60-
// g++ -Wall -O3 -march=native -I./ref_app/src/mcal/host -I./ref_app/src -DAPP_BENCHMARK_TYPE_HASH_SHA256 -DAPP_BENCHMARK_STANDALONE_MAIN ./ref_app/src/app/benchmark/app_benchmark_hash.cpp -o ./ref_app/bin/app_benchmark_hash.exe
60+
// g++ -Wall -O3 -march=native -I./ref_app/src/mcal/host -I./ref_app/src -DAPP_BENCHMARK_TYPE=APP_BENCHMARK_TYPE_HASH_SHA256 -DAPP_BENCHMARK_STANDALONE_MAIN ./ref_app/src/app/benchmark/app_benchmark_hash.cpp -o ./ref_app/bin/app_benchmark_hash.exe
6161

6262
auto result_is_ok = true;
6363

ref_app/src/app/led/app_led.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
///////////////////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 2007 - 2022.
2+
// Copyright Christopher Kormanyos 2007 - 2023.
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)
@@ -20,14 +20,18 @@ namespace local
2020
{
2121
using app_led_timer_type = util::timer<std::uint32_t>;
2222

23+
using app_led_tick_type = typename app_led_timer_type::tick_type;
24+
2325
auto app_led_timer() noexcept -> app_led_timer_type&;
2426
} // namespace local
2527

2628
auto local::app_led_timer() noexcept -> app_led_timer_type&
2729
{
28-
using local_app_led_tick_type = typename app_led_timer_type::tick_type;
29-
30-
static app_led_timer_type local_app_led_timer(app_led_timer_type::seconds(static_cast<local_app_led_tick_type>(UINT8_C(1))));
30+
static auto local_app_led_timer =
31+
static_cast<app_led_timer_type>
32+
(
33+
app_led_timer_type::seconds(static_cast<app_led_tick_type>(UINT8_C(1)))
34+
);
3135

3236
return local_app_led_timer;
3337
}
@@ -41,7 +45,10 @@ auto app::led::task_func() -> void
4145
{
4246
if(local::app_led_timer().timeout())
4347
{
44-
local::app_led_timer().start_interval(local::app_led_timer_type::seconds(1U));
48+
local::app_led_timer().start_interval
49+
(
50+
local::app_led_timer_type::seconds(static_cast<local::app_led_tick_type>(UINT8_C(1)))
51+
);
4552

4653
mcal::led::led0().toggle();
4754
}

ref_app/src/math/checksums/hash/hash_base.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,13 @@
8383
// available for storing the total bit count. So we must
8484
// transform the current block, then create and pad yet another
8585
// additional block.
86+
const auto message_top =
87+
static_cast<std::uint16_t>
88+
(
89+
message_index + static_cast<std::uint_least16_t>(message_length_total_width())
90+
);
8691

87-
if(static_cast<std::uint16_t>(message_index + static_cast<std::uint_least16_t>(message_length_total_width())) > message_buffer_static_size())
92+
if(message_top > message_buffer_static_size())
8893
{
8994
my_perform_algorithm();
9095
}
@@ -97,7 +102,7 @@
97102
auto carry = static_cast<std::uint_fast8_t>(UINT8_C(0));
98103

99104
for(auto ri = message_buffer.rbegin();
100-
ri != message_buffer.rbegin() + message_length_total_width();
105+
ri != message_buffer.rbegin() + static_cast<std::size_t>(message_length_total_width());
101106
++ri)
102107
{
103108
const std::uint_least16_t the_word =

0 commit comments

Comments
 (0)