mirror of
https://github.com/open-goal/jak-project
synced 2026-05-25 23:35:33 -04:00
e5f0fecf17
* get to vif * support basic bitfield access * make bitfields in dma work * clean up dma * fix merge conflict
42 lines
889 B
C++
42 lines
889 B
C++
#pragma once
|
|
|
|
#include <optional>
|
|
|
|
#include "common/util/Range.h"
|
|
|
|
constexpr int BITS_PER_BYTE = 8;
|
|
template <typename T>
|
|
std::optional<Range<int>> get_bit_range(T value) {
|
|
enum State { INITIAL_ZEROS, ONES, TRAILING_ZEROS } state = INITIAL_ZEROS;
|
|
|
|
int start_bit = 0;
|
|
int end_bit = 0;
|
|
int max_bit = BITS_PER_BYTE * sizeof(T);
|
|
|
|
for (int i = 0; i < max_bit; i++) {
|
|
bool bit = (value) & (T(1) << i);
|
|
if (state == INITIAL_ZEROS) {
|
|
if (bit) {
|
|
start_bit = i;
|
|
state = ONES;
|
|
}
|
|
} else if (state == ONES) {
|
|
if (!bit) {
|
|
end_bit = i;
|
|
state = TRAILING_ZEROS;
|
|
}
|
|
} else if (state == TRAILING_ZEROS) {
|
|
if (bit) {
|
|
return std::nullopt;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (state == INITIAL_ZEROS) {
|
|
return std::nullopt;
|
|
} else if (state == ONES) {
|
|
end_bit = max_bit;
|
|
}
|
|
|
|
return Range<int>{start_bit, end_bit};
|
|
} |