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=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,53a810f13e8ba40 X-Google-Attributes: gid103376,public From: Mats Weber Subject: Re: Byte/Bit Twiddling in Ada Date: 1997/02/14 Message-ID: <33048844.7AEB@elca-matrix.ch>#1/1 X-Deja-AN: 218962768 References: <5dvfnn$olj@neocad.xilinx.com> Content-Type: text/plain; charset=us-ascii Organization: ELCA Matrix SA Mime-Version: 1.0 Reply-To: Mats.Weber@elca-matrix.ch Newsgroups: comp.lang.ada X-Mailer: Mozilla 3.01 (Macintosh; I; PPC) Date: 1997-02-14T00:00:00+00:00 List-Id: > Is there any portable way to do byte and bit manipulation in Ada, on a > long word? I found no mention of this topic in the FAQ. longword_bits : constant := 32; type longword is array (0 .. longword_bits - 1) of boolean; pramga pack(longword); for longword'size use longword_bits; then you can do any bit-twiddling on variables of type Longword, e.g. A, B : Longword; A(0 .. 15) := A(16 .. 31); -- copy low-order 16 bits -- into high order 16 bits A(0 .. 30) := A(1 .. 31); -- right shift A := A(31) & A(0 .. 30); -- rotate left You can use Unchecked_Conversion if you want to manipulate floating point values at the bit level (at your own risk). In this case, set Longword_Bits = 'Size; (Robert, what kind of code would GNAT generate for these instructions ? Does the optimizer recognize the CPU's instruction set ?) > My friend is trying to write some code that does byte swapping, > presumably to handle conversions to and from IEEE number formats. Another possibility is using representation clauses: type Mantissa is range -2 ** 22 .. 2 ** 22 - 1; type Exponent is range -2 ** 8 .. 2 ** 8 - 1; type Float_Rec is record Mant : Mantissa; Expo : Exponent; end record; pragma Pack(Float_Rec); for Float_Rec'Size use 32; for Float_Rec use record at mod 4; -- if you want them aligned Mant at 0 range 0 .. 22; Expo at 0 range 23 .. 31; end record; Note: I did this without looking at the reference of any floating point type, and I did not check the code with a compiler. But it should work once adapted.