Skip to content

Commit 055e04b

Browse files
Centralize a couple JIT arithmetic helpers (dotnet#127781)
1 parent 1f84bc9 commit 055e04b

6 files changed

Lines changed: 13 additions & 92 deletions

File tree

src/coreclr/jit/bitset.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@
1010
#include "bitsetasshortlong.h"
1111
#include "bitsetasuint64inclass.h"
1212

13-
// clang-format off
14-
unsigned BitSetSupport::BitCountTable[16] = { 0, 1, 1, 2,
15-
1, 2, 2, 3,
16-
1, 2, 2, 3,
17-
2, 3, 3, 4 };
18-
// clang-format on
19-
2013
#ifdef DEBUG
2114
template <typename BitSetType, unsigned Uniq, typename Env, typename BitSetTraits>
2215
void BitSetSupport::RunTests(Env env)

src/coreclr/jit/bitset.h

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,6 @@ class BitSetSupport
2020
public:
2121
static const unsigned BitsInByte = 8;
2222

23-
// This maps 4-bit ("nibble") values into the number of 1 bits they contain.
24-
static unsigned BitCountTable[16];
25-
26-
// Returns the number of 1 bits in the binary representation of "u".
27-
template <typename T>
28-
static unsigned CountBitsInIntegral(T u)
29-
{
30-
unsigned res = 0;
31-
// We process "u" in 4-bit nibbles, hence the "*2" below.
32-
for (unsigned int i = 0; i < sizeof(T) * 2; i++)
33-
{
34-
res += BitCountTable[u & 0xf];
35-
u >>= 4;
36-
}
37-
return res;
38-
}
39-
4023
#ifdef DEBUG
4124
// This runs the "TestSuite" method for a few important instantiations of BitSet.
4225
static void TestSuite(CompAllocator env);
@@ -74,19 +57,6 @@ class BitSetSupport
7457
};
7558
};
7659

77-
template <>
78-
FORCEINLINE unsigned BitSetSupport::CountBitsInIntegral<unsigned>(unsigned c)
79-
{
80-
// Make sure we're 32 bit.
81-
assert(sizeof(unsigned) == 4);
82-
c = (c & 0x55555555) + ((c >> 1) & 0x55555555);
83-
c = (c & 0x33333333) + ((c >> 2) & 0x33333333);
84-
c = (c & 0x0f0f0f0f) + ((c >> 4) & 0x0f0f0f0f);
85-
c = (c & 0x00ff00ff) + ((c >> 8) & 0x00ff00ff);
86-
c = (c & 0x0000ffff) + ((c >> 16) & 0x0000ffff);
87-
return c;
88-
}
89-
9060
// A "BitSet" represents a set of integers from a "universe" [0..N-1]. This implementation assumes that "N"
9161
// (the "Size") is provided by the "Env" template argument type discussed below, and accessed from the Env
9262
// via a static method of the BitSetTraits type discussed below. The intent of "BitSet" is that the set is

src/coreclr/jit/bitsetasshortlong.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,11 @@ class BitSetOps</*BitSetType*/ BitSetShortLongRep,
178178
{
179179
if (IsShort(env))
180180
{
181-
return BitSetSupport::CountBitsInIntegral(size_t(bs));
181+
#if HOST_64BIT
182+
return BitOperations::PopCount(uint64_t(bs));
183+
#else
184+
return BitOperations::PopCount(uint32_t(bs));
185+
#endif
182186
}
183187
else
184188
{
@@ -757,7 +761,11 @@ unsigned BitSetOps</*BitSetType*/ BitSetShortLongRep,
757761
unsigned res = 0;
758762
for (unsigned i = 0; i < len; i++)
759763
{
760-
res += BitSetSupport::CountBitsInIntegral(bs[i]);
764+
#if HOST_64BIT
765+
res += BitOperations::PopCount(uint64_t(bs[i]));
766+
#else
767+
res += BitOperations::PopCount(uint32_t(bs[i]));
768+
#endif
761769
}
762770
return res;
763771
}

src/coreclr/jit/bitsetasuint64.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class BitSetOps</*BitSetType*/ UINT64,
6666

6767
static unsigned Count(Env env, UINT64 bs)
6868
{
69-
return BitSetSupport::CountBitsInIntegral(bs);
69+
return BitOperations::PopCount(bs);
7070
}
7171

7272
static bool IsEmptyUnion(Env env, UINT64 bs1, UINT64 bs2)

src/coreclr/jit/hashbv.cpp

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -115,29 +115,6 @@ bool hashBvNode::belongsIn(indexType index)
115115
return true;
116116
}
117117

118-
int countBitsInWord(unsigned int bits)
119-
{
120-
// In-place adder tree: perform 16 1-bit adds, 8 2-bit adds,
121-
// 4 4-bit adds, 2 8=bit adds, and 1 16-bit add.
122-
bits = ((bits >> 1) & 0x55555555) + (bits & 0x55555555);
123-
bits = ((bits >> 2) & 0x33333333) + (bits & 0x33333333);
124-
bits = ((bits >> 4) & 0x0F0F0F0F) + (bits & 0x0F0F0F0F);
125-
bits = ((bits >> 8) & 0x00FF00FF) + (bits & 0x00FF00FF);
126-
bits = ((bits >> 16) & 0x0000FFFF) + (bits & 0x0000FFFF);
127-
return (int)bits;
128-
}
129-
130-
int countBitsInWord(uint64_t bits)
131-
{
132-
bits = ((bits >> 1) & 0x5555555555555555) + (bits & 0x5555555555555555);
133-
bits = ((bits >> 2) & 0x3333333333333333) + (bits & 0x3333333333333333);
134-
bits = ((bits >> 4) & 0x0F0F0F0F0F0F0F0F) + (bits & 0x0F0F0F0F0F0F0F0F);
135-
bits = ((bits >> 8) & 0x00FF00FF00FF00FF) + (bits & 0x00FF00FF00FF00FF);
136-
bits = ((bits >> 16) & 0x0000FFFF0000FFFF) + (bits & 0x0000FFFF0000FFFF);
137-
bits = ((bits >> 32) & 0x00000000FFFFFFFF) + (bits & 0x00000000FFFFFFFF);
138-
return (int)bits;
139-
}
140-
141118
int hashBvNode::countBits()
142119
{
143120
int result = 0;
@@ -146,7 +123,7 @@ int hashBvNode::countBits()
146123
{
147124
elemType bits = elements[i];
148125

149-
result += countBitsInWord(bits);
126+
result += BitOperations::PopCount(bits);
150127

151128
result += (int)bits;
152129
}

src/coreclr/jit/hashbv.h

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -85,34 +85,7 @@ inline int log2(int number)
8585
// return greatest power of 2 that is less than or equal
8686
inline int nearest_pow2(unsigned number)
8787
{
88-
int result = 0;
89-
90-
if (number > 0xffff)
91-
{
92-
number >>= 16;
93-
result += 16;
94-
}
95-
if (number > 0xff)
96-
{
97-
number >>= 8;
98-
result += 8;
99-
}
100-
if (number > 0xf)
101-
{
102-
number >>= 4;
103-
result += 4;
104-
}
105-
if (number > 0x3)
106-
{
107-
number >>= 2;
108-
result += 2;
109-
}
110-
if (number > 0x1)
111-
{
112-
number >>= 1;
113-
result += 1;
114-
}
115-
return 1 << result;
88+
return 1 << BitOperations::Log2(number);
11689
}
11790

11891
class hashBvNode

0 commit comments

Comments
 (0)