From: Bill Findlay <yaldnif.w@blueyonder.co.uk>
Subject: Re: highest bit, statically determined
Date: Sun, 04 Nov 2012 22:00:52 +0000
Date: 2012-11-04T22:00:52+00:00 [thread overview]
Message-ID: <CCBC9614.2090C%yaldnif.w@blueyonder.co.uk> (raw)
In-Reply-To: op.wm9nx2i3ule2fv@cardamome
On 04/11/2012 20:45, in article op.wm9nx2i3ule2fv@cardamome, "Yannick
Duch�ne (Hibou57)" <yannick_duchene@yahoo.fr> wrote:
> Le Sat, 29 Sep 2012 21:16:01 +0200, Bill Findlay
> <yaldnif.w@blueyonder.co.uk> a �crit:
>
>>
>> On 29/09/2012 19:57, in article
>> CC8D0314.1E2CC%yaldnif.w@blueyonder.co.uk,
>> "Bill Findlay" <yaldnif.w@blueyonder.co.uk> wrote:
>>
>>> On 29/09/2012 18:34, in article
>>> 50673111$0$9505$9b4e6d93@newsspool1.arcor-online.net, "Georg Bauhaus"
>>> <rm.dash-bauhaus@futureapps.de> wrote:
>>>
>>>> Is there a shorter/better way of having the compiler
>>>> find the highest bit = 1 in a static numeric constant?
>>>>
>>>> If N is such a constant, e.g. Some_Type'Last where
>>>> Some_Type'Size = 8 and its bound are static, then
>>>>
>>>> Highest_Bit_In_Octet : constant :=
>>>> 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 less
>>> efficient than one would hope, and more obvious code works faster.
>>>
>>> Something like the following can be readily extended to greater operand
>>> widths:
>>>
>>> function first_1_bit (y : octet)
>>> return Natural is
>>> x : octet;
>>> r : Natural;
>>> begin
>>> if y = 0 then return 0; end if;
>>>
>>> if (y / 16) /= 0 then
>>> r := 4; x := y / 16;
>>> else
>>> r := 0; x := y;
>>> end if;
>>>
>>> if (x / 4) /= 0 then
>>> r := r + 2; x := x / 4;
>>> end if;
>>>
>>> if (x / 2) /= 0 then
>>> r := 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 fold,
>> 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?
Compile-time evaluation, so that the end result is a compile-time known
(I do not say static, as that is a term of art in Ada) value.
--
Bill Findlay
with blueyonder.co.uk;
use surname & forename;
next prev parent reply other threads:[~2012-11-04 22:00 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-09-29 17:34 highest bit, statically determined Georg Bauhaus
2012-09-29 18:11 ` Pascal Obry
2012-09-29 18:59 ` Georg Bauhaus
2012-09-29 19:18 ` Georg Bauhaus
2012-09-29 18:57 ` Bill Findlay
2012-09-29 19:16 ` Bill Findlay
2012-09-29 21:36 ` Georg Bauhaus
2012-09-29 22:06 ` Georg Bauhaus
2012-09-29 23:38 ` Bill Findlay
2012-09-30 15:01 ` Vadim Godunko
2012-11-04 20:45 ` Yannick Duchêne (Hibou57)
2012-11-04 22:00 ` Bill Findlay [this message]
2012-09-30 15:39 ` Anatoly Chernyshev
2012-09-30 18:36 ` Shark8
2012-10-01 8:07 ` Georg Bauhaus
2012-10-01 8:11 ` Georg Bauhaus
2012-10-01 8:52 ` Anatoly Chernyshev
2012-10-01 21:30 ` Georg Bauhaus
2012-10-01 22:55 ` Shark8
2012-10-01 23:25 ` Georg Bauhaus
2012-10-02 11:03 ` Brian Drummond
2012-10-03 9:30 ` kalvink65
2012-10-03 18:54 ` Georg Bauhaus
2012-10-04 7:46 ` Georg Bauhaus
2012-10-04 8:25 ` Stephen Leake
2012-10-04 10:01 ` kalvin.news
2012-10-05 7:50 ` Anatoly Chernyshev
2012-10-05 8:38 ` Anatoly Chernyshev
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox