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

Commit 6780daa

Browse files
committed
Don't avoid sign-extension when hashing byte/short/int
There is not a reason to keep this and removing it lets us remove a template.
1 parent 227a925 commit 6780daa

File tree

6 files changed

+10
-32
lines changed

6 files changed

+10
-32
lines changed

src/core/internal/hash.d

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -129,25 +129,6 @@ private template canBitwiseHash(T)
129129
}
130130
}
131131

132-
private template UnqualUnsigned(T) if (__traits(isIntegral, T) && !is(T == __vector))
133-
{
134-
static if (T.sizeof == ubyte.sizeof) alias UnqualUnsigned = ubyte;
135-
else static if (T.sizeof == ushort.sizeof) alias UnqualUnsigned = ushort;
136-
else static if (T.sizeof == uint.sizeof) alias UnqualUnsigned = uint;
137-
else static if (T.sizeof == ulong.sizeof) alias UnqualUnsigned = ulong;
138-
else static if (T.sizeof == ulong.sizeof * 2)
139-
{
140-
static assert(T.sizeof == ucent.sizeof);
141-
alias UnqualUnsigned = ucent;
142-
}
143-
else
144-
{
145-
static assert(0, "No known unsigned equivalent of " ~ T.stringof);
146-
}
147-
148-
static assert(UnqualUnsigned.sizeof == T.sizeof && __traits(isUnsigned, UnqualUnsigned));
149-
}
150-
151132
// Overly restrictive for simplicity: has false negatives but no false positives.
152133
private template useScopeConstPassByValue(T)
153134
{
@@ -344,7 +325,7 @@ if (!is(T == enum) && !is(T : typeof(null)) && is(T S: S[]) && !__traits(isStati
344325
size_t hashOf(T)(scope const T val) if (!is(T == enum) && __traits(isArithmetic, T)
345326
&& __traits(isIntegral, T) && T.sizeof <= size_t.sizeof && !is(T == __vector))
346327
{
347-
return cast(UnqualUnsigned!T) val;
328+
return val;
348329
}
349330

350331
//arithmetic type hash
@@ -371,10 +352,10 @@ size_t hashOf(T)(scope const T val, size_t seed) if (!is(T == enum) && __traits(
371352
enum uint r1 = 31;
372353
enum uint r2 = 27;
373354
}
374-
auto h = c1 * cast(UnqualUnsigned!T) val;
375-
h = (h << r1) | (h >>> (typeof(h).sizeof * 8 - r1));
355+
size_t h = c1 * val;
356+
h = (h << r1) | (h >>> (size_t.sizeof * 8 - r1));
376357
h = (h * c2) ^ seed;
377-
h = (h << r2) | (h >>> (typeof(h).sizeof * 8 - r2));
358+
h = (h << r2) | (h >>> (size_t.sizeof * 8 - r2));
378359
return h * 5 + c3;
379360
}
380361

src/rt/typeinfo/ti_byte.d

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ class TypeInfo_g : TypeInfo
2626

2727
override size_t getHash(scope const void* p)
2828
{
29-
// Hash as if unsigned.
30-
return *cast(const ubyte *)p;
29+
return *cast(const byte *)p;
3130
}
3231

3332
override bool equals(in void* p1, in void* p2)

src/rt/typeinfo/ti_cent.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class TypeInfo_zi : TypeInfo
2828

2929
override size_t getHash(scope const void* p)
3030
{
31-
// Hash as if unsigned.
31+
// cent & ucent hash the same if ucent.sizeof >= size_t.sizeof.
3232
return hashOf(*cast(const ucent*) p);
3333
}
3434

src/rt/typeinfo/ti_int.d

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ class TypeInfo_i : TypeInfo
2626

2727
override size_t getHash(scope const void* p)
2828
{
29-
// Hash as if unsigned.
30-
return *cast(const uint *)p;
29+
return *cast(const int *)p;
3130
}
3231

3332
override bool equals(in void* p1, in void* p2)

src/rt/typeinfo/ti_long.d

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ class TypeInfo_l : TypeInfo
2626

2727
override size_t getHash(scope const void* p)
2828
{
29-
// Hash as if unsigned.
3029
static if (ulong.sizeof <= size_t.sizeof)
31-
return *cast(const ulong*)p;
30+
return *cast(const long*)p;
3231
else
32+
// long & ulong hash the same if ulong.sizeof > size_t.sizeof.
3333
return hashOf(*cast(const ulong*)p);
3434
}
3535

src/rt/typeinfo/ti_short.d

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ class TypeInfo_s : TypeInfo
2626

2727
override size_t getHash(scope const void* p)
2828
{
29-
// Hash as if unsigned.
30-
return *cast(const ushort *)p;
29+
return *cast(const short *)p;
3130
}
3231

3332
override bool equals(in void* p1, in void* p2)

0 commit comments

Comments
 (0)