From: "Dann Corbit" Subject: Re: Quick square rooting algorithm Date: Tue, 21 Dec 1999 22:44:35 -0800 Newsgroups: sci.math.num-analysis "Oleg Soloviev" wrote in message news:Ti_74.2728$dq2.128141@news2.bora.net... > Need advice for implementing quick square rooting algorithm - wish to use > for graphic purposes - Are any methods quicker and better than square least > method/polinom approximation? If you only need integer precision, I find this one is hard to beat. unsigned long isqrt26(unsigned long input) { unsigned long s = 0; unsigned long s21 = 1073741824; if (s21 <= input) s += 32768, input -= s21; s21 = (s << 15) + 268435456; if (s21 <= input) s += 16384, input -= s21; s21 = (s << 14) + 67108864; if (s21 <= input) s += 8192, input -= s21; s21 = (s << 13) + 16777216; if (s21 <= input) s += 4096, input -= s21; s21 = (s << 12) + 4194304; if (s21 <= input) s += 2048, input -= s21; s21 = (s << 11) + 1048576; if (s21 <= input) s += 1024, input -= s21; s21 = (s << 10) + 262144; if (s21 <= input) s += 512, input -= s21; s21 = (s << 9) + 65536; if (s21 <= input) s += 256, input -= s21; s21 = (s << 8) + 16384; if (s21 <= input) s += 128, input -= s21; s21 = (s << 7) + 4096; if (s21 <= input) s += 64, input -= s21; s21 = (s << 6) + 1024; if (s21 <= input) s += 32, input -= s21; s21 = (s << 5) + 256; if (s21 <= input) s += 16, input -= s21; s21 = (s << 4) + 64; if (s21 <= input) s += 8, input -= s21; s21 = (s << 3) + 16; if (s21 <= input) s += 4, input -= s21; s21 = (s << 2) + 4; if (s21 <= input) s += 2, input -= s21; s21 = (s << 1) + 1; if (s21 <= input) s++; return s; } -- C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html "The C-FAQ Book" ISBN 0-201-84519-9 C.A.P. Newsgroup http://www.dejanews.com/~c_a_p C.A.P. FAQ: ftp://38.168.214.175/pub/Chess%20Analysis%20Project%20FAQ.htm ============================================================================== From: "Dann Corbit" Subject: Re: Calculating length of a line without square root Date: Thu, 30 Dec 1999 12:16:48 -0800 Newsgroups: sci.math.num-analysis That one is slow. Try isqrt28 or isqrt29 instead: unsigned long isqrt28(unsigned long input) { unsigned long s = 0; unsigned long s21 = 1073741824; if (s21 <= input) s |= 32768, input -= s21; s21 = (s << 15) + 268435456; if (s21 <= input) s |= 16384, input -= s21; s21 = (s << 14) + 67108864; if (s21 <= input) s |= 8192, input -= s21; s21 = (s << 13) + 16777216; if (s21 <= input) s |= 4096, input -= s21; s21 = (s << 12) + 4194304; if (s21 <= input) s |= 2048, input -= s21; s21 = (s << 11) + 1048576; if (s21 <= input) s |= 1024, input -= s21; s21 = (s << 10) + 262144; if (s21 <= input) s |= 512, input -= s21; s21 = (s << 9) + 65536; if (s21 <= input) s |= 256, input -= s21; s21 = (s << 8) + 16384; if (s21 <= input) s |= 128, input -= s21; s21 = (s << 7) + 4096; if (s21 <= input) s |= 64, input -= s21; s21 = (s << 6) + 1024; if (s21 <= input) s |= 32, input -= s21; s21 = (s << 5) + 256; if (s21 <= input) s |= 16, input -= s21; s21 = (s << 4) + 64; if (s21 <= input) s |= 8, input -= s21; s21 = (s << 3) + 16; if (s21 <= input) s |= 4, input -= s21; s21 = (s << 2) + 4; if (s21 <= input) s |= 2, input -= s21; s21 = (s << 1) + 1; if (s21 <= input) s++; return s; } /* Log base 2 estimator */ int qlog2(unsigned long n) { register int i = (n & 0xffff0000) ? 16 : 0; if ((n >>= i) & 0xff00) i |= 8, n >>= 8; if (n & 0xf0) i |= 4, n >>= 4; if (n & 0xc) i |= 2, n >>= 2; return (i | (n >> 1)); } unsigned long isqrt29(unsigned long x) { unsigned long xn, xs; if (x <= 1) return (x); xn = x >> (qlog2(x) / 2); // First guess is x shifted by half its // highest bit position do { xs = xn; // Save present Xn xn = (xn + x / xn) / 2; // Compiler outputs right shifts for /2^n } while (xn - xs > 1); return (xn); } If you just need an approximiation, use this: // Integer Square Root function // Uses factoring to find square root // A 256 entry table used to work out the square root of the 7 or 8 most // significant bits. A power of 2 used to approximate the rest. // Based on an 80386 Assembly implementation by Arne Steinarson unsigned const char sqq_table[] = { 0, 16, 22, 27, 32, 35, 39, 42, 45, 48, 50, 53, 55, 57, 59, 61, 64, 65, 67, 69, 71, 73, 75, 76, 78, 80, 81, 83, 84, 86, 87, 89, 90, 91, 93, 94, 96, 97, 98, 99, 101, 102, 103, 104, 106, 107, 108, 109, 110, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 128, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 144, 145, 146, 147, 148, 149, 150, 150, 151, 152, 153, 154, 155, 155, 156, 157, 158, 159, 160, 160, 161, 162, 163, 163, 164, 165, 166, 167, 167, 168, 169, 170, 170, 171, 172, 173, 173, 174, 175, 176, 176, 177, 178, 178, 179, 180, 181, 181, 182, 183, 183, 184, 185, 185, 186, 187, 187, 188, 189, 189, 190, 191, 192, 192, 193, 193, 194, 195, 195, 196, 197, 197, 198, 199, 199, 200, 201, 201, 202, 203, 203, 204, 204, 205, 206, 206, 207, 208, 208, 209, 209, 210, 211, 211, 212, 212, 213, 214, 214, 215, 215, 216, 217, 217, 218, 218, 219, 219, 220, 221, 221, 222, 222, 223, 224, 224, 225, 225, 226, 226, 227, 227, 228, 229, 229, 230, 230, 231, 231, 232, 232, 233, 234, 234, 235, 235, 236, 236, 237, 237, 238, 238, 239, 240, 240, 241, 241, 242, 242, 243, 243, 244, 244, 245, 245, 246, 246, 247, 247, 248, 248, 249, 249, 250, 250, 251, 251, 252, 252, 253, 253, 254, 254, 255 }; // Really fast, but not real accurate. unsigned long isqrt08(unsigned long n) { if (n >= 0x10000) if (n >= 0x1000000) if (n >= 0x10000000) if (n >= 0x40000000) return (sqq_table[n >> 24] << 8); else return (sqq_table[n >> 22] << 7); else if (n >= 0x4000000) return (sqq_table[n >> 20] << 6); else return (sqq_table[n >> 18] << 5); else if (n >= 0x100000) if (n >= 0x400000) return (sqq_table[n >> 16] << 4); else return (sqq_table[n >> 14] << 3); else if (n >= 0x40000) return (sqq_table[n >> 12] << 2); else return (sqq_table[n >> 10] << 1); else if (n >= 0x100) if (n >= 0x1000) if (n >= 0x4000) return (sqq_table[n >> 8]); else return (sqq_table[n >> 6] >> 1); else if (n >= 0x400) return (sqq_table[n >> 4] >> 2); else return (sqq_table[n >> 2] >> 3); else if (n >= 0x10) if (n >= 0x40) return (sqq_table[n] >> 4); else return (sqq_table[n << 2] << 5); else if (n >= 0x4) return (sqq_table[n >> 4] << 6); else return (sqq_table[n >> 6] << 7); } HTH. -- C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html "The C-FAQ Book" ISBN 0-201-84519-9 C.A.P. Newsgroup http://www.dejanews.com/~c_a_p C.A.P. FAQ: ftp://38.168.214.175/pub/Chess%20Analysis%20Project%20FAQ.htm