Skip to content

Commit

Permalink
fast_primes_ll 1.28
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuraiDangyo authored Nov 2, 2019
1 parent 3843726 commit e4fbb91
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 63 deletions.
115 changes: 57 additions & 58 deletions fast_primes_ll.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "fast_primes_ll.h"

#define NAME "fast_primes_ll"
#define VERSION "1.27"
#define VERSION "1.28"
#define AUTHOR "Toni Helminen"

#define MAX_TOKENS 32
Expand All @@ -32,12 +32,12 @@ typedef struct {
int primes_n;
int count;
int last_size;
} HASHTABLE_T;
} PRIMES_T;

static char TOKENS[MAX_TOKENS][64] = {{0}};
static int TOKENS_N = 0;
static int TOKENS_I = 0;
static HASHTABLE_T HASH = {0};
static PRIMES_T PRIMES = {0};

static inline /* <- make me faster! */ int Max(const int a, const int b)
{
Expand Down Expand Up @@ -136,51 +136,51 @@ static int Token_next_int()
return r;
}

static void Hashtable_free_memory()
static void Primestable_free_memory()
{
if (HASH.size > 0) {
free(HASH.primes);
HASH.primes = 0;
HASH.size = 0;
if (PRIMES.size > 0) {
free(PRIMES.primes);
PRIMES.primes = 0;
PRIMES.size = 0;
}
}

static void Free_memory()
{
Hashtable_free_memory();
Primestable_free_memory();
}

static void Hashtable_set_size(const int usize /* MB */)
static void Primestable_set_size(const int usize /* MB */)
{
BITBOARD size = ULL(usize);
Hashtable_free_memory();
size = MAX(size, 16);
Primestable_free_memory();
size = MAX(size, 1);
size = MIN(size, 1024 * 1024); // 1 PB
size = (1 << 20) * size; // To MB
HASH.size = 1;
while (HASH.size <= size) // Calculate needed memory in bytes
HASH.size <<= 1;
HASH.size >>= 1;
HASH.primes_n = 0;
HASH.count = INT(HASH.size / sizeof(BITBOARD));
HASH.primes = calloc(HASH.count, sizeof(BITBOARD));
FCP_ASSERT(HASH.primes != NULL) // Make sure there is enough space
PRIMES.size = 1;
while (PRIMES.size <= size) // Calculate needed memory in bytes
PRIMES.size <<= 1;
PRIMES.size >>= 1;
PRIMES.primes_n = 0;
PRIMES.count = INT(PRIMES.size / sizeof(BITBOARD));
PRIMES.primes = calloc(PRIMES.count, sizeof(BITBOARD));
FCP_ASSERT(PRIMES.primes != NULL) // Make sure there is enough space
Init_primes();
}

static void Hashtable_make_bigger()
static void Primestable_make_bigger()
{
HASH.count = 2 * HASH.count;
HASH.primes = realloc(&HASH.primes, HASH.count * sizeof(BITBOARD));
FCP_ASSERT(HASH.primes != NULL) // Make sure there is enough space
PRIMES.count = 2 * PRIMES.count;
PRIMES.primes = realloc(PRIMES.primes, PRIMES.count * sizeof(BITBOARD));
FCP_ASSERT(PRIMES.primes != NULL) // Make sure there is enough space
}

static void System()
{
P("{ # Hash");
P(" .primes_n = %i,", HASH.primes_n);
P(" .count = %i,", HASH.count);
P(" .size = %i # MB", (HASH.count * (sizeof(BITBOARD))) / (1024 * 1024));
P(" .primes_n = %i,", PRIMES.primes_n);
P(" .count = %i,", PRIMES.count);
P(" .size = %i # MB", (PRIMES.count * (sizeof(BITBOARD))) / (1024 * 1024));
P("}");
}

Expand Down Expand Up @@ -212,7 +212,7 @@ static void Print_help()
P(" -system = System info,");
P(" -is(prime) N = Check if whether N is a prime,");
P(" -nth(prime) N = Print Nth prime,");
P(" -list N = Print all primes up to N");
P(" -primes N = Print all primes up to N");
P("}");
P("");
P("{ # Full source code, please see:");
Expand All @@ -235,11 +235,11 @@ static BITBOARD Nth_prime(const int prime_n)
int n = Max(0, prime_n - 1);
if (prime_n == 0)
return 0;
if (n < HASH.primes_n)
return HASH.primes[n];
while (HASH.primes_n <= n)
if (n < PRIMES.primes_n)
return PRIMES.primes[n];
while (PRIMES.primes_n <= n)
Add_prime(Next_prime());
return HASH.primes[n];
return PRIMES.primes[n];
}

static void Command_nthprime()
Expand All @@ -251,7 +251,7 @@ static void Command_nthprime()
P("}");
}

