comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: Quick inverse square root
Date: Sun, 3 Jan 2021 00:18:11 +0100	[thread overview]
Message-ID: <rsquvl$rfi$1@dont-email.me> (raw)
In-Reply-To: <c7131827-fb1c-49e0-bb39-7bd9522aebdan@googlegroups.com>

On 1/2/21 11:26 PM, Matt Borchers wrote:
> 
>      generic
>          type F is digits <>;
>          type U is mod <>;
>          magic : U;
>      function G_Q_INV_SQRT( a : F ) return F;
> 
> I write the body like this:
> 
>      function G_Q_INV_SQRT( a : F ) return F is
>          y : F := a;
>          i : U;
>          for i'Address use y'Address;
>      begin
>          i := magic - shift_right( i, 1 );
>          return y * (1.5 - (0.5 * a * y * y));
>      end G_Q_INV_SQRT;
> 
>      function QUICK_INVERSE_SQRT is
>          new G_Q_INV_SQRT( FLOAT, UNSIGNED_32, 16#5F3759DF# );
> 
> This won't compile because the type U is not valid for the call to "shift_right".  How do I overcome this obstacle?

Make it an explicit generic formal function parameter:

    with function Shift_Right (...) return ...;

> Once that is overcome, is there a way I can eliminate having to pass in the unsigned type along with the floating point type?  That seems like the programmer would require internal knowledge to make use of the generic.  Any thoughts on how to get the compiler to compute the magic number in the generic at compile time?

You would want to make use of the attributes of floating point types in ARM A.5.3

http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-A-5-3.html

Whether these provide the information you need is another question. I don't see 
how you could declare the modular type in the generic.

-- 
Jeff Carter
"One day he told me he was a gynecologist. He
couldn't speak no foreign languages."
Take the Money and Run
147

  reply	other threads:[~2021-01-02 23:18 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-02 22:26 Quick inverse square root Matt Borchers
2021-01-02 23:18 ` Jeffrey R. Carter [this message]
2021-01-03 10:58 ` Dmitry A. Kazakov
2021-01-03 22:31   ` Matt Borchers
2021-01-03 23:47     ` Jeffrey R. Carter
2021-01-04  3:50       ` Matt Borchers
2021-01-04  4:28         ` Matt Borchers
2021-01-04 11:04         ` Jeffrey R. Carter
2021-01-04 11:13     ` AdaMagica
2021-01-04 11:28       ` AdaMagica
2021-01-04 12:13         ` Dmitry A. Kazakov
2021-01-04 13:39 ` Egil H H
2021-01-04 20:55   ` Matt Borchers
2021-01-04 21:06     ` Paul Rubin
2021-01-05  2:22       ` Matt Borchers
2021-01-07 17:49     ` Brian Drummond
replies disabled

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