12490Sjkh/*
22490Sjkh * Copyright (c) 1989, 1993
32490Sjkh *	The Regents of the University of California.  All rights reserved.
42490Sjkh *
52490Sjkh * This code is derived from software contributed to Berkeley by
62490Sjkh * Landon Curt Noll.
72490Sjkh *
82490Sjkh * Redistribution and use in source and binary forms, with or without
92490Sjkh * modification, are permitted provided that the following conditions
102490Sjkh * are met:
112490Sjkh * 1. Redistributions of source code must retain the above copyright
122490Sjkh *    notice, this list of conditions and the following disclaimer.
132490Sjkh * 2. Redistributions in binary form must reproduce the above copyright
142490Sjkh *    notice, this list of conditions and the following disclaimer in the
152490Sjkh *    documentation and/or other materials provided with the distribution.
16203932Simp * 3. Neither the name of the University nor the names of its contributors
172490Sjkh *    may be used to endorse or promote products derived from this software
182490Sjkh *    without specific prior written permission.
192490Sjkh *
202490Sjkh * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
212490Sjkh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
222490Sjkh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
232490Sjkh * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
242490Sjkh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
252490Sjkh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
262490Sjkh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
272490Sjkh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
282490Sjkh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
292490Sjkh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
302490Sjkh * SUCH DAMAGE.
312490Sjkh *
322490Sjkh *	@(#)primes.h	8.2 (Berkeley) 3/1/94
33104720Sfanf * $FreeBSD: releng/11.0/usr.bin/primes/primes.h 272207 2014-09-27 09:00:38Z cperciva $
342490Sjkh */
352490Sjkh
362490Sjkh/*
372490Sjkh * primes - generate a table of primes between two values
382490Sjkh *
392490Sjkh * By: Landon Curt Noll chongo@toad.com, ...!{sun,tolsoft}!hoptoad!chongo
402490Sjkh *
412490Sjkh * chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\
422490Sjkh */
432490Sjkh
44272207Scperciva#include <stdint.h>
45272207Scperciva
462490Sjkh/* ubig is the type that holds a large unsigned value */
47272207Scpercivatypedef uint64_t ubig;			/* must be >=32 bit unsigned value */
482490Sjkh#define	BIG		ULONG_MAX	/* largest value will sieve */
492490Sjkh
502490Sjkh/* bytes in sieve table (must be > 3*5*7*11) */
512490Sjkh#define	TABSIZE		256*1024
52104720Sfanf
53104720Sfanf/*
54104720Sfanf * prime[i] is the (i-1)th prime.
55104720Sfanf *
56104720Sfanf * We are able to sieve 2^32-1 because this byte table yields all primes
57104720Sfanf * up to 65537 and 65537^2 > 2^32-1.
58104720Sfanf */
59104720Sfanfextern const ubig prime[];
60104720Sfanfextern const ubig *const pr_limit;	/* largest prime in the prime array */
61104720Sfanf
62272166Scperciva/* Maximum size sieving alone can handle. */
63272166Scperciva#define	SIEVEMAX 4295098368ULL
64272166Scperciva
65104720Sfanf/*
66104720Sfanf * To avoid excessive sieves for small factors, we use the table below to
67108533Sschweikh * setup our sieve blocks.  Each element represents an odd number starting
68104720Sfanf * with 1.  All non-zero elements are factors of 3, 5, 7, 11 and 13.
69104720Sfanf */
70104720Sfanfextern const char pattern[];
71104720Sfanfextern const size_t pattern_size;	/* length of pattern array */
72272166Scperciva
73272166Scperciva/* Test for primality using strong pseudoprime tests. */
74272166Scpercivaint isprime(ubig);
75272166Scperciva
76272166Scperciva/* Maximum value which the SPSP code can handle. */
77272166Scperciva#define	SPSPMAX 3825123056546413050ULL
78