static void Command_list()
static void Command_primes()
{
int n = Max(2, Token_next_int());
P("{ # First %i primes generated by %s v%s ", n, NAME, VERSION);
Expand All @@ -262,19 +262,19 @@ static void Command_list()

static void Add_prime(const BITBOARD prime)
{
if (HASH.primes_n >= HASH.count)
Hashtable_make_bigger();
HASH.primes[HASH.primes_n] = prime;
HASH.primes_n++;
if (PRIMES.primes_n >= PRIMES.count)
Primestable_make_bigger();
PRIMES.primes[PRIMES.primes_n] = prime;
PRIMES.primes_n++;
}

static bool Is_prime(const BITBOARD prime)
{
int i;
if (prime < 2)
return 1;
for (i = 0; i < HASH.primes_n && HASH.primes[i] * HASH.primes[i] <= prime; i++)
if (prime % HASH.primes[i] == 0)
for (i = 0; i < PRIMES.primes_n && PRIMES.primes[i] * PRIMES.primes[i] <= prime; i++)
if (prime % PRIMES.primes[i] == 0)
return 0;
return 1;
}
Expand All @@ -284,8 +284,8 @@ static bool Is_prime_gen(const BITBOARD prime)

int i, p;
if (prime <= 1) return 0;
for (i = 0; i < HASH.primes_n; i++)
if (HASH.primes[i] == prime) return 1;
for (i = 0; i < PRIMES.primes_n; i++)
if (PRIMES.primes[i] == prime) return 1;
while (1) {
p = Next_prime();
Add_prime(p);
Expand All @@ -299,26 +299,25 @@ static bool Is_prime_gen(const BITBOARD prime)

static int Last_prime()
{
return HASH.primes[HASH.primes_n - 1];
return PRIMES.primes[PRIMES.primes_n - 1];
}

static int Next_prime()
{
int candidate = Last_prime() + 1;
while (1) {
while (1)
if (Is_prime(candidate))
return candidate;
else
candidate++;
}
FCP_ASSERT(0)
}

static void Insert_primes(const int n)
{
int i = 0;
int l = HASH.primes_n;
while (HASH.primes_n < n) {
int l = PRIMES.primes_n;
while (PRIMES.primes_n < n) {
Add_prime(Next_prime());
P(" %i = %llu%s", i + 1 + l, Last_prime(), i + 1 + l < n ? ",": "");
i++;
Expand All @@ -328,7 +327,7 @@ static void Insert_primes(const int n)
static void Init_primes()
{
int i;
HASH.primes_n = 0;
PRIMES.primes_n = 0;
Add_prime(2);
Add_prime(3);
for (i = 0; i < 10; i++)
Expand All @@ -340,29 +339,29 @@ static void Bench()
BITBOARD diff;
BITBOARD start = Now();
P("> Benching ...\n");
HASH.primes_n = 0;
PRIMES.primes_n = 0;
Add_prime(2);
Add_prime(3);
while (HASH.primes_n < 1000000) {
while (PRIMES.primes_n < 1000000) {
Add_prime(Next_prime());
if (HASH.primes_n % 100000 == 0)
P("{ progress = %i%% }", HASH.primes_n / 10000);
if (PRIMES.primes_n % 100000 == 0)
P("{ progress = %i%% }", PRIMES.primes_n / 10000);
}
diff = Now() - start;
P("");
P("{ # Benchmarks");
P(" time = %.3fs,", 0.001f * DOUBLE(diff));
P(" primes_per_second = %llu,", Pps(HASH.primes_n, diff));
P(" primes = %i", HASH.primes_n);
P(" primes_per_second = %llu,", Pps(PRIMES.primes_n, diff));
P(" primes = %i", PRIMES.primes_n);
P("}");
}

static void Print_primes(const int usize)
{
int i;
int n = Min(HASH.primes_n, usize);
int n = Min(PRIMES.primes_n, usize);
for (i = 0; i < n; i++)
P(" %i = %llu%s", i + 1, HASH.primes[i], i + 1 < n ? ",": "");
P(" %i = %llu%s", i + 1, PRIMES.primes[i], i + 1 < n ? ",": "");
}

static void FCP_commands()
Expand All @@ -378,8 +377,8 @@ static void FCP_commands()
Bench();
else if (Token_next("system"))
System();
else if (Token_next("list"))
Command_list();
else if (Token_next("primes"))
Command_primes();
else if (Token_next("nthprime") || Token_next("nth"))
Command_nthprime();
Token_expect(";");
Expand Down Expand Up @@ -413,7 +412,7 @@ static void Ok()

static void Go()
{
Hashtable_set_size(HASH_SIZE);
Primestable_set_size(HASH_SIZE);
Init_primes();
Ok();
FCP_commands();
Expand Down
10 changes: 5 additions & 5 deletions fast_primes_ll.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ static bool Token_is(const char *s);
static void Token_expect(const char *s);
static bool Token_next(const char *s);
static int Token_next_int();
static void Hashtable_free_memory();
static void Primestable_free_memory();
static void Free_memory();
static void Hashtable_set_size(const int usize /* MB */);
static void Hashtable_make_bigger();
static void Primestable_set_size(const int usize /* MB */);
static void Primestable_make_bigger();
static void System();
static void Print_version();
static void Print_help();
static void Command_isprime();
static void Command_list();
static void Command_primes();
static void Add_prime(const BITBOARD prime);
static int Last_prime();
static bool Is_prime(const BITBOARD prime);
Expand All @@ -59,4 +59,4 @@ static void Init_tokens(int argc, char **argv);

// !! i love you grandpa !!

#endif // FAST_PRIMES_LL_H
#endif // FAST_PRIMES_LL_H

0 comments on commit e4fbb91

Please sign in to comment.