|
| 1 | +/////////////////////////////////////////////////////////////////// |
| 2 | +// // |
| 3 | +// Copyright Iliass Mahjoub 2022. // |
| 4 | +// Copyright Christopher Kormanyos 2019 - 2025. // |
| 5 | +// Distributed under the Boost Software License, // |
| 6 | +// Version 1.0. (See accompanying file LICENSE_1_0.txt // |
| 7 | +// or copy at http://www.boost.org/LICENSE_1_0.txt) // |
| 8 | +// // |
| 9 | +/////////////////////////////////////////////////////////////////// |
| 10 | + |
| 11 | +// pi_spigot.cpp |
| 12 | + |
| 13 | +// This program can be used to compute many thousands |
| 14 | +// of decimal digits of digits of pi. It uses a so-called |
| 15 | +// "spigot" algorithm having quadratic complexity. |
| 16 | + |
| 17 | +// In this embedded software adaptation of this work, |
| 18 | +// we limit the decimal digit count to about 100k or fewer. |
| 19 | + |
| 20 | +// cd /mnt/c/Users/ckorm/Documents/Ks/uC_Software/Boards/Osek_pi_crunch_cm3 |
| 21 | +// ./Build.sh |
| 22 | + |
| 23 | +// To build pi_spigot.cpp on LINUX host: |
| 24 | +// cd /mnt/c/Users/ckorm/Documents/Ks/uC_Software/Boards/Osek_pi_crunch_cm3 |
| 25 | +// g++ -std=c++20 -Werror -Wall -Wextra -Wpedantic -O3 -DPI_CRUNCH_METAL_STANDALONE_MAIN -DPI_CRUNCH_METAL_PI_SPIGOT_DIGITS=10000 -I./Application/ref_app/src -I./Application ./Application/pi_spigot/pi_spigot.cpp -o pi_spigot.exe |
| 26 | + |
| 27 | +#include <pi_calc_cfg.h> |
| 28 | + |
| 29 | +#include <math/checksums/hash/hash_sha1.h> |
| 30 | +#include <math/pi_spigot/pi_spigot.h> |
| 31 | +#if !defined(PI_CRUNCH_METAL_STANDALONE_MAIN) |
| 32 | +#include <mcal_benchmark.h> |
| 33 | +#include <mcal_memory/mcal_memory_sram_array.h> |
| 34 | +#endif |
| 35 | +#include <util/utility/util_baselexical_cast.h> |
| 36 | + |
| 37 | +#if !defined(PI_CRUNCH_METAL_DISABLE_IOSTREAM) |
| 38 | +#include <array> |
| 39 | +#include <chrono> |
| 40 | +#include <iomanip> |
| 41 | +#include <iostream> |
| 42 | +#include <sstream> |
| 43 | +#endif // !PI_CRUNCH_METAL_DISABLE_IOSTREAM |
| 44 | + |
| 45 | +namespace local |
| 46 | +{ |
| 47 | + #if defined(PI_CRUNCH_METAL_PI_SPIGOT_DIGITS) |
| 48 | + #if (PI_CRUNCH_METAL_PI_SPIGOT_DIGITS == PI_CRUNCH_METAL_PI_SPIGOT_USE_100_DIGITS) |
| 49 | + constexpr auto result_digit = static_cast<std::uint32_t>(UINT32_C(101)); |
| 50 | + #elif (PI_CRUNCH_METAL_PI_SPIGOT_DIGITS == PI_CRUNCH_METAL_PI_SPIGOT_USE_1K_DIGITS) |
| 51 | + constexpr auto result_digit = static_cast<std::uint32_t>(UINT32_C(1001)); |
| 52 | + #elif (PI_CRUNCH_METAL_PI_SPIGOT_DIGITS == PI_CRUNCH_METAL_PI_SPIGOT_USE_10K_DIGITS) |
| 53 | + constexpr auto result_digit = static_cast<std::uint32_t>(UINT32_C(10001)); |
| 54 | + #elif (PI_CRUNCH_METAL_PI_SPIGOT_DIGITS == PI_CRUNCH_METAL_PI_SPIGOT_USE_100K_DIGITS) |
| 55 | + constexpr auto result_digit = static_cast<std::uint32_t>(UINT32_C(100001)); |
| 56 | + #endif |
| 57 | + #else |
| 58 | + #error Error: Please define PI_CRUNCH_METAL_PI_SPIGOT_DIGITS |
| 59 | + #endif |
| 60 | + |
| 61 | + constexpr auto loop_digit = static_cast<std::uint32_t>(UINT8_C(9)); |
| 62 | + |
| 63 | + using pi_spigot_type = math::constants::pi_spigot<result_digit, loop_digit>; |
| 64 | + |
| 65 | + pi_spigot_type pi_spigot_instance; |
| 66 | + |
| 67 | + using hash_type = math::checksums::hash::hash_sha1; |
| 68 | + |
| 69 | + hash_type pi_spigot_hash; |
| 70 | + |
| 71 | + auto pi_output_digits10 = static_cast<std::uint32_t>(UINT8_C(0)); |
| 72 | + |
| 73 | + #if !defined(PI_CRUNCH_METAL_STANDALONE_MAIN) |
| 74 | + using benchmark_port_type = ::mcal::benchmark::benchmark_port_type; |
| 75 | + #endif |
| 76 | + |
| 77 | + #if defined(PI_CRUNCH_METAL_STANDALONE_MAIN) |
| 78 | + using mcal_sram_uintptr_t = std::uintptr_t; |
| 79 | + #endif |
| 80 | + |
| 81 | + constexpr auto pi_spigot_input_start_address = static_cast<mcal_sram_uintptr_t>(UINT8_C(0)); |
| 82 | + |
| 83 | + #if !defined(PI_CRUNCH_METAL_DISABLE_IOSTREAM) |
| 84 | + using pi_spigot_input_container_type = std::array<std::uint32_t, |
| 85 | + pi_spigot_type::input_static_size>; |
| 86 | + #else |
| 87 | + using pi_spigot_input_container_type = mcal::memory::sram::array<std::uint32_t, |
| 88 | + pi_spigot_type::input_static_size, |
| 89 | + pi_spigot_input_start_address>; |
| 90 | + #endif |
| 91 | + |
| 92 | + pi_spigot_input_container_type pi_spigot_input; |
| 93 | +} // namespace local |
| 94 | + |
| 95 | +extern "C" |
| 96 | +{ |
| 97 | + auto mcal_led_toggle() -> void; |
| 98 | + |
| 99 | + auto pi_main() -> int; |
| 100 | + |
| 101 | + auto pi_led_toggle() -> void; |
| 102 | +} |
| 103 | + |
| 104 | +extern "C" |
| 105 | +auto pi_led_toggle() -> void |
| 106 | +{ |
| 107 | + ::mcal_led_toggle(); |
| 108 | +} |
| 109 | + |
| 110 | +extern auto pi_lcd_progress(const std::uint32_t pi_output_digits10) -> void; |
| 111 | +extern auto pi_count_of_calculations() -> std::uint32_t&; |
| 112 | + |
| 113 | +auto pi_main() -> int |
| 114 | +{ |
| 115 | + #if !defined(PI_CRUNCH_METAL_STANDALONE_MAIN) |
| 116 | + local::benchmark_port_type::toggle_pin(); |
| 117 | + #endif |
| 118 | + |
| 119 | + local::pi_spigot_instance.calculate(local::pi_spigot_input.data(), pi_lcd_progress, &local::pi_spigot_hash); |
| 120 | + |
| 121 | + ++pi_count_of_calculations(); |
| 122 | + |
| 123 | + // Check the hash result of the pi calculation. |
| 124 | + const auto hash_control = |
| 125 | + typename local::hash_type::result_type |
| 126 | + #if (PI_CRUNCH_METAL_PI_SPIGOT_DIGITS == PI_CRUNCH_METAL_PI_SPIGOT_USE_100_DIGITS) |
| 127 | + { |
| 128 | + 0x93U, 0xF1U, 0xB4U, 0xEAU, 0xABU, 0xCBU, 0xC9U, 0xB9U, |
| 129 | + 0x0CU, 0x93U, 0x93U, 0x24U, 0xF7U, 0x85U, 0x13U, 0x2EU, |
| 130 | + 0xDFU, 0x3BU, 0xF2U, 0x01U |
| 131 | + }; |
| 132 | + #elif (PI_CRUNCH_METAL_PI_SPIGOT_DIGITS == PI_CRUNCH_METAL_PI_SPIGOT_USE_1K_DIGITS) |
| 133 | + { |
| 134 | + 0xA0U, 0x92U, 0x47U, 0x1FU, 0xD5U, 0xFEU, 0x41U, 0x51U, |
| 135 | + 0x20U, 0xE7U, 0xDDU, 0x18U, 0x5BU, 0x93U, 0x0DU, 0x05U, |
| 136 | + 0x3AU, 0x86U, 0xF1U, 0x7EU |
| 137 | + }; |
| 138 | + #elif (PI_CRUNCH_METAL_PI_SPIGOT_DIGITS == PI_CRUNCH_METAL_PI_SPIGOT_USE_10K_DIGITS) |
| 139 | + { |
| 140 | + // 10001: 4BDF69A5FF25B9BED6BA9802BD2A68306FAB71EC |
| 141 | + 0x4BU, 0xDFU, 0x69U, 0xA5U, 0xFFU, 0x25U, 0xB9U, 0xBEU, |
| 142 | + 0xD6U, 0xBAU, 0x98U, 0x02U, 0xBDU, 0x2AU, 0x68U, 0x30U, |
| 143 | + 0x6FU, 0xABU, 0x71U, 0xECU |
| 144 | + }; |
| 145 | + #elif (PI_CRUNCH_METAL_PI_SPIGOT_DIGITS == PI_CRUNCH_METAL_PI_SPIGOT_USE_100K_DIGITS) |
| 146 | + { |
| 147 | + // 100001: D9D56240EB6B626A8FE179E3054D332F1767071D |
| 148 | + 0xD9U, 0xD5U, 0x62U, 0x40U, 0xEBU, 0x6BU, 0x62U, 0x6AU, |
| 149 | + 0x8FU, 0xE1U, 0x79U, 0xE3U, 0x05U, 0x4DU, 0x33U, 0x2FU, |
| 150 | + 0x17U, 0x67U, 0x07U, 0x1DU |
| 151 | + }; |
| 152 | + #endif |
| 153 | + |
| 154 | + using local_hash_type = local::hash_type; |
| 155 | + using local_hash_result_type = typename local_hash_type::result_type; |
| 156 | + |
| 157 | + local_hash_result_type hash_result { }; |
| 158 | + |
| 159 | + local::pi_spigot_hash.get_result(hash_result.data()); |
| 160 | + |
| 161 | + const bool result_is_ok { std::equal(hash_result.cbegin(), hash_result.cend(), hash_control.cbegin()) }; |
| 162 | + |
| 163 | + const int result_of_pi_main { (result_is_ok ? static_cast<int>(INT8_C(0)) : static_cast<int>(INT8_C(-1))) }; |
| 164 | + |
| 165 | + return result_of_pi_main; |
| 166 | +} |
| 167 | + |
| 168 | +#if defined(PI_CRUNCH_METAL_STANDALONE_MAIN) |
| 169 | + |
| 170 | +auto pi_count_of_calculations() -> std::uint32_t&; |
| 171 | +auto pi_lcd_progress(const std::uint32_t pi_output_digits10) -> void; |
| 172 | + |
| 173 | +auto pi_count_of_calculations() -> std::uint32_t& |
| 174 | +{ |
| 175 | + static std::uint32_t my_count { }; |
| 176 | + |
| 177 | + return my_count; |
| 178 | +} |
| 179 | + |
| 180 | +auto pi_lcd_progress(const std::uint32_t pi_output_digits10) -> void { static_cast<void>(pi_output_digits10); } |
| 181 | + |
| 182 | +extern "C" |
| 183 | +{ |
| 184 | + auto mcal_init () -> void; |
| 185 | + auto mcal_led_toggle() -> void; |
| 186 | + |
| 187 | + auto mcal_init () -> void { } |
| 188 | + auto mcal_led_toggle() -> void { } |
| 189 | +} |
| 190 | + |
| 191 | +auto main() -> int |
| 192 | +{ |
| 193 | + ::mcal_init(); |
| 194 | + |
| 195 | + #if !defined(PI_CRUNCH_METAL_DISABLE_IOSTREAM) |
| 196 | + std::stringstream strm { }; |
| 197 | + |
| 198 | + strm << "Begin pi spigot calculation...\n"; |
| 199 | + |
| 200 | + const auto start = std::chrono::high_resolution_clock::now(); |
| 201 | + #endif |
| 202 | + |
| 203 | + const int result_pi_main { ::pi_main() }; |
| 204 | + |
| 205 | + const bool result_is_ok { (result_pi_main == 0) }; |
| 206 | + |
| 207 | + #if !defined(PI_CRUNCH_METAL_DISABLE_IOSTREAM) |
| 208 | + |
| 209 | + const auto stop = std::chrono::high_resolution_clock::now(); |
| 210 | + |
| 211 | + const float |
| 212 | + elapsed |
| 213 | + { |
| 214 | + static_cast<float> |
| 215 | + ( |
| 216 | + static_cast<float>(std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count()) |
| 217 | + / 1000.0F |
| 218 | + ) |
| 219 | + }; |
| 220 | + |
| 221 | + strm << "digits10: " << local::pi_spigot_type::result_digit() << '\n' |
| 222 | + << "time: " << std::fixed << std::setprecision(2) << elapsed << "s\n" |
| 223 | + << "result_is_ok: " << std::boolalpha << result_is_ok; |
| 224 | + |
| 225 | + std::cout << strm.str() << std::endl; |
| 226 | + #endif |
| 227 | + |
| 228 | + const int result_of_main { (result_is_ok ? static_cast<int>(INT8_C(0)) : static_cast<int>(INT8_C(-1))) }; |
| 229 | + |
| 230 | + return result_of_main; |
| 231 | +} |
| 232 | + |
| 233 | +#else |
| 234 | + |
| 235 | +auto pi_calc_dummy() -> void; |
| 236 | + |
| 237 | +auto pi_calc_dummy() -> void { } |
| 238 | + |
| 239 | +#endif |
0 commit comments