Due to work needs , I need to implement square root operation on a single chip microcomputer. Currently, most of the methods for square root are Newton's iteration method. After checking some information, I found a method that is faster than Newton's iteration method. I dare not keep it to myself, so I introduce it to you, hoping it will be helpful.
1. Principle
For typesetting reasons, pow(X,Y) is used to represent X raised to the power of Y, and B[0], B[1], ..., B[m-1] is used to represent a sequence,
where [x] is the subscript.
Assumptions:
B[x], b[x] are both binary sequences, taking values 0 or 1.
M = B[m-1]*pow(2,m-1) + B[m-2]*pow(2,m-2) + ... + B[1]*pow(2,1) + B[0]*pow
(2,0)
N = b[n-1]*pow(2,n-1) + b[n-2]*pow(2,n-2) + ... + b[1]*pow(2,1) + n[0]*pow
(2,0)
pow(N,2) = M
(1) The highest bit b[n-1] of N can be directly obtained from the highest bit B[m-1] of M.
Assume m is known, since pow(2, m-1) <= M <= pow(2, m), then pow(2, (m-1)/2) <= N <=
pow(2, m/2)
If m is odd, let m=2*k+1,
then pow(2,k) <= N < pow(2, 1/2+k) < pow(2, k+1),
n-1=k, n=k+1=(m+1)/2If
m is even, let m=2k,
then pow(2,k) > N >= pow(2, k-1/2) > pow(2, k-1),
n-1=k-1,n=k=m/2So
b[n-1] is completely determined by B[m-1].
Remainder M[1] = M - b[n-1]*pow(2, 2*n-2)
(2) The second highest bit b[n-2] of N can be determined by trial and error.
Since b[n-1]=1, assuming b[n-2]=1, then pow(b[n-1]*pow(2,n-1) + b[n-1]*pow(2,n-2),
2) = b[n-1]*pow(2,2*n-2) + (b[n-1]*pow(2,2*n-2) + b[n-2]*pow(2,2*n-4)),
and then compare whether the remainder M[1] is greater than or equal to (pow(2,2)*b[n-1] + b[n-2]) * pow(2,2*n-4). This
comparison can be made based on B[m-1], B[m-2], ..., B[2*n-4], and the other lower bits are not compared.
If M[1] >= (pow(2,2)*b[n-1] + b[n-2]) * pow(2,2*n-4), then assume valid, b[n-2] =
1;
remainder M[2] = M[1] - pow(pow(2,n-1)*b[n-1] + pow(2,n-2)*b[n-2], 2) = M[1] -
(pow(2,2)+1)*pow(2,2*n-4);
if M[1] < (pow(2,2)*b[n-1] + b[n-2]) * pow(2,2*n-4), then assume invalid, b[n-2] =
0; remainder M[2] = M[1].
(3) Similarly, we can find each digit of the square root N of M from high to low.
When using this algorithm to calculate the square root of a 32-bit number, only 16 comparisons are required at most, and each comparison does not require
comparing each bit of M one by one, especially at the beginning when the number of bits to be compared is very small, so the time consumed is much lower than the Newton iteration method.
2. Flowchart
(under construction, to be uploaded later)
3. Implementation code
Here is the C language code to implement the square root of a 32-bit unsigned integer to get a 16-bit unsigned integer.
------------------------------------------------------------------------------- - /********************************************/ /*Function: Square root processing*/ /*Entry parameter: radicand, long integer*/ /*Exit parameter: square root result, integer*/ /********************************************/ unsigned int sqrt_16(unsigned long M) { unsigned int N, i; unsigned long tmp, ttp; // Result, loop countif (M == 0) // radicand, square root result is also 0 return 0; N = 0; tmp = (M >> 30); // Get the highest bit: B[m-1] M <<= 2; if (tmp > 1) // The highest bit is 1 { N ++; // The current bit of the result is 1, otherwise it is the default 0 tmp -= N; } for (i=15; i>0; i--) // Find the remaining 15 bits { N <<= 1; // Shift left one bit tmp <<= 2; tmp += (M >> 30); // Assume ttp = N; ttp = (ttp<<1)+1; M <<= 2; if (tmp >= ttp) // Assumption holds { tmp -= ttp; N ++; } } return N; }
Previous article:Implementation of single-button power on/off
Next article:Design of low power consumption temperature detector based on single chip microcomputer
Recommended ReadingLatest update time:2024-11-16 13:25
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- EEWORLD University ---- Live playback: The most important component of the analog world - Signal chain and power supply: Interface special
- EEWORLD University Hall----Live Replay: ADI Digital Active Noise Cancelling Headphone Solution
- If the number of 1 bits in the accumulator is odd, P is set (odd parity); otherwise, P is cleared.
- 【NXP Rapid IoT Review】W3 Environmental Data Collection
- [Flower carving hands-on] Interesting and fun music visualization project (09) - X Music Spectrum
- Fudan Micro FM33LC046N Review Summary
- TMS320C55x Assembly Language Programming
- Does the POR of the 430 microcontroller need to be reset if the voltage is not enough?
- Tektronix 618 promotion has started! The lowest price of the year!
- Request a cadencePCB file