omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / bench / omc_efficiency / kernels / sieve.c
| 1 | /* sieve.c -- REFERENCE TWIN ONLY (gcc oracle) of kernels/sieve.om: same passes, same output. */ |
| 2 | #include <stdio.h> |
| 3 | #include <stdint.h> |
| 4 | #include <time.h> |
| 5 | static void putnum(uint64_t v) { printf("%llu\n", (unsigned long long)v); } |
| 6 | /* OMC-EFF-2: the computation is timed in process with CLOCK_MONOTONIC and the nanoseconds go to stderr. |
| 7 | The empty asm statements are compiler barriers: they keep the timed work between the two clock reads. */ |
| 8 | static uint64_t nowns(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return (uint64_t)t.tv_sec * 1000000000u + (uint64_t)t.tv_nsec; } |
| 9 | static void putns(uint64_t t0, uint64_t t1) { fprintf(stderr, "%llu\n", (unsigned long long)(t1 - t0)); } |
| 10 | static uint8_t fl[2000001]; |
| 11 | static __attribute__((noinline)) uint64_t pass(void) { |
| 12 | for (uint64_t i = 0; i < 2000001u; i++) fl[i] = 0; |
| 13 | for (uint64_t p = 2; p * p <= 2000000u; p++) |
| 14 | if (fl[p] == 0) |
| 15 | for (uint64_t m = p * p; m <= 2000000u; m += p) fl[m] = 1; |
| 16 | uint64_t c = 0; |
| 17 | for (uint64_t i = 0; i < 1999999u; i++) if (fl[i + 2] == 0) c++; |
| 18 | return c; |
| 19 | } |
| 20 | int main(void) { |
| 21 | uint64_t t0 = nowns(); |
| 22 | uint64_t sum = 0, last = 0; |
| 23 | __asm__ volatile("" : "+r"(sum), "+r"(last) : : "memory"); |
| 24 | for (uint64_t r = 0; r < 50; r++) { last = pass(); sum += last; } |
| 25 | __asm__ volatile("" : : "r"(last), "r"(sum) : "memory"); |
| 26 | putns(t0, nowns()); |
| 27 | putnum(last); putnum(sum); |
| 28 | return 0; |
| 29 | } |