Welcome
Username or Email:

Password:


Missing Code




[ ]
[ ]
Online
  • Guests: 61
  • Members: 0
  • Newest Member: omjtest
  • Most ever online: 396
    Guests: 396, Members: 0 on 12 Jan : 12:51
Members Birthdays:
All today's birthdays', congrats!
Dax (42)
Mino (49)


Next birthdays
11/29 Sonic (58)
11/29 kamelryttarn (46)
11/30 arnsfelt (45)
Contact
If you need assistance, please send an email to forum at 4hv dot org. To ensure your email is not marked as spam, please include the phrase "4hv help" in the subject line. You can also find assistance via IRC, at irc.shadowworld.net, room #hvcomm.
Support 4hv.org!
Donate:
4hv.org is hosted on a dedicated server. Unfortunately, this server costs and we rely on the help of site members to keep 4hv.org running. Please consider donating. We will place your name on the thanks list and you'll be helping to keep 4hv.org alive and free for everyone. Members whose names appear in red bold have donated recently. Green bold denotes those who have recently donated to keep the server carbon neutral.


Special Thanks To:
  • Aaron Holmes
  • Aaron Wheeler
  • Adam Horden
  • Alan Scrimgeour
  • Andre
  • Andrew Haynes
  • Anonymous000
  • asabase
  • Austin Weil
  • barney
  • Barry
  • Bert Hickman
  • Bill Kukowski
  • Blitzorn
  • Brandon Paradelas
  • Bruce Bowling
  • BubeeMike
  • Byong Park
  • Cesiumsponge
  • Chris F.
  • Chris Hooper
  • Corey Worthington
  • Derek Woodroffe
  • Dalus
  • Dan Strother
  • Daniel Davis
  • Daniel Uhrenholt
  • datasheetarchive
  • Dave Billington
  • Dave Marshall
  • David F.
  • Dennis Rogers
  • drelectrix
  • Dr. John Gudenas
  • Dr. Spark
  • E.TexasTesla
  • eastvoltresearch
  • Eirik Taylor
  • Erik Dyakov
  • Erlend^SE
  • Finn Hammer
  • Firebug24k
  • GalliumMan
  • Gary Peterson
  • George Slade
  • GhostNull
  • Gordon Mcknight
  • Graham Armitage
  • Grant
  • GreySoul
  • Henry H
  • IamSmooth
  • In memory of Leo Powning
  • Jacob Cash
  • James Howells
  • James Pawson
  • Jeff Greenfield
  • Jeff Thomas
  • Jesse Frost
  • Jim Mitchell
  • jlr134
  • Joe Mastroianni
  • John Forcina
  • John Oberg
  • John Willcutt
  • Jon Newcomb
  • klugesmith
  • Leslie Wright
  • Lutz Hoffman
  • Mads Barnkob
  • Martin King
  • Mats Karlsson
  • Matt Gibson
  • Matthew Guidry
  • mbd
  • Michael D'Angelo
  • Mikkel
  • mileswaldron
  • mister_rf
  • Neil Foster
  • Nick de Smith
  • Nick Soroka
  • nicklenorp
  • Nik
  • Norman Stanley
  • Patrick Coleman
  • Paul Brodie
  • Paul Jordan
  • Paul Montgomery
  • Ped
  • Peter Krogen
  • Peter Terren
  • PhilGood
  • Richard Feldman
  • Robert Bush
  • Royce Bailey
  • Scott Fusare
  • Scott Newman
  • smiffy
  • Stella
  • Steven Busic
  • Steve Conner
  • Steve Jones
  • Steve Ward
  • Sulaiman
  • Thomas Coyle
  • Thomas A. Wallace
  • Thomas W
  • Timo
  • Torch
  • Ulf Jonsson
  • vasil
  • Vaxian
  • vladi mazzilli
  • wastehl
  • Weston
  • William Kim
  • William N.
  • William Stehl
  • Wesley Venis
The aforementioned have contributed financially to the continuing triumph of 4hv.org. They are deserving of my most heartfelt thanks.
Forums
4hv.org :: Forums :: Computer Science
« Previous topic | Next topic »   

Square root algorithms

1 2 
Move Thread LAN_403
Bjørn
Sat Apr 22 2006, 11:50AM Print
Bjørn Registered Member #27 Joined: Fri Feb 03 2006, 02:20AM
Location: Hyperborea
Posts: 2058
Does anyone know any efficient square root algorithms? I made this one in an attempt to make it very short and failsafe but it is the fastest one I have tried out so far.

I was surprised not to find anything faster so if anyone has any ideas or pointers towards something efficient it would be interesting to hear. I saw an algorithm years ago that was very efficient but I can't recall the name.


;r0 = sqrt(r1)
		mov     r0, #0
		mov     r3, #32768
loop1   orr     r0, r0, r3
		mul     r2, r0, r0
		cmp     r2, r1
		bichi   r0, r0, r3
		movnes  r3, r3, lsr #1
		bne     loop1
Back to top
ShawnLG
Sat Apr 22 2006, 03:48PM
ShawnLG Registered Member #286 Joined: Mon Mar 06 2006, 04:52AM
Location:
Posts: 399
This one says it is under 20 clock cycles on some cpus.
Link2
Back to top
JimG
Sat Apr 22 2006, 04:38PM
JimG Registered Member #52 Joined: Thu Feb 09 2006, 04:22AM
Location: Austin TX
Posts: 57
The code you've shown is equivalent to the following C code:

