diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2009-03-03 19:29:28 -0800 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2009-03-03 19:29:28 -0800 |
commit | 441f72d43a9b550baa779fc82f70816da5f74f0e (patch) | |
tree | 477ac2b54c1ef5b8e25e4255b27bca9469ed3911 /lib/next_prime.c | |
parent | 7a80622f69812ca3262d2027c07a4ed0c0242945 (diff) | |
download | android_external_elfutils-441f72d43a9b550baa779fc82f70816da5f74f0e.tar.gz android_external_elfutils-441f72d43a9b550baa779fc82f70816da5f74f0e.tar.bz2 android_external_elfutils-441f72d43a9b550baa779fc82f70816da5f74f0e.zip |
auto import from //depot/cupcake/@135843
Diffstat (limited to 'lib/next_prime.c')
-rw-r--r-- | lib/next_prime.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/lib/next_prime.c b/lib/next_prime.c new file mode 100644 index 00000000..5d608046 --- /dev/null +++ b/lib/next_prime.c @@ -0,0 +1,54 @@ +/* Determine prime number. + Copyright (C) 2000, 2002 Free Software Foundation, Inc. + Written by Ulrich Drepper <drepper@redhat.com>, 2000. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, version 2. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +#include <stddef.h> + + +/* Test whether CANDIDATE is a prime. */ +static int +is_prime (size_t candidate) +{ + /* No even number and none less than 10 will be passed here. */ + size_t divn = 3; + size_t sq = divn * divn; + + while (sq < candidate && candidate % divn != 0) + { + size_t old_sq = sq; + ++divn; + sq += 4 * divn; + if (sq < old_sq) + return 1; + ++divn; + } + + return candidate % divn != 0; +} + + +/* We need primes for the table size. */ +size_t +next_prime (size_t seed) +{ + /* Make it definitely odd. */ + seed |= 1; + + while (!is_prime (seed)) + seed += 2; + + return seed; +} |