comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Interfaces.Shift_Left
Date: Wed, 14 Dec 2011 16:49:15 -0600
Date: 2011-12-14T16:49:15-06:00	[thread overview]
Message-ID: <jcb95d$j72$1@munin.nbi.dk> (raw)
In-Reply-To: a76daa61-49f7-4235-af65-11973a012f35@v29g2000yqv.googlegroups.com

"awdorrin" <awdorrin@gmail.com> wrote in message 
news:a76daa61-49f7-4235-af65-11973a012f35@v29g2000yqv.googlegroups.com...
>I understand what you are saying and I do agree in theory.
>
> The situation is that bit shifting is being used to retrieve data of
> various bit lengths and positions from compound records.
>
> For instance, retrieving a 6-bit signed value from a 32-bit field.

Right. This definitely should be done using record representation clauses if 
there is a choice. But of course if the code already exists, that might be 
too much.

> For the purposes of obtaining those 6 bits - sign is irrelevant, but
> that 6 bit value is then sent back to a calling process as an 'Int32'
> so I need the sign then.

Right. A record representation clause would do this correctly with no 
(visible) shifting, and it would be far less prone to errors. To take a 
quick example from my solitare solvers:

    Max_Score : constant := 1000;
    type Suits is (Diamonds, Hearts, Spades, Clubs);
    subtype Pip_Value is Natural range 1 .. 13; -- 1 = Ace, 13 = King. (I 
have names for these not shown here.)
    subtype Score_Value is Integer range -Max_Score..Max_Score;

    type Card is record
         Suit : Suits;
         Pip  : Pip_Value;
         Score : Score_Value;
    end record;
    for Card use record
         Suit at 0 range 14..15;
         Pip at 0 range 10..13;
         Score at 0 range 0..9;
    end record;
    for Card'Size use 16;

    Great_Card : constant Card := (Suit => Spades, Pip => 1, Score => 800);

Now, a reference to Great_Card.Score will give you a signed result, and the 
compiler will select the best set of shifts and masks for your target 
machine (not to mention combining them when possible). Much less 
complication, much easier to read, much less likely to make a mistake.

> Given more time and funding, I would replace this logic with something
> that makes more sense, but a quick Unchecked_Conversion saves
> rewriting 15,000+ lines of code. :)

I certainly understand, but personally I would guess that replacing the 
explicit shifting and masking with a record type would probably take me 
about the same time as writing all of the Unchecked_Conversion instances you 
would need. And there will be a lot less chance of error. YMMV, of course.

BTW, if there are really 15000 lines that would get changed by this, there 
is a really bad lack of encapsulation with this code. The interfaces between 
Ada and other languages/hardware/OSes should always be made as narrow as 
possible, and always should go through a well-encapulated interface (so it 
is easy to change one side without distrubing the other). But I realize that 
you probably know this.

                                                Randy.





  reply	other threads:[~2011-12-14 22:49 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-12 22:30 Interfaces.Shift_Left awdorrin
2011-12-12 23:34 ` Interfaces.Shift_Left Simon Wright
2011-12-13  1:36   ` Interfaces.Shift_Left Adam Beneschan
2011-12-13 12:00 ` Interfaces.Shift_Left Brian Drummond
2011-12-13 13:15   ` Interfaces.Shift_Left awdorrin
2011-12-13 21:48     ` Interfaces.Shift_Left Randy Brukardt
2011-12-14 18:28       ` Interfaces.Shift_Left awdorrin
2011-12-14 22:49         ` Randy Brukardt [this message]
2011-12-15  9:51           ` Interfaces.Shift_Left Niklas Holsti
2011-12-16  0:23             ` Interfaces.Shift_Left Randy Brukardt
2011-12-18 20:47               ` Interfaces.Shift_Left Niklas Holsti
2011-12-20  0:38                 ` Interfaces.Shift_Left Randy Brukardt
2011-12-20  2:18                   ` Interfaces.Shift_Left Shark8
2011-12-20 10:08                   ` Interfaces.Shift_Left Dmitry A. Kazakov
2011-12-20 19:38                   ` Interfaces.Shift_Left Niklas Holsti
2011-12-20 20:46                     ` Interfaces.Shift_Left Niklas Holsti
2011-12-20 21:13                       ` Interfaces.Shift_Left Simon Wright
2011-12-20 21:08                     ` Interfaces.Shift_Left Simon Wright
2011-12-20 23:26                       ` Interfaces.Shift_Left Randy Brukardt
2011-12-20 23:36                     ` Interfaces.Shift_Left Randy Brukardt
2011-12-21  0:44                       ` Interfaces.Shift_Left Georg Bauhaus
2011-12-21  7:23                       ` Interfaces.Shift_Left AdaMagica
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox