Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit b28789a

Browse files
committed
Fix Issue 21006 - core.internal.hash.bytesHash: in 64-bit builds use a 64-bit-oriented algorithm
1 parent addfbeb commit b28789a

File tree

1 file changed

+88
-7
lines changed

1 file changed

+88
-7
lines changed

src/core/internal/hash.d

+88-7
Original file line numberDiff line numberDiff line change
@@ -623,14 +623,15 @@ size_t bytesHash()(scope const(void)* buf, size_t len, size_t seed)
623623

624624
private template bytesHashAlignedBy(AlignType)
625625
{
626-
alias bytesHashAlignedBy = bytesHash!(AlignType.alignof >= uint.alignof);
626+
static if (size_t.sizeof == 4)
627+
alias bytesHashAlignedBy = bytesHash!(AlignType.alignof >= uint.alignof);
628+
else
629+
alias bytesHashAlignedBy = bytesHash64!();
627630
}
628631

629632
private template bytesHashWithExactSizeAndAlignment(SizeAndAlignType)
630633
{
631-
static if (SizeAndAlignType.alignof < uint.alignof
632-
? SizeAndAlignType.sizeof <= 12
633-
: SizeAndAlignType.sizeof <= 10)
634+
static if (size_t.sizeof <= 3)
634635
alias bytesHashWithExactSizeAndAlignment = smallBytesHash;
635636
else
636637
alias bytesHashWithExactSizeAndAlignment = bytesHashAlignedBy!SizeAndAlignType;
@@ -670,6 +671,7 @@ private uint get32bits()(scope const(ubyte)* x) @nogc nothrow pure @system
670671
Params:
671672
dataKnownToBeAligned = whether the data is known at compile time to be uint-aligned.
672673
+/
674+
static if (size_t.sizeof == 4)
673675
@nogc nothrow pure @trusted
674676
private size_t bytesHash(bool dataKnownToBeAligned)(scope const(ubyte)[] bytes, size_t seed)
675677
{
@@ -725,6 +727,84 @@ private size_t bytesHash(bool dataKnownToBeAligned)(scope const(ubyte)[] bytes,
725727
return h1;
726728
}
727729

730+
static if (size_t.sizeof == 8)
731+
@nogc nothrow pure @trusted
732+
private ulong bytesHash64()(scope const ubyte[] bytes, ulong seed)
733+
{
734+
alias h1 = seed;
735+
736+
enum ulong c1 = 0x87c37b91114253d5;
737+
enum ulong c2 = 0x4cf5ad432745937f;
738+
enum uint c3 = 0x52dce729;
739+
740+
const(ubyte)* data = bytes.ptr;
741+
//----------
742+
// body
743+
for (const end_data = bytes.ptr + (bytes.length & ~7);
744+
data !is end_data;
745+
data += 8)
746+
{
747+
version (BigEndian)
748+
{
749+
auto k1 =
750+
((cast(ulong) data[0]) << 56) |
751+
((cast(ulong) data[1]) << 48) |
752+
((cast(ulong) data[2]) << 40) |
753+
((cast(ulong) data[3]) << 32) |
754+
((cast(ulong) data[4]) << 24) |
755+
((cast(ulong) data[5]) << 16) |
756+
((cast(ulong) data[6]) << 8) |
757+
(cast(ulong) data[7]);
758+
}
759+
else
760+
{
761+
auto k1 =
762+
((cast(ulong) data[7]) << 56) |
763+
((cast(ulong) data[6]) << 48) |
764+
((cast(ulong) data[5]) << 40) |
765+
((cast(ulong) data[4]) << 32) |
766+
((cast(ulong) data[3]) << 24) |
767+
((cast(ulong) data[2]) << 16) |
768+
((cast(ulong) data[1]) << 8) |
769+
(cast(ulong) data[0]);
770+
}
771+
772+
k1 *= c1;
773+
k1 = (k1 << 31) | (k1 >> (64 - 31));
774+
k1 *= c2;
775+
776+
h1 ^= k1;
777+
h1 = (h1 << 27) | (h1 >> (64 - 27));
778+
h1 = h1*5+c3;
779+
}
780+
781+
//----------
782+
// tail
783+
ulong k1 = 0;
784+
785+
switch (bytes.length & 7)
786+
{
787+
case 7: k1 ^= (cast(ulong) data[6]) << 48; goto case;
788+
case 6: k1 ^= (cast(ulong) data[5]) << 40; goto case;
789+
case 5: k1 ^= (cast(ulong) data[4]) << 32; goto case;
790+
case 4: k1 ^= (cast(ulong) data[3]) << 24; goto case;
791+
case 3: k1 ^= (cast(ulong) data[2]) << 16; goto case;
792+
case 2: k1 ^= (cast(ulong) data[1]) << 8; goto case;
793+
case 1: k1 ^= (cast(ulong) data[0]);
794+
k1 *= c1; k1 = (k1 << 31) | (k1 >> (64 - 31)); k1 *= c2; h1 ^= k1;
795+
goto default;
796+
default:
797+
}
798+
799+
//----------
800+
// finalization
801+
h1 ^= bytes.length;
802+
// Force all bits of the hash block to avalanche.
803+
h1 = (h1 ^ (h1 >> 33)) * 0xff51afd7ed558ccd;
804+
h1 = (h1 ^ (h1 >> 33)) * 0xc4ceb9fe1a85ec53;
805+
return h1 ^= h1 >> 33;
806+
}
807+
728808
// Check that bytesHash works with CTFE
729809
pure nothrow @system @nogc unittest
730810
{
@@ -750,7 +830,8 @@ pure nothrow @system @nogc unittest
750830
}
751831
// It is okay to change the below values if you make a change
752832
// that you expect to change the result of bytesHash.
753-
assert(bytesHash(&a[1], a.length - 2, 0) == 2727459272);
754-
assert(bytesHash(&b, 5, 0) == 2727459272);
755-
assert(bytesHashAlignedBy!uint((cast(const ubyte*) &b)[0 .. 5], 0) == 2727459272);
833+
enum expectedResult = size_t.sizeof == 4 ? 2727459272U : 10677742034643552556UL;
834+
assert(bytesHash(&a[1], a.length - 2, 0) == expectedResult);
835+
assert(bytesHash(&b, 5, 0) == expectedResult);
836+
assert(bytesHashAlignedBy!uint((cast(const ubyte*) &b)[0 .. 5], 0) == expectedResult);
756837
}

0 commit comments

Comments
 (0)