Skip to content

Commit

Permalink
Merge pull request #598 from ckormanyos/example_chapter16_08
Browse files Browse the repository at this point in the history
Update example chapter16_08
  • Loading branch information
ckormanyos authored Dec 23, 2024
2 parents d614b7c + 1269e55 commit 3fcede9
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 66 deletions.
4 changes: 2 additions & 2 deletions examples/chapter11_07/chapter11_07.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@
<ClInclude Include="src\mcal\host\mcal_wdg.h" />
<ClInclude Include="src\mcal\host\mcal_wdg_watchdog.h" />
<ClInclude Include="src\mcal\mcal.h" />
<ClInclude Include="src\mcal\mcal_reg_access_template.h" />
<ClInclude Include="src\mcal\mcal_reg_dynamic_access_template.h" />
<ClInclude Include="src\mcal\mcal_reg_access_dynamic.h" />
<ClInclude Include="src\mcal\mcal_reg_access_static.h" />
<ClInclude Include="src\mcal_led\mcal_led_base.h" />
<ClInclude Include="src\mcal_led\mcal_led_boolean_state_base.h" />
<ClInclude Include="src\mcal_led\mcal_led_console.h" />
Expand Down
12 changes: 6 additions & 6 deletions examples/chapter11_07/chapter11_07.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,6 @@
<ClInclude Include="src\mcal\host\mcal_cpu.h">
<Filter>src\mcal\host</Filter>
</ClInclude>
<ClInclude Include="src\mcal\mcal_reg_access_template.h">
<Filter>src\mcal</Filter>
</ClInclude>
<ClInclude Include="src\mcal\mcal_reg_dynamic_access_template.h">
<Filter>src\mcal</Filter>
</ClInclude>
<ClInclude Include="src\os\FreeRTOS\Source\include\FreeRTOS.h">
<Filter>src\os\FreeRTOS\Source\include</Filter>
</ClInclude>
Expand Down Expand Up @@ -449,6 +443,12 @@
<ClInclude Include="src\math\checksums\crc\crc32.h">
<Filter>src\math\checksums\crc</Filter>
</ClInclude>
<ClInclude Include="src\mcal\mcal_reg_access_dynamic.h">
<Filter>src\mcal</Filter>
</ClInclude>
<ClInclude Include="src\mcal\mcal_reg_access_static.h">
<Filter>src\mcal</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="src\util\STL_C++XX_stdfloat\cstdfloat">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ configTOTAL_HEAP_SIZE is not defined. */
#define configUSE_PREEMPTION 1
#define configUSE_IDLE_HOOK 0
#define configUSE_TICK_HOOK 0
#define configCPU_CLOCK_HZ (( unsigned long) 16000000ULL)
#define configTICK_RATE_HZ (( TickType_t) 1000ULL)
#define configCPU_CLOCK_HZ ((unsigned long) 16000000ULL)
#define configTICK_RATE_HZ ((TickType_t) 1000ULL)
#define configMAX_PRIORITIES (4)
#define configMINIMAL_STACK_SIZE ((unsigned short) 96)
#define configMAX_TASK_NAME_LEN (32)
Expand Down
6 changes: 4 additions & 2 deletions examples/chapter16_08/chapter16_08.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@
<CodeAnalysisRuleAssemblies Condition="'$(Configuration)|$(Platform)'=='Release|x64'" />
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<IncludePath>$(SolutionDir)\src;$(SolutionDir)\src\mcal\win32;C:\boost\boost_1_86_0;$(IncludePath)</IncludePath>
<IncludePath>$(SolutionDir)\src;$(SolutionDir)\src\mcal\win32;C:\boost\boost_1_87_0;$(IncludePath)</IncludePath>
<OutDir>$(SolutionDir)$(Configuration)\</OutDir>
<IntDir>$(Configuration)\</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<IncludePath>$(SolutionDir)\src;$(SolutionDir)\src\mcal\win32;C:\boost\boost_1_86_0;$(IncludePath)</IncludePath>
<IncludePath>$(SolutionDir)\src;$(SolutionDir)\src\mcal\win32;C:\boost\boost_1_87_0;$(IncludePath)</IncludePath>
<OutDir>$(SolutionDir)$(Configuration)\</OutDir>
<IntDir>$(Configuration)\</IntDir>
</PropertyGroup>
Expand All @@ -78,6 +78,7 @@
<OutputFile>$(SolutionDir)$(Configuration)\$(ProjectName).exe</OutputFile>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Console</SubSystem>
<AdditionalDependencies>$(ProjectDir)src\math\test\wstp64i4.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand All @@ -95,6 +96,7 @@
<SubSystem>Console</SubSystem>
<OptimizeReferences>true</OptimizeReferences>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<AdditionalDependencies>$(ProjectDir)src\math\test\wstp64i4.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2019 - 2022.
// Copyright Christopher Kormanyos 2019 - 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 MATH_TEST_INDEPENDENT_TEST_SYSTEM_BASE_2020_07_16_H_
#define MATH_TEST_INDEPENDENT_TEST_SYSTEM_BASE_2020_07_16_H_
#ifndef MATH_TEST_INDEPENDENT_TEST_SYSTEM_BASE_2020_07_16_H
#define MATH_TEST_INDEPENDENT_TEST_SYSTEM_BASE_2020_07_16_H

