Open pixelflinger opened 1 year ago
We could use respectively 9 and 17 digits, which is always enough
This approach failed in one of my tests, but maybe I did something wrong. Either way, shortening the string would be great, it's been a long standing todo that just never bubbled up as something critical, but we need to have a strategy on how we're going to verify it if we're going to implement this. For FP32 we can roundtrip every possible value in a reasonable amount of time, and we should be able to test a reasonable subset of FP64 as well.
@HansKristian-Work there is some good information here (as to why %.9g
works for floats):
https://randomascii.wordpress.com/2013/02/07/float-precision-revisited-nine-digit-float-portability
Currently spirv-cross uses
%.32g
by default to formatfloats
, this can be overridden withSPIRV_CROSS_FLT_FMT
at compile time. When embedding shaders with binaries, reducing their binary footprint can be important.What really matters is to preserve the binary representation of the float in the SPIRV file. 32 digits is overkill for
floats
anddoubles
. We could use respectively 9 and 17 digits, which is always enough, however, this would still generate strings larger than needed.Ideally, we'd want to use the smallest string that preserves the binary representation.
For instance, I have tested the following implementation for
floats
only:For
doubles
andlong doubles
it might better to binary-search the right format length.