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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,f584bf624aabe591 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-05-15 17:02:01 PST Path: archiver1.google.com!postnews1.google.com!not-for-mail From: adam@irvine.com (Adam Beneschan) Newsgroups: comp.lang.ada Subject: Re: Signed integer to modular type conversion Date: 15 May 2002 17:02:01 -0700 Organization: http://groups.google.com/ Message-ID: References: <3CE26A21.3EC6F802@raytheon.com> NNTP-Posting-Host: 66.126.103.122 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1021507321 831 127.0.0.1 (16 May 2002 00:02:01 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 16 May 2002 00:02:01 GMT Xref: archiver1.google.com comp.lang.ada:24137 Date: 2002-05-16T00:02:01+00:00 List-Id: Mark Johnson 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