#include <util/utility/util_noncopyable.h>

Expand All @@ -17,7 +17,7 @@
public:
virtual ~independent_test_system_base() noexcept = default;

virtual bool is_prime(const char* const pstr_prime_candidate) const = 0;
virtual auto is_prime(const char* const pstr_prime_candidate) const -> bool = 0;

protected:
independent_test_system_base() noexcept = default;
Expand All @@ -27,4 +27,4 @@
} // namespace math
} // namespace WIDE_INTEGER_NAMESPACE

#endif // MATH_TEST_INDEPENDENT_TEST_SYSTEM_BASE_2020_07_16_H_
#endif // MATH_TEST_INDEPENDENT_TEST_SYSTEM_BASE_2020_07_16_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
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2019 - 2022.
// Copyright Christopher Kormanyos 2019 - 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 MATH_TEST_INDEPENDENT_TEST_SYSTEM_MATHLINK_2019_12_29_H_
#define MATH_TEST_INDEPENDENT_TEST_SYSTEM_MATHLINK_2019_12_29_H_
#ifndef MATH_TEST_INDEPENDENT_TEST_SYSTEM_MATHLINK_2019_12_29_H
#define MATH_TEST_INDEPENDENT_TEST_SYSTEM_MATHLINK_2019_12_29_H

#include <array>
#include <string>
Expand Down Expand Up @@ -40,15 +40,15 @@

