UP | HOME

[8jun2024] gcc: getting builtin defines

Table of Contents

1. Problem

Want to know the set of builtin #defines provided by compiler, as a function of command line arguments.

My use case was figuring out which symbols for vector instructions got picked up with -march=native on my dev host.

2. Solution

  • stumbled on this stack overflow question https://stackoverflow.com/questions/28939652/how-to-detect-sse-sse2-avx-avx2-avx-512-avx-128-fma-kcvi-availability-at-compile
  • turns out to be an easy one-liner

    gcc -dM -E - < /dev/null | sort
    

    with output like

    #define _FORTIFY_SOURCE 3
    #define _LP64 1
    #define _STDC_PREDEF_H 1
    #define __ATOMIC_ACQUIRE 2
    #define __ATOMIC_ACQ_REL 4
    ...
    

    Here:

    • -E tells compiler to emit preprocessor output
    • -dM tells compiler to produce defines instead of preprocesed source code
    • - as last argument tells compiler to compile input from stdin.
  • to look at say SSE/AVX related instructions:

    (using gcc 13.2 here)

    gcc -dM -E - < /dev/null | egrep "SSE|AVX" | sort
    
    #define __MMX_WITH_SSE__ 1
    #define __SSE2_MATH__ 1
    #define __SSE2__ 1
    #define __SSE_MATH__ 1
    #define __SSE__ 1
    

    but with -mavx512f:

    gcc -mavx512f -dM -E - < /dev/null | egrep "SSE|AVX" | sort
    

    with output:

    #define __AVX2__ 1
    #define __AVX512F__ 1
    #define __AVX__ 1
    #define __MMX_WITH_SSE__ 1
    #define __SSE2_MATH__ 1
    #define __SSE2__ 1
    #define __SSE3__ 1
    #define __SSE4_1__ 1
    #define __SSE4_2__ 1
    #define __SSE_MATH__ 1
    #define __SSE__ 1
    #define __SSSE3__ 1
    

Author: Roland Conybeare

Created: 2024-09-08 Sun 18:01

Validate