2
votes

I am implementing Kahan summation, in a project that supports compilation with gcc47, gcc48, clang33, icc13, and icc14.

As part of this algorithm, I would like to disable optimizations that take advantage of the associativity of addition of real numbers. (Floating point operations are not associative.)

I would like to disable those optimizations only in the relevant function. I have figured out how to do this under gcc, using the ''no-associative-math'' attribute. How can I do this in icc or clang? I have searched without luck.

class KahanSummation
{
  // GCC declaration
  void operator()(float observation) __attribute__((__optimize__("no-associative-math")))
  {
    // Kahan summation implementation
  }
};

Other GCC attributes that would imply no-associative-math are no-unsafe-math-optimizations or no-fast-math.

Looking at an Intel presentation (PDF, slide 8) or another or another (PDF, slide 11), I want to set "fp-model precise" in ICC, for only this function. The compilers I care about are ICC 13 and ICC 14.

class KahanSummation
{
  // ICC or clang declaration
  void operator()(float observation) __attribute__((????))
  {
    // Kahan summation implementation
  }
};
1
__attribute__ is a GCC extension. Implement those functions in a separate translation unit and compile that using the appropriate switch?T.C.

1 Answers

1
votes

__attribute__ is a GCC extension. Clang also supports that syntax to maintain some GCC compatibility, but the only optimization-related attribute it seems to support is optnone, which turns off all optimizations. ICC has a few optimization pragmas, but nothing that does what you want to do, from what I can tell. appears to support #pragma float_control for VC++ compatibility, although I can't find anything on exactly how that pragma is supposed to be used in ICC's documentation, so you'll have to use VC++'s.

What you can do, though, is to define the function you want in a separate translation unit (i.e., cpp file):

// Your header
class KahanSummation
{
  // Declaration
  void operator()(float observation);
};

// Separate cpp file - implements only this function
void KahanSummation::operator()(float observation)
{
    // Kahan summation implementation
}

You can then compile the separate file using whatever compiler option you need to use, and link the resulting object file to the rest of your program.