Skip to content

Commit

Permalink
Merge pull request #525 from beached/v2
Browse files Browse the repository at this point in the history
Added digit counting of unsigned integers
  • Loading branch information
beached authored Aug 10, 2024
2 parents fcd352a + c765c92 commit e9e9f87
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
cmake_minimum_required( VERSION 3.14 )

project( "daw-header-libraries"
VERSION "2.106.2"
VERSION "2.107.0"
DESCRIPTION "Various headers"
HOMEPAGE_URL "https://github.com/beached/header_libraries"
LANGUAGES C CXX
Expand Down
29 changes: 29 additions & 0 deletions include/daw/daw_cxmath.h
Original file line number Diff line number Diff line change
Expand Up @@ -1340,4 +1340,33 @@ namespace daw::cxmath {
DAW_ATTRIB_INLINE constexpr auto to_signed( Integer value ) {
return static_cast<daw::make_signed_t<Integer>>( value );
}

inline constexpr std::uint64_t powers_of_ten[19] = {
10ull,
100ull,
1'000ull,
10'000ull,
100'000ull,
1'000'000ull,
10'000'000ull,
100'000'000ull,
1'000'000'000ull,
10'000'000'000ull,
100'000'000'000ull,
1'000'000'000'000ull,
10'000'000'000'000ull,
100'000'000'000'000ull,
1'000'000'000'000'000ull,
10'000'000'000'000'000ull,
100'000'000'000'000'000ull,
1'000'000'000'000'000'000ull,
10'000'000'000'000'000'000ull };

constexpr int count_digits( std::uint64_t value ) {
auto b = -( value > 0 ) & ( 63 - count_leading_zeroes( value ) );
auto a = ( b * 77 ) / 256;
return static_cast<int>( 1 + a + ( value >= powers_of_ten[a] ) );
}
static_assert( count_digits( 1'000'000ULL ) == 7 );
} // namespace daw::cxmath

0 comments on commit e9e9f87

Please sign in to comment.