comp.lang.ada
 help / color / mirror / Atom feed
From: okellogg@cube.net (Oliver Kellogg)
Subject: Re: Integer Square Root
Date: 1996/10/22
Date: 1996-10-22T00:00:00+00:00	[thread overview]
Message-ID: <54jk3f$k8o@salyko.cube.net> (raw)
In-Reply-To: mheaney-ya023180001710962222450001@news.ni.net


-- This is a square root algorithm for unsigned 32 bit integers that
-- uses addition, subtraction, and shifts only.
-- Result is *truncated*, i.e. Sqrt(62) = 7.
-- For a description of the algorithm, see the German computer magazine
-- c't, January 1990, page 300 ff. (Otto Peter, "Prozessor zieht Wurzeln")

with Interfaces;
use  Interfaces;
function Sqrt (X : Unsigned_32) return Unsigned_32 is
   Root : Unsigned_32 := 0;
   M    : Unsigned_32 := 16#4000_0000#;
   X1   : Unsigned_32 := X;
   X2   : Unsigned_32;
begin
   loop
      X2 := Root + M;
      Root := Shift_Right (Root, 1);
      if X2 <= X1 then
         X1 := X1 - X2;
         Root := Root + M;
      end if;
      M := Shift_Right (M, 2);
      exit when M = 0;
   end loop;
   return Root;
end Sqrt;






  reply	other threads:[~1996-10-22  0:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-10-15  0:00 Integer Square Root Matthew Heaney
1996-10-17  0:00 ` Keith Thompson
1996-10-17  0:00   ` Matthew Heaney
1996-10-22  0:00     ` Oliver Kellogg [this message]
1996-10-17  0:00 ` Mats Weber
  -- strict thread matches above, loose matches on Subject: below --
1996-10-15  0:00 W. Wesley Groleau (Wes)
replies disabled

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