Search code examples
cfortranintel-fortrancompiler-flags

ifort's real-size flag equivalent for C


Do gcc and cl have an equivalent for ifort real-size compiler flag? If not, what should I use instead? MACRO?

#ifdef DOUBLE_PRECISION
  #define REAL double
#else
  #define REAL float
#endif
REAL myvar;

Solution

  • As far as I know there is no "universal float" whose size can be controlled with a compiler switch. We use an approach similar to yours, albeit with a typedef instead of a define:

    #ifdef DOUBLE_PRECISION
        typedef double real;
    #else
        typedef float real;
    #endif
    

    There's a bit more to it than just the floating-point type, however. For example, consider these function signatures:

    double exp(double x);
    float expf(float x);
    long double expl(long double x);
    

    Each function has a suffix denoting the type of arguments and return values. Or consider the types of floating-point literals:

    1.2f   // float
    1.2    // double
    

    If you want to be on the safe side, you'd have to write macros for these cases, too:

    #ifdef DOUBLE_PRECISION
        #define REAL(x) x
        #define SIN(x) sin(x)
    #else
        #define REAL(x) x ## f
        #define SIN(x) sinf(x)
    #endif
    

    In practice, I've found that with optimisation switched on (-O2 or more), the compiler works out the best function signatures and which type to use for literals, so I just use unadorned functions and literals in order not to clutter the code. This seems to work for gcc and for the Intel and PGI compilers.