如何达夫设备code编译? [英] How can Duff's device code be compiled?

查看:260
本文介绍了如何达夫设备code编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白了为什么达夫设备是比正常循环code更快从而可以解开,但不进行优化。但我不明白是怎么code,可又编译。结果
我想这是一个关于开关的语法把戏。但现在不是了。

I understood why Duff's device is faster than normal loop code which can be unrolled but is not optimized. But I can't understand how the code can be compiled yet.
I guess it's a trick about the switch syntax. But not anymore.

哪有的做的,而的句子中的开关的句子存在吗?十分怪异。结果
是否有任何人谁可以解释一下吗?

How can do while sentence exist in switch sentence? Very weird.
Is there anyone who can explain this?

编辑:
另一个问题。
为什么达夫使用8?这可能是16,65536或什么的。因为code尺寸的?难道还有其他原因吗?例如,高速缓存或流水线的好处。

Another question. Why did duff use 8? It could be 16, 65536 or whatever. Because of code size? Is there another reason? For example, cache or pipelining benefits.

推荐答案

为什么达夫设备编译一个简单的解释是,开关的语法语句不是特别具体有关的开关语句块可能需要采取的形式。有一些限制,以及一对夫妇在受控声明允许的事情,不是在开关允许外(即情况默认标签)。但除此之外,受控声明就任何其他声明,与有对的切换到目标标签的可能性。

A simple explanation of why Duff's Device compiles is that the syntax of the switch statement isn't particularly specific about the form that the switch statement block might need to take. There are a few restrictions, and a couple of things permitted in the controlled statement that aren't permitted outside a switch (the case and default labels). But other than that, the controlled statement is just any other statement, with the likelihood that there are labels for the switch to target.

下面是从C99的语法:

Here's the syntax from C99:

switch ( expression ) statement

除了语法,该标准规定了一些限制:

Beyond the syntax, the standard imposes a few constraints:


  • 控制前pression必须有一个整数类型

  • 有什么地方可以沃拉斯switch语句中出现的限制

  • 情况标签前pressions必须是整型常量前pressions

  • 不能有重复的情况标签前pressions或默认标签

  • the controlling expression must have an integer type
  • there are restrictions about where VLAs can occur in the switch statement
  • case label expressions must be integer constant expressions
  • there cannot be duplicate case label expressions or default labels

除此之外,在语句块允许的任何结构应该在受控的语句允许(加了情况默认标签OK)。请记住,情况默认只是标签交换机跳到基础上,控制前pression和情况标签前pressions。 <一href=\"http://stackoverflow.com/questions/5569416/how-can-be-duffs-device-$c$c-compiled/5569851#5569851\">As Potatoswatter说,开关只是一个计算转到。所以就像转到可以跳进一个循环的中间,这样可以通过开关

Other than that, any construct permitted in a statement block should be permitted in the controlled statement (with the addition that case and default labels are OK). Remember that case and default are just labels that the switch jumps to based on the controlling expression and the case label expressions. As Potatoswatter says, switch is just a computed goto. So just like goto can jump into the middle of a loop, so can switch.

另外,我认为,在这里你可以看到达夫设备福利金的案件是pretty罕见今日(我觉得他们是罕见的,即使在20世纪80年代)。不要忘了,汤姆达夫自己说在他的描述如下:

Also, I think that the cases where you might see a benefit from Duff's Device are pretty rare today (I think they were rare even in the 1980's). Don't forget that Tom Duff himself said the following in his description:


  • 讨厌,不是吗?

  • 我感到骄傲和厌恶在这一发现的组合。

甚至比当它最初描述,达夫设备应比使用工具被认为更多的是好奇了。

Even more than when it was initially described, Duff's Device should be considered more of a curiosity than a tool to use.

这篇关于如何达夫设备code编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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