EmbeddedRelated.com
Forums

Does GCC use single-precision FP hardware on Cortex-M4 parts?

Started by Dave Nadler September 24, 2016
On Wednesday, September 28, 2016 at 8:50:31 AM UTC-4, Anders....@kapsi.spam.stop.fi.invalid wrote:
> Tim Wescott <tim@seemywebsite.com> wrote: > > On Sat, 24 Sep 2016 09:19:58 -0700, Dave Nadler wrote: > > >> The PIC32MZ EF series parts have hardware double precision; > >> presumably GCC uses this for double? > > I would check. You may have to direct the compiler to do so. > > By default Microchip's XC32 compiler uses 32-bit doubles. There is a > compiler switch for 64-bit doubles, but there have been several reports > on their forums about the wrong libraries being linked in or other such > nonsense.
Yes, this is lovely stuff: http://www.microchip.com/forums/m940391.aspx Thanks for the heads-up on -fno-short-double. I expected this for PIC24, but a bit surprising for PIC32? For some of our applications, 23 bits of precision doesn't do it... Thanks Anders, Best Regards, Dave
On Sunday, September 25, 2016 at 6:29:40 AM UTC-4, David Brown wrote:
> Yes, you need to tell gcc about the processor you are using: > > -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 > > You may also want to add "-fsingle-precision-constant" which makes gcc > treat floating point constants as single precision, so that "x * 2.5" is > done as single precision rather than double precision (without having to > write "x * 2.5f"). > > And you /definitely/ want to have "-Wdouble-promotion" to warn about any > implicit promotions to double that might have occurred accidentally. > (You can always cast to double, or assign values to double variables, if > you really want slow but accurate doubles in the code.) > > > As for the maths library, "sinf", "cosf", and friends are standard > functions for single-precision versions of the trigonometric functions. > But remember that there is no such thing as the "gcc library" - gcc > can come with a variety of libraries. Some of these will have good > implementations of "sinf" and friends, done using only single-precision > floating point in order to be fast on a the M4F and similar processors. > Others simply convert their argument to double, call "sin", then > convert the result back to single. If you need to use standard library > maths functions, and you need the speed of single precision, then check > the details for the libraries available in the particular toolchain you > have. > > Of course, when you need fast trig functions, it is usually best to have > tables, interpolations, or other approximations that give you the > required accuracy far faster than any IEEE-compliant standard library. > > > > (In C++ one _could_ have a smart math library that would see the type of > > the argument and call the correct library function -- I don't believe > > that's done, and I could see it causing all sorts of trouble when things > > get inadvertently up-cast to double as you maintain the code.) > > If you are going for C++, you can easily create a wrapper around "float" > that does not have any implicit conversions to double so that you can be > sure to avoid accidents. > > Or you can use the gcc flags I mentioned above :-)
Thanks David for your (as usual) clear explanation, Best Regards, Dave
On Sunday, September 25, 2016 at 4:19:20 AM UTC-4, John Devereux wrote:
> FYI the latest M7 processors from ST have double-precision floating > point e.g. STM32F769 > > <http://www.st.com/content/st_com/en/products/microcontrollers/stm32-32-bit-arm-cortex-mcus/stm32f7-series.html?querycriteria=productId=SS1858>
Thanks John, we'll add the ST part to the list...