int const isqrt( int const square )
{
   int guess = 0;
   int bit = 1 << 15;  // do the first 15 LSBs
   while ( bit > 0 ) 
   {
      guess |= bit;   // build up a value
      if ( guess * guess > square ) // is the square of the guess bigger than the orginal?
         guess &= ~bit; // remove the bit 
      bit >>= 1;
   }
   return guess;
}

It doesn't scale very well beyond 32 bit inputs and will always take 96 instructions to execute, but the algorithm you choose is dependant on your situation. If you're ever going to be taking the square root of 8 bit numbers you would use a different algorithm from if you were getting the square root of an 80 bit floating point number.

Normally table lookup with a Newton iteration like Newton-Raphson yeilds fast and accurate results for larger numbers. A purely iterative approach works really well for small numbers. It's always a trade off of overhead

I have used the BSR trick on Intel that is pointed out in the link ShawnLG provided to get an approximation before doing an iterative approach, as well.


Update: Fixed or to an and.
Back to top
Electroholic
Sat Apr 22 2006, 09:20PM
Electroholic Registered Member #191 Joined: Fri Feb 17 2006, 02:01AM
Location: Esbjerg Denmark
Posts: 720
Copied from circuit cellar issue 187

Sum of odds

uint16_t Exh_sumodd-opt
(uint16_t A)
{
uint16_t x_odd = 1;

while (x_odd <= A)
{
A -= x_odd;
x_odd += 2;
}

return (x_odd>>1);
}
Back to top
Alfons
Sat Apr 22 2006, 11:43PM
Alfons Registered Member #134 Joined: Fri Feb 10 2006, 10:44PM
Location: Belgium
Posts: 86
In BASIC, this will work:

10 INPUT "Calculate square root of";A
20 GOSUB 50
30 PRINT "Square root of "A" is: ";B
40 END
50 O=1:B=0
60 AA=A*100
70 AA=AA-O
80 B=B+1
90 O=O+2
100 IF AA<=0 THEN B=B/10:RETURN
110 GOTO 70

(The code was given to me by Fedor Steeman, a Tandy Color Computer enthousiast)
Back to top
Bjørn
Sun Apr 23 2006, 12:05AM
Bjørn Registered Member #27 Joined: Fri Feb 03 2006, 02:20AM
Location: Hyperborea
Posts: 2058
My code does exactly the same as the following C code except that it exits early if it finds an exact result.

Table lookup in Flash memory is quite slow and the accuracy is not what I was aiming for but I have made a note of the <20 cycle algorithm for other uses.

I tried the sum of odds and i was faster on 8 bit numbers than my first attempt but if I configure my code for 8 bit then it becomes a lot faster than the sum of odds.
Back to top
Simon
Sun Apr 23 2006, 12:15AM
Simon Registered Member #32 Joined: Sat Feb 04 2006, 08:58AM
Location: Australia
Posts: 549
The Newton method should work well. To keep the options coming, what about a Taylor expansion? Ie. approximating the square-root function with a polynomial. Each term in your approximation should only involve two extra multiplications at most.

This isn't the best applicaton for Taylor approximations, however. Exponents and trig are better examples.

I guess this is meant to be a general algorithm, otherwise that would change things a lot.
Back to top
Bjørn
Mon Apr 24 2006, 04:02PM
Bjørn Registered Member #27 Joined: Fri Feb 03 2006, 02:20AM
Location: Hyperborea
Posts: 2058
It is supposed to be a general purpose algorithm for a microcomputer and have these properties that does not mix well:

  • Predictable
  • Accurate
  • Fast
  • Small


My first attempt unrolls well and I can use an unrolled version for 16 bit numbers and use that for small numbers and to approximate the square root of large numbers and use the looped version for 32 bit numbers.
Back to top
Carbon_Rod
Tue Apr 25 2006, 04:43AM
Carbon_Rod Registered Member #65 Joined: Thu Feb 09 2006, 06:43AM
Location:
Posts: 1155
For low speed 8-bit real time fixed-root core design a look-up style hash table is nice as it uses no ram. =) Its cost is O (1) so it’s fast, simple, and predictable. There is a space-time trade off.. However, it also offers controlled number rounding if a weighted result is needed.

Did you mean an n-th root function or specifically square root? Estimated results using successive approximation techniques are common.
Back to top
Bjørn
Tue Apr 25 2006, 12:54PM
Bjørn Registered Member #27 Joined: Fri Feb 03 2006, 02:20AM
Location: Hyperborea
Posts: 2058
I was specifically thinking about square roots since that is what I needed at the time. I would be interested in efficient implementations of all kinds of useful functions, including IEEE 754 floating point functions. There is only room for 8192 instructions and there is a lot of other things to be included.
Back to top
1 2 

Moderator(s): Chris Russell, Noelle, Alex, Tesladownunder, Dave Marshall, Dave Billington, Bjørn, Steve Conner, Wolfram, Kizmo, Mads Barnkob

Go to:

Powered by e107 Forum System
 
Legal Information
This site is powered by e107, which is released under the GNU GPL License. All work on this site, except where otherwise noted, is licensed under a Creative Commons Attribution-ShareAlike 2.5 License. By submitting any information to this site, you agree that anything submitted will be so licensed. Please read our Disclaimer and Policies page for information on your rights and responsibilities regarding this site.