6 Commits
v0.2 ... v0.2.1

4 changed files with 20 additions and 10 deletions

View File

@ -21,19 +21,21 @@ void initList(List *l) {
} }
void deInitList(List *l) { void deInitList(List *l) {
for(ulli i = 0; i < l->size; ++i) {
mpz_clear(l->list[i]);
}
free(l->list); free(l->list);
} }
void addToList(List *l, mpz_t n) { void addToList(List *l, mpz_t n) {
if(l->end == l->size) { if(unlikely(l->end == l->size)) {
l->size += BLOCK_SIZE; l->size += BLOCK_SIZE;
if(unlikely(l->size == 0)) { if(unlikely(l->size == 0)) {
fprintf(stderr, fprintf(stderr, "`l->size' has overflowed!\n");
"size has reached limit of `long long int' type!\n");
exit(1); exit(1);
} }
void *tmp = realloc(l->list, sizeof(mpz_t) * l->size); void *tmp = realloc(l->list, sizeof(mpz_t) * l->size);
if(!tmp) { if(unlikely(!tmp)) {
fprintf(stderr, "Failed to allocate more memory to list!\n"); fprintf(stderr, "Failed to allocate more memory to list!\n");
exit(1); exit(1);
} }

View File

@ -2,13 +2,15 @@
#include <gmp.h> #include <gmp.h>
#include <stdbool.h> #include <stdbool.h>
#include "types.h"
/** /**
* @brief An infinitely expanding list type. * @brief An infinitely expanding list type.
*/ */
typedef struct { typedef struct {
mpz_t *list; //!< The list of elements mpz_t *list; //!< The list of elements
unsigned long long int size; //!< How many elements are in the list ulli size; //!< How many elements are in the list
unsigned long long int end; //!< The last element of the list (in use) ulli end; //!< The last element of the list (in use)
} List; } List;
/** /**

View File

@ -5,6 +5,8 @@
#include <gmp.h> #include <gmp.h>
#include "list.h" #include "list.h"
#include "types.h"
#include "optimizers.h"
static bool run; static bool run;
void leave(); void leave();
@ -36,14 +38,15 @@ int main(void) {
do { do {
// Loop through found primes // Loop through found primes
for(unsigned long long int i = 0; i < primes.size; ++i) { for(ulli i = 0; i < primes.size; ++i) {
// If `num' is divisible by a prime then go to the next number // If `num' is divisible by a prime then go to the next number
if(mpz_divisible_p(num, primes.list[i]) != 0) goto nextPrime; if(mpz_divisible_p(num, primes.list[i]) != 0)
goto nextPrime;
} }
// `num' is a prime so we add it to the list and print it // `num' is a prime so we add it to the list and print it
addToList(&primes, num); addToList(&primes, num);
if(mpz_out_str(stdout, 10, num) == 0) { if(unlikely(mpz_out_str(stdout, 10, num) == 0)) {
fprintf(stderr, "Could not print to `stdout'!\n"); fprintf(stderr, "Could not print to `stdout'!\n");
exit(1); exit(1);
} }
@ -52,8 +55,9 @@ int main(void) {
nextPrime: nextPrime:
// Add 2 (skip even numbers since they're all divisible by 2) // Add 2 (skip even numbers since they're all divisible by 2)
mpz_add_ui(num, num, 2); mpz_add_ui(num, num, 2);
} while(run); } while(likely(run));
mpz_clear(num);
// Deinitialize the list // Deinitialize the list
deInitList(&primes); deInitList(&primes);
return 0; return 0;

2
src/types.h Normal file
View File

@ -0,0 +1,2 @@
#pragma once
typedef unsigned long long int ulli;