27#ifndef CCSDS124_ENCODER_HPP
28#define CCSDS124_ENCODER_HPP
43 0xC0U, 0xC1U, 0xC2U, 0xC3U, 0xC4U, 0xC5U, 0xC6U, 0xC7U,
44 0xC8U, 0xC9U, 0xCAU, 0xCBU, 0xCCU, 0xCDU, 0xCEU, 0xCFU,
45 0xD0U, 0xD1U, 0xD2U, 0xD3U, 0xD4U, 0xD5U, 0xD6U, 0xD7U,
46 0xD8U, 0xD9U, 0xDAU, 0xDBU, 0xDCU, 0xDDU, 0xDEU, 0xDFU
50 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U,
51 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U,
52 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U,
53 8U, 8U, 8U, 8U, 8U, 8U, 8U, 8U
70template <std::
size_t MaxBytes>
72 if (A == 0 || A > 65535) [[unlikely]] {
77 if (A <= 33) [[likely]] {
80 return output.append_bit(0);
89 auto result = output.append_value(0b111U, 3);
94 std::uint32_t value = A - 2;
95 int highest_bit = 31 - __builtin_clz(value);
96 int E = (2 * (highest_bit + 1)) - 6;
99 return output.append_value(value,
static_cast<std::size_t
>(E));
113template <std::
size_t MaxBytes, std::
size_t N>
116 int old_bit_position =
static_cast<int>(N);
120 std::uint32_t word_data = input.data()[word];
123 while (word_data != 0) {
125 int trailing_zeros = __builtin_ctz(word_data);
126 std::uint32_t lsb = 1U <<
static_cast<unsigned>(trailing_zeros);
129 int bit_position_in_word = 31 - trailing_zeros;
132 int new_bit_position = (word * 32) + bit_position_in_word;
135 int delta = old_bit_position - new_bit_position;
138 auto result =
count_encode(output,
static_cast<std::uint32_t
>(delta));
143 old_bit_position = new_bit_position;
151 return output.append_value(0b10U, 2);
166template <std::
size_t MaxBytes, std::
size_t N>
170 std::uint32_t batch_value = 0;
171 std::size_t batch_count = 0;
175 std::uint32_t mask_word = mask.data()[word];
176 std::uint32_t data_word = data.data()[word];
179 for (
int bits_remaining = __builtin_popcount(mask_word); bits_remaining > 0;
182 int trailing_zeros = __builtin_ctz(mask_word);
183 std::uint32_t lsb = 1U <<
static_cast<unsigned>(trailing_zeros);
186 int bit_pos_in_word = 31 - trailing_zeros;
189 int global_pos = (word * 32) + bit_pos_in_word;
190 if (
static_cast<std::size_t
>(global_pos) < N) {
192 int bit = (data_word & lsb) != 0 ? 1 : 0;
193 batch_value = (batch_value << 1) | static_cast<std::uint32_t>(bit);
197 if (batch_count == 24) {
198 auto result = output.append_value(batch_value, 24);
207 mask_word &= mask_word - 1;
212 if (batch_count > 0) {
213 auto result = output.append_value(batch_value, batch_count);
233template <std::
size_t MaxBytes, std::
size_t N>
237 std::uint32_t batch_value = 0;
238 std::size_t batch_count = 0;
241 for (std::size_t word = 0; word < BitVector<N>::NUM_WORDS; ++word) {
242 std::uint32_t mask_word = mask.data()[word];
243 std::uint32_t data_word = data.data()[word];
246 for (
int bits_remaining = __builtin_popcount(mask_word); bits_remaining > 0;
251 std::size_t global_pos = (word * 32) +
static_cast<std::size_t
>(clz);
253 if (global_pos < N) {
255 std::uint32_t bit_mask = 1U << (31U -
static_cast<std::uint32_t
>(clz));
256 int bit = (data_word & bit_mask) != 0 ? 1 : 0;
257 batch_value = (batch_value << 1) | static_cast<std::uint32_t>(bit);
261 if (batch_count == 24) {
262 auto result = output.append_value(batch_value, 24);
273 if (batch_count > 0) {
274 auto result = output.append_value(batch_value, batch_count);
285template <std::
size_t MaxBytes, std::
size_t N1, std::
size_t N2>
288 if constexpr (N1 != N2) {
291 return bit_extract<MaxBytes, N1>(output, data, mask);
Variable-length bit buffer for building compressed output.
Fixed-length bit vector with static allocation.
Variable-length bit buffer with static allocation.
Fixed-length bit vector with compile-time size.
CCSDS 124.0-B-1 compile-time configuration.
CCSDS 124.0-B-1 error handling.
constexpr std::uint8_t COUNT_VALUES[34]
int extract_msb(std::uint32_t &word) noexcept
Extract and clear the most significant set bit from a word.
constexpr std::uint8_t COUNT_BITS[34]
Error count_encode(BitBuffer< MaxBytes > &output, std::uint32_t A) noexcept
Counter encoding (CCSDS Section 5.2.2, Equation 9).
Error bit_extract(BitBuffer< MaxBytes > &output, const BitVector< N > &data, const BitVector< N > &mask) noexcept
Bit extraction in reverse order (CCSDS Section 5.2.4, Equation 11).
Error
Error codes for error-code-based error handling.
@ InvalidArg
Invalid argument.
Error bit_extract_forward(BitBuffer< MaxBytes > &output, const BitVector< N > &data, const BitVector< N > &mask) noexcept
Bit extraction in forward order.
Error rle_encode(BitBuffer< MaxBytes > &output, const BitVector< N > &input) noexcept
Run-length encoding (CCSDS Section 5.2.3, Equation 10).