/***************************************************************************/ /* Name : Sqrt.C */ /* Uses : Fast integer square root function */ /* Date : 4/24/2002 */ /* Authors : */ /* Stephen Bye - Orignal square root code posted on "comp.lang.c" */ /* Andrew Que */ /* */ /* Unit revisions : */ /* 1.00 - 4/24/2002 - QUE - Creation */ /* */ /* Andrew Que */ /* Public Domain */ /***************************************************************************/ #include "Genral.H" //-------------------------------------------------------------------------- // Returns integer square root value of input // Orignal code: Stephen Bye //-------------------------------------------------------------------------- uint16 SquareRoot( uint32 Number ) { #define Precision ( 32 >> 1 ) uint16 Root; uint16 RootSquared; uint32 Mask; uint32 MaskSquared; uint32 Test; uint16 Power; Root = 0; RootSquared = 0; Mask = 1 << ( Precision - 1 ); MaskSquared = 1L << ( ( Precision - 1 ) << 1 ); for ( Power = Precision; Power > 0; --Power ) { Test = RootSquared + MaskSquared + ( (uint32)Root << Power ); if ( Test <= Number ) { RootSquared = Test; Root |= Mask; } Mask >>= 1; MaskSquared >>= 2; } return Root; }