Forráskód Böngészése

Add Native C Implementation

rexim 5 éve
szülő
commit
4a37bcfec7
3 módosított fájl, 37 hozzáadás és 3 törlés
  1. 1 0
      .gitignore
  2. 5 3
      2020-09-04.org
  3. 31 0
      main.c

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+a.out

+ 5 - 3
2020-09-04.org

@@ -1,7 +1,9 @@
 * Extending Native Browser Capabilities with WebAssembly
 
-- [ ] Premise: Performance of WASM is probably comparable to native code
-  - [ ] Let's check it by implementing a bunch of benchmarks
-    - [ ] https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/c.html
+- [-] Premise: Performance of WASM is probably comparable to native code
+  - [-] Let's check it by implementing a bunch of benchmarks
+    - [-] Prime Number Crunching
+      - [X] [[file:main.c][Native C Implementation]]
+      - [ ] WebAssembly Implemention
 - [ ] Idea: Let's imagine we invented a new image format that didn't exist before
   - [ ] If we implement the image support in WASM can we make it feel like a native browser support

+ 31 - 0
main.c

@@ -0,0 +1,31 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#define N (1000 * 1000)
+
+int primes[N] = {2};
+size_t primes_count = 1;
+
+int is_prime(int x)
+{
+    for (size_t i = 0; primes[i] * primes[i] <= x; ++i) {
+        if (x % primes[i] == 0) {
+            return 0;
+        }
+    }
+
+    return 1;
+}
+
+int main(int argc, char *argv[])
+{
+    for (int x = 3; primes_count < N; ++x) {
+        if (is_prime(x)) {
+            primes[primes_count++] = x;
+        }
+    }
+
+    printf("%d\n", primes[primes_count - 1]);
+
+    return 0;
+}