Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: 2005-02-12 (15:38)
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 <> 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:
        pushl   %ebp
        movl    %esp, %ebp
        movl    8(%ebp), %eax
        testl   %eax, %eax
        jns     .L2
        addl    $31, %eax
        sarl    $5, %eax

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:
        sarl    $1, %eax
        testl   %eax, %eax
        jge     .L101
        addl    $31, %eax
.L101:  sarl    $5, %eax
        lea     1(%eax, %eax), %eax

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