0

There are predefined macros such as __OPTIMIZE__ (defined in all optimizing compilations) and __OPTIMIZE_SIZE__ (defined if the compiler is optimizing for size).

I use these macros to check if the correct optimization level is set for the release target, if not I print out a warning.

Is there a possibility to check whether the optimization level -Ofast is set or not? Possibly something like __OPTIMIZE_FAST__ or __OPTIMIZE_SPEED__.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Pacinwa
  • 45
  • 5
  • So... what research did you do? Did you read your compiler documentation? – KamilCuk Aug 02 '20 at 20:12
  • Yes, I read the documentation. According to my research and knowledge, there is no macro available. My hope is, that somebody else would know more... – Pacinwa Aug 03 '20 at 06:24

1 Answers1

0

I checked the ARMCC command line options and it seems there's no -Ofast like in many other compilers. However if the behavior is the same as in GCC/Clang/ICC... then -Ofast is essentially just -O3 with --fpmode=fast so you can check it with __FP_FAST, probably in conjunction with __OPTIMISE_LEVEL

In GCC you can use __FAST_MATH__. __NO_MATH_ERRNO__ may also be used although it may not be an exact match because that'll also be defined if -fno-math-errno is specified

#ifdef __OPTIMIZE__
    printf("Optimized\n");

    #ifdef __ARMCC_VERSION
        #if defined(__FP_FAST) && __OPTIMISE_LEVEL >= 3
            printf("-Ofast ARMCC\n");
        #endif
    #elif defined(__GNUC__)
        #if defined(__FAST_MATH__)
//      #if defined(__NO_MATH_ERRNO__)
            printf("-Ofast GNUC\n");
        #endif
    #endif
#endif
phuclv
  • 37,963
  • 15
  • 156
  • 475