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

被刻印的时光 ゝ 提交于 2021-02-08 19:42:39

问题


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?


回答1:


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.)



来源:https://stackoverflow.com/questions/54047415/do-denormal-flags-like-denormals-are-zero-daz-affect-comparisons-for-equality

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!