Compare commits
49 Commits
Author | SHA1 | Date | |
---|---|---|---|
c522196d66 | |||
b5dcadce19 | |||
003b94dcdb | |||
fcee95da17 | |||
43620ba2d3 | |||
9f1160242a | |||
b414bff9dc | |||
d8c81b172b | |||
c2f0fb0ffd | |||
5bbac132bc | |||
2a3e97f4bc | |||
06d5ddb0cc | |||
a9019291c2 | |||
5aa0b333c0 | |||
3110c74174 | |||
a5ce845c68 | |||
449fef2994 | |||
2e9326b5fb | |||
06cb271dba | |||
4cbc3fae7d | |||
9b4fa96474 | |||
e2aedab3b0 | |||
bc8b48dd29 | |||
dab78093ab | |||
a782bdb57d | |||
9157d15383 | |||
f1fd758bfc | |||
66c0a5d027 | |||
f4ee9872bc | |||
8a42e85d04 | |||
dd38b53e31 | |||
3c8b9922fb | |||
cb9e1648e9 | |||
30703314dd | |||
4905391c82 | |||
d757f3a79f | |||
79a9ba11ff | |||
05c50652b8 | |||
7d6ed15523 | |||
b66755174f | |||
04a8d7ddde | |||
6bfab05e37 | |||
05b6257e44 | |||
1593e57e90 | |||
75b35b3356 | |||
3bb757dc0d | |||
1032e099a1 | |||
a236f88d12 | |||
87b96e4ebc |
6
.gitignore
vendored
6
.gitignore
vendored
@ -1,3 +1,9 @@
|
||||
# Ignore build files
|
||||
build/*
|
||||
!build/.keep
|
||||
|
||||
# Ignore documentation
|
||||
doc/
|
||||
|
||||
# Ignore vim temporary files
|
||||
*.sw[a-z]
|
||||
|
@ -2,12 +2,19 @@ image: gcc
|
||||
|
||||
build:
|
||||
stage: build
|
||||
# Install dependencies
|
||||
before_script:
|
||||
- apt update && apt -y install cmake
|
||||
- apt update && apt -y install cmake libgmp-dev
|
||||
# Build the project
|
||||
script:
|
||||
- cd build/
|
||||
- cmake -DCMAKE_BUILD_TYPE=Release ..
|
||||
- make
|
||||
# Find the resulting binary
|
||||
artifacts:
|
||||
paths:
|
||||
- build/indivisible
|
||||
# Cache .o files for faster compiling
|
||||
cache:
|
||||
paths:
|
||||
- "build/CMakeFiles/indivisible.dir/src/*.o"
|
||||
|
20
CHANGELOG
20
CHANGELOG
@ -4,4 +4,22 @@ Change Log
|
||||
- v0.1: Initial release
|
||||
- Basic prime calculation.
|
||||
- Uses a growing vector of known primes and gets the modulus of the number divided by these primes.
|
||||
- Uses type `long long` to hold prime numbers.
|
||||
- Uses type `long long' to hold prime numbers.
|
||||
- v0.2: Multi-Precision
|
||||
- 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.
|
||||
- v0.4: Fixed Algorithm
|
||||
- Fixed algorithm to actually calculate primes.
|
||||
- Added extra C99 optimizations.
|
||||
- v0.5: Minor Changes
|
||||
- Use `size_t' instead of `unsigned long long int'.
|
||||
- Minor optimizations to the algorithm.
|
||||
- Added commandline argument parsing.
|
||||
|
@ -1,23 +1,39 @@
|
||||
cmake_minimum_required(VERSION 2.6)
|
||||
project(Indivisible)
|
||||
|
||||
cmake_policy(SET CMP0012 OLD)
|
||||
|
||||
set(TARGET_NAME indivisible)
|
||||
|
||||
if(NOT CMAKE_BUILD_TYPE)
|
||||
set(CMAKE_BUILD_TYPE Debug)
|
||||
endif()
|
||||
|
||||
set(SRCS
|
||||
src/Main.cpp)
|
||||
set(CMAKE_MODULE_PATH
|
||||
${CMAKE_MODULE_PATH}
|
||||
${CMAKE_SOURCE_DIR}/cmake/)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "-std=c++14 -fno-elide-constructors -pedantic-errors -Wall -Wextra -Werror -Wpedantic -Winit-self -Wmissing-declarations -Wuninitialized -Wfatal-errors")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O3")
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -g -O0")
|
||||
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS} -g -O3")
|
||||
set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS} -Os")
|
||||
find_package(GMP REQUIRED)
|
||||
|
||||
include_directories(
|
||||
${GMP_INCLUDE_DIR})
|
||||
|
||||
set(SRCS
|
||||
src/main.c
|
||||
src/list.c)
|
||||
|
||||
# Define the C flags.
|
||||
set(CMAKE_C_FLAGS "-std=gnu99 -Wall -Wextra -Werror -Wfatal-errors -Wmissing-declarations -pedantic-errors")
|
||||
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -g -O0")
|
||||
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O3")
|
||||
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS} -g -O3")
|
||||
set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS} -Os")
|
||||
|
||||
if(NOT CMAKE_BUILD_TYPE MATCHES Debug AND NOT CMAKE_BUILD_TYPE MATCHES RelWithDebInfo)
|
||||
add_definitions(-DNDEBUG)
|
||||
endif()
|
||||
|
||||
add_executable(${TARGET_NAME} ${SRCS})
|
||||
|
||||
target_link_libraries(${TARGET_NAME}
|
||||
${GMP_LIBRARY})
|
||||
|
@ -2,12 +2,15 @@ Indivisible
|
||||
===========
|
||||
[](https://gitlab.com/Deathsbreed/Indivisible/commits/master)
|
||||
|
||||
Indivisible is an optimized prime number generator written in C++.
|
||||
Indivisible is an optimized prime number generator written in C.
|
||||
|
||||
Building
|
||||
--------
|
||||
This project uses CMake to build.
|
||||
There are multiple dependencies to install before compiling the project:
|
||||
- CMake
|
||||
- GMP
|
||||
|
||||
Once the dependencies are installed you can compile by running the following from the root directory of the project:
|
||||
```bash
|
||||
$ cd build/
|
||||
$ cmake ..
|
||||
|
24
cmake/FindGMP.cmake
Normal file
24
cmake/FindGMP.cmake
Normal file
@ -0,0 +1,24 @@
|
||||
# SOURCE: http://stackoverflow.com/questions/29307862/error-linking-gmp-library
|
||||
set(GMP_PREFIX "" CACHE PATH "path ")
|
||||
|
||||
find_path(GMP_INCLUDE_DIR gmp.h gmpxx.h
|
||||
PATHS ${GMP_PREFIX}/include /usr/include /usr/local/include )
|
||||
|
||||
find_library(GMP_LIBRARY NAMES gmp libgmp
|
||||
PATHS ${GMP_PREFIX}/lib /usr/lib /usr/local/lib)
|
||||
|
||||
|
||||
if(GMP_INCLUDE_DIR AND GMP_LIBRARY)
|
||||
get_filename_component(GMP_LIBRARY_DIR ${GMP_LIBRARY} PATH)
|
||||
set(GMP_FOUND TRUE)
|
||||
endif()
|
||||
|
||||
if(GMP_FOUND)
|
||||
if(NOT GMP_FIND_QUIETLY)
|
||||
MESSAGE(STATUS "Found GMP: ${GMP_LIBRARY}")
|
||||
endif()
|
||||
else()
|
||||
if(GMP_FIND_REQUIRED)
|
||||
message(FATAL_ERROR "Could not find GMP")
|
||||
endif()
|
||||
endif()
|
38
src/Main.cpp
38
src/Main.cpp
@ -1,38 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <csignal>
|
||||
|
||||
static bool run;
|
||||
|
||||
void leave(int signum);
|
||||
|
||||
int main(void) {
|
||||
std::cout << "Indivisible v0.1\n";
|
||||
run = true;
|
||||
|
||||
signal(SIGINT, leave);
|
||||
|
||||
std::vector<unsigned long long> primes;
|
||||
primes.push_back(2);
|
||||
unsigned long long num = 2;
|
||||
|
||||
while(run) {
|
||||
bool isPrime = true;
|
||||
for(auto i : primes) {
|
||||
if(i > num / 2) break;
|
||||
if(num % i == 0) isPrime = false;
|
||||
}
|
||||
if(isPrime) {
|
||||
primes.push_back(num);
|
||||
std::cout << num << std::endl;
|
||||
}
|
||||
++num;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void leave(int signum) {
|
||||
std::cout << "Exiting (" << signum << ")\n";
|
||||
run = false;
|
||||
}
|
43
src/list.c
Normal file
43
src/list.c
Normal file
@ -0,0 +1,43 @@
|
||||
#include "list.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/**
|
||||
* This is the number of elements by which the list expands.
|
||||
*/
|
||||
#define BLOCK_SIZE 1024
|
||||
|
||||
void initList(List *restrict l) {
|
||||
l->list = malloc(sizeof(mpz_t) * BLOCK_SIZE);
|
||||
if(!l->list) {
|
||||
fprintf(stderr, "Failed to allocate memory to list!\n");
|
||||
exit(1);
|
||||
}
|
||||
l->size = BLOCK_SIZE;
|
||||
l->end = 0;
|
||||
}
|
||||
|
||||
void deInitList(List *restrict l) {
|
||||
for(size_t 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(l->size == 0) {
|
||||
fprintf(stderr, "`l->size' has overflowed!\n");
|
||||
exit(1);
|
||||
}
|
||||
void *tmp = realloc(l->list, sizeof(mpz_t) * l->size);
|
||||
if(!tmp) {
|
||||
fprintf(stderr, "Failed to allocate more memory to list!\n");
|
||||
exit(1);
|
||||
}
|
||||
l->list = (mpz_t*)tmp;
|
||||
}
|
||||
mpz_init(l->list[l->end]);
|
||||
mpz_set(l->list[l->end++], n);
|
||||
}
|
42
src/list.h
Normal file
42
src/list.h
Normal file
@ -0,0 +1,42 @@
|
||||
/**
|
||||
* @file list.h
|
||||
* @author Deathsbreed <deathsbreed@themusicinnoise.net>
|
||||
* @brief Code responsible for List management.
|
||||
* @details Code responsible for the definition and management of the
|
||||
* List object.
|
||||
*/
|
||||
#pragma once
|
||||
#include <gmp.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/**
|
||||
* @brief An infinitely expanding list type.
|
||||
*/
|
||||
typedef struct {
|
||||
mpz_t *list; //!< The list of elements
|
||||
size_t size; //!< How many elements are in the list
|
||||
size_t end; //!< The last element of the list (in use)
|
||||
} List;
|
||||
|
||||
/**
|
||||
* @brief Initialize a List.
|
||||
* @details Initialize the list and its variables, allocating memory
|
||||
* to the pointer array inside.
|
||||
* @param[in] l A pointer to a List type to be initialized.
|
||||
*/
|
||||
void initList(List *restrict l);
|
||||
|
||||
/**
|
||||
* @brief Deinitialize a List.
|
||||
* @details Release all memory that has been allocated to the list.
|
||||
* @param[in] l A pointer to a List type to be deinitialized.
|
||||
*/
|
||||
void deInitList(List *restrict l);
|
||||
|
||||
/**
|
||||
* @brief Adds a new item to a List type.
|
||||
* @details Add item `n' at the end of a List type.
|
||||
* @param[out] l List to which the variable should be appended.
|
||||
* @param[in] n variable to be appended to the list.
|
||||
*/
|
||||
void addToList(List *l, mpz_t n);
|
125
src/main.c
Normal file
125
src/main.c
Normal file
@ -0,0 +1,125 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
#include <stdbool.h>
|
||||
#include <getopt.h>
|
||||
#include <gmp.h>
|
||||
|
||||
#include "list.h"
|
||||
|
||||
#define VERSION "v0.5"
|
||||
|
||||
static bool run;
|
||||
|
||||
void printUsage(char *progName);
|
||||
void leave();
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
bool f_help = false, f_version = false, f_quiet = false;
|
||||
|
||||
int c;
|
||||
while((c = getopt(argc, argv, "hvq")) != -1) {
|
||||
switch(c) {
|
||||
case 'h':
|
||||
f_help = true;
|
||||
break;
|
||||
case 'v':
|
||||
f_version = true;
|
||||
break;
|
||||
case 'q':
|
||||
f_quiet = true;
|
||||
break;
|
||||
default:
|
||||
printUsage(argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if(f_help) {
|
||||
printUsage(argv[0]);
|
||||
puts(" -h print this help information");
|
||||
puts(" -v print version number of program");
|
||||
puts(" -q quiet mode");
|
||||
return 0;
|
||||
} else if(f_version) {
|
||||
printf("Indivisible %s\n", VERSION);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Quit on ^C by setting `run = false'
|
||||
run = true;
|
||||
signal(SIGINT, leave);
|
||||
|
||||
if(f_quiet) {
|
||||
puts("Use Ctrl+C (SIGINT) to exit.");
|
||||
}
|
||||
|
||||
// Primes we've found
|
||||
List primes;
|
||||
initList(&primes);
|
||||
|
||||
// The number we're going to be testing for
|
||||
mpz_t num;
|
||||
mpz_init(num);
|
||||
|
||||
// Add 2, a known prime to this list
|
||||
mpz_set_ui(num, 2);
|
||||
addToList(&primes, num);
|
||||
if(!f_quiet) {
|
||||
if(mpz_out_str(stdout, 10, num) == 0) {
|
||||
fprintf(stderr, "Could not print to `stdout'!\n");
|
||||
exit(1);
|
||||
}
|
||||
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 primes we've found until we get to half of the number
|
||||
* we're analyzing
|
||||
*/
|
||||
for(size_t i = 0; 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;
|
||||
}
|
||||
|
||||
// `num' is a prime so we add it to the list and print it
|
||||
addToList(&primes, num);
|
||||
if(!f_quiet) {
|
||||
if(mpz_out_str(stdout, 10, num) == 0) {
|
||||
fprintf(stderr, "Could not print to `stdout'!\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
nextPrime:
|
||||
// Add 2 (skip even numbers since they're all divisible by 2)
|
||||
mpz_add_ui(num, num, 2);
|
||||
} while(run);
|
||||
|
||||
printf("Found %zu primes.\n", primes.end);
|
||||
puts("Clearing memory...");
|
||||
// Clear GMP variables
|
||||
mpz_clear(halfNum);
|
||||
mpz_clear(num);
|
||||
// Deinitialize the list
|
||||
deInitList(&primes);
|
||||
|
||||
puts("Exit successful.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void printUsage(char *progName) {
|
||||
printf("%s [-v | -h | -q]\n", progName);
|
||||
}
|
||||
|
||||
void leave() { run = false; }
|
Reference in New Issue
Block a user