Skip to content

Commit

Permalink
Update wide-decimal and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ckormanyos committed Feb 19, 2024
1 parent 03de7db commit 72897de
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 63 deletions.
4 changes: 0 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@
<img src="https://sonarcloud.io/api/project_badges/measure?project=ckormanyos_real-time-cpp&metric=alert_status" alt="Quality Gate Status"></a>
<a href="https://github.com/ckormanyos/real-time-cpp/blob/master/LICENSE_1_0.txt">
<img src="https://img.shields.io/badge/license-BSL%201.0-blue.svg" alt="Boost Software License 1.0"></a>
<a href="https://img.shields.io/github/commit-activity/y/ckormanyos/real-time-cpp">
<img src="https://img.shields.io/github/commit-activity/y/ckormanyos/real-time-cpp" alt="GitHub commit activity" /></a>
<a href="https://github.com/ckormanyos/real-time-cpp">
<img src="https://img.shields.io/github/languages/code-size/ckormanyos/real-time-cpp" alt="GitHub code size in bytes" /></a>
</p>

This is the companion code
Expand Down
27 changes: 16 additions & 11 deletions ref_app/src/math/wide_decimal/decwide_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@
% static_cast<int>(decwide_t_elem_digits10)
);

constexpr auto digit_loops =
constexpr auto digit_loops = // NOLINT(altera-id-dependent-backward-branch)
static_cast<int>
(
digit_elem_whole
Expand Down Expand Up @@ -2525,7 +2525,7 @@

auto data_exp_buf = exp_array_type { };

const char* p_end = util::baselexical_cast(ul_exp, data_exp_buf.data());
const char* p_end = util::baselexical_cast(ul_exp, data_exp_buf.data(), data_exp_buf.data() + data_exp_buf.size());

const auto exp_len = std::distance(static_cast<const char*>(data_exp_buf.data()), p_end);

Expand Down Expand Up @@ -2596,8 +2596,8 @@
val += static_cast<unsigned long long>(xn.my_data[limb_index]); // NOLINT(google-runtime-int)
}

signed_long_long_result = ((!b_neg) ? static_cast<signed long long>(val) // NOLINT(google-runtime-int)
: static_cast<signed long long>(-static_cast<signed long long>(val))); // NOLINT(google-runtime-int)
signed_long_long_result = ((!b_neg) ? static_cast<signed long long>(val) // NOLINT(google-runtime-int)
: static_cast<signed long long>(detail::negate(val))); // NOLINT(google-runtime-int)
}
}
}
Expand Down Expand Up @@ -3867,7 +3867,7 @@
// Obtain the digits in the first limb.
auto it_rep = x.crepresentation().cbegin(); // NOLINT(llvm-qualified-auto,readability-qualified-auto)

const char* p_end = util::baselexical_cast(*it_rep, data_elem_buf.data()); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
const char* p_end = util::baselexical_cast(*it_rep, data_elem_buf.data(), data_elem_buf.data() + data_elem_buf.size()); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)

++it_rep;

Expand All @@ -3893,7 +3893,7 @@
// beginning with the data element having index 1.
while(it_rep != (x.crepresentation().cbegin() + static_cast<std::size_t>(number_of_elements))) // NOLINT(altera-id-dependent-backward-branch)
{
p_end = util::baselexical_cast(*it_rep, data_elem_buf.data());
p_end = util::baselexical_cast(*it_rep, data_elem_buf.data(), data_elem_buf.data() + data_elem_buf.size());

++it_rep;

Expand Down Expand Up @@ -4083,12 +4083,13 @@

std::array<char, static_cast<std::size_t>(UINT8_C(20))> ptr_str = {{ '\0' }}; // NOLINT(,cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)

auto ptr_end = util::baselexical_cast(u_exp, ptr_str.data()); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg,llvm-qualified-auto,readability-qualified-auto)
const char* ptr_end = util::baselexical_cast(u_exp, ptr_str.data(), ptr_str.data() + ptr_str.size()); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg,llvm-qualified-auto,readability-qualified-auto)

auto str_exp = std::string(ptr_str.data(), ptr_end);
const auto str_exp_len = static_cast<std::size_t>(std::distance(static_cast<const char*>(ptr_str.data()), ptr_end));

auto str_exp = std::string(ptr_str.data(), ptr_str.data() + str_exp_len);

// Format the exponent string to have a width that is an even multiple of three.
const auto str_exp_len = static_cast<std::size_t>(str_exp.length());
const auto str_exp_len_mod3 = static_cast<std::size_t>(str_exp_len % static_cast<std::size_t>(UINT8_C(3)));

