comp.lang.ada
 help / color / mirror / Atom feed
From: Samuel Mize <smize@imagin.net>
Subject: Re: Question for the folks who designed Ada95
Date: 1999/04/27
Date: 1999-04-27T00:00:00+00:00	[thread overview]
Message-ID: <7g4jqm$1rkg@news1.newsguy.com> (raw)
In-Reply-To: 7g2qu4$ca4$1@usenet.rational.com

Corey Ashford <yeroca@rocketmail.com> wrote:
> 
> About modular types.
...
> Why didn't Ada95 make a shift operator a first-class
> operator on modular types.
...
> The problem with the interfaces package shift
> operators is that they have fixed types.  So it's
> difficult to write code for types whose size can vary.
...
> Any thoughts as to why the shift operator wasn't given
> a better treatment?

I'm not one of "the folks who designed Ada95" but I have a guess.

I expect it was because shifting is usually a low-level thing to do,
which comes up in interfaces to the machine.  So, the concern was to
make it available for the types that have direct machine support on a
given architecture.  Nobody cared about other moduluses (modulii?).

Also, bear in mind that modular types don't have to have a power-of-2
modulus.  Consider a mod 5 type.  If its value before shifting is
three, its lowest four bits are:

  0011

Shift left 1, and its bits are:

  0110

which is 6, which is out of range for the type.  Should we get a
constraint error, or should it saturate to 5, or should it wrap to 6
mod 5 (0001)?  The last makes the most sense, in terms of modular
numbers, and would do what you want for a power-of-two modulus.  But
it destroys the meaning of shifting.

Any of the above adds complexity -- you're no longer doing an
efficient shift operation, but shifting and checking constraints.  Of
course, you could expect a compiler to elide all this in the case of a
power-of-two modulus.  But you can't test for that, so you can't make
it a requirement.  It's reasonable behavior to expect, but so is
implementing division by a constant power of two as a simple shift.


> Or am I all wet, and there's really a good way to do shifts?

For a non-hardware-supported power-of-two-modulus unsigned type, you
will need to do a shift, then mask the result down under your modulus.
You can do this with a type from Interfaces that has more bits than
your type.  For instance:

  with Interfaces; use Interfaces;
  ...
  type My_Type is mod 2 ** 30;
  My_Var: My_Type;
  ...
  My_Var := My_Type (Shift_Left (Unsigned_32 (My_Var), 3) mod 2 ** 30);

The conversion to Unsigned_32 should take no cycles, it's implicit
in reading the value into a register to work on it.

The mod 2**30 has to be done anyway, as I described above, since the
machine doesn't directly support a mod 2**30 type (or it would be in
Interfaces).  I'd expect a mod operation using a static power of two
to be implemented as a simple bit-mask operation.

I wouldn't expect the conversion to My_Type to do a range check, since
the compiler can tell statically that the value can't be out of range.
If you're extremely exercised about CPU time you'll be compiling with
range-checks off anyway.

So this should be as efficient as possible.

Best,
Sam Mize

-- 
Samuel Mize -- smize@imagin.net (home email) -- Team Ada
Fight Spam: see http://www.cauce.org/ \\\ Smert Spamonam




  parent reply	other threads:[~1999-04-27  0:00 UTC|newest]

