Gaara
Gaara

Reputation: 697

Loop unrolling via macro substitution ineffective

I am trying to perform loop unrolling in gcc with following code.

#define SINGLE_INSTRUCTION(x,y) x + y
#define FIVE_INSTRUCTIONS(x,y) SINGLE_INSTRUCTION(x,y); SINGLE_INSTRUCTION(x,y); SINGLE_INSTRUCTION(x,y); SINGLE_INSTRUCTION(x,y); SINGLE_INSTRUCTION(x,y);
#define TWENTYFIVE_INSTRUCTIONS(x,y)  FIVE_INSTRUCTIONS(x,y); FIVE_INSTRUCTIONS(x,y); FIVE_INSTRUCTIONS(x,y); FIVE_INSTRUCTIONS(x,y); FIVE_INSTRUCTIONS(x,y);
#define ONEHUNDRED_INSTRUCTIONS(x,y);  TWENTYFIVE_INSTRUCTIONS(x,y); TWENTYFIVE_INSTRUCTIONS(x,y); TWENTYFIVE_INSTRUCTIONS(x,y); TWENTYFIVE_INSTRUCTIONS(x,y);

#define FIVEHUNDERED_INSTRUCTIONS(x,y); ONEHUNDRED_INSTRUCTIONS(x,y); ONEHUNDRED_INSTRUCTIONS(x,y); ONEHUNDRED_INSTRUCTIONS(x,y); ONEHUNDRED_INSTRUCTIONS(x,y); ONEHUNDRED_INSTRUCTIONS(x,y);

#define TWOTHOUSAND_INSTRUCTIONS(x,y); FIVEHUNDERED_INSTRUCTIONS(x,y); FIVEHUNDERED_INSTRUCTIONS(x,y); FIVEHUNDERED_INSTRUCTIONS(x,y); FIVEHUNDERED_INSTRUCTIONS(x,y);

#define TENTHOUSAND_INSTRUCTIONS(x,y) TWOTHOUSAND_INSTRUCTIONS(x,y); TWOTHOUSAND_INSTRUCTIONS(x,y); TWOTHOUSAND_INSTRUCTIONS(x, y); TWOTHOUSAND_INSTRUCTIONS(x,y); TWOTHOUSAND_INSTRUCTIONS(x,y);

#define FIFTYTHOUSAND_INSTRUCTIONS(x,y) TENTHOUSAND_INSTRUCTIONS(x,y); TENTHOUSAND_INSTRUCTIONS(x,y); TENTHOUSAND_INSTRUCTIONS(x, y); TENTHOUSAND_INSTRUCTIONS(x,y); TENTHOUSAND_INSTRUCTIONS(x,y);

#define TWOHUNDREDTHOUSAND_INSTRUCTIONS(x,y) FIFTYTHOUSAND_INSTRUCTIONS(x,y); FIFTYTHOUSAND_INSTRUCTIONS(x,y); FIFTYTHOUSAND_INSTRUCTIONS(x, y); FIFTYTHOUSAND_INSTRUCTIONS(x,y);

#define ONEMILLION_INSTRUCTIONS(x,y) TWOHUNDREDTHOUSAND_INSTRUCTIONS(x,y); TWOHUNDREDTHOUSAND_INSTRUCTIONS(x,y); TWOHUNDREDTHOUSAND_INSTRUCTIONS(x, y); TWOHUNDREDTHOUSAND_INSTRUCTIONS(x,y); TWOHUNDREDTHOUSAND_INSTRUCTIONS(x,y);

void main() {

ONEMILLION_INSTRUCTIONS(50,100);
ONEMILLION_INSTRUCTIONS(200,300);
/*
    ONEMILLION_INSTRUCTIONS(200,300);
ONEMILLION_INSTRUCTIONS(50,100);
ONEMILLION_INSTRUCTIONS(50,100);
ONEMILLION_INSTRUCTIONS(200,300);
ONEMILLION_INSTRUCTIONS(50,100);
ONEMILLION_INSTRUCTIONS(200,300);
ONEMILLION_INSTRUCTIONS(50,100);
*/

} but everytime I increase the macro of instructions, the output bin seems to be always <8KB. I am expecting the size to me somewhere around 40MB. I don't understand what is happening.

Upvotes: 1

Views: 83

Answers (1)

M.M
M.M

Reputation: 141554

Your instructions don't produce any observable behaviour. C is defined in terms of an abstract machine. The compiler can generate any code whatsoever so long as the program's observable behaviour matches what the spec says.

In C the observable behaviour is: calls to library functions, data written to files, and accesses of volatile variables.

As a workaround you could change your one instruction to be { volatile int z = x + y; }.

Upvotes: 2

Related Questions