SeqAn3  3.0.3
The Modern C++ library for sequence analysis.
bit_manipulation.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2021, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2021, Knut Reinert & MPI für molekulare Genetik
4 // This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5 // shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
6 // -----------------------------------------------------------------------------------------------------
7 
15 #pragma once
16 
17 #include <cassert>
18 
21 
22 SEQAN3_DEPRECATED_HEADER("This header is deprecated and will be removed in SeqAn-3.1.0; Please #include <seqan3/std/bit> XOR <seqan3/utility/detail/bits_of.hpp> XOR <seqan3/utility/detail/to_little_endian.hpp> instead.")
23 
24 #ifdef SEQAN3_DEPRECATED_310
25 namespace seqan3::detail
26 {
33 template <typename type_t>
34 SEQAN3_DEPRECATED_310 constexpr auto sizeof_bits = seqan3::detail::bits_of<type_t>;
35 
46 SEQAN3_DEPRECATED_310 constexpr bool is_power_of_two(size_t const n)
47 {
48  return std::has_single_bit(n);
49 }
50 
63 SEQAN3_DEPRECATED_310 constexpr size_t next_power_of_two(size_t n)
64 {
65  return std::bit_ceil(n);
66 }
67 
93 template <std::unsigned_integral unsigned_t>
94 SEQAN3_DEPRECATED_310 constexpr uint8_t popcount(unsigned_t const n) noexcept
95 {
96  return std::popcount(n);
97 }
98 
126 template <std::unsigned_integral unsigned_t>
127 SEQAN3_DEPRECATED_310 constexpr uint8_t count_leading_zeros(unsigned_t const n) noexcept
128 {
129  assert(n > 0); // n == 0 might have undefined behaviour
130  return std::countl_zero(n);
131 }
132 
160 template <std::unsigned_integral unsigned_t>
161 SEQAN3_DEPRECATED_310 constexpr uint8_t count_trailing_zeros(unsigned_t const n) noexcept
162 {
163  assert(n > 0); // n == 0 might have undefined behaviour
164  return std::countr_zero(n);
165 }
166 
194 template <std::unsigned_integral unsigned_t>
195 SEQAN3_DEPRECATED_310 constexpr uint8_t most_significant_bit_set(unsigned_t const n) noexcept
196 {
197  assert(n > 0); // n == 0 might have undefined behaviour
198  return std::bit_width(n) - 1;
199 }
200 
201 } // namespace seqan3::detail
202 #endif // SEQAN3_DEPRECATED_310
Provides utility functions for bit twiddling.
constexpr bool has_single_bit(T x) noexcept
Checks if x is an integral power of two.
Definition: bit:122
constexpr T bit_ceil(T x) noexcept
Calculates the smallest integral power of two that is not smaller than x.
Definition: bit:133
constexpr int popcount(T x) noexcept
Returns the number of 1 bits in the value of x.
Definition: bit:216
constexpr T bit_width(T x) noexcept
If x is not zero, calculates the number of bits needed to store the value x, that is,...
Definition: bit:154
constexpr int countl_zero(T x) noexcept
Returns the number of consecutive 0 bits in the value of x, starting from the most significant bit ("...
Definition: bit:181
constexpr int countr_zero(T x) noexcept
Returns the number of consecutive 0 bits in the value of x, starting from the least significant bit (...
Definition: bit:199
#define SEQAN3_DEPRECATED_310
Deprecation message for SeqAn 3.1.0 release.
Definition: platform.hpp:203
#define SEQAN3_DEPRECATED_HEADER(message)
Deprecation message for deprecated header.
Definition: platform.hpp:213
Provides utility functions for bit twiddling.