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,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,ASCII-7-bit Received: by 10.180.107.167 with SMTP id hd7mr558768wib.0.1348945030804; Sat, 29 Sep 2012 11:57:10 -0700 (PDT) Path: q10ni24079333wif.0!nntp.google.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Bill Findlay Newsgroups: comp.lang.ada Subject: Re: highest bit, statically determined Date: Sat, 29 Sep 2012 19:57:08 +0100 Message-ID: References: <50673111$0$9505$9b4e6d93@newsspool1.arcor-online.net> Mime-Version: 1.0 X-Trace: individual.net 7YY7OXFT6Og0s+QT+Qo2RwoVEbJnPmpYSvN1kAia4iiYq7Mt1C+qTcU3Z27WDmm7bZ Cancel-Lock: sha1:hBHLNi0Rq2oQjJskijtgV1cSY7o= User-Agent: Microsoft-Entourage/12.33.0.120411 Thread-Topic: highest bit, statically determined Thread-Index: Ac2edDlmWZQw/7/nukOgrqFPIITRBg== Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit Date: 2012-09-29T19:57:08+01:00 List-Id: 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 = 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. -- Bill Findlay with blueyonder.co.uk; use surname & forename;