Xilinx MicroBlaze Reference Manual page 86

32-bit soft processor
Hide thumbs Also See for MicroBlaze:
Table of Contents

Advertisement

The following "bad" example calculates the sum of squares of the integers from 1 to 10
using floating-point representation:
float sum, t;
int i;
sum = 0.0f;
for (i = 1; i <= 10; i++) {
t = (float)i;
sum += t * t;
}
The above code requires a cast from an integer to a float on each loop iteration. This can be
rewritten as:
float sum, t;
int i;
t = sum = 0.0f;
for(i = 1; i <= 10; i++) {
t += 1.0f;
sum += t * t;
}
Note that the compiler is not at liberty to perform this optimization in general, as the two
code fragments above may give different results in some cases (for example, very large t).
Square root runtime library function
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 that 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.
MicroBlaze Processor Reference Guide
UG984 (v2016.2) June 8, 2016
UG984 (v2016.1) April 6, 2016
www.xilinx.com
Chapter 2: MicroBlaze Architecture
Send Feedback
86

Advertisement

Table of Contents
loading

Table of Contents