const auto exp_has_mod3 = (str_exp_len_mod3 != static_cast<std::size_t>(UINT8_C(0)));
Expand Down Expand Up @@ -5878,8 +5879,12 @@

// Ensure that the resulting power is non-negative.
// Also enforce that m >= 3.
const auto m = (std::max)(static_cast<std::int32_t>(n_times_factor - lg_x_over_lg2),
static_cast<std::int32_t>(3));
const auto m =
static_cast<std::int32_t>
(
(std::max)(static_cast<float>(n_times_factor - lg_x_over_lg2),
static_cast<float>(3))
);

local_wide_decimal_type bk =
one<ParamDigitsBaseTen, LimbType, AllocatorType, InternalFloatType, ExponentType, FftFloatType>();
Expand Down
10 changes: 5 additions & 5 deletions ref_app/src/mcal/mcal_gcc_cxx_completion.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2007 - 2023.
// Copyright Christopher Kormanyos 2007 - 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 Expand Up @@ -134,13 +134,13 @@ extern "C"
#endif

// Provide some patched data values.
const char* const __env[1U] = { nullptr };
const char** const environ = { nullptr };
const char* const __env[1U] = { nullptr };
const char** const environ = { nullptr };

#if (defined(__GNUC__) && defined(__v850__))
#else
int* __errno(void);
int* __errno(void) { static int __my_errno; return &__my_errno; }
extern int* __errno(void);
int* __errno(void) { return nullptr; }
#endif

std::uint8_t __fdlib_version;
Expand Down
46 changes: 23 additions & 23 deletions ref_app/src/mcal_lcd/mcal_lcd_generic_st7066.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2020 - 2022.
// Copyright Christopher Kormanyos 2020 - 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 Expand Up @@ -40,17 +40,12 @@
private:
using timer_type = util::timer<std::uint32_t>;

static void blocking_delay(const typename timer_type::tick_type blocking_delay_value)
{
timer_type::blocking_delay(blocking_delay_value);
}

public:
lcd_generic_st7066() = default;

virtual ~lcd_generic_st7066() = default;
~lcd_generic_st7066() override = default;

virtual bool init(void)
auto init(void) -> bool override
{
port_pin_rs__type::set_pin_low();
port_pin_rw__type::set_pin_low();
Expand Down Expand Up @@ -85,9 +80,9 @@
return write_clear_lines_is_ok;
}

virtual bool write_n(const char* pstr,
std::uint_fast8_t length,
std::uint_fast8_t line_index)
auto write(const char* pstr,
std::uint_fast8_t length,
std::uint_fast8_t line_index) -> bool override
{
std::uint_fast8_t char_index = 0U;

Expand All @@ -111,7 +106,12 @@
}

private:
void write(const std::uint8_t i)
static void blocking_delay(const typename timer_type::tick_type blocking_delay_value)
{
timer_type::blocking_delay(blocking_delay_value);
}

auto write(const std::uint8_t i) -> void
{
P1_set(i); // P1 = i; // Put data on the output Port
port_pin_rs__type::set_pin_high(); // D_I =1; // D/I=HIGH : send data
Expand All @@ -121,7 +121,7 @@
port_pin_e___type::set_pin_low(); // E = 0; // Clock enable: falling edge
}

void command(std::uint8_t i)
auto command(std::uint8_t i) -> void
{
P1_set(i); // P1 = i; // Put data on output Port
port_pin_rs__type::set_pin_low(); // D_I =0; // D/I=LOW : send instruction
Expand All @@ -132,19 +132,19 @@
blocking_delay(timer_type::microseconds(40U)); // Command execution delay
}

