Version française
Home     About     Download     Resources     Contact us    
Browse thread
Estimating the size of the ocaml community
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Brian Hurt <bhurt@s...>
Subject: Re: [Caml-list] The boon of static type checking
On Mon, 7 Feb 2005, Marcin 'Qrczak' Kowalczyk wrote:

> Brian Hurt <bhurt@spnz.org> writes:
> 
> > Hint: I've yet to meet a compiler that couldn't turn x/32 into 
> > x >> 5.
> 
> Actually this is incorrect when x is a signed type (and might be
> negative).

I've just compiled the code:
signed long foo(signed long x) {
    return x/32;
}

with gcc -O1 (gcc version 3.2.2, Redhat 9.0 default for the x86), and got 
the assembly language output:
foo:
        pushl   %ebp
        movl    %esp, %ebp
        movl    8(%ebp), %eax
        testl   %eax, %eax
        jns     .L2
        addl    $31, %eax
.L2:
        sarl    $5, %eax
        leave
        ret

Note the use of a sarl instruction, instead of a div instruction.

By the same logic, compiling:

let foo x = x / 32;;

under Ocamlopt (3.08.1, x86 on Redhat) we get the function:
camlEx2__foo_57:
.L100:
        sarl    $1, %eax
        testl   %eax, %eax
        jge     .L101
        addl    $31, %eax
.L101:  sarl    $5, %eax
        lea     1(%eax, %eax), %eax
        ret

While it's not technically a >> operator, a shift operator (as opposed to 
a divide) is being used.

Brian