Could I force compiler to compile some intrinsic functions outside -march
setting in some particular part of code?
Of course, the rest would remain within -march
setting.
Is it possible to enable -mavx2
on only specific part of source code?
Or is the only way that I must compile -mavx2
section separately?
Try __attribute__((target("avx2")))
. Both GCC and Clang support it.
Example:
#include <stdlib.h>
#include <stdio.h>
#include <immintrin.h>
__attribute__((target("avx2")))
int add_with_avx2(int a, int b) {
__m256i av = _mm256_set_epi32(a, 0, 0, 0, 0, 0, 0, 0);
__m256i bv = _mm256_set_epi32(b, 0, 0, 0, 0, 0, 0, 0);
__m256i result = _mm256_add_epi32(av, bv);
return ((int*)&result)[7];
}
int main(void) {
return add_with_avx2(5, 6);
}
However, it's probably a better idea to put the functions that need intrinsics in a seperate file, in case you ever need to use a compiler that doesn't have this feature.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With