Duff的设备代码如何编译? [英] How can Duff's device code be compiled?

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

问题描述

我明白为什么 Duff 的设备 比可以展开的普通循环代码更快,但是没有优化.但我还不明白如何编译代码.
我想这是关于 switch 语法的技巧.但现在不是了.

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.

switch句中怎么能存在do while句?很奇怪.
有谁可以解释一下吗?

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

另一个问题.为什么 duff 使用 8?它可以是 16、65536 或其他.因为代码大小?还有别的原因吗?例如,缓存或流水线的好处.

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.

推荐答案

为什么 Duff 的 Device 可以编译,一个简单的解释是 switch 语句的语法对于switch 语句块可能需要占用.在受控语句中有一些限制,以及在 switch 之外不允许的几件事(casedefault标签).但除此之外,受控语句只是任何其他语句,可能有标签供 switch 为目标.

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:

  • 控制表达式必须是整数类型
  • VLA 可以在 switch 语句中出现的位置受到限制
  • case标签表达式必须是整型常量表达式
  • 不能有重复的 case 标签表达式或 default 标签
  • 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

除此之外,语句块中允许的任何构造都应允许在受控语句中(除了 casedefault 标签都可以).请记住,casedefault 只是开关根据控制表达式和 case 标签表达式跳转到的标签.正如 Potatoswatter 所说的切换code> 只是一个计算出来的 goto.所以就像 goto 可以跳到循环的中间一样,switch 也可以.

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.

此外,我认为您可能会从 Duff 的设备中受益的情况在今天非常罕见(我认为即使在 1980 年代也很少见).不要忘记 Tom Duff 本人在他的 description 中说过以下内容:

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:

  • 恶心,不是吗?"
  • 我对这一发现感到既自豪又厌恶."

与最初描述时相比,Duff 的设备应该被视为一种好奇心而不是使用的工具.

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

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

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