r/cpp • u/R3DKn16h7 • 22d ago
Pretty nasty MSVC 19.51 hash of float bug
Hash of 0.0 and -0.0 are not the same, as the condition:
_Keyval == 0.0F ? 0.0F : _Keyval
is optimized away if you compile with:
/arch:AVX2 /O2
https://godbolt.org/z/45WPzceYx
If you use Visual Studio 2022 it works fine. I'm somewhat surprised they don't have regression tests since the standard library explicitly handles this case.
So I guess, check you hash maps if you update.
62
Upvotes
•
u/STL MSVC STL Dev 21d ago edited 21d ago
After investigating, I believe this has already been fixed. (I will use my mod powers to sticky this comment.)
The original test case, and my reduced test case, passes with
-std:c++20 /arch:AVX2 /O2when compiled with the latest MSVC Compiler Preview 19.53.36919. (A frontend dev also confirmed that it passes with 19.52.37030.) I believe that this was fixed by internal MSVC-PR-757910 "[BE] Preserve signed zero in EQ/NE select fold under fp:precise/fp:strict" on 2026-07-14, which according to our records first shipped in the MSVC Compiler Preview 19.52.36615.0. The 19.52 Compiler (aka 14.52 Build Tools) will have its General Availability production release very soon, as it is already a Release Candidate and "Version 14.52 is expected to go stable in November and will ship with the 18.12 release of Visual Studio (Stable Channel)."This PR fixed public bug report DevCom-11119601 "ARM64 misoptimizes +0.0 vs. -0.0." The bug report claimed that this was specific to ARM64 in the 19.51 Compiler, and the internal PR also said "ARM64-only (x64 keeps this as branches, so the OPQUESTION simplifier never sees it)". But I believe that this also fixed the x64
/arch:AVX2scenario, and both the MSFT dev who reported the bug and the backend dev who fixed the bug didn't realize it also affected x64 when/arch:AVX2is used (as that is not a default flag). The fix was in the backend's internal source filessa_optimizer_question.cpp(SSA = "Static Single Assignment", a popular optimization technique throughout the industry, and "question" means the conditional/ternary operator). I know a few things about compiler internals and remembered that the backend has special switches to disable certain optimizations. I was able to confirm that both the original repro and my reduced repro on Compiler Explorer (see https://godbolt.org/z/5M8KGEc5v ) pass when the undocumented and unsupported option to disable SSA optimization of question-operators is passed,-std:c++20 /arch:AVX2 /O2 /d2ssa-patterns-quest-. That convincingly proves to me that this MSVC-PR actually fixed both the ARM64 and x64 repros.34
u/manni66 22d ago edited 22d ago
Is this a bug? Or does it fix a bug?
Edit: it is a bug.
If k1 == k2 is true, h(k1) == h(k2) is also true.
https://en.cppreference.com/cpp/utility/hash
29
u/HommeMusical 22d ago
This is certainly a bug, but hashing floats is very often a bad idea, for exactly the same reason that comparing floats for equality is a bad idea: two numbers that only differ in the 53rd bit, the least significant bit, would hash to totally different.
Given you have a minus zero, it has to be that you are doing floating calculations, so comparing for equality at the end seems fraught with risks.
Why are you doing this?
10
u/AKostur 22d ago
0.0 and -0.0 aren’t the same value: I wouldn’t expect them to hash to the same value.
3
u/FrogNoPants 21d ago
A simple solution is to bit cast your float to an integer, and now the comparison returns false, and the problem no longer exists, yw. In fact you should probably be doing this to avoid it shitting itself on NANs anyway.
1
u/Depixelate_me 20d ago
I wish vs2022 had this bug as it would mean we have an ABI stable c++23 option in IDE...:(
This greediness (we paid good money for vs2022!) is how you get people to explore other windows c++ dev solutions...
0
u/PoorAnalysis 22d ago
There are plenty of data types where different underlying bit patterns actually represent the same conceptual value, and should therefore "compare" equal even though their hashes wouldn't match.
Floats are one such type.
Unless std::hash is required to apply some sort of normalization before hashing, I don't see how this is a bug, or avoidable.