aboutsummaryrefslogtreecommitdiff
path: root/src/NonStupidBitfield.h
blob: 124ba76f051b129b8bc49f97c86ac73aa4664a6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#ifndef NONSTUPIDBITFIELD_H
#define NONSTUPIDBITFIELD_H

#include "types.h"

#include <memory.h>

#include <initializer_list>
#include <algorithm>

// like std::bitset but less stupid and optimised for 
// our use case (keeping track of memory invalidations)

template <u32 Size>
struct NonStupidBitField
{
    static_assert((Size % 8) == 0, "bitfield size must be a multiple of 8");
    static const u32 DataLength = Size / 8;
    u8 Data[DataLength];

    struct Ref
    {
        NonStupidBitField<Size>& BitField;
        u32 Idx;

        operator bool()
        {
            return BitField.Data[Idx >> 3] & (1 << (Idx & 0x7));
        }

        Ref& operator=(bool set)
        {
            BitField.Data[Idx >> 3] &= ~(1 << (Idx & 0x7));
            BitField.Data[Idx >> 3] |= ((u8)set << (Idx & 0x7));
            return *this;
        }
    };

    struct Iterator
    {
        NonStupidBitField<Size>& BitField;
        u32 DataIdx;
        u32 BitIdx;
        u64 RemainingBits;

        u32 operator*() { return DataIdx * 8 + BitIdx; }

        bool operator==(const Iterator& other) { return other.DataIdx == DataIdx; }
        bool operator!=(const Iterator& other) { return other.DataIdx != DataIdx; }

        template <typename T>
        void Next()
        {
            while (RemainingBits == 0 && DataIdx < DataLength)
            {
                DataIdx += sizeof(T);
                RemainingBits = *(T*)&BitField.Data[DataIdx];
            }

            BitIdx = __builtin_ctzll(RemainingBits);
            RemainingBits &= ~(1ULL << BitIdx);
        }

        Iterator operator++(int)
        {
            Iterator prev(*this);
            ++*this;
            return prev;
        }

        Iterator& operator++()
        {
            if ((DataLength % 8) == 0)
                Next<u64>();
            else if ((DataLength % 4) == 0)
                Next<u32>();
            else if ((DataLength % 2) == 0)
                Next<u16>();
            else
                Next<u8>();

            return *this;
        }
    };

    NonStupidBitField(u32 start, u32 size)
    {
        memset(Data, 0, sizeof(Data));

        if (size == 0)
            return;

        u32 roundedStartBit = (start + 7) & ~7;
        u32 roundedEndBit = (start + size) & ~7;
        if (roundedStartBit != roundedEndBit)
            memset(Data + roundedStartBit / 8, 0xFF, (roundedEndBit - roundedStartBit) / 8);

        if (start & 0x7)
            Data[start >> 3] = 0xFF << (start & 0x7);
        if ((start + size) & 0x7)
            Data[(start + size) >> 3] = 0xFF >> ((start + size) & 0x7);
    }

    NonStupidBitField()
    {
        memset(Data, 0, sizeof(Data));
    }

    Iterator End()
    {
        return Iterator{*this, DataLength, 0, 0};
    }
    Iterator Begin()
    {
        if ((DataLength % 8) == 0)
            return ++Iterator{*this, 0, 0, *(u64*)Data};
        else if ((DataLength % 4) == 0)
            return ++Iterator{*this, 0, 0, *(u32*)Data};
        else if ((DataLength % 2) == 0)
            return ++Iterator{*this, 0, 0, *(u16*)Data};
        else
            return ++Iterator{*this, 0, 0, *Data};
    }

    Ref operator[](u32 idx)
    {
        return Ref{*this, idx};
    }

    NonStupidBitField& operator|=(const NonStupidBitField<Size>& other)
    {
        for (u32 i = 0; i < DataLength; i++)
        {
            Data[i] |= other.Data[i];
        }
        return *this;
    }
    NonStupidBitField& operator&=(const NonStupidBitField<Size>& other)
    {
        for (u32 i = 0; i < DataLength; i++)
        {
            Data[i] &= other.Data[i];
        }
        return *this;
    }
};


#endif