From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id PAA03945; Wed, 7 Jul 2004 15:07:26 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f 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 PAA03923; Wed, 7 Jul 2004 15:07:25 +0200 (MET DST) Received: from annexia.force9.co.uk (annexia.force9.co.uk [212.56.101.183]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i67D7ISH013355; Wed, 7 Jul 2004 15:07:23 +0200 Received: from rich by annexia.force9.co.uk with local (Exim 3.36 #1 (Debian)) id 1BiC8C-0006Jv-00; Wed, 07 Jul 2004 14:06:36 +0100 Date: Wed, 7 Jul 2004 14:06:35 +0100 To: Diego Olivier Fernandez Pons Cc: "Basile Starynkevitch [local]" , caml-list@inria.fr Subject: Re: [Caml-list] Does Caml have slow arithmetics ? Message-ID: <20040707130635.GA24113@redhat.com> References: <20040707091308.GA26172@bourg.inria.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.5.1+cvs20040105i From: Richard Jones X-Miltered: at concorde with ID 40EBF586.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 arithmetics:01 2004:99 pons:01 subsidiary:99 silently:01 ocamlopt:01 lea:99 ocamlopt:01 compiles:01 subl:01 rounding:01 compiles:01 $1,:01 decl:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Wed, Jul 07, 2004 at 02:32:56PM +0200, Diego Olivier Fernandez Pons wrote: > Subsidiary question : why LSB instead of MSB ? I thought it would be > simpler to let the computations silently overflow and correct when > necessary the tag bit. I don't know, but the current representation allows simplifications of some common mathematical operations. You don't need to worry about the tag bit at all. Addition (a + b) ---------------- let r1 = 2 * a + 1 # Actual representation of 'a' in a register let r2 = 2 * b + 1 r1 + r2 - 1 # Addition, proof follows: = 2 * a + 1 + 2 * b + 1 - 1 = 2 * a + 2 * b + 1 = 2 * (a + b) + 1 ocamlopt actually uses the effective address calculation unit to do this, so it's basically a single instruction: lea -1(%eax, %ebx), %eax Subtractions (a - b) -------------------- let r1 = 2 * a + 1 # Actual representation of 'a' in a register let r2 = 2 * b + 1 r1 - r2 + 1 # Subtraction, proof follows: = 2 * a + 1 - 2 * b + 1 + 1 = 2 * a - 2 * b + 1 = 2 * (a - b) + 1 ocamlopt compiles this to two instructions: subl %ebx, %eax incl %eax Multiplication (a * b) ---------------------- let r1 = 2 * a + 1 # Actual representation of 'a' in a register let r2 = 2 * b + 1 (r1 - 1) * (r2 / 2) + 1 = (2 * a + 1 - 1) * b + 1 # NB: r2 / 2 = b because of integer rounding = 2 * a * b + 1 = 2 * (a * b) + 1 ocamlopt compiles this into four instructions: sarl $1, %ebx decl %eax imull %ebx, %eax incl %eax Rich. -- Richard Jones. http://www.annexia.org/ http://www.j-london.com/ Merjis Ltd. http://www.merjis.com/ - improving website return on investment MONOLITH is an advanced framework for writing web applications in C, easier than using Perl & Java, much faster and smaller, reusable widget-based arch, database-backed, discussion, chat, calendaring: http://www.annexia.org/freeware/monolith/ ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners