Skip to content

Commit

Permalink
compress C/C++ code
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Sep 3, 2024
1 parent 739eb7a commit 28940dd
Show file tree
Hide file tree
Showing 21 changed files with 52 additions and 110 deletions.
14 changes: 5 additions & 9 deletions c/src/include/bcd.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,13 @@ BCD_int BCD_from_bytes(const uint8_t *str, size_t chars, bool negative, bool lit
c.zero = false;
c.negative = negative;
c.digits = (packed_BCD_pair *) malloc(sizeof(packed_BCD_pair) * chars);
if (little_endian) {
for (i = 0; i < chars; i++) {
if (little_endian)
for (i = 0; i < chars; i++)
c.digits[i] = str[i];
}
}
else {
size_t j;
for (i = 0, j = chars - 1; i < chars; i++, j--) {
for (i = 0, j = chars - 1; i < chars; i++, j--)
c.digits[i] = str[j];
}
}
for (i = chars - 1; i != -1; i--) {
if (c.digits[i] & 0xF0) {
Expand Down Expand Up @@ -371,9 +368,8 @@ BCD_int mul_bcd(BCD_int x, BCD_int y) {
addend = mul_bcd_pow_10(tmp, pow_10); // this was not added to performance analysis
free_BCD_int(tmp);
}
else {
addend = new_BCD_int(staging, false);;
}
else
addend = new_BCD_int(staging, false);
tmp = add_bcd(answer, addend);
free_BCD_int(addend);
free_BCD_int(answer);
Expand Down
5 changes: 1 addition & 4 deletions c/src/include/digits.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef DIGITS_H
#define DIGITS_H
#pragma once

#include <stdint.h>
#include "macros.h"
Expand Down Expand Up @@ -47,5 +46,3 @@ void free_digit_counter(digit_counter dc) {
if (dc.digits != NULL)
free(dc.digits);
}

#endif
5 changes: 1 addition & 4 deletions c/src/include/factors.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef DIVISORS_H
#define DIVISORS_H
#pragma once

#include <stdint.h>
#include "iterator.h"
Expand Down Expand Up @@ -43,5 +42,3 @@ inline uintmax_t proper_divisor_count(uintmax_t target) {
}
return ret;
}

#endif
5 changes: 1 addition & 4 deletions c/src/include/fibonacci.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef FIBONACCI_H
#define FIBONACCI_H
#pragma once

#include <stdint.h>
#include "iterator.h"
Expand Down Expand Up @@ -36,5 +35,3 @@ fibonacci fibonacci0();
inline fibonacci fibonacci0() {
return fibonacci1(-1);
}

#endif
5 changes: 1 addition & 4 deletions c/src/include/iterator.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef ITERATOR_H
#define ITERATOR_H
#pragma once

#include <stdbool.h>
#include <stdint.h>
Expand Down Expand Up @@ -139,5 +138,3 @@ inline counter counter1(uintmax_t stop) {
*/
return counter2(0, stop);
}

#endif
5 changes: 1 addition & 4 deletions c/src/include/macros.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef MACROS_H
#define MACROS_H
#pragma once

// compiler info section

Expand Down Expand Up @@ -102,5 +101,3 @@
#define POW_OF_MAX_POW_10_64 19
#define MAX_POW_10_128 ((uintmax_t) MAX_POW_10_64 * (uintmax_t) MAX_POW_10_64)
#define POW_OF_MAX_POW_10_128 38

#endif
5 changes: 1 addition & 4 deletions c/src/include/math.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef EULER_MATH_H
#define EULER_MATH_H
#pragma once

#include "macros.h"

Expand Down Expand Up @@ -110,5 +109,3 @@ uintmax_t n_choose_r(uint32_t n, uint32_t r) {
return ret + 1;
}
#endif

#endif
10 changes: 4 additions & 6 deletions c/src/include/primes.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef PRIMES_H
#define PRIMES_H
#pragma once

#include <stdint.h>
#include <inttypes.h>
Expand Down Expand Up @@ -84,10 +83,11 @@ uintmax_t advance_prime_counter(prime_counter *pc) {
if (!broken) { // is prime
if (pc->idx == prime_cache_idx) {
#ifdef PRIME_CACHE_SIZE_LIMIT
if (prime_cache_size == prime_cache_idx && prime_cache_size < PRIME_CACHE_SIZE_LIMIT) {
if (prime_cache_size == prime_cache_idx && prime_cache_size < PRIME_CACHE_SIZE_LIMIT)
#else
if (prime_cache_size == prime_cache_idx) {
if (prime_cache_size == prime_cache_idx)
#endif
{
size_t new_size = prime_cache_size * 2;
#ifdef PRIME_CACHE_SIZE_LIMIT
if (new_size > PRIME_CACHE_SIZE_LIMIT)
Expand Down Expand Up @@ -340,5 +340,3 @@ inline bool is_prime(uintmax_t n) {
*/
return n && n != 1 && !is_composite(n);
}

#endif
37 changes: 12 additions & 25 deletions c/src/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ typedef struct {
AnswerType type;
} Answer;

#ifdef _WIN32
#define strtok_r strtok_s
#endif

Answer EMSCRIPTEN_KEEPALIVE get_answer(uint16_t id) {
Answer ret = {
.id = id,
Expand All @@ -147,54 +151,33 @@ Answer EMSCRIPTEN_KEEPALIVE get_answer(uint16_t id) {
char s_id[6];
snprintf(s_id, sizeof(s_id), "%" PRIu16, id);

#ifdef _WIN32
char *line = strtok_s(answers, "\n", &linepointer); // skip header
while ((line = strtok_s(NULL, "\n", &linepointer)) != NULL) {
#else
char *line = strtok_r(answers, "\n", &linepointer); // skip header
while ((line = strtok_r(NULL, "\n", &linepointer)) != NULL) {
#endif
#ifdef _WIN32
char *token = strtok_s(line, "\t", &tabpointer);
#else
char *token = strtok_r(line, "\t", &tabpointer);
#endif
if (strcmp(token, s_id) != 0)
continue;

#ifdef _WIN32
token = strtok_s(NULL, "\t", &tabpointer);
#else
token = strtok_r(NULL, "\t", &tabpointer);
#endif
if (!token)
continue;

if (strcmp(token, "uint") == 0) {
if (strcmp(token, "uint") == 0)
ret.type = UINT8T; // will adjust size later
} else if (strcmp(token, "int") == 0) {
else if (strcmp(token, "int") == 0)
ret.type = INT8T; // will adjust size later
} else if (strcmp(token, "str") == 0) {
else if (strcmp(token, "str") == 0)
ret.type = STRINGT;
} else {
else {
fprintf(stderr, "Error: Unknown type '%s'\n", token);
return ret;
}

#ifdef _WIN32
token = strtok_s(NULL, "\t", &tabpointer);
#else
token = strtok_r(NULL, "\t", &tabpointer);
#endif
if (!token)
continue;
size_t size = strtoull(token, NULL, 10);

#ifdef _WIN32
token = strtok_s(NULL, "\t", &tabpointer);
#else
token = strtok_r(NULL, "\t", &tabpointer);
#endif
if (!token)
continue;

Expand Down Expand Up @@ -273,3 +256,7 @@ Answer EMSCRIPTEN_KEEPALIVE get_answer(uint16_t id) {
free(answers);
return ret;
}

#ifdef _WIN32
#undef strtok_r
#endif
4 changes: 1 addition & 3 deletions c/src/p0008.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0008() {
digits[i] = plain_digits[i] - '0';
for (i = 0; i < 1000 - 13; i++) {
tmp = digits[i];
for (j = i + 1; j < i + 13; j++) {
// printf("%" PRIu64 " * %d\n", tmp, digits[j]);
for (j = i + 1; j < i + 13; j++)
tmp *= digits[j];
}
answer = max(answer, tmp);
}
return answer;
Expand Down
3 changes: 1 addition & 2 deletions c/src/p0017.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ British usage.

uint32_t to_string_len(uint64_t n);
uint32_t to_string_len(uint64_t n) {
if (n >= 1000) {
if (n >= 1000)
return to_string_len(n / 1000 % 100) + 8; //len("thousand")
}
else if (n >= 100) {
uint32_t hundreds = to_string_len(n / 100 % 10) + 7; // len("hundred")
if (n % 100)
Expand Down
4 changes: 2 additions & 2 deletions c/src/p0025.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ What is the index of the first term in the Fibonacci sequence to contain 1000 di
uint64_t EMSCRIPTEN_KEEPALIVE p0025() {
uint64_t answer = 2;
BCD_int a = BCD_one, b = BCD_one;
while (b.decimal_digits < 1000) {
do {
iadd_bcd(&a, b);
swap(a, b);
answer++;
}
} while (b.decimal_digits < 1000)
free_BCD_int(a);
free_BCD_int(b);
return answer;
Expand Down
4 changes: 2 additions & 2 deletions c/src/p0030.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0030() {
for (uint64_t i = 2; i < 1000000; i++) {
digit_counter dc = digits(i);
sum = 0;
while (!dc.exhausted) {
do {
tmp = next(dc);
sum += tmp * tmp * tmp * tmp * tmp;
}
} while (!dc.exhausted);
if (sum == i)
answer += i;
free_digit_counter(dc);
Expand Down
3 changes: 1 addition & 2 deletions c/src/p0076.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ uint32_t EMSCRIPTEN_KEEPALIVE p0076() {
answer += (100 + counts[2] - sum) / 2;
idx = 2;
do {
counts[idx] = 0;
++idx;
counts[idx++] = 0;
counts[idx] += idx;
sum = counts[2];
for (i = 3; i < 100; ++i)
Expand Down
5 changes: 1 addition & 4 deletions cplusplus/src/include/macros.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef MACROS_H
#define MACROS_H
#pragma once

// compiler info section

Expand Down Expand Up @@ -74,5 +73,3 @@
#define POW_OF_MAX_POW_10_64 19
#define MAX_POW_10_128 ((uintmax_t) MAX_POW_10_64 * (uintmax_t) MAX_POW_10_64)
#define POW_OF_MAX_POW_10_128 38

#endif
5 changes: 1 addition & 4 deletions cplusplus/src/include/math.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef EULER_MATH_H
#define EULER_MATH_H
#pragma once

#include "macros.hpp"
#include <math.h>
Expand Down Expand Up @@ -78,5 +77,3 @@ uintmax_t n_choose_r(uint32_t n, uint32_t r) {
free(factors);
return answer;
}

#endif
20 changes: 8 additions & 12 deletions cplusplus/src/include/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,24 @@ std::string get_parent_directory(const std::string &path, const uint32_t levels)
std::string dir = path;
for (uint32_t i = 0; i < levels; ++i) {
size_t pos = dir.find_last_of(PATH_SEPARATOR);
if (pos != std::string::npos) {
if (pos != std::string::npos)
dir.erase(pos);
} else {
else
break;
}
}
return dir;
}

std::string get_data_file(const std::string &name) {
char absolute_path[MAX_PATH];
std::ostringstream oss;
#ifdef _WIN32
if (!_fullpath(absolute_path, __FILE__, sizeof(absolute_path))) {
std::ostringstream oss;
oss << "_fullpath failed with error code " << GetLastError();
throw std::runtime_error(oss.str());
}
#else
if (!realpath(__FILE__, absolute_path)) {
std::ostringstream oss;
oss << "realpath failed with error code " << errno;
throw std::runtime_error(oss.str());
}
Expand All @@ -56,15 +54,13 @@ std::string get_data_file(const std::string &name) {
std::string file_path = get_parent_directory(std::string(absolute_path), 4) + PATH_SEPARATOR "_data" PATH_SEPARATOR + name;
std::ifstream file(file_path.c_str(), std::ios::in | std::ios::binary);
if (!file) {
std::ostringstream oss;
oss << "Failed to open file: " << file_path << " with error code " << errno;
throw std::runtime_error(oss.str());
}

std::ostringstream content_stream;
content_stream << file.rdbuf();
if (!file) {
std::ostringstream oss;
oss << "Error reading file: " << file_path << " with error code " << errno;
throw std::runtime_error(oss.str());
}
Expand Down Expand Up @@ -131,13 +127,13 @@ Answer EMSCRIPTEN_KEEPALIVE get_answer(const uint16_t id) {
if (!std::getline(lineStream, token, '\t'))
continue;

if (token == "uint") {
if (token == "uint")
answer.type = UINT8T; // will adjust size later
} else if (token == "int") {
else if (token == "int")
answer.type = INT8T; // will adjust size later
} else if (token == "str") {
else if (token == "str")
answer.type = STRINGT;
} else {
else {
std::cerr << "Error: Unknown type '" << token << "'\n";
return answer;
}
Expand Down Expand Up @@ -202,7 +198,7 @@ Answer EMSCRIPTEN_KEEPALIVE get_answer(const uint16_t id) {
strncpy(answer.value.string, token.c_str(), size);
answer.value.string[size] = 0;
} else {
std::cerr << "Error: Memory allocation failed for string\n";
std::cerr << "Error: Memory allocation failed for string\n";
Answer err = {{0}};
return err;
}
Expand Down
16 changes: 6 additions & 10 deletions cplusplus/src/p0002.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,13 @@ four million, find the sum of the even-valued terms.
uint64_t EMSCRIPTEN_KEEPALIVE p0002() {
uint64_t answer = 0,
a = 1, b = 2, t;
while (b < 4000000) {
// odd (1, 3, 13, 55, ...)
// odd (1, 5, 21, 89, ...)
// even (2, 8, 34, 144, ...)
do {
answer += b;
for (uint8_t z = 0; z < 3; z++) {
t = b;
b = a + b;
a = t;
}
}
// see python fibonacci file for proof this works
t = b;
b = a + b * 4;
a = t;
} while (b < 4000000);
return answer;
}

Expand Down
Loading

0 comments on commit 28940dd

Please sign in to comment.