Complete.Org: Mailing Lists: Archives: freeciv-dev: September 2001:
[Freeciv-Dev] Re: the directional system
Home

[Freeciv-Dev] Re: the directional system

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: Trent Piepho <xyzzy@xxxxxxxxxxxxx>
Cc: jdorje@xxxxxxxxxxxxxxxxxxxxx, freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] Re: the directional system
From: Raimar Falke <hawk@xxxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 7 Sep 2001 14:18:45 +0200
Reply-to: rf13@xxxxxxxxxxxxxxxxxxxxxx

On Fri, Sep 07, 2001 at 04:51:36AM -0700, Trent Piepho wrote:
> On Fri, 7 Sep 2001, Jason Dorje Short wrote:
> 
> +#define DIR_REVERSE(dir) (((dir) + 4) % 8)
> 
> This is a faster way to do it.  Modulus operations are very expensive, but a
> bitwise and is very cheap.
> 
> +#define DIR_REVERSE(dir) (((dir) + 4) & 7)

$ cat a.c

#include <stdio.h>

unsigned int f(unsigned int x)
{
        return (x+4)%8;
}

unsigned int g(unsigned int x)
{
        return (x+4)&7;
}

int main()
{
        int i;

        for(i=0;i<8;i++)
                printf("%d %d %d\n",i,f(i),g(i));
        return 0;
}
$ gcc -S -O2 a.c
$ cat a.s
        .file   "a.c"
        .version        "01.01"
gcc2_compiled.:
.text
        .align 4
.globl f
        .type    f,@function
f:
        pushl   %ebp
        movl    %esp, %ebp
        movl    8(%ebp), %eax
        addl    $4, %eax
        andl    $7, %eax
        popl    %ebp
        ret
.Lfe1:
        .size    f,.Lfe1-f
        .align 4
.globl g
        .type    g,@function
g:
        pushl   %ebp
        movl    %esp, %ebp
        movl    8(%ebp), %eax
        addl    $4, %eax
        andl    $7, %eax
        popl    %ebp
        ret
.Lfe2:
        .size    g,.Lfe2-g
                .section        .rodata
.LC0:
        .string "%d %d %d\n"
.text
        .align 4
.globl main
        .type    main,@function
main:
        pushl   %ebp
        movl    %esp, %ebp
        pushl   %ebx
        pushl   %eax
        xorl    %ebx, %ebx
        .p2align 2
.L23:
        subl    $12, %esp
        pushl   %ebx
        call    g
        addl    $16, %esp
        pushl   %eax
        subl    $8, %esp
        pushl   %ebx
        call    f
        addl    $12, %esp
        pushl   %eax
        pushl   %ebx
        pushl   $.LC0
        incl    %ebx
        call    printf
        addl    $16, %esp
        cmpl    $7, %ebx
        jle     .L23
        xorl    %eax, %eax
        movl    -4(%ebp), %ebx
        leave
        ret
.Lfe3:
        .size    main,.Lfe3-main
        .ident  "GCC: (GNU) 2.96 20000731 (Red Hat Linux 7.0)"
$

If the unsigned is removed f() does an extra check for signedness but
no div:

        pushl   %ebp
        movl    %esp, %ebp
        movl    8(%ebp), %ecx
        leal    4(%ecx), %eax
        testl   %eax, %eax
        movl    %eax, %edx
        jns     .L18
        leal    11(%ecx), %edx
.L18:
        andl    $-8, %edx
        subl    %edx, %eax
        popl    %ebp

Can people now please pay attention to a real perfomance problem: gui
updating.

        Raimar

-- 
 email: rf13@xxxxxxxxxxxxxxxxx
 "How about the new language C&? No, that's not 'c ampersand', 'c reference', 
  'reference to c' or 'c and'. It's pronounced 'campersand', to confuse the 
  hell out of people who are unfamiliar with it, and it will, of course, 
  have no pointers."
    -- Xazziri in comp.lang.c++ about C#


[Prev in Thread] Current Thread [Next in Thread]