Using Square Root Runtime Library Function - 2025.1 English - UG984

MicroBlaze Processor Reference Guide (UG984)

Document ID
UG984
Release Date
2025-05-29
Version
2025.1 English

The standard C runtime math library functions operate using double-precision arithmetic. When using a single-precision FPU, calls to the square root functions (sqrt()) result in inefficient emulation routines being used instead of FPU instructions:

#include <math.h>
...
float x=-1.0F;
...
x = sqrt(x); /* uses double precision */

Here the math.h header is included to avoid a warning message from the compiler.

When used with single-precision data types, the result is a cast to double, a runtime library call is made (which does not use the FPU) and then a truncation back to float is performed.

The solution is to use the non-ANSI function sqrtf() instead, which operates using single precision and can be carried out using the FPU. For example:

#include <math.h>
...
float x=-1.0F;
...
x = sqrtf(x); /* uses single precision */
Note: When compiling this code, the compiler flag -fno-math-errno (in addition to -mhard-float and -mxl-float-sqrt) must be used, to ensure that the compiler does not generate unnecessary code to handle error conditions by updating the errno variable.