Thread overview: 88+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-04-26  0:00 Question for the folks who designed Ada95 Corey Ashford
1999-04-27  0:00 ` Matthew Heaney
1999-04-27  0:00   ` Matthew Heaney
1999-04-27  0:00   ` Corey Ashford
1999-04-28  0:00     ` Robert A Duff
1999-04-28  0:00       ` Keith Thompson
1999-04-29  0:00         ` Robert A Duff
1999-04-28  0:00     ` Marin David Condic
1999-04-27  0:00 ` Samuel Mize [this message]
1999-04-27  0:00   ` Samuel Mize
1999-04-27  0:00     ` Robert Dewar
1999-04-28  0:00       ` Samuel Mize
1999-04-28  0:00         ` Samuel Mize
1999-04-28  0:00       ` Corey Ashford
1999-04-28  0:00         ` Robert Dewar
1999-04-29  0:00           ` Corey Ashford
1999-04-29  0:00             ` Robert Dewar
1999-04-29  0:00               ` Fraser Wilson
1999-04-29  0:00                 ` Marin David Condic
1999-04-29  0:00                 ` Robert Dewar
1999-04-28  0:00     ` Corey Ashford
1999-04-28  0:00     ` Corey Ashford
1999-04-27  0:00 ` Robert Dewar
1999-04-27  0:00   ` Corey Ashford
1999-04-27  0:00   ` Tarjei Tj�stheim Jensen
1999-04-27  0:00     ` bill
1999-04-27  0:00       ` dennison
1999-04-27  0:00         ` Robert Dewar
1999-04-29  0:00           ` Robert A Duff
1999-04-27  0:00       ` Robert Dewar
1999-04-27  0:00         ` Al Christians
1999-04-27  0:00           ` Larry Kilgallen
1999-04-27  0:00             ` Al Christians
1999-04-28  0:00         ` Robert A Duff
1999-04-28  0:00           ` Robert Dewar
1999-04-29  0:00             ` Matthew Heaney
1999-04-29  0:00               ` Jean-Pierre Rosen
1999-04-29  0:00               ` Robert Dewar
1999-04-27  0:00     ` Robert Dewar
1999-04-28  0:00       ` Corey Ashford
1999-04-28  0:00         ` Robert Dewar
1999-04-28  0:00           ` Robert A Duff
1999-04-28  0:00             ` Robert Dewar
1999-04-29  0:00               ` Robert A Duff
1999-04-29  0:00           ` Corey Ashford
1999-04-29  0:00             ` Robert Dewar
1999-04-29  0:00               ` Geert Bosch
1999-04-29  0:00               ` Fraser Wilson
1999-04-29  0:00                 ` Tucker Taft
1999-04-29  0:00                 ` Mark A Biggar
1999-04-30  0:00                   ` Tucker Taft
1999-04-29  0:00                 ` Marin David Condic
1999-04-30  0:00                 ` David Brown
1999-04-29  0:00             ` Corey Ashford
1999-04-27  0:00     ` David Starner
1999-04-27  0:00       ` Robert Dewar
1999-04-27  0:00         ` David Starner
1999-04-28  0:00           ` Robert Dewar
1999-04-28  0:00           ` Samuel Mize
1999-04-28  0:00         ` Tarjei Tj�stheim Jensen
1999-04-28  0:00           ` Robert A Duff
1999-04-28  0:00           ` Larry Kilgallen
1999-04-28  0:00           ` dennison
1999-04-28  0:00             ` Robert Dewar
1999-04-28  0:00               ` Robert A Duff
1999-04-28  0:00                 ` Brian Hanson
1999-04-28  0:00                   ` bill
1999-04-29  0:00                     ` Robert Dewar
1999-04-28  0:00                       ` Pat Rogers
1999-04-29  0:00                         ` Robert Dewar
1999-04-29  0:00                           ` dennison
1999-04-29  0:00                             ` Pat Rogers
1999-04-29  0:00                     ` Robert S. White
1999-04-29  0:00                       ` Robert Dewar
1999-04-30  0:00                         ` Robert S. White
1999-04-29  0:00                       ` Robert Dewar
1999-04-30  0:00                         ` Robert S. White
1999-04-30  0:00                           ` dennison
1999-04-30  0:00                 ` Robert Dewar
1999-04-28  0:00           ` Robert Dewar
1999-04-28  0:00             ` Robert A Duff
1999-04-29  0:00             ` Tarjei Tj�stheim Jensen
1999-04-29  0:00             ` Bill Ghrist
1999-04-29  0:00           ` Question for the folks who designed Ad David Kristola
1999-04-29  0:00             ` Paul Duquennoy
1999-04-27  0:00     ` Question for the folks who designed Ada95 Larry Kilgallen
1999-04-29  0:00     ` Aidan Skinner
1999-04-30  0:00       ` Matthew Heaney
replies disabled

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