0

My compiler supports avx2, and I added -mavx2 to C++ flags, but the __AVX2__ macro is not defined in my code.

#ifdef __AVX2__
#include <immintrin.h>
#endif

appears to be disabled in the code.

Edit:

My complier version is:

g++ (Ubuntu 8.3.0-6ubuntu1) 8.3.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
chtz
  • 17,329
  • 4
  • 26
  • 56
Jacko
  • 12,665
  • 18
  • 75
  • 126
  • 2
    `-mavx2` should work with gcc-4.7 or later: https://godbolt.org/z/aEZQez Are you using that or `-march=native`? In the latter case, are you sure your CPU supports AVX2? – chtz Sep 06 '19 at 09:43
  • Thanks, I am using `-mavx2`. And yes, doing `cat /proc/cpuinfo` shows avx2 supported. Sadly, I have to manually check cpuinfo and then define these macros. – Jacko Sep 06 '19 at 17:44
  • 1
    Can you show the output of `gcc --version` (or `g++ --version`)? – chtz Sep 06 '19 at 21:15
  • Thanks - updated above. – Jacko Sep 07 '19 at 13:28
  • 2
    What's the complete compiler command line? And how have you determined that `__AVX2__` is not defined? – Florian Weimer Sep 08 '19 at 08:22
  • 1
    @FlorianWeimer I am using cmake to generate the make files and compiler command. I know `__AVX2__` is not defined because my editor Eclipse CDT is greying out the code. – Jacko Sep 08 '19 at 15:29
  • @FlorianWeimer :) thanks - so, this is just an editor issue! it turns out the __AVX2__ is indeed defined. – Jacko Sep 08 '19 at 15:33
  • You can put this comment as an answer to the question - purely an editor issue. – Jacko Sep 08 '19 at 15:34

1 Answers1

1

To make eclipse aware that AVX2 is available, you can go to the project properties under

C/C++ General
    Preprocessor Include Paths, Macros etc.
        Providers
            CDT GCC Built-in Compiler Settings

and add -mavx2 or -march=native to the "Command to get compiler specs".

Alternatively, you can set this in the global preferences under

C/C++
    Build
        Settings
            Discovery
                CDT GCC Built-in Compiler Settings

N.B. while you are at that setting, you can also add things like -std=c++17 (or whatever standard you usually use).

chtz
  • 17,329
  • 4
  • 26
  • 56
  • Thanks! I added ${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}" "-mavx2" in Discovery but it didn't change anything unfortunately – Jacko Sep 09 '19 at 12:14
  • Also, I didn't see an entry for `CDT GCC Built-in Compiler Settings` under project settings – Jacko Sep 09 '19 at 12:30
  • 1
    Leave out the `"` around `-mavx2`. And perhaps try it in front of `"${INPUTS}"`. You may need to re-index your project (try this with a small test-project first). – chtz Sep 09 '19 at 12:37
  • Thanks, removed the quotes, moved it in front, and re-indexed - still greyed out :( – Jacko Sep 09 '19 at 12:51