Skip to content

Commit

Permalink
Merge pull request #277 from ckormanyos/optimize_examples
Browse files Browse the repository at this point in the history
Optimize examples
  • Loading branch information
ckormanyos authored Nov 1, 2023
2 parents 87b0196 + 6b957a6 commit 0f0c07c
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 41 deletions.
78 changes: 61 additions & 17 deletions examples/example011_trig_trapezoid_integral.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
///////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2021 - 2022. //
// Copyright Christopher Kormanyos 2021 - 2023. //
// 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 @@ -174,13 +174,27 @@ namespace example011_trig

auto sin(const dec51_t& x) -> dec51_t // NOLINT(misc-no-recursion)
{
dec51_t s { static_cast<unsigned>(UINT8_C(0)) };
dec51_t s { };

if(x < static_cast<int>(INT8_C(0)))
using local_limb_type = typename dec51_t::limb_type;

const auto n_cmp =
static_cast<int>
(
x.cmp
(
dec51_t::from_lst
(
{ static_cast<local_limb_type>(UINT8_C(0)) }
)
)
);

if(n_cmp < static_cast<int>(INT8_C(0)))
{
s = -sin(-x);
s = sin(dec51_t(x).negate()).negate();
}
else if(x > static_cast<int>(INT8_C(0)))
else if(n_cmp > static_cast<int>(INT8_C(0)))
{
// Perform argument reduction and subsequent scaling of the result.

Expand All @@ -198,13 +212,13 @@ namespace example011_trig
const auto k = static_cast<unsigned>(x / my_pi_half);
const auto n = static_cast<unsigned>(k % static_cast<unsigned>(UINT8_C(4)));

dec51_t r = x - (my_pi_half * k);

auto n_angle_identity = static_cast<unsigned>(UINT8_C(0));
auto r = x - (my_pi_half * k);

static const auto two_tenths = dec51_t(static_cast<unsigned>(UINT8_C(2))) / static_cast<unsigned>(UINT8_C(10));

// Reduce the argument with factors of three until it is less than 2/10.
auto n_angle_identity = static_cast<unsigned>(UINT8_C(0));

while(r > two_tenths) // NOLINT(altera-id-dependent-backward-branch)
{
r /= static_cast<unsigned>(UINT8_C(3));
Expand All @@ -230,7 +244,7 @@ namespace example011_trig
{
static_cast<void>(t);

s = (s * static_cast<unsigned>(UINT8_C(3))) - (((s * s) * s) * static_cast<unsigned>(UINT8_C(4)));
s *= (static_cast<unsigned>(UINT8_C(3)) - ((s * s) * static_cast<unsigned>(UINT8_C(4))));
}

if(s.isneg()) { s.negate(); }
Expand All @@ -239,19 +253,41 @@ namespace example011_trig

if(b_neg) { s.negate(); }
}
else
{
s =
dec51_t::from_lst
(
{ static_cast<local_limb_type>(UINT8_C(0)) }
);
}

return s;
}

auto cos(const dec51_t& x) -> dec51_t // NOLINT(misc-no-recursion)
{
dec51_t c { static_cast<unsigned>(UINT8_C(1)) };
dec51_t c { };

if(x < static_cast<int>(INT8_C(0)))
using local_limb_type = typename dec51_t::limb_type;

const auto n_cmp =
static_cast<int>
(
x.cmp
(
dec51_t::from_lst
(
{ static_cast<local_limb_type>(UINT8_C(0)) }
)
)
);

if(n_cmp < static_cast<int>(INT8_C(0)))
{
c = cos(-x);
c = cos(dec51_t(x).negate());
}
else if(x > static_cast<int>(INT8_C(0)))
else if(n_cmp > static_cast<int>(INT8_C(0)))
{
// Perform argument reduction and subsequent scaling of the result.

Expand All @@ -269,13 +305,13 @@ namespace example011_trig
const auto k = static_cast<unsigned>(x / my_pi_half);
const auto n = static_cast<unsigned>(k % static_cast<unsigned>(UINT8_C(4)));

dec51_t r = x - (my_pi_half * k);

auto n_angle_identity = static_cast<unsigned>(UINT8_C(0));
auto r = x - (my_pi_half * k);

static const auto two_tenths = dec51_t(static_cast<unsigned>(UINT8_C(2))) / static_cast<unsigned>(UINT8_C(10));

// Reduce the argument with factors of three until it is less than 2/10.
auto n_angle_identity = static_cast<unsigned>(UINT8_C(0));

while(r > two_tenths) // NOLINT(altera-id-dependent-backward-branch)
{
r /= static_cast<unsigned>(UINT8_C(3));
Expand All @@ -302,7 +338,7 @@ namespace example011_trig
{
static_cast<void>(t);

c = (((c * c) * c) * static_cast<unsigned>(UINT8_C(4))) - (c * static_cast<unsigned>(UINT8_C(3)));
c *= (((c * c) * static_cast<unsigned>(UINT8_C(4))) - static_cast<unsigned>(UINT8_C(3)));
}

if(c.isneg()) { c.negate(); }
Expand All @@ -311,6 +347,14 @@ namespace example011_trig

if(b_neg) { c.negate(); }
}
else
{
c =
dec51_t::from_lst
(
{ static_cast<local_limb_type>(UINT8_C(1)) }
);
}

return c;
}
Expand Down
54 changes: 30 additions & 24 deletions examples/example012_rational_floor_ceil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

#include <algorithm>
#include <cstdint>
#include <ctime>
#include <iostream>
#include <random>

#include <examples/example_decwide_t.h>
#include <math/wide_decimal/decwide_t.h>
#include <util/utility/util_baselexical_cast.h>
#include <util/utility/util_pseudorandom_time_point_seed.h>

#if defined(__clang__)
#if defined __has_feature && (__has_feature(thread_sanitizer) || __has_feature(address_sanitizer))
Expand All @@ -29,7 +29,7 @@ namespace example012_rational
{
auto dist_sign() -> unsigned
{
static std::minstd_rand0 my_eng(static_cast<std::minstd_rand0::result_type>(std::clock()));
static std::minstd_rand0 my_eng(util::util_pseudorandom_time_point_seed::value<std::minstd_rand0::result_type>());

static std::uniform_int_distribution<unsigned> my_dist_sign
(
Expand All @@ -40,6 +40,18 @@ namespace example012_rational
return my_dist_sign(my_eng);
}

constexpr auto lo_index_min = static_cast<std::int32_t>(INT32_C(101));
#if !defined(DECWIDE_T_REDUCE_TEST_DEPTH)
constexpr auto lo_index_max = static_cast<std::int32_t>(INT32_C(1010));
#else
constexpr auto lo_index_max = static_cast<std::int32_t>(INT32_C(210));
#endif
constexpr auto hi_index_min = static_cast<std::int32_t>(INT32_C(10001));
constexpr auto hi_index_max = static_cast<std::int32_t>(INT32_C(40010));

static_assert(hi_index_min < hi_index_max, "Error hi_index_min must be less than hi_index_max");
static_assert(lo_index_min < lo_index_max, "Error lo_index_min must be less than lo_index_max");

template<typename DecimalType>
auto test_rational_floor() -> bool
{
Expand All @@ -52,19 +64,16 @@ namespace example012_rational

bool result_is_ok = true;

#if !defined(DECWIDE_T_REDUCE_TEST_DEPTH)
constexpr auto lo_index_max = static_cast<std::int32_t>(INT32_C(1010));
#else
constexpr auto lo_index_max = static_cast<std::int32_t>(INT32_C(310));
#endif
constexpr auto hi_index_max = static_cast<std::int32_t>(INT32_C(100010));

for(auto lo_index = static_cast<std::int32_t>(INT32_C(101)); lo_index < lo_index_max; lo_index += static_cast<std::int32_t>(INT32_C(7)))
for(auto lo_index = lo_index_min;
lo_index < lo_index_max; // NOLINT(altera-id-dependent-backward-branch)
lo_index = static_cast<std::int32_t>(lo_index + static_cast<std::int32_t>(INT32_C(7))))
{
for(auto hi_index = static_cast<std::int32_t>(INT32_C(10001)); hi_index < hi_index_max; hi_index += static_cast<std::int32_t>(INT32_C(17)))
for(auto hi_index = hi_index_min;
hi_index < hi_index_max; // NOLINT(altera-id-dependent-backward-branch)
hi_index = static_cast<std::int32_t>(hi_index + static_cast<std::int32_t>(INT32_C(17))))
{
const auto lo_is_neg = (static_cast<unsigned>(dist_sign() % 2U) == 0U);
const auto hi_is_neg = (static_cast<unsigned>(dist_sign() % 2U) == 0U);
const auto lo_is_neg = static_cast<unsigned>(static_cast<unsigned>(dist_sign() % 2U) == static_cast<unsigned>(UINT8_C(0)));
const auto hi_is_neg = static_cast<unsigned>(static_cast<unsigned>(dist_sign() % 2U) == static_cast<unsigned>(UINT8_C(0)));

const auto lo = static_cast<std::int32_t>((!lo_is_neg) ? lo_index : -lo_index);
const auto hi = static_cast<std::int32_t>((!hi_is_neg) ? hi_index : -hi_index);
Expand Down Expand Up @@ -98,19 +107,16 @@ namespace example012_rational

bool result_is_ok = true;

#if !defined(DECWIDE_T_REDUCE_TEST_DEPTH)
constexpr auto lo_index_max = static_cast<std::int32_t>(INT32_C(1010));
#else
constexpr auto lo_index_max = static_cast<std::int32_t>(INT32_C(310));
#endif
constexpr auto hi_index_max = static_cast<std::int32_t>(INT32_C(100010));

for(auto lo_index = static_cast<std::int32_t>(INT32_C(101)); lo_index < lo_index_max; lo_index += static_cast<std::int32_t>(INT32_C(7)))
for(auto lo_index = lo_index_min;
lo_index < lo_index_max; // NOLINT(altera-id-dependent-backward-branch)
lo_index = static_cast<std::int32_t>(lo_index + static_cast<std::int32_t>(INT32_C(7))))
{
for(auto hi_index = static_cast<std::int32_t>(INT32_C(10001)); hi_index < hi_index_max; hi_index += static_cast<std::int32_t>(INT32_C(17)))
for(auto hi_index = hi_index_min;
hi_index < hi_index_max; // NOLINT(altera-id-dependent-backward-branch)
hi_index = static_cast<std::int32_t>(hi_index + static_cast<std::int32_t>(INT32_C(17))))
{
const auto lo_is_neg = (static_cast<unsigned>(dist_sign() % 2U) == 0U);
const auto hi_is_neg = (static_cast<unsigned>(dist_sign() % 2U) == 0U);
const auto lo_is_neg = static_cast<unsigned>(static_cast<unsigned>(dist_sign() % 2U) == static_cast<unsigned>(UINT8_C(0)));
const auto hi_is_neg = static_cast<unsigned>(static_cast<unsigned>(dist_sign() % 2U) == static_cast<unsigned>(UINT8_C(0)));

const auto lo = static_cast<std::int32_t>((!lo_is_neg) ? lo_index : -lo_index);
const auto hi = static_cast<std::int32_t>((!hi_is_neg) ? hi_index : -hi_index);
Expand Down

0 comments on commit 0f0c07c

Please sign in to comment.