├── src ├── primegen-0.97 │ ├── THANKS │ ├── hasgethr.h │ ├── math.lib │ ├── VERSION │ ├── hasrdtsc.h │ ├── CHANGES │ ├── make-compile.sh │ ├── systype │ ├── conf-ld │ ├── install │ ├── primes │ ├── warn-auto.sh │ ├── auto-str │ ├── eratspeed │ ├── exit.h │ ├── instcheck │ ├── primegaps │ ├── bin │ │ ├── primes │ │ └── primegaps │ ├── int64.h │ ├── int64.h1 │ ├── make-load.sh │ ├── primespeed │ ├── SYSDEPS │ ├── int64.h2 │ ├── uint32.h │ ├── uint32.h1 │ ├── uint64.h │ ├── auto_home.h │ ├── uint32.h2 │ ├── uint64.h1 │ ├── uint64.h2 │ ├── conf-cc │ ├── readwrite.h │ ├── trycpp.c │ ├── load │ ├── trygethr.c │ ├── compile │ ├── auto-ccld.sh │ ├── makelib │ ├── open_read.c │ ├── open_trunc.c │ ├── make-compile │ ├── fs64.h │ ├── open.h │ ├── subfderr.c │ ├── TODO │ ├── make-load │ ├── strerr_sys.c │ ├── conf-words │ ├── conf-home │ ├── make-makelib.sh │ ├── substdio.c │ ├── str_len.c │ ├── byte.h │ ├── subfd.h │ ├── auto_home.c │ ├── str.h │ ├── byte_copy.c │ ├── tryrdtsc.c │ ├── error_str.3 │ ├── trylong64.c │ ├── tryulong32.c │ ├── tryulong64.c │ ├── byte_cr.c │ ├── BLURB │ ├── substdio_copy.c │ ├── make-makelib │ ├── README │ ├── error.h │ ├── fs64.c │ ├── primegen_init.c │ ├── primegaps.1 │ ├── man │ │ ├── man1 │ │ │ ├── primegaps.1 │ │ │ └── primes.1 │ │ ├── cat1 │ │ │ ├── primegaps.0 │ │ │ └── primes.0 │ │ ├── man3 │ │ │ └── primegen.3 │ │ └── cat3 │ │ │ └── primegen.0 │ ├── primegaps.c │ ├── primegen_impl.h │ ├── primes.1 │ ├── include │ │ └── primegen.h │ ├── primegen.h.sed │ ├── primegaps.0 │ ├── primespeed.1 │ ├── auto-str.c │ ├── primegen.h │ ├── hier.c │ ├── error.3 │ ├── TARGETS │ ├── primes.0 │ ├── strerr_die.c │ ├── timing.h │ ├── INSTALL │ ├── substdio.h │ ├── FILES │ ├── primespeed.0 │ ├── error.c │ ├── substdi.c │ ├── primegen.3 │ ├── primegen_next.c │ ├── primes.c │ ├── substdo.c │ ├── primegen_skip.c │ ├── primegen.0 │ ├── instcheck.c │ ├── primespeed.c │ ├── strerr.h │ ├── find-systype.sh │ ├── find-systype │ ├── install.c │ ├── error_str.c │ ├── Makefile │ └── primegen.c ├── pollard │ ├── sizes │ ├── sizes.c │ ├── kernel.h │ ├── Makefile │ ├── gpu_main.c │ ├── TIMES │ └── kernel.cu ├── pollard_internet │ ├── pollard_p1 │ ├── compile.sh │ ├── pollard_p1.cc │ ├── Integer.h │ └── Integer.cc └── mpz │ ├── kernel.h │ ├── cuda_string.h │ ├── README.md │ ├── compile.h │ ├── Makefile │ ├── kernel.cu │ ├── cpu_main.c │ └── digit.h ├── ._project_template ├── README.md ├── .gitignore └── Makefile /src/primegen-0.97/THANKS: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/primegen-0.97/hasgethr.h: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/primegen-0.97/math.lib: -------------------------------------------------------------------------------- 1 | -lm 2 | -------------------------------------------------------------------------------- /src/primegen-0.97/VERSION: -------------------------------------------------------------------------------- 1 | primegen 0.97 2 | -------------------------------------------------------------------------------- /src/primegen-0.97/hasrdtsc.h: -------------------------------------------------------------------------------- 1 | #define HASRDTSC 1 2 | -------------------------------------------------------------------------------- /src/primegen-0.97/CHANGES: -------------------------------------------------------------------------------- 1 | 19990304 version: primegen 0.97. 2 | -------------------------------------------------------------------------------- /src/primegen-0.97/make-compile.sh: -------------------------------------------------------------------------------- 1 | echo exec "$CC" -c '${1+"$@"}' 2 | -------------------------------------------------------------------------------- /src/primegen-0.97/systype: -------------------------------------------------------------------------------- 1 | linux-2.6.32-279.22.1.el6.x86_64-:-:-:x86_64-:- 2 | -------------------------------------------------------------------------------- /src/pollard/sizes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmatlack/cuda-rsa/HEAD/src/pollard/sizes -------------------------------------------------------------------------------- /._project_template: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmatlack/cuda-rsa/HEAD/._project_template -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | cuda-rsa 2 | ======== 3 | 4 | [Project Site](http://dmatlack.github.io/cuda-rsa) 5 | -------------------------------------------------------------------------------- /src/primegen-0.97/conf-ld: -------------------------------------------------------------------------------- 1 | gcc 2 | 3 | This will be used to link .o files into an executable. 4 | -------------------------------------------------------------------------------- /src/primegen-0.97/install: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmatlack/cuda-rsa/HEAD/src/primegen-0.97/install -------------------------------------------------------------------------------- /src/primegen-0.97/primes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmatlack/cuda-rsa/HEAD/src/primegen-0.97/primes -------------------------------------------------------------------------------- /src/primegen-0.97/warn-auto.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # WARNING: This file was auto-generated. Do not edit! 3 | -------------------------------------------------------------------------------- /src/primegen-0.97/auto-str: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmatlack/cuda-rsa/HEAD/src/primegen-0.97/auto-str -------------------------------------------------------------------------------- /src/primegen-0.97/eratspeed: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmatlack/cuda-rsa/HEAD/src/primegen-0.97/eratspeed -------------------------------------------------------------------------------- /src/primegen-0.97/exit.h: -------------------------------------------------------------------------------- 1 | #ifndef EXIT_H 2 | #define EXIT_H 3 | 4 | extern void _exit(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /src/primegen-0.97/instcheck: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmatlack/cuda-rsa/HEAD/src/primegen-0.97/instcheck -------------------------------------------------------------------------------- /src/primegen-0.97/primegaps: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmatlack/cuda-rsa/HEAD/src/primegen-0.97/primegaps -------------------------------------------------------------------------------- /src/primegen-0.97/bin/primes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmatlack/cuda-rsa/HEAD/src/primegen-0.97/bin/primes -------------------------------------------------------------------------------- /src/primegen-0.97/int64.h: -------------------------------------------------------------------------------- 1 | #ifndef INT64_H 2 | #define INT64_H 3 | 4 | typedef long int64; 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /src/primegen-0.97/int64.h1: -------------------------------------------------------------------------------- 1 | #ifndef INT64_H 2 | #define INT64_H 3 | 4 | typedef long int64; 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /src/primegen-0.97/make-load.sh: -------------------------------------------------------------------------------- 1 | echo 'main="$1"; shift' 2 | echo exec "$LD" '-o "$main" "$main".o ${1+"$@"}' 3 | -------------------------------------------------------------------------------- /src/primegen-0.97/primespeed: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmatlack/cuda-rsa/HEAD/src/primegen-0.97/primespeed -------------------------------------------------------------------------------- /src/pollard_internet/pollard_p1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmatlack/cuda-rsa/HEAD/src/pollard_internet/pollard_p1 -------------------------------------------------------------------------------- /src/primegen-0.97/SYSDEPS: -------------------------------------------------------------------------------- 1 | VERSION 2 | systype 3 | uint32.h 4 | int64.h 5 | uint64.h 6 | hasgethr.h 7 | hasrdtsc.h 8 | -------------------------------------------------------------------------------- /src/primegen-0.97/bin/primegaps: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmatlack/cuda-rsa/HEAD/src/primegen-0.97/bin/primegaps -------------------------------------------------------------------------------- /src/primegen-0.97/int64.h2: -------------------------------------------------------------------------------- 1 | #ifndef INT64_H 2 | #define INT64_H 3 | 4 | typedef long long int64; 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /src/primegen-0.97/uint32.h: -------------------------------------------------------------------------------- 1 | #ifndef UINT32_H 2 | #define UINT32_H 3 | 4 | typedef unsigned int uint32; 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /src/primegen-0.97/uint32.h1: -------------------------------------------------------------------------------- 1 | #ifndef UINT32_H 2 | #define UINT32_H 3 | 4 | typedef unsigned int uint32; 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /src/primegen-0.97/uint64.h: -------------------------------------------------------------------------------- 1 | #ifndef UINT64_H 2 | #define UINT64_H 3 | 4 | typedef unsigned long uint64; 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /src/primegen-0.97/auto_home.h: -------------------------------------------------------------------------------- 1 | #ifndef AUTO_HOME_H 2 | #define AUTO_HOME_H 3 | 4 | extern char auto_home[]; 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /src/primegen-0.97/uint32.h2: -------------------------------------------------------------------------------- 1 | #ifndef UINT32_H 2 | #define UINT32_H 3 | 4 | typedef unsigned long uint32; 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /src/primegen-0.97/uint64.h1: -------------------------------------------------------------------------------- 1 | #ifndef UINT64_H 2 | #define UINT64_H 3 | 4 | typedef unsigned long uint64; 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /src/primegen-0.97/uint64.h2: -------------------------------------------------------------------------------- 1 | #ifndef UINT64_H 2 | #define UINT64_H 3 | 4 | typedef unsigned long long uint64; 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /src/primegen-0.97/conf-cc: -------------------------------------------------------------------------------- 1 | gcc -O1 -fomit-frame-pointer -include /usr/include/errno.h 2 | 3 | This will be used to compile .c files. 4 | -------------------------------------------------------------------------------- /src/primegen-0.97/readwrite.h: -------------------------------------------------------------------------------- 1 | #ifndef READWRITE_H 2 | #define READWRITE_H 3 | 4 | extern int read(); 5 | extern int write(); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /src/primegen-0.97/trycpp.c: -------------------------------------------------------------------------------- 1 | void main() 2 | { 3 | #ifdef NeXT 4 | printf("nextstep\n"); exit(0); 5 | #endif 6 | printf("unknown\n"); exit(0); 7 | } 8 | -------------------------------------------------------------------------------- /src/primegen-0.97/load: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # WARNING: This file was auto-generated. Do not edit! 3 | main="$1"; shift 4 | exec gcc -o "$main" "$main".o ${1+"$@"} 5 | -------------------------------------------------------------------------------- /src/primegen-0.97/trygethr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | main() 5 | { 6 | hrtime_t t; 7 | 8 | t = gethrtime(); 9 | } 10 | -------------------------------------------------------------------------------- /src/primegen-0.97/compile: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # WARNING: This file was auto-generated. Do not edit! 3 | exec gcc -O1 -fomit-frame-pointer -include /usr/include/errno.h -c ${1+"$@"} 4 | -------------------------------------------------------------------------------- /src/primegen-0.97/auto-ccld.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # WARNING: This file was auto-generated. Do not edit! 3 | CC='gcc -O1 -fomit-frame-pointer -include /usr/include/errno.h' 4 | LD='gcc' 5 | -------------------------------------------------------------------------------- /src/primegen-0.97/makelib: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # WARNING: This file was auto-generated. Do not edit! 3 | main="$1"; shift 4 | rm -f "$main" 5 | ar cr "$main" ${1+"$@"} 6 | ranlib "$main" 7 | -------------------------------------------------------------------------------- /src/primegen-0.97/open_read.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "open.h" 4 | 5 | int open_read(fn) char *fn; 6 | { return open(fn,O_RDONLY | O_NDELAY); } 7 | -------------------------------------------------------------------------------- /src/primegen-0.97/open_trunc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "open.h" 4 | 5 | int open_trunc(fn) char *fn; 6 | { return open(fn,O_WRONLY | O_NDELAY | O_TRUNC | O_CREAT,0644); } 7 | -------------------------------------------------------------------------------- /src/primegen-0.97/make-compile: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # WARNING: This file was auto-generated. Do not edit! 3 | CC='gcc -O1 -fomit-frame-pointer -include /usr/include/errno.h' 4 | LD='gcc' 5 | echo exec "$CC" -c '${1+"$@"}' 6 | -------------------------------------------------------------------------------- /src/pollard_internet/compile.sh: -------------------------------------------------------------------------------- 1 | g++ -m64 -g -DDEBUG -Wall -Wextra pollard_p1.cc -c -o pollard_p1.o; g++ -m64 -g -DDEBUG -Wall -Wextra pollard_p1.o Integer.o -o pollard_p1 -lgmp; ./pollard_p1 1284717778837 >> out.txt 2 | -------------------------------------------------------------------------------- /src/primegen-0.97/fs64.h: -------------------------------------------------------------------------------- 1 | #ifndef FS64_H 2 | #define FS64_H 3 | 4 | #include "uint64.h" 5 | 6 | extern unsigned int scan_uint64(char *,uint64 *); 7 | extern unsigned int fmt_uint64(char *,uint64); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /src/primegen-0.97/open.h: -------------------------------------------------------------------------------- 1 | #ifndef OPEN_H 2 | #define OPEN_H 3 | 4 | extern int open_read(); 5 | extern int open_excl(); 6 | extern int open_append(); 7 | extern int open_trunc(); 8 | extern int open_write(); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /src/primegen-0.97/subfderr.c: -------------------------------------------------------------------------------- 1 | #include "readwrite.h" 2 | #include "substdio.h" 3 | #include "subfd.h" 4 | 5 | char subfd_errbuf[256]; 6 | static substdio it = SUBSTDIO_FDBUF(write,2,subfd_errbuf,256); 7 | substdio *subfderr = ⁢ 8 | -------------------------------------------------------------------------------- /src/primegen-0.97/TODO: -------------------------------------------------------------------------------- 1 | speed up squarefree 2 | speed up initial jumps in doit 3 | 4 | rewrite from scratch 5 | review code for safe use of 32-bit variables 6 | review code for safe use of int variables 7 | 8 | rewrite critical routines in asm 9 | -------------------------------------------------------------------------------- /src/primegen-0.97/make-load: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # WARNING: This file was auto-generated. Do not edit! 3 | CC='gcc -O1 -fomit-frame-pointer -include /usr/include/errno.h' 4 | LD='gcc' 5 | echo 'main="$1"; shift' 6 | echo exec "$LD" '-o "$main" "$main".o ${1+"$@"}' 7 | -------------------------------------------------------------------------------- /src/primegen-0.97/strerr_sys.c: -------------------------------------------------------------------------------- 1 | #include "error.h" 2 | #include "strerr.h" 3 | 4 | struct strerr strerr_sys; 5 | 6 | void strerr_sysinit() 7 | { 8 | strerr_sys.who = 0; 9 | strerr_sys.x = error_str(errno); 10 | strerr_sys.y = ""; 11 | strerr_sys.z = ""; 12 | } 13 | -------------------------------------------------------------------------------- /src/primegen-0.97/conf-words: -------------------------------------------------------------------------------- 1 | 2048 2 | 3 | This is B/32: the number of 32-bit words of space used in the primegen 4 | inner loop. This should fit into the CPU's level-1 cache. 5 | 6 | 2048 works well on a Pentium-100. 7 | 3600 works well on a Pentium II-350. 8 | 4004 works well on an UltraSPARC-I/167. 9 | -------------------------------------------------------------------------------- /src/primegen-0.97/conf-home: -------------------------------------------------------------------------------- 1 | /afs/andrew.cmu.edu/usr7/ajkaufma/Documents/S13/15418/cuda-rsa/primegen-0.97 2 | 3 | This is the primegen home directory. Programs will be installed in 4 | .../bin; the primegen library will be installed in .../lib; man pages 5 | will be installed in subdirectories of .../man. 6 | -------------------------------------------------------------------------------- /src/primegen-0.97/make-makelib.sh: -------------------------------------------------------------------------------- 1 | echo 'main="$1"; shift' 2 | echo 'rm -f "$main"' 3 | echo 'ar cr "$main" ${1+"$@"}' 4 | 5 | case "$1" in 6 | sunos-5.*) ;; 7 | unix_sv*) ;; 8 | irix64-*) ;; 9 | irix-*) ;; 10 | dgux-*) ;; 11 | hp-ux-*) ;; 12 | sco*) ;; 13 | *) 14 | echo 'ranlib "$main"' 15 | ;; 16 | esac 17 | -------------------------------------------------------------------------------- /src/primegen-0.97/substdio.c: -------------------------------------------------------------------------------- 1 | #include "substdio.h" 2 | 3 | void substdio_fdbuf(s,op,fd,buf,len) 4 | register substdio *s; 5 | register int (*op)(); 6 | register int fd; 7 | register char *buf; 8 | register int len; 9 | { 10 | s->x = buf; 11 | s->fd = fd; 12 | s->op = op; 13 | s->p = 0; 14 | s->n = len; 15 | } 16 | -------------------------------------------------------------------------------- /src/mpz/kernel.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file kernel.h 3 | * 4 | * @author David Matlack 5 | */ 6 | #ifndef __418_MPZTEST_KERNEL_H__ 7 | #define __418_MPZTEST_KERNEL_H__ 8 | 9 | #define STRING_MAX_SIZE 512 10 | 11 | void run_addition_kernel(char *A, char *B, char *C, unsigned size); 12 | 13 | #endif /* __418_MPZTEST_KERNEL_H__ */ 14 | -------------------------------------------------------------------------------- /src/primegen-0.97/str_len.c: -------------------------------------------------------------------------------- 1 | #include "str.h" 2 | 3 | unsigned int str_len(s) 4 | register char *s; 5 | { 6 | register char *t; 7 | 8 | t = s; 9 | for (;;) { 10 | if (!*t) return t - s; ++t; 11 | if (!*t) return t - s; ++t; 12 | if (!*t) return t - s; ++t; 13 | if (!*t) return t - s; ++t; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/primegen-0.97/byte.h: -------------------------------------------------------------------------------- 1 | #ifndef BYTE_H 2 | #define BYTE_H 3 | 4 | extern unsigned int byte_chr(); 5 | extern unsigned int byte_rchr(); 6 | extern void byte_copy(); 7 | extern void byte_copyr(); 8 | extern int byte_diff(); 9 | extern void byte_zero(); 10 | 11 | #define byte_equal(s,n,t) (!byte_diff((s),(n),(t))) 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | obj/ 6 | 7 | # Compiled Dynamic libraries 8 | *.so 9 | *.dylib 10 | 11 | # Compiled Static libraries 12 | *.lai 13 | *.la 14 | *.a 15 | 16 | # emacs tmp files 17 | *~ 18 | *# 19 | 20 | # Project Specific Executables 21 | src/mpz/cpu 22 | src/mpz/gpu 23 | src/pollard/gpu 24 | mpz_test 25 | -------------------------------------------------------------------------------- /src/pollard/sizes.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char *argv[]) { 4 | (void) argc; 5 | (void) argv; 6 | 7 | printf("unsigned: %u bytes\n", sizeof(unsigned)); 8 | printf("unsigned long: %u bytes\n", sizeof(unsigned long)); 9 | printf("unsigned long long: %u bytes\n", sizeof(unsigned long long)); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /src/primegen-0.97/subfd.h: -------------------------------------------------------------------------------- 1 | #ifndef SUBFD_H 2 | #define SUBFD_H 3 | 4 | #include "substdio.h" 5 | 6 | extern substdio *subfdin; 7 | extern substdio *subfdinsmall; 8 | extern substdio *subfdout; 9 | extern substdio *subfdoutsmall; 10 | extern substdio *subfderr; 11 | 12 | extern int subfd_read(); 13 | extern int subfd_readsmall(); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /src/primegen-0.97/auto_home.c: -------------------------------------------------------------------------------- 1 | char auto_home[] = "\ 2 | \057\141\146\163\057\141\156\144\162\145\167\056\143\155\165\056\145\144\165\057\165\163\162\067\057\141\152\153\141\165\146\155\141\057\104\157\143\165\155\145\156\164\163\057\123\061\063\057\061\065\064\061\070\057\143\165\144\141\055\162\163\141\057\160\162\151\155\145\147\145\156\055\060\056\071\067\ 3 | "; 4 | -------------------------------------------------------------------------------- /src/primegen-0.97/str.h: -------------------------------------------------------------------------------- 1 | #ifndef STR_H 2 | #define STR_H 3 | 4 | extern unsigned int str_copy(); 5 | extern int str_diff(); 6 | extern int str_diffn(); 7 | extern unsigned int str_len(); 8 | extern unsigned int str_chr(); 9 | extern unsigned int str_rchr(); 10 | extern int str_start(); 11 | 12 | #define str_equal(s,t) (!str_diff((s),(t))) 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /src/primegen-0.97/byte_copy.c: -------------------------------------------------------------------------------- 1 | #include "byte.h" 2 | 3 | void byte_copy(to,n,from) 4 | register char *to; 5 | register unsigned int n; 6 | register char *from; 7 | { 8 | for (;;) { 9 | if (!n) return; *to++ = *from++; --n; 10 | if (!n) return; *to++ = *from++; --n; 11 | if (!n) return; *to++ = *from++; --n; 12 | if (!n) return; *to++ = *from++; --n; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/primegen-0.97/tryrdtsc.c: -------------------------------------------------------------------------------- 1 | main() 2 | { 3 | unsigned long x[2]; 4 | unsigned long y[2]; 5 | 6 | x[0] = 0; 7 | x[1] = 0; 8 | y[0] = 0; 9 | y[1] = 0; 10 | 11 | asm volatile(".byte 15;.byte 49" : "=a"(x[0]),"=d"(x[1]) ); 12 | asm volatile(".byte 15;.byte 49" : "=a"(y[0]),"=d"(y[1]) ); 13 | 14 | if (x[0] != y[0]) _exit(0); 15 | if (x[1] != y[1]) _exit(0); 16 | _exit(1); 17 | } 18 | -------------------------------------------------------------------------------- /src/mpz/cuda_string.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file cuda_string.h 3 | * 4 | * @brief String functions to call from cuda code. 5 | */ 6 | #ifndef __418_CUDA_STRING_H__ 7 | #define __418_CUDA_STRING_H__ 8 | 9 | inline __device__ __host__ int cuda_strlen(const char *str) { 10 | int len = 0; 11 | 12 | while (str[len] != (char) 0) len++; 13 | 14 | return len; 15 | } 16 | 17 | #endif /* __418_CUDA_STRING_H__ */ 18 | -------------------------------------------------------------------------------- /src/primegen-0.97/error_str.3: -------------------------------------------------------------------------------- 1 | .TH error_str 3 2 | .SH NAME 3 | error_str \- names for syscall error codes 4 | .SH SYNTAX 5 | .B #include 6 | 7 | char *\fBerror_str\fP(\fIe\fR); 8 | 9 | int \fIe\fR; 10 | .SH DESCRIPTION 11 | .B error_str 12 | returns a printable string describing syscall error code 13 | .IR e . 14 | Normally 15 | .I e 16 | is 17 | .BR errno . 18 | .SH "SEE ALSO" 19 | error(3) 20 | -------------------------------------------------------------------------------- /src/primegen-0.97/trylong64.c: -------------------------------------------------------------------------------- 1 | void main() 2 | { 3 | unsigned long u; 4 | u = 1; 5 | u += u; u += u; u += u; u += u; u += u; u += u; u += u; u += u; 6 | u += u; u += u; u += u; u += u; u += u; u += u; u += u; u += u; 7 | u += u; u += u; u += u; u += u; u += u; u += u; u += u; u += u; 8 | u += u; u += u; u += u; u += u; u += u; u += u; u += u; u += u; 9 | if (!u) _exit(1); 10 | _exit(0); 11 | } 12 | -------------------------------------------------------------------------------- /src/primegen-0.97/tryulong32.c: -------------------------------------------------------------------------------- 1 | void main() 2 | { 3 | unsigned long u; 4 | u = 1; 5 | u += u; u += u; u += u; u += u; u += u; u += u; u += u; u += u; 6 | u += u; u += u; u += u; u += u; u += u; u += u; u += u; u += u; 7 | u += u; u += u; u += u; u += u; u += u; u += u; u += u; u += u; 8 | u += u; u += u; u += u; u += u; u += u; u += u; u += u; u += u; 9 | if (!u) _exit(0); 10 | _exit(1); 11 | } 12 | -------------------------------------------------------------------------------- /src/primegen-0.97/tryulong64.c: -------------------------------------------------------------------------------- 1 | void main() 2 | { 3 | unsigned long u; 4 | u = 1; 5 | u += u; u += u; u += u; u += u; u += u; u += u; u += u; u += u; 6 | u += u; u += u; u += u; u += u; u += u; u += u; u += u; u += u; 7 | u += u; u += u; u += u; u += u; u += u; u += u; u += u; u += u; 8 | u += u; u += u; u += u; u += u; u += u; u += u; u += u; u += u; 9 | if (!u) _exit(1); 10 | _exit(0); 11 | } 12 | -------------------------------------------------------------------------------- /src/primegen-0.97/byte_cr.c: -------------------------------------------------------------------------------- 1 | #include "byte.h" 2 | 3 | void byte_copyr(to,n,from) 4 | register char *to; 5 | register unsigned int n; 6 | register char *from; 7 | { 8 | to += n; 9 | from += n; 10 | for (;;) { 11 | if (!n) return; *--to = *--from; --n; 12 | if (!n) return; *--to = *--from; --n; 13 | if (!n) return; *--to = *--from; --n; 14 | if (!n) return; *--to = *--from; --n; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/primegen-0.97/BLURB: -------------------------------------------------------------------------------- 1 | primegen is a small, fast library to generate prime numbers in order. 2 | It generates the 50847534 primes up to 1000000000 in just 8 seconds on a 3 | Pentium II-350; it prints them in decimal in just 35 seconds. 4 | 5 | primegen can generate primes up to 1000000000000000, although it is not 6 | optimized for primes past 32 bits. It uses the Sieve of Atkin instead of 7 | the traditional Sieve of Eratosthenes. 8 | -------------------------------------------------------------------------------- /src/pollard/kernel.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERNEL_H__ 2 | #define __KERNEL_H__ 3 | 4 | #include "mpz.h" 5 | 6 | #define RESULTS_PER_THREAD 2 7 | #define TABLE_SIZE (200 * 1000 * 1000) 8 | #define B_START 2 9 | 10 | #define UL unsigned long 11 | 12 | int parallel_factorize(mpz_t n, unsigned *table, unsigned num_primes, mpz_t *factor); 13 | 14 | int generate_prime_table(unsigned **d_table, unsigned num_primes); 15 | 16 | #endif /* __KERNEL_H__ */ 17 | -------------------------------------------------------------------------------- /src/primegen-0.97/substdio_copy.c: -------------------------------------------------------------------------------- 1 | #include "substdio.h" 2 | 3 | int substdio_copy(ssout,ssin) 4 | register substdio *ssout; 5 | register substdio *ssin; 6 | { 7 | register int n; 8 | register char *x; 9 | 10 | for (;;) { 11 | n = substdio_feed(ssin); 12 | if (n < 0) return -2; 13 | if (!n) return 0; 14 | x = substdio_PEEK(ssin); 15 | if (substdio_put(ssout,x,n) == -1) return -3; 16 | substdio_SEEK(ssin,n); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/primegen-0.97/make-makelib: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # WARNING: This file was auto-generated. Do not edit! 3 | CC='gcc -O1 -fomit-frame-pointer -include /usr/include/errno.h' 4 | LD='gcc' 5 | echo 'main="$1"; shift' 6 | echo 'rm -f "$main"' 7 | echo 'ar cr "$main" ${1+"$@"}' 8 | 9 | case "$1" in 10 | sunos-5.*) ;; 11 | unix_sv*) ;; 12 | irix64-*) ;; 13 | irix-*) ;; 14 | dgux-*) ;; 15 | hp-ux-*) ;; 16 | sco*) ;; 17 | *) 18 | echo 'ranlib "$main"' 19 | ;; 20 | esac 21 | -------------------------------------------------------------------------------- /src/primegen-0.97/README: -------------------------------------------------------------------------------- 1 | primegen 0.97 2 | 19990304 3 | Copyright 1999 4 | D. J. Bernstein, djb@pobox.com 5 | http://pobox.com/~djb/primegen.html 6 | 7 | primegen is a small, fast library to generate prime numbers in order. 8 | See BLURB for a more detailed advertisement. 9 | 10 | INSTALL says how to set up and test primegen. 11 | 12 | The rest of this file is a list of systypes where various versions of 13 | primegen have been reported to work. 14 | 15 | 0.97: bsd.os-2.0.1-:i386-:-:pentium-:- 16 | -------------------------------------------------------------------------------- /src/mpz/README.md: -------------------------------------------------------------------------------- 1 | MPZ 2 | === 3 | 4 | MPZ stands for Multiple Precision Integer (Z = integers in math). 5 | 6 | The MPZ code is structured so that it can be compiled by `nvcc` for use in CUDA 7 | kernels, or by `g++` for use on the cpu. 8 | 9 | To use the MPZ code, just `#include "mpz.h"` in your source file. Note that the 10 | MPZ source code is distributed across `mpz.h`, `digit.h`, and `cuda_string.h`. 11 | 12 | The available mpz functions are documented in mpz.h. 13 | 14 | For examples on how to run on the GPU and CPU, see the Makefile. 15 | -------------------------------------------------------------------------------- /src/primegen-0.97/error.h: -------------------------------------------------------------------------------- 1 | #ifndef ERROR_H 2 | #define ERROR_H 3 | 4 | extern int errno; 5 | 6 | extern int error_intr; 7 | extern int error_nomem; 8 | extern int error_noent; 9 | extern int error_txtbsy; 10 | extern int error_io; 11 | extern int error_exist; 12 | extern int error_timeout; 13 | extern int error_inprogress; 14 | extern int error_wouldblock; 15 | extern int error_again; 16 | extern int error_pipe; 17 | extern int error_perm; 18 | extern int error_acces; 19 | 20 | extern char *error_str(); 21 | extern int error_temp(); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /src/primegen-0.97/fs64.c: -------------------------------------------------------------------------------- 1 | #include "fs64.h" 2 | 3 | unsigned int scan_uint64(char *s,uint64 *u) 4 | { 5 | unsigned int pos = 0; 6 | uint64 result = 0; 7 | uint64 c; 8 | while ((c = (uint64) (unsigned char) (s[pos] - '0')) < 10) { 9 | result = result * 10 + c; 10 | ++pos; 11 | } 12 | *u = result; 13 | return pos; 14 | } 15 | 16 | unsigned int fmt_uint64(char *s,uint64 u) 17 | { 18 | unsigned int len = 1; 19 | uint64 q = u; 20 | while (q > 9) { ++len; q /= 10; } 21 | if (s) { s += len; do { *--s = '0' + (u % 10); u /= 10; } while(u); } 22 | return len; 23 | } 24 | -------------------------------------------------------------------------------- /src/primegen-0.97/primegen_init.c: -------------------------------------------------------------------------------- 1 | #include "primegen.h" 2 | #include "primegen_impl.h" 3 | 4 | void primegen_init(primegen *pg) 5 | { 6 | pg->L = 1; 7 | pg->base = 60; 8 | 9 | pg->pos = PRIMEGEN_WORDS; 10 | 11 | pg->p[0] = 59; 12 | pg->p[1] = 53; 13 | pg->p[2] = 47; 14 | pg->p[3] = 43; 15 | pg->p[4] = 41; 16 | pg->p[5] = 37; 17 | pg->p[6] = 31; 18 | pg->p[7] = 29; 19 | pg->p[8] = 23; 20 | pg->p[9] = 19; 21 | pg->p[10] = 17; 22 | pg->p[11] = 13; 23 | pg->p[12] = 11; 24 | pg->p[13] = 7; 25 | pg->p[14] = 5; 26 | pg->p[15] = 3; 27 | pg->p[16] = 2; 28 | 29 | pg->num = 17; 30 | } 31 | -------------------------------------------------------------------------------- /src/primegen-0.97/primegaps.1: -------------------------------------------------------------------------------- 1 | .TH primegaps 1 2 | .SH NAME 3 | primegaps \- print large prime gaps 4 | .SH SYNTAX 5 | .B primegaps 6 | .SH DESCRIPTION 7 | .B primegaps 8 | searches for large gaps in the sequence of primes. 9 | The gaps between 2, 3, 5, 7, 11, and so on 10 | are 1, 2, 2, 4, and so on. 11 | 12 | When 13 | .B primegaps 14 | sees a new largest gap 15 | .IR n , 16 | from 17 | .I p\fB-\fIn 18 | to 19 | .IR p , 20 | it prints a line 21 | .I p n r 22 | where 23 | .I r 24 | is the approximate ratio between the log of 25 | .I n 26 | and the log of the log of 27 | .IR p . 28 | .SH "SEE ALSO" 29 | primes(1), 30 | primegen(3) 31 | -------------------------------------------------------------------------------- /src/primegen-0.97/man/man1/primegaps.1: -------------------------------------------------------------------------------- 1 | .TH primegaps 1 2 | .SH NAME 3 | primegaps \- print large prime gaps 4 | .SH SYNTAX 5 | .B primegaps 6 | .SH DESCRIPTION 7 | .B primegaps 8 | searches for large gaps in the sequence of primes. 9 | The gaps between 2, 3, 5, 7, 11, and so on 10 | are 1, 2, 2, 4, and so on. 11 | 12 | When 13 | .B primegaps 14 | sees a new largest gap 15 | .IR n , 16 | from 17 | .I p\fB-\fIn 18 | to 19 | .IR p , 20 | it prints a line 21 | .I p n r 22 | where 23 | .I r 24 | is the approximate ratio between the log of 25 | .I n 26 | and the log of the log of 27 | .IR p . 28 | .SH "SEE ALSO" 29 | primes(1), 30 | primegen(3) 31 | -------------------------------------------------------------------------------- /src/primegen-0.97/primegaps.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "primegen.h" 3 | 4 | primegen pg; 5 | 6 | void main() 7 | { 8 | uint64 p; 9 | uint64 lastp; 10 | uint32 diff; 11 | uint32 maxdiff; 12 | 13 | primegen_init(&pg); 14 | 15 | primegen_next(&pg); 16 | lastp = primegen_next(&pg); 17 | maxdiff = 0; 18 | 19 | for (;;) { 20 | p = primegen_next(&pg); 21 | diff = p - lastp; 22 | if (diff > maxdiff) { 23 | printf("%.0f %.0f %f\n" 24 | ,(double) p 25 | ,(double) diff 26 | ,log((double) diff)/log(log((double) p)) 27 | ); 28 | maxdiff = diff; 29 | } 30 | 31 | lastp = p; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/mpz/compile.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file compile.h 3 | */ 4 | #ifndef __418_COMPILE_H__ 5 | #define __418_COMPILE_H__ 6 | 7 | #define true 1 8 | #define false 0 9 | 10 | #ifndef __CUDACC__ /* when compiling with g++ ... */ 11 | 12 | #define __device__ 13 | #define __host__ 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | inline int max(int a, int b) { return (a > b) ? a : b; } 21 | inline int min(int a, int b) { return (a < b) ? a : b; } 22 | inline int abs(int a) { return (a < 0) ? -a : a; } 23 | 24 | #else /* when compiling with nvcc ... */ 25 | 26 | #endif /* __CUDACC__ */ 27 | 28 | #endif /* __418_COMPILE_H__ */ 29 | 30 | -------------------------------------------------------------------------------- /src/primegen-0.97/primegen_impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | B is 32 times X. 3 | Total memory use for one generator is 2B bytes = 64X bytes. 4 | Covers primes in an interval of length 1920X. 5 | Working set size for one generator is B bits = 4X bytes. 6 | 7 | Speedup by a factor of 2 or 3 for L1 cache instead of L2 cache. 8 | Slowdown by a factor of roughly n for primes past (nB)^2. 9 | 10 | Possible choices of X: 11 | 2002 to fit inside an 8K L1 cache (e.g., Pentium). 12 | 4004 to fit inside a 16K L1 cache (e.g., Pentium II). 13 | 64064 to fit inside a 256K L2 cache. 14 | 15 | There are various word-size limits on X; 1000000 should still be okay. 16 | */ 17 | 18 | #define B32 PRIMEGEN_WORDS 19 | #define B (PRIMEGEN_WORDS * 32) 20 | -------------------------------------------------------------------------------- /src/primegen-0.97/primes.1: -------------------------------------------------------------------------------- 1 | .TH primes 1 2 | .SH NAME 3 | primes \- generate prime numbers 4 | .SH SYNTAX 5 | .B primes 6 | [ 7 | .I low 8 | [ 9 | .I high 10 | ] 11 | ] 12 | .SH DESCRIPTION 13 | .B primes 14 | prints the sequence of prime numbers: 15 | 2, 3, 5, 7, 11, and so on, 16 | one per line. 17 | 18 | If 19 | .I low 20 | is supplied, 21 | .B primes 22 | does not print any primes smaller than 23 | .IR low . 24 | 25 | If 26 | .I high 27 | is supplied, 28 | .B primes 29 | does not print any primes larger than 30 | .IR high . 31 | If 32 | .I high 33 | is not supplied, 34 | .B primes 35 | stops at 1000000000; 36 | the last prime is 999999937. 37 | 38 | .B primes 39 | can handle 40 | .I high 41 | as large as 1000000000000000. 42 | .SH "SEE ALSO" 43 | primegen(3) 44 | -------------------------------------------------------------------------------- /src/primegen-0.97/man/man1/primes.1: -------------------------------------------------------------------------------- 1 | .TH primes 1 2 | .SH NAME 3 | primes \- generate prime numbers 4 | .SH SYNTAX 5 | .B primes 6 | [ 7 | .I low 8 | [ 9 | .I high 10 | ] 11 | ] 12 | .SH DESCRIPTION 13 | .B primes 14 | prints the sequence of prime numbers: 15 | 2, 3, 5, 7, 11, and so on, 16 | one per line. 17 | 18 | If 19 | .I low 20 | is supplied, 21 | .B primes 22 | does not print any primes smaller than 23 | .IR low . 24 | 25 | If 26 | .I high 27 | is supplied, 28 | .B primes 29 | does not print any primes larger than 30 | .IR high . 31 | If 32 | .I high 33 | is not supplied, 34 | .B primes 35 | stops at 1000000000; 36 | the last prime is 999999937. 37 | 38 | .B primes 39 | can handle 40 | .I high 41 | as large as 1000000000000000. 42 | .SH "SEE ALSO" 43 | primegen(3) 44 | -------------------------------------------------------------------------------- /src/primegen-0.97/include/primegen.h: -------------------------------------------------------------------------------- 1 | #ifndef PRIMEGEN_H 2 | #define PRIMEGEN_H 3 | 4 | #include "uint32.h" 5 | #include "uint64.h" 6 | 7 | #define PRIMEGEN_WORDS 2048 8 | 9 | typedef struct { 10 | uint32 buf[16][PRIMEGEN_WORDS]; 11 | uint64 p[512]; /* p[num-1] ... p[0], in that order */ 12 | int num; 13 | int pos; /* next entry to use in buf; WORDS to restart */ 14 | uint64 base; 15 | uint64 L; 16 | } primegen; 17 | 18 | extern void primegen_sieve(primegen *); 19 | extern void primegen_fill(primegen *); 20 | 21 | extern void primegen_init(primegen *); 22 | extern uint64 primegen_next(primegen *); 23 | extern uint64 primegen_peek(primegen *); 24 | extern uint64 primegen_count(primegen *,uint64 to); 25 | extern void primegen_skipto(primegen *,uint64 to); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /src/primegen-0.97/primegen.h.sed: -------------------------------------------------------------------------------- 1 | #ifndef PRIMEGEN_H 2 | #define PRIMEGEN_H 3 | 4 | #include "uint32.h" 5 | #include "uint64.h" 6 | 7 | #define PRIMEGEN_WORDS conf-words 8 | 9 | typedef struct { 10 | uint32 buf[16][PRIMEGEN_WORDS]; 11 | uint64 p[512]; /* p[num-1] ... p[0], in that order */ 12 | int num; 13 | int pos; /* next entry to use in buf; WORDS to restart */ 14 | uint64 base; 15 | uint64 L; 16 | } primegen; 17 | 18 | extern void primegen_sieve(primegen *); 19 | extern void primegen_fill(primegen *); 20 | 21 | extern void primegen_init(primegen *); 22 | extern uint64 primegen_next(primegen *); 23 | extern uint64 primegen_peek(primegen *); 24 | extern uint64 primegen_count(primegen *,uint64 to); 25 | extern void primegen_skipto(primegen *,uint64 to); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /src/primegen-0.97/primegaps.0: -------------------------------------------------------------------------------- 1 | primegaps(1) primegaps(1) 2 | 3 | 4 | 5 | NNAAMMEE 6 | primegaps - print large prime gaps 7 | 8 | SSYYNNTTAAXX 9 | pprriimmeeggaappss 10 | 11 | DDEESSCCRRIIPPTTIIOONN 12 | pprriimmeeggaappss searches for large gaps in the sequence of primes. The gaps 13 | between 2, 3, 5, 7, 11, and so on are 1, 2, 2, 4, and so on. 14 | 15 | When pprriimmeeggaappss sees a new largest gap _n, from _p--_n to _p, it prints a 16 | line _p _n _r where _r is the approximate ratio between the log of _n and 17 | the log of the log of _p. 18 | 19 | SSEEEE AALLSSOO 20 | primes(1), primegen(3) 21 | 22 | 23 | 24 | primegaps(1) 25 | -------------------------------------------------------------------------------- /src/primegen-0.97/man/cat1/primegaps.0: -------------------------------------------------------------------------------- 1 | primegaps(1) primegaps(1) 2 | 3 | 4 | 5 | NNAAMMEE 6 | primegaps - print large prime gaps 7 | 8 | SSYYNNTTAAXX 9 | pprriimmeeggaappss 10 | 11 | DDEESSCCRRIIPPTTIIOONN 12 | pprriimmeeggaappss searches for large gaps in the sequence of primes. The gaps 13 | between 2, 3, 5, 7, 11, and so on are 1, 2, 2, 4, and so on. 14 | 15 | When pprriimmeeggaappss sees a new largest gap _n, from _p--_n to _p, it prints a 16 | line _p _n _r where _r is the approximate ratio between the log of _n and 17 | the log of the log of _p. 18 | 19 | SSEEEE AALLSSOO 20 | primes(1), primegen(3) 21 | 22 | 23 | 24 | primegaps(1) 25 | -------------------------------------------------------------------------------- /src/primegen-0.97/primespeed.1: -------------------------------------------------------------------------------- 1 | .TH primespeed 1 2 | .SH NAME 3 | primespeed \- count prime numbers 4 | .SH SYNTAX 5 | .B primespeed 6 | [ \fIhigh\fP ] 7 | .SH DESCRIPTION 8 | .B primespeed 9 | counts the prime numbers between 1 and 10 | .I high 11 | exclusive. 12 | If 13 | .I high 14 | is not supplied, 15 | .B primespeed 16 | stops at 1000000000. 17 | 18 | .B primespeed 19 | uses 64-bit arithmetic; 20 | .I high 21 | can be as large as 10^15. 22 | There are 23 | 50847534 primes up to 10^9; 24 | 455052511 up to 10^10; 25 | 4118054813 up to 10^11; 26 | 37607912018 up to 10^12; 27 | 346065536839 up to 10^13; 28 | 3204941750802 up to 10^14; and 29 | 29844570422669 up to 10^15. 30 | 31 | .B primespeed 32 | prints the number of primes 33 | and various information about the time it used. 34 | There are faster ways to count primes; 35 | the purpose of 36 | .B primespeed 37 | is to measure the speed of the 38 | .B primegen 39 | library. 40 | .SH "SEE ALSO" 41 | primegen(3) 42 | -------------------------------------------------------------------------------- /src/primegen-0.97/auto-str.c: -------------------------------------------------------------------------------- 1 | #include "substdio.h" 2 | #include "readwrite.h" 3 | #include "exit.h" 4 | 5 | char buf1[256]; 6 | substdio ss1 = SUBSTDIO_FDBUF(write,1,buf1,sizeof(buf1)); 7 | 8 | void puts(s) 9 | char *s; 10 | { 11 | if (substdio_puts(&ss1,s) == -1) _exit(111); 12 | } 13 | 14 | void main(argc,argv) 15 | int argc; 16 | char **argv; 17 | { 18 | char *name; 19 | char *value; 20 | unsigned char ch; 21 | char octal[4]; 22 | 23 | name = argv[1]; 24 | if (!name) _exit(100); 25 | value = argv[2]; 26 | if (!value) _exit(100); 27 | 28 | puts("char "); 29 | puts(name); 30 | puts("[] = \"\\\n"); 31 | 32 | while (ch = *value++) { 33 | puts("\\"); 34 | octal[3] = 0; 35 | octal[2] = '0' + (ch & 7); ch >>= 3; 36 | octal[1] = '0' + (ch & 7); ch >>= 3; 37 | octal[0] = '0' + (ch & 7); 38 | puts(octal); 39 | } 40 | 41 | puts("\\\n\";\n"); 42 | if (substdio_flush(&ss1) == -1) _exit(111); 43 | _exit(0); 44 | } 45 | -------------------------------------------------------------------------------- /src/primegen-0.97/primegen.h: -------------------------------------------------------------------------------- 1 | #ifndef PRIMEGEN_H 2 | #define PRIMEGEN_H 3 | 4 | #include "uint32.h" 5 | #include "uint64.h" 6 | 7 | #define PRIMEGEN_WORDS 2048 8 | 9 | typedef struct { 10 | uint32 buf[16][PRIMEGEN_WORDS]; 11 | uint64 p[512]; /* p[num-1] ... p[0], in that order */ 12 | int num; 13 | int pos; /* next entry to use in buf; WORDS to restart */ 14 | uint64 base; 15 | uint64 L; 16 | } primegen; 17 | 18 | #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */ 19 | extern "C" { 20 | #endif 21 | 22 | extern void primegen_sieve(primegen *); 23 | extern void primegen_fill(primegen *); 24 | 25 | extern void primegen_init(primegen *); 26 | extern uint64 primegen_next(primegen *); 27 | extern uint64 primegen_peek(primegen *); 28 | extern uint64 primegen_count(primegen *,uint64 to); 29 | extern void primegen_skipto(primegen *,uint64 to); 30 | 31 | #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */ 32 | } 33 | #endif 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /src/primegen-0.97/hier.c: -------------------------------------------------------------------------------- 1 | #include "auto_home.h" 2 | 3 | void hier() 4 | { 5 | h(auto_home,-1,-1,0755); 6 | 7 | d(auto_home,"bin",-1,-1,0755); 8 | d(auto_home,"lib",-1,-1,0755); 9 | d(auto_home,"include",-1,-1,0755); 10 | d(auto_home,"man",-1,-1,0755); 11 | d(auto_home,"man/man1",-1,-1,0755); 12 | d(auto_home,"man/cat1",-1,-1,0755); 13 | d(auto_home,"man/man3",-1,-1,0755); 14 | d(auto_home,"man/cat3",-1,-1,0755); 15 | 16 | c(auto_home,"include","primegen.h",-1,-1,0644); 17 | c(auto_home,"lib","primegen.a",-1,-1,0644); 18 | c(auto_home,"bin","primes",-1,-1,0755); 19 | c(auto_home,"bin","primegaps",-1,-1,0755); 20 | 21 | c(auto_home,"man/man1","primes.1",-1,-1,0644); 22 | c(auto_home,"man/man1","primegaps.1",-1,-1,0644); 23 | c(auto_home,"man/man3","primegen.3",-1,-1,0644); 24 | 25 | c(auto_home,"man/cat1","primes.0",-1,-1,0644); 26 | c(auto_home,"man/cat1","primegaps.0",-1,-1,0644); 27 | c(auto_home,"man/cat3","primegen.0",-1,-1,0644); 28 | } 29 | -------------------------------------------------------------------------------- /src/primegen-0.97/error.3: -------------------------------------------------------------------------------- 1 | .TH error 3 2 | .SH NAME 3 | error \- syscall error codes 4 | .SH SYNTAX 5 | .B #include 6 | 7 | extern int \fBerrno\fP; 8 | 9 | extern int \fBerror_intr\fP; 10 | .br 11 | extern int \fBerror_nomem\fP; 12 | .br 13 | extern int \fBerror_noent\fP; 14 | .br 15 | extern int \fBerror_txtbsy\fP; 16 | .br 17 | extern int \fBerror_io\fP; 18 | .br 19 | extern int \fBerror_exist\fP; 20 | .br 21 | extern int \fBerror_timeout\fP; 22 | .br 23 | extern int \fBerror_inprogress\fP; 24 | .br 25 | extern int \fBerror_wouldblock\fP; 26 | .br 27 | extern int \fBerror_again\fP; 28 | .br 29 | extern int \fBerror_pipe\fP; 30 | .br 31 | extern int \fBerror_perm\fP; 32 | .br 33 | extern int \fBerror_acces\fP; 34 | .SH DESCRIPTION 35 | UNIX syscalls provide detailed error codes in the 36 | .B errno 37 | variable. 38 | The 39 | .B error 40 | library provides portable names for a variety of possible 41 | .B errno 42 | values. 43 | .SH "SEE ALSO" 44 | error_str(3), 45 | error_temp(3) 46 | -------------------------------------------------------------------------------- /src/primegen-0.97/TARGETS: -------------------------------------------------------------------------------- 1 | primes.0 2 | primespeed.0 3 | primegaps.0 4 | primegen.0 5 | man 6 | auto-ccld.sh 7 | make-load 8 | find-systype 9 | systype 10 | load 11 | make-compile 12 | compile 13 | primegen.h 14 | uint32.h 15 | uint64.h 16 | primes.o 17 | fs64.o 18 | make-makelib 19 | makelib 20 | int64.h 21 | primegen.o 22 | primegen_init.o 23 | primegen_next.o 24 | primegen_skip.o 25 | primegen.a 26 | primes 27 | hasrdtsc.h 28 | hasgethr.h 29 | primespeed.o 30 | primespeed 31 | primegaps.o 32 | primegaps 33 | eratspeed.o 34 | eratspeed 35 | prog 36 | it 37 | install.o 38 | hier.o 39 | auto-str.o 40 | substdio.o 41 | substdi.o 42 | substdo.o 43 | subfderr.o 44 | substdio_copy.o 45 | substdio.a 46 | error.o 47 | error_str.o 48 | error.a 49 | str_len.o 50 | byte_copy.o 51 | byte_cr.o 52 | str.a 53 | auto-str 54 | auto_home.c 55 | auto_home.o 56 | strerr_sys.o 57 | strerr_die.o 58 | strerr.a 59 | open_read.o 60 | open_trunc.o 61 | open.a 62 | install 63 | setup 64 | instcheck.o 65 | instcheck 66 | check 67 | -------------------------------------------------------------------------------- /src/primegen-0.97/primes.0: -------------------------------------------------------------------------------- 1 | primes(1) primes(1) 2 | 3 | 4 | 5 | NNAAMMEE 6 | primes - generate prime numbers 7 | 8 | SSYYNNTTAAXX 9 | pprriimmeess [ _l_o_w [ _h_i_g_h ] ] 10 | 11 | DDEESSCCRRIIPPTTIIOONN 12 | pprriimmeess prints the sequence of prime numbers: 2, 3, 5, 7, 11, and so on, 13 | one per line. 14 | 15 | If _l_o_w is supplied, pprriimmeess does not print any primes smaller than _l_o_w. 16 | 17 | If _h_i_g_h is supplied, pprriimmeess does not print any primes larger than _h_i_g_h. 18 | If _h_i_g_h is not supplied, pprriimmeess stops at 1000000000; the last prime is 19 | 999999937. 20 | 21 | pprriimmeess can handle _h_i_g_h as large as 1000000000000000. 22 | 23 | SSEEEE AALLSSOO 24 | primegen(3) 25 | 26 | 27 | 28 | primes(1) 29 | -------------------------------------------------------------------------------- /src/primegen-0.97/man/cat1/primes.0: -------------------------------------------------------------------------------- 1 | primes(1) primes(1) 2 | 3 | 4 | 5 | NNAAMMEE 6 | primes - generate prime numbers 7 | 8 | SSYYNNTTAAXX 9 | pprriimmeess [ _l_o_w [ _h_i_g_h ] ] 10 | 11 | DDEESSCCRRIIPPTTIIOONN 12 | pprriimmeess prints the sequence of prime numbers: 2, 3, 5, 7, 11, and so on, 13 | one per line. 14 | 15 | If _l_o_w is supplied, pprriimmeess does not print any primes smaller than _l_o_w. 16 | 17 | If _h_i_g_h is supplied, pprriimmeess does not print any primes larger than _h_i_g_h. 18 | If _h_i_g_h is not supplied, pprriimmeess stops at 1000000000; the last prime is 19 | 999999937. 20 | 21 | pprriimmeess can handle _h_i_g_h as large as 1000000000000000. 22 | 23 | SSEEEE AALLSSOO 24 | primegen(3) 25 | 26 | 27 | 28 | primes(1) 29 | -------------------------------------------------------------------------------- /src/primegen-0.97/strerr_die.c: -------------------------------------------------------------------------------- 1 | #include "substdio.h" 2 | #include "subfd.h" 3 | #include "exit.h" 4 | #include "strerr.h" 5 | 6 | void strerr_warn(x1,x2,x3,x4,x5,x6,se) 7 | char *x1; char *x2; char *x3; char *x4; char *x5; char *x6; 8 | struct strerr *se; 9 | { 10 | strerr_sysinit(); 11 | 12 | if (x1) substdio_puts(subfderr,x1); 13 | if (x2) substdio_puts(subfderr,x2); 14 | if (x3) substdio_puts(subfderr,x3); 15 | if (x4) substdio_puts(subfderr,x4); 16 | if (x5) substdio_puts(subfderr,x5); 17 | if (x6) substdio_puts(subfderr,x6); 18 | 19 | while(se) { 20 | if (se->x) substdio_puts(subfderr,se->x); 21 | if (se->y) substdio_puts(subfderr,se->y); 22 | if (se->z) substdio_puts(subfderr,se->z); 23 | se = se->who; 24 | } 25 | 26 | substdio_puts(subfderr,"\n"); 27 | substdio_flush(subfderr); 28 | } 29 | 30 | void strerr_die(e,x1,x2,x3,x4,x5,x6,se) 31 | int e; 32 | char *x1; char *x2; char *x3; char *x4; char *x5; char *x6; 33 | struct strerr *se; 34 | { 35 | strerr_warn(x1,x2,x3,x4,x5,x6,se); 36 | _exit(e); 37 | } 38 | -------------------------------------------------------------------------------- /src/primegen-0.97/timing.h: -------------------------------------------------------------------------------- 1 | #ifndef TIMING_H 2 | #define TIMING_H 3 | 4 | #include "hasrdtsc.h" 5 | #include "hasgethr.h" 6 | #include 7 | #include 8 | 9 | typedef struct timeval timing_basic; 10 | #define timing_basic_now(x) gettimeofday((x),(struct timezone *) 0) 11 | #define timing_basic_diff(x,y) (1000.0 * ((x)->tv_usec - (double) (y)->tv_usec) + 1000000000.0 * ((x)->tv_sec - (double) (y)->tv_sec)) 12 | 13 | 14 | #ifdef HASRDTSC 15 | 16 | typedef struct { unsigned long t[2]; } timing; 17 | #define timing_now(x) asm volatile(".byte 15;.byte 49" : "=a"((x)->t[0]),"=d"((x)->t[1])) 18 | #define timing_diff(x,y) (((x)->t[0] - (double) (y)->t[0]) + 4294967296.0 * ((x)->t[1] - (double) (y)->t[1])) 19 | 20 | #else 21 | #ifdef HASGETHRTIME 22 | 23 | typedef struct { hrtime_t t; } timing; 24 | #define timing_now(x) ((x)->t = gethrtime()) 25 | #define timing_diff(x,y) ((double) ((x)->t - (y)->t)) 26 | 27 | #else 28 | 29 | #define timing timing_basic 30 | #define timing_now timing_basic_now 31 | #define timing_diff timing_basic_diff 32 | 33 | #endif 34 | #endif 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /src/primegen-0.97/INSTALL: -------------------------------------------------------------------------------- 1 | Like any other piece of software (and information generally), primegen 2 | comes with NO WARRANTY. 3 | 4 | 5 | Things you have to decide before starting: 6 | 7 | * The primegen home directory, normally /usr/local. To change this 8 | directory, edit conf-home now. 9 | 10 | * The compiler to use, normally gcc -O1 -fomit-frame-pointer. To change 11 | this compiler, edit conf-cc now. 12 | 13 | * The number of 32-bit words of space used in the primegen inner loop, 14 | normally 2048. To change this number, edit conf-words now. 15 | 16 | 17 | How to install: 18 | 19 | 1. Compile the programs and create the formatted man pages: 20 | % make 21 | 22 | 2. Install the programs and man pages: 23 | # make setup check 24 | 25 | 26 | How to test: 27 | 28 | 3. If you have the md5 program installed: 29 | % ./primes 1 100000000 | md5 30 | The result should be 4e2b0027288a27e9c99699364877c9db. (There are 31 | 5761455 primes up to 10^8.) 32 | 33 | 34 | That's it! To report success: 35 | % ( echo 'First M. Last'; cat `cat SYSDEPS` ) | mail djb-qst@cr.yp.to 36 | Replace First M. Last with your name. 37 | -------------------------------------------------------------------------------- /src/primegen-0.97/substdio.h: -------------------------------------------------------------------------------- 1 | #ifndef SUBSTDIO_H 2 | #define SUBSTDIO_H 3 | 4 | typedef struct substdio { 5 | char *x; 6 | int p; 7 | int n; 8 | int fd; 9 | int (*op)(); 10 | } substdio; 11 | 12 | #define SUBSTDIO_FDBUF(op,fd,buf,len) { (buf), 0, (len), (fd), (op) } 13 | 14 | extern void substdio_fdbuf(); 15 | 16 | extern int substdio_flush(); 17 | extern int substdio_put(); 18 | extern int substdio_bput(); 19 | extern int substdio_putflush(); 20 | extern int substdio_puts(); 21 | extern int substdio_bputs(); 22 | extern int substdio_putsflush(); 23 | 24 | extern int substdio_get(); 25 | extern int substdio_bget(); 26 | extern int substdio_feed(); 27 | 28 | extern char *substdio_peek(); 29 | extern void substdio_seek(); 30 | 31 | #define substdio_fileno(s) ((s)->fd) 32 | 33 | #define SUBSTDIO_INSIZE 8192 34 | #define SUBSTDIO_OUTSIZE 8192 35 | 36 | #define substdio_PEEK(s) ( (s)->x + (s)->n ) 37 | #define substdio_SEEK(s,len) ( ( (s)->p -= (len) ) , ( (s)->n += (len) ) ) 38 | 39 | #define substdio_BPUTC(s,c) \ 40 | ( ((s)->n != (s)->p) \ 41 | ? ( (s)->x[(s)->p++] = (c), 0 ) \ 42 | : substdio_bput((s),&(c),1) \ 43 | ) 44 | 45 | extern int substdio_copy(); 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /src/mpz/Makefile: -------------------------------------------------------------------------------- 1 | ##################################################################### 2 | # 3 | # Multiple Precision Integers Library Makefile 4 | # 5 | ##################################################################### 6 | 7 | CXX = g++ -m64 8 | CXXFLAGS = -g -DDEBUG -Wall -Wextra -I/usr/local/cuda/include -I../include 9 | 10 | NVCC = nvcc 11 | NVCCFLAGS = -g -G -m64 -arch compute_20 12 | 13 | LIBS += cudart 14 | LDLIBS = $(addprefix -l, $(LIBS)) 15 | LDFLAGS = -L/usr/local/cuda/lib64/ -lcudart 16 | 17 | ### 18 | # Files we don't compile with directly, but we want Make to know that we 19 | # depend on them 20 | ### 21 | OTHER_FILES = cuda_string.h digit.h mpz.h 22 | 23 | default: cpu 24 | 25 | ######################################################## 26 | # # 27 | # CPU test program # 28 | # # 29 | ######################################################## 30 | cpu: $(OTHER_FILES) cpu_main.c 31 | mkdir -p obj 32 | $(CXX) $(CXXFLAGS) cpu_main.c -c -o obj/cpu_main.o 33 | $(CXX) $(CXXFLAGS) obj/cpu_main.o -o cpu 34 | ./cpu 35 | 36 | %.c: 37 | %.h: 38 | %.cu: 39 | %.cpp: 40 | 41 | clean: 42 | rm -rf obj/ 43 | rm -f gpu 44 | rm -f cpu 45 | -------------------------------------------------------------------------------- /src/primegen-0.97/FILES: -------------------------------------------------------------------------------- 1 | BLURB 2 | README 3 | TODO 4 | THANKS 5 | CHANGES 6 | FILES 7 | TARGETS 8 | VERSION 9 | SYSDEPS 10 | Makefile 11 | INSTALL 12 | primes.1 13 | primes.c 14 | primespeed.1 15 | primespeed.c 16 | primegaps.1 17 | primegaps.c 18 | primegen.3 19 | conf-words 20 | primegen.h.sed 21 | primegen_impl.h 22 | primegen.c 23 | primegen_init.c 24 | primegen_next.c 25 | primegen_skip.c 26 | eratspeed.c 27 | fs64.h 28 | fs64.c 29 | math.lib 30 | hier.c 31 | conf-home 32 | auto-str.c 33 | auto_home.h 34 | install.c 35 | instcheck.c 36 | conf-cc 37 | conf-ld 38 | find-systype.sh 39 | make-compile.sh 40 | make-load.sh 41 | make-makelib.sh 42 | trycpp.c 43 | warn-auto.sh 44 | substdio.h 45 | substdio.c 46 | substdi.c 47 | substdo.c 48 | substdio_copy.c 49 | subfd.h 50 | subfderr.c 51 | readwrite.h 52 | exit.h 53 | strerr.h 54 | strerr_sys.c 55 | strerr_die.c 56 | error.3 57 | error_str.3 58 | error.h 59 | error.c 60 | error_str.c 61 | open.h 62 | open_read.c 63 | open_trunc.c 64 | byte.h 65 | byte_copy.c 66 | byte_cr.c 67 | str.h 68 | str_len.c 69 | uint32.h1 70 | uint32.h2 71 | tryulong32.c 72 | uint64.h1 73 | uint64.h2 74 | tryulong64.c 75 | int64.h1 76 | int64.h2 77 | trylong64.c 78 | timing.h 79 | tryrdtsc.c 80 | trygethr.c 81 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # 3 | # 15-418 Final Project Makefile 4 | # 5 | # AJ Kaufmann 6 | # David Matlack 7 | # 8 | ############################################################################### 9 | 10 | SRC_DIR = src 11 | OBJ_DIR = obj 12 | 13 | CXX = g++ -m64 14 | 15 | CXXFLAGS = -g -DDEBUG -Wall -Wextra \ 16 | -I/usr/local/cuda/include \ 17 | -I/usr/local/cuda/CUDALibraries/common/inc \ 18 | -I$(SRC_DIR)/primegen-0.97 \ 19 | -I$(SRC_DIR)/mpz/ 20 | 21 | NVCC = nvcc 22 | NVCCFLAGS = -g -G -m64 -arch compute_20 \ 23 | -I$(SRC_DIR)/mpz 24 | 25 | LDFLAGS = -L/usr/local/cuda/lib64/ -lcudart -L../primegen-0.97/lib -lprimegen 26 | 27 | default: mpz 28 | 29 | ################################################# 30 | # MPZ Library # 31 | ################################################# 32 | 33 | MPZ_SRC = \ 34 | mpz/compile.h \ 35 | mpz/cuda_string.h \ 36 | mpz/digit.h \ 37 | mpz/mpz.h 38 | 39 | mpz: 40 | mkdir -p obj 41 | $(CXX) $(CXXFLAGS) $(SRC_DIR)/mpz/cpu_main.c -c -o $(OBJ_DIR)/cpu_main.o 42 | $(CXX) $(CXXFLAGS) $(OBJ_DIR)/cpu_main.o -o mpz_test 43 | ./mpz_test 44 | 45 | clean: 46 | rm -f mpz_test 47 | rm -rf $(OBJ_DIR) 48 | -------------------------------------------------------------------------------- /src/primegen-0.97/primespeed.0: -------------------------------------------------------------------------------- 1 | primespeed(1) primespeed(1) 2 | 3 | 4 | 5 | NNAAMMEE 6 | primespeed - count prime numbers 7 | 8 | SSYYNNTTAAXX 9 | pprriimmeessppeeeedd [ _h_i_g_h ] 10 | 11 | DDEESSCCRRIIPPTTIIOONN 12 | pprriimmeessppeeeedd counts the prime numbers between 1 and _h_i_g_h exclusive. If 13 | _h_i_g_h is not supplied, pprriimmeessppeeeedd stops at 1000000000. 14 | 15 | pprriimmeessppeeeedd uses 64-bit arithmetic; _h_i_g_h can be as large as 10^15. 16 | There are 50847534 primes up to 10^9; 455052511 up to 10^10; 4118054813 17 | up to 10^11; 37607912018 up to 10^12; 346065536839 up to 10^13; 18 | 3204941750802 up to 10^14; and 29844570422669 up to 10^15. 19 | 20 | pprriimmeessppeeeedd prints the number of primes and various information about 21 | the time it used. There are faster ways to count primes; the purpose 22 | of pprriimmeessppeeeedd is to measure the speed of the pprriimmeeggeenn library. 23 | 24 | SSEEEE AALLSSOO 25 | primegen(3) 26 | 27 | 28 | 29 | primespeed(1) 30 | -------------------------------------------------------------------------------- /src/primegen-0.97/error.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "error.h" 3 | 4 | /* warning: as coverage improves here, should update error_{str,temp} */ 5 | 6 | int error_intr = 7 | #ifdef EINTR 8 | EINTR; 9 | #else 10 | -1; 11 | #endif 12 | 13 | int error_nomem = 14 | #ifdef ENOMEM 15 | ENOMEM; 16 | #else 17 | -2; 18 | #endif 19 | 20 | int error_noent = 21 | #ifdef ENOENT 22 | ENOENT; 23 | #else 24 | -3; 25 | #endif 26 | 27 | int error_txtbsy = 28 | #ifdef ETXTBSY 29 | ETXTBSY; 30 | #else 31 | -4; 32 | #endif 33 | 34 | int error_io = 35 | #ifdef EIO 36 | EIO; 37 | #else 38 | -5; 39 | #endif 40 | 41 | int error_exist = 42 | #ifdef EEXIST 43 | EEXIST; 44 | #else 45 | -6; 46 | #endif 47 | 48 | int error_timeout = 49 | #ifdef ETIMEDOUT 50 | ETIMEDOUT; 51 | #else 52 | -7; 53 | #endif 54 | 55 | int error_inprogress = 56 | #ifdef EINPROGRESS 57 | EINPROGRESS; 58 | #else 59 | -8; 60 | #endif 61 | 62 | int error_wouldblock = 63 | #ifdef EWOULDBLOCK 64 | EWOULDBLOCK; 65 | #else 66 | -9; 67 | #endif 68 | 69 | int error_again = 70 | #ifdef EAGAIN 71 | EAGAIN; 72 | #else 73 | -10; 74 | #endif 75 | 76 | int error_pipe = 77 | #ifdef EPIPE 78 | EPIPE; 79 | #else 80 | -11; 81 | #endif 82 | 83 | int error_perm = 84 | #ifdef EPERM 85 | EPERM; 86 | #else 87 | -12; 88 | #endif 89 | 90 | int error_acces = 91 | #ifdef EACCES 92 | EACCES; 93 | #else 94 | -13; 95 | #endif 96 | -------------------------------------------------------------------------------- /src/pollard/Makefile: -------------------------------------------------------------------------------- 1 | ##################################################################### 2 | # 3 | # Multiple Precision Integers Library Makefile 4 | # 5 | ##################################################################### 6 | 7 | CXX = g++ -m64 8 | CXXFLAGS = -g -DDEBUG -Wall -Wextra \ 9 | -I/usr/local/cuda/include \ 10 | -I../mpz \ 11 | -I/usr/local/cuda/CUDALibraries/common/inc 12 | 13 | NVCC = nvcc 14 | NVCCFLAGS = -g -G -m64 -arch compute_20 \ 15 | -I../primegen-0.97 \ 16 | -I../mpz 17 | 18 | LIBS += cudart primegen 19 | LDLIBS = $(addprefix -l, $(LIBS)) 20 | LDFLAGS = -L/usr/local/cuda/lib64/ -L../primegen-0.97/lib 21 | 22 | ### 23 | # Files we don't compile with directly, but we want Make to know that we 24 | # depend on them 25 | ### 26 | OTHER_FILES = kernel.h ../mpz/*.h 27 | 28 | default: dirs gpu 29 | 30 | ######################################################## 31 | # # 32 | # GPU test program # 33 | # # 34 | ######################################################## 35 | gpu: $(OTHER_FILES) kernel.cu gpu_main.c 36 | $(NVCC) $(NVCCFLAGS) kernel.cu -c -o obj/kernel.o 37 | $(CXX) $(CXXFLAGS) $(LDFLAGS) gpu_main.c -c -o obj/gpu_main.o $(LDLIBS) 38 | $(CXX) $(LDFLAGS) obj/gpu_main.o obj/kernel.o -o gpu $(LDLIBS) 39 | 40 | 41 | ######################################################## 42 | # # 43 | # CPU test program # 44 | # # 45 | ######################################################## 46 | dirs: 47 | mkdir -p obj 48 | 49 | %.c: 50 | %.h: 51 | %.cu: 52 | %.cpp: 53 | 54 | clean: 55 | rm -rf obj/ 56 | rm -f gpu 57 | rm -f cpu 58 | -------------------------------------------------------------------------------- /src/mpz/kernel.cu: -------------------------------------------------------------------------------- 1 | /** 2 | * @file kernel.cu 3 | * 4 | * @brief The CUDA kernel that will run. 5 | */ 6 | #include "kernel.h" 7 | 8 | #include "mpz.h" // multiple precision cuda code 9 | #include "cuda_string.h" 10 | #include 11 | 12 | char *devA; 13 | char *devB; 14 | char *devC; 15 | 16 | /** 17 | * for i in 0...count: 18 | * devC[i] = devA[i] + devB[i] 19 | */ 20 | __global__ void additionKernel(char *devA, char *devB, char *devC, unsigned count) { 21 | char str[STRING_MAX_SIZE]; 22 | char *global_str; 23 | int threadId = threadIdx.x; 24 | int numThreads = blockDim.x; 25 | int index; 26 | mpz_t sum; 27 | mpz_t op1; 28 | mpz_t op2; 29 | 30 | mpz_init(&op1); 31 | mpz_init(&op2); 32 | mpz_init(&sum); 33 | 34 | for (index = threadId; index < count; index += numThreads) { 35 | mpz_set_str(&op1, devA + (index * STRING_MAX_SIZE)); 36 | mpz_set_str(&op2, devB + (index * STRING_MAX_SIZE)); 37 | 38 | mpz_add(&sum, &op1, &op2); 39 | 40 | mpz_get_str(&sum, str, STRING_MAX_SIZE); 41 | 42 | global_str = devC + (index * STRING_MAX_SIZE); 43 | 44 | memcpy(global_str, str, cuda_strlen(str) + 1); 45 | } 46 | 47 | mpz_destroy(&sum); 48 | mpz_destroy(&op1); 49 | mpz_destroy(&op2); 50 | } 51 | 52 | void run_addition_kernel(char *A, char *B, char *C, unsigned num_strings) { 53 | size_t size = num_strings * STRING_MAX_SIZE; 54 | 55 | cudaMalloc(&devA, size); 56 | cudaMalloc(&devB, size); 57 | cudaMalloc(&devC, size); 58 | 59 | cudaMemset(&devA, 0, size); 60 | cudaMemset(&devB, 0, size); 61 | cudaMemset(&devC, 0, size); 62 | 63 | cudaMemcpy(devA, A, size, cudaMemcpyHostToDevice); 64 | cudaMemcpy(devB, B, size, cudaMemcpyHostToDevice); 65 | 66 | additionKernel<<<1,32>>>(devA, devB, devC, num_strings); 67 | 68 | cudaMemcpy(C, devC, size, cudaMemcpyDeviceToHost); 69 | } 70 | -------------------------------------------------------------------------------- /src/primegen-0.97/substdi.c: -------------------------------------------------------------------------------- 1 | #include "substdio.h" 2 | #include "byte.h" 3 | #include "error.h" 4 | 5 | static int oneread(op,fd,buf,len) 6 | register int (*op)(); 7 | register int fd; 8 | register char *buf; 9 | register int len; 10 | { 11 | register int r; 12 | 13 | for (;;) { 14 | r = op(fd,buf,len); 15 | if (r == -1) if (errno == error_intr) continue; 16 | return r; 17 | } 18 | } 19 | 20 | static int getthis(s,buf,len) 21 | register substdio *s; 22 | register char *buf; 23 | register int len; 24 | { 25 | register int r; 26 | register int q; 27 | 28 | r = s->p; 29 | q = r - len; 30 | if (q > 0) { r = len; s->p = q; } else s->p = 0; 31 | byte_copy(buf,r,s->x + s->n); 32 | s->n += r; 33 | return r; 34 | } 35 | 36 | int substdio_feed(s) 37 | register substdio *s; 38 | { 39 | register int r; 40 | register int q; 41 | 42 | if (s->p) return s->p; 43 | q = s->n; 44 | r = oneread(s->op,s->fd,s->x,q); 45 | if (r <= 0) return r; 46 | s->p = r; 47 | q -= r; 48 | s->n = q; 49 | if (q > 0) /* damn, gotta shift */ byte_copyr(s->x + q,r,s->x); 50 | return r; 51 | } 52 | 53 | int substdio_bget(s,buf,len) 54 | register substdio *s; 55 | register char *buf; 56 | register int len; 57 | { 58 | register int r; 59 | 60 | if (s->p > 0) return getthis(s,buf,len); 61 | r = s->n; if (r <= len) return oneread(s->op,s->fd,buf,r); 62 | r = substdio_feed(s); if (r <= 0) return r; 63 | return getthis(s,buf,len); 64 | } 65 | 66 | int substdio_get(s,buf,len) 67 | register substdio *s; 68 | register char *buf; 69 | register int len; 70 | { 71 | register int r; 72 | 73 | if (s->p > 0) return getthis(s,buf,len); 74 | if (s->n <= len) return oneread(s->op,s->fd,buf,len); 75 | r = substdio_feed(s); if (r <= 0) return r; 76 | return getthis(s,buf,len); 77 | } 78 | 79 | char *substdio_peek(s) 80 | register substdio *s; 81 | { 82 | return s->x + s->n; 83 | } 84 | 85 | void substdio_seek(s,len) 86 | register substdio *s; 87 | register int len; 88 | { 89 | s->n += len; 90 | s->p -= len; 91 | } 92 | -------------------------------------------------------------------------------- /src/primegen-0.97/primegen.3: -------------------------------------------------------------------------------- 1 | .TH primegen 3 2 | .SH NAME 3 | primegen \- enumerate small primes 4 | .SH SYNTAX 5 | .B #include 6 | 7 | void \fBprimegen_init\fP(&\fIpg\fR); 8 | .br 9 | uint64 \fBprimegen_next\fP(&\fIpg\fR); 10 | .br 11 | uint64 \fBprimegen_peek\fP(&\fIpg\fR); 12 | .br 13 | uint64 \fBprimegen_count\fP(&\fIpg\fR,\fIbound\fR); 14 | .br 15 | void \fBprimegen_skipto\fP(&\fIpg\fR,\fIbound\fR); 16 | 17 | struct primegen \fIpg\fR; 18 | .br 19 | uint64 \fIbound\fR; 20 | .SH DESCRIPTION 21 | The primegen library generates prime numbers in order: 22 | 2, 3, 5, 7, etc. 23 | It can generate primes as large as 10^15. 24 | 25 | The simplest way to use primegen 26 | is to call 27 | .B primegen_init 28 | and then call 29 | .B primegen_next 30 | repeatedly. 31 | The first call to 32 | .B primegen_next 33 | will return 2; 34 | the next call will return 3; 35 | the next call will return 5; 36 | and so on. 37 | 38 | At each moment the next few primes to be returned by 39 | .B primegen_next 40 | are buffered inside 41 | .IR pg . 42 | .B primegen_init 43 | initializes 44 | .I pg 45 | with the first few primes, 46 | starting at 2. 47 | You must initialize 48 | .I pg 49 | with 50 | .B primegen_init 51 | before using 52 | .I pg 53 | with any of the other 54 | .B primegen 55 | routines. 56 | You may call 57 | .B primegen_init 58 | again to start over at 2. 59 | 60 | .B primegen_next 61 | returns the next buffered prime 62 | and removes that prime from 63 | .IR pg . 64 | .B primegen_peek 65 | returns the next buffered prime 66 | but does not change 67 | .IR pg . 68 | 69 | .B primegen_skipto 70 | discards all primes up to 71 | .IR bound . 72 | It has the same effect as 73 | 74 | .EX 75 | while (primegen_peek(&pg) < bound) 76 | primegen_next(&pg); 77 | .EE 78 | 79 | .B primegen_count 80 | discards all primes up to 81 | .IR bound 82 | and returns the number of primes discarded. 83 | 84 | You can set up several independent generators; 85 | all primegen data is stored in 86 | .IR pg . 87 | Beware, however, that each struct primegen 88 | uses quite a bit of memory. 89 | .SH "SEE ALSO" 90 | primes(1), 91 | primespeed(1) 92 | -------------------------------------------------------------------------------- /src/primegen-0.97/man/man3/primegen.3: -------------------------------------------------------------------------------- 1 | .TH primegen 3 2 | .SH NAME 3 | primegen \- enumerate small primes 4 | .SH SYNTAX 5 | .B #include 6 | 7 | void \fBprimegen_init\fP(&\fIpg\fR); 8 | .br 9 | uint64 \fBprimegen_next\fP(&\fIpg\fR); 10 | .br 11 | uint64 \fBprimegen_peek\fP(&\fIpg\fR); 12 | .br 13 | uint64 \fBprimegen_count\fP(&\fIpg\fR,\fIbound\fR); 14 | .br 15 | void \fBprimegen_skipto\fP(&\fIpg\fR,\fIbound\fR); 16 | 17 | struct primegen \fIpg\fR; 18 | .br 19 | uint64 \fIbound\fR; 20 | .SH DESCRIPTION 21 | The primegen library generates prime numbers in order: 22 | 2, 3, 5, 7, etc. 23 | It can generate primes as large as 10^15. 24 | 25 | The simplest way to use primegen 26 | is to call 27 | .B primegen_init 28 | and then call 29 | .B primegen_next 30 | repeatedly. 31 | The first call to 32 | .B primegen_next 33 | will return 2; 34 | the next call will return 3; 35 | the next call will return 5; 36 | and so on. 37 | 38 | At each moment the next few primes to be returned by 39 | .B primegen_next 40 | are buffered inside 41 | .IR pg . 42 | .B primegen_init 43 | initializes 44 | .I pg 45 | with the first few primes, 46 | starting at 2. 47 | You must initialize 48 | .I pg 49 | with 50 | .B primegen_init 51 | before using 52 | .I pg 53 | with any of the other 54 | .B primegen 55 | routines. 56 | You may call 57 | .B primegen_init 58 | again to start over at 2. 59 | 60 | .B primegen_next 61 | returns the next buffered prime 62 | and removes that prime from 63 | .IR pg . 64 | .B primegen_peek 65 | returns the next buffered prime 66 | but does not change 67 | .IR pg . 68 | 69 | .B primegen_skipto 70 | discards all primes up to 71 | .IR bound . 72 | It has the same effect as 73 | 74 | .EX 75 | while (primegen_peek(&pg) < bound) 76 | primegen_next(&pg); 77 | .EE 78 | 79 | .B primegen_count 80 | discards all primes up to 81 | .IR bound 82 | and returns the number of primes discarded. 83 | 84 | You can set up several independent generators; 85 | all primegen data is stored in 86 | .IR pg . 87 | Beware, however, that each struct primegen 88 | uses quite a bit of memory. 89 | .SH "SEE ALSO" 90 | primes(1), 91 | primespeed(1) 92 | -------------------------------------------------------------------------------- /src/primegen-0.97/primegen_next.c: -------------------------------------------------------------------------------- 1 | #include "primegen.h" 2 | #include "primegen_impl.h" 3 | 4 | void primegen_fill(primegen *pg) 5 | { 6 | int i; 7 | uint32 mask; 8 | uint32 bits0, bits1, bits2, bits3, bits4, bits5, bits6, bits7; 9 | uint32 bits8, bits9, bits10, bits11, bits12, bits13, bits14, bits15; 10 | uint64 base; 11 | 12 | i = pg->pos; 13 | if (i == B32) { 14 | primegen_sieve(pg); 15 | pg->L += B; 16 | i = 0; 17 | } 18 | pg->pos = i + 1; 19 | 20 | bits0 = ~pg->buf[0][i]; 21 | bits1 = ~pg->buf[1][i]; 22 | bits2 = ~pg->buf[2][i]; 23 | bits3 = ~pg->buf[3][i]; 24 | bits4 = ~pg->buf[4][i]; 25 | bits5 = ~pg->buf[5][i]; 26 | bits6 = ~pg->buf[6][i]; 27 | bits7 = ~pg->buf[7][i]; 28 | bits8 = ~pg->buf[8][i]; 29 | bits9 = ~pg->buf[9][i]; 30 | bits10 = ~pg->buf[10][i]; 31 | bits11 = ~pg->buf[11][i]; 32 | bits12 = ~pg->buf[12][i]; 33 | bits13 = ~pg->buf[13][i]; 34 | bits14 = ~pg->buf[14][i]; 35 | bits15 = ~pg->buf[15][i]; 36 | 37 | base = pg->base + 1920; 38 | pg->base = base; 39 | 40 | pg->num = 0; 41 | 42 | for (mask = 0x80000000;mask;mask >>= 1) { 43 | base -= 60; 44 | if (bits15 & mask) pg->p[pg->num++] = base + 59; 45 | if (bits14 & mask) pg->p[pg->num++] = base + 53; 46 | if (bits13 & mask) pg->p[pg->num++] = base + 49; 47 | if (bits12 & mask) pg->p[pg->num++] = base + 47; 48 | if (bits11 & mask) pg->p[pg->num++] = base + 43; 49 | if (bits10 & mask) pg->p[pg->num++] = base + 41; 50 | if (bits9 & mask) pg->p[pg->num++] = base + 37; 51 | if (bits8 & mask) pg->p[pg->num++] = base + 31; 52 | if (bits7 & mask) pg->p[pg->num++] = base + 29; 53 | if (bits6 & mask) pg->p[pg->num++] = base + 23; 54 | if (bits5 & mask) pg->p[pg->num++] = base + 19; 55 | if (bits4 & mask) pg->p[pg->num++] = base + 17; 56 | if (bits3 & mask) pg->p[pg->num++] = base + 13; 57 | if (bits2 & mask) pg->p[pg->num++] = base + 11; 58 | if (bits1 & mask) pg->p[pg->num++] = base + 7; 59 | if (bits0 & mask) pg->p[pg->num++] = base + 1; 60 | } 61 | } 62 | 63 | uint64 primegen_next(primegen *pg) 64 | { 65 | while (!pg->num) 66 | primegen_fill(pg); 67 | 68 | return pg->p[--pg->num]; 69 | } 70 | 71 | uint64 primegen_peek(primegen *pg) 72 | { 73 | while (!pg->num) 74 | primegen_fill(pg); 75 | 76 | return pg->p[pg->num - 1]; 77 | } 78 | -------------------------------------------------------------------------------- /src/primegen-0.97/primes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "primegen.h" 3 | #include "fs64.h" 4 | 5 | uint32 mod10[200] = { 6 | 0,1,2,3,4,5,6,7,8,9 ,0,1,2,3,4,5,6,7,8,9 7 | ,0,1,2,3,4,5,6,7,8,9 ,0,1,2,3,4,5,6,7,8,9 8 | ,0,1,2,3,4,5,6,7,8,9 ,0,1,2,3,4,5,6,7,8,9 9 | ,0,1,2,3,4,5,6,7,8,9 ,0,1,2,3,4,5,6,7,8,9 10 | ,0,1,2,3,4,5,6,7,8,9 ,0,1,2,3,4,5,6,7,8,9 11 | ,0,1,2,3,4,5,6,7,8,9 ,0,1,2,3,4,5,6,7,8,9 12 | ,0,1,2,3,4,5,6,7,8,9 ,0,1,2,3,4,5,6,7,8,9 13 | ,0,1,2,3,4,5,6,7,8,9 ,0,1,2,3,4,5,6,7,8,9 14 | ,0,1,2,3,4,5,6,7,8,9 ,0,1,2,3,4,5,6,7,8,9 15 | ,0,1,2,3,4,5,6,7,8,9 ,0,1,2,3,4,5,6,7,8,9 16 | } ; 17 | 18 | uint32 div10[200] = { 19 | 0,0,0,0,0,0,0,0,0,0 ,1,1,1,1,1,1,1,1,1,1 20 | ,2,2,2,2,2,2,2,2,2,2 ,3,3,3,3,3,3,3,3,3,3 21 | ,4,4,4,4,4,4,4,4,4,4 ,5,5,5,5,5,5,5,5,5,5 22 | ,6,6,6,6,6,6,6,6,6,6 ,7,7,7,7,7,7,7,7,7,7 23 | ,8,8,8,8,8,8,8,8,8,8 ,9,9,9,9,9,9,9,9,9,9 24 | ,10,10,10,10,10,10,10,10,10,10 ,11,11,11,11,11,11,11,11,11,11 25 | ,12,12,12,12,12,12,12,12,12,12 ,13,13,13,13,13,13,13,13,13,13 26 | ,14,14,14,14,14,14,14,14,14,14 ,15,15,15,15,15,15,15,15,15,15 27 | ,16,16,16,16,16,16,16,16,16,16 ,17,17,17,17,17,17,17,17,17,17 28 | ,18,18,18,18,18,18,18,18,18,18 ,19,19,19,19,19,19,19,19,19,19 29 | } ; 30 | 31 | uint64 low = 2; 32 | uint64 high = 1000000000; 33 | 34 | primegen pg; 35 | 36 | uint32 digits[40]; 37 | int len; 38 | 39 | void main(argc,argv) 40 | int argc; 41 | char **argv; 42 | { 43 | uint64 p; 44 | uint32 u; 45 | int i; 46 | 47 | if (argv[1]) { 48 | scan_uint64(argv[1],&low); 49 | if (argv[2]) 50 | scan_uint64(argv[2],&high); 51 | } 52 | 53 | primegen_init(&pg); 54 | 55 | primegen_skipto(&pg,low); 56 | 57 | for (i = 0;i < 40;++i) digits[i] = 0; 58 | 59 | p = primegen_peek(&pg); 60 | len = 0; 61 | do { 62 | digits[len++] = p % 10; 63 | p /= 10; 64 | } while (p); 65 | 66 | p = primegen_peek(&pg); 67 | 68 | for (;;) { 69 | u = primegen_next(&pg) - p; 70 | p += u; 71 | if (p > high) break; 72 | 73 | for (i = 0;u;++i) { 74 | u += digits[i]; 75 | if (u >= 200) { 76 | digits[i] = u % 10; 77 | u = u / 10; 78 | } 79 | else { 80 | digits[i] = mod10[u]; 81 | u = div10[u]; 82 | } 83 | } 84 | if (i > len) len = i; 85 | 86 | for (i = len - 1;i >= 0;--i) 87 | putchar('0' + digits[i]); 88 | putchar('\n'); 89 | } 90 | 91 | exit(0); 92 | } 93 | -------------------------------------------------------------------------------- /src/primegen-0.97/substdo.c: -------------------------------------------------------------------------------- 1 | #include "substdio.h" 2 | #include "str.h" 3 | #include "byte.h" 4 | #include "error.h" 5 | 6 | static int allwrite(op,fd,buf,len) 7 | register int (*op)(); 8 | register int fd; 9 | register char *buf; 10 | register int len; 11 | { 12 | register int w; 13 | 14 | while (len) { 15 | w = op(fd,buf,len); 16 | if (w == -1) { 17 | if (errno == error_intr) continue; 18 | return -1; /* note that some data may have been written */ 19 | } 20 | if (w == 0) ; /* luser's fault */ 21 | buf += w; 22 | len -= w; 23 | } 24 | return 0; 25 | } 26 | 27 | int substdio_flush(s) 28 | register substdio *s; 29 | { 30 | register int p; 31 | 32 | p = s->p; 33 | if (!p) return 0; 34 | s->p = 0; 35 | return allwrite(s->op,s->fd,s->x,p); 36 | } 37 | 38 | int substdio_bput(s,buf,len) 39 | register substdio *s; 40 | register char *buf; 41 | register int len; 42 | { 43 | register int n; 44 | 45 | while (len > (n = s->n - s->p)) { 46 | byte_copy(s->x + s->p,n,buf); s->p += n; buf += n; len -= n; 47 | if (substdio_flush(s) == -1) return -1; 48 | } 49 | /* now len <= s->n - s->p */ 50 | byte_copy(s->x + s->p,len,buf); 51 | s->p += len; 52 | return 0; 53 | } 54 | 55 | int substdio_put(s,buf,len) 56 | register substdio *s; 57 | register char *buf; 58 | register int len; 59 | { 60 | register int n; 61 | 62 | n = s->n; 63 | if (len > n - s->p) { 64 | if (substdio_flush(s) == -1) return -1; 65 | /* now s->p == 0 */ 66 | if (n < SUBSTDIO_OUTSIZE) n = SUBSTDIO_OUTSIZE; 67 | while (len > s->n) { 68 | if (n > len) n = len; 69 | if (allwrite(s->op,s->fd,buf,n) == -1) return -1; 70 | buf += n; 71 | len -= n; 72 | } 73 | } 74 | /* now len <= s->n - s->p */ 75 | byte_copy(s->x + s->p,len,buf); 76 | s->p += len; 77 | return 0; 78 | } 79 | 80 | int substdio_putflush(s,buf,len) 81 | register substdio *s; 82 | register char *buf; 83 | register int len; 84 | { 85 | if (substdio_flush(s) == -1) return -1; 86 | return allwrite(s->op,s->fd,buf,len); 87 | } 88 | 89 | int substdio_bputs(s,buf) 90 | register substdio *s; 91 | register char *buf; 92 | { 93 | return substdio_bput(s,buf,str_len(buf)); 94 | } 95 | 96 | int substdio_puts(s,buf) 97 | register substdio *s; 98 | register char *buf; 99 | { 100 | return substdio_put(s,buf,str_len(buf)); 101 | } 102 | 103 | int substdio_putsflush(s,buf) 104 | register substdio *s; 105 | register char *buf; 106 | { 107 | return substdio_putflush(s,buf,str_len(buf)); 108 | } 109 | -------------------------------------------------------------------------------- /src/primegen-0.97/primegen_skip.c: -------------------------------------------------------------------------------- 1 | #include "primegen.h" 2 | #include "primegen_impl.h" 3 | 4 | static const unsigned long pop[256] = { 5 | 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5 6 | ,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6 7 | ,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6 8 | ,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7 9 | ,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6 10 | ,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7 11 | ,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7 12 | ,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8 13 | }; 14 | 15 | uint64 primegen_count(primegen *pg,uint64 to) 16 | { 17 | uint64 count = 0; 18 | register int pos; 19 | register int j; 20 | register uint32 bits; 21 | register uint32 smallcount; 22 | 23 | for (;;) { 24 | while (pg->num) { 25 | if (pg->p[pg->num - 1] >= to) return count; 26 | ++count; 27 | --pg->num; 28 | } 29 | 30 | smallcount = 0; 31 | pos = pg->pos; 32 | while ((pos < B32) && (pg->base + 1920 < to)) { 33 | for (j = 0;j < 16;++j) { 34 | bits = ~pg->buf[j][pos]; 35 | smallcount += pop[bits & 255]; bits >>= 8; 36 | smallcount += pop[bits & 255]; bits >>= 8; 37 | smallcount += pop[bits & 255]; bits >>= 8; 38 | smallcount += pop[bits & 255]; 39 | } 40 | pg->base += 1920; 41 | ++pos; 42 | } 43 | pg->pos = pos; 44 | count += smallcount; 45 | 46 | if (pos == B32) 47 | while (pg->base + B * 60 < to) { 48 | primegen_sieve(pg); 49 | pg->L += B; 50 | 51 | smallcount = 0; 52 | for (j = 0;j < 16;++j) 53 | for (pos = 0;pos < B32;++pos) { 54 | bits = ~pg->buf[j][pos]; 55 | smallcount += pop[bits & 255]; bits >>= 8; 56 | smallcount += pop[bits & 255]; bits >>= 8; 57 | smallcount += pop[bits & 255]; bits >>= 8; 58 | smallcount += pop[bits & 255]; 59 | } 60 | count += smallcount; 61 | pg->base += B * 60; 62 | } 63 | 64 | primegen_fill(pg); 65 | } 66 | } 67 | 68 | void primegen_skipto(primegen *pg,uint64 to) 69 | { 70 | int pos; 71 | 72 | for (;;) { 73 | while (pg->num) { 74 | if (pg->p[pg->num - 1] >= to) return; 75 | --pg->num; 76 | } 77 | 78 | pos = pg->pos; 79 | while ((pos < B32) && (pg->base + 1920 < to)) { 80 | pg->base += 1920; 81 | ++pos; 82 | } 83 | pg->pos = pos; 84 | if (pos == B32) 85 | while (pg->base + B * 60 < to) { 86 | pg->L += B; 87 | pg->base += B * 60; 88 | } 89 | 90 | primegen_fill(pg); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/primegen-0.97/primegen.0: -------------------------------------------------------------------------------- 1 | primegen(3) primegen(3) 2 | 3 | 4 | 5 | NNAAMMEE 6 | primegen - enumerate small primes 7 | 8 | SSYYNNTTAAXX 9 | ##iinncclluuddee <> 10 | 11 | void pprriimmeeggeenn__iinniitt(&_p_g); 12 | uint64 pprriimmeeggeenn__nneexxtt(&_p_g); 13 | uint64 pprriimmeeggeenn__ppeeeekk(&_p_g); 14 | uint64 pprriimmeeggeenn__ccoouunntt(&_p_g,_b_o_u_n_d); 15 | void pprriimmeeggeenn__sskkiippttoo(&_p_g,_b_o_u_n_d); 16 | 17 | struct primegen _p_g; 18 | uint64 _b_o_u_n_d; 19 | 20 | DDEESSCCRRIIPPTTIIOONN 21 | The primegen library generates prime numbers in order: 2, 3, 5, 7, etc. 22 | It can generate primes as large as 10^15. 23 | 24 | The simplest way to use primegen is to call pprriimmeeggeenn__iinniitt and then call 25 | pprriimmeeggeenn__nneexxtt repeatedly. The first call to pprriimmeeggeenn__nneexxtt will return 26 | 2; the next call will return 3; the next call will return 5; and so on. 27 | 28 | At each moment the next few primes to be returned by pprriimmeeggeenn__nneexxtt are 29 | buffered inside _p_g. pprriimmeeggeenn__iinniitt initializes _p_g with the first few 30 | primes, starting at 2. You must initialize _p_g with pprriimmeeggeenn__iinniitt 31 | before using _p_g with any of the other pprriimmeeggeenn routines. You may call 32 | pprriimmeeggeenn__iinniitt again to start over at 2. 33 | 34 | pprriimmeeggeenn__nneexxtt returns the next buffered prime and removes that prime 35 | from _p_g. pprriimmeeggeenn__ppeeeekk returns the next buffered prime but does not 36 | change _p_g. 37 | 38 | pprriimmeeggeenn__sskkiippttoo discards all primes up to _b_o_u_n_d. It has the same 39 | effect as 40 | 41 | while (primegen_peek(&pg) < bound) 42 | primegen_next(&pg); 43 | 44 | pprriimmeeggeenn__ccoouunntt discards all primes up to _b_o_u_n_d and returns the number 45 | of primes discarded. 46 | 47 | You can set up several independent generators; all primegen data is 48 | stored in _p_g. Beware, however, that each struct primegen uses quite a 49 | bit of memory. 50 | 51 | SSEEEE AALLSSOO 52 | primes(1), primespeed(1) 53 | 54 | 55 | 56 | primegen(3) 57 | -------------------------------------------------------------------------------- /src/primegen-0.97/man/cat3/primegen.0: -------------------------------------------------------------------------------- 1 | primegen(3) primegen(3) 2 | 3 | 4 | 5 | NNAAMMEE 6 | primegen - enumerate small primes 7 | 8 | SSYYNNTTAAXX 9 | ##iinncclluuddee <> 10 | 11 | void pprriimmeeggeenn__iinniitt(&_p_g); 12 | uint64 pprriimmeeggeenn__nneexxtt(&_p_g); 13 | uint64 pprriimmeeggeenn__ppeeeekk(&_p_g); 14 | uint64 pprriimmeeggeenn__ccoouunntt(&_p_g,_b_o_u_n_d); 15 | void pprriimmeeggeenn__sskkiippttoo(&_p_g,_b_o_u_n_d); 16 | 17 | struct primegen _p_g; 18 | uint64 _b_o_u_n_d; 19 | 20 | DDEESSCCRRIIPPTTIIOONN 21 | The primegen library generates prime numbers in order: 2, 3, 5, 7, etc. 22 | It can generate primes as large as 10^15. 23 | 24 | The simplest way to use primegen is to call pprriimmeeggeenn__iinniitt and then call 25 | pprriimmeeggeenn__nneexxtt repeatedly. The first call to pprriimmeeggeenn__nneexxtt will return 26 | 2; the next call will return 3; the next call will return 5; and so on. 27 | 28 | At each moment the next few primes to be returned by pprriimmeeggeenn__nneexxtt are 29 | buffered inside _p_g. pprriimmeeggeenn__iinniitt initializes _p_g with the first few 30 | primes, starting at 2. You must initialize _p_g with pprriimmeeggeenn__iinniitt 31 | before using _p_g with any of the other pprriimmeeggeenn routines. You may call 32 | pprriimmeeggeenn__iinniitt again to start over at 2. 33 | 34 | pprriimmeeggeenn__nneexxtt returns the next buffered prime and removes that prime 35 | from _p_g. pprriimmeeggeenn__ppeeeekk returns the next buffered prime but does not 36 | change _p_g. 37 | 38 | pprriimmeeggeenn__sskkiippttoo discards all primes up to _b_o_u_n_d. It has the same 39 | effect as 40 | 41 | while (primegen_peek(&pg) < bound) 42 | primegen_next(&pg); 43 | 44 | pprriimmeeggeenn__ccoouunntt discards all primes up to _b_o_u_n_d and returns the number 45 | of primes discarded. 46 | 47 | You can set up several independent generators; all primegen data is 48 | stored in _p_g. Beware, however, that each struct primegen uses quite a 49 | bit of memory. 50 | 51 | SSEEEE AALLSSOO 52 | primes(1), primespeed(1) 53 | 54 | 55 | 56 | primegen(3) 57 | -------------------------------------------------------------------------------- /src/primegen-0.97/instcheck.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "strerr.h" 4 | #include "error.h" 5 | #include "readwrite.h" 6 | #include "exit.h" 7 | 8 | extern void hier(); 9 | 10 | #define FATAL "instcheck: fatal: " 11 | #define WARNING "instcheck: warning: " 12 | 13 | void perm(prefix1,prefix2,prefix3,file,type,uid,gid,mode) 14 | char *prefix1; 15 | char *prefix2; 16 | char *prefix3; 17 | char *file; 18 | int type; 19 | int uid; 20 | int gid; 21 | int mode; 22 | { 23 | struct stat st; 24 | 25 | if (stat(file,&st) == -1) { 26 | if (errno == error_noent) 27 | strerr_warn6(WARNING,prefix1,prefix2,prefix3,file," does not exist",0); 28 | else 29 | strerr_warn4(WARNING,"unable to stat .../",file,": ",&strerr_sys); 30 | return; 31 | } 32 | 33 | if ((uid != -1) && (st.st_uid != uid)) 34 | strerr_warn6(WARNING,prefix1,prefix2,prefix3,file," has wrong owner",0); 35 | if ((gid != -1) && (st.st_gid != gid)) 36 | strerr_warn6(WARNING,prefix1,prefix2,prefix3,file," has wrong group",0); 37 | if ((st.st_mode & 07777) != mode) 38 | strerr_warn6(WARNING,prefix1,prefix2,prefix3,file," has wrong permissions",0); 39 | if ((st.st_mode & S_IFMT) != type) 40 | strerr_warn6(WARNING,prefix1,prefix2,prefix3,file," has wrong type",0); 41 | } 42 | 43 | void h(home,uid,gid,mode) 44 | char *home; 45 | int uid; 46 | int gid; 47 | int mode; 48 | { 49 | perm("","","",home,S_IFDIR,uid,gid,mode); 50 | } 51 | 52 | void d(home,subdir,uid,gid,mode) 53 | char *home; 54 | char *subdir; 55 | int uid; 56 | int gid; 57 | int mode; 58 | { 59 | if (chdir(home) == -1) 60 | strerr_die4sys(111,FATAL,"unable to switch to ",home,": "); 61 | perm("",home,"/",subdir,S_IFDIR,uid,gid,mode); 62 | } 63 | 64 | void p(home,fifo,uid,gid,mode) 65 | char *home; 66 | char *fifo; 67 | int uid; 68 | int gid; 69 | int mode; 70 | { 71 | if (chdir(home) == -1) 72 | strerr_die4sys(111,FATAL,"unable to switch to ",home,": "); 73 | perm("",home,"/",fifo,S_IFIFO,uid,gid,mode); 74 | } 75 | 76 | void c(home,subdir,file,uid,gid,mode) 77 | char *home; 78 | char *subdir; 79 | char *file; 80 | int uid; 81 | int gid; 82 | int mode; 83 | { 84 | if (chdir(home) == -1) 85 | strerr_die4sys(111,FATAL,"unable to switch to ",home,": "); 86 | if (chdir(subdir) == -1) 87 | strerr_die6sys(111,FATAL,"unable to switch to ",home,"/",subdir,": "); 88 | perm(".../",subdir,"/",file,S_IFREG,uid,gid,mode); 89 | } 90 | 91 | void z(home,file,len,uid,gid,mode) 92 | char *home; 93 | char *file; 94 | int len; 95 | int uid; 96 | int gid; 97 | int mode; 98 | { 99 | if (chdir(home) == -1) 100 | strerr_die4sys(111,FATAL,"unable to switch to ",home,": "); 101 | perm("",home,"/",file,S_IFREG,uid,gid,mode); 102 | } 103 | 104 | void main() 105 | { 106 | hier(); 107 | _exit(0); 108 | } 109 | -------------------------------------------------------------------------------- /src/primegen-0.97/primespeed.c: -------------------------------------------------------------------------------- 1 | #include "timing.h" 2 | #include "primegen.h" 3 | #include "primegen_impl.h" 4 | #include "fs64.h" 5 | 6 | static const unsigned long pop[256] = { 7 | 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5 8 | ,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6 9 | ,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6 10 | ,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7 11 | ,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6 12 | ,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7 13 | ,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7 14 | ,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8 15 | }; 16 | 17 | timing told; 18 | timing t; 19 | 20 | uint64 timedcount(primegen *pg,uint64 to) 21 | { 22 | uint64 count = 0; 23 | uint32 (*buf)[B32] = pg->buf; 24 | int pos; 25 | int j; 26 | register uint32 *bufj; 27 | register uint32 bits; 28 | register uint32 smallcount; 29 | 30 | timing_now(&told); 31 | 32 | for (;;) { 33 | while (pg->num) { 34 | if (pg->p[pg->num - 1] >= to) return count; 35 | ++count; 36 | --pg->num; 37 | } 38 | 39 | smallcount = 0; 40 | pos = pg->pos; 41 | while ((pos < B32) && (pg->base + 1920 < to)) { 42 | for (j = 0;j < 16;++j) { 43 | bits = ~buf[j][pos]; 44 | smallcount += pop[bits & 255]; bits >>= 8; 45 | smallcount += pop[bits & 255]; bits >>= 8; 46 | smallcount += pop[bits & 255]; bits >>= 8; 47 | smallcount += pop[bits]; 48 | } 49 | pg->base += 1920; 50 | ++pos; 51 | } 52 | pg->pos = pos; 53 | count += smallcount; 54 | 55 | if (pos == B32) 56 | while (pg->base + B * 60 < to) { 57 | primegen_sieve(pg); 58 | 59 | timing_now(&t); 60 | printf("Finished L=%d: %f\n",(int) pg->L,timing_diff(&t,&told)); 61 | told = t; 62 | pg->L += B; 63 | 64 | smallcount = 0; 65 | for (j = 0;j < 16;++j) { 66 | bufj = buf[j]; 67 | for (pos = 0;pos < B32;++pos) { 68 | bits = ~bufj[pos]; 69 | smallcount += pop[bits & 255]; bits >>= 8; 70 | smallcount += pop[bits & 255]; bits >>= 8; 71 | smallcount += pop[bits & 255]; bits >>= 8; 72 | smallcount += pop[bits]; 73 | } 74 | } 75 | count += smallcount; 76 | pg->base += B * 60; 77 | } 78 | 79 | primegen_fill(pg); 80 | } 81 | } 82 | 83 | char strnum[40]; 84 | 85 | primegen pg; 86 | 87 | timing start; 88 | timing_basic startb; 89 | timing finish; 90 | timing_basic finishb; 91 | 92 | void main(argc,argv) 93 | int argc; 94 | char **argv; 95 | { 96 | uint64 high = 1000000000; 97 | uint64 result; 98 | int loop; 99 | 100 | if (argv[1]) 101 | scan_uint64(argv[1],&high); 102 | 103 | timing_basic_now(&startb); 104 | timing_now(&start); 105 | 106 | primegen_init(&pg); 107 | result = timedcount(&pg,high); 108 | 109 | timing_basic_now(&finishb); 110 | timing_now(&finish); 111 | 112 | strnum[fmt_uint64(strnum,result)] = 0; 113 | printf("%s primes up to ",strnum); 114 | strnum[fmt_uint64(strnum,high)] = 0; 115 | printf("%s.\n",strnum); 116 | 117 | printf("Timings are in ticks. Nanoseconds per tick: approximately %f.\n",timing_basic_diff(&finishb,&startb) / timing_diff(&finish,&start)); 118 | printf("Overall seconds: approximately %f.\n",0.000000001 * timing_basic_diff(&finishb,&startb)); 119 | printf("Tick counts may be underestimates on systems without hardware tick support.\n"); 120 | 121 | exit(0); 122 | } 123 | -------------------------------------------------------------------------------- /src/primegen-0.97/strerr.h: -------------------------------------------------------------------------------- 1 | #ifndef STRERR_H 2 | #define STRERR_H 3 | 4 | struct strerr 5 | { 6 | struct strerr *who; 7 | char *x; 8 | char *y; 9 | char *z; 10 | } 11 | ; 12 | 13 | extern struct strerr strerr_sys; 14 | extern void strerr_sysinit(); 15 | 16 | extern char *strerr(); 17 | extern void strerr_warn(); 18 | extern void strerr_die(); 19 | 20 | #define STRERR(r,se,a) \ 21 | { se.who = 0; se.x = a; se.y = 0; se.z = 0; return r; } 22 | 23 | #define STRERR_SYS(r,se,a) \ 24 | { se.who = &strerr_sys; se.x = a; se.y = 0; se.z = 0; return r; } 25 | #define STRERR_SYS3(r,se,a,b,c) \ 26 | { se.who = &strerr_sys; se.x = a; se.y = b; se.z = c; return r; } 27 | 28 | #define strerr_warn6(x1,x2,x3,x4,x5,x6,se) \ 29 | strerr_warn((x1),(x2),(x3),(x4),(x5),(x6),(struct strerr *) (se)) 30 | #define strerr_warn5(x1,x2,x3,x4,x5,se) \ 31 | strerr_warn((x1),(x2),(x3),(x4),(x5),(char *) 0,(struct strerr *) (se)) 32 | #define strerr_warn4(x1,x2,x3,x4,se) \ 33 | strerr_warn((x1),(x2),(x3),(x4),(char *) 0,(char *) 0,(struct strerr *) (se)) 34 | #define strerr_warn3(x1,x2,x3,se) \ 35 | strerr_warn((x1),(x2),(x3),(char *) 0,(char *) 0,(char *) 0,(struct strerr *) (se)) 36 | #define strerr_warn2(x1,x2,se) \ 37 | strerr_warn((x1),(x2),(char *) 0,(char *) 0,(char *) 0,(char *) 0,(struct strerr *) (se)) 38 | #define strerr_warn1(x1,se) \ 39 | strerr_warn((x1),(char *) 0,(char *) 0,(char *) 0,(char *) 0,(char *) 0,(struct strerr *) (se)) 40 | 41 | #define strerr_die6(e,x1,x2,x3,x4,x5,x6,se) \ 42 | strerr_die((e),(x1),(x2),(x3),(x4),(x5),(x6),(struct strerr *) (se)) 43 | #define strerr_die5(e,x1,x2,x3,x4,x5,se) \ 44 | strerr_die((e),(x1),(x2),(x3),(x4),(x5),(char *) 0,(struct strerr *) (se)) 45 | #define strerr_die4(e,x1,x2,x3,x4,se) \ 46 | strerr_die((e),(x1),(x2),(x3),(x4),(char *) 0,(char *) 0,(struct strerr *) (se)) 47 | #define strerr_die3(e,x1,x2,x3,se) \ 48 | strerr_die((e),(x1),(x2),(x3),(char *) 0,(char *) 0,(char *) 0,(struct strerr *) (se)) 49 | #define strerr_die2(e,x1,x2,se) \ 50 | strerr_die((e),(x1),(x2),(char *) 0,(char *) 0,(char *) 0,(char *) 0,(struct strerr *) (se)) 51 | #define strerr_die1(e,x1,se) \ 52 | strerr_die((e),(x1),(char *) 0,(char *) 0,(char *) 0,(char *) 0,(char *) 0,(struct strerr *) (se)) 53 | 54 | #define strerr_die6sys(e,x1,x2,x3,x4,x5,x6) \ 55 | strerr_die((e),(x1),(x2),(x3),(x4),(x5),(x6),&strerr_sys) 56 | #define strerr_die5sys(e,x1,x2,x3,x4,x5) \ 57 | strerr_die((e),(x1),(x2),(x3),(x4),(x5),(char *) 0,&strerr_sys) 58 | #define strerr_die4sys(e,x1,x2,x3,x4) \ 59 | strerr_die((e),(x1),(x2),(x3),(x4),(char *) 0,(char *) 0,&strerr_sys) 60 | #define strerr_die3sys(e,x1,x2,x3) \ 61 | strerr_die((e),(x1),(x2),(x3),(char *) 0,(char *) 0,(char *) 0,&strerr_sys) 62 | #define strerr_die2sys(e,x1,x2) \ 63 | strerr_die((e),(x1),(x2),(char *) 0,(char *) 0,(char *) 0,(char *) 0,&strerr_sys) 64 | #define strerr_die1sys(e,x1) \ 65 | strerr_die((e),(x1),(char *) 0,(char *) 0,(char *) 0,(char *) 0,(char *) 0,&strerr_sys) 66 | 67 | #define strerr_die6x(e,x1,x2,x3,x4,x5,x6) \ 68 | strerr_die((e),(x1),(x2),(x3),(x4),(x5),(x6),(struct strerr *) 0) 69 | #define strerr_die5x(e,x1,x2,x3,x4,x5) \ 70 | strerr_die((e),(x1),(x2),(x3),(x4),(x5),(char *) 0,(struct strerr *) 0) 71 | #define strerr_die4x(e,x1,x2,x3,x4) \ 72 | strerr_die((e),(x1),(x2),(x3),(x4),(char *) 0,(char *) 0,(struct strerr *) 0) 73 | #define strerr_die3x(e,x1,x2,x3) \ 74 | strerr_die((e),(x1),(x2),(x3),(char *) 0,(char *) 0,(char *) 0,(struct strerr *) 0) 75 | #define strerr_die2x(e,x1,x2) \ 76 | strerr_die((e),(x1),(x2),(char *) 0,(char *) 0,(char *) 0,(char *) 0,(struct strerr *) 0) 77 | #define strerr_die1x(e,x1) \ 78 | strerr_die((e),(x1),(char *) 0,(char *) 0,(char *) 0,(char *) 0,(char *) 0,(struct strerr *) 0) 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /src/pollard_internet/pollard_p1.cc: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * pollard.cc -- Use Pollard's p-1 Algorithm to factor a large integer 3 | * 4 | * Uses Pollard's algorithm to (sometimes) factor a large integer into 5 | * smaller pieces. Note this algorithm has a rather high failure rate, 6 | * and is only used as an intro to Lenstra's Elliptic Curve Algorithm. 7 | * The algorithm was taken from "Rational Points on Elliptic Curves", by 8 | * Silverman/Tate, pages 130-132. 9 | * 10 | * Compile: 11 | * g++ -s -O4 -o pollard pollard.cc 12 | * Invoke: 13 | * ./pollard NumberToFactor A K 14 | * Where A is the base for the a^k-1 calculation (usually A=2). 15 | * and K is a small number, s.t. LCM{1,2,...,K} is product of small 16 | * primes to small powers. 17 | * 18 | * ChangeLog: 19 | * 950516 -- Created by Ami Fischman 20 | * 970301 -- minor fixes -- Paul Herman 21 | * 970324 -- added iteration condition -- Paul 22 | **************************************************************************/ 23 | 24 | #include "Integer.h" 25 | #include 26 | #include 27 | #include 28 | 29 | #define max_iter 80 // number of iterations before trying a new K 30 | // this, I think, should be a factor of the size of n 31 | 32 | Integer factor(Integer n, Integer a, Integer K) { 33 | /* Return a non-trivial factor of N using Pollard's p-1 algorithm */ 34 | /* Assumes 1 1) { 43 | // cout << "Ran " << count << " iterations." << endl; 44 | return gcd(a, n); // See if we get a freebee. 45 | } // (Besides, gcd(a,n) must 46 | // be 1 for the rest to work) 47 | 48 | t = PowModN(a, k, n); // We will allways have 1max_iter) { 56 | k = LCM(++K); // Change exponent 57 | a %= n; iteration = 0; 58 | // cout << "Trying new K = " << K << ", k = " << k << endl; 59 | } 60 | else if ( d==1 ) { 61 | a++; // Try another base 62 | // cout << "Trying new a = " << a << endl; 63 | } 64 | else break; 65 | } 66 | // cout << "Ran " << count << " iterations (and failed)." << endl; 67 | // cout << "K is " << K << " -- a is " << a << " -- n is " << n << " -- d is "<< d << endl; 68 | return 0; 69 | } 70 | 71 | int main(int argc, char *argv[]) { 72 | Integer n, pf, a, k; 73 | 74 | if (argc < 2) { 75 | cerr << "Usage: " << argv[0] << " NumberToFactor [K]" << endl; 76 | return 1; 77 | } 78 | 79 | n = argv[1]; 80 | a = 2; 81 | k = 2; 82 | if (argc == 3) k = argv[2]; 83 | 84 | cout << n << ": " << flush; 85 | struct timeval start, end; 86 | while(!isprime(n)) { 87 | gettimeofday(&start, NULL); 88 | pf = factor(n, a, k); 89 | gettimeofday(&end, NULL); 90 | long elapsed_us = (end.tv_sec * 1000 * 1000 + end.tv_usec) - 91 | (start.tv_sec * 1000 * 1000 + start.tv_usec); 92 | cout << pf << " (in " << elapsed_us << " us)" << flush; 93 | n /= pf; 94 | } 95 | cout << n << endl; 96 | return 0; 97 | } 98 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /src/primegen-0.97/find-systype.sh: -------------------------------------------------------------------------------- 1 | # oper-:arch-:syst-:chip-:kern- 2 | # oper = operating system type; e.g., sunos-4.1.4 3 | # arch = machine language; e.g., sparc 4 | # syst = which binaries can run; e.g., sun4 5 | # chip = chip model; e.g., micro-2-80 6 | # kern = kernel version; e.g., sun4m 7 | # dependence: arch --- chip 8 | # \ \ 9 | # oper --- syst --- kern 10 | # so, for example, syst is interpreted in light of oper, but chip is not. 11 | # anyway, no slashes, no extra colons, no uppercase letters. 12 | # the point of the extra -'s is to ease parsing: can add hierarchies later. 13 | # e.g., *:i386-*:*:pentium-*:* would handle pentium-100 as well as pentium, 14 | # and i386-486 (486s do have more instructions, you know) as well as i386. 15 | # the idea here is to include ALL useful available information. 16 | 17 | exec 2>/dev/null 18 | sys="`uname -s | tr '/:[A-Z]' '..[a-z]'`" 19 | if [ x"$sys" != x ] 20 | then 21 | unamer="`uname -r | tr /: ..`" 22 | unamem="`uname -m | tr /: ..`" 23 | unamev="`uname -v | tr /: ..`" 24 | 25 | case "$sys" in 26 | bsd.os) 27 | # in bsd 4.4, uname -v does not have useful info. 28 | # in bsd 4.4, uname -m is arch, not chip. 29 | oper="$sys-$unamer" 30 | arch="$unamem" 31 | syst="" 32 | chip="`sysctl -n hw.model`" 33 | kern="" 34 | ;; 35 | freebsd) 36 | # see above about bsd 4.4 37 | oper="$sys-$unamer" 38 | arch="$unamem" 39 | syst="" 40 | chip="`sysctl -n hw.model`" # hopefully 41 | kern="" 42 | ;; 43 | netbsd) 44 | # see above about bsd 4.4 45 | oper="$sys-$unamer" 46 | arch="$unamem" 47 | syst="" 48 | chip="`sysctl -n hw.model`" # hopefully 49 | kern="" 50 | ;; 51 | linux) 52 | # as in bsd 4.4, uname -v does not have useful info. 53 | oper="$sys-$unamer" 54 | syst="" 55 | chip="$unamem" 56 | kern="" 57 | case "$chip" in 58 | i386|i486|i586|i686) 59 | arch="i386" 60 | ;; 61 | alpha) 62 | arch="alpha" 63 | ;; 64 | esac 65 | ;; 66 | aix) 67 | # naturally IBM has to get uname -r and uname -v backwards. dorks. 68 | oper="$sys-$unamev-$unamer" 69 | arch="`arch | tr /: ..`" 70 | syst="" 71 | chip="$unamem" 72 | kern="" 73 | ;; 74 | sunos) 75 | oper="$sys-$unamer-$unamev" 76 | arch="`(uname -p || mach) | tr /: ..`" 77 | syst="`arch | tr /: ..`" 78 | chip="$unamem" # this is wrong; is there any way to get the real info? 79 | kern="`arch -k | tr /: ..`" 80 | ;; 81 | unix_sv) 82 | oper="$sys-$unamer-$unamev" 83 | arch="`uname -m`" 84 | syst="" 85 | chip="$unamem" 86 | kern="" 87 | ;; 88 | *) 89 | oper="$sys-$unamer-$unamev" 90 | arch="`arch | tr /: ..`" 91 | syst="" 92 | chip="$unamem" 93 | kern="" 94 | ;; 95 | esac 96 | else 97 | $CC -c trycpp.c 98 | $LD -o trycpp trycpp.o 99 | case `./trycpp` in 100 | nextstep) 101 | oper="nextstep-`hostinfo | sed -n 's/^[ ]*NeXT Mach \([^:]*\):.*$/\1/p'`" 102 | arch="`hostinfo | sed -n 's/^Processor type: \(.*\) (.*)$/\1/p' | tr /: ..`" 103 | syst="" 104 | chip="`hostinfo | sed -n 's/^Processor type: .* (\(.*\))$/\1/p' | tr ' /:' '...'`" 105 | kern="" 106 | ;; 107 | *) 108 | oper="unknown" 109 | arch="" 110 | syst="" 111 | chip="" 112 | kern="" 113 | ;; 114 | esac 115 | rm -f trycpp.o trycpp 116 | fi 117 | 118 | case "$chip" in 119 | 80486) 120 | # let's try to be consistent here. (BSD/OS) 121 | chip=i486 122 | ;; 123 | i486DX) 124 | # respect the hyphen hierarchy. (FreeBSD) 125 | chip=i486-dx 126 | ;; 127 | i486.DX2) 128 | # respect the hyphen hierarchy. (FreeBSD) 129 | chip=i486-dx2 130 | ;; 131 | Intel.586) 132 | # no, you nitwits, there is no such chip. (NeXTStep) 133 | chip=pentium 134 | ;; 135 | i586) 136 | # no, you nitwits, there is no such chip. (Linux) 137 | chip=pentium 138 | ;; 139 | i686) 140 | # STOP SAYING THAT! (Linux) 141 | chip=ppro 142 | esac 143 | 144 | echo "$oper-:$arch-:$syst-:$chip-:$kern-" | tr ' [A-Z]' '.[a-z]' 145 | -------------------------------------------------------------------------------- /src/primegen-0.97/find-systype: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # WARNING: This file was auto-generated. Do not edit! 3 | CC='gcc -O1 -fomit-frame-pointer -include /usr/include/errno.h' 4 | LD='gcc' 5 | # oper-:arch-:syst-:chip-:kern- 6 | # oper = operating system type; e.g., sunos-4.1.4 7 | # arch = machine language; e.g., sparc 8 | # syst = which binaries can run; e.g., sun4 9 | # chip = chip model; e.g., micro-2-80 10 | # kern = kernel version; e.g., sun4m 11 | # dependence: arch --- chip 12 | # \ \ 13 | # oper --- syst --- kern 14 | # so, for example, syst is interpreted in light of oper, but chip is not. 15 | # anyway, no slashes, no extra colons, no uppercase letters. 16 | # the point of the extra -'s is to ease parsing: can add hierarchies later. 17 | # e.g., *:i386-*:*:pentium-*:* would handle pentium-100 as well as pentium, 18 | # and i386-486 (486s do have more instructions, you know) as well as i386. 19 | # the idea here is to include ALL useful available information. 20 | 21 | exec 2>/dev/null 22 | sys="`uname -s | tr '/:[A-Z]' '..[a-z]'`" 23 | if [ x"$sys" != x ] 24 | then 25 | unamer="`uname -r | tr /: ..`" 26 | unamem="`uname -m | tr /: ..`" 27 | unamev="`uname -v | tr /: ..`" 28 | 29 | case "$sys" in 30 | bsd.os) 31 | # in bsd 4.4, uname -v does not have useful info. 32 | # in bsd 4.4, uname -m is arch, not chip. 33 | oper="$sys-$unamer" 34 | arch="$unamem" 35 | syst="" 36 | chip="`sysctl -n hw.model`" 37 | kern="" 38 | ;; 39 | freebsd) 40 | # see above about bsd 4.4 41 | oper="$sys-$unamer" 42 | arch="$unamem" 43 | syst="" 44 | chip="`sysctl -n hw.model`" # hopefully 45 | kern="" 46 | ;; 47 | netbsd) 48 | # see above about bsd 4.4 49 | oper="$sys-$unamer" 50 | arch="$unamem" 51 | syst="" 52 | chip="`sysctl -n hw.model`" # hopefully 53 | kern="" 54 | ;; 55 | linux) 56 | # as in bsd 4.4, uname -v does not have useful info. 57 | oper="$sys-$unamer" 58 | syst="" 59 | chip="$unamem" 60 | kern="" 61 | case "$chip" in 62 | i386|i486|i586|i686) 63 | arch="i386" 64 | ;; 65 | alpha) 66 | arch="alpha" 67 | ;; 68 | esac 69 | ;; 70 | aix) 71 | # naturally IBM has to get uname -r and uname -v backwards. dorks. 72 | oper="$sys-$unamev-$unamer" 73 | arch="`arch | tr /: ..`" 74 | syst="" 75 | chip="$unamem" 76 | kern="" 77 | ;; 78 | sunos) 79 | oper="$sys-$unamer-$unamev" 80 | arch="`(uname -p || mach) | tr /: ..`" 81 | syst="`arch | tr /: ..`" 82 | chip="$unamem" # this is wrong; is there any way to get the real info? 83 | kern="`arch -k | tr /: ..`" 84 | ;; 85 | unix_sv) 86 | oper="$sys-$unamer-$unamev" 87 | arch="`uname -m`" 88 | syst="" 89 | chip="$unamem" 90 | kern="" 91 | ;; 92 | *) 93 | oper="$sys-$unamer-$unamev" 94 | arch="`arch | tr /: ..`" 95 | syst="" 96 | chip="$unamem" 97 | kern="" 98 | ;; 99 | esac 100 | else 101 | $CC -c trycpp.c 102 | $LD -o trycpp trycpp.o 103 | case `./trycpp` in 104 | nextstep) 105 | oper="nextstep-`hostinfo | sed -n 's/^[ ]*NeXT Mach \([^:]*\):.*$/\1/p'`" 106 | arch="`hostinfo | sed -n 's/^Processor type: \(.*\) (.*)$/\1/p' | tr /: ..`" 107 | syst="" 108 | chip="`hostinfo | sed -n 's/^Processor type: .* (\(.*\))$/\1/p' | tr ' /:' '...'`" 109 | kern="" 110 | ;; 111 | *) 112 | oper="unknown" 113 | arch="" 114 | syst="" 115 | chip="" 116 | kern="" 117 | ;; 118 | esac 119 | rm -f trycpp.o trycpp 120 | fi 121 | 122 | case "$chip" in 123 | 80486) 124 | # let's try to be consistent here. (BSD/OS) 125 | chip=i486 126 | ;; 127 | i486DX) 128 | # respect the hyphen hierarchy. (FreeBSD) 129 | chip=i486-dx 130 | ;; 131 | i486.DX2) 132 | # respect the hyphen hierarchy. (FreeBSD) 133 | chip=i486-dx2 134 | ;; 135 | Intel.586) 136 | # no, you nitwits, there is no such chip. (NeXTStep) 137 | chip=pentium 138 | ;; 139 | i586) 140 | # no, you nitwits, there is no such chip. (Linux) 141 | chip=pentium 142 | ;; 143 | i686) 144 | # STOP SAYING THAT! (Linux) 145 | chip=ppro 146 | esac 147 | 148 | echo "$oper-:$arch-:$syst-:$chip-:$kern-" | tr ' [A-Z]' '.[a-z]' 149 | -------------------------------------------------------------------------------- /src/primegen-0.97/install.c: -------------------------------------------------------------------------------- 1 | #include "substdio.h" 2 | #include "strerr.h" 3 | #include "error.h" 4 | #include "open.h" 5 | #include "readwrite.h" 6 | #include "exit.h" 7 | 8 | extern void hier(); 9 | 10 | #define FATAL "install: fatal: " 11 | 12 | int fdsourcedir = -1; 13 | 14 | void h(home,uid,gid,mode) 15 | char *home; 16 | int uid; 17 | int gid; 18 | int mode; 19 | { 20 | if (mkdir(home,0700) == -1) 21 | if (errno != error_exist) 22 | strerr_die4sys(111,FATAL,"unable to mkdir ",home,": "); 23 | if (chown(home,uid,gid) == -1) 24 | strerr_die4sys(111,FATAL,"unable to chown ",home,": "); 25 | if (chmod(home,mode) == -1) 26 | strerr_die4sys(111,FATAL,"unable to chmod ",home,": "); 27 | } 28 | 29 | void d(home,subdir,uid,gid,mode) 30 | char *home; 31 | char *subdir; 32 | int uid; 33 | int gid; 34 | int mode; 35 | { 36 | if (chdir(home) == -1) 37 | strerr_die4sys(111,FATAL,"unable to switch to ",home,": "); 38 | if (mkdir(subdir,0700) == -1) 39 | if (errno != error_exist) 40 | strerr_die6sys(111,FATAL,"unable to mkdir ",home,"/",subdir,": "); 41 | if (chown(subdir,uid,gid) == -1) 42 | strerr_die6sys(111,FATAL,"unable to chown ",home,"/",subdir,": "); 43 | if (chmod(subdir,mode) == -1) 44 | strerr_die6sys(111,FATAL,"unable to chmod ",home,"/",subdir,": "); 45 | } 46 | 47 | char inbuf[SUBSTDIO_INSIZE]; 48 | char outbuf[SUBSTDIO_OUTSIZE]; 49 | substdio ssin; 50 | substdio ssout; 51 | 52 | void c(home,subdir,file,uid,gid,mode) 53 | char *home; 54 | char *subdir; 55 | char *file; 56 | int uid; 57 | int gid; 58 | int mode; 59 | { 60 | int fdin; 61 | int fdout; 62 | 63 | if (fchdir(fdsourcedir) == -1) 64 | strerr_die2sys(111,FATAL,"unable to switch back to source directory: "); 65 | 66 | fdin = open_read(file); 67 | if (fdin == -1) 68 | strerr_die4sys(111,FATAL,"unable to read ",file,": "); 69 | substdio_fdbuf(&ssin,read,fdin,inbuf,sizeof inbuf); 70 | 71 | if (chdir(home) == -1) 72 | strerr_die4sys(111,FATAL,"unable to switch to ",home,": "); 73 | if (chdir(subdir) == -1) 74 | strerr_die6sys(111,FATAL,"unable to switch to ",home,"/",subdir,": "); 75 | 76 | fdout = open_trunc(file); 77 | if (fdout == -1) 78 | strerr_die6sys(111,FATAL,"unable to write .../",subdir,"/",file,": "); 79 | substdio_fdbuf(&ssout,write,fdout,outbuf,sizeof outbuf); 80 | 81 | switch(substdio_copy(&ssout,&ssin)) { 82 | case -2: 83 | strerr_die4sys(111,FATAL,"unable to read ",file,": "); 84 | case -3: 85 | strerr_die6sys(111,FATAL,"unable to write .../",subdir,"/",file,": "); 86 | } 87 | 88 | close(fdin); 89 | if (substdio_flush(&ssout) == -1) 90 | strerr_die6sys(111,FATAL,"unable to write .../",subdir,"/",file,": "); 91 | if (fsync(fdout) == -1) 92 | strerr_die6sys(111,FATAL,"unable to write .../",subdir,"/",file,": "); 93 | if (close(fdout) == -1) /* NFS silliness */ 94 | strerr_die6sys(111,FATAL,"unable to write .../",subdir,"/",file,": "); 95 | 96 | if (chown(file,uid,gid) == -1) 97 | strerr_die6sys(111,FATAL,"unable to chown .../",subdir,"/",file,": "); 98 | if (chmod(file,mode) == -1) 99 | strerr_die6sys(111,FATAL,"unable to chmod .../",subdir,"/",file,": "); 100 | } 101 | 102 | void z(home,subdir,file,len,uid,gid,mode) 103 | char *home; 104 | char *subdir; 105 | char *file; 106 | int len; 107 | int uid; 108 | int gid; 109 | int mode; 110 | { 111 | int fdout; 112 | 113 | if (chdir(home) == -1) 114 | strerr_die4sys(111,FATAL,"unable to switch to ",home,": "); 115 | if (chdir(subdir) == -1) 116 | strerr_die6sys(111,FATAL,"unable to switch to ",home,"/",subdir,": "); 117 | 118 | fdout = open_trunc(file); 119 | if (fdout == -1) 120 | strerr_die6sys(111,FATAL,"unable to write .../",subdir,"/",file,": "); 121 | substdio_fdbuf(&ssout,write,fdout,outbuf,sizeof outbuf); 122 | 123 | while (len-- > 0) 124 | if (substdio_put(&ssout,"",1) == -1) 125 | strerr_die6sys(111,FATAL,"unable to write .../",subdir,"/",file,": "); 126 | 127 | if (substdio_flush(&ssout) == -1) 128 | strerr_die6sys(111,FATAL,"unable to write .../",subdir,"/",file,": "); 129 | if (fsync(fdout) == -1) 130 | strerr_die6sys(111,FATAL,"unable to write .../",subdir,"/",file,": "); 131 | if (close(fdout) == -1) /* NFS silliness */ 132 | strerr_die6sys(111,FATAL,"unable to write .../",subdir,"/",file,": "); 133 | 134 | if (chown(file,uid,gid) == -1) 135 | strerr_die6sys(111,FATAL,"unable to chown .../",subdir,"/",file,": "); 136 | if (chmod(file,mode) == -1) 137 | strerr_die6sys(111,FATAL,"unable to chmod .../",subdir,"/",file,": "); 138 | } 139 | 140 | void main() 141 | { 142 | fdsourcedir = open_read("."); 143 | if (fdsourcedir == -1) 144 | strerr_die2sys(111,FATAL,"unable to open current directory: "); 145 | 146 | umask(077); 147 | hier(); 148 | _exit(0); 149 | } 150 | -------------------------------------------------------------------------------- /src/pollard/gpu_main.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @brief TODO 3 | * 4 | * @author AJ Kaufmann 5 | * @author David Matlack 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | 15 | #include "kernel.h" 16 | 17 | using namespace std; 18 | 19 | void lcm(mpz_t *lcm, mpz_t *a, mpz_t *b) { 20 | mpz_t prod; 21 | mpz_t gcd; 22 | mpz_t tmp; 23 | 24 | mpz_init(&prod); 25 | mpz_init(&gcd); 26 | mpz_init(&tmp); 27 | 28 | mpz_mult(&prod, a, b); 29 | mpz_gcd(&gcd, a, b); 30 | 31 | mpz_div(lcm, &tmp, &prod, &gcd); 32 | } 33 | 34 | void LCM(mpz_t *e, unsigned *primes, unsigned B, mpz_t *tmp) { 35 | (void) primes; 36 | (void) tmp; 37 | 38 | if (B < 3) mpz_set_lui(e, 2); 39 | else { 40 | mpz_t b; 41 | mpz_t l; 42 | 43 | mpz_init(&b); 44 | mpz_init(&l); 45 | 46 | mpz_set_lui(&b, B); 47 | 48 | LCM(&l, primes, B-1, tmp); 49 | lcm(e, &l, &b); 50 | } 51 | } 52 | 53 | void PP(mpz_t *e, unsigned *primes, unsigned B, mpz_t *tmp) { 54 | unsigned p_i; 55 | unsigned power; 56 | unsigned prime_ul = (UL) primes[0]; 57 | 58 | mpz_set_lui(e, 1); 59 | 60 | for (p_i = 0; prime_ul < B; p_i ++) { 61 | power = (unsigned) (log((double) B) / log((double) prime_ul)); 62 | 63 | mpz_mult_u(tmp, e, pow(prime_ul, power)); 64 | mpz_set(e, tmp); 65 | 66 | prime_ul = primes[p_i + 1]; 67 | } 68 | 69 | } 70 | 71 | int cpu_factor(mpz_t n, unsigned *primes, unsigned num_primes, 72 | mpz_t *result) { 73 | (void) num_primes; 74 | 75 | unsigned B; 76 | unsigned max_it; 77 | unsigned iteration; 78 | mpz_t a, d, e, b, tmp; 79 | 80 | mpz_init(&a); 81 | mpz_init(&d); 82 | mpz_init(&e); 83 | mpz_init(&b); 84 | mpz_init(&tmp); 85 | 86 | mpz_set_lui(&a, 2); 87 | B = 2; 88 | max_it = 2000; 89 | 90 | PP(&e, primes, B, &tmp); 91 | for (;;iteration++) { 92 | mpz_gcd(&d, &a, &n); 93 | 94 | if (mpz_gt_one(&d)) { 95 | *result = d; 96 | return 0; 97 | } 98 | 99 | mpz_powmod(&b, &a, &e, &n); // b = (a ** e) % n 100 | mpz_addeq_i(&b, -1); // b -= 1 101 | mpz_gcd(&d, &b, &n); // d = gcd(b, n) 102 | 103 | if (mpz_gt_one(&d) && mpz_lt(&d, &n)) { 104 | *result = d; 105 | return 0; 106 | } 107 | 108 | // tmp = a+1 109 | mpz_set(&tmp, &a); 110 | mpz_addeq_i(&tmp, 1); 111 | 112 | if ( (0 == (B%60)) || (mpz_equal(&d, &n) && mpz_lt(&tmp, &n)) || 113 | (iteration > max_it) ) { 114 | PP(&e, primes, ++B, &tmp); 115 | 116 | // a %= n 117 | mpz_div(&tmp, &d, &a, &n); 118 | mpz_set(&a, &d); 119 | 120 | iteration = 0; 121 | } 122 | else if (mpz_equal_one(&d)) { 123 | mpz_addeq_i(&a, 1); 124 | } 125 | else break; 126 | } 127 | 128 | // couldn't find anything... :( 129 | // printf("Ran in %d iterations (and failed).\n", count); 130 | return -1; 131 | } 132 | 133 | int serial_factorize(mpz_t n, unsigned *primes, unsigned num_primes, 134 | mpz_t *result) { 135 | struct timeval start, end; 136 | int ret; 137 | 138 | gettimeofday(&start, NULL); 139 | 140 | ret = cpu_factor(n, primes, num_primes, result); 141 | 142 | gettimeofday(&end, NULL); 143 | 144 | long elapsed_us = (end.tv_sec * 1000 * 1000 + end.tv_usec) - 145 | (start.tv_sec * 1000 * 1000 + start.tv_usec); 146 | 147 | printf("(in %lu us) ", elapsed_us); 148 | 149 | return ret; 150 | } 151 | 152 | int main(int argc, char *argv[]) { 153 | if (1 == argc) { 154 | fprintf(stderr, "Usage: %s \n", argv[0]); 155 | return -1; 156 | } 157 | 158 | int (*factorize)(mpz_t, unsigned *, unsigned, mpz_t *) = 159 | ((strcmp(argv[1], "-s")) ? parallel_factorize : serial_factorize); 160 | unsigned num_to_factor = ((strcmp(argv[1], "-s")) ? 1 : 2); 161 | 162 | unsigned num_primes = TABLE_SIZE; 163 | unsigned *h_table = (unsigned *) malloc(num_primes * sizeof(unsigned)); 164 | if (0 != generate_prime_table(&h_table, num_primes)) { 165 | return 0; 166 | } 167 | 168 | mpz_t n; 169 | mpz_t factor; 170 | mpz_t mod; 171 | mpz_t div; 172 | 173 | mpz_init(&factor); 174 | mpz_init(&n); 175 | mpz_init(&div); 176 | mpz_init(&mod); 177 | 178 | for (; (int) num_to_factor < argc; num_to_factor ++) { 179 | 180 | char *num_str = argv[num_to_factor]; 181 | mpz_set_str(&n, num_str); 182 | 183 | printf("Factoring 0x%s: ", num_str); 184 | fflush(stdout); 185 | 186 | factorize(n, h_table, num_primes, &factor); 187 | 188 | char factor_str[1024]; 189 | mpz_get_str(&factor, factor_str, 1024); 190 | printf("%s ", factor_str); 191 | 192 | mpz_div(&div, &mod, &n, &factor); 193 | mpz_set_lui(&div, 0); 194 | if (mpz_equal(&mod, &div)) { // mod == 0 195 | printf("correct"); 196 | } 197 | else { 198 | printf("incorrect!"); 199 | } 200 | 201 | printf("\n"); 202 | } 203 | 204 | free(h_table); 205 | 206 | return 0; 207 | } 208 | 209 | 210 | 211 | -------------------------------------------------------------------------------- /src/pollard/TIMES: -------------------------------------------------------------------------------- 1 | 2 | 0x1def = 0x4f * 0x61 3 | 0x531adf = 0x73c * 0xab5 4 | 0x12b1f259795 = 0x721f7 * 0x29efd3 5 | 6 | At | CPU/GPU | Number | Time (s) 7 | ------+--------------------+---------------------------+----------- 8 | 3:12 | cuda bullshit 1024 | 0x1def | 3.838 9 | 3:12 | cuda bullshit 1024 | 0x531adf | 39.383 10 | 3:25 | cuda bullshit 1024 | 0x1def | 2.3985 11 | 3:25 | cuda bullshit 1024 | 0x531adf | 39.244 12 | 13 | Talk about how we chose # of blocks and # threads per block (and how it 14 | changes for certain numbers). 15 | 16 | Why are we bounded by 2 threads per block? 17 | 18 | 7:42 am, running with 2048 blocks, 1 thread per block: 19 | dmatlack@ghc57 (master) $ ./gpu 1def 531adf 12b1f259795 20 | Generating prime table... done! 21 | Factoring 0x1def: (in 132993 us) 61 22 | Factoring 0x531adf: (in 2264554 us) ab5 23 | Factoring 0x12b1f259795: (in 236823318 us) 721f7 24 | 25 | 7:44 am, running serial on cpu 26 | dmatlack@ghc57 (master) $ ./gpu -s 1def 531adf 12b1f259795 27 | Generating prime table... done! 28 | Factoring 0x1def: (in 43856 us) 4f 29 | Factoring 0x531adf: (in 256861 us) ab5 30 | Factoring 0x12b1f259795: (in 11097196 us) 721f7 31 | 32 | 7:56 am, running 2048-1, added lots of checks to finished 33 | dmatlack@ghc57 (master) $ ./gpu 1def 531adf 12b1f259795 34 | Generating prime table... done! 35 | Factoring 0x1def: (in 17329 us) 4f 36 | Factoring 0x531adf: (in 2192480 us) ab5 37 | Factoring 0x12b1f259795: (in 236313563 us) 721f7 38 | 39 | 8:44 am, 2048-2, added more checks to *finished 40 | dmatlack@ghc57 (master) $ ./gpu 1def 531adf 12b1f259795 41 | Generating prime table... done! 42 | Factoring 0x1def: (in 105455 us) 4f 43 | Factoring 0x531adf: (in 492011 us) 7c3 44 | Factoring 0x12b1f259795: (in 183865262 us) 721f7 45 | 46 | 9:05 am, running 2048-2, added preference of l1 cache 47 | ajkaufma@ghc54 $ ./gpu 1def 531adf 12B1F259795 48 | Generating prime table... done! 49 | Factoring 0x1def: (in 102672 us) 4f 50 | Factoring 0x531adf: (in 424434 us) 7c3 51 | Factoring 0x12B1F259795: (in 158295459 us) 721f7 52 | 53 | 9:12 am, running 2048-2, simplified initialization of mpz_t 54 | ajkaufma@ghc54 $ ./gpu 1def 531adf 12B1F259795 55 | Generating prime table... done! 56 | Factoring 0x1def: (in 59488 us) 4f 57 | Factoring 0x531adf: (in 242536 us) 7c3 58 | Factoring 0x12B1F259795: (in 88606677 us) 721f7 59 | 60 | 9:26 am, running 2048-3, decrease digits to 4 61 | ajkaufma@ghc54 $ ./gpu 1def 531adf 12B1F259795 62 | Generating prime table... done! 63 | Factoring 0x1def: (in 48888 us) 4f 64 | Factoring 0x531adf: (in 353144 us) ab5 65 | Factoring 0x12B1F259795: (in 34930322 us) 721f7 66 | 67 | 11:14 am, wayyy decreased max_it 68 | ajkaufma@ghc57 $ ./gpu 1def 531adf 12B1F259795 69 | Generating prime table... done! 70 | Factoring 0x1def: (in 71887 us) 4f 71 | Factoring 0x531adf: (in 117544 us) ab5 72 | Factoring 0x12B1F259795: (in 2250395 us) 721f7 73 | 74 | 11:44 am, bitches. 75 | ajkaufma@ghc57 $ ./gpu 1def 531adf 12B1F259795 89FD383381B 3CF5F89ED5F5 600FF385C031 878D4C7D68E9 B8C8CBD2DAEE7D 6926C73F919FA3E7 76 | Generating prime table... done! 77 | Factoring 0x1def: (in 249676 us) 4f 78 | Factoring 0x531adf: (in 461650 us) ab5 79 | Factoring 0x12B1F259795: (in 740693 us) 721f7 80 | Factoring 0x89FD383381B: (in 1008644 us) 7a3d0d 81 | Factoring 0x3CF5F89ED5F5: (in 5758910 us) 4f37ad 82 | Factoring 0x600FF385C031: (in 28797009 us) 605119 83 | Factoring 0x878D4C7D68E9: (in 2409094 us) ca1e31 84 | Factoring 0xB8C8CBD2DAEE7D: (in 14683225 us) d978d0b 85 | Factoring 0x6926C73F919FA3E7: (in 5960449 us) dcd39125 86 | 87 | 12:14 am, steady 88 | ajkaufma@ghc57 $ ./gpu 1def 531adf 12B1F259795 89FD383381B 3CF5F89ED5F5 600FF385C031 878D4C7D68E9 B8C8CBD2DAEE7D 6926C73F919FA3E7 89 | Generating prime table... done! 90 | Factoring 0x1def: (in 432459 us) 61 91 | Factoring 0x531adf: (in 782495 us) ab5 92 | Factoring 0x12B1F259795: (in 1272479 us) 721f7 93 | Factoring 0x89FD383381B: (in 1533002 us) 7a3d0d 94 | Factoring 0x3CF5F89ED5F5: (in 1709826 us) 4f37ad 95 | Factoring 0x600FF385C031: (in 20946349 us) ff52d9 96 | Factoring 0x878D4C7D68E9: (in 2644197 us) ca1e31 97 | Factoring 0xB8C8CBD2DAEE7D: (in 2001645 us) d978d0b 98 | Factoring 0x6926C73F919FA3E7: (in 5992349 us) 79e6711b 99 | 100 | 1:57, some serial times, not sure how good they are though 101 | Factoring 0x1def: (in 24787 us) 4f correct 102 | Factoring 0x531adf: (in 1179829 us) 7c3 correct 103 | Factoring 0x12B1F259795: (in 51622158 us) 721f7 correct 104 | Factoring 0x89FD383381B: (in 33528633 us) 120fc7 correct 105 | Factoring 0x3CF5F89ED5F5: (in 13363419 us) 4f37ad correct 106 | Factoring 0x600FF385C031: (in 27469101 us) 605119 correct 107 | Factoring 0x878D4C7D68E9: (in 75766724 us) abb039 correct 108 | Factoring 0xB8C8CBD2DAEE7D: (in 167479719 us) d978d0b correct 109 | 110 | 2:37 pm, gmp times 111 | ajkaufma@ghc57 $ 112 | 7663: 79 (in 1749 us)97 113 | 5446367: 2741 (in 455 us)1987 114 | 1284717778837: 467447 (in 13053 us)2748371 115 | 9482541414427: 8011021 (in 19989 us)1183687 116 | 67027135813109: 12910697 (in 37422 us)5191597 117 | 105621626404913: 6312217 (in 313840 us)16732889 118 | 149040943425769: 11251769 (in 9647 us)13246001 119 | 52012173456240253: 228035851 (in 33204 us)228087703 120 | 7576962498937463783: 3704852773 (in 2016773 us)2045145371 121 | 38817779006326811: 236877139 (in 119678 us)163873049 122 | -------------------------------------------------------------------------------- /src/pollard/kernel.cu: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "kernel.h" 4 | #include "primegen.h" 5 | 6 | #include 7 | #include 8 | 9 | #define NUM_BLOCKS 64 10 | #define THREADS_PER_BLOCK 32 11 | 12 | __constant__ unsigned c_table[TABLE_SIZE]; 13 | 14 | __global__ 15 | void parallel_factorize_kernel(mpz_t n, unsigned *primes, volatile bool *finished, 16 | mpz_t *result) { 17 | const unsigned tid = blockDim.x * blockIdx.x + threadIdx.x; 18 | const unsigned threads = gridDim.x * blockDim.x; 19 | const unsigned bid = blockIdx.x; 20 | // unsigned i = blockIdx.x * blockDim.x; 21 | 22 | const unsigned max_it = 400000 / threads; 23 | 24 | const unsigned b_start = B_START + bid;//bid * blockDim.x / max_it; 25 | const unsigned b_inc = gridDim.x;//threads / max_it; 26 | 27 | unsigned B; 28 | const unsigned B_MAX = TABLE_SIZE; 29 | unsigned it; 30 | unsigned p_i; 31 | unsigned power; 32 | unsigned prime_ul; 33 | 34 | mpz_t a, d, e, b, tmp; 35 | 36 | mpz_init(&a); 37 | mpz_init(&d); 38 | mpz_init(&e); 39 | mpz_init(&b); 40 | mpz_init(&tmp); 41 | 42 | // try a variety of a values 43 | mpz_set_ui(&a, (UL) tid * max_it + 2); 44 | 45 | for (B = b_start; B < B_MAX; B += b_inc) { 46 | 47 | /* Compute e as a product of prime powers */ 48 | prime_ul = (UL) c_table[0]; 49 | mpz_set_lui(&e, (UL) 1); 50 | for (p_i = 0; prime_ul < B; p_i ++) { 51 | if (*finished) return; 52 | 53 | power = (unsigned) (log((double) B) / log((double) prime_ul)); 54 | mpz_mult_u(&tmp, &e, (unsigned) pow((double) prime_ul, (double) power)); 55 | 56 | if (*finished) return; 57 | 58 | mpz_set(&e, &tmp); 59 | prime_ul = c_table[p_i + 1]; 60 | } 61 | 62 | if (mpz_equal_one(&e)) continue; 63 | // if (*finished) return; 64 | 65 | for (it = 0; it < max_it; it ++) { 66 | // printf("it = %d\n", it); 67 | 68 | if (*finished) return; 69 | 70 | // check for a freebie 71 | mpz_gcd(&d, &a, &n); 72 | // if (*finished) return; 73 | if (mpz_gt_one(&d)) { 74 | *result = d; 75 | *finished = true; 76 | } 77 | // if (*finished) return; 78 | 79 | mpz_powmod(&b, &a, &e, &n); // b = (a ** e) % n 80 | mpz_addeq_i(&b, -1); // b -= 1 81 | mpz_gcd(&d, &b, &n); // d = gcd(tmp, n) 82 | 83 | // if (*finished) return; 84 | 85 | // success! 86 | if (mpz_gt_one(&d) && mpz_lt(&d, &n)) { 87 | *result = d; 88 | *finished = true; 89 | } 90 | 91 | // if (*finished) return; 92 | // otherwise get a new value for a 93 | #if 0 94 | mpz_mult(&tmp, &a, &a); // tmp = a ** 2 95 | mpz_set_lui(&a, (UL) (i + it + tid)); // a = i + it + tid 96 | mpz_add(&tmp_2, &tmp, &a); // tmp_2 = &tmp + a 97 | mpz_div(&tmp, &a, &tmp_2, &n); // a = tmp_2 % n 98 | #else 99 | mpz_addeq_i(&a, threads * max_it); // a += 1 100 | #endif 101 | } 102 | } 103 | // couldn't find anything... :( 104 | // printf("Ran in %d iterations (and failed).\n", count); 105 | } 106 | 107 | int parallel_factorize(mpz_t n, unsigned *h_table, unsigned num_primes, 108 | mpz_t *factor) { 109 | unsigned blocks = NUM_BLOCKS; 110 | unsigned threads_per_block = THREADS_PER_BLOCK; 111 | //unsigned threads = blocks * threads_per_block; 112 | 113 | size_t result_bytes = sizeof(mpz_t); 114 | 115 | /* move prime table to the gpu */ 116 | // unsigned *d_table; 117 | if (cudaSuccess != cudaMemcpyToSymbol(c_table, h_table, 118 | num_primes * sizeof(unsigned))) { 119 | fprintf(stderr, "Unable to allocate device prime table!\n"); 120 | return -1; 121 | } 122 | 123 | mpz_t *d_result; 124 | bool *d_finished; 125 | if ((cudaSuccess != cudaMalloc((void **) &d_result, result_bytes)) || 126 | (cudaSuccess != cudaMalloc((void **) &d_finished, sizeof(bool))) || 127 | (cudaSuccess != cudaMemset(d_result, 0L, result_bytes)) || 128 | (cudaSuccess != cudaMemset(d_finished, false, sizeof(bool)))) { 129 | fprintf(stderr, "Unable to allocate device memory!\n"); 130 | return -1; 131 | } 132 | 133 | cudaDeviceSetCacheConfig(cudaFuncCachePreferL1); 134 | 135 | struct timeval start, end; 136 | 137 | gettimeofday(&start, NULL); 138 | 139 | parallel_factorize_kernel<<>> 140 | (n, NULL, d_finished, d_result); 141 | 142 | cudaDeviceSynchronize(); 143 | 144 | gettimeofday(&end, NULL); 145 | 146 | long elapsed_us = (end.tv_sec * 1000 * 1000 + end.tv_usec) - 147 | (start.tv_sec * 1000 * 1000 + start.tv_usec); 148 | 149 | printf("(in %ld us) ", elapsed_us); 150 | 151 | if (cudaSuccess != cudaMemcpy(factor, d_result, result_bytes, 152 | cudaMemcpyDeviceToHost)) { 153 | fprintf(stderr, "Unable to retrieve result from host!\n"); 154 | return -1; 155 | } 156 | 157 | cudaFree(d_result); 158 | cudaFree(d_finished); 159 | 160 | return 0; 161 | } 162 | 163 | void get_prime_table(unsigned *table, unsigned n) { 164 | primegen pg; 165 | 166 | primegen_init(&pg); 167 | 168 | for (unsigned i = 0; i < n; i ++) { 169 | table[i] = (unsigned) primegen_next(&pg); 170 | } 171 | } 172 | 173 | int generate_prime_table(unsigned **h_table, unsigned num_primes) { 174 | /* approximately the number of unsigned 32-bit primes */ 175 | /* (actual number is 203,280,221) */ 176 | /* paper claimed to use ~170,000,000 primes for experiments */ 177 | /* may want to write this to disk at some point... */ 178 | *h_table = (unsigned *) malloc(num_primes * sizeof(unsigned)); 179 | if (NULL == *h_table) { 180 | fprintf(stderr, "Unable to allocate host prime table!\n"); 181 | return -1; 182 | } 183 | 184 | printf("Generating prime table... "); fflush(stdout); 185 | get_prime_table(*h_table, num_primes); 186 | printf("done!\n"); fflush(stdout); 187 | 188 | return 0; 189 | } 190 | -------------------------------------------------------------------------------- /src/primegen-0.97/error_str.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "error.h" 3 | 4 | #define X(e,s) if (i == e) return s; 5 | 6 | char *error_str(i) 7 | int i; 8 | { 9 | X(0,"no error") 10 | X(error_intr,"interrupted system call") 11 | X(error_nomem,"out of memory") 12 | X(error_noent,"file does not exist") 13 | X(error_txtbsy,"text busy") 14 | X(error_io,"input/output error") 15 | X(error_exist,"file already exists") 16 | X(error_timeout,"timed out") 17 | X(error_inprogress,"operation in progress") 18 | X(error_again,"temporary failure") 19 | X(error_wouldblock,"input/output would block") 20 | X(error_pipe,"broken pipe") 21 | X(error_perm,"permission denied") 22 | X(error_acces,"access denied") 23 | #ifdef ESRCH 24 | X(ESRCH,"no such process") 25 | #endif 26 | #ifdef ENXIO 27 | X(ENXIO,"device not configured") 28 | #endif 29 | #ifdef E2BIG 30 | X(E2BIG,"argument list too long") 31 | #endif 32 | #ifdef ENOEXEC 33 | X(ENOEXEC,"exec format error") 34 | #endif 35 | #ifdef EBADF 36 | X(EBADF,"file descriptor not open") 37 | #endif 38 | #ifdef ECHILD 39 | X(ECHILD,"no child processes") 40 | #endif 41 | #ifdef EDEADLK 42 | X(EDEADLK,"operation would cause deadlock") 43 | #endif 44 | #ifdef EFAULT 45 | X(EFAULT,"bad address") 46 | #endif 47 | #ifdef ENOTBLK 48 | X(ENOTBLK,"not a block device") 49 | #endif 50 | #ifdef EBUSY 51 | X(EBUSY,"device busy") 52 | #endif 53 | #ifdef EXDEV 54 | X(EXDEV,"cross-device link") 55 | #endif 56 | #ifdef ENODEV 57 | X(ENODEV,"device does not support operation") 58 | #endif 59 | #ifdef ENOTDIR 60 | X(ENOTDIR,"not a directory") 61 | #endif 62 | #ifdef EISDIR 63 | X(EISDIR,"is a directory") 64 | #endif 65 | #ifdef EINVAL 66 | X(EINVAL,"invalid argument") 67 | #endif 68 | #ifdef ENFILE 69 | X(ENFILE,"system cannot open more files") 70 | #endif 71 | #ifdef EMFILE 72 | X(EMFILE,"process cannot open more files") 73 | #endif 74 | #ifdef ENOTTY 75 | X(ENOTTY,"not a tty") 76 | #endif 77 | #ifdef EFBIG 78 | X(EFBIG,"file too big") 79 | #endif 80 | #ifdef ENOSPC 81 | X(ENOSPC,"out of disk space") 82 | #endif 83 | #ifdef ESPIPE 84 | X(ESPIPE,"unseekable descriptor") 85 | #endif 86 | #ifdef EROFS 87 | X(EROFS,"read-only file system") 88 | #endif 89 | #ifdef EMLINK 90 | X(EMLINK,"too many links") 91 | #endif 92 | #ifdef EDOM 93 | X(EDOM,"input out of range") 94 | #endif 95 | #ifdef ERANGE 96 | X(ERANGE,"output out of range") 97 | #endif 98 | #ifdef EALREADY 99 | X(EALREADY,"operation already in progress") 100 | #endif 101 | #ifdef ENOTSOCK 102 | X(ENOTSOCK,"not a socket") 103 | #endif 104 | #ifdef EDESTADDRREQ 105 | X(EDESTADDRREQ,"destination address required") 106 | #endif 107 | #ifdef EMSGSIZE 108 | X(EMSGSIZE,"message too long") 109 | #endif 110 | #ifdef EPROTOTYPE 111 | X(EPROTOTYPE,"incorrect protocol type") 112 | #endif 113 | #ifdef ENOPROTOOPT 114 | X(ENOPROTOOPT,"protocol not available") 115 | #endif 116 | #ifdef EPROTONOSUPPORT 117 | X(EPROTONOSUPPORT,"protocol not supported") 118 | #endif 119 | #ifdef ESOCKTNOSUPPORT 120 | X(ESOCKTNOSUPPORT,"socket type not supported") 121 | #endif 122 | #ifdef EOPNOTSUPP 123 | X(EOPNOTSUPP,"operation not supported") 124 | #endif 125 | #ifdef EPFNOSUPPORT 126 | X(EPFNOSUPPORT,"protocol family not supported") 127 | #endif 128 | #ifdef EAFNOSUPPORT 129 | X(EAFNOSUPPORT,"address family not supported") 130 | #endif 131 | #ifdef EADDRINUSE 132 | X(EADDRINUSE,"address already used") 133 | #endif 134 | #ifdef EADDRNOTAVAIL 135 | X(EADDRNOTAVAIL,"address not available") 136 | #endif 137 | #ifdef ENETDOWN 138 | X(ENETDOWN,"network down") 139 | #endif 140 | #ifdef ENETUNREACH 141 | X(ENETUNREACH,"network unreachable") 142 | #endif 143 | #ifdef ENETRESET 144 | X(ENETRESET,"network reset") 145 | #endif 146 | #ifdef ECONNABORTED 147 | X(ECONNABORTED,"connection aborted") 148 | #endif 149 | #ifdef ECONNRESET 150 | X(ECONNRESET,"connection reset") 151 | #endif 152 | #ifdef ENOBUFS 153 | X(ENOBUFS,"out of buffer space") 154 | #endif 155 | #ifdef EISCONN 156 | X(EISCONN,"already connected") 157 | #endif 158 | #ifdef ENOTCONN 159 | X(ENOTCONN,"not connected") 160 | #endif 161 | #ifdef ESHUTDOWN 162 | X(ESHUTDOWN,"socket shut down") 163 | #endif 164 | #ifdef ETOOMANYREFS 165 | X(ETOOMANYREFS,"too many references") 166 | #endif 167 | #ifdef ECONNREFUSED 168 | X(ECONNREFUSED,"connection refused") 169 | #endif 170 | #ifdef ELOOP 171 | X(ELOOP,"symbolic link loop") 172 | #endif 173 | #ifdef ENAMETOOLONG 174 | X(ENAMETOOLONG,"file name too long") 175 | #endif 176 | #ifdef EHOSTDOWN 177 | X(EHOSTDOWN,"host down") 178 | #endif 179 | #ifdef EHOSTUNREACH 180 | X(EHOSTUNREACH,"host unreachable") 181 | #endif 182 | #ifdef ENOTEMPTY 183 | X(ENOTEMPTY,"directory not empty") 184 | #endif 185 | #ifdef EPROCLIM 186 | X(EPROCLIM,"too many processes") 187 | #endif 188 | #ifdef EUSERS 189 | X(EUSERS,"too many users") 190 | #endif 191 | #ifdef EDQUOT 192 | X(EDQUOT,"disk quota exceeded") 193 | #endif 194 | #ifdef ESTALE 195 | X(ESTALE,"stale NFS file handle") 196 | #endif 197 | #ifdef EREMOTE 198 | X(EREMOTE,"too many levels of remote in path") 199 | #endif 200 | #ifdef EBADRPC 201 | X(EBADRPC,"RPC structure is bad") 202 | #endif 203 | #ifdef ERPCMISMATCH 204 | X(ERPCMISMATCH,"RPC version mismatch") 205 | #endif 206 | #ifdef EPROGUNAVAIL 207 | X(EPROGUNAVAIL,"RPC program unavailable") 208 | #endif 209 | #ifdef EPROGMISMATCH 210 | X(EPROGMISMATCH,"program version mismatch") 211 | #endif 212 | #ifdef EPROCUNAVAIL 213 | X(EPROCUNAVAIL,"bad procedure for program") 214 | #endif 215 | #ifdef ENOLCK 216 | X(ENOLCK,"no locks available") 217 | #endif 218 | #ifdef ENOSYS 219 | X(ENOSYS,"system call not available") 220 | #endif 221 | #ifdef EFTYPE 222 | X(EFTYPE,"bad file type") 223 | #endif 224 | #ifdef EAUTH 225 | X(EAUTH,"authentication error") 226 | #endif 227 | #ifdef ENEEDAUTH 228 | X(ENEEDAUTH,"not authenticated") 229 | #endif 230 | #ifdef ENOSTR 231 | X(ENOSTR,"not a stream device") 232 | #endif 233 | #ifdef ETIME 234 | X(ETIME,"timer expired") 235 | #endif 236 | #ifdef ENOSR 237 | X(ENOSR,"out of stream resources") 238 | #endif 239 | #ifdef ENOMSG 240 | X(ENOMSG,"no message of desired type") 241 | #endif 242 | #ifdef EBADMSG 243 | X(EBADMSG,"bad message type") 244 | #endif 245 | #ifdef EIDRM 246 | X(EIDRM,"identifier removed") 247 | #endif 248 | #ifdef ENONET 249 | X(ENONET,"machine not on network") 250 | #endif 251 | #ifdef ERREMOTE 252 | X(ERREMOTE,"object not local") 253 | #endif 254 | #ifdef ENOLINK 255 | X(ENOLINK,"link severed") 256 | #endif 257 | #ifdef EADV 258 | X(EADV,"advertise error") 259 | #endif 260 | #ifdef ESRMNT 261 | X(ESRMNT,"srmount error") 262 | #endif 263 | #ifdef ECOMM 264 | X(ECOMM,"communication error") 265 | #endif 266 | #ifdef EPROTO 267 | X(EPROTO,"protocol error") 268 | #endif 269 | #ifdef EMULTIHOP 270 | X(EMULTIHOP,"multihop attempted") 271 | #endif 272 | #ifdef EREMCHG 273 | X(EREMCHG,"remote address changed") 274 | #endif 275 | return "unknown error"; 276 | } 277 | -------------------------------------------------------------------------------- /src/pollard_internet/Integer.h: -------------------------------------------------------------------------------- 1 | /****************************************************** 2 | * Integer.h -- C++ Integer class for the gmp integer library (from the 3 | * GNU project.) Allows you to use the arbitrary precision integers 4 | * from gmp as if they were plain old (int)'s or (long int)'s. Just 5 | * include this file in your source code. Example main(): 6 | * 7 | * #include "Integer.h" 8 | * int main(int argc, char **argv) { 9 | * Integer a, b, c; 10 | * 11 | * a = argv[1]; // automaticaly converts a string 12 | * b = argv[2]; // into an Integer. 13 | * c = a*b; 14 | * cout << a << " x " << b << " = " << c << endl; 15 | * } 16 | * 17 | ******** 18 | * To Do: 19 | * Rewrite all additions to check for signed (int)s (Done! with 000605) 20 | * ChangeLog: 21 | * 970304 - Created -- Paul Herman 22 | * 980804 - Cleaned up -- Paul Herman 23 | * 000605 - bug fixes -- Paul Herman 24 | *******************************************************/ 25 | 26 | 27 | /***************************** BEGIN ********************************/ 28 | 29 | #include 30 | 31 | #ifndef FALSE 32 | #define FALSE 0 33 | #endif // FALSE 34 | #ifndef TRUE 35 | #define TRUE 1 36 | #endif // TRUE 37 | 38 | #ifndef __GMP_INTEGER_CLASS__ 39 | #define __GMP_INTEGER_CLASS__ 40 | 41 | using namespace std; 42 | 43 | #include // provides the mpz integer routines 44 | #include // for cin & cout istreams & ostreams 45 | #include // use time as seed for random numbers 46 | 47 | #ifndef ABSmodN 48 | #define ABSmodN(x, n) (x%n) // in gmp lib, (-x)%n = (n-x)%n 49 | #endif // ABSmodN 50 | 51 | 52 | /**************************** INTEGER ***************************************/ 53 | 54 | class Integer { 55 | private: 56 | mpz_t _x; 57 | int base; 58 | public: 59 | // 60 | // Constructors 61 | // 62 | Integer(void); 63 | Integer(int a); 64 | Integer(unsigned int a); 65 | Integer(const Integer &a); 66 | Integer(char * a); 67 | Integer(mpz_t a); 68 | // 69 | // Destructor 70 | // 71 | ~Integer(void); 72 | // 73 | // Simple access functions 74 | // 75 | int getbase(void); 76 | void setbase(int b); 77 | // 78 | // Casting (changing Integer to another type) 79 | // 80 | operator bool(void); // (bool) i 81 | operator int(void); // (int) i 82 | operator unsigned int(void);// (unsigned int) i 83 | operator float(void); // (float) i 84 | operator double(void); // (double)i 85 | // 86 | // Assignments (Setting Integer equal to something else) 87 | // 88 | void operator =(Integer a); // c = (Integer) a; 89 | void operator =(int a); // c = (int) a; 90 | void operator =(unsigned int a); // c = (unsigned int) a; 91 | void operator =(char *a); // c = a (a string); 92 | void operator =(const char *a); // c = "1231231"; 93 | void operator =(mpz_t a); // c = a (a mpz number); 94 | // 95 | // Binary (one sided) operators 96 | // 97 | Integer operator -(void); // c = -a; 98 | bool operator !(void); // c = (bool) (!a) 99 | void operator +=(Integer a); // c += a; 100 | void operator +=(int a); // c += a; 101 | void operator +=(unsigned int a); // c += a; 102 | void operator -=(Integer a); // c -= a; 103 | void operator -=(int a); // c -= a; 104 | void operator -=(unsigned int a); // c -= a; 105 | void operator *=(Integer a); // c *= a; 106 | void operator *=(int a); // c *= a; 107 | void operator *=(unsigned int a); // c *= a; 108 | void operator /=(Integer a); // c /= a; 109 | void operator /=(int a); // c /= a; 110 | void operator /=(unsigned int a); // c /= a; 111 | void operator %=(Integer a); // c %= a; 112 | void operator %=(int a); // c %= a; 113 | void operator ^=(int a); // c ^= a; 114 | // WARNING: No ^ with type (Integer) as exponent 115 | 116 | Integer operator ++(void); // c = a++; 117 | Integer operator --(void); // c = a--; 118 | Integer operator ++(int from); // c = ++a; 119 | Integer operator --(int from); // c = --a; 120 | 121 | // 122 | // The following operators must be friendly because the are binary 123 | // 124 | friend Integer operator +(Integer a, Integer b); // a + b; 125 | friend Integer operator +(Integer a, int b); // a + b; 126 | friend Integer operator +(int a, Integer b); // a + b; 127 | friend Integer operator -(Integer a, Integer b); // a - b; 128 | friend Integer operator -(Integer a, int b); // a - b; 129 | friend Integer operator -(int a, Integer b); // a - b; 130 | friend Integer operator *(Integer a, Integer b); // a * b; 131 | friend Integer operator *(Integer a, int b); // a * b; 132 | friend Integer operator *(int a, Integer b); // a * b; 133 | friend Integer operator /(Integer a, Integer b); // a / b; 134 | friend Integer operator /(Integer a, int b); // a / b; 135 | friend Integer operator /(int a, Integer b); // a / b; 136 | friend Integer operator %(Integer a, Integer b); // a % b; 137 | friend Integer operator %(Integer a, int b); // a % b; 138 | friend Integer operator %(int a, Integer b); // a % b; 139 | friend Integer operator ^(Integer a, int b); // a ^ b; 140 | friend Integer operator &(Integer a, Integer b); // a & b; 141 | friend Integer operator |(Integer a, Integer b); // a | b; 142 | 143 | friend bool operator ==(Integer a, Integer b); // (c == a)? 144 | friend bool operator ==(Integer a, int b); // (c == a)? 145 | friend bool operator ==(int a, Integer b); // (c == a)? 146 | friend bool operator !=(Integer a, Integer b); // (c != a)? 147 | friend bool operator !=(Integer a, int b); // (c != a)? 148 | friend bool operator !=(int a, Integer b); // (c != a)? 149 | friend bool operator <(Integer a, Integer b); // (c < a)? 150 | friend bool operator <(Integer a, int b); // (c < a)? 151 | friend bool operator <(int a, Integer b); // (c < a)? 152 | friend bool operator <=(Integer a, Integer b); // (c <= a)? 153 | friend bool operator <=(Integer a, int b); // (c <= a)? 154 | friend bool operator <=(int a, Integer b); // (c <= a)? 155 | friend bool operator >(Integer a, Integer b); // (c > a)? 156 | friend bool operator >(Integer a, int b); // (c > a)? 157 | friend bool operator >(int a, Integer b); // (c > a)? 158 | friend bool operator >=(Integer a, Integer b); // (c >= a)? 159 | friend bool operator >=(Integer a, int b); // (c >= a)? 160 | friend bool operator >=(int a, Integer b); // (c >= a)? 161 | friend ostream& operator <<(ostream &s, Integer a); // cout << a; 162 | friend istream& operator >>(istream &s, Integer &a); // cin >> a; 163 | friend Integer& operator <<(Integer a, int b); // a << b; 164 | friend Integer& operator >>(Integer a, int b); // a >> b; 165 | // 166 | // These functions are friendly to speed up access to the class' 167 | // private parts. 168 | // 169 | friend void Int2a(Integer i, char *string); 170 | friend Integer gcd(Integer a, Integer b); 171 | friend Integer exgcd(Integer a, Integer b, Integer &c, Integer &d); 172 | friend Integer InvModN(Integer a, Integer n); 173 | friend Integer PowModN(Integer base, Integer exp, Integer n); 174 | friend Integer PowModN(Integer base, int exp, Integer n); 175 | friend bool isprime(Integer a); 176 | friend bool issquare(Integer a); 177 | friend bool testbit(Integer a, unsigned long int bit_number); 178 | friend Integer sqrt(Integer a); 179 | friend int digits(Integer a, int base); 180 | friend int Jacobi(Integer m, Integer n); 181 | friend int Legendre(Integer m, Integer n); 182 | friend Integer random(Integer range); 183 | }; // end 'class Integer' 184 | // 185 | // Misc Number theory functions (non-friendly) 186 | // 187 | Integer lcm(Integer a, Integer b); 188 | Integer LCM(Integer a); // LCM(a) = lcm[1, 2, 3, ..., a] 189 | int logn(Integer a, int base); 190 | int log2(Integer a); 191 | Integer fact(Integer a); 192 | Integer P_n_k(Integer n, Integer k); 193 | Integer C_n_k(Integer n, Integer k); 194 | Integer nextprime(Integer a); 195 | Integer prevprime(Integer a); 196 | Integer SqrtModN(Integer x, Integer p); 197 | 198 | #endif // __GMP_INTEGER_CLASS__ 199 | -------------------------------------------------------------------------------- /src/primegen-0.97/Makefile: -------------------------------------------------------------------------------- 1 | # Don't edit Makefile! Use conf-* for configuration. 2 | 3 | SHELL=/bin/sh 4 | 5 | default: it 6 | 7 | auto-ccld.sh: \ 8 | conf-cc conf-ld warn-auto.sh 9 | ( cat warn-auto.sh; \ 10 | echo CC=\'`head -1 conf-cc`\'; \ 11 | echo LD=\'`head -1 conf-ld`\' \ 12 | ) > auto-ccld.sh 13 | 14 | auto-str: \ 15 | load auto-str.o substdio.a error.a str.a 16 | ./load auto-str substdio.a error.a str.a 17 | 18 | auto-str.o: \ 19 | compile auto-str.c substdio.h readwrite.h exit.h 20 | ./compile auto-str.c 21 | 22 | auto_home.c: \ 23 | auto-str conf-home 24 | ./auto-str auto_home `head -1 conf-home` > auto_home.c 25 | 26 | auto_home.o: \ 27 | compile auto_home.c 28 | ./compile auto_home.c 29 | 30 | byte_copy.o: \ 31 | compile byte_copy.c byte.h 32 | ./compile byte_copy.c 33 | 34 | byte_cr.o: \ 35 | compile byte_cr.c byte.h 36 | ./compile byte_cr.c 37 | 38 | check: \ 39 | it instcheck 40 | ./instcheck 41 | 42 | compile: \ 43 | make-compile warn-auto.sh systype 44 | ( cat warn-auto.sh; ./make-compile "`cat systype`" ) > \ 45 | compile 46 | chmod 755 compile 47 | 48 | eratspeed: \ 49 | load eratspeed.o 50 | ./load eratspeed 51 | 52 | eratspeed.o: \ 53 | compile eratspeed.c timing.h hasrdtsc.h hasgethr.h uint32.h 54 | ./compile eratspeed.c 55 | 56 | error.a: \ 57 | makelib error.o error_str.o 58 | ./makelib error.a error.o error_str.o 59 | 60 | error.o: \ 61 | compile error.c error.h 62 | ./compile error.c 63 | 64 | error_str.o: \ 65 | compile error_str.c error.h 66 | ./compile error_str.c 67 | 68 | find-systype: \ 69 | find-systype.sh auto-ccld.sh 70 | cat auto-ccld.sh find-systype.sh > find-systype 71 | chmod 755 find-systype 72 | 73 | fs64.o: \ 74 | compile fs64.c fs64.h uint64.h 75 | ./compile fs64.c 76 | 77 | hasgethr.h: \ 78 | trygethr.c compile load 79 | ( ( ./compile trygethr.c && ./load trygethr ) >/dev/null \ 80 | 2>&1 \ 81 | && echo \#define HASGETHRTIME 1 || exit 0 ) > hasgethr.h 82 | rm -f trygethr.o 83 | 84 | hasrdtsc.h: \ 85 | tryrdtsc.c compile load 86 | ( ( ./compile tryrdtsc.c && ./load tryrdtsc && ./tryrdtsc \ 87 | ) >/dev/null 2>&1 \ 88 | && echo \#define HASRDTSC 1 || exit 0 ) > hasrdtsc.h 89 | rm -f tryrdtsc.o tryrdtsc 90 | 91 | hier.o: \ 92 | compile hier.c auto_home.h 93 | ./compile hier.c 94 | 95 | install: \ 96 | load install.o hier.o auto_home.o strerr.a substdio.a open.a error.a \ 97 | str.a 98 | ./load install hier.o auto_home.o strerr.a substdio.a \ 99 | open.a error.a str.a 100 | 101 | install.o: \ 102 | compile install.c substdio.h strerr.h error.h open.h readwrite.h \ 103 | exit.h 104 | ./compile install.c 105 | 106 | instcheck: \ 107 | load instcheck.o hier.o auto_home.o strerr.a substdio.a error.a str.a 108 | ./load instcheck hier.o auto_home.o strerr.a substdio.a \ 109 | error.a str.a 110 | 111 | instcheck.o: \ 112 | compile instcheck.c strerr.h error.h readwrite.h exit.h 113 | ./compile instcheck.c 114 | 115 | int64.h: \ 116 | trylong64.c compile load int64.h1 int64.h2 117 | ( ( ./compile trylong64.c && ./load trylong64 && \ 118 | ./trylong64 ) >/dev/null 2>&1 \ 119 | && cat int64.h1 || cat int64.h2 ) > int64.h 120 | rm -f trylong64.o trylong64 121 | 122 | it: \ 123 | man prog 124 | 125 | load: \ 126 | make-load warn-auto.sh systype 127 | ( cat warn-auto.sh; ./make-load "`cat systype`" ) > load 128 | chmod 755 load 129 | 130 | make-compile: \ 131 | make-compile.sh auto-ccld.sh 132 | cat auto-ccld.sh make-compile.sh > make-compile 133 | chmod 755 make-compile 134 | 135 | make-load: \ 136 | make-load.sh auto-ccld.sh 137 | cat auto-ccld.sh make-load.sh > make-load 138 | chmod 755 make-load 139 | 140 | make-makelib: \ 141 | make-makelib.sh auto-ccld.sh 142 | cat auto-ccld.sh make-makelib.sh > make-makelib 143 | chmod 755 make-makelib 144 | 145 | makelib: \ 146 | make-makelib warn-auto.sh systype 147 | ( cat warn-auto.sh; ./make-makelib "`cat systype`" ) > \ 148 | makelib 149 | chmod 755 makelib 150 | 151 | man: \ 152 | primes.0 primespeed.0 primegaps.0 primegen.0 153 | 154 | open.a: \ 155 | makelib open_read.o open_trunc.o 156 | ./makelib open.a open_read.o open_trunc.o 157 | 158 | open_read.o: \ 159 | compile open_read.c open.h 160 | ./compile open_read.c 161 | 162 | open_trunc.o: \ 163 | compile open_trunc.c open.h 164 | ./compile open_trunc.c 165 | 166 | primegaps: \ 167 | load primegaps.o fs64.o primegen.a math.lib 168 | ./load primegaps fs64.o primegen.a `cat math.lib` 169 | 170 | primegaps.0: \ 171 | primegaps.1 172 | nroff -man primegaps.1 > primegaps.0 173 | 174 | primegaps.o: \ 175 | compile primegaps.c primegen.h uint32.h uint64.h 176 | ./compile primegaps.c 177 | 178 | primegen.0: \ 179 | primegen.3 180 | nroff -man primegen.3 > primegen.0 181 | 182 | primegen.a: \ 183 | makelib primegen.o primegen_init.o primegen_next.o primegen_skip.o 184 | ./makelib primegen.a primegen.o primegen_init.o \ 185 | primegen_next.o primegen_skip.o 186 | 187 | primegen.h: \ 188 | conf-words primegen.h.sed 189 | sed s/conf-words/`head -1 conf-words`/ \ 190 | < primegen.h.sed > primegen.h 191 | 192 | primegen.o: \ 193 | compile primegen.c primegen.h uint32.h uint64.h primegen_impl.h \ 194 | int64.h 195 | ./compile primegen.c 196 | 197 | primegen_init.o: \ 198 | compile primegen_init.c primegen.h uint32.h uint64.h primegen_impl.h 199 | ./compile primegen_init.c 200 | 201 | primegen_next.o: \ 202 | compile primegen_next.c primegen.h uint32.h uint64.h primegen_impl.h 203 | ./compile primegen_next.c 204 | 205 | primegen_skip.o: \ 206 | compile primegen_skip.c primegen.h uint32.h uint64.h primegen_impl.h 207 | ./compile primegen_skip.c 208 | 209 | primes: \ 210 | load primes.o fs64.o primegen.a 211 | ./load primes fs64.o primegen.a 212 | 213 | primes.0: \ 214 | primes.1 215 | nroff -man primes.1 > primes.0 216 | 217 | primes.o: \ 218 | compile primes.c primegen.h uint32.h uint64.h fs64.h uint64.h 219 | ./compile primes.c 220 | 221 | primespeed: \ 222 | load primespeed.o fs64.o primegen.a 223 | ./load primespeed fs64.o primegen.a 224 | 225 | primespeed.0: \ 226 | primespeed.1 227 | nroff -man primespeed.1 > primespeed.0 228 | 229 | primespeed.o: \ 230 | compile primespeed.c timing.h hasrdtsc.h hasgethr.h primegen.h \ 231 | uint32.h uint64.h primegen_impl.h fs64.h uint64.h 232 | ./compile primespeed.c 233 | 234 | prog: \ 235 | primes primespeed primegaps eratspeed 236 | 237 | setup: \ 238 | it install 239 | ./install 240 | 241 | shar: \ 242 | FILES BLURB README TODO THANKS CHANGES FILES TARGETS VERSION SYSDEPS \ 243 | Makefile INSTALL primes.1 primes.c primespeed.1 primespeed.c \ 244 | primegaps.1 primegaps.c primegen.3 conf-words primegen.h.sed \ 245 | primegen_impl.h primegen.c primegen_init.c primegen_next.c \ 246 | primegen_skip.c eratspeed.c fs64.h fs64.c math.lib hier.c conf-home \ 247 | auto-str.c auto_home.h install.c instcheck.c conf-cc conf-ld \ 248 | find-systype.sh make-compile.sh make-load.sh make-makelib.sh trycpp.c \ 249 | warn-auto.sh substdio.h substdio.c substdi.c substdo.c \ 250 | substdio_copy.c subfd.h subfderr.c readwrite.h exit.h strerr.h \ 251 | strerr_sys.c strerr_die.c error.3 error_str.3 error.h error.c \ 252 | error_str.c open.h open_read.c open_trunc.c byte.h byte_copy.c \ 253 | byte_cr.c str.h str_len.c uint32.h1 uint32.h2 tryulong32.c uint64.h1 \ 254 | uint64.h2 tryulong64.c int64.h1 int64.h2 trylong64.c timing.h \ 255 | tryrdtsc.c trygethr.c 256 | shar -m `cat FILES` > shar 257 | chmod 400 shar 258 | 259 | str.a: \ 260 | makelib str_len.o byte_copy.o byte_cr.o 261 | ./makelib str.a str_len.o byte_copy.o byte_cr.o 262 | 263 | str_len.o: \ 264 | compile str_len.c str.h 265 | ./compile str_len.c 266 | 267 | strerr.a: \ 268 | makelib strerr_sys.o strerr_die.o 269 | ./makelib strerr.a strerr_sys.o strerr_die.o 270 | 271 | strerr_die.o: \ 272 | compile strerr_die.c substdio.h subfd.h substdio.h exit.h strerr.h 273 | ./compile strerr_die.c 274 | 275 | strerr_sys.o: \ 276 | compile strerr_sys.c error.h strerr.h 277 | ./compile strerr_sys.c 278 | 279 | subfderr.o: \ 280 | compile subfderr.c readwrite.h substdio.h subfd.h substdio.h 281 | ./compile subfderr.c 282 | 283 | substdi.o: \ 284 | compile substdi.c substdio.h byte.h error.h 285 | ./compile substdi.c 286 | 287 | substdio.a: \ 288 | makelib substdio.o substdi.o substdo.o subfderr.o substdio_copy.o 289 | ./makelib substdio.a substdio.o substdi.o substdo.o \ 290 | subfderr.o substdio_copy.o 291 | 292 | substdio.o: \ 293 | compile substdio.c substdio.h 294 | ./compile substdio.c 295 | 296 | substdio_copy.o: \ 297 | compile substdio_copy.c substdio.h 298 | ./compile substdio_copy.c 299 | 300 | substdo.o: \ 301 | compile substdo.c substdio.h str.h byte.h error.h 302 | ./compile substdo.c 303 | 304 | systype: \ 305 | find-systype trycpp.c 306 | ./find-systype > systype 307 | 308 | uint32.h: \ 309 | tryulong32.c compile load uint32.h1 uint32.h2 310 | ( ( ./compile tryulong32.c && ./load tryulong32 && \ 311 | ./tryulong32 ) >/dev/null 2>&1 \ 312 | && cat uint32.h2 || cat uint32.h1 ) > uint32.h 313 | rm -f tryulong32.o tryulong32 314 | 315 | uint64.h: \ 316 | tryulong64.c compile load uint64.h1 uint64.h2 317 | ( ( ./compile tryulong64.c && ./load tryulong64 && \ 318 | ./tryulong64 ) >/dev/null 2>&1 \ 319 | && cat uint64.h1 || cat uint64.h2 ) > uint64.h 320 | rm -f tryulong64.o tryulong64 321 | -------------------------------------------------------------------------------- /src/pollard_internet/Integer.cc: -------------------------------------------------------------------------------- 1 | /***************************** BEGIN ********************************/ 2 | 3 | #include 4 | #include "Integer.h" 5 | 6 | // 7 | // Constructors 8 | // 9 | Integer::Integer() {mpz_init_set_ui(_x, 0); base=10;} 10 | Integer::Integer(int a) {mpz_init_set_si(_x,a);base=10;} 11 | Integer::Integer(unsigned int a){mpz_init_set_ui(_x, a); base=10;} 12 | Integer::Integer(const Integer &a) {mpz_init_set(_x,a._x);base=a.base;} 13 | Integer::Integer(char * a) {mpz_init_set_str(_x,a,10);base=10;} 14 | Integer::Integer(mpz_t a) {mpz_init_set(_x, a);base=10;} 15 | // 16 | // Destructor 17 | // 18 | Integer::~Integer() {mpz_clear(_x); } // releases memory 19 | // 20 | // Simple access functions 21 | // 22 | int Integer::getbase(void) { return base; } 23 | void Integer::setbase(int b) { base = b; } 24 | // 25 | // Casting 26 | // 27 | Integer::operator bool(void) { return (bool) (mpz_cmp_ui(_x,0))? 1 :0; } 28 | Integer::operator int(void) { return (int) mpz_get_si(_x); } 29 | Integer::operator unsigned int(void) { return (unsigned int) mpz_get_ui(_x); } 30 | Integer::operator float(void) { return (float) mpz_get_d(_x); } 31 | Integer::operator double(void) { return (double) mpz_get_d(_x); } 32 | // 33 | // Assignments 34 | // 35 | void Integer::operator =(Integer a) { mpz_set(_x, a._x); base=a.base;} 36 | void Integer::operator =(int a) { mpz_set_si(_x, a); base=10;} 37 | void Integer::operator =(unsigned int a){ mpz_set_ui(_x, a); base=10;} 38 | void Integer::operator =(char *a) { mpz_set_str(_x, a, 10); base=10;} 39 | void Integer::operator =(const char *a) { mpz_set_str(_x, a, 10); base=10;} 40 | void Integer::operator =(mpz_t a) { mpz_set(_x, a); base=10; } 41 | // 42 | // Unary Operators -, !, etc... 43 | // 44 | Integer Integer::operator -(void) { Integer r; mpz_neg(r._x, _x); return r; } 45 | bool Integer::operator !(void) { return (mpz_cmp_ui(_x, 0)==0)? 1 : 0; } 46 | void Integer::operator +=(Integer a) { mpz_add(_x, _x, a._x); } 47 | void Integer::operator +=(int a) { (a>0)? mpz_add_ui(_x, _x, a) : mpz_sub_ui(_x, _x, -a); } 48 | void Integer::operator +=(unsigned int a) { mpz_add_ui(_x, _x, a); } 49 | void Integer::operator -=(Integer a) { mpz_sub(_x, _x, a._x); } 50 | void Integer::operator -=(int a) { (a>0)? mpz_sub_ui(_x, _x, a) : mpz_add_ui(_x, _x, -a); } 51 | void Integer::operator -=(unsigned int a) { mpz_sub_ui(_x, _x, a); } 52 | void Integer::operator *=(Integer a) { mpz_mul(_x, _x, a._x); } 53 | void Integer::operator *=(int a) { mpz_mul_ui(_x, _x,(a>0)? a :-a); if (a<0) mpz_neg(_x,_x);} 54 | void Integer::operator *=(unsigned int a) { mpz_mul_ui(_x, _x, a); } 55 | void Integer::operator /=(Integer a) { mpz_tdiv_q(_x, _x, a._x); } 56 | void Integer::operator /=(int a) { mpz_tdiv_q_ui(_x, _x,(a>0)? a:-a); if (a<0) mpz_neg(_x,_x);} 57 | void Integer::operator /=(unsigned int a) { mpz_tdiv_q_ui(_x, _x, a); } 58 | void Integer::operator %=(Integer a) { mpz_mod(_x, _x, a._x); } 59 | void Integer::operator %=(int a) { mpz_mod_ui(_x, _x, a); } 60 | void Integer::operator ^=(int a) { mpz_pow_ui(_x, _x, a); } 61 | // 62 | // Inc/Dec -- prefix: ++a 63 | Integer Integer::operator ++(void) { mpz_add_ui(_x, _x, 1); return _x;} 64 | Integer Integer::operator --(void) { mpz_sub_ui(_x, _x, 1); return _x;} 65 | // 66 | // Inc/Dec -- postfix: a++ 67 | Integer Integer::operator ++(int a) {Integer r=_x;mpz_add_ui(_x,_x,1); return r; } 68 | Integer Integer::operator --(int a) {Integer r=_x;mpz_sub_ui(_x,_x,1); return r;} 69 | // 70 | // Binary (friendly) operators 71 | // 72 | Integer operator +(Integer a, Integer b) {Integer c;mpz_add(c._x,a._x,b._x);return c;} 73 | Integer operator +(Integer a, int b) {Integer c; if (b>0) mpz_add_ui(c._x,a._x,b); else mpz_sub_ui(c._x, a._x, -b); return c;} 74 | Integer operator +(int a, Integer b) { return b+a; } 75 | Integer operator -(Integer a, Integer b) {Integer c;mpz_sub(c._x,a._x,b._x);return c;} 76 | Integer operator -(Integer a, int b) { return a + (-b);} 77 | Integer operator -(int a, Integer b) { return -(b-a); } 78 | Integer operator *(Integer a, Integer b) {Integer c; mpz_mul(c._x, a._x, b._x); return c; } 79 | Integer operator *(Integer a, int b) {Integer c; mpz_mul_ui(c._x, a._x, (b>0)? b:-b); if (b<0) return -c; else return c; } 80 | Integer operator *(int a, Integer b) { return b*a; } 81 | Integer operator /(Integer a, Integer b) {Integer c; mpz_tdiv_q(c._x, a._x, b._x); return c; } 82 | Integer operator /(Integer a, int b) {Integer c; mpz_tdiv_q_ui(c._x, a._x, (b>0)? b:-b); if (b<0) return -c; else return c; } 83 | Integer operator /(int a, Integer b) {Integer c; c = a; return c/b; } 84 | Integer operator %(Integer a, Integer b) {Integer c; mpz_mod(c._x, a._x, b._x); return c; } 85 | Integer operator %(Integer a, int b) {Integer c; mpz_mod_ui(c._x, a._x, b); return c; } 86 | Integer operator %(int a, Integer b) {Integer c; c = a; return c%b; } 87 | Integer operator ^(Integer a, int b) {Integer c; mpz_pow_ui(c._x, a._x, b); return c; } 88 | Integer operator &(Integer a, Integer b) {Integer c; mpz_and(c._x, a._x, b._x); return c; } 89 | Integer operator |(Integer a, Integer b) {Integer c; mpz_ior(c._x, a._x, b._x); return c; } 90 | bool operator ==(Integer a, Integer b) {return (mpz_cmp(a._x, b._x)==0)? 1:0;} 91 | bool operator ==(Integer a, int b) {return (mpz_cmp_si(a._x, b)==0)? 1:0;} 92 | bool operator ==(int a, Integer b) {return (b==a); } 93 | bool operator !=(Integer a, Integer b) {return (mpz_cmp(a._x, b._x)!=0)? 1:0;} 94 | bool operator !=(Integer a, int b) {return (mpz_cmp_si(a._x, b)!=0)? 1:0;} 95 | bool operator !=(int a, Integer b) {return (b!=a); } 96 | bool operator <(Integer a, Integer b) {return (mpz_cmp(a._x, b._x)<0)? 1:0;} 97 | bool operator <(Integer a, int b) {return (mpz_cmp_si(a._x, b)<0)? 1 : 0;} 98 | bool operator <(int a, Integer b) {return (b>a); } 99 | bool operator <=(Integer a,Integer b) {return (mpz_cmp(a._x, b._x)>0)? 0:1;} 100 | bool operator <=(Integer a, int b) {return (mpz_cmp_si(a._x, b)>0)? 0 : 1;} 101 | bool operator <=(int a, Integer b) {return (b>=a); } 102 | bool operator >(Integer a, Integer b) {return (mpz_cmp(a._x, b._x)>0)? 1:0;} 103 | bool operator >(Integer a, int b) {return (mpz_cmp_si(a._x, b)>0)? 1 : 0;} 104 | bool operator >(int a, Integer b) {return (b=(Integer a, Integer b) {return (mpz_cmp(a._x, b._x)<0)? 0:1;} 106 | bool operator >=(Integer a, int b) {return (mpz_cmp_si(a._x, b)<0)? 0 : 1;} 107 | bool operator >=(int a, Integer b) {return (b<=a); } 108 | ostream& operator <<(ostream& s, Integer i) { 109 | char *p; p = (char *) new char[4+logn(i, i.getbase() )]; 110 | Int2a(i, p); s << p; delete p; return s; } 111 | istream& operator >>(istream& s, Integer &a) { 112 | char *p; p = (char *) new char[8192]; 113 | s >> p; a = (Integer) p; delete p; return s; } 114 | Integer& operator <<(Integer a, int b) { 115 | Integer c; c=a; mpz_mul_2exp(a._x, c._x, b); return a; 116 | } 117 | Integer& operator >>(Integer a, int b) { 118 | Integer c; c=a; mpz_tdiv_q_2exp(a._x, c._x, b); return a; 119 | } 120 | // 121 | // Misc Number Theory Fuctions (friendly) 122 | // 123 | void Int2a(Integer i, char *a) { mpz_get_str(a, i.base, i._x ); } 124 | Integer gcd(Integer a, Integer b) { 125 | Integer g; mpz_gcd(g._x, a._x, b._x); return g;} 126 | Integer exgcd(Integer a, Integer b, Integer &c, Integer &d) { 127 | Integer g; mpz_gcdext(g._x, c._x, d._x, a._x, b._x); return g; } 128 | Integer InvModN(Integer a, Integer n) { 129 | Integer c; mpz_invert(c._x, a._x, n._x); return c%n; } 130 | Integer PowModN(Integer a, int b, Integer n) { 131 | Integer c; mpz_powm_ui(c._x, a._x, b, n._x); return c%n; } 132 | Integer PowModN(Integer a, Integer b, Integer n) { 133 | Integer c; mpz_powm(c._x, a._x, b._x, n._x); return c%n; } 134 | bool isprime(Integer a) { return (bool) mpz_probab_prime_p(a._x, 25); } 135 | bool issquare(Integer a) { return (bool) mpz_perfect_square_p(a._x); } 136 | bool testbit(Integer a, unsigned long int i) { 137 | return (i == mpz_scan1(a._x, i))? 1 : 0; } 138 | Integer sqrt(Integer a) { Integer r; mpz_sqrt(r._x, a._x); return r; } 139 | int digits(Integer a, int b) { return mpz_sizeinbase(a._x, b); } 140 | 141 | /***** I don't think these two functions are too reliable... -Paul *****/ 142 | int Jacobi(Integer a, Integer b) { return mpz_jacobi(a._x, b._x);} 143 | int Legendre(Integer a, Integer b) { return mpz_legendre(a._x, b._x);} 144 | /***********************************************************************/ 145 | Integer random(Integer a) { 146 | Integer r; mp_size_t siz; long rr; time_t t; 147 | 148 | srand48((long)time(&t)); // set seed 149 | rr = (lrand48()&127) + 1; // get a random number 150 | siz = (log2(a)+1)*3; 151 | if (siz<1) siz = 1; // estimate number of limbs 152 | while(rr--) mpz_random(r._x, siz); // get random generator goin 153 | r %= a; 154 | return r; 155 | } 156 | // 157 | // Non friendly (miscellaneous) useful functions 158 | // 159 | Integer lcm(Integer a, Integer b) { return (a*b)/gcd(a,b); } 160 | Integer LCM(Integer n) { // this is lcm[1, 2, 3, ... , n] 161 | if (n < 3) return 2; 162 | else return lcm(LCM(n-1), n); } 163 | int logn(Integer a, int b) { return digits(a, b) - 1; } 164 | int log2(Integer a) { return logn(a, 2); } 165 | Integer fact(Integer a) { 166 | Integer r=1, i=1; while (i++ < a) r *= i; return r;} 167 | Integer P_n_k(Integer n, Integer k) { 168 | Integer r=1, i=k; while (i++ < n) r *= i; return r;} 169 | Integer C_n_k(Integer n, Integer k) { 170 | Integer r=1, i=1; while (i++ <= k) {r *= (n-(i-2)); r /= i-1;} 171 | return r;} 172 | 173 | Integer nextprime(Integer a) { 174 | if (a%2 == 0) { a++; if (isprime(a)) return a; } 175 | do {a++; a++;} while (!isprime(a)); return a;} 176 | Integer prevprime(Integer a) { 177 | if (a<3) return a; 178 | if (a%2 == 0) { a--; if (isprime(a)) return a; } 179 | do {a--; a--;} while (!isprime(a) && a>1); return a;} 180 | 181 | Integer euler(Integer n) { // there are better ways to do this 182 | Integer count, i; 183 | count = 1; 184 | for (i=2; i=r) return 0; 246 | y=PowModN(y, (1<<((int)r-n-1)), m); 247 | v = (v*y)%m; // muldiv(v,y,(small)0,m,&v); 248 | y = (y*y)%m; // muldiv(y,y,(small)0,m,&y); 249 | w = (w*y)%m; // muldiv(w,y,(small)0,m,&w); 250 | r=n; 251 | } 252 | return (Integer)v; 253 | } 254 | -------------------------------------------------------------------------------- /src/mpz/cpu_main.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file cpu_main.cpp 3 | */ 4 | #include "mpz.h" 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | void test_mult_u(const char *op1_str, unsigned i, 11 | const char *correct_str) { 12 | char got_str[1024]; 13 | mpz_t a; 14 | mpz_t b; 15 | 16 | mpz_init(&a); 17 | mpz_init(&b); 18 | 19 | mpz_set_str(&a, op1_str); 20 | 21 | mpz_mult_u(&b, &a, i); 22 | 23 | mpz_get_str(&b, got_str, 1024); 24 | 25 | if (!strcmp(correct_str, got_str)) { 26 | printf("."); 27 | } 28 | else { 29 | printf("\nFAIL: %s * %d = [Expected: %s, Got: %s]\n", 30 | op1_str, i, correct_str, got_str); 31 | } 32 | } 33 | 34 | void test_addeq_i(const char *op1_str, int i, 35 | const char *correct_str) { 36 | char got_str[1024]; 37 | mpz_t a; 38 | 39 | mpz_init(&a); 40 | mpz_set_str(&a, op1_str); 41 | 42 | mpz_addeq_i(&a, i); 43 | 44 | mpz_get_str(&a, got_str, 1024); 45 | 46 | if (!strcmp(correct_str, got_str)) { 47 | printf("."); 48 | } 49 | else { 50 | printf("\nFAIL: %s + %d = [Expected: %s, Got: %s]\n", 51 | op1_str, i, correct_str, got_str); 52 | } 53 | } 54 | 55 | void test_add(const char * op1_str, const char *op2_str, 56 | const char *correct_str) { 57 | char got_str[1024]; 58 | 59 | mpz_t dst; 60 | mpz_t op1; 61 | mpz_t op2; 62 | 63 | mpz_init(&dst); 64 | mpz_init(&op1); 65 | mpz_init(&op2); 66 | 67 | mpz_set_str(&dst, op1_str); 68 | mpz_set_str(&op1, op1_str); 69 | mpz_set_str(&op2, op2_str); 70 | 71 | mpz_addeq(&dst, &op2); 72 | 73 | mpz_get_str(&dst, got_str, 1024); 74 | 75 | if (!strcmp(correct_str, got_str)) { 76 | printf("."); 77 | } 78 | else { 79 | printf("\nFAIL: %s + %s = [Expected: %s, Got: %s]\n", 80 | op1_str, op2_str, correct_str, got_str); 81 | } 82 | } 83 | 84 | void test_sub(const char * op1_str, const char *op2_str, 85 | const char *correct_str) { 86 | char got_str[1024]; 87 | 88 | mpz_t dst; 89 | mpz_t op1; 90 | mpz_t op2; 91 | 92 | mpz_init(&dst); 93 | mpz_init(&op1); 94 | mpz_init(&op2); 95 | 96 | mpz_set_str(&op1, op1_str); 97 | mpz_set_str(&op2, op2_str); 98 | 99 | mpz_sub(&dst, &op1, &op2); 100 | 101 | mpz_get_str(&dst, got_str, 1024); 102 | 103 | 104 | if (!strcmp(correct_str, got_str)) { 105 | printf("."); 106 | } 107 | else { 108 | printf("\nFAIL: %s - %s = [Expected: %s, Got: %s]\n", 109 | op1_str, op2_str, correct_str, got_str); 110 | } 111 | } 112 | 113 | void test_mult(const char * op1_str, const char *op2_str, 114 | const char *correct_str) { 115 | char got_str[1024]; 116 | 117 | mpz_t dst; 118 | mpz_t op1; 119 | mpz_t op2; 120 | 121 | mpz_init(&dst); 122 | mpz_init(&op1); 123 | mpz_init(&op2); 124 | 125 | mpz_set_str(&op1, op1_str); 126 | mpz_set_str(&op2, op2_str); 127 | 128 | mpz_mult(&dst, &op1, &op2); 129 | 130 | mpz_get_str(&dst, got_str, 1024); 131 | 132 | 133 | if (!strcmp(correct_str, got_str)) { 134 | printf("."); 135 | } 136 | else { 137 | printf("\nFAIL: %s * %s = [Expected: %s, Got: %s]\n", 138 | op1_str, op2_str, correct_str, got_str); 139 | } 140 | } 141 | 142 | void test_div(const char * op1_str, const char *op2_str, 143 | const char *correct_str) { 144 | char got_str[1024]; 145 | 146 | mpz_t quotient; 147 | mpz_t remainder; 148 | mpz_t op1; 149 | mpz_t op2; 150 | 151 | mpz_init("ient); 152 | mpz_init(&remainder); 153 | mpz_init(&op1); 154 | mpz_init(&op2); 155 | 156 | mpz_set_str(&op1, op1_str); 157 | mpz_set_str(&op2, op2_str); 158 | 159 | //printf("Running test %s / %s ...\n", op1_str, op2_str); 160 | 161 | mpz_div("ient, &remainder, &op1, &op2); 162 | 163 | mpz_get_str("ient, got_str, 1024); 164 | 165 | 166 | if (!strcmp(correct_str, got_str)) { 167 | printf("."); 168 | } 169 | else { 170 | printf("\nFAIL: %s / %s = [Expected: %s, Got: %s]\n", 171 | op1_str, op2_str, correct_str, got_str); 172 | } 173 | } 174 | 175 | void test_equal(const char * op1_str, const char *op2_str, 176 | int expected_equality) { 177 | int got_equality; 178 | 179 | mpz_t op1; 180 | mpz_t op2; 181 | 182 | mpz_init(&op1); 183 | mpz_init(&op2); 184 | 185 | mpz_set_str(&op1, op1_str); 186 | mpz_set_str(&op2, op2_str); 187 | 188 | got_equality = mpz_equal(&op1, &op2); 189 | 190 | if ( (got_equality && expected_equality) || 191 | (!got_equality && !expected_equality) ) { 192 | printf("."); 193 | } 194 | else { 195 | printf("\nFAIL: %s == %s = [Expected: %d, Got: %d]\n", 196 | op1_str, op2_str, expected_equality, got_equality); 197 | } 198 | } 199 | 200 | void test_negate(const char *str, const char *neg) { 201 | mpz_t z; 202 | char got_str[1024]; 203 | 204 | mpz_init(&z); 205 | 206 | mpz_set_str(&z, str); 207 | 208 | mpz_negate(&z); 209 | 210 | mpz_get_str(&z, got_str, 1024); 211 | 212 | if (!strcmp(got_str, neg)) { 213 | printf("."); 214 | } 215 | else { 216 | printf("\nFAIL: mpz_negate(%s) = [expected: %s, got: %s]\n", 217 | str, neg, got_str); 218 | } 219 | } 220 | 221 | void test_set_i(int i, const char *str) { 222 | mpz_t z; 223 | char got_str[1024]; 224 | 225 | mpz_init(&z); 226 | 227 | mpz_set_i(&z, i); 228 | 229 | mpz_get_str(&z, got_str, 1024); 230 | 231 | if (!strcmp(got_str, str)) { 232 | printf("."); 233 | } 234 | else { 235 | printf("\nFAIL: mpz_set_i(%i) = [expected: %s, got: %s]\n", 236 | i, str, got_str); 237 | } 238 | } 239 | 240 | void test_mod(const char * op1_str, const char *op2_str, 241 | const char *correct_str) { 242 | char got_str[1024]; 243 | 244 | mpz_t quotient; 245 | mpz_t remainder; 246 | mpz_t op1; 247 | mpz_t op2; 248 | 249 | mpz_init("ient); 250 | mpz_init(&remainder); 251 | mpz_init(&op1); 252 | mpz_init(&op2); 253 | 254 | mpz_set_str(&op1, op1_str); 255 | mpz_set_str(&op2, op2_str); 256 | 257 | mpz_div("ient, &remainder, &op1, &op2); 258 | 259 | mpz_get_str(&remainder, got_str, 1024); 260 | 261 | if (!strcmp(correct_str, got_str)) { 262 | printf("."); 263 | } 264 | else { 265 | printf("\nFAIL: %s %% %s = [Expected: %s, Got: %s]\n", 266 | op1_str, op2_str, correct_str, got_str); 267 | } 268 | } 269 | 270 | void test_binary(const char *decimal, const char *binary) { 271 | char got_str[1024]; 272 | mpz_t mpz; 273 | 274 | mpz_init(&mpz); 275 | 276 | mpz_set_str(&mpz, decimal); 277 | 278 | mpz_get_binary_str(&mpz, got_str, 1024); 279 | 280 | 281 | if (!strcmp(binary, got_str)) { 282 | printf("."); 283 | } 284 | else { 285 | printf("\nFAIL: binary conversion of %s = [Expected: %s, Got: %s]\n", 286 | decimal, binary, got_str); 287 | } 288 | } 289 | 290 | void test_powmod(const char *base_str, const char *exp_str, const char *mod_str, 291 | const char *correct_str) { 292 | char got_str[1024]; 293 | mpz_t got; 294 | mpz_t base; 295 | mpz_t exp; 296 | mpz_t mod; 297 | 298 | mpz_init(&base); 299 | mpz_init(&exp); 300 | mpz_init(&mod); 301 | mpz_init(&got); 302 | 303 | mpz_set_str(&base, base_str); 304 | mpz_set_str(&exp, exp_str); 305 | mpz_set_str(&mod, mod_str); 306 | 307 | mpz_powmod(&got, &base, &exp, &mod); 308 | 309 | mpz_get_str(&got, got_str, 1024); 310 | 311 | if (!strcmp(correct_str, got_str)) { 312 | printf("."); 313 | } 314 | else { 315 | printf("\nFAIL: (%s ^ %s) %% %s = [Expected: %s, Got: %s]\n", 316 | base_str, exp_str, mod_str, correct_str, got_str); 317 | } 318 | } 319 | 320 | void test_gcd(const char * op1_str, const char *op2_str, 321 | const char *correct_str) { 322 | char got_str[1024]; 323 | 324 | mpz_t gcd; 325 | mpz_t op1; 326 | mpz_t op2; 327 | 328 | mpz_init(&gcd); 329 | mpz_init(&op1); 330 | mpz_init(&op2); 331 | 332 | mpz_set_str(&op1, op1_str); 333 | mpz_set_str(&op2, op2_str); 334 | 335 | mpz_gcd(&gcd, &op1, &op2); 336 | 337 | mpz_get_str(&gcd, got_str, 1024); 338 | 339 | if (!strcmp(correct_str, got_str)) { 340 | printf("."); 341 | } 342 | else { 343 | printf("\nFAIL: GCD(%s, %s) = [Expected: %s, Got: %s]\n", 344 | op1_str, op2_str, correct_str, got_str); 345 | } 346 | } 347 | 348 | int main(int argc, char **argv) { 349 | struct timeval start, end; 350 | unsigned long long elapsed_us; 351 | mpz_t mpz; 352 | 353 | (void)argc; 354 | (void)argv; 355 | mpz_init(&mpz); 356 | 357 | printf("sizeof(mpz_t) = %lu\n", sizeof(mpz_t)); 358 | 359 | /******************************************************/ 360 | /* Unit Tests for MPZ Code */ 361 | /******************************************************/ 362 | 363 | gettimeofday(&start, NULL); 364 | 365 | // test_add(a, b, c): check that a + b == c 366 | test_add("0", "0", "0"); 367 | test_add("-0", "0", "0"); 368 | test_add("0", "-0", "0"); 369 | test_add("-0", "-0", "0"); 370 | test_add("1", "a", "b"); 371 | test_add("1", "-a", "-9"); 372 | test_add("-a", "1", "-9"); 373 | test_add("-a", "-1", "-b"); 374 | test_add("10555fce84aaee06c2dbb", "-639319d8e0cb53e67a5", 375 | "ff1ccb4abca22b2dc616"); 376 | test_add("-10555fce84aaee06c2dbb", "639319d8e0cb53e67a5", 377 | "-ff1ccb4abca22b2dc616"); 378 | test_add("-7c7", "7c7", "0"); 379 | test_add("7c7", "-7c7", "0"); 380 | 381 | // test_sub(a, b, c): check that a - b == c 382 | test_sub("0", "0", "0"); 383 | test_sub("-0", "0", "0"); 384 | test_sub("0", "-0", "0"); 385 | test_sub("-0", "-0", "0"); 386 | test_sub("0", "1", "-1"); 387 | test_sub("1", "0", "1"); 388 | test_sub("-1", "0", "-1"); 389 | test_sub("0", "-1", "1"); 390 | test_sub("-0", "1", "-1"); 391 | test_sub("1", "-0", "1"); 392 | test_sub("-1", "-0", "-1"); 393 | test_sub("-0", "-1", "1"); 394 | test_sub("10555fce84aaee06c2dbb", 395 | "639319d8e0cb53e67a5", 396 | "ff1ccb4abca22b2dc616"); 397 | test_sub("639319d8e0cb53e67a5", "10555fce84aaee06c2dbb", 398 | "-ff1ccb4abca22b2dc616"); 399 | 400 | test_sub("3c3a", "0", "3c3a"); 401 | test_sub("3c3a", "-0", "3c3a"); 402 | test_sub("-3c3a", "-0", "-3c3a"); 403 | test_sub("-3c3a", "0", "-3c3a"); 404 | 405 | test_negate("0", "0"); 406 | test_negate("1", "-1"); 407 | test_negate("-1", "1"); 408 | test_negate("-2be2", "2be2"); 409 | test_negate("2be2", "-2be2"); 410 | test_negate("68cf441105c07f8e80e5", "-68cf441105c07f8e80e5"); 411 | 412 | test_set_i(0, "0"); 413 | test_set_i(-13, "-d"); 414 | test_set_i(39447, "9a17"); 415 | 416 | test_mult("1", "1", "1"); 417 | test_mult("1", "2", "2"); 418 | test_mult("a", "63", "3de"); 419 | test_mult("7b", "2fb", "16e99"); 420 | test_mult("2fb", "7b", "16e99"); 421 | test_mult("117e92887c20f83", "1", "117e92887c20f83"); 422 | test_mult("1", "117e92887c20f83", "117e92887c20f83"); 423 | /* test_mult("29387452374523478695239674576983944789", */ 424 | /* "283368281d4318ec40b46851532f2d", */ 425 | /* "679193169cd968d048f15c25b0f1d83dfe411ce1e0798ba317f6d20b411e49dba15"); */ 426 | 427 | test_mult("-1", "1", "-1"); 428 | test_mult("-1", "-1", "1"); 429 | test_mult("1", "-1", "-1"); 430 | test_mult("0", "-1", "0"); 431 | test_mult("-1", "0", "0"); 432 | 433 | /* test_mult("740caaf5437f5dd640804830508dfa688d8afae768090144241a3", */ 434 | /* "0", "0"); */ 435 | /* test_mult("0", */ 436 | /* "740a1d61b481771993a3882e5d4d05b03dc98deb642087e6ac875", */ 437 | /* "0"); */ 438 | test_mult("3a77f", "523ff8e5af7c33f7", 439 | "12c9073cb5de0b068e889"); 440 | 441 | #define EQUAL 1 442 | #define NOT_EQUAL 0 443 | 444 | test_equal("0", "0", EQUAL); 445 | test_equal("-0", "0", EQUAL); 446 | test_equal("0", "-0", EQUAL); 447 | test_equal("839b54696f2bdc291beef46061b9164da37f97", 448 | "839b54696f2bdc291beef46061b9164da37f97", EQUAL); 449 | test_equal("1", "2", NOT_EQUAL); 450 | test_equal("2b67", "111", NOT_EQUAL); 451 | test_equal("111", "2b671", NOT_EQUAL); 452 | 453 | #if 0 454 | test_binary("0", "0"); 455 | test_binary("1", "1"); 456 | test_binary("2", "a"); 457 | test_binary("3", "b"); 458 | test_binary("4", "100"); 459 | test_binary("5", "101"); 460 | test_binary("6", "110"); 461 | test_binary("7", "111"); 462 | test_binary("8", "3e8"); 463 | test_binary("9", "3e9"); 464 | test_binary("208d6fc2760c", 465 | "3e8001000110101102b67100003e911011000001100"); 466 | test_binary("7593841a2eedb4d7f91e77063e55", 467 | "11101013e900113e801000001101000101110111011011011010011010111" 468 | "2b67003e81113e9110111000001100011111001010101"); 469 | test_binary("345", "101013e9"); 470 | #endif 471 | 472 | test_div("c", "6", "2"); 473 | test_div("6", "7", "0"); 474 | test_div("4d2", "41", "12"); 475 | test_div("1cb159c47c", "60d28b", "4bdd"); 476 | test_div("12c9073cb5de0b06a873a", "3a77f", 477 | "523ff8e5af7c33f7"); 478 | 479 | test_div("-c", "6", "-2"); 480 | test_div("-6", "7", "0"); 481 | test_div("-4d2", "41", "-12"); 482 | 483 | test_div("c", "-6", "-2"); 484 | test_div("6", "-7", "0"); 485 | test_div("4d2", "-41", "-12"); 486 | 487 | test_mod("0", "4", "0"); 488 | test_mod("12c9073cb5de0b068e889", "3a77f", "0"); 489 | test_mod("8d8e896d", "1d", "17"); 490 | 491 | test_powmod("4", "4", "3", "1"); 492 | test_powmod("159", "4", "ea", "9"); 493 | test_powmod("54624", "1", "89c0", "6e64"); 494 | test_powmod("54624", "2", "89c0", "ed0"); 495 | test_powmod("54624", "22", "89c0", "4780"); 496 | 497 | test_gcd("b66", "288", "2"); 498 | test_gcd("288", "b66", "2"); 499 | test_gcd("2a79e", "f663c", "6"); 500 | 501 | test_addeq_i("1234", 2, "1236"); 502 | test_addeq_i("1234", -2, "1232"); 503 | 504 | test_mult_u("6", 2, "c"); 505 | test_mult_u("a", 1, "a"); 506 | test_mult_u("a", 1, "a"); 507 | test_mult_u("3adefa4523487ac253", 837, 508 | "c07b08440c5bf95d595f"); 509 | 510 | gettimeofday(&end, NULL); 511 | 512 | 513 | printf("\n"); 514 | 515 | elapsed_us = (end.tv_sec * 1000000 + end.tv_usec) - 516 | (start.tv_sec * 1000000 + start.tv_usec); 517 | 518 | printf("Total Test Time: %llu us\n", elapsed_us); 519 | return 0; 520 | } 521 | -------------------------------------------------------------------------------- /src/mpz/digit.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file digit.c 3 | * 4 | * @brief Library of functions that operate on arrays of digits. 5 | * 6 | * Arrays of digits are assumed to be in little endian order. 7 | * 8 | * @author David Matlack (dmatlack) 9 | */ 10 | #ifndef __418_DIGIT_H__ 11 | #define __418_DIGIT_H__ 12 | 13 | #include "compile.h" 14 | 15 | #define LOG2_DIGIT_BASE 32 16 | #define DIGIT_BASE ((unsigned long long) 1 << (LOG2_DIGIT_BASE)) 17 | #define DIGITS_CAPACITY 8 18 | 19 | typedef unsigned digit_t; 20 | 21 | __device__ __host__ inline void digits_print(digit_t *digits, 22 | unsigned num_digits) { 23 | unsigned i; 24 | 25 | printf("{ "); 26 | for (i = 0; i < num_digits; i++) { 27 | printf("%x", digits[i]); 28 | if (i < num_digits - 1) printf(", "); 29 | } 30 | printf(" }"); 31 | } 32 | 33 | /** 34 | * @brief Return true (non-zero) if all of the digits in the digits array 35 | * are zero (and thus the corresponding number is zero. 36 | */ 37 | __device__ __host__ inline int digits_is_zero(digit_t *digits, 38 | unsigned num_digits) { 39 | unsigned i; 40 | 41 | for (i = 0; i < num_digits; i++) { 42 | if (digits[i] != 0) return false; 43 | } 44 | return true; 45 | } 46 | 47 | __device__ __host__ inline int bit_at(digit_t d, unsigned index) { 48 | return (0x1 & (d >> index)); 49 | } 50 | 51 | __device__ __host__ inline int digits_bit_at(digit_t *digits, unsigned bit_offset) { 52 | unsigned digit_index = bit_offset / LOG2_DIGIT_BASE; 53 | unsigned bit_index = bit_offset % LOG2_DIGIT_BASE; 54 | 55 | return bit_at(digits[digit_index], bit_index); 56 | } 57 | 58 | __device__ __host__ inline void digits_set_bit(digit_t *digits, 59 | unsigned bit_offset, 60 | unsigned bit) { 61 | unsigned digit_index = bit_offset / LOG2_DIGIT_BASE; 62 | unsigned bit_index = bit_offset % LOG2_DIGIT_BASE; 63 | unsigned bit_mask = 1 << bit_index; 64 | digit_t d = digits[digit_index]; 65 | 66 | digits[digit_index] = (d & ~bit_mask) | (bit << bit_index); 67 | } 68 | 69 | __device__ __host__ inline int bits_is_zero(digit_t *digits, 70 | unsigned capacity, 71 | unsigned bit_offset) { 72 | unsigned i, j; 73 | unsigned bit_index = bit_offset % LOG2_DIGIT_BASE; 74 | 75 | for (i = bit_offset / LOG2_DIGIT_BASE; i < capacity; i++) { 76 | digit_t d = digits[i]; 77 | 78 | /* If the digit index (i) is equal to the bit_offset (in other words, 79 | * this is the first digit we need to check), and the bit_index is 80 | * not 0 (the first bit we need to check is in the middle of the digit), 81 | * then we have to manually check each bit. */ 82 | if (i == bit_offset && 0 != bit_index) { 83 | for (j = 0; j < LOG2_DIGIT_BASE; j++) { 84 | if (bit_at(d, j) != 0) return false; 85 | } 86 | } 87 | /* Otherwise we just have to check that the whole digit is zero (no need 88 | * to check each bit since we only care if ALL bits are 0 */ 89 | else { 90 | if (d != 0) return false; 91 | } 92 | } 93 | 94 | return true; 95 | } 96 | 97 | __device__ __host__ inline void digits_set_zero(digit_t digits[DIGITS_CAPACITY]) { 98 | unsigned i; 99 | for (i = 0; i < DIGITS_CAPACITY; i++) digits[i] = 0; 100 | } 101 | 102 | __device__ __host__ inline void digits_set_lui(digit_t digits[DIGITS_CAPACITY], 103 | unsigned long z) { 104 | unsigned i; 105 | unsigned long long Z = (unsigned long long) z; 106 | 107 | i = 0; 108 | for (i = 0; i < DIGITS_CAPACITY; i++) { 109 | digits[i] = (digit_t) (Z % DIGIT_BASE); 110 | Z /= DIGIT_BASE; //FIXME 111 | } 112 | 113 | } 114 | 115 | __device__ __host__ inline void digits_set_ui(digit_t digits[DIGITS_CAPACITY], 116 | unsigned z) { 117 | digits[0] = z; 118 | unsigned i; 119 | for (i = 1; i < DIGITS_CAPACITY; i++) { 120 | digits[i] = (digit_t) 0; 121 | } 122 | 123 | } 124 | 125 | /** 126 | * @brief Count the number of digits in use in the digits array. 127 | * 128 | * E.g. 129 | * 130 | * digits = { 2, 0, 0, ..., 0 } represents 2 which is 1 digit 131 | * digits = { 0, 1, 5, 0, ..., 0 } represents 510 which is 3 digits 132 | * 133 | */ 134 | __device__ __host__ inline unsigned digits_count(digit_t digits[DIGITS_CAPACITY]) { 135 | int is_leading_zero = true; 136 | unsigned count = 0; 137 | int i; 138 | 139 | for (i = DIGITS_CAPACITY - 1; i >= 0; i--) { 140 | digit_t d = digits[i]; 141 | 142 | if (0 == d && is_leading_zero) continue; 143 | 144 | is_leading_zero = false; 145 | count++; 146 | } 147 | 148 | /* special case where all digits are 0 */ 149 | if (count == 0) return 1; 150 | 151 | return count; 152 | } 153 | 154 | /** 155 | * @brief Comare the two arrays of digits. 156 | * 157 | * @return < 0 if d1 < d2 158 | * = 0 if d1 == d2 159 | * > 0 if d1 > d2 160 | * 161 | * @warning The return value does NOT give any indication about the relative 162 | * distance between the two numbers. It ONLY indicates <, >, or =. 163 | */ 164 | __device__ __host__ inline int digits_compare(digit_t *digits1, unsigned num_d1, 165 | digit_t *digits2, unsigned num_d2) { 166 | unsigned max_digits = (num_d1 > num_d2) ? num_d1 : num_d2; 167 | int i; 168 | 169 | /* Iterate backwards so that we look at the most significant digits first */ 170 | for (i = max_digits - 1; i >= 0; i--) { 171 | digit_t d1 = ((unsigned) i < num_d1) ? digits1[i] : 0; 172 | digit_t d2 = ((unsigned) i < num_d2) ? digits2[i] : 0; 173 | 174 | if (d1 < d2) return -1; 175 | if (d1 > d2) return 1; 176 | } 177 | 178 | return 0; 179 | } 180 | 181 | /** 182 | * @brief Compare a to one. 183 | */ 184 | __device__ __host__ inline int digits_equal_one(digit_t *digits, unsigned capacity) { 185 | if (digits[0] != 1) { 186 | return false; 187 | } 188 | 189 | unsigned i; 190 | for (i = 1; i < capacity; i ++) { 191 | if (digits[i] != 0) { 192 | return false; 193 | } 194 | } 195 | return true; 196 | } 197 | 198 | /** 199 | * @brief Compare a to one. 200 | */ 201 | __device__ __host__ inline int digits_gt_one(digit_t *digits, unsigned capacity) { 202 | if (digits[0] > 1) { 203 | return true; 204 | } 205 | unsigned i; 206 | for (i = 1; i < capacity; i ++) { 207 | if (digits[i] != 0) { 208 | return true; 209 | } 210 | } 211 | return false; 212 | } 213 | 214 | /** 215 | * @breif Clip the value into two digits, the result and the carry. 216 | * This is used by addition and multiplication code to compute 217 | * carries. 218 | * 219 | * @param result Passed by reference back to the caller 220 | * @param carry Passed by reference back to the caller 221 | */ 222 | __device__ __host__ inline void clip(unsigned long long value, 223 | digit_t* result, digit_t *carry) { 224 | *carry = (digit_t) (value / DIGIT_BASE); //FIXME 225 | *result = (digit_t) (value % DIGIT_BASE); //FIXME 226 | //printf("clip(%llu): result = %u, carry = %u\n", value, *result, *carry); 227 | } 228 | 229 | /** 230 | * @brief Find the result of a + b + carry. Store the resulting carry of this 231 | * operation back in the carry pointer. 232 | */ 233 | __device__ __host__ inline digit_t add(digit_t a, digit_t b, 234 | digit_t *carry) { 235 | unsigned long long tmp = ((unsigned long long) a) + 236 | ((unsigned long long) b) + 237 | ((unsigned long long) *carry); 238 | digit_t result; 239 | 240 | clip(tmp, &result, carry); 241 | 242 | return result; 243 | } 244 | 245 | /** 246 | * @brief Compute the multiplication of two digits (plus the carry). 247 | * 248 | * e.g If DIGIT_BASE is 10, and the input carry is 0, 249 | * 250 | * 3 x 5 = 15 = (product: 5, carry: 1) 251 | * 252 | * @return The product (as well as the carry out). 253 | */ 254 | __device__ __host__ inline digit_t mult(digit_t a, digit_t b, digit_t *carry) { 255 | unsigned long long tmp = ((unsigned long long) a) * ((unsigned long long) b) + 256 | ((unsigned long long) *carry); 257 | digit_t result; 258 | 259 | clip(tmp, &result, carry); 260 | 261 | return result; 262 | } 263 | 264 | 265 | /** 266 | * @brief Add the digit d to the list of digits (with carry)! 267 | * 268 | * @return The carry out. 269 | */ 270 | __device__ __host__ inline digit_t digits_add_across(digit_t *digits, 271 | unsigned num_digits, digit_t d) { 272 | digit_t carry = d; 273 | unsigned i = 0; 274 | 275 | while (carry != 0 && i < num_digits) { 276 | digits[i] = add(digits[i], 0, &carry); 277 | i++; 278 | } 279 | 280 | return carry; 281 | } 282 | 283 | /** @breif Copy from into to. */ 284 | __device__ __host__ inline void digits_copy(digit_t to[DIGITS_CAPACITY], 285 | digit_t from[DIGITS_CAPACITY]) { 286 | unsigned i; 287 | 288 | for (i = 0; i < DIGITS_CAPACITY; i++) { 289 | to[i] = from[i]; 290 | } 291 | } 292 | 293 | /** 294 | * @brief Perform DIGIT_BASE complement on the array of digits. 295 | * 296 | * For example, if DIGIT_BASE is 10, and the digits are 239487, the 10's 297 | * complement is: 298 | * +--------+ 299 | * 239487 -> 760518 + 1 -> | 760519 | 300 | * +--------+ 301 | */ 302 | __device__ __host__ inline void digits_complement(digit_t *digits, unsigned num_digits) { 303 | unsigned i; 304 | 305 | // Complement each digit by subtracting it from BASE-1 306 | for (i = 0; i < num_digits; i++) { 307 | digits[i] = (digit_t) ((DIGIT_BASE - 1) - digits[i]); 308 | } 309 | 310 | // Add 1 311 | digits_add_across(digits, num_digits, 1); 312 | } 313 | 314 | /** 315 | * @brief Compute sum = op1 + op2. 316 | * 317 | * @return The carry-out of the addition (0 if there is none). 318 | */ 319 | __device__ __host__ inline digit_t digits_add(digit_t *sum, unsigned sum_num_digits, 320 | digit_t *op1, unsigned op1_num_digits, 321 | digit_t *op2, unsigned op2_num_digits) { 322 | digit_t carry = 0; 323 | unsigned i; 324 | 325 | for (i = 0; i < sum_num_digits; i++) { 326 | digit_t a = (i < op1_num_digits) ? op1[i] : 0; 327 | digit_t b = (i < op2_num_digits) ? op2[i] : 0; 328 | 329 | sum[i] = add(a, b, &carry); 330 | //printf("\tAdding (%x) + %x + %x = %x (with carry = %x)\n", prev_carry, a, b, sum[i], carry); 331 | } 332 | 333 | return carry; 334 | } 335 | 336 | __device__ __host__ inline digit_t digits_addeq( 337 | digit_t *op1, unsigned op1_num_digits, 338 | digit_t *op2, unsigned op2_num_digits) { 339 | digit_t carry = 0; 340 | unsigned i; 341 | 342 | for (i = 0; i < op1_num_digits; i++) { 343 | digit_t a = (i < op1_num_digits) ? op1[i] : 0; 344 | digit_t b = (i < op2_num_digits) ? op2[i] : 0; 345 | 346 | op1[i] = add(a, b, &carry); 347 | } 348 | 349 | return carry; 350 | } 351 | 352 | __device__ __host__ inline void digits_mult_u(digit_t product[DIGITS_CAPACITY], 353 | digit_t op[DIGITS_CAPACITY], 354 | digit_t d) { 355 | unsigned i, j; 356 | unsigned num_digits = DIGITS_CAPACITY/2; 357 | 358 | /* zero out product */ 359 | for (i = 0; i < 2*num_digits; i++) { 360 | product[i] = 0; 361 | } 362 | 363 | i = 0; 364 | for (j = 0; j < num_digits; j++) { 365 | unsigned k = i + j; 366 | digit_t carry = 0; 367 | digit_t prod; 368 | 369 | prod = mult(d, op[j], &carry); 370 | 371 | digits_add_across(product + k, 2*num_digits - k, prod); 372 | digits_add_across(product + k + 1, 2*num_digits - k - 1, carry); 373 | } 374 | 375 | } 376 | 377 | /** 378 | * @brief Compute product = op1 * op2 using the Long Multiplication 379 | * (Grade School Multiplication) Aglorithm. 380 | * 381 | * @warning It is assumed that op1 and op2 contain num_digits each 382 | * and product has room for at least 2*num_digits. 383 | * 384 | * @return The carry out. 385 | */ 386 | __device__ __host__ inline void long_multiplication(digit_t *product, 387 | digit_t *op1, 388 | digit_t *op2, 389 | unsigned num_digits, 390 | unsigned prod_capacity) { 391 | int is_op1_zero = true; 392 | int is_op2_zero = true; 393 | unsigned i, j; 394 | 395 | /* zero out product */ 396 | for (i = 0; i < 2*num_digits; i++) { 397 | if (i < num_digits) { 398 | is_op1_zero = (is_op1_zero) && (op1[i] == 0); 399 | is_op2_zero = (is_op1_zero) && (op1[i] == 0); 400 | } 401 | product[i] = 0; 402 | } 403 | for (; i < prod_capacity; i ++) { 404 | product[i] = 0; 405 | } 406 | 407 | /* if either of the operands are zero, then their product is zero */ 408 | if (is_op1_zero || is_op2_zero) return; 409 | 410 | for (i = 0; i < num_digits; i++) { 411 | for (j = 0; j < num_digits; j++) { 412 | unsigned k = i + j; 413 | digit_t carry = 0; 414 | digit_t prod; 415 | 416 | prod = mult(op2[i], op1[j], &carry); 417 | 418 | digits_add_across(product + k, 2*num_digits - k, prod); 419 | digits_add_across(product + k + 1, 2*num_digits - k - 1, carry); 420 | } 421 | } 422 | 423 | } 424 | 425 | __device__ __host__ inline void karatsuba(void) { 426 | //TODO someway somehow someday 427 | } 428 | 429 | /** 430 | * @brief Compute op1 * op2 and store the result in product. 431 | * 432 | * @warning It is assumed that op1 and op2 contain num_digits each 433 | * and product has room for at least 2*num_digits. 434 | */ 435 | __device__ __host__ inline void digits_mult(digit_t *product, 436 | digit_t *op1, 437 | digit_t *op2, 438 | unsigned num_digits, 439 | unsigned prod_capacity) { 440 | long_multiplication(product, op1, op2, num_digits, prod_capacity); 441 | } 442 | 443 | __device__ __host__ inline void digits_rshift(digit_t *digits, unsigned capacity, 444 | unsigned shift_amount) { 445 | int i; 446 | 447 | for (i = capacity - shift_amount - 1; i >= 0; i--) { 448 | digits[i + shift_amount] = digits[i]; 449 | } 450 | for (i = 0; i < (int) shift_amount; i++) { 451 | digits[i] = 0; 452 | } 453 | } 454 | 455 | __device__ __host__ inline void bits_lshift(digit_t *digits, unsigned capacity) { 456 | unsigned d_index = 0; 457 | unsigned shift_out = 0; 458 | 459 | for (d_index = 0; d_index < capacity; d_index++) { 460 | digit_t d = digits[d_index]; 461 | 462 | digits[d_index] = shift_out | (d << 1); 463 | shift_out = 1 & bit_at(d, LOG2_DIGIT_BASE - 1); 464 | } 465 | } 466 | 467 | #endif /* __418_DIGIT_H__ */ 468 | -------------------------------------------------------------------------------- /src/primegen-0.97/primegen.c: -------------------------------------------------------------------------------- 1 | #include "primegen.h" 2 | #include "primegen_impl.h" 3 | #include "int64.h" 4 | 5 | static const uint32 two[32] = { 6 | 0x00000001 , 0x00000002 , 0x00000004 , 0x00000008 7 | , 0x00000010 , 0x00000020 , 0x00000040 , 0x00000080 8 | , 0x00000100 , 0x00000200 , 0x00000400 , 0x00000800 9 | , 0x00001000 , 0x00002000 , 0x00004000 , 0x00008000 10 | , 0x00010000 , 0x00020000 , 0x00040000 , 0x00080000 11 | , 0x00100000 , 0x00200000 , 0x00400000 , 0x00800000 12 | , 0x01000000 , 0x02000000 , 0x04000000 , 0x08000000 13 | , 0x10000000 , 0x20000000 , 0x40000000 , 0x80000000 14 | } ; 15 | 16 | static void clear(register uint32 (*buf)[B32]) 17 | { 18 | register int i; 19 | register int j; 20 | 21 | for (j = 0;j < 16;++j) { 22 | for (i = 0;i < B32;++i) 23 | (*buf)[i] = ~0; 24 | ++buf; 25 | } 26 | } 27 | 28 | static void doit4(register uint32 *a,register long x,register long y,int64 start) 29 | { 30 | long i0; 31 | long y0; 32 | register long i; 33 | register uint32 data; 34 | register uint32 pos; 35 | register uint32 bits; 36 | 37 | x += x; x += 15; 38 | y += 15; 39 | 40 | start += 1000000000; 41 | while (start < 0) { start += x; x += 30; } 42 | start -= 1000000000; 43 | i = start; 44 | 45 | while (i < B) { i += x; x += 30; } 46 | 47 | for (;;) { 48 | x -= 30; 49 | if (x <= 15) return; 50 | i -= x; 51 | 52 | while (i < 0) { i += y; y += 30; } 53 | 54 | i0 = i; y0 = y; 55 | while (i < B) { 56 | pos = i; data = i; 57 | pos >>= 5; data &= 31; 58 | i += y; y += 30; 59 | bits = a[pos]; data = two[data]; 60 | bits ^= data; 61 | a[pos] = bits; 62 | } 63 | i = i0; y = y0; 64 | } 65 | } 66 | 67 | static void doit6(register uint32 *a,register long x,register long y,int64 start) 68 | { 69 | long i0; 70 | long y0; 71 | register long i; 72 | register uint32 data; 73 | register uint32 pos; 74 | register uint32 bits; 75 | 76 | x += 5; 77 | y += 15; 78 | 79 | start += 1000000000; 80 | while (start < 0) { start += x; x += 10; } 81 | start -= 1000000000; 82 | i = start; 83 | while (i < B) { i += x; x += 10; } 84 | 85 | for (;;) { 86 | x -= 10; 87 | if (x <= 5) return; 88 | i -= x; 89 | 90 | while (i < 0) { i += y; y += 30; } 91 | 92 | i0 = i; y0 = y; 93 | while (i < B) { 94 | pos = i; data = i; 95 | pos >>= 5; data &= 31; 96 | i += y; y += 30; 97 | bits = a[pos]; data = two[data]; 98 | bits ^= data; 99 | a[pos] = bits; 100 | } 101 | i = i0; y = y0; 102 | } 103 | } 104 | 105 | static void doit12(register uint32 *a,register long x,register long y,int64 start) 106 | { 107 | long i0; 108 | long y0; 109 | register long i; 110 | register uint32 data; 111 | register uint32 pos; 112 | register uint32 bits; 113 | 114 | x += 5; 115 | 116 | start += 1000000000; 117 | while (start < 0) { start += x; x += 10; } 118 | start -= 1000000000; 119 | i = start; 120 | while (i < 0) { i += x; x += 10; } 121 | 122 | y += 15; 123 | x += 10; 124 | 125 | for (;;) { 126 | while (i >= B) { 127 | if (x <= y) return; 128 | i -= y; 129 | y += 30; 130 | } 131 | i0 = i; 132 | y0 = y; 133 | while ((i >= 0) && (y < x)) { 134 | pos = i; data = i; 135 | pos >>= 5; data &= 31; 136 | i -= y; 137 | y += 30; 138 | bits = a[pos]; data = two[data]; 139 | bits ^= data; 140 | a[pos] = bits; 141 | } 142 | i = i0; 143 | y = y0; 144 | i += x - 10; 145 | x += 10; 146 | } 147 | } 148 | 149 | static const int deltainverse[60] = { 150 | -1,B32 * 0,-1,-1,-1,-1,-1,B32 * 1,-1,-1,-1,B32 * 2,-1,B32 * 3,-1 151 | ,-1,-1,B32 * 4,-1,B32 * 5,-1,-1,-1,B32 * 6,-1,-1,-1,-1,-1,B32 * 7 152 | ,-1,B32 * 8,-1,-1,-1,-1,-1,B32 * 9,-1,-1,-1,B32 * 10,-1,B32 * 11,-1 153 | ,-1,-1,B32 * 12,-1,B32 * 13,-1,-1,-1,B32 * 14,-1,-1,-1,-1,-1,B32 * 15 154 | } ; 155 | 156 | static void squarefree1big(uint32 (*buf)[B32],uint64 base,uint32 q,uint64 qq) 157 | { 158 | uint64 i; 159 | uint32 pos; 160 | int n; 161 | uint64 bound = base + 60 * B; 162 | 163 | while (qq < bound) { 164 | if (bound < 2000000000) 165 | i = qq - (((uint32) base) % ((uint32) qq)); 166 | else 167 | i = qq - (base % qq); 168 | if (!(i & 1)) i += qq; 169 | 170 | if (i < B * 60) { 171 | pos = i; 172 | n = deltainverse[pos % 60]; 173 | if (n >= 0) { 174 | pos /= 60; 175 | (*buf)[n + (pos >> 5)] |= two[pos & 31]; 176 | } 177 | } 178 | 179 | qq += q; q += 1800; 180 | } 181 | } 182 | 183 | static void squarefree1(register uint32 (*buf)[B32],uint64 L,uint32 q) 184 | { 185 | uint32 qq; 186 | register uint32 qqhigh; 187 | uint32 i; 188 | register uint32 ilow; 189 | register uint32 ihigh; 190 | register int n; 191 | uint64 base; 192 | 193 | base = 60 * L; 194 | qq = q * q; 195 | q = 60 * q + 900; 196 | 197 | while (qq < B * 60) { 198 | if (base < 2000000000) 199 | i = qq - (((uint32) base) % qq); 200 | else 201 | i = qq - (base % qq); 202 | if (!(i & 1)) i += qq; 203 | 204 | if (i < B * 60) { 205 | qqhigh = qq / 60; 206 | ilow = i % 60; ihigh = i / 60; 207 | 208 | qqhigh += qqhigh; 209 | while (ihigh < B) { 210 | n = deltainverse[ilow]; 211 | if (n >= 0) 212 | (*buf)[n + (ihigh >> 5)] |= two[ihigh & 31]; 213 | 214 | ilow += 2; ihigh += qqhigh; 215 | if (ilow >= 60) { ilow -= 60; ihigh += 1; } 216 | } 217 | } 218 | 219 | qq += q; q += 1800; 220 | } 221 | 222 | squarefree1big(buf,base,q,qq); 223 | } 224 | 225 | static void squarefree49big(uint32 (*buf)[B32],uint64 base,uint32 q,uint64 qq) 226 | { 227 | uint64 i; 228 | uint32 pos; 229 | int n; 230 | uint64 bound = base + 60 * B; 231 | 232 | while (qq < bound) { 233 | if (bound < 2000000000) 234 | i = qq - (((uint32) base) % ((uint32) qq)); 235 | else 236 | i = qq - (base % qq); 237 | if (!(i & 1)) i += qq; 238 | 239 | if (i < B * 60) { 240 | pos = i; 241 | n = deltainverse[pos % 60]; 242 | if (n >= 0) { 243 | pos /= 60; 244 | (*buf)[n + (pos >> 5)] |= two[pos & 31]; 245 | } 246 | } 247 | 248 | qq += q; q += 1800; 249 | } 250 | } 251 | 252 | static void squarefree49(register uint32 (*buf)[B32],uint64 L,uint32 q) 253 | { 254 | uint32 qq; 255 | register uint32 qqhigh; 256 | uint32 i; 257 | register uint32 ilow; 258 | register uint32 ihigh; 259 | register int n; 260 | uint64 base = 60 * L; 261 | 262 | qq = q * q; 263 | q = 60 * q + 900; 264 | 265 | while (qq < B * 60) { 266 | if (base < 2000000000) 267 | i = qq - (((uint32) base) % qq); 268 | else 269 | i = qq - (base % qq); 270 | if (!(i & 1)) i += qq; 271 | 272 | if (i < B * 60) { 273 | qqhigh = qq / 60; 274 | ilow = i % 60; ihigh = i / 60; 275 | 276 | qqhigh += qqhigh; 277 | qqhigh += 1; 278 | while (ihigh < B) { 279 | n = deltainverse[ilow]; 280 | if (n >= 0) 281 | (*buf)[n + (ihigh >> 5)] |= two[ihigh & 31]; 282 | 283 | ilow += 38; ihigh += qqhigh; 284 | if (ilow >= 60) { ilow -= 60; ihigh += 1; } 285 | } 286 | } 287 | 288 | qq += q; q += 1800; 289 | } 290 | 291 | squarefree49big(buf,base,q,qq); 292 | } 293 | 294 | /* squares of primes >= 7, < 240 */ 295 | uint32 qqtab[49] = { 296 | 49,121,169,289,361,529,841,961,1369,1681 297 | ,1849,2209,2809,3481,3721,4489,5041,5329,6241,6889 298 | ,7921,9409,10201,10609,11449,11881,12769,16129,17161,18769 299 | ,19321,22201,22801,24649,26569,27889,29929,32041,32761,36481 300 | ,37249,38809,39601,44521,49729,51529,52441,54289,57121 301 | } ; 302 | 303 | /* (qq * 11 + 1) / 60 or (qq * 59 + 1) / 60 */ 304 | uint32 qq60tab[49] = { 305 | 9,119,31,53,355,97,827,945,251,1653 306 | ,339,405,515,3423,3659,823,4957,977,6137 307 | ,1263,7789,1725,10031,1945,2099,11683,2341,2957 308 | ,16875,3441,18999,21831,22421,4519,4871,5113,5487 309 | ,31507,32215,35873,6829,7115,38941,43779,9117,9447,51567,9953,56169 310 | } ; 311 | 312 | static void squarefreetiny(register uint32 *a,uint32 *Lmodqq,int d) 313 | { 314 | int j; 315 | register uint32 k; 316 | register uint32 qq; 317 | register uint32 pos; 318 | register uint32 data; 319 | register uint32 bits; 320 | 321 | for (j = 0;j < 49;++j) { 322 | qq = qqtab[j]; 323 | k = qq - 1 - ((Lmodqq[j] + qq60tab[j] * d - 1) % qq); 324 | while (k < B) { 325 | pos = k; 326 | data = k; 327 | pos >>= 5; 328 | data &= 31; 329 | k += qq; 330 | bits = a[pos]; 331 | data = two[data]; 332 | bits |= data; 333 | a[pos] = bits; 334 | } 335 | } 336 | } 337 | 338 | typedef struct { char index; char f; char g; char k; } todo; 339 | 340 | static const todo for4[] = { 341 | {0,2,15,4} , {0,3,5,1} , {0,3,25,11} , {0,5,9,3} 342 | , {0,5,21,9} , {0,7,15,7} , {0,8,15,8} , {0,10,9,8} 343 | , {0,10,21,14} , {0,12,5,10} , {0,12,25,20} , {0,13,15,15} 344 | , {0,15,1,15} , {0,15,11,17} , {0,15,19,21} , {0,15,29,29} 345 | , {3,1,3,0} , {3,1,27,12} , {3,4,3,1} , {3,4,27,13} 346 | , {3,6,7,3} , {3,6,13,5} , {3,6,17,7} , {3,6,23,11} 347 | , {3,9,7,6} , {3,9,13,8} , {3,9,17,10} , {3,9,23,14} 348 | , {3,11,3,8} , {3,11,27,20} , {3,14,3,13} , {3,14,27,25} 349 | , {4,2,1,0} , {4,2,11,2} , {4,2,19,6} , {4,2,29,14} 350 | , {4,7,1,3} , {4,7,11,5} , {4,7,19,9} , {4,7,29,17} 351 | , {4,8,1,4} , {4,8,11,6} , {4,8,19,10} , {4,8,29,18} 352 | , {4,13,1,11} , {4,13,11,13} , {4,13,19,17} , {4,13,29,25} 353 | , {7,1,5,0} , {7,1,25,10} , {7,4,5,1} , {7,4,25,11} 354 | , {7,5,7,2} , {7,5,13,4} , {7,5,17,6} , {7,5,23,10} 355 | , {7,10,7,7} , {7,10,13,9} , {7,10,17,11} , {7,10,23,15} 356 | , {7,11,5,8} , {7,11,25,18} , {7,14,5,13} , {7,14,25,23} 357 | , {9,2,9,1} , {9,2,21,7} , {9,3,1,0} , {9,3,11,2} 358 | , {9,3,19,6} , {9,3,29,14} , {9,7,9,4} , {9,7,21,10} 359 | , {9,8,9,5} , {9,8,21,11} , {9,12,1,9} , {9,12,11,11} 360 | , {9,12,19,15} , {9,12,29,23} , {9,13,9,12} , {9,13,21,18} 361 | , {10,2,5,0} , {10,2,25,10} , {10,5,1,1} , {10,5,11,3} 362 | , {10,5,19,7} , {10,5,29,15} , {10,7,5,3} , {10,7,25,13} 363 | , {10,8,5,4} , {10,8,25,14} , {10,10,1,6} , {10,10,11,8} 364 | , {10,10,19,12} , {10,10,29,20} , {10,13,5,11} , {10,13,25,21} 365 | , {13,1,15,3} , {13,4,15,4} , {13,5,3,1} , {13,5,27,13} 366 | , {13,6,5,2} , {13,6,25,12} , {13,9,5,5} , {13,9,25,15} 367 | , {13,10,3,6} , {13,10,27,18} , {13,11,15,11} , {13,14,15,16} 368 | , {13,15,7,15} , {13,15,13,17} , {13,15,17,19} , {13,15,23,23} 369 | , {14,1,7,0} , {14,1,13,2} , {14,1,17,4} , {14,1,23,8} 370 | , {14,4,7,1} , {14,4,13,3} , {14,4,17,5} , {14,4,23,9} 371 | , {14,11,7,8} , {14,11,13,10} , {14,11,17,12} , {14,11,23,16} 372 | , {14,14,7,13} , {14,14,13,15} , {14,14,17,17} , {14,14,23,21} 373 | } ; 374 | 375 | static const todo for6[] = { 376 | {1,1,2,0} , {1,1,8,1} , {1,1,22,8} , {1,1,28,13} 377 | , {1,3,10,2} , {1,3,20,7} , {1,7,10,4} , {1,7,20,9} 378 | , {1,9,2,4} , {1,9,8,5} , {1,9,22,12} , {1,9,28,17} 379 | , {5,1,4,0} , {5,1,14,3} , {5,1,16,4} , {5,1,26,11} 380 | , {5,5,2,1} , {5,5,8,2} , {5,5,22,9} , {5,5,28,14} 381 | , {5,9,4,4} , {5,9,14,7} , {5,9,16,8} , {5,9,26,15} 382 | , {8,3,2,0} , {8,3,8,1} , {8,3,22,8} , {8,3,28,13} 383 | , {8,5,4,1} , {8,5,14,4} , {8,5,16,5} , {8,5,26,12} 384 | , {8,7,2,2} , {8,7,8,3} , {8,7,22,10} , {8,7,28,15} 385 | , {11,1,10,1} , {11,1,20,6} , {11,3,4,0} , {11,3,14,3} 386 | , {11,3,16,4} , {11,3,26,11} , {11,7,4,2} , {11,7,14,5} 387 | , {11,7,16,6} , {11,7,26,13} , {11,9,10,5} , {11,9,20,10} 388 | } ; 389 | 390 | static const todo for12[] = { 391 | {2,2,1,0} , {2,2,11,-2} , {2,2,19,-6} , {2,2,29,-14} 392 | , {2,3,4,0} , {2,3,14,-3} , {2,3,16,-4} , {2,3,26,-11} 393 | , {2,5,2,1} , {2,5,8,0} , {2,5,22,-7} , {2,5,28,-12} 394 | , {2,7,4,2} , {2,7,14,-1} , {2,7,16,-2} , {2,7,26,-9} 395 | , {2,8,1,3} , {2,8,11,1} , {2,8,19,-3} , {2,8,29,-11} 396 | , {2,10,7,4} , {2,10,13,2} , {2,10,17,0} , {2,10,23,-4} 397 | , {6,1,10,-2} , {6,1,20,-7} , {6,2,7,-1} , {6,2,13,-3} 398 | , {6,2,17,-5} , {6,2,23,-9} , {6,3,2,0} , {6,3,8,-1} 399 | , {6,3,22,-8} , {6,3,28,-13} , {6,4,5,0} , {6,4,25,-10} 400 | , {6,6,5,1} , {6,6,25,-9} , {6,7,2,2} , {6,7,8,1} 401 | , {6,7,22,-6} , {6,7,28,-11} , {6,8,7,2} , {6,8,13,0} 402 | , {6,8,17,-2} , {6,8,23,-6} , {6,9,10,2} , {6,9,20,-3} 403 | , {12,1,4,-1} , {12,1,14,-4} , {12,1,16,-5} , {12,1,26,-12} 404 | , {12,2,5,-1} , {12,2,25,-11} , {12,3,10,-2} , {12,3,20,-7} 405 | , {12,4,1,0} , {12,4,11,-2} , {12,4,19,-6} , {12,4,29,-14} 406 | , {12,6,1,1} , {12,6,11,-1} , {12,6,19,-5} , {12,6,29,-13} 407 | , {12,7,10,0} , {12,7,20,-5} , {12,8,5,2} , {12,8,25,-8} 408 | , {12,9,4,3} , {12,9,14,0} , {12,9,16,-1} , {12,9,26,-8} 409 | , {15,1,2,-1} , {15,1,8,-2} , {15,1,22,-9} , {15,1,28,-14} 410 | , {15,4,7,-1} , {15,4,13,-3} , {15,4,17,-5} , {15,4,23,-9} 411 | , {15,5,4,0} , {15,5,14,-3} , {15,5,16,-4} , {15,5,26,-11} 412 | , {15,6,7,0} , {15,6,13,-2} , {15,6,17,-4} , {15,6,23,-8} 413 | , {15,9,2,3} , {15,9,8,2} , {15,9,22,-5} , {15,9,28,-10} 414 | , {15,10,1,4} , {15,10,11,2} , {15,10,19,-2} , {15,10,29,-10} 415 | } ; 416 | 417 | void primegen_sieve(primegen *pg) 418 | { 419 | uint32 (*buf)[B32]; 420 | uint64 L; 421 | int i; 422 | uint32 Lmodqq[49]; 423 | 424 | buf = pg->buf; 425 | L = pg->L; 426 | 427 | if (L > 2000000000) 428 | for (i = 0;i < 49;++i) 429 | Lmodqq[i] = L % qqtab[i]; 430 | else 431 | for (i = 0;i < 49;++i) 432 | Lmodqq[i] = ((uint32) L) % qqtab[i]; 433 | 434 | clear(buf); 435 | 436 | for (i = 0;i < 16;++i) 437 | doit4(buf[0],for4[i].f,for4[i].g,(int64) for4[i].k - L); 438 | squarefreetiny(buf[0],Lmodqq,1); 439 | for (;i < 32;++i) 440 | doit4(buf[3],for4[i].f,for4[i].g,(int64) for4[i].k - L); 441 | squarefreetiny(buf[3],Lmodqq,13); 442 | for (;i < 48;++i) 443 | doit4(buf[4],for4[i].f,for4[i].g,(int64) for4[i].k - L); 444 | squarefreetiny(buf[4],Lmodqq,17); 445 | for (;i < 64;++i) 446 | doit4(buf[7],for4[i].f,for4[i].g,(int64) for4[i].k - L); 447 | squarefreetiny(buf[7],Lmodqq,29); 448 | for (;i < 80;++i) 449 | doit4(buf[9],for4[i].f,for4[i].g,(int64) for4[i].k - L); 450 | squarefreetiny(buf[9],Lmodqq,37); 451 | for (;i < 96;++i) 452 | doit4(buf[10],for4[i].f,for4[i].g,(int64) for4[i].k - L); 453 | squarefreetiny(buf[10],Lmodqq,41); 454 | for (;i < 112;++i) 455 | doit4(buf[13],for4[i].f,for4[i].g,(int64) for4[i].k - L); 456 | squarefreetiny(buf[13],Lmodqq,49); 457 | for (;i < 128;++i) 458 | doit4(buf[14],for4[i].f,for4[i].g,(int64) for4[i].k - L); 459 | squarefreetiny(buf[14],Lmodqq,53); 460 | 461 | for (i = 0;i < 12;++i) 462 | doit6(buf[1],for6[i].f,for6[i].g,(int64) for6[i].k - L); 463 | squarefreetiny(buf[1],Lmodqq,7); 464 | for (;i < 24;++i) 465 | doit6(buf[5],for6[i].f,for6[i].g,(int64) for6[i].k - L); 466 | squarefreetiny(buf[5],Lmodqq,19); 467 | for (;i < 36;++i) 468 | doit6(buf[8],for6[i].f,for6[i].g,(int64) for6[i].k - L); 469 | squarefreetiny(buf[8],Lmodqq,31); 470 | for (;i < 48;++i) 471 | doit6(buf[11],for6[i].f,for6[i].g,(int64) for6[i].k - L); 472 | squarefreetiny(buf[11],Lmodqq,43); 473 | 474 | for (i = 0;i < 24;++i) 475 | doit12(buf[2],for12[i].f,for12[i].g,(int64) for12[i].k - L); 476 | squarefreetiny(buf[2],Lmodqq,11); 477 | for (;i < 48;++i) 478 | doit12(buf[6],for12[i].f,for12[i].g,(int64) for12[i].k - L); 479 | squarefreetiny(buf[6],Lmodqq,23); 480 | for (;i < 72;++i) 481 | doit12(buf[12],for12[i].f,for12[i].g,(int64) for12[i].k - L); 482 | squarefreetiny(buf[12],Lmodqq,47); 483 | for (;i < 96;++i) 484 | doit12(buf[15],for12[i].f,for12[i].g,(int64) for12[i].k - L); 485 | squarefreetiny(buf[15],Lmodqq,59); 486 | 487 | squarefree49(buf,L,247); 488 | squarefree49(buf,L,253); 489 | squarefree49(buf,L,257); 490 | squarefree49(buf,L,263); 491 | squarefree1(buf,L,241); 492 | squarefree1(buf,L,251); 493 | squarefree1(buf,L,259); 494 | squarefree1(buf,L,269); 495 | } 496 | --------------------------------------------------------------------------------