primes.c00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00054
00055 #include <stdio.h>
00056 #include <stdarg.h>
00057 #include <sys/param.h>
00058 #include <sys/sio.h>
00059 #include <string.h>
00060 #include <imath.h>
00061
00062
00063
00064
00065
00066
00067
00068
00069 #if DATA_SIZE > 8192 + 512
00070 # undef DATA_SIZE
00071 # define DATA_SIZE 8192 + 512
00072 #endif
00073
00074
00075 #if DATA_SIZE < 512
00076 # define MAX_PRIMES (DATA_SIZE - 128)
00077 #else
00078 # define MAX_PRIMES (DATA_SIZE - 512)
00079 #endif
00080
00081 #define LAST_PRIME (MAX_PRIMES * 8L)
00082
00083
00084 unsigned char prime_list[MAX_PRIMES];
00085
00086
00087 static inline int
00088 is_prime (unsigned long n)
00089 {
00090 unsigned short bit = (unsigned short) (n) & 0x07;
00091
00092 return prime_list[n >> 3] & (1 << bit);
00093 }
00094
00095
00096 static inline void
00097 set_prime (unsigned long n)
00098 {
00099 unsigned short bit = (unsigned short) (n) & 0x07;
00100
00101 prime_list[n >> 3] |= (1 << bit);
00102 }
00103
00104
00105
00106 static int
00107 check_for_prime (unsigned long n)
00108 {
00109 unsigned long i;
00110 unsigned char *p;
00111 unsigned long last_value;
00112 unsigned char small_n;
00113
00114 small_n = (n & 0xffff0000) == 0;
00115 i = 0;
00116
00117
00118 last_value = lsqrt (n);
00119
00120
00121
00122 p = prime_list;
00123 do
00124 {
00125 unsigned char val;
00126
00127 val = *p++;
00128 if (val)
00129 {
00130 unsigned short j;
00131 unsigned long q;
00132
00133 q = i;
00134 for (j = 1; val && j <= 0x80; j <<= 1, q++)
00135 {
00136 if (val & j)
00137 {
00138 val &= ~j;
00139
00140
00141 if (small_n)
00142 {
00143 unsigned short r;
00144
00145
00146 r = (unsigned short) (n) % (unsigned short) (q);
00147 if (r == 0)
00148 return 0;
00149 }
00150 else
00151 {
00152 unsigned long r;
00153
00154 r = n % q;
00155
00156
00157 if (r == 0)
00158 return 0;
00159 }
00160 }
00161 }
00162 }
00163 i += 8;
00164 }
00165 while (i < last_value);
00166 return 1;
00167 }
00168
00169 #if 0
00170
00171
00172 static void
00173 print_primes (void)
00174 {
00175 long i;
00176
00177 for (i = 0; i < LAST_PRIME; i++)
00178 if (is_prime (i))
00179 printf ("%ld\n", i);
00180 }
00181 #endif
00182
00183
00184
00185 int verbose = 0;
00186
00187 int
00188 main ()
00189 {
00190 long i;
00191 short cnt = 0;
00192
00193 serial_init ();
00194 printf ("Computing prime numbers below %ld\n",
00195 (long) LAST_PRIME);
00196 memset (prime_list, 0, sizeof (prime_list));
00197 for (i = 2; i < LAST_PRIME; i++)
00198 {
00199 if (check_for_prime (i))
00200 {
00201 set_prime (i);
00202 cnt ++;
00203 if (verbose)
00204 printf ("%ld\n", i);
00205 }
00206 }
00207 printf ("Found %ld prime numbers below %ld\n",
00208 (long) cnt, (long) LAST_PRIME);
00209
00210 return 0;
00211 }
|