How are floating point operations emulated in software?

How does the software do floating point arithmetic when the processor doesn't have (or buggy) floating point block? Examples include the PIC, AVR and 8051 microcontroller architectures.

0


source to share


4 answers


"Emulated" is a misnomer in the context of PIC, AVR, and 8051. Floating point emulation refers to the emulation of FPU hardware on architectures with the FPU option, but for which not all parts include FPU. This allows binary floating point instructions for the non-FPU option. Where used, FPU emulation is implemented as an invalid command exception handler; when an FPU instruction is encountered but no FPU is present, an exception is thrown and the handler reads the instruction value and implements the operation in software.



However, none of the architectures you listed define and implement FPU or FPU, so there is nothing to emulate. Instead, in these cases, floating point operations are fully implemented in software, and the compiler generates code to call the floating point routines as needed. For example, an expression x = y * z ;

will generate code equivalent to a function call x = _fmul( y, z ) ;

. In fact, if you look at the output of the layout linker from the floating point operations associated with the build, you will probably see the usual symbol names like _fmul

, _fdiv

etc. - these functions are an integral part of the compiler.

+2


source


Floating point is just scientific notation in base-2. Both the mantissa and exponent are integers, and the softfloat libraries break down floating-point operations into operations that affect the mantissa and exponent, which can take advantage of integer CPU support.

For example, (x 2 n ) * (y 2 m ) = x * y 2 n + m .



Often a normalization step is also needed to keep the floating point representation canonical, but it may be possible to perform multiple operations during normalization. Also, since IEEE-754 stores exponent biased, this must be considered.

0


source


The floating point is not "emulated". They are usually stored as described in IEEE754 .

Fixed point is another type of implementation. The number 2.54 can be implemented in fixed or floating point.

Software implementation of VS FPU (floating point unit)

Some modern MCUs like the ARM cortex M4 F have a floating point unit and can handle floating point operations (like multiply, divide, add) on hardware much faster than software. >

In 8-bit MCUs like AVR, PIC and 8051, operations are performed only in software (separation can take up to hundreds of operations). He will have to separate separately the mantissa (fraction) part and the exponential part plus all special cases (eg NaN). The compiler often has many routines to threaten the same operation (like division) and will choose based on optimization (size / speed) and other parameters (like if it knows the numbers are always positive ...)

-1


source


There is another SO question that covers the C / C ++ standards requirements of floating point numbers. So, strictly speaking, float can be represented in any form the compiler prefers. But in practice, if the floating point implementation is significantly different from IEEE754 , then you can expect a lot of errors caused by programs that are used for IEEE754. And the compiler should be programmer friendly and shouldn't create problems using unspecified places in the standards. Thus, in most cases, floating point numbers will be represented the same way, as they are represented on all other architectures, including x86. Fixed point arithmetic is too different.

In case the AVR and PIC compiler knows there is no fpu, so it will translate every operation to a bunch of instructions supported by the CPU. It would have to normalize both operands to a common exponent and then perform an operation on the mantissa, for example on integers, and then adjust the exponent. This is quite a lot of operations, so emulated floating point is slow. Also, if you are optimizing for size, every floating point operation can become a function call.

And on the ARM arch, things can get pretty weird. There are ARM with and without FPU. And you can have a universal app that works on both. In this case, there is a complex (and slow) scheme. The app uses FPU commands. If your processor does not have an FPU, then such a command will cause an interrupt, and in it the OS will emulate the instruction, clear the error bit and return control to the application. But this scheme was very slow and not usually used.

-1


source







All Articles