Search code examples
floating-pointembeddedfpu

How are floating point operations emulated in software?


How does software perform floating point arithmetic when the CPU has no (or buggy) floating point unit? Examples would be the PIC, AVR, and 8051 microcontrollers architectures.


Solution

  • "Emulated" is the wrong term in the context of PIC, AVR and 8051. Floating-point emulation refers to the emulation of FPU hardware on architectures that have an FPU option but for which not all parts include the FPU. This allows a binary containing floating point instructions to run on a variant without an FPU. Where used, FPU emulation is implemented as an invalid-instruction exception handler; when an FPU instruction is encountered but no FPU is present, an exception occurs, and the handler reads the instruction value and implements the operation in software.

    However none of the architectures you have listed define an FPU or FPU instructions so there is nothing to emulate. Instead in these cases floating-point operations are implemented entirely in software, and the compiler generates code to invoke floating-point routines as necessary. For example the expression x = y * z ; will generate code that is equivalent to a function call x = _fmul( y, z ) ;. In fact if you look at the linker map output from a build containing floating point operations you will probably see routine symbol names such as _fmul, _fdiv and the like - these functions are intrinsic to the compiler.