3

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.

sandye51
  • 91
  • 9
  • I don't quite get that "round to even" Could you give an example what it is supposed to do? Maybe then I could write a bit hacking routine doing that for you. – Jake 'Alquimista' LEE Sep 24 '14 at 00:48
  • Ok, I got it. Still, I don't know how to handle 1.0 and -1.0 for example. The simple bit hacking algorithm of mine will turn them to 2 and 0 each. Is it ok? – Jake 'Alquimista' LEE Sep 24 '14 at 02:23
  • I mean that 2.5 must be 2, 3.5 must be 4 and so on. – sandye51 Sep 24 '14 at 04:08
  • but what about 1 and -1? they aren't even. That's what I was asking about. – Jake 'Alquimista' LEE Sep 24 '14 at 10:09
  • Hmm, unless of course, given the "bankers' rounding" tag, we're actually talking "Round to nearest, _ties_ to even". In that case, that's what NEON already does! (the "negative infinity" stated in the question is incorrect) – Notlikethat Sep 24 '14 at 18:55
  • Neon simply truncates floating part, but I want 2 for 2.5, 4 for 3.5, 2 for 2.4, 3 for 2.6, etc – sandye51 Sep 25 '14 at 07:39
  • 1
    Ah, on closer inspection it's the VCVT instruction's "always round towards zero" behaviour rather than NEON specifically. Still, I think everything here still applies. – Notlikethat Sep 25 '14 at 18:08

1 Answers1

5

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

Notlikethat
  • 18,119
  • 2
  • 34
  • 67