Skip to content

Commit 858b9f7

Browse files
committed
optimized EnumSet::count()
1 parent 5a719bc commit 858b9f7

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/EnumSet.php

+10-7
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,14 @@ private function doCountBin()
275275
}
276276

277277
$ord = \ord($bitset[$bytePos]);
278-
for ($bitPos = 0; $bitPos < 8; ++$bitPos) {
279-
if ($ord & (1 << $bitPos)) {
280-
++$count;
281-
}
282-
}
278+
if ($ord & 0b00000001) ++$count;
279+
if ($ord & 0b00000010) ++$count;
280+
if ($ord & 0b00000100) ++$count;
281+
if ($ord & 0b00001000) ++$count;
282+
if ($ord & 0b00010000) ++$count;
283+
if ($ord & 0b00100000) ++$count;
284+
if ($ord & 0b01000000) ++$count;
285+
if ($ord & 0b10000000) ++$count;
283286
}
284287
return $count;
285288
}
@@ -305,8 +308,8 @@ private function doCountInt()
305308
}
306309

307310
// iterate byte by byte and count set bits
308-
for ($i = 0; $i < \PHP_INT_SIZE; ++$i) {
309-
$bitPos = $i * 8;
311+
$phpIntBitSize = \PHP_INT_SIZE * 8;
312+
for ($bitPos = 0; $bitPos < $phpIntBitSize; $bitPos += 8) {
310313
$bitChk = 0xff << $bitPos;
311314
$byte = $bitset & $bitChk;
312315
if ($byte) {

0 commit comments

Comments
 (0)