0

I have two functions for drawing a 3D line. Function uint32 getVertCount() determines the number of vertices I'll need to allocate, while function void setVerts(void * v) also determines the number of vertices, using the same math and inputs while doing unrelated math throughout, and assumes this count matches what the first function determined.

These functions run in different threads on the same CPU, using 32-bit float. The number of vertices needed is very sensitive to small variations in the count calculation.

Can I assume these functions will arrive to the same count always, given the same inputs and given I write the count calculation with the same operations in the same order?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Anne Quinn
  • 12,609
  • 8
  • 54
  • 101
  • 2
    As long as you're using 64-bit double floats in SSE mode everything should match up. If you ever start to mix 80-bit x87 operations in things can get hinky if your result depends on particular rounding. The compilers will freely mix and match 80-bit and 64-bit rounding almost at random as a result of register allocation and memory spilling. – Zan Lynx Sep 22 '20 at 00:21
  • @ZanLynx - What if the inputs and operations use 32-bit float? (modified my question to specify) – Anne Quinn Sep 22 '20 at 00:25
  • 2
    Compilers aren't obligated to perform operations in limited precision. The `float` 32-bit type only specifies its in-memory storage, not what registers the CPU or GPU will use to calculate with. When it gets stored back to RAM it will get rounded to 32 bit. But, as I said, you have no control over when a value gets written to RAM. – Zan Lynx Sep 22 '20 at 00:34
  • At any rate, if calculating in higher precision causes a problem then in my opinion the code is wrong anyway. Having a better answer should always be a good thing. – Zan Lynx Sep 22 '20 at 00:36
  • 2
    You must also make sure that no sort of `fast-math` optimizations are enabled (which ICC does by default, or it might be hidden somewhere in your build-chain). – chtz Sep 22 '20 at 00:47
  • Well, GCC has `-ffloat-store` and there's several other options related to floating point. Not sure what MSVC does. – Zan Lynx Sep 22 '20 at 00:49
  • https://en.wikipedia.org/wiki/Floating-point_error_mitigation – Zan Lynx Sep 22 '20 at 01:01
  • 2
    @ZanLynx float in SSE will also be determinable. Only if you use x87 then things are complicated. In short, just avoid x87 – phuclv Sep 22 '20 at 01:57
  • 1
    @AnneQuinn Could you clarify in the question whether these two functions are compiled separately or within the same compilation unit, and if the former, whether they are compiled with the same compiler, with the exact same compiler settings and compiled against physically identical libraries? – njuffa Sep 22 '20 at 02:15
  • 1
    [How can floating point calculations be made deterministic?](https://stackoverflow.com/q/7365790/995714), [Cross Platform Floating Point Consistency](https://stackoverflow.com/q/20963419/995714), [How to keep float/double arithmetic deterministic?](https://stackoverflow.com/q/46796126/995714) – phuclv Sep 22 '20 at 04:10
  • The API appears to be C, not C++. The whole problem goes away with `std::vector getVerts();` – MSalters Sep 22 '20 at 08:40

0 Answers0