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 / fib.c
| 1 | /* fib.c -- REFERENCE TWIN ONLY (gcc oracle) of kernels/fib.om: same recursion, 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 uint64_t fib(uint64_t n) { |
| 11 | if (n < 2) return n; |
| 12 | uint64_t a = fib(n - 1); |
| 13 | uint64_t b = fib(n - 2); |
| 14 | return a + b; |
| 15 | } |
| 16 | int main(void) { |
| 17 | uint64_t t0 = nowns(); |
| 18 | uint64_t n = 38; |
| 19 | __asm__ volatile("" : "+r"(n)); |
| 20 | uint64_t r = fib(n); |
| 21 | __asm__ volatile("" : : "r"(r) : "memory"); |
| 22 | putns(t0, nowns()); |
| 23 | putnum(r); |
| 24 | return 0; |
| 25 | } |