void P1_set(const std::uint8_t c)
auto P1_set(const std::uint8_t c) -> void
{
std::uint_fast8_t(c & UINT8_C(0x01)) != UINT8_C(0) ? port_pin_db0_type::set_pin_high() : port_pin_db0_type::set_pin_low();
std::uint_fast8_t(c & UINT8_C(0x02)) != UINT8_C(0) ? port_pin_db1_type::set_pin_high() : port_pin_db1_type::set_pin_low();
std::uint_fast8_t(c & UINT8_C(0x04)) != UINT8_C(0) ? port_pin_db2_type::set_pin_high() : port_pin_db2_type::set_pin_low();
std::uint_fast8_t(c & UINT8_C(0x08)) != UINT8_C(0) ? port_pin_db3_type::set_pin_high() : port_pin_db3_type::set_pin_low();
std::uint_fast8_t(c & UINT8_C(0x10)) != UINT8_C(0) ? port_pin_db4_type::set_pin_high() : port_pin_db4_type::set_pin_low();
std::uint_fast8_t(c & UINT8_C(0x20)) != UINT8_C(0) ? port_pin_db5_type::set_pin_high() : port_pin_db5_type::set_pin_low();
std::uint_fast8_t(c & UINT8_C(0x40)) != UINT8_C(0) ? port_pin_db6_type::set_pin_high() : port_pin_db6_type::set_pin_low();
std::uint_fast8_t(c & UINT8_C(0x80)) != UINT8_C(0) ? port_pin_db7_type::set_pin_high() : port_pin_db7_type::set_pin_low();
(static_cast<std::uint_fast8_t>(c & UINT8_C(0x01)) != UINT8_C(0)) ? port_pin_db0_type::set_pin_high() : port_pin_db0_type::set_pin_low();
(static_cast<std::uint_fast8_t>(c & UINT8_C(0x02)) != UINT8_C(0)) ? port_pin_db1_type::set_pin_high() : port_pin_db1_type::set_pin_low();
(static_cast<std::uint_fast8_t>(c & UINT8_C(0x04)) != UINT8_C(0)) ? port_pin_db2_type::set_pin_high() : port_pin_db2_type::set_pin_low();
(static_cast<std::uint_fast8_t>(c & UINT8_C(0x08)) != UINT8_C(0)) ? port_pin_db3_type::set_pin_high() : port_pin_db3_type::set_pin_low();
(static_cast<std::uint_fast8_t>(c & UINT8_C(0x10)) != UINT8_C(0)) ? port_pin_db4_type::set_pin_high() : port_pin_db4_type::set_pin_low();
(static_cast<std::uint_fast8_t>(c & UINT8_C(0x20)) != UINT8_C(0)) ? port_pin_db5_type::set_pin_high() : port_pin_db5_type::set_pin_low();
(static_cast<std::uint_fast8_t>(c & UINT8_C(0x40)) != UINT8_C(0)) ? port_pin_db6_type::set_pin_high() : port_pin_db6_type::set_pin_low();
(static_cast<std::uint_fast8_t>(c & UINT8_C(0x80)) != UINT8_C(0)) ? port_pin_db7_type::set_pin_high() : port_pin_db7_type::set_pin_low();
}

