Hash: fix for masks at upper boundary of what signed ints allow

on 64-bit PHP installs (1 << 63) - 1 == (1 << 63)

-1 ^ (-1 << 63) gives the correct result

on 32-bit PHP installs (1 << 31) - 1 returns a float and using the result as a bitmask yields this error on sufficiently new versions of PHP:

Deprecated: Implicit conversion from float -2147483649 to int loses precision

Explicitly casting (1 << 31) - 1 to an int yields the correct result but, then again, so does -1 ^ (-1 << 31) and that one is consistent with how it works on 64-bit PHP installs
This commit is contained in:
terrafrost 2024-09-08 07:18:47 -05:00
parent 8307eb3b01
commit 28866e826b

View File

@ -1322,9 +1322,10 @@ class Hash
list($lo, $hi) = $x;
}
$mask = -1 ^ (-1 << $shift);
return [
($hi << $shift) | (($lo >> (32 - $shift)) & (1 << $shift) - 1),
($lo << $shift) | (($hi >> (32 - $shift)) & (1 << $shift) - 1)
($hi << $shift) | (($lo >> (32 - $shift)) & $mask),
($lo << $shift) | (($hi >> (32 - $shift)) & $mask)
];
}
@ -1481,7 +1482,8 @@ class Hash
*/
private static function rotateLeft64($x, $shift)
{
return ($x << $shift) | (($x >> (64 - $shift)) & ((1 << $shift) - 1));
$mask = -1 ^ (-1 << $shift);
return ($x << $shift) | (($x >> (64 - $shift)) & $mask);
}
/**