Usually I do *not* use floating point numbers in my projects. For this, I select ‘None’ during the project creation in CodeWarrior for MCU:
But what if I need to change my mind later? How to change such a ‘no-floating-point-needed’ project to one with floating point format support?
No Floating Point, that’s the default
Clearly, in my view, using floating point numbers in many projects is a bad thing, similar like using printf(). Because it adds a huge overhead and performance penalty especially to small microprocessors like the Freescale HCS08. And because in many cases using floating point numbers is not really needed.
No Floats needed?
I have seen to many cases where projects where using floating point number say to represent a temperature. Say to display temperature values like 37.5°. That looks like a good reason for using floating point numbers? Nope. I better use ‘centiGrades’, and instead of using
float temperature; /* e.g. 37.51 degree celcsius */
it is better to use ‘centigrades’:
uint16_t temperature; /* in centigrades, so 3751 is 37.51 degree celcsius */
Way better and more efficient. If I have plenty of RAM and ROM available with a powerful processor, why not. Otherwise: I keep things simple.
Changing my mind?
But if there is a real good case indeed to use a floating point number representation, then it is not that hard to change an existing project for the S08 to use floating point numbers. There are the following things which make the difference:
- ‘No-Float’ projects have the __NO_FLOAT__ compiler define set
- There are different libraries depending on the ‘float’ or ‘no-float’ settings
- Compiler switches for ‘float’ format
Knowing the mechanics allow me to change a project from ‘no-float’ to ‘use floating point’ and back again, without the need to create a new project.
‘No-Floating-Point’ project have the ‘__NO_FLOAT__’ preprocessor macro set:
So depending if I want floating point or not, I need to add or remove that macro.
The library itself needs to support floating point (or only integral types). Which library is used, this is set in the Linker Input settings of the project:
In the above example I’m using a library without floating point support, marked with the ‘i’ (for integral types only) at the end of the name. The readme.txt inside the MCU\lib\hc08c folder lists all the different libraries with the compiler options used:
Project Name Memory Model Library Compiler Options ------------------------------------------------------------------------------------ c_ieee_32_32 small ansif.lib -W2 -Os -Ms -Fd -F2 -NoPath c_ieee_32_64 small ansi.lib -W2 -Os -Ms -F2 -NoPath c_no_float only small ansii.lib -W2 -Os -Ms -D__NO_FLOAT__ -F2 -NoPath c++_ieee_32_32 small cppf.lib -W2 -Os -Ms -Fd -F2 -NoPath c++_ieee_32_64 small cpp.lib -W2 -Os -Ms -F2 -NoPath c++_no_float only small cppi.lib -W2 -Os -Ms -D__NO_FLOAT__ -F2 -NoPath c_tiny_ieee_32_32 tiny ansitf.lib -W2 -Os -Mt -Fd -F2 -NoPath c_tiny_ieee_32_64 tiny ansit.lib -W2 -Os -Mt -F2 -NoPath c_tiny_no_float tiny ansiti.lib -W2 -Os -Mt -D__NO_FLOAT__ -F2 -NoPath c++_tiny_ieee_32_32 tiny cpptf.lib -W2 -Os -Mt -Fd -F2 -NoPath c++_tiny_ieee_32_64 tiny cppt.lib -W2 -Os -Mt -F2 -NoPath c++_tiny_no_float tiny cppti.lib -W2 -Os -Mt -D__NO_FLOAT__ -F2 -NoPath hcs08_c_ieee_32_32 small HCS08 ansifs.lib -W2 -Os -cs08 -Ms -Fd -F2 -NoPath hcs08_c_ieee_32_64 small HCS08 ansis.lib -W2 -Os -cs08 -Ms -F2 -NoPath hcs08_c_no_float only small HCS08 ansiis.lib -W2 -Os -cs08 -Ms -D__NO_FLOAT__ -F2 -NoPath hcs08_c++_ieee_32_32 small HCS08 cppfs.lib -W2 -Os -cs08 -Ms -Fd -F2 -NoPath hcs08_c++_ieee_32_64 small HCS08 cpps.lib -W2 -Os -cs08 -Ms -F2 -NoPath hcs08_c++_no_float only small HCS08 cppis.lib -W2 -Os -cs08 -Ms -D__NO_FLOAT__ -F2 -NoPath hcs08_c_tiny_ieee_32_32 tiny HCS08 ansitfs.lib -W2 -Os -cs08 -Mt -Fd -F2 -NoPath hcs08_c_tiny_ieee_32_64 tiny HCS08 ansits.lib -W2 -Os -cs08 -Mt -F2 -NoPath hcs08_c_tiny_no_float only tiny HCS08 ansitis.lib -W2 -Os -cs08 -Mt -D__NO_FLOAT__ -F2 -NoPath hcs08_c++_tiny_ieee_32_32 tiny HCS08 cpptfs.lib -W2 -Os -cs08 -Mt -Fd -F2 -NoPath hcs08_c++_tiny_ieee_32_64 tiny HCS08 cppts.lib -W2 -Os -cs08 -Mt -F2 -NoPath hcs08_c++_tiny_no_float only tiny HCS08 cpptis.lib -W2 -Os -cs08 -Mt -D__NO_FLOAT__ -F2 -NoPath hcs08_c_banked_ieee_32_32 banked HCS08 with MMU ansibfm.lib -W2 -Os -cs08 -MMU -Mb -Fd -F2 -NoPath hcs08_c_banked_ieee_32_64 banked HCS08 with MMU ansibm.lib -W2 -Os -cs08 -MMU -Mb -F2 -NoPath hcs08_c_banked_no_float only banked HCS08 with MMU ansibim.lib -W2 -Os -cs08 -MMU -Mb -D__NO_FLOAT__ -F2 -NoPath hcs08_c++_banked_ieee_32_32 banked HCS08 with MMU cppbfm.lib -W2 -Os -cs08 -MMU -Mb -Fd -F2 -NoPath hcs08_c++_banked_ieee_32_64 banked HCS08 with MMU cppbm.lib -W2 -Os -cs08 -MMU -Mb -F2 -NoPath hcs08_c++_banked_no_float only banked HCS08 with MMU cppbim.lib -W2 -Os -cs08 -MMU -Mb -D__NO_FLOAT__ -F2 -NoPath Options used: -W2: Suppress warning and information messages -Os: Optimize for code size -cs08: Generate code for HCS08 -MMU: Enable MMU support -Ms: Small memory model -Mt: tiny memory model -Mb: Banked memory model -Fd: Float and double is IEEE32. The -T option -TD2LD2LLD2 is equivalent to -Fd. -F2: ELF/DWARF 2.0 -NoPath: Do not write path information into object files (for more details, see the provided online documentation)
So for my above case I could use ‘ansi.lib’ (float are 32bit, double are 64bit) or ‘ansif.lib’ (where both float and double are 32bit).
💡 If I’m not 100% sure, then I simply create a new ‘dummy’ project with the wizard and inspect which libraries it is using.
Floating Point Format Compiler Options
The last thing to consider is the format of ‘double’: the compiler/library supports a model where doubles have only 32bits. So if I decide to use such a format, I need to make sure that the correct library is linked (see previous section). Additionally I need to check that the compiler option for the floating point format is set correctly. I find this option in the compiler settings under ‘Code Generation’:
I rarely need floating point numbers, as most problems I have simply do not need it. And adding unnecessarily floating point support (and using it) to a project is not a good thing anyway. But if I change my mind, and I really need it, can change my projects easily with the knowledge from above.
💡 The above points apply for S12 projects as well 🙂
Happy Floating 🙂
Pingback: MC9S08QD4 – assigning to doubles/longs “corrupts” unrelated variables – TheInstaPreneur