IAP GITLAB

Skip to content
Snippets Groups Projects
Commit a6683aa8 authored by ralfulrich's avatar ralfulrich
Browse files

Merge branch 'sibyll'

parents 50bedea5 4de279f2
No related branches found
No related tags found
No related merge requests found
Showing
with 2496 additions and 0 deletions
// BITSET2
//
// Copyright Claas Bontus
//
// Use, modification and distribution is subject to the
// Boost Software License, Version 1.0. (See accompanying
// file LICENSE.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/ClaasBontus/bitset2
//
#ifndef BITSET2_INDEX_LSB_SET_CB_HPP
#define BITSET2_INDEX_LSB_SET_CB_HPP
#include <limits>
#include <climits>
#include <cstddef>
namespace Bitset2
{
namespace detail
{
/// https://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightBinSearch
template<class T>
struct index_lsb_set
{
enum : size_t { npos= std::numeric_limits<size_t>::max()
, n_bits= sizeof(T) * CHAR_BIT };
constexpr
index_lsb_set() noexcept
{
static_assert( ( n_bits & ( n_bits - 1 ) ) == 0,
"Number of bits in data type is not a power of 2" );
}
/// \brief Returns index of first (least significant) bit set in val.
/// Returns npos if all bits are zero.
constexpr
size_t
operator()( T val ) const noexcept
{
return ( T(0) == val ) ? npos
: find_idx( val, T( T(~T(0)) >> (n_bits >> 1) ), n_bits >> 1, 1 );
}
private:
constexpr
size_t
find_idx( T val, T pttrn, size_t sh_rght, size_t ct ) const noexcept
{
return ( sh_rght == 1 ) ? ( ct - size_t( T(val & T(0x1)) ) )
: T( val & pttrn ) == T(0)
? find_idx( T(val >> sh_rght), T( pttrn >> ( sh_rght >> 1 )),
sh_rght >> 1, ct + sh_rght )
: find_idx( val, T(pttrn >> ( sh_rght >> 1 )), sh_rght >> 1, ct );
}
}; // struct index_lsb_set
} // namespace detail
} // namespace Bitset2
#endif // BITSET2_INDEX_LSB_SET_CB_HPP
// BITSET2
//
// Copyright Claas Bontus
//
// Use, modification and distribution is subject to the
// Boost Software License, Version 1.0. (See accompanying
// file LICENSE.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/ClaasBontus/bitset2
//
#ifndef BITSET2_REVERSE_BITS_CB_HPP
#define BITSET2_REVERSE_BITS_CB_HPP
#include <limits>
#include <climits>
#include <cstddef>
namespace Bitset2
{
namespace detail
{
/// https://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel
template<class T>
struct reverse_bits
{
enum : size_t { n_bits= sizeof(T) * CHAR_BIT
, n_bits_h= n_bits >> 1 };
constexpr
reverse_bits() noexcept
{
static_assert( ( n_bits & ( n_bits - 1 ) ) == 0,
"Number of bits in data type is not a power of 2" );
}
/// \brief Reverses bits in val.
constexpr
T
operator()( T val ) const noexcept { return rvrs( val ); }
private:
constexpr
T
rvrs( T val,
T mask= T( ~T(0) ) >> n_bits_h,
size_t s= n_bits_h ) const noexcept
{
return s == 0 ? val
: rvrs( ( (val >> s) & mask ) | ( (val << s) & ~mask ),
mask ^ ( mask << ( s >> 1 ) ),
s >> 1 );
}
}; // struct reverse_bits
} // namespace detail
} // namespace Bitset2
#endif // BITSET2_REVERSE_BITS_CB_HPP
// BITSET2
//
// Copyright Claas Bontus
//
// Use, modification and distribution is subject to the
// Boost Software License, Version 1.0. (See accompanying
// file LICENSE.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/ClaasBontus/bitset2
//
#ifndef BITSET2_SELECT_BASE_T_CB_HPP
#define BITSET2_SELECT_BASE_T_CB_HPP
#include <cstdint>
#include <cstddef>
namespace Bitset2
{
namespace detail
{
template<bool b,class T1, class T2>
struct if_else { using type= T1; };
template<class T1, class T2>
struct if_else<false,T1,T2> { using type= T2; };
template<bool b,class T1, class T2>
using if_else_t= typename if_else<b,T1,T2>::type ;
/// \brief Select any of uint8_t, uint16_t, uint32_t or
/// unsigned long long. Result depends on N and on provision
/// of these types by compiler.
template<size_t N>
struct select_base
{
#ifdef INT8_MIN
enum : bool { has_int8= true };
using UI8= uint8_t;
#else
enum : bool { has_int8= false };
using UI8= void;
#endif
#ifdef INT16_MIN
enum : bool { has_int16= true };
using UI16= uint16_t;
#else
enum : bool { has_int16= false };
using UI16= void;
#endif
#ifdef INT32_MIN
enum : bool { has_int32= true };
using UI32= uint32_t;
#else
enum : bool { has_int32= false };
using UI32= void;
#endif
using type=
if_else_t< has_int8 && (N<=8), UI8,
if_else_t< has_int16 && (N<=16), UI16,
if_else_t< has_int32 && (N<=32), UI32,
unsigned long long > > >;
}; // struct select_base
template<size_t N>
using select_base_t= typename select_base<N>::type;
} // namespace detail
} // namespace Bitset2
#endif // BITSET2_SELECT_BASE_T_CB_HPP
// BITSET2
//
// Copyright Claas Bontus
//
// Use, modification and distribution is subject to the
// Boost Software License, Version 1.0. (See accompanying
// file LICENSE.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/ClaasBontus/bitset2
//
#ifndef BITSET2_ULLONG2ARRAY_CB_HPP
#define BITSET2_ULLONG2ARRAY_CB_HPP
#include "bit_chars.hpp"
namespace Bitset2
{
namespace detail
{
/// \brief Takes a variable 'v' of type unsigned long long
/// and returns a std::array 'a' equivalent to v. 'a' represents
/// an N bit bitset2 with base_t == T.
template<size_t N,class T>
struct ullong2array
{
using base_t= T;
using b_c= bit_chars<N,T>;
using ULLONG_t= typename b_c::ULLONG_t;
enum : size_t
{ n_bits= N
, base_t_n_bits= b_c::base_t_n_bits
, ullong_n_bits= b_c::ullong_n_bits
, n_array= b_c::n_array
, centrl_i= ce_min( (ullong_n_bits-1) / base_t_n_bits, n_array - 1 )
, n_empty_vals= n_array - centrl_i - 1
};
enum : base_t
{ hgh_bit_pattern= b_c::hgh_bit_pattern
, use_pattern= (n_empty_vals==0) ? hgh_bit_pattern : base_t(~base_t(0))
};
using array_t= typename h_types<T>::template array_t<n_array>;
constexpr
array_t
operator()( ULLONG_t v ) const noexcept
{
return fill( gen_empty_array<n_array,T>(), v,
std::make_index_sequence<n_empty_vals>(),
std::make_index_sequence<centrl_i>() );
}
template<size_t ... S1,size_t ... S2>
constexpr
array_t
fill( array_t const & empty, ULLONG_t v,
std::index_sequence<S1...>,
std::index_sequence<S2...> ) const noexcept
{
return {{ base_t(ce_right_shift(v, S2 * base_t_n_bits))...,
base_t(ce_right_shift(v, centrl_i * base_t_n_bits)&use_pattern),
empty[S1]... }};
}
}; // struct ullong2array
} // namespace detail
} // namespace Bitset2
#endif // BITSET2_ULLONG2ARRAY_CB_HPP
This diff is collapsed.
// BITSET2
//
// Copyright Claas Bontus
//
// Use, modification and distribution is subject to the
// Boost Software License, Version 1.0. (See accompanying
// file LICENSE.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/ClaasBontus/bitset2
//
#include <iostream>
#include "bitset2.hpp"
int main()
{
Bitset2::bitset2<128> c;
constexpr Bitset2::bitset2<128> tst{ 0xFFFFFFFFull };
for( ;; ++c )
{
if( ( c & tst) == tst )
std::cout << c.to_hex_string() << "\n";
}
} // main
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment