2

I'd like to define a preprocessor statement like this:

#define INSERT_NOPS(num) .......

and somewhere in my code

INSERT_NOPS(10);

Now I want my preprocessor to roll out the following code:

asm volatile("nop");
asm volatile("nop");
asm volatile("nop");
asm volatile("nop");
asm volatile("nop");

How can I realise this? (0<=num<100000)

I don't want to any if/else statements inside my Macro.

Thanks in advance!

2 Answers 2

3

This is far from trivial to roll by hand. Have a look at the Boost Preprocessor Library, which works for C as well as C++. BOOST_PP_REPEAT does what you want:

#include <boost/preprocessor/repetition/repeat.hpp>

#define INSERT_NOP(z, n, data) asm volatile("nop");
#define INSERT_NOPS(num) BOOST_PP_REPEAT(num, INSERT_NOP, )

INSERT_NOPS(10)

Proof:

$ g++ -E test.cc
[...]
asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop");
4
  • Well, actually i don't want to use Boost or any third party libraries / definitions... I'll have a look inside BOOST_PP_REPEAT. Thanks!
    – rralf
    Nov 6, 2013 at 20:13
  • That file alone is 825 lines. You might find that it borders on insanity.
    – Thomas
    Nov 6, 2013 at 20:16
  • I know. But Code size / Compile Time doesn't matter in my case. I don't want to have side effects triggered by some loops. It's a very ciritical system in time.
    – rralf
    Nov 6, 2013 at 20:23
  • "Side effects triggered by some loops"? This is a preprocessor library. There are no side effects. My demo above shows that it expands exactly to the code you want, and nothing else besides.
    – Thomas
    Nov 6, 2013 at 20:26
1

I think I found a better solution without using Boost:

#pragma GCC push_options
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("O2")
void nops() {
 int i = 0;
 for(; i < 10 ; i++) {
   asm volatile("nop");
 }
}
#pragma GCC pop_options

int main(void) {
  nops();
}

To compile with:

gcc -c -o test.o -O3 test.c 

Having a look at the disassembly:

objdump -d test.o

test.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <nops>:
   0:   90                      nop
   1:   90                      nop
   2:   90                      nop
   3:   90                      nop
   4:   90                      nop
   5:   90                      nop
   6:   90                      nop
   7:   90                      nop
   8:   90                      nop
   9:   90                      nop
   a:   c3                      retq   

Disassembly of section .text.startup:

0000000000000000 <main>:
   0:   31 c0                   xor    %eax,%eax
   2:   e8 00 00 00 00          callq  7 <main+0x7>
   7:   f3 c3                   repz retq 
2
  • But how do you invoke this for 10 nops one call and 20 nops another call?
    – Michael
    Dec 19, 2013 at 4:48
  • You would need two different functions. Those functions could automatically be expanded via macros. Calling those functions parameterized makes no sense as I want the nop's to be statically compiled as atomar instructions.
    – rralf
    Dec 19, 2013 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.