namespace detail {

inline constexpr char* strcpy_unsafe(char* dst, const char* src)
inline constexpr auto strcpy_unsafe(char* dst, const char* src) -> char*
{
// Use a local implementation of string copy.
while((*dst++ = *src++) != char('\0')) { ; }

return dst;
}

inline constexpr std::uint_fast32_t strlen_unsafe(const char* pstr)
inline constexpr auto strlen_unsafe(const char* pstr) -> std::uint_fast32_t
{
// Use a local implementation of string length.
std::uint_fast32_t u = 0U;
Expand All @@ -65,13 +65,12 @@
} // namespace detail

template<const char* PtrStrLocationMathLinkKernel = nullptr>
class independent_test_system_mathlink
: public math::test::independent_test_system_base
class independent_test_system_mathlink : public math::test::independent_test_system_base
{
private:
static constexpr int RETURNPKT = 3;
static constexpr int RETURNPKT { 3 };

static constexpr int return_packet_id() { return RETURNPKT; }
static constexpr auto return_packet_id() noexcept -> int { return RETURNPKT; }

public:
// This is the computer_algebra_system_mathlink class.
Expand All @@ -89,7 +88,7 @@
static_cast<void>(close_is_ok);
}

virtual bool is_prime(const char* const pstr_prime_candidate) const override
auto is_prime(const char* const pstr_prime_candidate) const -> bool override
{
// Test with, for eample,
// PrimeQ[FromDigits["A6BAC43FEE73E832401B1E32417BD0AF",16]]
Expand All @@ -101,14 +100,14 @@
return (send_command(str_cmd, &str_rsp) && (str_rsp == "True"));
}

static constexpr bool get_valid() noexcept { return my_valid; }
static constexpr auto get_valid() noexcept -> bool { return my_valid; }

private:
static bool my_valid;
static WSENV env_ptr;
static WSLINK lnk_ptr;

static bool send_command(const std::string& str_cmd, std::string* str_rsp)
static auto send_command(const std::string& str_cmd, std::string* str_rsp) -> bool
{
const bool suppress_output = (str_rsp == nullptr);

Expand Down Expand Up @@ -162,17 +161,17 @@
return result_is_ok;
}

static constexpr WSENV& global_env_ptr() noexcept { return env_ptr; }
static constexpr WSLINK& global_lnk_ptr() noexcept { return lnk_ptr; }
static constexpr auto global_env_ptr() noexcept -> WSENV& { return env_ptr; }
static constexpr auto global_lnk_ptr() noexcept -> WSLINK& { return lnk_ptr; }

static int next_packet () noexcept { return ::WSNextPacket (global_lnk_ptr()); }
static int new_packet () noexcept { return ::WSNewPacket (global_lnk_ptr()); }
static bool put_function(const std::string& str, int argc) noexcept { return (::WSPutFunction(global_lnk_ptr(), str.c_str(), argc) != 0); }
static bool put_string (const std::string& str) noexcept { return (::WSPutString (global_lnk_ptr(), str.c_str()) != 0); }
static bool end_packet () noexcept { return (::WSEndPacket (global_lnk_ptr()) != 0); }
static int error () noexcept { return ::WSError (global_lnk_ptr()); }
static auto next_packet () noexcept -> int { return ::WSNextPacket (global_lnk_ptr()); }
static auto new_packet () noexcept -> int { return ::WSNewPacket (global_lnk_ptr()); }
static auto put_function(const std::string& str, int argc) noexcept -> bool { return (::WSPutFunction(global_lnk_ptr(), str.c_str(), argc) != 0); }
static auto put_string (const std::string& str) noexcept -> bool { return (::WSPutString (global_lnk_ptr(), str.c_str()) != 0); }
static auto end_packet () noexcept -> bool { return (::WSEndPacket (global_lnk_ptr()) != 0); }
static auto error () noexcept -> int { return ::WSError (global_lnk_ptr()); }

static bool get_string(std::string* str_rsp)
static auto get_string(std::string* str_rsp) -> bool
{
const char* s = nullptr;

Expand All @@ -190,13 +189,13 @@
return get_string_is_ok;
}

static constexpr bool is_open() noexcept
static constexpr auto is_open() noexcept -> bool
{
return ( (global_env_ptr() != nullptr)
&& (global_lnk_ptr() != nullptr));
}

static bool open(const char* str_location_math_kernel_user = PtrStrLocationMathLinkKernel) noexcept
static auto open(const char* str_location_math_kernel_user = PtrStrLocationMathLinkKernel) noexcept -> bool
{
// Initialize the mathlink kernel.
global_env_ptr() = ::WSInitialize(nullptr);
Expand All @@ -212,8 +211,10 @@

// TBD: What about supporting non-Win* platforms like *nix?

constexpr char str_location_math_kernel_default[] =
"\"C:\\Program Files\\Wolfram Research\\Mathematica\\12.0\\MathKernel.exe\"";
constexpr char str_location_math_kernel_default[]
{
"\"C:\\Program Files\\Wolfram Research\\Mathematica\\14.0\\MathKernel.exe\""
};

// Create a list of constant arguments for opening the mathlink kernel.
using const_args_strings_type = std::array<std::string, 5U>;
Expand Down Expand Up @@ -258,7 +259,7 @@
return is_open();
}

static bool close() noexcept
static auto close() noexcept -> bool
{
const bool close_is_ok = is_open();

Expand All @@ -280,12 +281,12 @@
}
};

template<const char* PtrStrLocationMathLinkKernel> bool math::test::independent_test_system_mathlink<PtrStrLocationMathLinkKernel>::my_valid = false;
template<const char* PtrStrLocationMathLinkKernel> WSENV math::test::independent_test_system_mathlink<PtrStrLocationMathLinkKernel>::env_ptr = nullptr;
template<const char* PtrStrLocationMathLinkKernel> WSLINK math::test::independent_test_system_mathlink<PtrStrLocationMathLinkKernel>::lnk_ptr = nullptr;
template<const char* PtrStrLocationMathLinkKernel> bool math::test::independent_test_system_mathlink<PtrStrLocationMathLinkKernel>::my_valid { false };
template<const char* PtrStrLocationMathLinkKernel> WSENV math::test::independent_test_system_mathlink<PtrStrLocationMathLinkKernel>::env_ptr { nullptr };
template<const char* PtrStrLocationMathLinkKernel> WSLINK math::test::independent_test_system_mathlink<PtrStrLocationMathLinkKernel>::lnk_ptr { nullptr };

} // namespace test
} // namespace math
} // namespace WIDE_INTEGER_NAMESPACE

