Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Dann Corbit <dcorbit@connx.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Optimisation questions |
| Date | 2012-02-06 09:40 -0800 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <MPG.2999b0533378d8cf989693@aioe.org> (permalink) |
| References | <jgkbiu$432$1@speranza.aioe.org> <jgkfso$1bh$2@dont-email.me> <jgkgra$886$1@dont-email.me> <jgkou6$g96$1@dont-email.me> <20120205034114.797@kylheku.com> |
In article <20120205034114.797@kylheku.com>, kaz@kylheku.com says...
> int div8_0(int x)
> {
> return x >> 3;
> }
>
> int div8_1(int x)
> {
> return x / 8;
> }
>
>
Using gcc 4.7 with -O3 on this program:
#include <stdio.h>
#include <stdlib.h>
int div8_0(int x)
{
return x >> 3;
}
int div8_1(int x)
{
return x / 8;
}
int main(void)
{
int a = rand();
int result0 = div8_0(a);
int result1 = div8_1(a);
printf("%d / 8 = %d, %d\n", a, result0, result1);
return 0;
}
gave this output, which adds additional instructions for the division as
opposed to the shift:
C:\tmp>type test.s
.file "test.c"
.text
.p2align 4,,15
.globl div8_0
.def div8_0; .scl 2; .type 32; .endef
.seh_proc div8_0
div8_0:
.seh_endprologue
movl %ecx, %eax
sarl $3, %eax
ret
.seh_endproc
.p2align 4,,15
.globl div8_1
.def div8_1; .scl 2; .type 32; .endef
.seh_proc div8_1
div8_1:
.seh_endprologue
leal 7(%rcx), %eax
testl %ecx, %ecx
cmovns %ecx, %eax
sarl $3, %eax
ret
.seh_endproc
.def __main; .scl 2; .type 32; .endef
.section .rdata,"dr"
.LC0:
.ascii "%d / 8 = %d, %d\12\0"
.section .text.startup,"x"
.p2align 4,,15
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
subq $40, %rsp
.seh_stackalloc 40
.seh_endprologue
call __main
call rand
leal 7(%rax), %r9d
testl %eax, %eax
movl %eax, %r8d
leaq .LC0(%rip), %rcx
movl %eax, %edx
cmovns %eax, %r9d
sarl $3, %r8d
sarl $3, %r9d
call printf
xorl %eax, %eax
addq $40, %rsp
ret
.seh_endproc
.def rand; .scl 2; .type 32; .endef
.def printf; .scl 2; .type 32; .endef
With similar output for the MS compiler:
; Listing generated by Microsoft (R) Optimizing Compiler Version
16.00.40219.01
include listing.inc
INCLUDELIB LIBCMT
INCLUDELIB OLDNAMES
_DATA SEGMENT
$SG4006 DB '%d / 8 = %d, %d', 0aH, 00H
_DATA ENDS
PUBLIC div8_0
; Function compile flags: /Ogtpy
_TEXT SEGMENT
x$ = 8
div8_0 PROC
; File c:\tmp\test.c
; Line 6
sar ecx, 3
mov eax, ecx
; Line 7
ret 0
div8_0 ENDP
_TEXT ENDS
PUBLIC div8_1
; Function compile flags: /Ogtpy
_TEXT SEGMENT
x$ = 8
div8_1 PROC
; Line 11
mov eax, ecx
cdq
and edx, 7
add eax, edx
sar eax, 3
; Line 12
ret 0
div8_1 ENDP
_TEXT ENDS
PUBLIC main
EXTRN printf:PROC
EXTRN rand:PROC
pdata SEGMENT
$pdata$main DD imagerel $LN7
DD imagerel $LN7+53
DD imagerel $unwind$main
pdata ENDS
xdata SEGMENT
$unwind$main DD 010401H
DD 04204H
; Function compile flags: /Ogtpy
xdata ENDS
_TEXT SEGMENT
main PROC
; Line 15
$LN7:
sub rsp, 40 ; 00000028H
; Line 16
call rand
; Line 19
lea rcx, OFFSET FLAT:$SG4006
mov r11d, eax
cdq
and edx, 7
mov r8d, r11d
lea r9d, DWORD PTR [rdx+rax]
sar r8d, 3
mov edx, r11d
sar r9d, 3
call printf
; Line 20
xor eax, eax
; Line 21
add rsp, 40 ; 00000028H
ret 0
main ENDP
_TEXT ENDS
END
I didn't bother to time it, but it seems that state of the art compilers
create faster code for the shift than for the division.
I guess that the bottom line is that if you need the last little
smackerel of performance then you have better try all the alternatives
and benchmark them.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Optimisation questions Raj Pashwar <raj121190@hotmail.NOSPAM.com> - 2012-02-04 22:30 +0000
Re: Optimisation questions Ian Collins <ian-news@hotmail.com> - 2012-02-05 11:44 +1300
Re: Optimisation questions Keith Thompson <kst-u@mib.org> - 2012-02-04 14:52 -0800
Re: Optimisation questions Ian Collins <ian-news@hotmail.com> - 2012-02-05 11:55 +1300
Re: Optimisation questions Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-02-21 18:41 +0200
Re: Optimisation questions "BartC" <bc@freeuk.com> - 2012-02-04 22:44 +0000
Re: Optimisation questions "BartC" <bc@freeuk.com> - 2012-02-04 23:41 +0000
Re: Optimisation questions Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-02-04 17:51 -0500
Re: Optimisation questions Keith Thompson <kst-u@mib.org> - 2012-02-04 14:52 -0800
Re: Optimisation questions James Kuyper <jameskuyper@verizon.net> - 2012-02-04 18:44 -0500
Re: Optimisation questions Stephen Sprunk <stephen@sprunk.org> - 2012-02-04 18:00 -0600
Re: Optimisation questions James Kuyper <jameskuyper@verizon.net> - 2012-02-04 21:18 -0500
Re: Optimisation questions Kaz Kylheku <kaz@kylheku.com> - 2012-02-05 03:04 +0000
Re: Optimisation questions Dann Corbit <dcorbit@connx.com> - 2012-02-06 09:40 -0800
Re: Optimisation questions Willem <willem@toad.stack.nl> - 2012-02-06 18:51 +0000
Re: Optimisation questions Markus Wichmann <nullplan@gmx.net> - 2012-02-07 14:19 +0100
Re: Optimisation questions Dann Corbit <dcorbit@connx.com> - 2012-02-07 14:25 -0800
Re: Optimisation questions Stephen Sprunk <stephen@sprunk.org> - 2012-02-06 17:29 -0600
Re: Optimisation questions Kaz Kylheku <kaz@kylheku.com> - 2012-02-06 23:52 +0000
Re: Optimisation questions pete <pfiland@mindspring.com> - 2012-02-06 19:10 -0500
Re: Optimisation questions Raj Pashwar <raj121190@hotmail.NOSPAM.com> - 2012-02-05 10:51 +0000
Re: Optimisation questions Ian Collins <ian-news@hotmail.com> - 2012-02-06 00:08 +1300
Re: Optimisation questions Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-02-05 08:13 -0500
Re: Optimisation questions "BartC" <bc@freeuk.com> - 2012-02-05 11:33 +0000
Re: Optimisation questions Zoltan Kocsi <zoltan@bendor.com.au> - 2012-02-05 23:24 +1100
Re: Optimisation questions Raj Pashwar <raj121190@hotmail.NOSPAM.com> - 2012-02-05 22:28 +0000
Re: Optimisation questions Kaz Kylheku <kaz@kylheku.com> - 2012-02-05 22:37 +0000
Re: Optimisation questions Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-02-05 17:48 -0500
Re: Optimisation questions Keith Thompson <kst-u@mib.org> - 2012-02-05 15:57 -0800
Re: Optimisation questions "BartC" <bc@freeuk.com> - 2012-02-05 12:53 +0000
Re: Optimisation questions Geoff <geoff@invalid.invalid> - 2012-02-04 16:07 -0800
Re: Optimisation questions "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2012-02-04 15:37 -0800
Re: Optimisation questions Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-02-05 04:26 -0800
Re: Optimisation questions lawrence.jones@siemens.com - 2012-02-07 10:56 -0500
Re: Optimisation questions Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-02-05 04:29 -0800
Re: Optimisation questions Michael Angelo Ravera <maravera@prodigy.net> - 2012-02-05 23:24 -0800
csiph-web