【发布时间】:2012-10-01 20:16:03
【问题描述】:
我看不出 gcc 对限制指针的代码有什么不同。
文件1
void test (int *a, int *b, int *c)
{
while (*a)
{
*c++ = *a++ + *b++;
}
}
文件2
void test (int *restrict a, int *restrict b, int *restrict c)
{
while (*a)
{
*c++ = *a++ + *b++;
}
}
编译
gcc -S -std=c99 -masm=intel file1.c
gcc -S -std=c99 -masm=intel file2.c
file1.s 和 file2.s 都相同,除了 .file 行,它告诉文件名:
.file "file1.c"
.text
.globl test
.type test, @function
test:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movq %rdi, -8(%rbp)
movq %rsi, -16(%rbp)
movq %rdx, -24(%rbp)
jmp .L2
.L3:
movq -8(%rbp), %rax
movl (%rax), %edx
movq -16(%rbp), %rax
movl (%rax), %eax
addl %eax, %edx
movq -24(%rbp), %rax
movl %edx, (%rax)
addq $4, -24(%rbp)
addq $4, -8(%rbp)
addq $4, -16(%rbp)
.L2:
movq -8(%rbp), %rax
movl (%rax), %eax
testl %eax, %eax
jne .L3
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size test, .-test
.ident "GCC: (GNU) 4.6.3 20120306 (Red Hat 4.6.3-2)"
.section .note.GNU-stack,"",@progbits
这两个代码都从内存中读取,然后将a指向的内存位置分配给b。我预计restrict 版本不会重新读取a 和b 的地址,a 和b 的地址将在寄存器中递增并在最后写入内存。
我做错了什么吗?还是选例行不行?
我尝试使用不同的开关 -O0、-O1、-O2、-O3、-Ofast 和 -fstrict-aliasing,两个文件的结果相同。
注意: gcc --version = gcc (GCC) 4.6.3 20120306 (Red Hat 4.6.3-2)
编辑代码已更改。
【问题讨论】:
标签: c pointers restrict-qualifier