authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-01 23:45:36-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-10 20:47:58-07:00
log34dae73005baa3be54e0d9e0725ab31cb0723a06
tree3c0730944b48a116c6170580c7f36dae1c34e228
parente0179640d54f4a61aa7522ac8529d36769fb9c08

std.hash: auto hash signed ints as bitcasts of unsigned ints


1 files changed, 15 insertions(+), 9 deletions(-)

lib/std/hash/auto_hash.zig+15-9
...@@ -91,15 +91,21 @@ pub fn hash(hasher: anytype, key: anytype, comptime strat: HashStrategy) void {...@@ -91,15 +91,21 @@ pub fn hash(hasher: anytype, key: anytype, comptime strat: HashStrategy) void {
9191
92 // Help the optimizer see that hashing an int is easy by inlining!92 // Help the optimizer see that hashing an int is easy by inlining!
93 // TODO Check if the situation is better after #561 is resolved.93 // TODO Check if the situation is better after #561 is resolved.
94 .Int => {94 .Int => |int| switch (int.signedness) {
95 if (comptime meta.trait.hasUniqueRepresentation(Key)) {95 .signed => hash(hasher, @bitCast(@Type(.{ .Int = .{
96 @call(.always_inline, Hasher.update, .{ hasher, std.mem.asBytes(&key) });96 .bits = int.bits,
97 } else {97 .signedness = .unsigned,
98 // Take only the part containing the key value, the remaining98 } }), key), strat),
99 // bytes are undefined and must not be hashed!99 .unsigned => {
100 const byte_size = comptime std.math.divCeil(comptime_int, @bitSizeOf(Key), 8) catch unreachable;100 if (comptime meta.trait.hasUniqueRepresentation(Key)) {
101 @call(.always_inline, Hasher.update, .{ hasher, std.mem.asBytes(&key)[0..byte_size] });101 @call(.always_inline, Hasher.update, .{ hasher, std.mem.asBytes(&key) });
102 }102 } else {
103 // Take only the part containing the key value, the remaining
104 // bytes are undefined and must not be hashed!
105 const byte_size = comptime std.math.divCeil(comptime_int, @bitSizeOf(Key), 8) catch unreachable;
106 @call(.always_inline, Hasher.update, .{ hasher, std.mem.asBytes(&key)[0..byte_size] });
107 }
108 },
103 },109 },
104110
105 .Bool => hash(hasher, @boolToInt(key), strat),111 .Bool => hash(hasher, @boolToInt(key), strat),