123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- #ifndef ISQRT_TEST
- # include "libbb.h"
- #else
- # include <stdlib.h>
- # include <stdio.h>
- # include <time.h>
- # define FAST_FUNC
- #endif
- unsigned long FAST_FUNC isqrt(unsigned long long N)
- {
- unsigned long x;
- unsigned shift;
- #define LL_WIDTH_BITS (unsigned)(sizeof(N)*8)
- shift = LL_WIDTH_BITS - 2;
- x = 0;
- do {
- x = (x << 1) + 1;
- if ((unsigned long long)x * x > (N >> shift))
- x--;
- shift -= 2;
- } while ((int)shift >= 0);
- return x;
- }
- #ifdef ISQRT_TEST
- int main(int argc, char **argv)
- {
- unsigned long long n = argv[1] ? strtoull(argv[1], NULL, 0) : time(NULL);
- for (;;) {
- unsigned long h;
- n--;
- h = isqrt(n);
- if (!(n & 0xffff))
- printf("isqrt(%llx)=%lx\n", n, h);
- if ((unsigned long long)h * h > n) {
- printf("BAD1: isqrt(%llx)=%lx\n", n, h);
- return 1;
- }
- h++;
- if ((unsigned long long)h * h != 0
- && (unsigned long long)h * h <= n)
- {
- printf("BAD2: isqrt(%llx)=%lx\n", n, h);
- return 1;
- }
- }
- }
- #endif
|