Square root program on single chip microcomputer

Publisher:LianaiLatest update time:2015-01-26 Source: laoguKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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; }

Keywords:MCU Reference address:Square root program on single chip microcomputer

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

51 MCU serial communication interrupt method
Program explanation: /*---------------------------------------------------------------*/  //Serial communication  //Press the button, the MCU sends data "Come On!\r\n" to the host  //MCU crystal: 11.0592MHz  //Baud rate: 9600bps      /*-------------------------------------------------------------*/  //Include
[Microcontroller]
What is the use of learning microcontroller? What are the self-study websites for microcontrollers?
The word microcontroller is unfamiliar to most people. I always like to compare microcontrollers to candles: burning yourself and illuminating everyone. The products made with microcontrollers can be said to be overwhelming. Take home appliances as an example: refrigerators, air conditioners, rice cookers, microwave
[Microcontroller]
Several methods of microcontroller soft reset
    If the slave receives a reset command (software command), how does the program reset the machine? Although it is best not to use "reset" to keep the software in a controllable state, because reset is a pure hardware process and the software is uncontrollable. But we still have to discuss the method. The generally c
[Microcontroller]
The role of the microcontroller watchdog
The function of the watchdog is to feed the watchdog regularly and reset the timer when the system CPU is working normally. If there is a problem with the system and the watchdog is not fed, the watchdog will reset the CPU due to timeout. After the system is initialized, it registers the watchdog interrupt request_irq
[Microcontroller]
Several Ways to Reset Software of MCU Embedded System
Phase One series 8-bit microcontrollers do not have a specific control register to implement software reset. When the code needs to force a reset during program execution, some software techniques must be used to achieve it: Soft reset, the program runs from the beginning, the hardware is not reset. The reset p
[Microcontroller]
Using a single chip microcomputer to make a multi-input voltage meter
In industrial control and intelligent instruments, single-chip microcomputers are often used for real-time control and real-time data processing. The information processed by the single-chip microcomputer is digital, while the relevant parameters of the controlled or measured object are often continuously changing anal
[Microcontroller]
Using a single chip microcomputer to make a multi-input voltage meter
How do domestic chips obtain automotive certification?
Microcontrollers (MCUs) are widely used in automobiles and are one of the most important chips in automotive electronics. From high-end pre-controllers to simple door and window controls, MCUs are used in everything. Compared with consumer, industrial, and even military MCUs, automotive MCUs have the following chara
[Embedded]
How do domestic chips obtain automotive certification?
Design of Presettable Programmable Broadband DC Power Amplifier Circuit Based on AVR Microcontroller
  This paper uses AVR single-chip microcomputer ATmegal28 as the core controller, combined with 10-bit serial D/A chip TLC5615, power amplifier THS3092, programmable gain amplifier AD603 and other related circuits to form a preset programmable broadband DC power amplifier circuit. The gain adjustment range of this cir
[Microcontroller]
Design of Presettable Programmable Broadband DC Power Amplifier Circuit Based on AVR Microcontroller
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号