Twinklebear / embree-rs

Rust bindings to Embree 3
MIT License
35 stars 14 forks source link

Enable FTZ and DAZ flags #14

Open matthiascy opened 2 years ago

matthiascy commented 2 years ago

Hi, @Twinklebear,

In the mentioned code, I think _MM_SET_FLUSH_ZERO_MODE is insufficient to enable FTZ and DAZ at the same time. https://github.com/Twinklebear/embree-rs/blob/41bf8b8d183f21bc07b8da7ea040e4293ff21f28/src/device.rs#L14-L21

In rust stdarch, _MM_SET_FLUSH_ZERO_ON is defined as 0x8000 which is the flag for FTZ. As for the flag of DAZ 0x0040, it's not defined. That's why Embree always gives the warning

================================================================================ WARNING: "Flush to Zero" or "Denormals are Zero" mode not enabled

There exists a related issue in stdarch trying to add the constant for DAZ flag, but it's been 2 years, nothing changed.

For the moment, we can only enable both FTZ and DAZ by calling _mm_setcsr() with their corresponding flags:

let flag = (1 << 15) | (1 << 6); // the same as `_MM_FLUSH_ZERO_ON | 0x0040`
unsafe {
    let csr = (_mm_getcsr() & !flag) | flag;
    _mm_setcsr(csr);
}