From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p4QFpAqO006595 for ; Thu, 26 May 2011 17:51:10 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AkAHAIR23k1ii1tkemdsb2JhbAA/AwEShEmTLDGOHgEBCwkNBRQFI6gGjiM8gjeEQIkbAQQFAYElg2qBBwSGYY4RJopR X-IronPort-AV: E=Sophos;i="4.65,273,1304287200"; d="scan'208";a="84108655" Received: from nm30.bullet.mail.sp2.yahoo.com ([98.139.91.100]) by mail3-smtp-sop.national.inria.fr with SMTP; 26 May 2011 17:51:06 +0200 Received: from [98.139.91.66] by nm30.bullet.mail.sp2.yahoo.com with NNFMP; 26 May 2011 15:51:05 -0000 Received: from [98.139.91.55] by tm6.bullet.mail.sp2.yahoo.com with NNFMP; 26 May 2011 15:51:05 -0000 Received: from [127.0.0.1] by omp1055.mail.sp2.yahoo.com with NNFMP; 26 May 2011 15:51:05 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 251594.80832.bm@omp1055.mail.sp2.yahoo.com Received: (qmail 22599 invoked by uid 60001); 26 May 2011 15:51:04 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1306425064; bh=D0ShhpBzM69e+I37MVWz2uBI6b6I/UheigIF/9EmxEY=; h=Message-ID:X-YMail-OSG:Received:X-Mailer:Date:From:Subject:To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=kv3pdVplooehmpRH+pl0qwjtxGXODs50RoBnjNUBHOEXlIDgzFrDmJFRfjbMLhxUycAwMf2VYhmVwJBXTf4kkmdDwDXI21ql+avpd3AAxbh3HJ/9mqtTyccGOWnYjWDfixqSazCN9XrAi3SH96EgY+bhu3GK6ACUFsI3x0SAauw= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:X-YMail-OSG:Received:X-Mailer:Date:From:Subject:To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=KHOB3kvUlpXSwf8OX3FTaDMwriGHkAfEh7dXNyCRdJCMz8aIyiglqmhL+ZjpvEVNgcYbWFuvra61N/MV4dC94AMCaPUB6v/SbKLGki41J6LKGZ4GfDnZtNFxUgQFnvl/DA639iRGZan9y8jy1BdojveFM8NffJE+ZzQDDeVvfKo=; Message-ID: <707567.2845.qm@web111505.mail.gq1.yahoo.com> X-YMail-OSG: PwResYEVM1m8dHhcqgdcWnNgwjq_KtlO_GB7KaUvIpBS7Q1 A6ufkFBDtBh_2pTicXuqI3NlBJuoT0uzvP8QnELOeb1KE.Ys2CpnBHiIqNhq k1H0i049XF_gVvXtih8mvcR8cd1IYPLS5s8qqhqXGit0.Xw6Z7wquOjMx2NM uLU7tTDuvYKrrnMbOT43Ud6lflihWuhRdzcCzDFR8Lsf5wLuLLqnXPlYqTCW wbnGMuS2Azosd6lHSUcoy98rlmwpoBqqpBBGYL9Dd_2sb8r2rPD4eFdcIp2. 7duUB2B75uWMBsZwN7Vf9Q7ZRO2c8FrC5yRrs6RJdSbanGGGH3Gawu9ikhO6 XkKvsZ7e0WDjpp8KGHlc6nnVqYH16_QiUmUmV9bbSeDkC.yeTw02bo9LmlDi kmBsYC8VnBdSKrA-- Received: from [213.205.70.209] by web111505.mail.gq1.yahoo.com via HTTP; Thu, 26 May 2011 08:51:04 PDT X-Mailer: YahooMailClassic/14.0.1 YahooMailWebService/0.8.111.303096 Date: Thu, 26 May 2011 08:51:04 -0700 (PDT) From: Dario Teixeira To: caml-list@inria.fr MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by walapai.inria.fr id p4QFpAqO006595 Subject: [Caml-list] Binary logarithm of a power of 2 Hi, In the critical path of an application I need to compute the binary logarithm of an int32. With one nuance: I know the input is always a power of 2, which opens the door to bit-trickery (the log2 of a power of 2 is simply the position of the sole bit set to '1'). GCC has a builtin function __builtin_ctz which I suppose will translate into optimal assembly for all platforms, so my current solution uses a small C stub that basically just invokes this builtin (see below). Still, my question is whether this is indeed the optimal solution. Is there some penalty associated with invoking C functions that may negate the supposed advantage of __builtin_ctz? Thanks in advance! Best regards, Dario Teixeira external logbin: int32 -> int = "logbin_stub" CAMLprim value logbin_stub (value v_num) { CAMLparam1 (v_num); CAMLlocal1 (ml_res); int32 num = Int32_val (v_num); int res = __builtin_ctz (num); ml_res = Val_int (res); CAMLreturn (ml_res); }