diff --git a/include/bfs.h b/include/bfs.h index 695ec63..89a1edc 100644 --- a/include/bfs.h +++ b/include/bfs.h @@ -12,6 +12,6 @@ * @param print_mode The mode for printing the results (1 for full, 2 for minimal) * @return 1 if a solution is found, 0 otherwise */ -int run_bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode); +bool run_bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode); #endif /* BFS_H */ \ No newline at end of file diff --git a/include/hash.h b/include/hash.h index 51a3c27..dabb43a 100644 --- a/include/hash.h +++ b/include/hash.h @@ -12,7 +12,7 @@ * @param c The third integer * @return A hash value for the three integers */ -unsigned int hash_three128(uint128_t a, uint128_t b, uint128_t c); +uint32_t hash_three128(uint128_t a, uint128_t b, uint128_t c); /** * @brief Sorts a triplet of 128-bit integers in ascending order diff --git a/include/maths.h b/include/maths.h index 9fde19d..1df7dc0 100755 --- a/include/maths.h +++ b/include/maths.h @@ -58,7 +58,7 @@ int128_t power(int128_t b, int128_t ex); * @param n1 The first integer * @param print_mode The mode for printing the results */ -void generate_factor_pairs(int128_t* sum, int prime_index, int* exponents, +void generate_factor_pairs(int128_t* sum, int32_t prime_index, int32_t* exponents, int128_t* values, int128_t n1, int print_mode); /** diff --git a/include/node.h b/include/node.h index 515ef64..ded9faf 100644 --- a/include/node.h +++ b/include/node.h @@ -14,7 +14,7 @@ typedef struct Node { * @brief Initializes the node pool with a specified maximum number of nodes * @param max_nodes The maximum number of nodes the pool can hold */ -void node_pool_init(int max_nodes); +void node_pool_init(int32_t max_nodes); /** * @brief Allocates a node from the pool @@ -36,7 +36,7 @@ void node_pool_destroy(); * @brief Returns the current usage of the node pool * @return The number of nodes currently allocated from the pool */ -int node_pool_usage(); +int32_t node_pool_usage(); /** * @brief Tries to push child nodes based on the current node's move @@ -44,11 +44,6 @@ int node_pool_usage(); */ void try_push_children(Node* curr); -typedef struct VisitedEntry { - int128_t a, b, c; - struct VisitedEntry* next; -} VisitedEntry; - /** * @brief Marks a triplet as visited and checks if it's new * @param a The first integer of the triplet @@ -56,7 +51,7 @@ typedef struct VisitedEntry { * @param c The third integer of the triplet * @return 1 if the triplet is new and marked, 0 if it was already visited */ -int visit_and_mark(int128_t a, int128_t b, int128_t c); +bool visit_and_mark(int128_t a, int128_t b, int128_t c); /** * @brief Clears the hash table diff --git a/include/queue.h b/include/queue.h index 58bf09c..97bdafa 100644 --- a/include/queue.h +++ b/include/queue.h @@ -8,7 +8,7 @@ * @brief Initializes the queue with a maximum number of elements * @param max_elements The maximum number of elements the queue can hold */ -void queue_init(int max_elements); +void queue_init(int32_t max_elements); /** * @brief Pushes a node into the queue @@ -26,7 +26,7 @@ Node* queue_pop(); * @brief Returns the current size of the queue * @return The current size of the queue */ -int queue_size(); +int32_t queue_size(); /** * @brief Clears the queue without freeing memory diff --git a/include/utils.h b/include/utils.h index fa8d958..ad6fa46 100644 --- a/include/utils.h +++ b/include/utils.h @@ -2,6 +2,8 @@ #define UTILS_H #include +#include +#include __extension__ typedef __int128 int128_t; __extension__ typedef unsigned __int128 uint128_t; diff --git a/src/bfs.c b/src/bfs.c index 2737e39..609eab0 100644 --- a/src/bfs.c +++ b/src/bfs.c @@ -6,13 +6,13 @@ #include #include -int run_bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode) { +bool run_bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode) { node_pool_reset(); queue_clear(); clear_hash_table(); Node* root = node_pool_alloc(); - if (!root) return 0; + if (!root) return false; root->a = a0; root->b = b0; root->c = c0; root->move = '\0'; root->parent = NULL; @@ -35,20 +35,20 @@ int run_bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode if (!found) { if (print_mode == 1) printf("No solution found.\n"); - return 0; + return false; } - int max_moves = 0; + int32_t max_moves = 0; for (Node* p = found; p && p->move != '\0'; p = p->parent) max_moves++; char* moves; if (print_mode == 1) { moves = malloc(max_moves + 1); - if (!moves) { fprintf(stderr, "Out of memory\n"); return 0; } + if (!moves) { fprintf(stderr, "Out of memory\n"); return false; } moves[max_moves] = '\0'; Node* p = found; - int idx = max_moves - 1; + int32_t idx = max_moves - 1; while (p && p->move != '\0') { moves[idx--] = p->move; p = p->parent; @@ -73,5 +73,5 @@ int run_bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode *sum+=max_moves; if (print_mode == 1) free(moves); - return 1; + return true; } \ No newline at end of file diff --git a/src/hash.c b/src/hash.c index 8f2ae40..ff01923 100644 --- a/src/hash.c +++ b/src/hash.c @@ -1,10 +1,10 @@ #include "hash.h" -unsigned int hash_three128(uint128_t a, uint128_t b, uint128_t c) { +uint32_t hash_three128(uint128_t a, uint128_t b, uint128_t c) { uint128_t x = a + (uint128_t)0x9e3779b97f4a7c15ULL + ((uint128_t)0x9e3779b97f4a7c15ULL << 64); x ^= b + ((uint128_t)0xbf58476d1ce4e5b9ULL + ((uint128_t)0xbf58476d1ce4e5b9ULL << 64)) + (x << 6) + (x >> 2); x ^= c + ((uint128_t)0x94d049bb133111ebULL + ((uint128_t)0x94d049bb133111ebULL << 64)) + (x << 6) + (x >> 2); - return (unsigned int)(x % HASH_SIZE); + return (uint32_t)(x % HASH_SIZE); } void sort_triplet(int128_t* a, int128_t* b, int128_t* c) { diff --git a/src/maths.c b/src/maths.c index 113f041..b56c030 100755 --- a/src/maths.c +++ b/src/maths.c @@ -1,14 +1,15 @@ #include "maths.h" +#include "node.h" +#include "bfs.h" #include #include extern int128_t LIMIT; -extern int run_bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode); -extern int is_new_root(int128_t a, int128_t b, int128_t c); +bool is_new_root(int128_t a, int128_t b, int128_t c); -const int NUM_PRIMES = 3; -const int primes[] = {2, 3, 5}; +const int32_t NUM_PRIMES = 3; +const int32_t primes[] = {2, 3, 5}; bool is_prime(int128_t n) { if (n < 2) return false; @@ -54,7 +55,7 @@ int128_t power(int128_t b, int128_t ex) { return res; } -void generate_factor_pairs(int128_t* sum, int prime_index, int* exponents, +void generate_factor_pairs(int128_t* sum, int32_t prime_index, int32_t* exponents, int128_t* values, int128_t n1, int print_mode) { if (prime_index == NUM_PRIMES) { int128_t n2 = values[0] / n1; @@ -83,8 +84,8 @@ void generate_factor_pairs(int128_t* sum, int prime_index, int* exponents, return; } - int prime = primes[prime_index]; - int exponent = exponents[prime_index]; + int32_t prime = primes[prime_index]; + int32_t exponent = exponents[prime_index]; int128_t power_of_prime = 1; for (int j = 0; j <= exponent; j++) { diff --git a/src/node.c b/src/node.c index 74dd059..7435489 100644 --- a/src/node.c +++ b/src/node.c @@ -6,14 +6,19 @@ #include #include +typedef struct VisitedEntry { + int128_t a, b, c; + struct VisitedEntry* next; +} VisitedEntry; + static Node* pool = NULL; static VisitedEntry* visited_pool = NULL; static VisitedEntry* hash_table[HASH_SIZE] = {0}; -static int pool_ptr = 0; -static int pool_capacity = 0; -static int visited_ptr = 0; +static int32_t pool_ptr = 0; +static int32_t pool_capacity = 0; +static int32_t visited_ptr = 0; -void node_pool_init(int max_nodes) { +void node_pool_init(int32_t max_nodes) { pool_capacity = max_nodes; pool = malloc(pool_capacity * sizeof(Node)); visited_pool = malloc(pool_capacity * sizeof(VisitedEntry)); @@ -44,7 +49,7 @@ void node_pool_destroy() { queue_destroy(); } -int node_pool_usage() { +int32_t node_pool_usage() { return pool_ptr; } @@ -98,9 +103,9 @@ void try_push_children(Node* curr) { } } -int visit_and_mark(int128_t a, int128_t b, int128_t c) { +bool visit_and_mark(int128_t a, int128_t b, int128_t c) { sort_triplet(&a, &b, &c); - unsigned int h = hash_three128(a,b,c); + uint32_t h = hash_three128(a,b,c); VisitedEntry* cur = hash_table[h]; while (cur) { if (cur->a == a && cur->b == b && cur->c == c) return 0; diff --git a/src/queue.c b/src/queue.c index 3b2571b..5d96793 100644 --- a/src/queue.c +++ b/src/queue.c @@ -4,10 +4,10 @@ static Node* queue_front = NULL; static Node* queue_back = NULL; -static int current_size = 0; -static int capacity = 0; +static int32_t current_size = 0; +static int32_t capacity = 0; -void queue_init(int max_elements) { +void queue_init(int32_t max_elements) { capacity = max_elements; queue_front = NULL; queue_back = NULL; @@ -45,7 +45,7 @@ Node* queue_pop() { return r; } -int queue_size() { +int32_t queue_size() { return current_size; } diff --git a/src/stergios.c b/src/stergios.c index ac699b3..8ce989a 100644 --- a/src/stergios.c +++ b/src/stergios.c @@ -9,7 +9,7 @@ #include #include -int LIMIT = 0; +int32_t LIMIT = 0; typedef struct GlobalEntry { int128_t a, b, c; @@ -18,22 +18,22 @@ typedef struct GlobalEntry { static GlobalEntry* global_root_table[HASH_SIZE] = {0}; -int is_new_root(int128_t a, int128_t b, int128_t c) { +bool is_new_root(int128_t a, int128_t b, int128_t c) { sort_triplet(&a, &b, &c); - unsigned int h = hash_three128((uint128_t)a, (uint128_t)b, (uint128_t)c); + uint32_t h = hash_three128((uint128_t)a, (uint128_t)b, (uint128_t)c); GlobalEntry* cur = global_root_table[h]; while (cur) { - if (cur->a == a && cur->b == b && cur->c == c) return 0; + if (cur->a == a && cur->b == b && cur->c == c) return false; cur = cur->next; } GlobalEntry* e = malloc(sizeof(GlobalEntry)); - if (!e) return 1; /* Fallback to running BFS if malloc fails */ + if (!e) return true; /* Fallback to running BFS if malloc fails */ e->a = a; e->b = b; e->c = c; e->next = global_root_table[h]; global_root_table[h] = e; - return 1; + return true; } void clear_global_table() { @@ -100,8 +100,8 @@ int main() { } else if (mode == 2) { clear_global_table(); /* Reset unique root tracker */ - int p = 7; - int exponents[3] = {2*p + 2, p, p}; + int32_t p = 7; + int32_t exponents[3] = {2*p + 2, p, p}; int128_t values[3] = {4 * power(a0, p) * power(b0, p), power(a0, p), power(b0, p)}; generate_factor_pairs(&sum, 0, exponents, values, 1, print_mode);