ARM NEON convert f32 to s32 with round toward even

1.7k Views Asked by At

Is there any functions that controls rounding mode of vcvt_s32_f32 intrinsic? I want to use round toward even instead of round toward negative infinity.

Thanks.

1

There are 1 best solutions below

0
On

No, you can't change the rounding mode.

NEON is designed for performance rather than precision, and thus is restricted compared to VFP. Unlike VFP, it's not a full IEEE 754 implementation, and is hardwired to certain settings - quoting from the ARM ARM:

  • denormalized numbers are flushed to zero
  • only default NaNs are supported
  • the Round to Nearest* rounding mode selected
  • untrapped exception handling selected for all floating-point exceptions

The specific case of floating-point to integer conversion is slightly different in that the behaviour of the VCVT instruction in this case (for both VFP and NEON) is to ignore the selected rounding mode and always round towards zero. The VCVTR instruction which does use the selected rounding mode is only available in VFP.

The ARMv8 architecture introduced a whole bunch of rounding and conversion instructions for using specific rounding modes, but I suspect that's not much help in this particular case. If you want to do conversions under a different rounding mode on ARMv7 and earlier, you'll either have to use VFP (if available) or some bit-hacking to implement it manually.

* The ARM ARM uses IEEE 754-1985 terminology, so more precisely this is round to nearest, ties to even