Error while compiling _pext_u64 (Intel C++ compiler)

Discussion of chess software programming and technical issues.

Moderator: Ras

chessbit
Posts: 9
Joined: Fri Dec 29, 2023 4:47 pm
Location: Belgium
Full name: thomas albert

Error while compiling _pext_u64 (Intel C++ compiler)

Post by chessbit »

Hello everyone,

I am facing an issue with the Intel C++ compiler. When I use the "immintrin.h" library, I should be able to use _pext_u64 (it works without issue with the Visual Studio compiler), but I'm getting the following error:

Code: Select all

use of undeclared identifier '_pext_u64'
I played a bit with the include files to get rid of this error but now I'm getting:

Code: Select all

"always_inline function '_pext_u64' requires target feature 'bmi2', but would be inlined into function 'getBishopAttacks' that is compiled without support for 'bmi2'"
There is this relevant part in the "immintrin.h" file, which suggests I don't meet the requirements to use "bmi2intrin.h"

Code: Select all

#if !(defined(_MSC_VER) || defined(__SCE__)) || __has_feature(modules) ||      \
    defined(__BMI2__) || defined(__M_INTRINSIC_PROMOTE__)
#include <bmi2intrin.h>
#endif
I am quite inexperienced in C++ and compilers in general so I don't know if there is a compile option I should add or something?
I would like to keep using the Intel compiler because it yields better speed results.

I'm hoping some of you have an idea how to solve this! Thanks.
User avatar
mvanthoor
Posts: 1784
Joined: Wed Jul 03, 2019 4:42 pm
Location: Netherlands
Full name: Marcel Vanthoor

Re: Error while compiling _pext_u64 (Intel C++ compiler)

Post by mvanthoor »

PEXT is only available on CPU's that have the BMI2 functionality, which are Intel Haswell CPU's and newer. (And you want to avoid it for AMD, for any CPU older than a Zen 4, because on Zen 3 and down, BMI2 is emulated and very slow.) The error says that you need to enable BMI2 as a target feature. In Rust, you can pass "target-feature=+bmi2" to the compiler to do so.. In C++, I don't know. It depends on the compiler you're using. (Clang uses LLVM's backend, just like Rust, so it also has the "target-feature" setting.)

(I must admit that my C++ knowledge is so outdated (C++98, C++03) that I don't know on top of my head what the flags for the current-day compilers would be. Maybe I should some day port my engine to C++20 just to update my C++ knowledge.)
Author of Rustic, an engine written in Rust.
Releases | Code | Docs | Progress | CCRL
Modern Times
Posts: 3703
Joined: Thu Jun 07, 2012 11:02 pm

Re: Error while compiling _pext_u64 (Intel C++ compiler)

Post by Modern Times »

It is Zen 2 and down that you should avoid PEXT builds, Zen 3 and up are fine, that is my understanding.
chessbit
Posts: 9
Joined: Fri Dec 29, 2023 4:47 pm
Location: Belgium
Full name: thomas albert

Re: Error while compiling _pext_u64 (Intel C++ compiler)

Post by chessbit »

Thanks for the replies.
I have this CPU: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz though I don't know how to see if it has bmi2.
So are you saying that with Visual Studio, the compiler is using an emulated version of pext? I would expect it to be slower but it still gives me a little performance increase over the magic bitboard approach.
User avatar
Bo Persson
Posts: 256
Joined: Sat Mar 11, 2006 8:31 am
Location: Malmö, Sweden
Full name: Bo Persson

Re: Error while compiling _pext_u64 (Intel C++ compiler)

Post by Bo Persson »

chessbit wrote: Tue Jan 02, 2024 2:16 pm So are you saying that with Visual Studio, the compiler is using an emulated version of pext?
No, it does not. If you use the intrinsic, the compiler issues that instruction. You asked it to, right?

If it works, then apparently your processor has support for it.
chessbit
Posts: 9
Joined: Fri Dec 29, 2023 4:47 pm
Location: Belgium
Full name: thomas albert

Re: Error while compiling _pext_u64 (Intel C++ compiler)

Post by chessbit »

Bo Persson wrote: Tue Jan 02, 2024 3:24 pm
chessbit wrote: Tue Jan 02, 2024 2:16 pm So are you saying that with Visual Studio, the compiler is using an emulated version of pext?
No, it does not. If you use the intrinsic, the compiler issues that instruction. You asked it to, right?

If it works, then apparently your processor has support for it.
I assumed so because of the performance boost. But then I don't understand why the Intel compiler (as well as Clang for that matter) is complaining.
User avatar
RubiChess
Posts: 640
Joined: Fri Mar 30, 2018 7:20 am
Full name: Andreas Matthies

Re: Error while compiling _pext_u64 (Intel C++ compiler)

Post by RubiChess »

You should add -mbmi2 to the compiler's flags.

And to check which features your cpu supports, you can run my engine:

Here's the output on my Ryzen 3700x which has bad/slow/emulated bmi2 support so the engine doesn't show it:
...
System: AMD Ryzen 7 3700X 8-Core Processor Family: 23 Model: 113
CPU-Features of system: sse2 ssse3 popcnt lzcnt bmi1 avx2

Regards, Andreas
chessbit
Posts: 9
Joined: Fri Dec 29, 2023 4:47 pm
Location: Belgium
Full name: thomas albert

Re: Error while compiling _pext_u64 (Intel C++ compiler)

Post by chessbit »

RubiChess wrote: Tue Jan 02, 2024 5:55 pm You should add -mbmi2 to the compiler's flags.
That works! Thanks to you I can try the Pext implementation. Many thanks for your help Andreas! :D