comp.lang.ada
 help / color / mirror / Atom feed
From: eachus@spectre.mitre.org (Robert I. Eachus)
Subject: Re: Ada 83 - avoiding unchecked conversions.
Date: 1996/11/27
Date: 1996-11-27T00:00:00+00:00	[thread overview]
Message-ID: <EACHUS.96Nov27185253@spectre.mitre.org> (raw)
In-Reply-To: 329C63BC.41C6@lmco.com


In article <329C63BC.41C6@lmco.com> Mary Cronk <mary.m.cronk@lmco.com> writes:

  > we have two 16 bit integers which we need to assemble into a single 32
  > bit integer (one is high order, the other low order).  We wish to avoid
  > unchecked conversion if we can.  Is there a standard accepted way of
  > doing this?

   I could ask why you want to avoid Unchecked_Conversion, since in
this case the message of the name is exactly what you apparently want,
a conversion that does no checking.

   But I won't ask.

   The best way to do this in Ada 95 is to have the low order word
declared as a modular type, and the upper word as a 16-bit integer:

  type Mod16 is mod 2**16;
  type Int16 is range -2**15..2**15-1;
  type Int32 is range -2**31..2**31-1;
  ...
  function Merge_Halves(High: Int16; Low: Mod16) return Int32 is
  begin return Int32(High) * 2**16 + Int32(Low); end Merge_Halves;

  If you are using Ada 83 and want portability, or you have the low
order bits in a signed number for some reason:

  function Merge_Halves(High, Low: Int16) return Int32 is
  begin
    if Low >= 0 then
       return Int32(High) * 2**16 + Int32(Low);
    else
       return Int32(High) * 2**16 + (2**16 + Int32(Low));
    end if;
  end Merge_Halves;

  You may be tempted to write:

       return Int32(High + 1) * 2**16 + Int32(Low);

  But that risks overflow, as does just omitting the parentheses.  A
VERY clever compiler might be able to figure out a way to simplify the
expression and still do no range checking, but I'll settle for
compilers that replace the multiply with a shift, and only do an
overflow check on the final add.
--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




  reply	other threads:[~1996-11-27  0:00 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-11-27  0:00 Ada 83 - avoiding unchecked conversions Ensco Vendor
1996-11-27  0:00 ` Robert I. Eachus [this message]
1996-11-29  0:00 ` Robert Dewar
1996-12-01  0:00   ` Darel Cullen
1996-11-30  0:00     ` Robert Dewar
1996-12-11  0:00     ` Richard Riehle
1996-12-02  0:00 ` Ted Dennison
1996-12-10  0:00   ` Matthew Heaney
     [not found] <md5:8B831999BCF200C6E70994BDF6CC529F>
1996-12-11  0:00 ` Chris Sparks (Mr. Ada)
1996-12-11  0:00   ` Dewi Daniels
1996-12-12  0:00     ` Richard Kenner
1996-12-17  0:00       ` Eric Miller
1996-12-18  0:00         ` Robert Dewar
1996-12-18  0:00           ` Robert A Duff
1996-12-19  0:00           ` Keith Thompson
1996-12-26  0:00             ` Robert Dewar
1996-12-11  0:00   ` Matthew Heaney
1996-12-12  0:00     ` Chris Brand
1996-12-13  0:00       ` Stephen Leake
1996-12-14  0:00         ` Robert A Duff
1996-12-14  0:00     ` BGaffney42
1996-12-19  0:00   ` Robert I. Eachus
replies disabled

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