4#ifndef MUONNSWCOMMONDECODE_NSWDECODEHELPER_H
5#define MUONNSWCOMMONDECODE_NSWDECODEHELPER_H
37 uint32_t
get_bits (uint32_t word, uint32_t mask, uint8_t position);
43 std::string
format(
const std::string&
str,
const T& arg) {
45 std::size_t pos =
str.find(
"{}");
46 if (pos == std::string::npos) {
49 ss <<
str.substr(0, pos) << arg <<
str.substr(pos + 2);
55 template<
typename T,
typename...
Args>
56 std::string
format(
const std::string&
str,
const T& arg,
const Args&... args) {
58 std::size_t pos =
str.find(
"{}");
59 if (pos == std::string::npos) {
62 ss <<
str.substr(0, pos) << arg <<
format(
str.substr(pos + 2), args...);
79 template <
typename Target,
typename Source>
80 Target
bit_slice(
const std::span<const Source> words,
const std::size_t start,
const std::size_t end) {
83 const auto wordSize =
sizeof(Source) * 8;
85 const auto n = end / wordSize;
86 const auto m = start / wordSize;
89 throw std::runtime_error(
format(
"End must be larger than start ({} vs {})", start, end));
91 if (n >= std::size(words)) {
92 throw std::runtime_error(
format(
"End index ({}) out of range (number of words: {}, maximum allowed index: {})", n, std::size(words), std::size(words) - 1));
94 if (
sizeof(Target) <
sizeof(Source) * (n - m + 1)) {
95 throw std::runtime_error(
format(
"Target type (size {}) too small to fit result of bit_slice given start {} and end {} and source size {}",
sizeof(Target), start, end,
sizeof(Source)));
98 for (
auto i = m; i <= n; ++i) {
99 s = (s << wordSize) + words[i];
101 s >>= (n + 1) * wordSize - (end + 1);
103 const Target mask = ((Target{1}) << (end - start + 1)) - 1;
118 template <
typename Target,
typename Source>
119 constexpr Target
decode_and_advance(
const std::span<const Source> words, std::size_t& start,
const std::size_t size) {
136 template <
typename Target,
typename Source>
137 constexpr Target
decode_at_loc(
const std::span<const Source> words, std::size_t& start,
const int offset,
const std::size_t size) {
148 return std::bit_width(
static_cast<std::make_unsigned_t<T>
>(
number)) - 1;
156 if (
number == 0)
return -1;
158 std::countr_zero(
static_cast<std::make_unsigned_t<T>
>(
number));
161 constexpr Out
fill_bitmask(
const uint8_t first_bit,
const uint8_t num_bits) {
166 uint16_t
get_16bxor_crc(
const uint32_t * dataPointer, uint32_t dataSize);
174 return (word >> position) & mask;
182 bool hasPadding = (dataPointer[dataSize-1] & 0xFFFF)==0;
185 for (uint32_t i = 0; i<dataSize-1; ++i) {
186 crc = crc ^ ((dataPointer[i] >> 16) & 0xFFFF);
187 crc = crc ^ ((dataPointer[i] >> 0) & 0xFFFF);
191 if (!hasPadding) {crc = crc ^ ((dataPointer[dataSize-1] >> 16) & 0xFFFF);}
std::pair< std::vector< unsigned int >, bool > res
constexpr T ones(unsigned int n)
Return a bit mask with the lower n bits set.
uint32_t get_bits(uint32_t word, uint32_t mask, uint8_t position)
constexpr int8_t min_bit(const T number)
Returns the most right hand bit which is set in a number.
uint16_t get_16bxor_crc(const uint32_t *dataPointer, uint32_t dataSize)
Target bit_slice(const std::span< const Source > words, const std::size_t start, const std::size_t end)
Decode bits from data of words.
constexpr int8_t max_bit(const T number)
Returns the most left hand bit which is set in a number.
constexpr Out fill_bitmask(const uint8_t first_bit, const uint8_t num_bits)
std::string format(const std::string &str, const T &arg)
constexpr Target decode_at_loc(const std::span< const Source > words, std::size_t &start, const int offset, const std::size_t size)
Decode bits from data of words at read pointer + offset and NOT advance the read pointer.
constexpr Target decode_and_advance(const std::span< const Source > words, std::size_t &start, const std::size_t size)
Decode bits from data of words and advance the read pointer.
@ OFFLINE_CHANNEL_TYPE_STRIP
@ OFFLINE_CHANNEL_TYPE_PAD
@ OFFLINE_CHANNEL_TYPE_WIRE
NRpcCablingAlg reads raw condition data and writes derived condition data to the condition store.
std::string number(const double &d, const std::string &s)