HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
bits.hpp
1// Copyright 2020 Pokitec
2// All rights reserved.
3
4#pragma once
5
6#include "TTauri/Foundation/exceptions.hpp"
7#include <nonstd/span>
8#include <cstddef>
9
10namespace tt {
11
17[[nodiscard]] inline bool get_bit(nonstd::span<std::byte const> buffer, ssize_t &index) noexcept
18{
19 auto byte_index = index >> 3;
20 auto bit_index = static_cast<uint8_t>(index & 7);
21 ++index;
22
23 tt_assume(byte_index < ssize(buffer));
24 return static_cast<bool>(
25 static_cast<int>(buffer[byte_index] >> bit_index) & 1
26 );
27}
28
46[[nodiscard]] inline int get_bits(nonstd::span<std::byte const> buffer, ssize_t &index, int length) noexcept
47{
48 auto value = 0;
49
50 auto todo = length;
51 auto done = 0;
52 while (todo) {
53 auto byte_index = index >> 3;
54 auto bit_index = static_cast<int>(index & 7);
55 tt_assume(byte_index < ssize(buffer));
56
57 auto available_bits = 8 - bit_index;
58 auto nr_bits = std::min(available_bits, todo);
59
60 auto mask = (1 << nr_bits) - 1;
61
62 auto tmp = static_cast<int>(buffer[byte_index] >> bit_index) & mask;
63 value |= tmp << done;
64
65 todo -= nr_bits;
66 done += nr_bits;
67 index += nr_bits;
68 }
69
70 return value;
71}
72
73}
T min(T... args)