|
22 | 22 | #endif |
23 | 23 |
|
24 | 24 | #include <boost/int128/int128.hpp> |
| 25 | +#include <boost/int128/numeric.hpp> |
25 | 26 | #include <chrono> |
26 | 27 | #include <random> |
27 | 28 | #include <vector> |
|
31 | 32 | #include <cmath> |
32 | 33 | #include <cstring> |
33 | 34 | #include <functional> |
| 35 | +#include <numeric> |
| 36 | + |
| 37 | +#if defined(__cpp_lib_gcd_lcm) && __cpp_lib_gcd_lcm >= 201606L |
| 38 | +# define BOOST_INT128_BENCHMARK_BUILTIN_GCD |
| 39 | +#endif |
34 | 40 |
|
35 | 41 | constexpr unsigned N = 20'000'000; |
36 | 42 | constexpr unsigned K = 5; |
@@ -311,6 +317,34 @@ BOOST_INT128_NO_INLINE void test_two_element_operation(const std::vector<T>& dat |
311 | 317 | std::cerr << operation << "<" << std::left << std::setw(11) << type << ">: " << std::setw( 10 ) << ( t2 - t1 ) / 1us << " us (s=" << s << ")\n"; |
312 | 318 | } |
313 | 319 |
|
| 320 | +template <typename T> |
| 321 | +BOOST_INT128_NO_INLINE void test_gcd(const std::vector<T>& data_vec, const char* type) |
| 322 | +{ |
| 323 | + #ifdef BOOST_INT128_BENCHMARK_BUILTIN_GCD |
| 324 | + using std::gcd; |
| 325 | + #endif |
| 326 | + |
| 327 | + using boost::multiprecision::gcd; |
| 328 | + using boost::int128::gcd; |
| 329 | + |
| 330 | + const auto t1 = std::chrono::steady_clock::now(); |
| 331 | + std::size_t s = 0; // discard variable |
| 332 | + |
| 333 | + for (std::size_t k {}; k < K; ++k) |
| 334 | + { |
| 335 | + for (std::size_t i {}; i < data_vec.size() - 1U; ++i) |
| 336 | + { |
| 337 | + const auto val1 = data_vec[i]; |
| 338 | + const auto val2 = data_vec[i + 1]; |
| 339 | + s += static_cast<std::size_t>(gcd(val1, val2)); |
| 340 | + } |
| 341 | + } |
| 342 | + |
| 343 | + const auto t2 = std::chrono::steady_clock::now(); |
| 344 | + |
| 345 | + std::cerr << "gcd" << "<" << std::left << std::setw(11) << type << ">: " << std::setw( 10 ) << ( t2 - t1 ) / 1us << " us (s=" << s << ")\n"; |
| 346 | +} |
| 347 | + |
314 | 348 | int main() |
315 | 349 | { |
316 | 350 | using namespace boost::int128::detail; |
@@ -385,6 +419,15 @@ int main() |
385 | 419 | test_two_element_operation(mp_vector, std::modulus<>(), "mod", "mp::u128"); |
386 | 420 |
|
387 | 421 | std::cerr << std::endl; |
| 422 | + |
| 423 | + #if (defined(BOOST_INT128_HAS_INT128) || defined(BOOST_INT128_HAS_MSVC_INTERNAL_I128)) && defined(BOOST_INT128_BENCHMARK_BUILTIN_GCD) |
| 424 | + test_gcd(builtin_vector, "Builtin"); |
| 425 | + #endif |
| 426 | + |
| 427 | + test_gcd(library_vector, "Library"); |
| 428 | + test_gcd(mp_vector, "mp::u128"); |
| 429 | + |
| 430 | + std::cerr << std::endl; |
388 | 431 | } |
389 | 432 | // Single word operations |
390 | 433 | { |
|
0 commit comments