Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
e2aedab3b0 | |||
bc8b48dd29 | |||
dab78093ab | |||
a782bdb57d | |||
9157d15383 | |||
f1fd758bfc | |||
66c0a5d027 | |||
f4ee9872bc | |||
8a42e85d04 | |||
dd38b53e31 | |||
3c8b9922fb | |||
cb9e1648e9 | |||
30703314dd | |||
4905391c82 |
@ -9,3 +9,10 @@ Change Log
|
||||
- Switch to C.
|
||||
- Uses GNU Multiple Precision library (GMP) to hold prime numbers, allowing for 'infinite' size.
|
||||
- Add `likely()' and `unlikely()' macros to optimize.
|
||||
- v0.2.1: Memory Leak Fixes
|
||||
- Fixed a major memory leak at the end of the program.
|
||||
- Added more optimizers.
|
||||
- v0.3: Optimizations
|
||||
- Algorithm skips half the known primes.
|
||||
- Removed `likely()' and `unlikely()' macros due to lack of information.
|
||||
- Improved performance.
|
||||
|
15
TODO.md
Normal file
15
TODO.md
Normal file
@ -0,0 +1,15 @@
|
||||
To-Do
|
||||
=====
|
||||
A to-do list for the project. Feel free to remove items as you solve them in your pull requests.
|
||||
|
||||
Next
|
||||
----
|
||||
To-do items to do for the next version:
|
||||
|
||||
- Profile code to better use `likely()` and `unlikely()` macros.
|
||||
|
||||
Features
|
||||
--------
|
||||
Features for future versions (not _Next_) of Indivisible:
|
||||
|
||||
- Implement multi-threaded version using OpenCL or OpenMP (whichever works).
|
10
src/list.c
10
src/list.c
@ -2,8 +2,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "optimizers.h"
|
||||
|
||||
/**
|
||||
* This is the number of elements by which the list expands.
|
||||
* WARNING: Always use doubles for this number (2^X)
|
||||
@ -21,15 +19,17 @@ void initList(List *l) {
|
||||
}
|
||||
|
||||
void deInitList(List *l) {
|
||||
for(ulli i = 0; i < l->size; ++i) {
|
||||
mpz_clear(l->list[i]);
|
||||
}
|
||||
free(l->list);
|
||||
}
|
||||
|
||||
void addToList(List *l, mpz_t n) {
|
||||
if(l->end == l->size) {
|
||||
l->size += BLOCK_SIZE;
|
||||
if(unlikely(l->size == 0)) {
|
||||
fprintf(stderr,
|
||||
"size has reached limit of `long long int' type!\n");
|
||||
if(l->size == 0) {
|
||||
fprintf(stderr, "`l->size' has overflowed!\n");
|
||||
exit(1);
|
||||
}
|
||||
void *tmp = realloc(l->list, sizeof(mpz_t) * l->size);
|
||||
|
@ -2,13 +2,15 @@
|
||||
#include <gmp.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
/**
|
||||
* @brief An infinitely expanding list type.
|
||||
*/
|
||||
typedef struct {
|
||||
mpz_t *list; //!< The list of elements
|
||||
unsigned long long int size; //!< How many elements are in the list
|
||||
unsigned long long int end; //!< The last element of the list (in use)
|
||||
ulli size; //!< How many elements are in the list
|
||||
ulli end; //!< The last element of the list (in use)
|
||||
} List;
|
||||
|
||||
/**
|
||||
|
20
src/main.c
20
src/main.c
@ -5,12 +5,13 @@
|
||||
#include <gmp.h>
|
||||
|
||||
#include "list.h"
|
||||
#include "types.h"
|
||||
|
||||
static bool run;
|
||||
void leave();
|
||||
|
||||
int main(void) {
|
||||
printf("Indivisible v0.2\n");
|
||||
puts("Indivisible v0.2\n");
|
||||
|
||||
// Quit on ^C by setting `run = false'
|
||||
run = true;
|
||||
@ -34,11 +35,19 @@ int main(void) {
|
||||
printf("\n");
|
||||
mpz_add_ui(num, num, 1);
|
||||
|
||||
// Variable for half `num'
|
||||
mpz_t halfNum;
|
||||
mpz_init(halfNum);
|
||||
|
||||
do {
|
||||
// Calculate half of `num'
|
||||
mpz_fdiv_q_ui(halfNum, num, 2);
|
||||
// Loop through found primes
|
||||
for(unsigned long long int i = 0; i < primes.size; ++i) {
|
||||
// Skip 2 because we're skipping even nymbers
|
||||
for(ulli i = 1; mpz_cmp(primes.list[i], halfNum) >= 0; ++i) {
|
||||
// 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
|
||||
@ -54,12 +63,15 @@ nextPrime:
|
||||
mpz_add_ui(num, num, 2);
|
||||
} while(run);
|
||||
|
||||
// Clear GMP variables
|
||||
mpz_clear(halfNum);
|
||||
mpz_clear(num);
|
||||
// Deinitialize the list
|
||||
deInitList(&primes);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void leave() {
|
||||
printf("Exiting...\n");
|
||||
puts("Exiting...\n");
|
||||
run = false;
|
||||
}
|
||||
|
@ -1,3 +0,0 @@
|
||||
#pragma once
|
||||
#define likely(x) __builtin_expect(!!(x), 1)
|
||||
#define unlikely(x) __builtin_expect(!!(x), 0)
|
2
src/types.h
Normal file
2
src/types.h
Normal file
@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
typedef unsigned long long int ulli;
|
Reference in New Issue
Block a user