Home | History | Annotate | Download | only in lib
      1 /* Determine prime number.
      2    Copyright (C) 2006 Red Hat, Inc.
      3    This file is part of Red Hat elfutils.
      4    Written by Ulrich Drepper <drepper (at) redhat.com>, 2000.
      5 
      6    Red Hat elfutils is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU General Public License as published by the
      8    Free Software Foundation; version 2 of the License.
      9 
     10    Red Hat elfutils is distributed in the hope that it will be useful, but
     11    WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13    General Public License for more details.
     14 
     15    You should have received a copy of the GNU General Public License along
     16    with Red Hat elfutils; if not, write to the Free Software Foundation,
     17    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
     18 
     19    In addition, as a special exception, Red Hat, Inc. gives You the
     20    additional right to link the code of Red Hat elfutils with code licensed
     21    under an Open Source Initiative certified open source license
     22    (http://www.opensource.org/licenses/index.php) and to distribute linked
     23    combinations including the two.  Non-GPL Code permitted under this
     24    exception must only link to the code of Red Hat elfutils through those
     25    well defined interfaces identified in the file named EXCEPTION found in
     26    the source code files (the "Approved Interfaces").  The files of Non-GPL
     27    Code may instantiate templates or use macros or inline functions from
     28    the Approved Interfaces without causing the resulting work to be covered
     29    by the GNU General Public License.  Only Red Hat, Inc. may make changes
     30    or additions to the list of Approved Interfaces.  Red Hat's grant of
     31    this exception is conditioned upon your not adding any new exceptions.
     32    If you wish to add a new Approved Interface or exception, please contact
     33    Red Hat.  You must obey the GNU General Public License in all respects
     34    for all of the Red Hat elfutils code and other code used in conjunction
     35    with Red Hat elfutils except the Non-GPL Code covered by this exception.
     36    If you modify this file, you may extend this exception to your version
     37    of the file, but you are not obligated to do so.  If you do not wish to
     38    provide this exception without modification, you must delete this
     39    exception statement from your version and license this file solely under
     40    the GPL without exception.
     41 
     42    Red Hat elfutils is an included package of the Open Invention Network.
     43    An included package of the Open Invention Network is a package for which
     44    Open Invention Network licensees cross-license their patents.  No patent
     45    license is granted, either expressly or impliedly, by designation as an
     46    included package.  Should you wish to participate in the Open Invention
     47    Network licensing program, please visit www.openinventionnetwork.com
     48    <http://www.openinventionnetwork.com>.  */
     49 
     50 #include <stddef.h>
     51 
     52 
     53 /* Test whether CANDIDATE is a prime.  */
     54 static int
     55 is_prime (size_t candidate)
     56 {
     57   /* No even number and none less than 10 will be passed here.  */
     58   size_t divn = 3;
     59   size_t sq = divn * divn;
     60 
     61   while (sq < candidate && candidate % divn != 0)
     62     {
     63       size_t old_sq = sq;
     64       ++divn;
     65       sq += 4 * divn;
     66       if (sq < old_sq)
     67 	return 1;
     68       ++divn;
     69     }
     70 
     71   return candidate % divn != 0;
     72 }
     73 
     74 
     75 /* We need primes for the table size.  */
     76 size_t
     77 next_prime (size_t seed)
     78 {
     79   /* Make it definitely odd.  */
     80   seed |= 1;
     81 
     82   while (!is_prime (seed))
     83     seed += 2;
     84 
     85   return seed;
     86 }
     87