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

Commit 24e4042

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

File tree

1 file changed

+90
-7
lines changed

1 file changed

+90
-7
lines changed

src/core/internal/hash.d

+90-7
Original file line numberDiff line numberDiff line change
@@ -623,14 +623,17 @@ 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 = bytesHash32!(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
634+
static if (size_t.sizeof <= 4 && (SizeAndAlignType.alignof < uint.alignof
632635
? SizeAndAlignType.sizeof <= 12
633-
: SizeAndAlignType.sizeof <= 10)
636+
: SizeAndAlignType.sizeof <= 10))
634637
alias bytesHashWithExactSizeAndAlignment = smallBytesHash;
635638
else
636639
alias bytesHashWithExactSizeAndAlignment = bytesHashAlignedBy!SizeAndAlignType;
@@ -670,8 +673,9 @@ private uint get32bits()(scope const(ubyte)* x) @nogc nothrow pure @system
670673
Params:
671674
dataKnownToBeAligned = whether the data is known at compile time to be uint-aligned.
672675
+/
676+
static if (size_t.sizeof == 4)
673677
@nogc nothrow pure @trusted
674-
private size_t bytesHash(bool dataKnownToBeAligned)(scope const(ubyte)[] bytes, size_t seed)
678+
private uint bytesHash32(bool dataKnownToBeAligned)(scope const(ubyte)[] bytes, size_t seed)
675679
{
676680
auto len = bytes.length;
677681
auto data = bytes.ptr;
@@ -725,6 +729,84 @@ private size_t bytesHash(bool dataKnownToBeAligned)(scope const(ubyte)[] bytes,
725729
return h1;
726730
}
727731

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

0 commit comments

Comments
 (0)