comp.lang.ada
 help / color / mirror / Atom feed
From: adam@irvine.com (Adam Beneschan)
Subject: Re: Signed integer to modular type conversion
Date: 15 May 2002 17:02:01 -0700
Date: 2002-05-16T00:02:01+00:00	[thread overview]
Message-ID: <b4682ab7.0205151602.3c564c9e@posting.google.com> (raw)
In-Reply-To: 3CE26A21.3EC6F802@raytheon.com

Mark Johnson <mark_h_johnson@raytheon.com> wrote in message news:<3CE26A21.3EC6F802@raytheon.com>...
> Adam Beneschan wrote:
> > 
> > Supposing Standard.Integer is a 32-bit signed integer type.  I have a
> > modular type
> > 
> >     type ModType is mod 2**32;
> > 
> >     X : Integer;
> >     Y : ModType;
> > 
> > I'd like to set Y := X mod 2**32 (which should essentially just treat
> > X as an unsigned integer without changing the data).

> A reasonably efficient way to do this is Unchecked_Conversion. This for
> types of the same size (as in this case), it will copy the bytes from
> the source to destination. As you noted in the rest of your message, the
> direct type conversion doesn't do what you want and the arithmetic is an
> inefficient method. There are a lot of reasons not to use
> Unchecked_Conversion, but this doesn't appear to be one of them.
>   --Mark

OK, let me try a variation, and see if you still think
Unchecked_Conversion an appropriate way (or the best way) to handle
it.

I have two integer values.  One is in the range 0 .. 2**32-1, and the
other is in the range -2**31 .. 2**31-1.  I want to find the
mathematical sum of these two integers.  Let's assume that I expect
the result to be in the range 0..2**32-1, and am not worried about
what happens if it isn't.

    type ModType is mod 2**32;
    X : ModType;
    Y : Integer;
    Z : ModType;

I want to compute Z := X + Y, but of course I can't write this.  Z :=
X + ModType(Y) won't work if Y is negative; this is what led me to ask
the previous question, since I was trying to find a way to convert Y
to a ModType.

Again, I believe this is something that can be computed with one
machine instruction, so I'd like to avoid time-wasters like

    if Y < 0 then
	Z := X - ModType(-Y);
    else
	Z := X + ModType(Y);
    end if;

Would you still use something like Z := X + ToModType(Y) [where
ToModType is an instance of Unchecked_Conversion]?

				-- Adam



  reply	other threads:[~2002-05-16  0:02 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-05-15  2:53 Signed integer to modular type conversion Adam Beneschan
2002-05-15  3:34 ` Jim Rogers
2002-05-15 14:01 ` Mark Johnson
2002-05-16  0:02   ` Adam Beneschan [this message]
2002-05-16  2:26     ` Steven Deller
2002-05-16  9:53     ` Robert Dewar
2002-05-16 13:46       ` Steve Doiel
2002-05-16 14:00       ` Mark Johnson
2002-05-16 15:52       ` Adam Beneschan
2002-05-17  2:26         ` Robert Dewar
2002-05-17 16:44           ` Frank J. Lhota
2002-05-16 13:55     ` Mark Johnson
2002-05-15 19:12 ` Robert Dewar
2002-05-15 20:28 ` Adam Beneschan
  -- strict thread matches above, loose matches on Subject: below --
2002-05-15  5:43 Grein, Christoph
2002-05-16  4:39 Grein, Christoph
2002-05-16 14:13 ` Mark Johnson
replies disabled

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