interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / bench / omc_efficiency / kernels / branch.c

27 lines · 1196 bytes Raw Blame History
1/* branch.c -- REFERENCE TWIN ONLY (gcc oracle) of kernels/branch.om: same loop, same output. */
2#include <stdio.h>
3#include <stdint.h>
4#include <time.h>
5static 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. */
8static uint64_t nowns(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return (uint64_t)t.tv_sec * 1000000000u + (uint64_t)t.tv_nsec; }
9static void putns(uint64_t t0, uint64_t t1) { fprintf(stderr, "%llu\n", (unsigned long long)(t1 - t0)); }
10int main(void) {
11 uint64_t t0 = nowns();
12 uint64_t tot = 0, mx = 0;
13 __asm__ volatile("" : "+r"(tot), "+r"(mx));
14 for (uint64_t n = 0; n < 1000000u; n++) {
15 uint64_t x = n + 1, k = 0;
16 while (x != 1) {
17 if ((x & 1) == 0) x = x >> 1; else x = x * 3 + 1;
18 k++;
19 }
20 tot += k;
21 if (k > mx) mx = k;
22 }
23 __asm__ volatile("" : : "r"(tot), "r"(mx) : "memory");
24 putns(t0, nowns());
25 putnum(tot); putnum(mx);
26 return 0;
27}