summaryrefslogtreecommitdiff
path: root/src/jit/bitset.cpp
blob: 90ef25319928cfcadb3b65ce82717031f0f09b31 (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
// 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.

#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
#include "bitset.h"
#include "bitsetasuint64.h"
#include "bitsetasshortlong.h"
#include "bitsetasuint64inclass.h"

// clang-format off
unsigned BitSetSupport::BitCountTable[16] = { 0, 1, 1, 2, 
                                              1, 2, 2, 3, 
                                              1, 2, 2, 3, 
                                              2, 3, 3, 4 };
// clang-format on

#ifdef DEBUG
template <typename BitSetType, unsigned Uniq, typename Env, typename BitSetTraits>
void BitSetSupport::RunTests(Env env)
{

    typedef BitSetOps<BitSetType, Uniq, Env, BitSetTraits> LclBitSetOps;

    // The tests require that the Size is at least 52...
    assert(BitSetTraits::GetSize(env) > 51);

    BitSetType bs1;
    LclBitSetOps::AssignNoCopy(env, bs1, LclBitSetOps::MakeEmpty(env));
    unsigned bs1bits[] = {0, 10, 44, 45};
    LclBitSetOps::AddElemD(env, bs1, bs1bits[0]);
    LclBitSetOps::AddElemD(env, bs1, bs1bits[1]);
    LclBitSetOps::AddElemD(env, bs1, bs1bits[2]);
    LclBitSetOps::AddElemD(env, bs1, bs1bits[3]);

    typename LclBitSetOps::Iter bsi(env, bs1);
    unsigned                    bitNum = 0;
    unsigned                    k      = 0;
    while (bsi.NextElem(env, &bitNum))
    {
        assert(bitNum == bs1bits[k]);
        k++;
    }
    assert(k == 4);

    assert(LclBitSetOps::Equal(env, bs1, LclBitSetOps::Union(env, bs1, bs1)));
    assert(LclBitSetOps::Equal(env, bs1, LclBitSetOps::Intersection(env, bs1, bs1)));
    assert(LclBitSetOps::IsSubset(env, bs1, bs1));

    BitSetType bs2;
    LclBitSetOps::AssignNoCopy(env, bs2, LclBitSetOps::MakeEmpty(env));
    unsigned bs2bits[] = {0, 10, 50, 51};
    LclBitSetOps::AddElemD(env, bs2, bs2bits[0]);
    LclBitSetOps::AddElemD(env, bs2, bs2bits[1]);
    LclBitSetOps::AddElemD(env, bs2, bs2bits[2]);
    LclBitSetOps::AddElemD(env, bs2, bs2bits[3]);

    unsigned   unionBits[] = {0, 10, 44, 45, 50, 51};
    BitSetType bsU12;
    LclBitSetOps::AssignNoCopy(env, bsU12, LclBitSetOps::Union(env, bs1, bs2));
    k      = 0;
    bsi    = typename LclBitSetOps::Iter(env, bsU12);
    bitNum = 0;
    while (bsi.NextElem(env, &bitNum))
    {
        assert(bitNum == unionBits[k]);
        k++;
    }
    assert(k == 6);

    k                                = 0;
    typename LclBitSetOps::Iter bsiL = typename LclBitSetOps::Iter(env, bsU12);
    bitNum                           = 0;
    while (bsiL.NextElem(env, &bitNum))
    {
        assert(bitNum == unionBits[k]);
        k++;
    }
    assert(k == 6);

    unsigned   intersectionBits[] = {0, 10};
    BitSetType bsI12;
    LclBitSetOps::AssignNoCopy(env, bsI12, LclBitSetOps::Intersection(env, bs1, bs2));
    k      = 0;
    bsi    = typename LclBitSetOps::Iter(env, bsI12);
    bitNum = 0;
    while (bsi.NextElem(env, &bitNum))
    {
        assert(bitNum == intersectionBits[k]);
        k++;
    }
    assert(k == 2);
}

class TestBitSetTraits
{
public:
    static IAllocator* GetAllocator(IAllocator* alloc)
    {
        return alloc;
    }
    static unsigned GetSize(IAllocator* alloc)
    {
        return 64;
    }
    static unsigned GetArrSize(IAllocator* alloc, unsigned elemSize)
    {
        assert(elemSize == sizeof(size_t));
        return (64 / 8) / sizeof(size_t);
    }
    static unsigned GetEpoch(IAllocator* alloc)
    {
        return 0;
    }
};

void BitSetSupport::TestSuite(IAllocator* env)
{
    BitSetSupport::RunTests<UINT64, BSUInt64, IAllocator*, TestBitSetTraits>(env);
    BitSetSupport::RunTests<BitSetShortLongRep, BSShortLong, IAllocator*, TestBitSetTraits>(env);
    BitSetSupport::RunTests<BitSetUint64<IAllocator*, TestBitSetTraits>, BSUInt64Class, IAllocator*, TestBitSetTraits>(
        env);
}
#endif

const char* BitSetSupport::OpNames[BitSetSupport::BSOP_NUMOPS] = {
#define BSOPNAME(x) #x,
#include "bitsetops.h"
#undef BSOPNAME
};

void BitSetSupport::BitSetOpCounter::RecordOp(BitSetSupport::Operation op)
{
    OpCounts[op]++;
    TotalOps++;

    if ((TotalOps % 1000000) == 0)
    {
        if (OpOutputFile == nullptr)
        {
            OpOutputFile = fopen(m_fileName, "a");
        }
        fprintf(OpOutputFile, "@ %d total ops.\n", TotalOps);

        unsigned OpOrder[BSOP_NUMOPS];
        bool     OpOrdered[BSOP_NUMOPS];

        // First sort by total operations (into an index permutation array, using a simple n^2 sort).
        for (unsigned k = 0; k < BitSetSupport::BSOP_NUMOPS; k++)
        {
            OpOrdered[k] = false;
        }
        for (unsigned k = 0; k < BitSetSupport::BSOP_NUMOPS; k++)
        {
            bool     candSet = false;
            unsigned cand    = 0;
            unsigned candInd = 0;
            for (unsigned j = 0; j < BitSetSupport::BSOP_NUMOPS; j++)
            {
                if (OpOrdered[j])
                {
                    continue;
                }
                if (!candSet || OpCounts[j] > cand)
                {
                    candInd = j;
                    cand    = OpCounts[j];
                    candSet = true;
                }
            }
            assert(candSet);
            OpOrder[k]         = candInd;
            OpOrdered[candInd] = true;
        }

        for (unsigned ii = 0; ii < BitSetSupport::BSOP_NUMOPS; ii++)
        {
            unsigned i = OpOrder[ii];
            fprintf(OpOutputFile, "   Op %40s: %8d\n", OpNames[i], OpCounts[i]);
        }
    }
}