Implement the LibM Math Library

Submit New Article

March 3, 2009 11:00 PM PST



Challenge

Implement the LibM Math Library. The LibM library provides highly optimized scalar math functions that serve as direct replacements for the standard C calls. The LibM versions are fully accurate and do not attempt to extract algorithm-level parallelism, so they can be applied in even the most rigid coding situations.

LibM is packaged with the Intel® compilers and thus requires the purchase of an Intel Compiler license if you choose to build it into a retail product (even if you do not use the compiler itself). Many routines in the LibM library are more highly optimized for Intel microprocessors than for non-Intel microprocessors. See the optimization notice for more detail.


 

Solution

Disable the necessary intrinsic calls and then add "libm.lib at the head of the linker command line.

LibM is a snap to implement, because it usually does not require any changes to your code. (Many of the math functions are generated inline as an optimization). If you are using the Microsoft* Visual C++ compiler, turning off the intrinsic calls can be done in one of two ways:

  • Apply the /Oi- compiler switch (note the minus sign). This turns off the "Generate Intrinsic Functions" optimization, forcing the compiler to insert explicit calls to the C-runtime library or a substitute math library.
  • Insert "#pragma function(function name)" in the files containing your math calls, where function name is the operation you are using (e.g., sin, cos, exp, log, etc.). This is a more controlled way of forcing explicit calls only for the functions you specify.

 

A note to Intel Compiler users: #pragma function is accepted by the compiler, but it has no effect, so you must use the /Oi- switch. The #pragma optimize sequence also cannot be used to toggle specific optimizations, so localization of /Oi- can only be achieved at a function-level granularity by separating functions into different files.

Once you have the necessary intrinsic calls disabled, just add "libm.lib" at the head of your linker command line, and you should be in business using LibM. If you are using the Microsoft* Visual C++ compiler and have not fully installed the Intel® compiler, you'll also need to list "libirc.lib (the Intel® C-runtime library), and be sure that Microsoft Visual Studio* can find the Intel® libraries in your LIB path setting (under the Tools > Options > Directories pulldown).

Each LibM call begins with a run-time processor check. On Intel processors, it branches to the most appropriate optimized code path for the detected Intel processor, so you automatically maintain backward compatibility with earlier microarchitectures. On compatible, non-Intel processors, it executes a generic optimized code path that will work on all microarchitectures for both Intel and non-Intel processors. The performance of the generic optimized code path will likely be lower than for code paths optimized for specific Intel processors.


Source

Integrating Fast Math Libraries for the Intel Pentium® 4 Processor

 


 



Do you need more help?


This article applies to: Parallel Programming,   Intel® C++ Compiler for Windows* Knowledge Base