-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split mathhelpers.hpp into dedicated include files for each functiona…
…lity
- Loading branch information
Showing
21 changed files
with
281 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#pragma once | ||
|
||
#include <array> | ||
#include <cstdint> | ||
#include <limits> | ||
|
||
namespace cct { | ||
/// constexpr and integral version of math.power. | ||
/// Taken from https://gist.github.com/orlp/3551590 | ||
constexpr int64_t ipow(int64_t base, uint8_t exp) noexcept { | ||
constexpr uint8_t highest_bit_set[] = {0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, | ||
4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, | ||
5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, | ||
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, | ||
6, 6, 6, 255, // anything past 63 is a guaranteed overflow with base > 1 | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; | ||
|
||
int64_t result = 1; | ||
|
||
switch (highest_bit_set[exp]) { | ||
case 255: // we use 255 as an overflow marker and return 0 on overflow/underflow | ||
return base == 1 ? 1 : (base == -1 ? (1 - 2 * (exp & 1)) : 0); | ||
case 6: | ||
if ((exp & 1U) != 0) { | ||
result *= base; | ||
} | ||
exp >>= 1; | ||
base *= base; | ||
[[fallthrough]]; | ||
case 5: | ||
if ((exp & 1U) != 0) { | ||
result *= base; | ||
} | ||
exp >>= 1; | ||
base *= base; | ||
[[fallthrough]]; | ||
case 4: | ||
if ((exp & 1U) != 0) { | ||
result *= base; | ||
} | ||
exp >>= 1; | ||
base *= base; | ||
[[fallthrough]]; | ||
case 3: | ||
if ((exp & 1U) != 0) { | ||
result *= base; | ||
} | ||
exp >>= 1; | ||
base *= base; | ||
[[fallthrough]]; | ||
case 2: | ||
if ((exp & 1U) != 0) { | ||
result *= base; | ||
} | ||
exp >>= 1; | ||
base *= base; | ||
[[fallthrough]]; | ||
case 1: | ||
if ((exp & 1U) != 0) { | ||
result *= base; | ||
} | ||
[[fallthrough]]; | ||
default: | ||
return result; | ||
} | ||
} | ||
|
||
/// Optimization of ipow(10, uint8_t exp) | ||
constexpr int64_t ipow10(uint8_t exp) noexcept { | ||
constexpr const int64_t kPow10Table[] = {1LL, | ||
10LL, | ||
100LL, | ||
1000LL, | ||
10000LL, | ||
100000LL, | ||
1000000LL, | ||
10000000LL, | ||
100000000LL, | ||
1000000000LL, | ||
10000000000LL, | ||
100000000000LL, | ||
1000000000000LL, | ||
10000000000000LL, | ||
100000000000000LL, | ||
1000000000000000LL, | ||
10000000000000000LL, | ||
100000000000000000LL, | ||
1000000000000000000LL}; | ||
return exp < sizeof(kPow10Table) / sizeof(kPow10Table[0]) ? kPow10Table[exp] : std::numeric_limits<int64_t>::max(); | ||
} | ||
|
||
} // namespace cct |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
|
||
#include <concepts> | ||
|
||
#include "ndigits.hpp" | ||
|
||
namespace cct { | ||
|
||
/// Count the number of digits including the possible minus sign for negative integrals. | ||
constexpr int nchars(std::signed_integral auto n) noexcept { return ndigits(n) + static_cast<int>(n < 0); } | ||
|
||
/// Synonym of ndigits for unsigned types. | ||
constexpr int nchars(std::unsigned_integral auto n) noexcept { return ndigits(n); } | ||
|
||
} // namespace cct |
Oops, something went wrong.