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 {
9191
9292 // Help the optimizer see that hashing an int is easy by inlining!
9393 // TODO Check if the situation is better after #561 is resolved.
94 .Int => {
95 if (comptime meta.trait.hasUniqueRepresentation(Key)) {
96 @call(.always_inline, Hasher.update, .{ hasher, std.mem.asBytes(&key) });
97 } else {
98 // Take only the part containing the key value, the remaining
99 // bytes are undefined and must not be hashed!
100 const byte_size = comptime std.math.divCeil(comptime_int, @bitSizeOf(Key), 8) catch unreachable;
101 @call(.always_inline, Hasher.update, .{ hasher, std.mem.asBytes(&key)[0..byte_size] });
102 }
94 .Int => |int| switch (int.signedness) {
95 .signed => hash(hasher, @bitCast(@Type(.{ .Int = .{
96 .bits = int.bits,
97 .signedness = .unsigned,
98 } }), key), strat),
99 .unsigned => {
100 if (comptime meta.trait.hasUniqueRepresentation(Key)) {
101 @call(.always_inline, Hasher.update, .{ hasher, std.mem.asBytes(&key) });
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 },
103109 },
104110
105111 .Bool => hash(hasher, @boolToInt(key), strat),