- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
This is only an issue on GCC versions prior to 4.4, this was fixed in GCC 4.5.
是否可以告诉编译器 switch 中使用的变量适合提供的 case 语句?特别是如果它是一个小范围并且生成了一个跳转表。
extern int a;
main()
{
switch (a & 0x7) { // 0x7 == 111 values are 0-7
case 0: f0(); break;
case 1: f1(); break;
case 2: f2(); break;
case 3: f3(); break;
case 4: f4(); break;
case 5: f5(); break;
case 6: f6(); break;
case 7: f7(); break;
}
}
我尝试使用枚举对低位进行异或运算(作为示例),使用 gcc_unreachable() 无济于事。生成的代码始终检查变量是否在范围内,添加无意义的分支条件并移走跳表计算代码。
注意:这是在解码器的最内层循环中,性能非常重要。
There is no way to tell gcc that the default branch is never taken, although it will omit the default branch if it can prove that the value is never out of range based on earlier conditional checks.
那么,您如何帮助 gcc 证明变量适合并且上面的示例中没有默认分支? (当然,不添加条件分支。)
这是在带有 GCC 4.2(Xcode 的默认设置)的 OS X 10.6 Snow Leopard 上发生的。Linux 中的 GCC 4.4/4.3 没有发生(Nathon 和 Jens Gustedt 报告。)
<示例中的函数是为了提高可读性,认为它们是内联的或只是语句。在 x86 上进行函数调用非常昂贵。
此外,如注释中所述,该示例属于数据(大数据)循环内。
gcc 4.2/OS X 生成的代码是:
[...]
andl $7, %eax
cmpl $7, %eax
ja L11
mov %eax, %eax
leaq L20(%rip), %rdx
movslq (%rdx,%rax,4),%rax
addq %rdx, %rax
jmp *%rax
.align 2,0x90
L20:
.long L12-L20
.long L13-L20
.long L14-L20
.long L15-L20
.long L16-L20
.long L17-L20
.long L18-L20
.long L19-L20
L19:
[...]
问题出在cmp $7, %eax;
ja L11;
好的,我将采用丑陋的解决方案,并使用不带开关的不同版本并使用 goto 和 gcc 的 &&label 扩展为 4.4 以下的 gcc 版本添加一个特殊情况。
static void *jtb[] = { &&c_1, &&c_2, &&c_3, &&c_4, &&c_5, &&c_6, &&c_7, &&c_8 };
[...]
goto *jtb[a & 0x7];
[...]
while(0) {
c_1:
// something
break;
c_2:
// something
break;
[...]
}
请注意,标签数组是静态的,因此不会在每次调用时都对其进行计算。
最佳答案
或许您可以使用函数指针数组来代替开关?
#include <stdio.h>
typedef void (*func)(void);
static void f0(void) { printf("%s\n", __FUNCTION__); }
static void f1(void) { printf("%s\n", __FUNCTION__); }
static void f2(void) { printf("%s\n", __FUNCTION__); }
static void f3(void) { printf("%s\n", __FUNCTION__); }
static void f4(void) { printf("%s\n", __FUNCTION__); }
static void f5(void) { printf("%s\n", __FUNCTION__); }
static void f6(void) { printf("%s\n", __FUNCTION__); }
static void f7(void) { printf("%s\n", __FUNCTION__); }
int main(void)
{
const func f[8] = { f0, f1, f2, f3, f4, f5, f6, f7 };
int i;
for (i = 0; i < 8; ++i)
{
f[i]();
}
return 0;
}
关于c - 海湾合作委员会 4.4 : Avoid range check on switch/case statement in gcc?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3250178/
是否有使用 switch 语句检查数字 0-9 的简单方法?我正在编写一个程序来检查某些字符和数字。就像检查“\0”、“F”或“f”一样,想知道是否还有一种方法可以以类似的方式检查 0-9。我知道我可
我有一些数据需要转换,为此我需要一个超过 50 个案例的切换条件,我需要 3 次相同的案例,但在第三次我需要 50 个案例和一些更多,我不想写两次相同的代码。也许有可能做这样的事情。 switch (
我遇到这种情况,我必须检查两个 GET 变量。在检查语句内的第一个 switch 语句后,必须在第一个 case 循环内的第二个 switch 语句中检查第二个变量。 我无法在这里发布确切的代码,但这
如何使用函数指针代替 switch 语句? 最佳答案 与 ars 发布的链接略有不同的方法:您可以将 switch 语句中的值用作函数指针数组中的数组索引。所以不要写 switch (i) {
我必须评估很多条件。就我而言,我必须做这样的事情: switch(id) { case 5: // switch some other cases here case
switch 按钮位于 switch 语句内,但仅在 switch 语句外部时才有效这是我的代码:
我试图在 switch 语句中有一个 case 跳转到不同的 switch 语句。 在实践中,我希望用户在文本框中键入“关闭页面”,并且在浏览器关闭页面之前,我希望询问用户是否确定。输入“yes”将关
(引用java)我试图确定哪个更好,编写更多代码并可能节省一些计算时间,或者编写更少代码但可能牺牲一些计算时间。这就是我好奇的地方这样做会更有效率吗: switch (availability) {
我正在尝试构建一个 Android 应用程序,该应用程序可以访问加速度计传感器,并在单击按钮时将加速度计值(由 <> 包围)输出到串行 USB。当我更新值并尝试在 onClick 命令中调用它时遇到问
如何使用 Dwoo 模板引擎实现 switch case 语法。 最佳答案 {if 3 == 5} never gonna happen {elseif 3 == 3} if you don'
我想知道一个大的 switch 语句和几个小的 switch 语句之间是否有性能差异。 包含数百个案例的大型 switch 语句。 switch(quest){ case 1:
用户在 2 个选择框中进行选择后,我尝试计算出报值(value)。 看起来 1 需要 2 个 switch 语句。这可能吗? (可能的值比下面的值多得多。为了清楚起见,我删除了它们) var wor
我在主 while 循环内有一个开关,它将运行我的游戏。我正在尝试打破我的开关,以便转到不同的案例。下面的例子更好地解释了这一点: int j = 0; While(1){ switch(j){ ca
我用 Java 创建了一个菜单,其中每个选项都有另一个菜单。我想知道是否可以从内部菜单退出回到主菜单。 编辑:添加了主菜单选项 System.out.println("Main Menu");
我有一个计算新分数的方法。下面的方法有效,但问题是代码本身看起来可以被显着清理。我只是不知道什么是最好的方法。我根据 filterString 和枚举 individualScoreState 分配
在 Lisp 中使用字符串切换语句。 (defun switch(value) (case value (("XY") (print "XY"))
我想做这样的事情: int i = 0; switch(difficulty) { case 1: i++; break; case 2: i--; break; defaul
在 Obj-c 中,我做了一个 switch 语句,我曾经使用 UIsplitviewcontroller 在我的 iPad 应用程序中四处移动现在我想快速地做同样的事情……我试了几个小时,现在我唯一
我想写一个结构如下的报告: \begin{document} \input[option=a]{class} \input[option=b]{class} \in
我正在认真阅读 ngSwitch 的 AngularJS API 引用。当我谈到那部分时的指令: place an expression on the on="..." attribute (or t
我是一名优秀的程序员,十分优秀!