Skip to content

Commit caac5c8

Browse files
authored
Merge pull request #542 from beached/v2
Updates around bit_count and arith_traits
2 parents 6cdd89c + 2c1decf commit caac5c8

44 files changed

Lines changed: 524 additions & 662 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
cmake_minimum_required( VERSION 3.14 )
1010

1111
project( "daw-header-libraries"
12-
VERSION "2.123.1"
12+
VERSION "2.123.2"
1313
DESCRIPTION "Various headers"
1414
HOMEPAGE_URL "https://github.com/beached/header_libraries"
1515
LANGUAGES C CXX

include/daw/daw_arith_traits.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ namespace daw {
193193
inline constexpr bool is_system_integral_v = is_system_integral<T>::value;
194194

195195
template<typename T, std::size_t BitSize>
196-
using is_same_size = std::bool_constant<( ( sizeof( T ) * 8 ) == BitSize )>;
196+
using is_same_size = std::bool_constant<( bit_count_v<T> == BitSize )>;
197197

198198
template<typename T, std::size_t BitSize>
199199
inline constexpr bool is_same_size_v = is_same_size<T, BitSize>::value;

include/daw/daw_benchmark.h

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88

99
#pragma once
1010

11-
#include "ciso646.h"
12-
#include "daw_do_not_optimize.h"
13-
#include "daw_expected.h"
14-
#include "daw_move.h"
15-
#include "daw_string_view.h"
11+
#include "daw/ciso646.h"
12+
#include "daw/daw_arith_traits.h"
13+
#include "daw/daw_do_not_optimize.h"
14+
#include "daw/daw_expected.h"
15+
#include "daw/daw_move.h"
16+
#include "daw/daw_string_view.h"
1617

1718
#include <array>
1819
#include <chrono>
@@ -209,7 +210,7 @@ namespace daw {
209210
static_assert( std::is_invocable_v<Test, Args...>,
210211
"Unable to call Test with provided Args" );
211212

212-
double base_time = ( std::numeric_limits<double>::max )( );
213+
double base_time = max_value<double>;
213214
{
214215
for( size_t n = 0; n < 1000; ++n ) {
215216
daw::do_not_optimize( args... );
@@ -230,7 +231,7 @@ namespace daw {
230231
}
231232
}
232233
}
233-
double min_time = ( std::numeric_limits<double>::max )( );
234+
double min_time = max_value<double>;
234235
double max_time = 0.0;
235236

236237
auto const total_start = std::chrono::steady_clock::now( );
@@ -321,7 +322,7 @@ namespace daw {
321322
"Validator must be callable with the results of Function" );
322323

323324
auto results = std::array<double, Runs>{ };
324-
double base_time = ( std::numeric_limits<double>::max )( );
325+
double base_time = max_value<double>;
325326
{
326327
for( size_t n = 0; n < 1000; ++n ) {
327328
int a = 0;
@@ -371,7 +372,7 @@ namespace daw {
371372
static_assert( Runs > 0 );
372373
static_assert( std::is_invocable_v<Test, Args...>,
373374
"Unable to call Test with provided Args" );
374-
double base_time = ( std::numeric_limits<double>::max )( );
375+
double base_time = max_value<double>;
375376
{
376377
for( size_t n = 0; n < 1000; ++n ) {
377378
intmax_t a = 0;
@@ -390,7 +391,7 @@ namespace daw {
390391
}
391392
}
392393
}
393-
double min_time = ( std::numeric_limits<double>::max )( );
394+
double min_time = max_value<double>;
394395
double max_time = 0.0;
395396

396397
auto const total_start = std::chrono::steady_clock::now( );
@@ -481,8 +482,7 @@ namespace daw {
481482
static_assert( std::is_invocable_v<Function, Args...>,
482483
"Unable to call Test with provided Args" );
483484

484-
auto base_time =
485-
std::chrono::nanoseconds( ( std::numeric_limits<long long>::max )( ) );
485+
auto base_time = std::chrono::nanoseconds( max_value<long long> );
486486
{
487487
for( size_t n = 0; n < 1000; ++n ) {
488488
daw::do_not_optimize( args... );
@@ -538,8 +538,7 @@ namespace daw {
538538
std::is_invocable_v<Validator, std::invoke_result_t<Function, Args...>>,
539539
"Validator must be callable with the results of Function" );
540540

541-
auto base_time =
542-
std::chrono::nanoseconds( ( std::numeric_limits<long long>::max )( ) );
541+
auto base_time = std::chrono::nanoseconds( max_value<long long> );
543542
{
544543
for( size_t n = 0; n < 1000; ++n ) {
545544
daw::do_not_optimize( args... );

include/daw/daw_bit.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,43 @@
88

99
#pragma once
1010

11-
#include "ciso646.h"
12-
#include "daw_traits.h"
13-
#include "daw_utility.h"
11+
#include "daw/ciso646.h"
12+
#include "daw/daw_arith_traits.h"
13+
#include "daw/daw_bit_count.h"
14+
#include "daw/daw_traits.h"
15+
#include "daw/daw_utility.h"
1416

17+
#include <cstddef>
1518
#include <limits>
19+
#include <type_traits>
1620

1721
namespace daw {
1822
template<typename T>
1923
[[deprecated( "Use mask_msb" )]] constexpr T
2024
get_left_mask( size_t left_zero_bits ) noexcept {
21-
return static_cast<T>( ( std::numeric_limits<T>::max )( ) >>
22-
left_zero_bits );
25+
return static_cast<T>( max_value<T> >> left_zero_bits );
2326
}
2427

2528
template<typename T>
2629
constexpr T mask_msb( size_t bit_count ) noexcept {
2730
if( bit_count >= daw::bsizeof<T> ) {
2831
return static_cast<T>( 0 );
2932
}
30-
return static_cast<T>( ( std::numeric_limits<T>::max )( ) >> bit_count );
33+
return static_cast<T>( max_value<T> >> bit_count );
3134
}
3235

3336
template<typename T>
3437
[[deprecated( "Use mask_lsb" )]] constexpr T
3538
get_right_mask( size_t right_zero_bits ) noexcept {
36-
return static_cast<T>( ( std::numeric_limits<T>::max )( )
37-
<< right_zero_bits );
39+
return static_cast<T>( max_value<T> << right_zero_bits );
3840
}
3941

4042
template<typename T>
4143
constexpr T mask_lsb( size_t bit_count ) noexcept {
4244
if( bit_count >= daw::bsizeof<T> ) {
4345
return static_cast<T>( 0 );
4446
}
45-
return static_cast<T>( ( std::numeric_limits<T>::max )( ) << bit_count );
47+
return static_cast<T>( max_value<T> << bit_count );
4648
}
4749

4850
template<typename Bit, typename... Bits>

include/daw/daw_bit_count.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) Darrell Wright
2+
//
3+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
4+
// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
5+
//
6+
// Official repository: https://github.com/beached/header_libraries
7+
//
8+
9+
#pragma once
10+
11+
#include <climits>
12+
#include <cstddef>
13+
14+
namespace daw {
15+
template<typename T>
16+
inline constexpr std::size_t bit_count_v = sizeof( T ) * CHAR_BIT;
17+
}

include/daw/daw_bit_queues.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88

99
#pragma once
1010

11-
#include "ciso646.h"
12-
#include "daw_bit.h"
13-
#include "daw_endian.h"
14-
#include "daw_exception.h"
15-
#include "daw_move.h"
16-
#include "traits/daw_traits_conditional.h"
11+
#include "daw/ciso646.h"
12+
#include "daw/daw_arith_traits.h"
13+
#include "daw/daw_bit.h"
14+
#include "daw/daw_bit_count.h"
15+
#include "daw/daw_endian.h"
16+
#include "daw/daw_exception.h"
17+
#include "daw/daw_move.h"
18+
#include "daw/traits/daw_traits_conditional.h"
1719

1820
#include <cstdint>
1921

@@ -32,14 +34,14 @@ namespace daw {
3234
static_assert( std::is_unsigned_v<queue_type> and
3335
std::is_unsigned_v<value_type>,
3436
"Only unsigned integral types are supported" );
35-
size_t m_size = 0;
37+
std::size_t m_size = 0;
3638
queue_type m_queue = 0;
3739

3840
public:
3941
explicit basic_bit_queue( ) = default;
4042

4143
constexpr explicit basic_bit_queue( queue_type v ) noexcept
42-
: m_size( sizeof( m_queue ) * 8 )
44+
: m_size( bit_count_v<queue_type> )
4345
, m_queue( std::move( v ) ) {}
4446

4547
[[nodiscard]] constexpr size_t size( ) const noexcept {
@@ -54,8 +56,8 @@ namespace daw {
5456
return 0 == m_size;
5557
}
5658

57-
[[nodiscard]] constexpr size_t capacity( ) const noexcept {
58-
return sizeof( m_queue ) * 8;
59+
[[nodiscard]] static DAW_CONSTEVAL size_t capacity( ) {
60+
return bit_count_v<queue_type>;
5961
}
6062

6163
[[nodiscard]] constexpr bool full( ) const noexcept {
@@ -78,14 +80,14 @@ namespace daw {
7880

7981
public:
8082
constexpr void push_back( value_type value,
81-
size_t const bits = sizeof( value_type ) * 8 ) {
83+
size_t const bits = bit_count_v<value_type> ) {
8284
daw::exception::dbg_throw_on_false(
8385
( capacity( ) - m_size ) >= bits,
8486
"Not enough bits to hold value pushed" );
8587

8688
value &= mask_msb<value_type>(
87-
sizeof( value_type ) * 8 -
88-
bits ); // get_left_mask<value_type>( sizeof( value_type ) * 8 - bits );
89+
bit_count_v<value_type> -
90+
bits ); // get_left_mask<value_type>( bit_count_v<value_type> - bits );
8991
m_queue <<= bits;
9092
m_queue |= source_to_native_endian( value, BitQueueLSB{ } );
9193
m_size += bits;

include/daw/daw_bit_stream.h

Lines changed: 15 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,60 +8,17 @@
88

99
#pragma once
1010

11-
#include "ciso646.h"
12-
#include "daw_bit_queues.h"
13-
#include "daw_exception.h"
11+
#include "daw/ciso646.h"
12+
#include "daw/daw_bit_count.h"
13+
#include "daw/daw_bit_queues.h"
14+
#include "daw/daw_exception.h"
1415

1516
#include <cstdint>
1617
#include <iterator>
1718
#include <stdexcept>
1819
#include <type_traits>
1920

2021
namespace daw {
21-
/*template<typename InputIteratorF, typename InputIteratorL> class bit_stream;
22-
23-
template<typename BitStream>
24-
struct bit_stream_iterator: public std::input_iterator_tag {
25-
using value_type = typename
26-
std::iterator_traits<InputIteratorF>::value_type; using difference_type =
27-
typename std::iterator_traits<InputIteratorF>::difference_type; using
28-
reference = typename std::iterator_traits<InputIteratorF>::reference; using
29-
pointer = typename std::iterator_traits<InputIteratorF>::pointer; private:
30-
BitStream * m_bit_stream;
31-
bool m_is_end;
32-
33-
bool at_end( ) const {
34-
return !m_bit_stream->valid( );
35-
}
36-
37-
template<typename InputIteratorF, typename InputIteratorL> friend class
38-
bit_stream;
39-
40-
bit_stream_iterator( BitStream * bit_stream, bool is_end ):
41-
m_bit_stream{ bit_stream },
42-
m_is_end{ is_end } { }
43-
public:
44-
bit_stream_iterator( BitStream & bit_stream ):
45-
bit_stream_iterator{ &bit_stream, !bit_stream } { }
46-
47-
bool operator==( bit_stream_iterator const & rhs ) const {
48-
return (std::tie( m_bit_stream, m_bit_stream->m_first,
49-
m_bit_stream->m_left_overs ) == std::tie( rhs.m_bit_stream,
50-
rhs.m_bit_stream->m_first, rhs.m_bit_stream->m_left_overs )) or (is_end or
51-
!valid)
52-
}
53-
54-
bool operator!=( bit_stream_iterator const & rhs ) const {
55-
return std::tie( m_bit_stream, m_bit_stream->m_first,
56-
m_bit_stream->m_left_overs ) != std::tie( rhs.m_bit_stream,
57-
rhs.m_bit_stream->m_first, rhs.m_bit_stream->m_left_overs );
58-
}
59-
60-
value_type operator*( ) {
61-
return m_bit
62-
}
63-
};*/
64-
6522
template<typename InputIteratorF, typename InputIteratorL,
6623
typename BitQueueLSB = bit_queue_source_native_endian>
6724
struct bit_stream {
@@ -90,9 +47,9 @@ namespace daw {
9047
return valid( );
9148
}
9249

93-
value_type pop_bits( size_t num_bits = sizeof( value_type ) * 8 ) {
50+
value_type pop_bits( std::size_t num_bits = bit_count_v<value_type> ) {
9451
daw::exception::dbg_throw_on_true<std::overflow_error>(
95-
num_bits > sizeof( value_type ) * 8,
52+
num_bits > bit_count_v<value_type>,
9653
"Attempt to pop more bits than can fit into value" );
9754
daw::exception::dbg_throw_on_true<std::runtime_error>(
9855
num_bits == 0, "Attempt to pop 0 bits" );
@@ -123,15 +80,15 @@ namespace daw {
12380
template<typename T, typename BitStream>
12481
auto pop_value( BitStream &bs, size_t bits_needed ) {
12582
daw::exception::dbg_throw_on_false(
126-
bits_needed <= sizeof( T ) * 8,
83+
bits_needed <= bit_count_t<T>,
12784
"Attempt to extra more bits than can fit" );
12885

12986
using value_type = typename BitStream::value_type;
13087
T result = 0;
131-
while( bits_needed >= sizeof( value_type ) * 8 ) {
132-
result <<= sizeof( value_type ) * 8;
88+
while( bits_needed >= bit_count_v<value_type> ) {
89+
result <<= bit_count_v<value_type>;
13390
result |= bs.pop_bits( );
134-
bits_needed -= sizeof( value_type ) * 8;
91+
bits_needed -= bit_count_v<value_type>;
13592
}
13693
if( bits_needed > 0 ) {
13794
result <<= bits_needed;
@@ -142,28 +99,28 @@ namespace daw {
14299

143100
template<typename T, typename BitStream>
144101
auto pop_value( BitStream &bs ) {
145-
return pop_value<T>( bs, sizeof( T ) * 8 );
102+
return pop_value<T>( bs, bit_count_v<T> );
146103
}
147104

148105
template<typename BitStream>
149106
void skip_bits( BitStream &bs, size_t bits_needed ) {
150107
using value_type = typename BitStream::value_type;
151108

152-
while( bits_needed >= sizeof( value_type ) * 8 ) {
109+
while( bits_needed >= bit_count_v<value_type> ) {
153110
bs.pop_bits( );
154-
bits_needed -= sizeof( value_type ) * 8;
111+
bits_needed -= bit_count_v<value_type>;
155112
}
156113
}
157114

158115
template<typename BitStream>
159116
void skip_bytes( BitStream &bs, size_t bytes_needed ) {
160-
skip_bits( bs, bytes_needed * 8 );
117+
skip_bits( bs, bytes_needed * bit_count_v<char> );
161118
}
162119

163120
template<typename BitStream, typename TestValue>
164121
void skip_until( BitStream &bs, TestValue const &v, size_t bit_count ) {
165122
daw::exception::dbg_throw_on_true(
166-
bit_count > sizeof( TestValue ) * 8,
123+
bit_count > bit_count_v<TestValue>,
167124
"Attempt to use more bits than can be put into TestValue" );
168125
daw::exception::dbg_throw_on_true( bit_count < 1,
169126
"Attempt to pop less than 1 bits" );

include/daw/daw_bitset.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#pragma once
1010

1111
#include "daw/daw_attributes.h"
12+
#include "daw/daw_bit_count.h"
1213
#include "daw/daw_consteval.h"
1314

1415
#include <cassert>
@@ -20,7 +21,7 @@ namespace daw {
2021
class bitset {
2122
static_assert( BitCount > 2, "BitCount must be greater than 2" );
2223
using limb_t = std::uint32_t;
23-
static constexpr std::size_t limb_size = sizeof( limb_t ) * 8;
24+
static constexpr std::size_t limb_size = bit_count_v<limb_t>;
2425
static constexpr std::size_t num_limbs = ( BitCount - 1 ) / limb_size + 1;
2526
limb_t m_bits[num_limbs]{ };
2627

0 commit comments

Comments
 (0)