Commit a4c9b97e by Mark Mitchell Committed by Mark Mitchell

hashtab.c (higher_prime_number): Use a table, rather than a seive, to find the next prime.

	* hashtab.c (higher_prime_number): Use a table, rather than a
	seive, to find the next prime.

From-SVN: r37775
parent e9608fe6
2000-11-26 Mark Mitchell <mark@codesourcery.com>
* hashtab.c (higher_prime_number): Use a table, rather than a
seive, to find the next prime.
2000-11-22 H.J. Lu <hjl@gnu.org> 2000-11-22 H.J. Lu <hjl@gnu.org>
* cplus-dem.c (main): Handle gnat_demangling. * cplus-dem.c (main): Handle gnat_demangling.
......
...@@ -71,35 +71,69 @@ static PTR *find_empty_slot_for_expand PARAMS ((htab_t, hashval_t)); ...@@ -71,35 +71,69 @@ static PTR *find_empty_slot_for_expand PARAMS ((htab_t, hashval_t));
htab_hash htab_hash_pointer = hash_pointer; htab_hash htab_hash_pointer = hash_pointer;
htab_eq htab_eq_pointer = eq_pointer; htab_eq htab_eq_pointer = eq_pointer;
/* The following function returns the nearest prime number which is /* The following function returns a nearest prime number which is
greater than a given source number, N. */ greater than N, and near a power of two. */
static unsigned long static unsigned long
higher_prime_number (n) higher_prime_number (n)
unsigned long n; unsigned long n;
{ {
unsigned long i; /* These are primes that are near, but slightly smaller than, a
power of two. */
/* Ensure we have a larger number and then force to odd. */ static unsigned long primes[] = {
n++; 2,
n |= 0x01; 7,
13,
/* All odd numbers < 9 are prime. */ 31,
if (n < 9) 61,
return n; 127,
251,
/* Otherwise find the next prime using a sieve. */ 509,
1021,
next: 2039,
4093,
8191,
16381,
32749,
65521,
131071,
262139,
524287,
1048573,
2097143,
4194301,
8388593,
16777213,
33554393,
67108859,
134217689,
268435399,
536870909,
1073741789,
2147483647,
4294967291
};
unsigned long* low = &primes[0];
unsigned long* high = &primes[sizeof(primes) / sizeof(primes[0])];
while (low != high)
{
unsigned long* mid = low + (high - low) / 2;
if (n > *mid)
low = mid + 1;
else
high = mid;
}
for (i = 3; i * i <= n; i += 2) /* If we've run out of primes, abort. */
if (n % i == 0) if (n > *low)
{ {
n += 2; fprintf (stderr, "Cannot find prime bigger than %lu\n", n);
goto next; abort ();
} }
return n; return *low;
} }
/* Returns a hash code for P. */ /* Returns a hash code for P. */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment