From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Path: eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: "Jeffrey R. Carter" Newsgroups: comp.lang.ada Subject: Re: Quick inverse square root Date: Sun, 3 Jan 2021 00:18:11 +0100 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sat, 2 Jan 2021 23:18:13 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="15d5170199cbcd454c5ac2efae5bfc4b"; logging-data="28146"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/BOR85U+tZRx72tFa2uw5XSjBYSCfpDX0=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.5.0 Cancel-Lock: sha1:U2K3ryPzSPhjF3Ot/RxT6ukOOpA= In-Reply-To: Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:61014 List-Id: 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