5

I tried to slightly modify the example from the article:

#include <iostream>
#include <cfenv>

#pragma STDC FENV_ACCESS ON

int main()
{
    std::feclearexcept(FE_ALL_EXCEPT);
    //int r = std::feraiseexcept(FE_UNDERFLOW | FE_DIVBYZERO);
    double x = 1.0;
    double y = 0.0;
    double result{};
    asm volatile ("fldl %1\n"
                  "fdivl %2\n" : "=%t"(result) : "m"(x), "m"(y) : "memory");
    std::cout << result << std::endl;
    int e = std::fetestexcept(FE_ALL_EXCEPT);
    if (e & FE_DIVBYZERO) {
        std::cout << "division by zero\n";
    }
    if (e & FE_INEXACT) {
        std::cout << "inexact\n";
    }
    if (e & FE_INVALID) {
        std::cout << "invalid\n";
    }
    if (e & FE_UNDERFLOW) {
        std::cout << "underflow\n";
    }
    if (e & FE_OVERFLOW) {
        std::cout << "overflow\n";
    }
    return EXIT_SUCCESS;
}

but I get a warning (for clang++, but the same for G++ too):

warning: pragma STDC FENV_ACCESS ON is not supported, ignoring pragma [-Wunknown-pragmas]
#pragma STDC FENV_ACCESS ON
             ^
1 warning generated.

Another article reported, that the pragma is treat the class of so-called C standard pragmas, but the former mentioned article does containing the code, which uses the pragma.

Is it allowed to use the pragma in C++ code? There is <cfenv> header in C++. It alludes to the fact that this Floating-point environment is avaliable to using in C++. But this article reports about implementation dependent nature of the Floating-point environment. Is this touches upon C++?

Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
  • 5
    It's not supported in C compilers either (as of 2015). GCC has a file-scope option. See http://stackoverflow.com/questions/24541589/adding-two-floating-point-numbers – Pascal Cuoq Nov 02 '15 at 05:34
  • 3
    In practice, read your compiler documentation about FP environment access. Standardization is rather limited. Not that it really matters here anyway: the moment you wrote `asm` the standard stopped to apply. – MSalters Nov 02 '15 at 09:19
  • 1
    @Pascal Cuoq of the compilers I used, IBM, Oracle, and HP all support the FENV_ACCESS pragma, although I haven't tested it in-depth (it wasn't relevant since my code had to be portable to gcc, unfortunately) – Cubbi Nov 25 '15 at 16:49

0 Answers0