From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM, LOTS_OF_MONEY autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,7e15102eb14c0020 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,UTF8 Received: by 10.66.81.98 with SMTP id z2mr2835298pax.19.1352061933777; Sun, 04 Nov 2012 12:45:33 -0800 (PST) Path: s9ni80266pbb.0!nntp.google.com!news.glorb.com!aioe.org!.POSTED!not-for-mail From: =?utf-8?Q?Yannick_Duch=C3=AAne_=28Hibou57?= =?utf-8?Q?=29?= Newsgroups: comp.lang.ada Subject: Re: highest bit, statically determined Date: Sun, 04 Nov 2012 21:45:28 +0100 Organization: Ada @ Home Message-ID: References: <50673111$0$9505$9b4e6d93@newsspool1.arcor-online.net> NNTP-Posting-Host: UWDghlkk/YmM3BwNWrM/Kw.user.speranza.aioe.org Mime-Version: 1.0 X-Complaints-To: abuse@aioe.org User-Agent: Opera Mail/12.02 (Linux) X-Notice: Filtered by postfilter v. 0.8.2 Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes Content-Transfer-Encoding: Quoted-Printable Date: 2012-11-04T21:45:28+01:00 List-Id: Le Sat, 29 Sep 2012 21:16:01 +0200, Bill Findlay = a =C3=A9crit: > > On 29/09/2012 19:57, in article = > CC8D0314.1E2CC%yaldnif.w@blueyonder.co.uk, > "Bill Findlay" wrote: > >> On 29/09/2012 18:34, in article >> 50673111$0$9505$9b4e6d93@newsspool1.arcor-online.net, "Georg Bauhaus"= >> wrote: >> >>> Is there a shorter/better way of having the compiler >>> find the highest bit =3D 1 in a static numeric constant? >>> >>> If N is such a constant, e.g. Some_Type'Last where >>> Some_Type'Size =3D 8 and its bound are static, then >>> >>> Highest_Bit_In_Octet : constant :=3D >>> Natural'Max >>> (8 * Boolean'Pos (N / 2**7 > 0), >>> Natural'Max >>> (7 * Boolean'Pos (N / 2**6 > 0), >>> Natural'Max >>> (6 * Boolean'Pos (N / 2**5 > 0), >>> Natural'Max >>> (5 * Boolean'Pos (N / 2**4 > 0), >>> Natural'Max >>> (4 * Boolean'Pos (N / 2**3 > 0), >>> Natural'Max >>> (3 * Boolean'Pos (N / 2**2 > 0), >>> Natural'Max >>> (2 * Boolean'Pos (N / 2**1 > 0), >>> Natural'Max >>> (1 * Boolean'Pos (N / 2**0 > 0), 0)))))))); >> >> In my experience that sort of code, applied in non-static cases, is l= ess >> efficient than one would hope, and more obvious code works faster. >> >> Something like the following can be readily extended to greater opera= nd >> widths: >> >> function first_1_bit (y : octet) >> return Natural is >> x : octet; >> r : Natural; >> begin >> if y =3D 0 then return 0; end if; >> >> if (y / 16) /=3D 0 then >> r :=3D 4; x :=3D y / 16; >> else >> r :=3D 0; x :=3D y; >> end if; >> >> if (x / 4) /=3D 0 then >> r :=3D r + 2; x :=3D x / 4; >> end if; >> >> if (x / 2) /=3D 0 then >> r :=3D r + 1; >> end if; >> >> return r + 1; >> end first_1_bit; >> >> It looks fairly inline-able, and foldable for a static value of y. > > I can now confirm that with GNAT GPL 2012 at -O3 it does inline and fo= ld, > but I now see that you want the result to be static as well as the = > operand, > and this does not achieve that. > What means folding in this context? -- = =E2=80=9CSyntactic sugar causes cancer of the semi-colons.=E2=80=9D [1] =E2=80=9CStructured Programming supports the law of the excluded muddle.= =E2=80=9D [1] [1]: Epigrams on Programming =E2=80=94 Alan J. =E2=80=94 P. Yale Univers= ity