Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Fog A.How to optimize for the Pentium family of microprocessors.2004.pdf
Скачиваний:
12
Добавлен:
23.08.2013
Размер:
814.91 Кб
Скачать

of a critical dependence chain, especially on the P4 processor. There are several alternative ways to avoid this:

1.keep the most critical dependence chain entirely inside one function

2.use inline functions. An inline function will be expanded like a macro without parameter transfer, if possible.

3.use #define macros with parameters instead of functions.

But beware that macro parameters are evaluated every time they are used.

Example:

#define max(a,b) (a > b ? a : b) y = max(sin(x),cos(x));

In this example, sin(x) and cos(x) are both calculated twice because the macro is referencing them twice. This is certainly not optimal.

4.declare functions __fastcall. The first two or three (depending on compiler) integer parameters will be transferred in registers rather than on the stack when the function is declared __fastcall. Floating-point parameters are always stored on the stack. The implicit 'this' pointer in member functions (methods) is also treated like a parameter, so there may be only one free register left for transferring your parameters. Therefore, make sure that the most critical integer parameter comes first when you are using __fastcall.

5.declare functions static. Static functions have no external linkage. This enables the compiler to optimize across function calls.

Your compiler may ignore the optimization hints given by the inline and static modifiers, while __fastcall is certain to have an effect on the first one or two integer parameters. Using #define macros is likely to have an effect on floating-point parameters as well.

If a large object is transferred to a function as a parameter, then the entire object is copied. The copy constructor is called if there is one. If copying the object is not necessary for the logic of your algorithm, then you can save time by transferring a pointer or reference to the object rather than a copy of the object, or by making the function a member of the object's class. Whether you choose to use a pointer, a reference, or a member function, is a matter of programming style. All three methods will produce the same compiled code, which will be more efficient than copying the object. In general, pointers, references, and member functions are quite efficient. Feel free to use them whenever it is useful for the logic structure of your program. Function pointers and virtual functions are somewhat less efficient.

3.9 Conversion from floating-point numbers to integers

According to the standards for the C++ language, all conversions from floating-point numbers to integers use truncation towards zero, rather than rounding. This is unfortunate because truncation takes much longer time than rounding on most microprocessors. It is beyond my comprehension why there is no round function in standard C++ libraries. If you cannot avoid conversions from float or double to int in the critical part of your code, then you may make your own round function using assembly language:

inline int round (double x) { int n;

__asm fld x; __asm fistp n; return n;}