2

I am running a C code with SIMD instructions to test my environment. I am running this in Codeblocks and Windows 10

#include <immintrin.h>
#include <stdio.h>

#define ARRAY_LENGTH 8

int main(int argc, char* argv[]) {

    __m256i first = _mm256_set_epi32(10, 20, 30, 40, 50, 60, 70, 80);
    __m256i second = _mm256_set_epi32(5, 5, 5, 5, 5, 5, 5, 5);
    __m256i result = _mm256_add_epi32(first, second);

    int* values = (int*) &result;

    for (
        unsigned short i = 0;
        i < ARRAY_LENGTH;
        i += 1
    ) {
        printf("%d ", values[i]);
    }

    return 0;
}

This code is throwing an error and I am not able to fix it.

error: inlining failed in call to always_inline '_mm256_add_epi32': target specific option mismatch

Is it something caused due to Codeblocks environment?

Sabarna Hazra
  • 23
  • 1
  • 3

1 Answers1

4

You have to enable the corresponding instruction set natively by adding the correct option to the gcc command line, -mavx2 in this case, i.e.:

gcc -O2 -mavx2 prog.c -o prog

Of course, you have to make sure that the CPU you run the program on indeed supports this instruction set extension, otherwise you will run into a segmentation fault or illegal instruction exception.

Ctx
  • 18,090
  • 24
  • 36
  • 51
  • I would recommend `-march=haswell` or `-march=znver1` to also tune for an AVX2 CPU, and enable FMA and other good stuff like BMI2, instead of *just* using `-mavx2` but leaving the default of `-mtune=generic` and so on. – Peter Cordes Sep 27 '19 at 00:06