X hits on this document

40 views

0 shares

0 downloads

0 comments

10 / 17

APU Floating-Point Unit v3.1

x += 1.0;

/* warning - uses double addition! */

Note that the GNU C compiler can be instructed to treat all floating-point constants as single-precision (contrary to the ANSI C standard) by supplying the compiler flag -fsingle-precision-constants.

Avoid unnecessary casting

While conversions between floating-point and integer formats are supported in hardware by the FPU, it is still best to avoid them when possible. Such casts require transfers between the floating-point and the integer register files, which in the PowerPC architecture always go via memory. These transfers are a bottleneck and can cause performance degradation.

The following “bad” example calculates the sum of squares of the integers from 1 to 10 using float- ing-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;

{

}

By making the loop code independent of the integer loop counter, all code inside the loop is carried out using the FPU. 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).

Runtime library functions

The standard C runtime math library functions operate using double-precision arithmetic. When using a single-precision FPU, calls to certain functions (such as fabs()and sqrt()) result in inefficient emu- lation routines being used instead of FPU instructions:

float x=-1.0F;

x = fabs(x); x = sqrt(x);

/* uses double precision */ /* uses double precision */

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

The solution is to use the non-ANSI functions fabsf() and sqrtf(x) instead, which operate using sin- gle precision and can be carried out using the FPU. For example:

float x=-1.0F; x = fabsf(x); /* uses single precision */

10

www.xilinx.com

March 11, 2008 Product Specification

Document info
Document views40
Page views40
Page last viewedWed Dec 07 15:29:03 UTC 2016
Pages17
Paragraphs560
Words6171

Comments