82
MicroBlaze Processor Reference Guide
UG081 (v14.7)
Chapter 2:
MicroBlaze Architecture
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
Страница 1: ...MicroBlaze Processor Reference Guide Embedded Development Kit EDK 14 7 UG081 v14 7...
Страница 4: ...MicroBlaze Processor Reference Guide www xilinx com UG081 v14 7...
Страница 8: ...8 www xilinx com MicroBlaze Processor Reference Guide UG081 v14 7 Chapter 1 Introduction Send Feedback...
Страница 262: ...262 www xilinx com MicroBlaze Processor Reference Guide UG081 v14 7 Send Feedback...