Skip to content

Commit

Permalink
Prep for emscripten distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Aug 20, 2024
1 parent e2dae7d commit 68a11c2
Show file tree
Hide file tree
Showing 23 changed files with 88 additions and 29 deletions.
35 changes: 30 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,24 @@ Olivia's Project Euler Solutions
+============+============================+========+===================+
| C | C99+ in: |clang|, |br| | 25 | |Ci| |br| |
| | |gcc|, |pcc|, & |tcc| |br| | | |C-Cov| |br| |
| | C11+ in: |msvc| [1]_ | | |CodeQL| |br| |
| | | | |C-lint| |
| | C11+ in: |msvc| [1]_ |br| | | |CodeQL| |br| |
| | Browser [2]_ | | |C-lint| |
+------------+----------------------------+--------+-------------------+
| C++ | C++98+ in: |br| |clang|, & | 18 | |Cpi| |br| |
| | |gcc| |br| | | |Cp-Cov| |br| |
| | C++14+ in: |msvc| [1]_ | | |CodeQL| |br| |
| | | | |Cp-lint| |
| | |br| Browser [3]_ | | |Cp-lint| |
+------------+----------------------------+--------+-------------------+
| C# | .NET 2+ | 18 | |C#i| |br| |
| | | | |Cs-Cov| |br| |
| C# | .NET 2+ |br| | 18 | |C#i| |br| |
| | Browser [#]_ | | |Cs-Cov| |br| |
| | | | |CodeQL| |br| |
| | | | |C#-lint| |
+------------+----------------------------+--------+-------------------+
| Java | Java 8+ in Corretto, |br| | 18 | |Javai| |br| |
| | Dragonwell, Liberica, |br| | | |Ja-Cov| |br| |
| | Microsoft, Oracle, |br| | | |CodeQL| |br| |
| | Semeru, Temurin, & Zulu | | |Java-lint| |
| | |br| Browser [#]_ | | |Java-lint| |
+------------+----------------------------+--------+-------------------+
| JavaScript | Node 12+ |br| | 26 | |JavaScript| |br| |
| | Bun 1.0+ |br| | | |Js-Cov| |br| |
Expand All @@ -93,6 +94,22 @@ Olivia's Project Euler Solutions
| | | | |RustClippy| |
+------------+----------------------------+--------+-------------------+

.. |test-c-link| raw:: html

<a href="https://euler.oliviaappleton.com/_static/test-c.html" target="_blank">click here!</a>

.. |test-cp-link| raw:: html

<a href="https://euler.oliviaappleton.com/_static/test-cp.html" target="_blank">click here!</a>

.. |test-cs-link| raw:: html

<a href="https://euler.oliviaappleton.com/_static/test-cs.html" target="_blank">click here!</a>

.. |test-ja-link| raw:: html

<a href="https://euler.oliviaappleton.com/_static/test-ja.html" target="_blank">click here!</a>

.. |test-js-link| raw:: html

<a href="https://euler.oliviaappleton.com/_static/test-js.html" target="_blank">click here!</a>
Expand All @@ -106,6 +123,14 @@ Olivia's Project Euler Solutions
<a href="https://euler.oliviaappleton.com/_static/test-rs.html" target="_blank">click here!</a>

.. [1] This is the earliest standard the MSVC explicitly supports.
.. [2] While these solutions do run in most browsers, they need to be bundled with Emscripten first,
and these tests not yet automated as in |clang| and |gcc|. To run these tests yourself, |test-c-link|
.. [3] While these solutions do run in most browsers, they need to be bundled with Emscripten first,
and these tests not yet automated as in |clang| and |gcc|. To run these tests yourself, |test-cp-link|
.. [#] While these solutions do run in most browsers, they need to be bundled with DotNetAnywhere first,
and these tests not yet automated as in mainline .NET. To run these tests yourself, |test-cs-link|
.. [#] While these solutions do run in most browsers, they need to be bundled with CheerpJ first,
and these tests not yet automated as in mainline Java. To run these tests yourself, |test-ja-link|
.. [#] While these solutions do run in most browsers, they need to be bundled with WebPack 5.93+ & Babel 7.25+ first,
and these tests not yet automated as in Nodejs and Bun. To run these tests yourself, |test-js-link|
.. [#] While these solutions do run in most browsers, they need to be bundled with Pyodide 0.26.2+ first, and these
Expand Down
12 changes: 12 additions & 0 deletions cplusplus/src/include/macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
#else
#define GCC_COMPILER 0
#endif
#ifdef __EMSCRIPTEN__
#define EMCC_COMPILER 1
#else
#define EMCC_COMPILER 0
#endif

#if (defined(_M_X64) || defined(_M_AMD64) || defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64))
#define X64_COMPILER 1
Expand All @@ -38,6 +43,13 @@
#define ARM_THUMB 1
#else
#define ARM_THUMB 0
#
#if (defined(__wasm__) || defined(__wasm32__) || defined(__wasm64__))
#define WASM_COMPILER 1
#include <emscripten.h>
#else
#define WASM_COMPILER 0
#define EMSCRIPTEN_KEEPALIVE
#endif

// helper macro function section
Expand Down
3 changes: 2 additions & 1 deletion cplusplus/src/p0000_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ This paragraph should be replaced by the problem description, excluding images.
#define EULER_P0000
#include <stdint.h>
#include <iostream>
#include "../macros.hpp"

uint64_t p0000() {
uint64_t EMSCRIPTEN_KEEPALIVE p0000() {
return 0;
}

Expand Down
3 changes: 2 additions & 1 deletion cplusplus/src/p0001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
#define EULER_P0001
#include <stdint.h>
#include <iostream>
#include "../macros.hpp"

uint64_t p0001() {
uint64_t EMSCRIPTEN_KEEPALIVE p0001() {
uint64_t answer = 0;
for (int i = 0; i < 1000; i += 3)
answer += i;
Expand Down
3 changes: 2 additions & 1 deletion cplusplus/src/p0002.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ four million, find the sum of the even-valued terms.
#define EULER_P0002
#include <stdint.h>
#include <iostream>
#include "../macros.hpp"

uint64_t p0002() {
uint64_t EMSCRIPTEN_KEEPALIVE p0002() {
uint64_t answer = 0,
a = 1, b = 2, t;
while (b < 4000000) {
Expand Down
3 changes: 2 additions & 1 deletion cplusplus/src/p0004.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ Find the largest palindrome made from the product of two 3-digit numbers.
#include <algorithm>
#include <cstdio>
#include <iostream>
#include "../macros.hpp"

uint32_t p0004() {
uint32_t EMSCRIPTEN_KEEPALIVE p0004() {
uint32_t answer = 0, i, j, prod;
for (i = 100; i < 1000; i++) {
for (j = 100; j < 1000; j++) {
Expand Down
3 changes: 2 additions & 1 deletion cplusplus/src/p0006.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ natural numbers and the square of the sum.
#define EULER_P0006
#include <stdint.h>
#include <iostream>
#include "../macros.hpp"

uint64_t p0006() {
uint64_t EMSCRIPTEN_KEEPALIVE p0006() {
uint64_t sum = 100 * 101 / 2, sum_of_squares = 0;
for (uint64_t i = 1; i < 101; i++)
sum_of_squares += i * i;
Expand Down
3 changes: 2 additions & 1 deletion cplusplus/src/p0008.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ Find the thirteen adjacent digits in the 1000-digit number that have the greates
#define EULER_P0008
#include <stdint.h>
#include <iostream>
#include "../macros.hpp"

uint64_t p0008() {
uint64_t EMSCRIPTEN_KEEPALIVE p0008() {
size_t i, j;
uint64_t answer = 0, tmp;
const char *plain_digits = ("73167176531330624919225119674426574742355349194934"
Expand Down
3 changes: 2 additions & 1 deletion cplusplus/src/p0009.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ Find the product abc.
#define EULER_P0009
#include <stdint.h>
#include <iostream>
#include "../macros.hpp"

uint64_t p0009() {
uint64_t EMSCRIPTEN_KEEPALIVE p0009() {
uint64_t answer = 0;
for (uint32_t c = 3; !answer && c < 1000; c++) {
for (uint32_t b = 2; b < c; b++) {
Expand Down
3 changes: 2 additions & 1 deletion cplusplus/src/p0011.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ the 20×20 grid?
#define EULER_P0011
#include <stdint.h>
#include <iostream>
#include "../macros.hpp"

static const uint8_t grid[20][20] = {
{ 8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8},
Expand All @@ -59,7 +60,7 @@ static const uint8_t grid[20][20] = {
{ 1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48}
};

uint64_t p0011() {
uint64_t EMSCRIPTEN_KEEPALIVE p0011() {
uint64_t answer = 0, tmp;
uint8_t i, j;
for (i = 0; i < 20; i++) {
Expand Down
3 changes: 2 additions & 1 deletion cplusplus/src/p0013.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Work out the first ten digits of the sum of the following one-hundred 50-digit n
#define EULER_P0013
#include <stdint.h>
#include <iostream>
#include "../macros.hpp"

static const uint64_t numbers[100][3] = {
{ 37107287533902, 102798797998220837, 590246510135740250 },
Expand Down Expand Up @@ -215,7 +216,7 @@ static const uint64_t numbers[100][3] = {
static const uint64_t ten18 = 1000000000000000000;
static const uint64_t ten10 = 10000000000;

uint64_t p0013() {
uint64_t EMSCRIPTEN_KEEPALIVE p0013() {
uint64_t high = 0, med = 0, low = 0;
for (uint8_t i = 0; i < 100; ++i) {
low += numbers[i][2];
Expand Down
3 changes: 2 additions & 1 deletion cplusplus/src/p0014.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ NOTE: Once the chain starts the terms are allowed to go above one million.
#define EULER_P0014
#include <stdint.h>
#include <iostream>
#include "../macros.hpp"

#define CACHE_SIZE 1000000
static uint32_t collatz_len_cache[CACHE_SIZE] = {0, 1, 0};
Expand All @@ -43,7 +44,7 @@ uint32_t collatz_len(uint64_t n) {
return ret;
}

uint64_t p0014() {
uint64_t EMSCRIPTEN_KEEPALIVE p0014() {
uint64_t answer = 2, length = 2, tmp;
for (uint64_t test = 3; test < 1000000; test++) {
tmp = collatz_len(test);
Expand Down
3 changes: 2 additions & 1 deletion cplusplus/src/p0015.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ How many such routes are there through a 20×20 grid?
#define EULER_P0015
#include <stdint.h>
#include <iostream>
#include "../macros.hpp"
#include "include/math.hpp"

#define lattice_paths(height, width) (n_choose_r(height + width, height))

uint64_t p0015() {
uint64_t EMSCRIPTEN_KEEPALIVE p0015() {
return lattice_paths(20, 20);
}

Expand Down
3 changes: 2 additions & 1 deletion cplusplus/src/p0016.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ What is the sum of the digits of the number 2**1000?
#include <stdint.h>
#include <iostream>
#include <vector>
#include "../macros.hpp"


uint64_t p0016() {
uint64_t EMSCRIPTEN_KEEPALIVE p0016() {
std::vector<uint64_t> numbers(18, 0);
const uint64_t ten17 = 100000000000000000;
numbers[0] = 1;
Expand Down
3 changes: 2 additions & 1 deletion cplusplus/src/p0017.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ British usage.
#include <iostream>
#include <stdexcept>
#include <string>
#include "../macros.hpp"

std::string ToString(uint64_t n);
std::string ToString(uint64_t n) {
Expand Down Expand Up @@ -93,7 +94,7 @@ std::string ToString(uint64_t n) {
}
}

uint64_t p0017() {
uint64_t EMSCRIPTEN_KEEPALIVE p0017() {
uint64_t answer = 0;
for (uint32_t x = 1; x < 1001; x += 1) {
std::string str = ToString(x);
Expand Down
3 changes: 2 additions & 1 deletion cplusplus/src/p0019.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ How many Sundays fell on the first of the month during the twentieth century
#include <iostream>
#include <stdexcept>
#include <stdint.h>
#include "../macros.hpp"

#ifdef _WIN32
#include <cstring>
Expand All @@ -34,7 +35,7 @@ How many Sundays fell on the first of the month during the twentieth century
#include <ctime>
#endif

uint16_t p0019() {
uint16_t EMSCRIPTEN_KEEPALIVE p0019() {
uint16_t answer = 0;

#ifdef _WIN32
Expand Down
3 changes: 2 additions & 1 deletion cplusplus/src/p0020.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ Find the sum of the digits in the number 100!
#include <stdint.h>
#include <iostream>
#include <vector>
#include "../macros.hpp"


uint64_t p0020() {
uint64_t EMSCRIPTEN_KEEPALIVE p0020() {
std::vector<uint64_t> numbers(10, 0);
const uint64_t ten17 = 100000000000000000;
numbers[0] = 1;
Expand Down
3 changes: 2 additions & 1 deletion cplusplus/src/p0022.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include "../macros.hpp"
#include "include/utils.hpp"

uint64_t p0022() {
uint64_t EMSCRIPTEN_KEEPALIVE p0022() {
uint64_t answer = 0;
std::string fstring = get_data_file("p0022_names.txt");
const uint32_t name_count = 5163;
Expand Down
3 changes: 2 additions & 1 deletion cplusplus/src/p0034.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ Note: as 1! = 1 and 2! = 2 are not sums they are not included.
#include <stdint.h>
#include <iostream>
#include <cstdio>
#include "../macros.hpp"
#include "include/math.hpp"

uint64_t p0034() {
uint64_t EMSCRIPTEN_KEEPALIVE p0034() {
uint64_t answer = 0, sum;
for (uint64_t i = 10; i < 100000; i++) {
sum = 0;
Expand Down
2 changes: 1 addition & 1 deletion cplusplus/src/p0076.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ positive integers?
#include <iostream>
#include "include/macros.hpp"

uint32_t p0076() {
uint32_t EMSCRIPTEN_KEEPALIVE p0076() {
uint32_t answer = 0;
uint8_t idx, i, sum = 100, counts[101] = {0, 0, 100, 0};
while (!counts[100]) {
Expand Down
3 changes: 2 additions & 1 deletion cplusplus/src/p0836.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ Find f(20230401, 57). Give as your answer the concatenation of the first letters
#ifndef EULER_P0836
#define EULER_P0836
#include <iostream>
#include "../macros.hpp"

std::string p0836() {
std::string EMSCRIPTEN_KEEPALIVE p0836() {
return "aprilfoolsjoke";
}

Expand Down
6 changes: 4 additions & 2 deletions cplusplus/src/tests/test_compiler_macros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

int main(int argc, char const *argv[]) {
printf(
"%d %d %d %d %d %d",
"%d %d %d %d %d %d %d %d",
CL_COMPILER,
CLANG_COMPILER,
GCC_COMPILER,
EMCC_COMPILER,
X86_COMPILER,
X64_COMPILER,
ARM_COMPILER
ARM_COMPILER,
WASM_COMPILER
);
return 0;
}
8 changes: 5 additions & 3 deletions cplusplus/test_euler.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,11 @@ def test_compiler_macros(compiler):
assert flags[0] == compiler.startswith("CL+")
assert flags[1] == compiler.startswith("CLANG")
assert flags[2] == compiler.startswith("GCC")
assert flags[3] == (EXE_EXT == "x86" or expect_32)
assert flags[4] == (EXE_EXT == "x86_64" and not expect_32)
assert flags[5] == (EXE_EXT not in ("x86", "x86_64", "exe"))
assert flags[3] == compiler.startswith("EMCC")
assert flags[4] == (EXE_EXT == "x86" or expect_32)
assert flags[5] == (EXE_EXT == "x86_64" and not expect_32)
assert flags[6] == ("arm" in EXE_EXT.lower())
assert flags[7] == ("wasm" in EXE_EXT.lower())


# @mark.skipif('NO_OPTIONAL_TESTS or ONLY_SLOW')
Expand Down

0 comments on commit 68a11c2

Please sign in to comment.