对于与预处理器等效的循环 [英] For loop equivalent with the preprocessor

查看:58
本文介绍了对于与预处理器等效的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组和一个展开的循环,如下所示:

do_something(A [0]);

do_something(A [1 ]);

....

do_something(A [255]);


我想:我为什么要输入非常?我应该写一个宏。


所以我想写下以下内容:


#define write_256(array)#for (i,0,255,do_something(foo [i]);


但是我没有办法做到这一点...


有没有办法编写一个宏,C预处理器将扩展

到k指令,并且能够引用迭代次数?


我想过使用递归宏调用:


#define write_more(n,array)\

#if(n> 0) do_something(foo [256-n]); \

write_more(n-1,数组)


但似乎实现者不喜欢递归宏调用:-)


任何见解?

I have an array, and an unrolled loop which looks like this:

do_something(A[0]);
do_something(A[1]);
....
do_something(A[255]);

I thought: why should I type so much? I should write a macro.

So I was looking to write something along the lines of:

#define write_256(array) #for(i,0,255,do_something(foo[i]);

But I coudn''t find a way to do it...

Is there a way to write a macro that the C pre-processor will expand
to k instructions, and be able to reference the iteration number?

I thought of using recursive macro calls along the lines of:

#define write_more(n,array) \
#if (n>0) do_something(foo[256-n]); \
write_more(n-1,array)

but it seems implementers don''t like recursive macro calls :-)

Any insight?

推荐答案

Nudge< de * ****@kma.eu.org>潦草地写道:
Nudge <de*****@kma.eu.org> scribbled the following:
我有一个数组和一个展开的循环,如下所示:
do_something(A [0]);
do_something(A [1]);
...
do_something(A [255]);
我想:我为什么要这么打字?我应该写一个宏。
I have an array, and an unrolled loop which looks like this: do_something(A[0]);
do_something(A[1]);
...
do_something(A[255]); I thought: why should I type so much? I should write a macro.




我想:你为什么要这么烦?将你的循环重新转换为一个

正版for循环,将你的编译器的优化设置为相当昂贵的

high,它可能会为你展开循环生成代码时。

请记住源代码级别的优化规则:

(1)不要这样做。

(2 )(仅限专家!)不要这样做。


-

/ - Joona Palaste(pa ***** @ cc.helsinki.fi)--------------------------- \

| 飞翔的柠檬树中的金鸡王G ++ FR FW + M-#108 D + ADA N +++ |

| http://www.helsinki.fi/~palaste W ++ B OP + |

\ -----------------------------------------芬兰的规则! ------------ /



I think: Why are you bothering with this? Re-roll your loop into a
genuine for loop, set your compiler''s optimisation to "pretty darned
high", and it might unroll the loop for you when generating code.
Remember the rules about optimisation at source code level:
(1) Don''t do it.
(2) (For experts only!) Don''t do it yet.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/


Joona I Palaste写道:
Joona I Palaste wrote:
Nudge< de * ****@kma.eu.org>潦草地写了下面的内容:
Nudge <de*****@kma.eu.org> scribbled the following:
我有一个数组和一个展开的循环,如下所示:
I have an array, and an unrolled loop which looks like this:





do_something(A [0]);
do_something(A [1]);
...
do_something(A [255]);
do_something(A[0]);
do_something(A[1]);
...
do_something(A[255]);


我想:我为什么要这么打字?我应该写一个宏。
I thought: why should I type so much? I should write a macro.



我想:你为什么要这么烦?将你的循环重新转换为一个真正的for循环,将你的编译器的优化设置为相当高的
高,它可能会在生成代码时为你展开循环。
请记住源代码级别的优化规则:
(1)不要这样做。
(2)(仅限专家!)不要这样做。


I think: Why are you bothering with this? Re-roll your loop into a
genuine for loop, set your compiler''s optimisation to "pretty darned
high", and it might unroll the loop for you when generating code.
Remember the rules about optimisation at source code level:
(1) Don''t do it.
(2) (For experts only!) Don''t do it yet.

< br $>

ARGH!


我知道我应该说我比我的编译器更聪明:-)


我没有写任何严肃的代码,只是鬼混。


有没有办法击败预处理器提交?



ARGH!

I knew I should have said that I was smarter than my compiler :-)

I am not writing any serious code, only fooling around.

Is there a way to beat the preprocessor into submission?


在''comp.lang.c''中,Nudge< de ***** @ kma.eu.org>写道:
In ''comp.lang.c'', Nudge <de*****@kma.eu.org> wrote:
我有一个数组和一个展开的循环,如下所示:

do_something(A [0]);
do_something( A [1]);
...
do_something(A [255]);

我想:我为什么要输入这么多?我应该写一个宏。

所以我想写一些类似的东西:

#define write_256(array)#for(i,0,255,do_something(foo) [i]);

但是我没有办法做到这一点...
I have an array, and an unrolled loop which looks like this:

do_something(A[0]);
do_something(A[1]);
...
do_something(A[255]);

I thought: why should I type so much? I should write a macro.

So I was looking to write something along the lines of:

#define write_256(array) #for(i,0,255,do_something(foo[i]);

But I coudn''t find a way to do it...




没有可移植的方法吗直接这样做,但无论如何都有一个技巧(我从bc,BTW获得它b $ b):


定义常量值列表:


/ * values.itm * /

项目(0)

项目(1)

项目( 2)

项目(3)

....

项目(254)

项目(255)


这个文件可以通过一个简单的C程序自动创建。


现在,在您的应用程序中,您执行以下操作:


/ * myapp.c * /

< ...>


#define ITEM(a)\\ \\ b
do_something(A [a]);


#include" values.itm"

#undef ITEM


那是所有人。


我用m这个技巧来定义重复代码。这是非常强大的,并且

在掌握时制作可靠的代码。


-

-ed- em ********** @ noos.fr [在回答我之前删除YOURBRA]

C语言常见问题解答: http:/ /www.eskimo.com/~scs/C-faq/top.html

< blank line>

FAQ de fclc: http://www.isty-info.uvsq.fr/~rumeau/fclc /



There is no portable way do do this directly, but there is a trick anyway (I
got it from c.l.c, BTW):

Define the list of constant values:

/* values.itm */
ITEM(0)
ITEM(1)
ITEM(2)
ITEM(3)
....
ITEM(254)
ITEM(255)

This file can be automatically created with a trivial C program.

Now, in your application, you do the following:

/* myapp.c */
<...>

#define ITEM(a) \
do_something(A[a]);

#include "values.itm"
#undef ITEM

That''s all folks.

I use massively this trick to define repetitive code. It''s very powerful and
makes solid code when mastered.

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/


这篇关于对于与预处理器等效的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