comp.lang.ada
 help / color / mirror / Atom feed
* Re: 'Digit in generic package
@ 1996-08-26  0:00 W. Wesley Groleau (Wes)
  1996-08-27  0:00 ` Robert I. Eachus
  1996-08-27  0:00 ` Ken Garlington
  0 siblings, 2 replies; 4+ messages in thread
From: W. Wesley Groleau (Wes) @ 1996-08-26  0:00 UTC (permalink / raw)



R. A. O'Keefe asked
[how to make a generic use the most precise type out of its actuals]

R. Eachus answered

  [Write the generic body to dispatch at run-time]

At least one vendor supplied a generic math package that did exactly
that AND on examining the disassembled code for an instantiation, we
found that the compiler had looked at the parameters and eliminated
the "dead code" so that the effect was exactly what Mr. O'Keefe had
asked for.

Now I wonder whether in Ada 95 it would work to simplify R. Eachus's
suggestion with

   ...
   function Generic_F ... is

      Precision : constant := Max(Foo'Digits,Bar'Digits,...);

   begin
      case Precision is ...

--
---------------------------------------------------------------------------
W. Wesley Groleau (Wes)                                Office: 219-429-4923
Hughes Defense Communications (MS 10-40)                 Home: 219-471-7206
Fort Wayne,  IN   46808                  (Unix): wwgrol@pseserv3.fw.hac.com
---------------------------------------------------------------------------




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: 'Digit in generic package
  1996-08-26  0:00 'Digit in generic package W. Wesley Groleau (Wes)
@ 1996-08-27  0:00 ` Robert I. Eachus
  1996-08-28  0:00   ` Robert Dewar
  1996-08-27  0:00 ` Ken Garlington
  1 sibling, 1 reply; 4+ messages in thread
From: Robert I. Eachus @ 1996-08-27  0:00 UTC (permalink / raw)



In article <9608261530.AA06035@most> "W. Wesley Groleau (Wes)" <wwgrol@PSESERV3.FW.HAC.COM> writes:

  > At least one vendor supplied a generic math package that did exactly
  > that AND on examining the disassembled code for an instantiation, we
  > found that the compiler had looked at the parameters and eliminated
  > the "dead code" so that the effect was exactly what Mr. O'Keefe had
  > asked for.

   This is what you should expect in some--but not all--cases.

  > Now I wonder whether in Ada 95 it would work to simplify R. Eachus's
  > suggestion with

  >   ...
  >   function Generic_F ... is

  >	 Precision : constant := Max(Foo'Digits,Bar'Digits,...);

  >    begin
  >      case Precision is ...

    Make that:

      Precision : constant := Integer'Max(Foo'Digits,Bar'Digits);

   ...and the compiler should accept it if, and only if, all (sub)type
names are static.  This requires both static bounds, an that the type
not be a generic formal parameter.  (The initialization expression for
Precision must be static both in the generic and the instance.) If you
say instead:

      Precision: constant Integer := Integer'Max(Foo'Digits,Bar'Digits);

   then Precision can be static in the instance even though it is not
static in the generic template.  This is exactly the behavior
commented on above.  And if an instance is such that some code can be
statically eliminated, most compilers will do so.  (There is at least
one compiler on the market which uses a generic model which prevents
this, but that is an accepted trade-off.  By not doing any generic
instantiations at compile time, the resulting code size is smaller.)


					Robert I. Eachus

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

					Robert I. Eachus

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




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: 'Digit in generic package
  1996-08-26  0:00 'Digit in generic package W. Wesley Groleau (Wes)
  1996-08-27  0:00 ` Robert I. Eachus
@ 1996-08-27  0:00 ` Ken Garlington
  1 sibling, 0 replies; 4+ messages in thread
From: Ken Garlington @ 1996-08-27  0:00 UTC (permalink / raw)



W. Wesley Groleau (Wes) wrote:
> 
> R. A. O'Keefe asked
> [how to make a generic use the most precise type out of its actuals]
> 
> R. Eachus answered
> 
>   [Write the generic body to dispatch at run-time]
> 
> At least one vendor supplied a generic math package that did exactly
> that AND on examining the disassembled code for an instantiation, we
> found that the compiler had looked at the parameters and eliminated
> the "dead code" so that the effect was exactly what Mr. O'Keefe had
> asked for.

If you have DEC Ada installed, you can look at the body of the generic 
SQRT routine in ADA$PREDEFINED:MATH_LIB.ADC and see:

function SQRT (A: REAL) return REAL is
begin
  if REAL'MACHINE_MANTISSA = SYSTEM.F_FLOAT'MACHINE_MANTISSA then
    return REAL(VAX_F.SQRT(F_FLOAT(A)));
  elsif REAL'MACHINE_MANTISSA = SYSTEM.D_FLOAT'MACHINE_MANTISSA then
    ...

Usually, this gets optimized to a simple call to the pre-defined VAX
SQRT routine of the appropriate precision.

-- 
LMTAS - "Our Brand Means Quality"




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: 'Digit in generic package
  1996-08-27  0:00 ` Robert I. Eachus
@ 1996-08-28  0:00   ` Robert Dewar
  0 siblings, 0 replies; 4+ messages in thread
From: Robert Dewar @ 1996-08-28  0:00 UTC (permalink / raw)



iRobert Eachs says

"   then Precision can be static in the instance even though it is not
static in the generic template.  This is exactly the behavior
commented on above.  And if an instance is such that some code can be
statically eliminated, most compilers will do so.  (There is at least
one compiler on the market which uses a generic model which prevents
this, but that is an accepted trade-off.  By not doing any generic
instantiations at compile time, the resulting code size is smaller.)
"

Whether a compiler can or cannot perform optimziations based on compile
time known constant values has nothing whatsoever to do with whether
these values are static. The set of static expressions changed in Ada 95,
the set of expressions whose value can be determined at compile time
did not change (except from the addition of new facilities), so I
find the discussion of staticness in instances to be completely 
irrelevant to this discussion.





^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1996-08-28  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-08-26  0:00 'Digit in generic package W. Wesley Groleau (Wes)
1996-08-27  0:00 ` Robert I. Eachus
1996-08-28  0:00   ` Robert Dewar
1996-08-27  0:00 ` Ken Garlington

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