[Freeciv-Dev] Re: (PR#2745) Change style guide to favor a++ instead of +
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
On Tue, Jan 07, 2003 at 09:16:06PM -0800, rwetmore@xxxxxxxxxxxx via RT wrote:
>
> If you don't immediately use the value a++ is good.
>
> If you do, then the compiler has to save a somewhere
> before doing the update, and tends to generate extra
> instructions.
>
> In the latter case ++a is better as it can just update.
>
> So either
> for( i = 1; i < max; i++)
> or
> for( i = 0; ++i < max; )
>
> with the sedond actually generating more efficient code.
I love these discussions: it is easy to verify or to disprove.
$ cat b.c
int f1(int *p, int n)
{
int i, s = 0;
for (i = 1; i < n; i++) {
s += p[i];
}
return s;
}
int f2(int *p, int n)
{
int i, s = 0;
for (i = 0; ++i < n;) {
s += p[i];
}
return s;
}
$ gcc -O2 b.c -S
$ cat b.s
...
f1:
...
.L6:
addl (%ebx,%edx,4), %eax
incl %edx
cmpl %ecx, %edx
jl .L6
...
f2:
...
.L14:
addl (%ebx,%edx,4), %eax
incl %edx
cmpl %ecx, %edx
jl .L14
....
The intel compiler 6.0 however unrolls f1 and produces the same code
for f2 as gcc. Same for 7.0.
Raimar
--
email: rf13@xxxxxxxxxxxxxxxxx
"The two rules for success in life are:
1) Never tell them everything you know."
# -- Machine type IA32
# mark_description "Intel(R) C++ Compiler for 32-bit applications, Version
6.0.1 Build 20020822Z";
# mark_description "-long_double -S -O2 -o intel_6_0.s";
.ident "Intel(R) C++ Compiler for 32-bit applications, Version 6.0.1
Build 20020822Z"
.ident "-long_double -S -O2 -o intel_6_0.s"
.file "b.c"
.text
.data
.align 4
.bss
.align 4
.ident "-?comment:Intel(R) C++ Compiler for 32-bit applications,
Version 6.0.1 Build 20020822Z : b.c : -long_double -S -O2 -o intel_6_0.s"
.data
.text
# -- Begin f1
# mark_begin;
.align 4,0x90
# parameter 1: 12 + %esp
# parameter 2: 16 + %esp
.globl f1
f1:
.B1.1: # Preds .B1.0
pushl %esi #2.1
pushl %ebx #2.1
movl 12(%esp), %ebx #1.5
movl 16(%esp), %ecx #1.5
xorl %eax, %eax #3.10
movl $1, %edx #5.8
cmpl $1, %ecx #5.3
jle .B1.9 # Prob 2% #5.3
# LOE eax edx ecx ebx ebp edi
.B1.2: # Preds .B1.1
lea -1(%ecx), %esi #5.19
cmpl $6, %esi #5.3
jl .B1.7 # Prob 0% #5.3
# LOE eax edx ecx ebx ebp edi
.B1.3: # Preds .B1.2
lea -6(%ecx), %esi #5.19
# LOE eax edx ecx ebx ebp esi edi
.B1.4: # Preds .B1.3 .B1.4
addl (%ebx,%edx,4), %eax #6.5
addl 4(%ebx,%edx,4), %eax #6.5
addl 8(%ebx,%edx,4), %eax #6.5
addl 12(%ebx,%edx,4), %eax #6.5
addl 16(%ebx,%edx,4), %eax #6.5
addl $5, %edx #5.22
cmpl %esi, %edx #5.3
jle .B1.4 # Prob 91% #5.3
# LOE eax edx ecx ebx ebp esi edi
.B1.7: # Preds .B1.2 .B1.4 .B1.7
addl (%ebx,%edx,4), %eax #6.5
incl %edx #5.22
cmpl %ecx, %edx #5.3
jl .B1.7 # Prob 80% #5.3
# LOE eax edx ecx ebx ebp edi
.B1.9: # Preds .B1.7 .B1.1
popl %ebx #8.10
popl %esi #8.10
ret #8.10
.align 4,0x90
# LOE
# mark_end;
.type f1,@function
.size f1,.-f1
.data
# -- End f1
.data
.text
# -- Begin f2
# mark_begin;
.align 4,0x90
# parameter 1: 8 + %esp
# parameter 2: 12 + %esp
.globl f2
f2:
.B2.1: # Preds .B2.0
pushl %ebx #12.1
movl 8(%esp), %ecx #11.5
movl 12(%esp), %edx #11.5
xorl %ebx, %ebx #13.10
movl $1, %eax #15.17
cmpl $1, %edx #15.3
jle .B2.5 # Prob 2% #15.3
# LOE eax edx ecx ebx ebp esi edi
.B2.3: # Preds .B2.1 .B2.3
addl (%ecx,%eax,4), %ebx #16.5
incl %eax #15.17
cmpl %edx, %eax #15.3
jl .B2.3 # Prob 90% #15.3
# LOE eax edx ecx ebx ebp esi edi
.B2.5: # Preds .B2.3 .B2.1
movl %ebx, %eax #18.10
popl %ebx #18.10
ret #18.10
.align 4,0x90
# LOE
# mark_end;
.type f2,@function
.size f2,.-f2
.data
# -- End f2
.data
# End
# -- Machine type IA32
# mark_description "Intel(R) C++ Compiler for 32-bit applications, Version 7.0
Build 20021021Z";
# mark_description "-long_double -Xlinker -rpath -Xlinker
/opt/intel/compiler70/ia32/lib -S -O2 -o intel_7_0.s";
.ident "Intel(R) C++ Compiler for 32-bit applications, Version 7.0
Build 20021021Z"
.ident "-long_double -Xlinker -rpath -Xlinker
/opt/intel/compiler70/ia32/lib -S -O2 -o intel_7_0.s"
.file "b.c"
.text
.data
.align 4
.bss
.align 4
.data
.text
# -- Begin f1
# mark_begin;
.align 4,0x90
.globl f1
f1:
# parameter 1: 12 + %esp
# parameter 2: 16 + %esp
..B1.1: # Preds ..B1.0
pushl %ebx #2.1
pushl %esi #2.1
movl 16(%esp), %ebx #1.5
xorl %edx, %edx #3.10
movl $1, %ecx #5.8
cmpl $1, %ebx #5.3
jle ..B1.9 # Prob 2% #5.3
# LOE edx ecx ebx ebp esi edi
..B1.2: # Preds ..B1.1
lea -1(%ebx), %eax #5.19
cmpl $6, %eax #5.3
jl ..B1.6 # Prob 0% #5.3
# LOE edx ecx ebx ebp esi edi
..B1.3: # Preds ..B1.2
movl %ebp, (%esp) #5.19
movl 12(%esp), %ebp #5.19
lea -6(%ebx), %eax #5.19
# LOE eax edx ecx ebx ebp esi edi
..B1.4: # Preds ..B1.3 ..B1.4
addl (%ebp,%ecx,4), %edx #6.5
addl 4(%ebp,%ecx,4), %edx #6.5
addl 8(%ebp,%ecx,4), %edx #6.5
addl 12(%ebp,%ecx,4), %edx #6.5
addl 16(%ebp,%ecx,4), %edx #6.5
addl $5, %ecx #5.22
cmpl %eax, %ecx #5.3
jle ..B1.4 # Prob 91% #5.3
# LOE eax edx ecx ebx ebp esi edi
..B1.5: # Preds ..B1.4
movl (%esp), %ebp #
# LOE edx ecx ebx ebp esi edi
..B1.6: # Preds ..B1.5 ..B1.2
movl 12(%esp), %eax #
# LOE eax edx ecx ebx ebp esi edi
..B1.7: # Preds ..B1.6 ..B1.7
addl (%eax,%ecx,4), %edx #6.5
addl $1, %ecx #5.22
cmpl %ebx, %ecx #5.3
jl ..B1.7 # Prob 80% #5.3
# LOE eax edx ecx ebx ebp esi edi
..B1.9: # Preds ..B1.7 ..B1.1
movl %edx, %eax #8.10
popl %ecx #8.10
popl %ebx #8.10
ret #8.10
.align 4,0x90
# LOE
# mark_end;
.type f1,@function
.size f1,.-f1
.data
# -- End f1
.data
.text
# -- Begin f2
# mark_begin;
.align 4,0x90
.globl f2
f2:
# parameter 1: 8 + %esp
# parameter 2: 12 + %esp
..B2.1: # Preds ..B2.0
pushl %esi #12.1
movl 12(%esp), %ecx #11.5
xorl %edx, %edx #13.10
cmpl $1, %ecx #15.3
jle ..B2.5 # Prob 2% #15.3
# LOE edx ecx ebx ebp edi
..B2.2: # Preds ..B2.1
movl 8(%esp), %esi #
lea 4(%esi), %eax #
lea (%esi,%ecx,4), %ecx #
# LOE eax edx ecx ebx ebp edi
..B2.3: # Preds ..B2.2 ..B2.3
addl (%eax), %edx #16.5
addl $4, %eax #15.17
cmpl %ecx, %eax #15.3
jb ..B2.3 # Prob 90% #15.3
# LOE eax edx ecx ebx ebp edi
..B2.5: # Preds ..B2.3 ..B2.1
movl %edx, %eax #18.10
popl %esi #18.10
ret #18.10
.align 4,0x90
# LOE
# mark_end;
.type f2,@function
.size f2,.-f2
.data
# -- End f2
.data
# End
|
|