#endif // MATH_TEST_INDEPENDENT_TEST_SYSTEM_MATHLINK_2019_12_29_H_
#endif // MATH_TEST_INDEPENDENT_TEST_SYSTEM_MATHLINK_2019_12_29_H
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2019 - 2022.
// Copyright Christopher Kormanyos 2019 - 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 MATH_TEST_INDEPENDENT_TEST_SYSTEM_NONE_2020_07_15_H_
#define MATH_TEST_INDEPENDENT_TEST_SYSTEM_NONE_2020_07_15_H_
#ifndef MATH_TEST_INDEPENDENT_TEST_SYSTEM_NONE_2020_07_15_H
#define MATH_TEST_INDEPENDENT_TEST_SYSTEM_NONE_2020_07_15_H

#include <math/test/math_test_independent_test_system_base.h>

Expand All @@ -19,7 +19,7 @@

virtual ~independent_test_system_none() noexcept = default;

virtual bool is_prime(const char* const pstr_prime_candidate) const override
auto is_prime(const char* const pstr_prime_candidate) const -> bool override
{
static_cast<void>(pstr_prime_candidate);

Expand All @@ -31,4 +31,4 @@
} // namespace math
} // namespace WIDE_INTEGER_NAMESPACE

#endif // MATH_TEST_INDEPENDENT_TEST_SYSTEM_NONE_2020_07_15_H_
#endif // MATH_TEST_INDEPENDENT_TEST_SYSTEM_NONE_2020_07_15_H
Binary file added examples/chapter16_08/src/math/test/wstp64i4.lib
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -89,38 +89,74 @@
{
const limb_type lo_limb = my_n_trial.crepresentation().front();

const std::uint8_t lo_bit = std::uint8_t(std::uint8_t(lo_limb) & 1U);
const std::uint8_t
lo_bit
{
static_cast<std::uint8_t>
(
static_cast<std::uint8_t>(lo_limb) & std::uint8_t { UINT8_C(1) }
)
};

// Remove all even candidates since they are non-prime.
if(lo_bit != 0U)
if(lo_bit != std::uint8_t { UINT8_C(0) })
{
const std::uint8_t lo_digit10 = std::uint8_t(my_n_trial % std::uint8_t(10U));
const std::uint8_t
lo_digit10
{
static_cast<std::uint8_t>(my_n_trial % std::uint8_t { UINT8_C(10) })
};

// Continue by removing candidates having trailing digit 5.
// The result is all candidates have trailing digit 1,3,7,9
if(lo_digit10 != 5U)

if(lo_digit10 != std::uint8_t { UINT8_C(5) })
{
// Now remove candidates having digital root 3, 6 or 9
// because these are divisible by 3 and thus non-prime.

// To compute the digital root of n dr(n), use
// dr(n) = 1 + (n - 1) % 9.
wide_integer_type n_minus_one(my_n_trial);
wide_integer_type n_minus_one { my_n_trial };

--n_minus_one;

const std::uint8_t dr =
std::uint8_t(UINT8_C(1) + (std::uint8_t(n_minus_one % std::uint8_t(9U))));

if((dr != UINT8_C(3)) && (dr != UINT8_C(6)) && (dr != UINT8_C(9)))
const std::uint8_t
digital_root
{
static_cast<std::uint8_t>
(
std::uint8_t { UINT8_C(1) }
+ static_cast<std::uint8_t>(n_minus_one % std::uint8_t { UINT8_C(9) })
)
};

const bool
has_digital_root_of_3_6_or_9
{
(digital_root == std::uint8_t { UINT8_C(3) })
|| (digital_root == std::uint8_t { UINT8_C(6) })
|| (digital_root == std::uint8_t { UINT8_C(9) })
};

if(!has_digital_root_of_3_6_or_9)
{
// We have found a viable prime candidate. Use this prime
// candidate to start a new Miller-Rabin primality test.

set_n_is_ok = true;

// Reset the prime-search variables.

my_n_trial_is_probably_prime = false;
my_n = my_n_trial;
my_n_is_probably_prime = false;

my_n_total_mul_10 += 10U;
my_n_total_mul_10 =
static_cast<std::uint64_t>
(
my_n_total_mul_10 + std::uint_fast8_t { UINT8_C(10) }
);
}
}
}
Expand Down
Loading

0 comments on commit 3fcede9

Please sign in to comment.