C-language Linux kernel implementation of square root computing algorithm

Source: Internet
Author: User
Tags square root

The calculation of the square root is also implemented in the Linux kernel, just like the SQRT function in the MATH.H math library.

The formula for the square root is defined:

if a non-negative xof theSquareequals a, i.e.,, then this non-negative xcalled aof thesquare root of arithmetic. athe square root of the arithmetic is recorded as, read as " radical a", acalled Radicals (radicand). Ask for a non-negative number athe square root of the operation is calledOpen Square. Conclusion: The larger the radicals, the greater the square root of the corresponding arithmetic (which is true for all positive numbers). aPositiveIf there is a square root, then there must be two, they are mutuallyopposite number. Obviously, if we know one of these two square roots, then we can get another square root of it in time based on the concept of the opposite number. Haha, elementary school students understand, do not explain, directly to see the code:like, take the code out of the kernel:
#include <stdio.h> #ifdef config_64bit#define bits_per_long 64#else#define bits_per_long 32#endif/** * int_sqrt- Rough approximation to SQRT * @x:integer of which-calculate the sqrt * * A very rough approximation to the sqrt () func tion. */unsigned long int_sqrt (unsigned long x) {unsigned long op, res, one;op = x;res = 0;one = 1UL << (bits_per_long-2) ; while (one > op) one >>= 2;while (one! = 0) {if (op >= res + one) {op = op-(res + one); res = res +  2 * o NE;} Res/= 2;one/= 4;} return res;} int main (void) {printf ("%d\n", int_sqrt (+)); return 0;}
Operation Result:


C-language Linux kernel implementation of square root computing algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.