cancel
Showing results for 
Search instead for 
Did you mean: 

Cortex M4 Float to int: Round to Nearest / VCVTR?

flyer31
Senior
Posted on June 12, 2015 at 12:31

Is there any way to convince the Keil/ARM MDK compiler to use the VCVTR command to convert floats to ints?

That would be extremely nice.

Currently the compiler seems to use always the VCVT, but this rounds towards zero.

With the VCVTR command it is possible to round automatically towards nearest integer ...

Any idea?

Anybody knows how this is solved in GCC / IAR?
4 REPLIES 4
Posted on June 12, 2015 at 13:02

This is the requirement of C standard (C99 6.3.1.4#1), so you simply can't in C.

You can resort to inline assembler, though, for explicit conversions; get inspired by the intrinsic functions in the CMSIS headers.

JW
flyer31
Senior
Posted on June 12, 2015 at 13:23

You want to tell me, that truncating floats is a requirement in C?

... please don't phantasize.

C clearly would require of course, that floats are converted to nearest integer ... .

Truncating floats is quite abnormal (just unfortunately the standard VCVT round mode ... fortunately ARM supports the possibility to use VCVTR, and then ''rounding to nearest int'' can be specified as standard rounding mode (via the FPSCR register) ... but UNfortunately again Keil / ARM compiler seems to refuse to use VCVTR - even there is no intrinsic possibility to use VCVTR ... use of inline assembly really would be a hard task for the programmer - if you use float in a software, then float to int will happen really very often, and if you always have to program this conversion explicitely, it really gets extremely tedious (and possibly dangerous ..)... ).

Posted on June 12, 2015 at 13:45

> You want to tell me, that truncating floats is a requirement in C?

>

> ... please don't phantasize.

>

> C clearly would require of course, that floats are converted to nearest integer ... .

>

I gave you the chapter and verse.

JW

flyer31
Senior
Posted on June 12, 2015 at 13:58

ok, surprise surprise - I found this - really very strange spec, sorry.

So then I would assume that any other C compiler I used so far with floating points (mainly Borland C / Microsoft C is non-standard ...).

But it really makes not much sense and is quite dangerous, I think.

It really would be VERY nice to convince the Compiler somehow to use VCVTR.

Do you know how GCC / IAR is handling this?