#include <stdio.h>
#ifndef vax
#include <stdlib.h>
#endif
#include <math.h>
#define LIMIT 9
#ifdef Amiga
#include <exec/types.h>
#include <ctype.h>
#endif
#ifdef BORLAND_C
#include <ctype.h>
#include <dos.h>
#endif
#ifdef MSC
#include <ctype.h>
#endif
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
float nulltime,runtime,TimeArray[4];
float reftime,adjtime1,emips;
float hmips,lmips,smips[21];
long L_Prime,N_Prime;
long ErrorFlag;
long Number_Of_Primes[21];
long NLoops[21];
#ifdef xxxxxxxxxx
void main()
{
long i,j,k,p;
float sumtime;
printf("\n Sieve of Eratosthenes (Scaled to 10 Iterations)\n");
printf(" Version 1.2, 03 April 1992\n\n");
printf(" Array Size Number Last Prime Linear");
printf(" RunTime MIPS\n");
printf(" (Bytes) of Primes Time(sec)");
printf(" (Sec)\n");
Number_Of_Primes[0] = 1899;
Number_Of_Primes[1] = 2261;
Number_Of_Primes[2] = 4202;
Number_Of_Primes[3] = 7836;
Number_Of_Primes[4] = 14683;
Number_Of_Primes[5] = 27607;
Number_Of_Primes[6] = 52073;
Number_Of_Primes[7] = 98609;
Number_Of_Primes[8] = 187133;
Number_Of_Primes[9] = 356243;
Number_Of_Primes[10]= 679460;
Number_Of_Primes[11]= 1299068;
Number_Of_Primes[12]= 2488465;
Number_Of_Primes[13]= 0;
Number_Of_Primes[14]= 0;
Number_Of_Primes[15]= 0;
j = 8191;
k = 256;
p = 0;
SIEVE(j,k,p);
for( i=1 ; i<= 20 ; i++)
{
NLoops[i] = 1;
}
p = 8;
if ( runtime > 0.125 ) p = 1;
NLoops[0] = 256 * p;
NLoops[1] = 256 * p;
NLoops[2] = 128 * p;
NLoops[3] = 64 * p;
NLoops[4] = 32 * p;
NLoops[5] = 16 * p;
NLoops[6] = 8 * p;
NLoops[7] = 4 * p;
NLoops[8] = 2 * p;
NLoops[9] = p;
NLoops[10] = p / 2;
NLoops[11] = p / 4;
if ( p == 1 )
{
NLoops[10] = 1;
NLoops[11] = 1;
}
sumtime = 0.0;
i = 0;
j = 8191;
k = NLoops[0];
SIEVE(j,k,p);
sumtime = sumtime + runtime;
smips[i] = emips;
j = 5000;
ErrorFlag = 0;
for( i=1 ; i<= LIMIT ; i++)
{
j = 2 * j;
k = NLoops[i];
SIEVE(j,k,p);
smips[i] = emips;
if( ErrorFlag == 0L )
{
if( N_Prime != Number_Of_Primes[i] )
{
printf("\n Error --- Incorrect Number of Primes for Array: %ld\n",j);
printf(" Number of Primes Found is: %ld\n",N_Prime);
printf(" Correct Number of Primes is: %ld\n",Number_Of_Primes[i]);
ErrorFlag = 1L;
}
}
if( ErrorFlag > 0L ) break;
sumtime = sumtime + runtime * ( 8191.0 / (float)j );
}
if( ErrorFlag == 2L )
{
printf("\n Could Not Allocate Memory for Array Size: %ld\n",j);
}
sumtime = sumtime / (float)i;
j = LIMIT;
if( ErrorFlag == 1L) j = LIMIT - 1;
hmips = 0.0;
lmips = 1.0e+06;
for( i=0 ; i <= j ; i++)
{
if( smips[i] > hmips ) hmips = smips[i];
if( smips[i] < lmips ) lmips = smips[i];
}
printf("\n Relative to 10 Iterations and the 8191 Array Size:\n");
printf(" Average RunTime = %8.3f (sec)\n",sumtime);
printf(" High MIPS = %8.1f\n",hmips);
printf(" Low MIPS = %8.1f\n\n",lmips);
}
#endif
main()
{
SIEVE(8191,1000,0);
}
SIEVE(m,n,p)
long m,n,p;
{
register char *flags;
register long i,prime,k,ci;
register long count,size;
long iter,j;
char *ptr;
#ifdef vax
char *malloc();
int free();
#endif
size = m - 1;
ptr = malloc(m);
ErrorFlag = 0L;
N_Prime = 0L;
L_Prime = 0L;
if( !ptr )
{
ErrorFlag = 2L;
return 0;
}
flags = ptr;
dtime(TimeArray);
dtime(TimeArray);
nulltime = TimeArray[1];
if ( nulltime < 0.0 ) nulltime = 0.0;
j = 0;
dtime(TimeArray);
for(iter=1 ; iter<=n ; iter++)
{
count = 0;
for(i=0 ; i<=size ; i++)
{
*(flags+i) = TRUE;
}
ci = 0;
for(i=0 ; i<=size ; i++)
{
if(*(flags+i))
{
count++;
prime = i + i + 3;
for(k = i + prime ; k<=size ; k+=prime)
{
ci = ci + 1;
*(flags+k)=FALSE;
}
}
}
j = j + count;
}
dtime(TimeArray);
free(ptr);
runtime = (TimeArray[1] - nulltime) * 10.0 / (float)n;
if ( m == 8191 ) reftime = runtime;
adjtime1 = reftime * ( (float)m / 8191.0 );
emips = 9.0*(float)size+9.0*(float)count;
emips = emips+5.0*(float)ci;
emips = 1.0e-05*(emips/runtime);
N_Prime = j / n;
L_Prime = prime;
if ( p != 0L )
{
printf(" %9ld %8ld %8ld ",m,N_Prime,L_Prime);
printf("%9.3f %9.3f %6.1f\n",adjtime1,runtime,emips);
}
return 0;
}
#ifdef UNIX
#include <sys/time.h>
#include <sys/resource.h>
#ifdef hpux
#include <sys/syscall.h>
#define getrusage(a,b) syscall(SYS_getrusage,a,b)
#endif
struct rusage rusage;
dtime(p)
float p[];
{
float q;
q = p[2];
getrusage(RUSAGE_SELF,&rusage);
p[2] = (float)(rusage.ru_utime.tv_sec);
p[2] = p[2] + (float)(rusage.ru_utime.tv_usec) / 1.0e+06;
p[1] = p[2] - q;
return 0;
}
#endif
#ifdef UNIX_Old
#include <sys/times.h>
#include <sys/param.h>
#ifndef HZ
#define HZ 60
#endif
struct tms tms;
dtime(p)
float p[];
{
float q;
q = p[2];
times(&tms);
p[2] = (float)(tms.tms_utime) / (float)HZ;
p[1] = p[2] - q;
return 0;
}
#endif
#ifdef Amiga
#define HZ 50
dtime(p)
float p[];
{
float q;
struct tt {
long days;
long minutes;
long ticks;
} tt;
q = p[2];
DateStamp(&tt);
p[2] = ((float)(tt.ticks+(tt.minutes*60L*(long)HZ)))/(float)HZ;
p[1] = p[2] - q;
return 0;
}
#endif
#ifdef BORLAND_C
#include <time.h>
#define HZ 100
struct time now;
dtime(p)
float p[];
{
float q,v;
q = p[2];
gettime(&now);
v = 60.0*(float)(now.ti_min);
v = v + (float)(now.ti_sec);
v = v + (float)(now.ti_hund) / (float)HZ;
p[2] = v;
p[1] = v - q;
return 0;
}
#endif
#ifdef MSC
#include <time.h>
#define HZ CLK_TCK
clock_t tnow;
dtime(p)
float p[];
{
float q;
q = p[2];
tnow = clock();
p[2] = (float)tnow / (float)HZ;
p[1] = p[2] - q;
return 0;
}
#endif