Does the Double (Float64) use the M7 CPU registers by default in TeensyDuino and compiler ???

Angelo

Well-known member
I use Float64 in a Teensy 4 GPS application to compute things like line crossing detection, distance measurements, and probably RTK in the future.
I know the 600MHz is waaayyyy enough to compute the GPS 25Hz outputs, even unsing ieee library.
But it would be nice to use what the M7 core offers.
Are the Float64 enabled to use the M7 float64 registers by default ???
If not, what is the compiler flag to use ???
I use PlatformIO, but could switch to Arduino IDE.
 
Last edited:
I can't answer about PlatformIO, but under Arduino, if you are building for the Teensy 4.0, the Teensy 4.1, or the Teensy MicroMod, the default for double is 64-bit and constants are 64-bit by default. The hardware has direct support for both 32-bit and 64-bit floating point.

If you have the earlier Teensy 3.5 or 3.6, the default for double is 64-bit, but constants are 32-bit by default. The hardware has support for 32-bit floating point, but 64-bit is emulated in the library.

If you have the earlier Teensy LC, 3.2, 3.1, or 3.0, the default for double is 64-bit, but constants are 32-bit by default. Both 32-bit and 64-bit floating point is emulated in the library.

I believe if you have the Teensy 2.0 or 2.0++, the default for double is 32-bit. The 32-bit floating point is all done via emulation.
 
I realise my question was not clear, and I corrected it in the first post and title.
The right question is : are the Double handled by M7 core hardware by default ?
 
Yes, Teensy 4.x has hardware FPU for both 32 bit float and 64 bit double. It's enabled by default. Usage is automatic, just use "float" or "double" variables in your code.

The FPU generally does basic 32 bit float math in 1 cycle, but takes 2 cycles for basic 64 bit double math.

However, some older versions of PlatformIO mistakenly had the single precision constant flag, which we use on Teensy 3.x. PJRC never published any version for use with Arduino IDE using that flag on Teensy 4.x. It was purely a mistake in PlatformIO. It also has no effect at runtime, so 64 bit FPU is still used by the hardware. It only changes how the compiler treats your code at compile time, using only 32 bits for constants. If you use PlatformIO and care about 64 bit precision, check your compiler settings to make sure you're not using that flag. It has indeed come up a few times on this forum... sometimes where people didn't initially mention they were using PlatformIO.
 
Last edited:
Another small gotcha is printing 64 bit double, or even 32 bit float. While Serial.print() does handle 64 bit double, internally it uses a very simple algorithm that limits the integer portion magnitude to 2^32 and probably doesn't handle round off ideally in some corner cases. If you care deeply about full 64 bit precision, you should probably prefer to use Serial.printf(), which uses the newlib C library.
 
Another small gotcha is printing 64 bit double, or even 32 bit float. While Serial.print() does handle 64 bit double, internally it uses a very simple algorithm that limits the integer portion magnitude to 2^32 and probably doesn't handle round off ideally in some corner cases. If you care deeply about full 64 bit precision, you should probably prefer to use Serial.printf(), which uses the newlib C library.
IIRC, you also don't want to use the 'optimize space' option in Arduino, as I believe it uses a version of printf that does not have floating point support.
 
Back
Top