From: Mats Weber <Mats.Weber@elca-matrix.ch>
Subject: Re: Byte/Bit Twiddling in Ada
Date: 1997/02/14
Date: 1997-02-14T00:00:00+00:00 [thread overview]
Message-ID: <33048844.7AEB@elca-matrix.ch> (raw)
In-Reply-To: 5dvfnn$olj@neocad.xilinx.com
> 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 = <your floating point type>'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.
next prev parent reply other threads:[~1997-02-14 0:00 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <5dvfnn$olj@neocad.xilinx.com>
1997-02-14 0:00 ` Byte/Bit Twiddling in Ada Matthew Heaney
1997-02-14 0:00 ` Mats Weber [this message]
1997-02-15 0:00 ` Robert Dewar
1997-02-15 0:00 ` Robert Dewar
1997-02-15 0:00 ` wiljan
1997-02-26 0:00 ` Robert Dewar
1997-02-14 0:00 ` Keith Allan Shillington
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox