Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do denormal flags like Denormals-Are-Zero (DAZ) affect comparisons for equality?

If I have 2 denormal floating point numbers with different bit patterns and compare them for equality, can the result be affected by the Denormals-Are-Zero flag, the Flush-to-Zero flag, or other flags on commonly used processors?

Or do these flags only affect computation and not equality checks?

like image 487
Zachary Burns Avatar asked Jan 04 '19 23:01

Zachary Burns


1 Answers

DAZ (Denormals Are Zero) affects reading input, so DAZ affects compares. All denormals are literally treated as -0.0 or +0.0, according to their sign.

FTZ (Flush To Zero) affects only writing output, so FTZ doesn't affect compares. Compares don't produce an FP output, so there's nothing to flush.


(DAZ and FTZ are flags in the SSE MXCSR control/status register. There is no equivalent for x87.)


Why do both flags exist separately, instead of one flag controlling both things? I don't know, but you definitely need both effects.

You might be reading FP data from a file, or from the network, so you could get denormal inputs directly, not the result of your calculations. So you need DAZ for that.

Producing a correct denormal output (e.g. from subtracting nearby normal numbers, or multiplying two small normal numbers) is potentially slow, so you need FTZ to allow the CPU to just underflow to +-0.0 instead of taking a microcode assist to get the right value.


Or you might have one thread run with fast-math (FTZ+DAZ), while another thread runs with precise math for some computations that require it. If the fast-math thread reads data produced by the precise thread, those numbers might be denormals.

I'm not sure what a use-case would be for setting DAZ but not FTZ, or vice versa. It would certainly involve reading data you didn't produce yourself, or writing data for readers other than yourself, otherwise just FTZ is sufficient to avoid any slowdowns.

(Just DAZ alone is not sufficient: producing a denormal output from normal inputs is still slow, even if the next operation treats it as 0.)

like image 169
Peter Cordes Avatar answered Nov 08 '22 06:11

Peter Cordes