rexlib 0.1.0
Device-agnostic array computing library for CryoEM and CryoET image processing, with a pluggable backend architecture
Loading...
Searching...
No Matches
bit.hpp
1// SPDX-License-Identifier: GPL-3.0-only
2
3#pragma once
4
5#include "../platform/constexpr.hpp"
6#include "../platform/attributes.hpp"
7
8#include <cstddef>
9#include <type_traits>
10
11namespace rexlib
12{
13
25REXLIB_NODISCARD REXLIB_CONSTEXPR
26std::size_t bit(std::size_t i) noexcept;
27
36template<typename T>
37REXLIB_NODISCARD REXLIB_CONSTEXPR
38typename std::enable_if<std::is_unsigned<T>::value, T>::type
39bit_range_mask(std::size_t start, std::size_t end) noexcept;
40
51template<typename T>
52REXLIB_CONSTEXPR
53typename std::enable_if<std::is_unsigned<T>::value, T&>::type
55
66template<typename T>
67REXLIB_CONSTEXPR
68typename std::enable_if<std::is_unsigned<T>::value, T&>::type
70
85template<typename T>
86REXLIB_NODISCARD REXLIB_CONSTEXPR
87typename std::enable_if<std::is_unsigned<T>::value, T>::type
88mask_trailing_zeros(T x) noexcept;
89
103template<typename T>
104REXLIB_NODISCARD REXLIB_CONSTEXPR
105typename std::enable_if<std::is_unsigned<T>::value, T>::type
106mask_trailing_ones(T x) noexcept;
107
122template<typename T>
123REXLIB_NODISCARD REXLIB_CONSTEXPR
124typename std::enable_if<std::is_unsigned<T>::value, T>::type
125rotate_left(T x, int s) noexcept;
126
141template<typename T>
142REXLIB_NODISCARD REXLIB_CONSTEXPR
143typename std::enable_if<std::is_unsigned<T>::value, T>::type
144rotate_right(T x, int s) noexcept;
145
153template<typename T>
154REXLIB_NODISCARD REXLIB_CONSTEXPR
155typename std::enable_if<std::is_unsigned<T>::value, bool>::type
156has_single_bit(T x) noexcept;
157
167template<typename T>
168REXLIB_NODISCARD REXLIB_CONSTEXPR
169typename std::enable_if<std::is_unsigned<T>::value, T>::type
170bit_floor(T x) noexcept;
171
179template<typename T>
180REXLIB_NODISCARD REXLIB_CONSTEXPR
181typename std::enable_if<std::is_unsigned<T>::value, T>::type
182bit_ceil(T x) noexcept;
183
191template<typename T>
192REXLIB_NODISCARD REXLIB_CONSTEXPR
193typename std::enable_if<std::is_unsigned<T>::value, int>::type
194bit_width(T x) noexcept;
195
203template<typename T>
204REXLIB_NODISCARD REXLIB_CONSTEXPR
205typename std::enable_if<std::is_unsigned<T>::value, int>::type
207
215template<typename T>
216REXLIB_NODISCARD REXLIB_CONSTEXPR
217typename std::enable_if<std::is_unsigned<T>::value, int>::type
218count_leading_ones(T x) noexcept;
219
227template<typename T>
228REXLIB_CONSTEXPR
229typename std::enable_if<std::is_unsigned<T>::value, int>::type
231
239template<typename T>
240REXLIB_NODISCARD REXLIB_CONSTEXPR
241typename std::enable_if<std::is_unsigned<T>::value, int>::type
243
251template<typename T>
252REXLIB_NODISCARD REXLIB_CONSTEXPR
253typename std::enable_if<std::is_unsigned<T>::value, int>::type
254popcount(T x) noexcept;
255
266template<typename T>
267REXLIB_NODISCARD REXLIB_CONSTEXPR
268typename std::enable_if<std::is_unsigned<T>::value, bool>::type
269parity(T x) noexcept;
270
271} // namespace rexlib
272
273#include "bit.inl"
#define REXLIB_NODISCARD
Indicates that the return value should be used.
Definition attributes.hpp:76
Arrays, the operations on them and the devices that run those operations.
Definition cpu_device.hpp:10
REXLIB_NODISCARD REXLIB_CONSTEXPR std::size_t bit(std::size_t i) noexcept
Returns a std::size_t type with a single bit set at the provided index.
REXLIB_NODISCARD REXLIB_CONSTEXPR std::enable_if< std::is_unsigned< T >::value, T >::type mask_trailing_ones(T x) noexcept
Returns a mask for all the consecutive trailing '1'-s of the input word.
REXLIB_NODISCARD REXLIB_CONSTEXPR std::enable_if< std::is_unsigned< T >::value, int >::type count_leading_zeros(T x) noexcept
Counts the number of consecutive '0's from the MSB.
REXLIB_NODISCARD REXLIB_CONSTEXPR std::enable_if< std::is_unsigned< T >::value, int >::type bit_width(T x) noexcept
Returns the number of bits required to represent the input number.
REXLIB_NODISCARD REXLIB_CONSTEXPR std::enable_if< std::is_unsigned< T >::value, int >::type count_leading_ones(T x) noexcept
Counts the number of consecutive '1's from the MSB.
REXLIB_NODISCARD REXLIB_CONSTEXPR std::enable_if< std::is_unsigned< T >::value, T >::type rotate_left(T x, int s) noexcept
Returns the input word with a bitwise rotation towards the left.
REXLIB_NODISCARD REXLIB_CONSTEXPR std::enable_if< std::is_unsigned< T >::value, bool >::type parity(T x) noexcept
Computes the parity of the input word.
REXLIB_NODISCARD REXLIB_CONSTEXPR std::enable_if< std::is_unsigned< T >::value, T >::type bit_ceil(T x) noexcept
Returns the smallest power of 2 not less than the input word.
REXLIB_CONSTEXPR std::size_t end() noexcept
Get the value representing the end of an array to be used as count.
REXLIB_CONSTEXPR std::enable_if< std::is_unsigned< T >::value, T & >::type set_lowest_zero_inplace(T &x) noexcept
Sets the lowest '0' bit to '1' in place.
REXLIB_CONSTEXPR std::enable_if< std::is_unsigned< T >::value, T & >::type clear_lowest_one_inplace(T &x) noexcept
Sets the lowest '1' bit to '0' in place.
REXLIB_CONSTEXPR std::enable_if< std::is_unsigned< T >::value, int >::type count_trailing_zeros(T x) noexcept
Counts the number of consecutive '0's from the LSB.
REXLIB_NODISCARD REXLIB_CONSTEXPR std::enable_if< std::is_unsigned< T >::value, bool >::type has_single_bit(T x) noexcept
Checks if the input word has one and only one bit set to '1'.
REXLIB_NODISCARD REXLIB_CONSTEXPR std::enable_if< std::is_unsigned< T >::value, int >::type count_trailing_ones(T x) noexcept
Counts the number of consecutive '1's from the LSB.
REXLIB_NODISCARD REXLIB_CONSTEXPR std::enable_if< std::is_unsigned< T >::value, T >::type bit_range_mask(std::size_t start, std::size_t end) noexcept
REXLIB_NODISCARD REXLIB_CONSTEXPR std::enable_if< std::is_unsigned< T >::value, T >::type rotate_right(T x, int s) noexcept
Returns the input word with a bitwise rotation towards the right.
REXLIB_NODISCARD REXLIB_CONSTEXPR std::enable_if< std::is_unsigned< T >::value, T >::type mask_trailing_zeros(T x) noexcept
Returns a mask for all the consecutive trailing '0'-s of the input word.
REXLIB_NODISCARD REXLIB_CONSTEXPR std::enable_if< std::is_unsigned< T >::value, int >::type popcount(T x) noexcept
Counts the number '1's in the input word.
REXLIB_NODISCARD REXLIB_CONSTEXPR std::enable_if< std::is_unsigned< T >::value, T >::type bit_floor(T x) noexcept
Returns the largest power of 2 not greater than the input word.