mirror of
https://github.com/ziglang/zig.git
synced 2025-12-20 05:03:06 +00:00
std.hash: auto hash signed ints as bitcasts of unsigned ints
This commit is contained in:
parent
e0179640d5
commit
34dae73005
@ -91,7 +91,12 @@ pub fn hash(hasher: anytype, key: anytype, comptime strat: HashStrategy) void {
|
|||||||
|
|
||||||
// Help the optimizer see that hashing an int is easy by inlining!
|
// Help the optimizer see that hashing an int is easy by inlining!
|
||||||
// TODO Check if the situation is better after #561 is resolved.
|
// TODO Check if the situation is better after #561 is resolved.
|
||||||
.Int => {
|
.Int => |int| switch (int.signedness) {
|
||||||
|
.signed => hash(hasher, @bitCast(@Type(.{ .Int = .{
|
||||||
|
.bits = int.bits,
|
||||||
|
.signedness = .unsigned,
|
||||||
|
} }), key), strat),
|
||||||
|
.unsigned => {
|
||||||
if (comptime meta.trait.hasUniqueRepresentation(Key)) {
|
if (comptime meta.trait.hasUniqueRepresentation(Key)) {
|
||||||
@call(.always_inline, Hasher.update, .{ hasher, std.mem.asBytes(&key) });
|
@call(.always_inline, Hasher.update, .{ hasher, std.mem.asBytes(&key) });
|
||||||
} else {
|
} else {
|
||||||
@ -101,6 +106,7 @@ pub fn hash(hasher: anytype, key: anytype, comptime strat: HashStrategy) void {
|
|||||||
@call(.always_inline, Hasher.update, .{ hasher, std.mem.asBytes(&key)[0..byte_size] });
|
@call(.always_inline, Hasher.update, .{ hasher, std.mem.asBytes(&key)[0..byte_size] });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
|
||||||
.Bool => hash(hasher, @boolToInt(key), strat),
|
.Bool => hash(hasher, @boolToInt(key), strat),
|
||||||
.Enum => hash(hasher, @enumToInt(key), strat),
|
.Enum => hash(hasher, @enumToInt(key), strat),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user