static void P1_set_direction_output()
static auto P1_set_direction_output() -> void
{
port_pin_db0_type::set_direction_output();
port_pin_db1_type::set_direction_output();
Expand Down
25 changes: 14 additions & 11 deletions ref_app/src/util/memory/util_n_slot_array_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,27 @@
constexpr auto address( reference x) const -> pointer { return &x; }
constexpr auto address(const_reference x) const -> const_pointer { return &x; }

auto allocate
(
size_type count,
typename n_slot_array_allocator<void, slot_width, slot_count>::const_pointer p_hint = nullptr
) -> pointer
auto allocate(size_type count, const_pointer p_hint = nullptr) -> pointer
{
static_cast<void>(count);
static_cast<void>(p_hint);

pointer p = nullptr;
pointer p { nullptr };

// TBD: There is most likely significant optimization potential
// capable of being unlocked if a storage/lookup mechanism can be
// devised that uses a binary search when finding the next free slot.

for(std::size_t i = 0U; i < slot_count; ++i)
// (TBD) In fact, constant-time allocation probably possible, as shown in:
// SmallObjectAllocator from Modern C++ Design by Andrei Alexandrescu.

for(auto i = static_cast<std::size_t>(UINT8_C(0)); i < slot_count; ++i)
{
if(slot_flags[i] == 0U) // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)
using local_flags_value_type = typename slot_array_flags_type::value_type;

if(slot_flags[i] == static_cast<local_flags_value_type>(UINT8_C(0))) // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)
{
slot_flags[i] = 1U; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)
slot_flags[i] = static_cast<local_flags_value_type>(UINT8_C(1)); // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)

p = static_cast<pointer>(slot_array_memory[i].data()); // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)

Expand Down Expand Up @@ -126,13 +127,15 @@
{
static_cast<void>(sz);

auto index = static_cast<typename slot_array_memory_type::size_type>(UINT8_C(0));
typename slot_array_memory_type::size_type index { };

for(auto& slot_array_memory_entry : slot_array_memory)
{
if(p_slot == static_cast<pointer>(slot_array_memory_entry.data()))
{
slot_flags[index] = static_cast<typename slot_array_flags_type::value_type>(UINT8_C(0)); // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)
using local_flags_value_type = typename slot_array_flags_type::value_type;

slot_flags[index] = static_cast<local_flags_value_type>(UINT8_C(0)); // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)

break;
}
Expand Down
49 changes: 40 additions & 9 deletions ref_app/src/util/utility/util_baselexical_cast.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2020 - 2022.
// Copyright Christopher Kormanyos 2020 - 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 All @@ -8,13 +8,40 @@
#ifndef UTIL_BASELEXICAL_CAST_2020_06_28_H // NOLINT(llvm-header-guard)
#define UTIL_BASELEXICAL_CAST_2020_06_28_H

#if ((defined(__cplusplus) && (__cplusplus >= 201703L)) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)))
#if !(defined(__GNUC__) && defined(__AVR__))
#define UTIL_BASELEXICAL_CAST_HAS_CHARCONV
#endif
#endif

#if defined(UTIL_BASELEXICAL_CAST_HAS_CHARCONV)
#include <charconv>
#include <cstdint>
#else
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <iterator>
#endif

namespace util {

#if defined(UTIL_BASELEXICAL_CAST_HAS_CHARCONV)

template<typename UnsignedIntegerType,
const std::uint_fast8_t BaseRepresentation = static_cast<std::uint_fast8_t>(UINT8_C(10)),
const bool UpperCase = true>
auto baselexical_cast(const UnsignedIntegerType& u, char* first, char* last) -> const char*
{
constexpr auto my_base = static_cast<int>(BaseRepresentation);

const auto result = std::to_chars(first, last, u, my_base);

return result.ptr;
}

#else

template<typename OutputIterator,
const bool UpperCase,
const std::uint_fast8_t BaseRepresentation>
Expand All @@ -29,29 +56,29 @@

template<typename OutputIterator,
const bool UpperCase>
struct baselexical_cast_helper<OutputIterator, UpperCase, static_cast<std::uint_fast8_t>(UINT8_C(16))> // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
struct baselexical_cast_helper<OutputIterator, UpperCase, static_cast<std::uint_fast8_t>(UINT8_C(16))>
{
private:
using output_value_type = typename std::iterator_traits<OutputIterator>::value_type;

public:
static auto extract(output_value_type c) noexcept -> output_value_type
{
if(c <= static_cast<output_value_type>(9)) // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
if(c <= static_cast<output_value_type>(INT8_C(9)))
{
c =
static_cast<output_value_type>
(
c + static_cast<output_value_type>('0')
);
}
else if((c >= static_cast<output_value_type>(0xA)) && (c <= static_cast<output_value_type>(0xF))) // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
else if((c >= static_cast<output_value_type>(0xA)) && (c <= static_cast<output_value_type>(INT8_C(0xF)))) // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
{
c =
static_cast<output_value_type>
(
static_cast<output_value_type>(UpperCase ? static_cast<output_value_type>('A') : static_cast<output_value_type>('a'))
+ static_cast<output_value_type>(c - static_cast<output_value_type>(0xA)) // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
+ static_cast<output_value_type>(c - static_cast<output_value_type>(INT8_C(0xA)))
);
}

Expand All @@ -61,15 +88,15 @@

template<typename OutputIterator,
const bool UpperCase>
struct baselexical_cast_helper<OutputIterator, UpperCase, static_cast<std::uint_fast8_t>(UINT8_C(10))> // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
struct baselexical_cast_helper<OutputIterator, UpperCase, static_cast<std::uint_fast8_t>(UINT8_C(10))>
{
private:
using output_value_type = typename std::iterator_traits<OutputIterator>::value_type;

public:
static auto extract(output_value_type c) noexcept -> output_value_type
{
if(c <= static_cast<output_value_type>(9)) // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
if(c <= static_cast<output_value_type>(INT8_C(9)))
{
c =
static_cast<output_value_type>
Expand All @@ -84,10 +111,12 @@

template<typename UnsignedIntegerType,
typename OutputIterator,
const std::uint_fast8_t BaseRepresentation = static_cast<std::uint_fast8_t>(UINT8_C(10)), // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
const std::uint_fast8_t BaseRepresentation = static_cast<std::uint_fast8_t>(UINT8_C(10)),
const bool UpperCase = true>
auto baselexical_cast(const UnsignedIntegerType& u, OutputIterator out) -> OutputIterator
auto baselexical_cast(const UnsignedIntegerType& u, OutputIterator out, OutputIterator out_dummy) -> OutputIterator
{
static_cast<void>(out_dummy);

using unsigned_integer_type = UnsignedIntegerType;
using output_value_type = typename std::iterator_traits<OutputIterator>::value_type;

Expand Down Expand Up @@ -137,6 +166,8 @@
return out + static_cast<std::size_t>(UINT8_C(1));
}

#endif

} // namespace util

#endif // UTIL_BASELEXICAL_CAST_2020_06_28_H

0 comments on commit 72897de

Please sign in to comment.