Is there another mechanism to get the GCC preprocessor to do this:
#define LIMIT 16
#pragma GCC unroll LIMIT
for (size_t ii = 0; ii < LIMIT; ++ii) {
...
That code hits an error:
/path/to/my/file.c:100:20 error: 'LIMIT' undeclared (first use in this function)
100 | #pragma GCC unroll LIMIT
| ^~~~~
gcc documentation says that this wants an integer constant expression specifying the unrolling factor. I believe that my macro is an "integer constant expression", but...
My compiler is: riscv64-unknown-elf-gcc (g2ee5e430018) 12.2.0.
const and constexpr (in case you can use C++) seems to work (tested with gcc 13.2 on https://godbolt.org/).
const int x = 5;
#pragma GCC unroll x
for (std::size_t ii = 0; ii < x; ++ii) {
Instead of the #pragma directive, the _Pragma operator can be used. It is a bit tricky to get the argument expanded correctly, but this should work for both C and C++ (see example on godbolt.org):
#define STRING(V) #V
#define MAKE_PRAGMA(S) _Pragma(S)
#define UNROLL(N) MAKE_PRAGMA(STRING(GCC unroll N))
#define LIMIT 16
...
UNROLL(LIMIT)
for (size_t ii = 0; ii < LIMIT; ++ii) {
...
}
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