From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 5EF2FBC8B for ; Sat, 12 Feb 2005 16:38:52 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j1CFcpsq025002 for ; Sat, 12 Feb 2005 16:38:52 +0100 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id QAA27277 for ; Sat, 12 Feb 2005 16:38:51 +0100 (MET) Received: from herd.plethora.net (herd.plethora.net [205.166.146.1]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j1CFcnIK024999 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL) for ; Sat, 12 Feb 2005 16:38:51 +0100 Received: from bhurt.plethora.net (bhurt.plethora.net [205.166.146.49]) by herd.plethora.net (8.13.1/8.13.1) with ESMTP id j1CFcVZH001609 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sat, 12 Feb 2005 09:38:43 -0600 (CST) Date: Sat, 12 Feb 2005 09:42:10 -0600 (CST) From: Brian Hurt X-X-Sender: bhurt@localhost.localdomain To: "Marcin 'Qrczak' Kowalczyk" Cc: Ocaml Mailing List Subject: Re: [Caml-list] The boon of static type checking In-Reply-To: <874qgo8qno.fsf@qrnik.zagroda> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Miltered: at concorde with ID 420E230B.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 420E2309.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 wrote:01 compiler:01 foo:01 gcc:01 gcc:01 foo:01 ocamlopt:01 writes:01 checking:01 incorrect:02 compiling:02 div:97 output:02 brian:03 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: 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: 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