- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在写下这个问题的答案时:Using variable vs. using number我用 -O3 运行了 clang x86 9.0.0/trunk 以查看它是否可以对这个简单代码进行尾调用优化:
int faculty1 (const unsigned int n) {
return n == 1 ? n : n * faculty1(n - 1);
}
.LCPI0_0:
.long 0 # 0x0
.long 4294967295 # 0xffffffff
.long 4294967294 # 0xfffffffe
.long 4294967293 # 0xfffffffd
.LCPI0_1:
.long 1 # 0x1
.long 1 # 0x1
.long 1 # 0x1
.long 1 # 0x1
.LCPI0_2:
.long 4294967292 # 0xfffffffc
.long 4294967292 # 0xfffffffc
.long 4294967292 # 0xfffffffc
.long 4294967292 # 0xfffffffc
.LCPI0_3:
.long 4294967288 # 0xfffffff8
.long 4294967288 # 0xfffffff8
.long 4294967288 # 0xfffffff8
.long 4294967288 # 0xfffffff8
.LCPI0_4:
.long 4294967284 # 0xfffffff4
.long 4294967284 # 0xfffffff4
.long 4294967284 # 0xfffffff4
.long 4294967284 # 0xfffffff4
.LCPI0_5:
.long 4294967280 # 0xfffffff0
.long 4294967280 # 0xfffffff0
.long 4294967280 # 0xfffffff0
.long 4294967280 # 0xfffffff0
.LCPI0_6:
.long 4294967276 # 0xffffffec
.long 4294967276 # 0xffffffec
.long 4294967276 # 0xffffffec
.long 4294967276 # 0xffffffec
.LCPI0_7:
.long 4294967272 # 0xffffffe8
.long 4294967272 # 0xffffffe8
.long 4294967272 # 0xffffffe8
.long 4294967272 # 0xffffffe8
.LCPI0_8:
.long 4294967268 # 0xffffffe4
.long 4294967268 # 0xffffffe4
.long 4294967268 # 0xffffffe4
.long 4294967268 # 0xffffffe4
.LCPI0_9:
.long 4294967264 # 0xffffffe0
.long 4294967264 # 0xffffffe0
.long 4294967264 # 0xffffffe0
.long 4294967264 # 0xffffffe0
faculty1: # @faculty1
mov eax, 1
cmp edi, 1
je .LBB0_12
lea ecx, [rdi - 1]
mov eax, 1
cmp ecx, 8
jb .LBB0_11
mov r8d, ecx
and r8d, -8
movd xmm0, edi
pshufd xmm6, xmm0, 0 # xmm6 = xmm0[0,0,0,0]
paddd xmm6, xmmword ptr [rip + .LCPI0_0]
lea edx, [r8 - 8]
mov esi, edx
shr esi, 3
add esi, 1
mov eax, esi
and eax, 3
cmp edx, 24
jae .LBB0_4
movdqa xmm1, xmmword ptr [rip + .LCPI0_1] # xmm1 = [1,1,1,1]
movdqa xmm4, xmm1
jmp .LBB0_6
.LBB0_4:
and esi, -4
neg esi
movdqa xmm1, xmmword ptr [rip + .LCPI0_1] # xmm1 = [1,1,1,1]
movdqa xmm9, xmmword ptr [rip + .LCPI0_3] # xmm9 = [4294967288,4294967288,4294967288,4294967288]
movdqa xmm10, xmmword ptr [rip + .LCPI0_4] # xmm10 = [4294967284,4294967284,4294967284,4294967284]
movdqa xmm11, xmmword ptr [rip + .LCPI0_5] # xmm11 = [4294967280,4294967280,4294967280,4294967280]
movdqa xmm12, xmmword ptr [rip + .LCPI0_6] # xmm12 = [4294967276,4294967276,4294967276,4294967276]
movdqa xmm13, xmmword ptr [rip + .LCPI0_7] # xmm13 = [4294967272,4294967272,4294967272,4294967272]
movdqa xmm14, xmmword ptr [rip + .LCPI0_8] # xmm14 = [4294967268,4294967268,4294967268,4294967268]
movdqa xmm15, xmmword ptr [rip + .LCPI0_9] # xmm15 = [4294967264,4294967264,4294967264,4294967264]
movdqa xmm4, xmm1
.LBB0_5: # =>This Inner Loop Header: Depth=1
movdqa xmm0, xmm6
paddd xmm0, xmmword ptr [rip + .LCPI0_2]
pshufd xmm5, xmm1, 245 # xmm5 = xmm1[1,1,3,3]
pshufd xmm7, xmm6, 245 # xmm7 = xmm6[1,1,3,3]
pmuludq xmm7, xmm5
pmuludq xmm1, xmm6
pshufd xmm5, xmm4, 245 # xmm5 = xmm4[1,1,3,3]
pshufd xmm2, xmm0, 245 # xmm2 = xmm0[1,1,3,3]
pmuludq xmm2, xmm5
pmuludq xmm0, xmm4
movdqa xmm4, xmm6
paddd xmm4, xmm9
movdqa xmm5, xmm6
paddd xmm5, xmm10
pmuludq xmm1, xmm4
pshufd xmm4, xmm4, 245 # xmm4 = xmm4[1,1,3,3]
pmuludq xmm4, xmm7
pmuludq xmm0, xmm5
pshufd xmm5, xmm5, 245 # xmm5 = xmm5[1,1,3,3]
pmuludq xmm5, xmm2
movdqa xmm2, xmm6
paddd xmm2, xmm11
movdqa xmm7, xmm6
paddd xmm7, xmm12
pshufd xmm3, xmm2, 245 # xmm3 = xmm2[1,1,3,3]
pmuludq xmm3, xmm4
pmuludq xmm2, xmm1
pshufd xmm8, xmm7, 245 # xmm8 = xmm7[1,1,3,3]
pmuludq xmm8, xmm5
pmuludq xmm7, xmm0
movdqa xmm0, xmm6
paddd xmm0, xmm13
movdqa xmm5, xmm6
paddd xmm5, xmm14
pmuludq xmm2, xmm0
pshufd xmm1, xmm2, 232 # xmm1 = xmm2[0,2,2,3]
pshufd xmm0, xmm0, 245 # xmm0 = xmm0[1,1,3,3]
pmuludq xmm0, xmm3
pshufd xmm0, xmm0, 232 # xmm0 = xmm0[0,2,2,3]
punpckldq xmm1, xmm0 # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
pmuludq xmm7, xmm5
pshufd xmm4, xmm7, 232 # xmm4 = xmm7[0,2,2,3]
pshufd xmm0, xmm5, 245 # xmm0 = xmm5[1,1,3,3]
pmuludq xmm0, xmm8
pshufd xmm0, xmm0, 232 # xmm0 = xmm0[0,2,2,3]
punpckldq xmm4, xmm0 # xmm4 = xmm4[0],xmm0[0],xmm4[1],xmm0[1]
paddd xmm6, xmm15
add esi, 4
jne .LBB0_5
.LBB0_6:
movdqa xmm5, xmm1
movdqa xmm0, xmm4
test eax, eax
je .LBB0_9
neg eax
movdqa xmm2, xmmword ptr [rip + .LCPI0_2] # xmm2 = [4294967292,4294967292,4294967292,4294967292]
movdqa xmm3, xmmword ptr [rip + .LCPI0_3] # xmm3 = [4294967288,4294967288,4294967288,4294967288]
.LBB0_8: # =>This Inner Loop Header: Depth=1
movdqa xmm0, xmm6
paddd xmm0, xmm2
movdqa xmm5, xmm6
pmuludq xmm5, xmm1
pshufd xmm5, xmm5, 232 # xmm5 = xmm5[0,2,2,3]
pshufd xmm1, xmm1, 245 # xmm1 = xmm1[1,1,3,3]
pshufd xmm7, xmm6, 245 # xmm7 = xmm6[1,1,3,3]
pmuludq xmm7, xmm1
pshufd xmm1, xmm7, 232 # xmm1 = xmm7[0,2,2,3]
punpckldq xmm5, xmm1 # xmm5 = xmm5[0],xmm1[0],xmm5[1],xmm1[1]
pshufd xmm1, xmm0, 245 # xmm1 = xmm0[1,1,3,3]
pmuludq xmm0, xmm4
pshufd xmm0, xmm0, 232 # xmm0 = xmm0[0,2,2,3]
pshufd xmm4, xmm4, 245 # xmm4 = xmm4[1,1,3,3]
pmuludq xmm4, xmm1
pshufd xmm1, xmm4, 232 # xmm1 = xmm4[0,2,2,3]
punpckldq xmm0, xmm1 # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
paddd xmm6, xmm3
movdqa xmm1, xmm5
movdqa xmm4, xmm0
inc eax
jne .LBB0_8
.LBB0_9:
pshufd xmm1, xmm5, 245 # xmm1 = xmm5[1,1,3,3]
pshufd xmm2, xmm0, 245 # xmm2 = xmm0[1,1,3,3]
pmuludq xmm2, xmm1
pmuludq xmm0, xmm5
pshufd xmm1, xmm0, 78 # xmm1 = xmm0[2,3,0,1]
pmuludq xmm1, xmm0
pshufd xmm0, xmm2, 162 # xmm0 = xmm2[2,0,2,2]
pmuludq xmm0, xmm2
pmuludq xmm0, xmm1
movd eax, xmm0
cmp ecx, r8d
je .LBB0_12
sub edi, r8d
.LBB0_11: # =>This Inner Loop Header: Depth=1
imul eax, edi
add edi, -1
cmp edi, 1
jne .LBB0_11
.LBB0_12:
ret
faculty1:
mov eax, 1
cmp edi, 1
je .L4
.L3:
mov edx, edi
sub edi, 1
imul eax, edx
cmp edi, 1
jne .L3
ret
.L4:
ret
最佳答案
我安装了 Clang 7,它做同样的事情,这意味着它不是编译器错误。
正如评论中所指出的,这个递归被转换成一个被向量化的循环。
有符号结果与无符号操作数之间的乘法将结果提升为 unsigned int
,然后转换回 int
以实现定义的方式。这意味着 Clang 不能/不会使用整数溢出作为优化方法。
这个测试程序:
#include <stdio.h>
int faculty1 (const unsigned int n) {
return n == 1 ? n : n * faculty1(n - 1);
}
int main(void)
{
for(int i = 0; i < 65536; i++)
{
printf("%d: %d\n", i, faculty1(i));
}
}
-O2
, 8.6 秒运行 GCC 8.3.0
-O2
.所以是的,Clang 的版本更快。我认为这有点矫枉过正,但它有效并且符合标准。
关于c - clang 试图优化这个简单的递归算法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60773870/
我是 C++ 的新手,我在使用这段代码时遇到了问题: string output_date(int day, int month, int year){ string date; if
所以我这样做了 tar cvzf test.zip FP 为了创建目录 FP 的 zip 但是,它会列出 zip 中的目录 FP/ FP/php/ FP/php/pdf/ FP/php/docs/ F
我正在尝试在 Swift、Xcode 7.3(所以是 Swift 2.2)中创建一个通用类,但我似乎无法让它通过编译器: protocol Struct1Protocol { } struct Str
我的测试用例是这样的: class FooTest extends PHPUnit_Framework_TestCase { /** @covers MyClass::bar */ f
我正在尝试将brew install wine作为使electron-builder工作的一步。但是我所能得到的只是以下响应: ==> Installing dependencies for wine
我这样做: string[,] string1 = {{"one", "0"},{"Two", "5"},{"Three","1"}}; int b = 0; for(int i = 0; i <=
我正在尝试使用 SetWindowsHookEx 键盘 Hook Notepad.exe。 如您所见,工作线程正在将其 ASCII 代码(即 wParam)发送到指定的服务器。 UINT WINAPI
我正在尝试将 ListView 实现到我的 Fragment 中,但无论我尝试什么,我都会得到一个 NullPointerException。我检查对象是否为 null 并记录是否为 null,看起来
我尝试在一行中对齐两个 div。使用 float left 属性,一切顺利。但是当我在 div 中使用图像时,它开始产生问题。 所以这是我的示例代码:- Some headi
我目前正在使用此代码来获取图像的灰度图像表示并以 (512, 370, 1) 的格式表示它大批。 img_instance = cv2.imread(df.iloc[i][x_col]) / 255.
总结 我正在创建一个简单的应用程序,它允许用户选择一个包含顶级窗口的进程。用户首先键入 native DLL(而非托管 DLL)的路径。然后用户键入将在 Hook 过程中调用的方法的名称。该方法不得返
我是一名优秀的程序员,十分优秀!