通过宏替换循环展开无效

时间:2015-02-26 03:02:39

标签: c++ c gcc

我正在尝试使用以下代码在gcc中执行循环展开。

#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);
*/

} 但每次我增加指令宏时,输出框似乎总是<8KB。我期待我的大小大约40MB左右。我不明白发生了什么。

1 个答案:

答案 0 :(得分:3)

您的指示不会产生任何可观察到的行为。 C是根据抽象机器定义的。只要程序的可观察行为符合规范所说的内容,编译器就可以生成任何代码。

在C中,可观察的行为是:调用库函数,写入文件的数据以及访问volatile变量。

作为一种解决方法,您可以将一条指令更改为{ volatile int z = x + y; }