comp.lang.ada
 help / color / mirror / Atom feed
From: Mark Johnson <mark_h_johnson@raytheon.com>
Subject: Re: Signed integer to modular type conversion
Date: Wed, 15 May 2002 09:01:05 -0500
Date: 2002-05-15T09:01:05-05:00	[thread overview]
Message-ID: <3CE26A21.3EC6F802@raytheon.com> (raw)
In-Reply-To: b4682ab7.0205141853.7e835874@posting.google.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

PS: I don't generally recommend this solution because many use it to
excess. Try...
  with Unchecked_Conversion;
  function "+" is new Unchecked_Conversion(Integer, Mod_Type);
Then your example assignment will be...
  Y := +X;
The abuse comes from using this method for all sorts of type
conversions, avoiding type safety, etc.



  parent reply	other threads:[~2002-05-15 14:01 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 [this message]
2002-05-16  0:02   ` Adam Beneschan
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