diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2023-06-01 23:45:36 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2023-06-10 20:47:58 -0700 |
| commit | 34dae73005baa3be54e0d9e0725ab31cb0723a06 (patch) | |
| tree | 3c0730944b48a116c6170580c7f36dae1c34e228 | |
| parent | e0179640d54f4a61aa7522ac8529d36769fb9c08 (diff) | |
| download | zig-34dae73005baa3be54e0d9e0725ab31cb0723a06.tar.gz zig-34dae73005baa3be54e0d9e0725ab31cb0723a06.zip | |
std.hash: auto hash signed ints as bitcasts of unsigned ints
| -rw-r--r-- | lib/std/hash/auto_hash.zig | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/lib/std/hash/auto_hash.zig b/lib/std/hash/auto_hash.zig index 0c88caae7e..251ac120f6 100644 --- a/lib/std/hash/auto_hash.zig +++ b/lib/std/hash/auto_hash.zig @@ -91,15 +91,21 @@ pub fn hash(hasher: anytype, key: anytype, comptime strat: HashStrategy) void { // Help the optimizer see that hashing an int is easy by inlining! // TODO Check if the situation is better after #561 is resolved. - .Int => { - if (comptime meta.trait.hasUniqueRepresentation(Key)) { - @call(.always_inline, Hasher.update, .{ hasher, std.mem.asBytes(&key) }); - } else { - // Take only the part containing the key value, the remaining - // bytes are undefined and must not be hashed! - const byte_size = comptime std.math.divCeil(comptime_int, @bitSizeOf(Key), 8) catch unreachable; - @call(.always_inline, Hasher.update, .{ hasher, std.mem.asBytes(&key)[0..byte_size] }); - } + .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)) { + @call(.always_inline, Hasher.update, .{ hasher, std.mem.asBytes(&key) }); + } else { + // Take only the part containing the key value, the remaining + // bytes are undefined and must not be hashed! + const byte_size = comptime std.math.divCeil(comptime_int, @bitSizeOf(Key), 8) catch unreachable; + @call(.always_inline, Hasher.update, .{ hasher, std.mem.asBytes(&key)[0..byte_size] }); + } + }, }, .Bool => hash(hasher, @boolToInt(key), strat), |
