1

I have c inline function which may get constant int as argument:

    static __always_inline uint32_t funcA(uint32_t num)
    {
      //do something with that num
    }

I want to run this function with the numbers: 0-100, and I want the compiler to write the function assembler code 100 times, each time with different value.

    for(uint32_t i=0;i<100;i++)
    {
      funcA(i);
    }

Is it possible to run the function in a loop, and somehow to cause the compiler to duplicate it's assembler? or I need to write this function call 100 times?

Note: this is not duplicate of http://stackoverflow.com/questions/4071690/tell-gcc-to-specifically-unroll-a-loopbecause the solution suggested there, causes GCC errors regarding to inline assembler as I wrote below.

H.E.D
  • 19
  • 2
  • 1
    This is called "loop unrolling", by the way. – Colonel Thirty Two May 09 '16 at 21:31
  • 1
    how can I cause a loop to be unrolling? – H.E.D May 09 '16 at 21:32
  • 1
    Even writing out 100 calls manually won't force the compiler to duplicate the object code. A compiler may rewrite `f(0); f(1); f(2); ... f(100);` back into a loop. – Tavian Barnes May 09 '16 at 21:33
  • 2
    Why do you want to unroll a loop with so many iterations? Generally, 2-4 iterations will help, but with so many, it doesn't seem productive to do so. – Matt Jordan May 09 '16 at 21:33
  • I need it to test the funcA function which's encoding differently the arguments according to their values.I'm using inline assembly to encode the arguments, so it's must be different assembler for each value. – H.E.D May 09 '16 at 21:37
  • when adding the attribute:__attribute__((optimize("unroll-loops"))), I'm getting an error of 'impossible constraint' on other asm_volotile code which was o.k before. – H.E.D May 09 '16 at 21:47
  • @TavianBarnes that's a fairly theoretical concern though – harold May 09 '16 at 21:48
  • An assembler macro should also work, if you just want to see disassembly output. You'd have to wrap a macro around your function, though. – Peter Cordes May 09 '16 at 21:50
  • I prefer not to change the function, 'cause it's API function which I only need to test it's optimizations for each of the values. – H.E.D May 09 '16 at 21:53
  • What is your actual concern, here? You want to look at the generated assembly to see whether specific cases are well optimized? In that case, how about writing C code that actually corresponds to the assembly that you want to examine? As you describe it, you are interested only in cases where the function is called with a constant argument, so call it, separately, with every constant argument that interests you. – John Bollinger May 09 '16 at 22:02
  • Perhaps `#define foo(x) funcA(x); (volatile int) 0;` `#define f10(x) foo(x+0); foo(x+1);foo(x+2);foo(x+3);foo(x+4);foo(x+5);foo(x+6);foo(x+7);foo(x+8);foo(x+8);` `f10(0); f10(10); f10(20); ...`?? – chux - Reinstate Monica May 09 '16 at 22:20
  • and If I need to check all numbers 0-1000, it's not reasonable to call it 1000 times. I just want to know if there's a way to force the compiler to unroll the loop for 1000 code sections. (I have tried the "unroll-loops" argument but get errors as written above). – H.E.D May 10 '16 at 06:59
  • You should edit the error message into the question; don't just tell people to search the comments. If you get "impossible constraint" from unrolling, it makes me wonder about your inline asm: maybe it has a problem that unrolling exposed, just like you wanted it to. – Peter Cordes May 10 '16 at 19:49
  • For pure testing purposes, use a text-processing tool to emit 1000 different functions that call `funcA` with a different constant argument. e.g. a shell loop: `for i in {0..1000}; do echo "uint32_t test$i(void) { return funcA($i); }";done > test.c` – Peter Cordes May 10 '16 at 19:52

0 Answers0