summaryrefslogtreecommitdiff
path: root/src/jit/bitsetasuint64.h
blob: aec4d05c35269932a0d8bb5d065edb793d097a44 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#ifndef bitSetAsUint64_DEFINED
#define bitSetAsUint64_DEFINED 1

#include "bitset.h"

template <typename Env, typename BitSetTraits>
class BitSetOps</*BitSetType*/ UINT64,
                /*Brand*/ BSUInt64,
                /*Env*/ Env,
                /*BitSetTraits*/ BitSetTraits>
{
public:
    typedef UINT64 Rep;

private:
    static UINT64 Singleton(unsigned bitNum)
    {
        assert(bitNum < sizeof(UINT64) * BitSetSupport::BitsInByte);
        return (UINT64)1 << bitNum;
    }

public:
    static void Assign(Env env, UINT64& lhs, UINT64 rhs)
    {
        lhs = rhs;
    }

    static void AssignNouninit(Env env, UINT64& lhs, UINT64 rhs)
    {
        lhs = rhs;
    }

    static void AssignAllowUninitRhs(Env env, UINT64& lhs, UINT64 rhs)
    {
        lhs = rhs;
    }

    static void AssignNoCopy(Env env, UINT64& lhs, UINT64 rhs)
    {
        lhs = rhs;
    }

    static void OldStyleClearD(Env env, UINT64& bs)
    {
        bs = 0;
    }

    static void ClearD(Env env, UINT64& bs)
    {
        bs = 0;
    }

    static UINT64 MakeSingleton(Env env, unsigned bitNum)
    {
        assert(bitNum < BitSetTraits::GetSize(env));
        return Singleton(bitNum);
    }

    static UINT64 MakeCopy(Env env, UINT64 bs)
    {
        return bs;
    }

    static bool IsEmpty(Env env, UINT64 bs)
    {
        return bs == 0;
    }

    static unsigned Count(Env env, UINT64 bs)
    {
        return BitSetSupport::CountBitsInIntegral(bs);
    }

    static void UnionD(Env env, UINT64& bs1, UINT64 bs2)
    {
        bs1 |= bs2;
    }

    static UINT64 Union(Env env, UINT64& bs1, UINT64 bs2)
    {
        return bs1 | bs2;
    }

    static void DiffD(Env env, UINT64& bs1, UINT64 bs2)
    {
        bs1 = bs1 & ~bs2;
    }

    static UINT64 Diff(Env env, UINT64 bs1, UINT64 bs2)
    {
        return bs1 & ~bs2;
    }

    static void RemoveElemD(Env env, UINT64& bs1, unsigned i)
    {
        assert(i < BitSetTraits::GetSize(env));
        bs1 &= ~Singleton(i);
    }

    static UINT64 RemoveElem(Env env, UINT64 bs1, unsigned i)
    {
        return bs1 & ~Singleton(i);
    }

    static void AddElemD(Env env, UINT64& bs1, unsigned i)
    {
        assert(i < BitSetTraits::GetSize(env));
        bs1 |= Singleton(i);
    }

    static UINT64 AddElem(Env env, UINT64 bs1, unsigned i)
    {
        assert(i < BitSetTraits::GetSize(env));
        return bs1 | Singleton(i);
    }

    static bool IsMember(Env env, const UINT64 bs1, unsigned i)
    {
        assert(i < BitSetTraits::GetSize(env));
        return (bs1 & Singleton(i)) != 0;
    }

    static void IntersectionD(Env env, UINT64& bs1, UINT64 bs2)
    {
        bs1 &= bs2;
    }

    static UINT64 Intersection(Env env, UINT64 bs1, UINT64 bs2)
    {
        return bs1 & bs2;
    }

    static bool IsEmptyIntersection(Env env, UINT64 bs1, UINT64 bs2)
    {
        return (bs1 & bs2) == 0;
    }

    static bool IsSubset(Env env, UINT64 bs1, UINT64 bs2)
    {
        return ((bs1 & bs2) == bs1);
    }

    static bool Equal(Env env, UINT64 bs1, UINT64 bs2)
    {
        return bs1 == bs2;
    }

    static UINT64 MakeEmpty(Env env)
    {
        return 0;
    }

    static UINT64 MakeFull(Env env)
    {
        unsigned sz = BitSetTraits::GetSize(env);
        if (sz == sizeof(UINT64) * 8)
        {
            return UINT64(-1);
        }
        else
        {
            return (UINT64(1) << sz) - 1;
        }
    }

#ifdef DEBUG
    static const char* ToString(Env env, UINT64 bs)
    {
        IAllocator* alloc          = BitSetTraits::GetDebugOnlyAllocator(env);
        const int   CharsForUINT64 = sizeof(UINT64) * 2;
        char*       res            = nullptr;
        const int   AllocSize      = CharsForUINT64 + 4;
        res                        = (char*)alloc->Alloc(AllocSize);
        UINT64   bits              = bs;
        unsigned remaining         = AllocSize;
        char*    ptr               = res;
        for (unsigned bytesDone = 0; bytesDone < sizeof(UINT64); bytesDone += sizeof(unsigned))
        {
            unsigned bits0 = (unsigned)bits;
            sprintf_s(ptr, remaining, "%08X", bits0);
            ptr += 8;
            remaining -= 8;
            bytesDone += 4;
            assert(sizeof(unsigned) == 4);
            // Doing this twice by 16, rather than once by 32, avoids warnings when size_t == unsigned.
            bits = bits >> 16;
            bits = bits >> 16;
        }
        return res;
    }
#endif

    static UINT64 UninitVal()
    {
        return 0;
    }

    static bool MayBeUninit(UINT64 bs)
    {
        return bs == UninitVal();
    }

    class Iter
    {
        UINT64 m_bits;

    public:
        Iter(Env env, const UINT64& bits) : m_bits(bits)
        {
        }

        bool NextElem(Env env, unsigned* pElem)
        {
            if (m_bits)
            {
                unsigned bitNum = *pElem;
                while ((m_bits & 0x1) == 0)
                {
                    bitNum++;
                    m_bits >>= 1;
                }
                *pElem = bitNum;
                m_bits &= ~0x1;
                return true;
            }
            else
            {
                return false;
            }
        }
    };

    typedef UINT64 ValArgType;
    typedef UINT64 RetValType;
};

#endif // bitSetAsUint64_DEFINED