/* SPDX-License-Identifier: GPL-3.0-or-later
* Authored CPU workloads. Device-themed cases use synthetic buffers, not I/O.
* setup and collect run outside the measured interval. All result state is
* serialized into an output buffer for comparison with the independent Python model.
*/
#include <cmoc.h>
#include "workloads.h"
#ifndef BENCH_CASE
#error Define BENCH_CASE as an integer from 0 through 7
#endif
unsigned char bench_output[BENCH_OUTPUT_SIZE];
static unsigned collected;
static void begin_collect(void) { collected = 0; }
static void emit(unsigned value) { bench_output[collected++] = (unsigned char)value; }
#if BENCH_CASE != 4
static void emit_word(unsigned value) { emit(value >> 8); emit(value); }
#endif
#if BENCH_CASE == 0
static void emit_long(unsigned long value) { emit_word((unsigned)(value >> 16)); emit_word((unsigned)value); }
#endif
#if BENCH_CASE == 0
static unsigned values[16], factors[16];
static unsigned long accumulated;
void bench_setup(unsigned seed) {
unsigned i;
accumulated = 0;
for (i = 0; i < 16; ++i) {
values[i] = seed * 37 + i * 257 + 100;
factors[i] = (seed + i * 13 + 17) & 255;
}
}
void bench_run(void) {
unsigned round, i;
for (round = 0; round < 8; ++round)
for (i = 0; i < 16; ++i) {
unsigned long product = (unsigned long)values[i] * (factors[i] + 1) + ((unsigned long)i << 16);
accumulated += product;
values[i] = (unsigned)(product >> 8);
factors[i] = (factors[i] + i + round + 3) & 255;
}
}
void bench_collect(void) {
unsigned i;
begin_collect();
for (i = 0; i < 16; ++i) { emit_word(values[i]); emit_word(factors[i]); }
emit_long(accumulated);
}
#elif BENCH_CASE == 1
struct Sprite { unsigned x, y; unsigned char vx, vy; unsigned hits; };
static struct Sprite sprites[12];
void bench_setup(unsigned seed) {
unsigned i;
for (i = 0; i < 12; ++i) {
sprites[i].x = (seed + i * 17) & 255;
sprites[i].y = (seed * 3 + i * 11) & 255;
sprites[i].vx = (unsigned char)((seed + i * 3) & 7);
sprites[i].vy = (unsigned char)((seed + i * 5) & 7);
sprites[i].hits = 0;
}
}
void bench_run(void) {
unsigned round, i, j, dx, dy;
for (round = 0; round < 12; ++round) {
for (i = 0; i < 12; ++i) {
sprites[i].x = (sprites[i].x + sprites[i].vx) & 255;
sprites[i].y = (sprites[i].y + sprites[i].vy) & 255;
}
for (i = 0; i < 12; ++i)
for (j = i + 1; j < 12; ++j) {
dx = sprites[i].x > sprites[j].x ? sprites[i].x - sprites[j].x : sprites[j].x - sprites[i].x;
dy = sprites[i].y > sprites[j].y ? sprites[i].y - sprites[j].y : sprites[j].y - sprites[i].y;
if (dx < 8 && dy < 8) { ++sprites[i].hits; ++sprites[j].hits; }
}
}
}
void bench_collect(void) {
unsigned i;
begin_collect();
for (i = 0; i < 12; ++i) {
emit_word(sprites[i].x); emit_word(sprites[i].y);
emit(sprites[i].vx); emit(sprites[i].vy); emit_word(sprites[i].hits);
}
}
#elif BENCH_CASE == 2
static unsigned char source[128], destination[128];
static unsigned crc;
void bench_setup(unsigned seed) {
unsigned i;
crc = seed;
for (i = 0; i < 128; ++i) { source[i] = (unsigned char)(seed + i * 19); destination[i] = 0; }
}
void bench_run(void) {
unsigned round, i, bit;
for (round = 0; round < 4; ++round) {
memcpy(destination, source, sizeof(source));
for (i = 0; i < 128; ++i) {
unsigned char c = destination[i];
if (c >= 'a' && c <= 'z') c -= 'a' - 'A';
destination[i] = c;
crc ^= (unsigned)c << 8;
for (bit = 0; bit < 8; ++bit)
crc = (crc & 0x8000) ? (crc << 1) ^ 0x1021 : crc << 1;
source[i] = (unsigned char)(source[i] + round + 1);
}
}
}
void bench_collect(void) {
unsigned i;
begin_collect();
for (i = 0; i < 128; ++i) { emit(source[i]); emit(destination[i]); }
emit_word(crc);
}
#elif BENCH_CASE == 3
static unsigned char sorted[32], state;
static unsigned counters[4];
void bench_setup(unsigned seed) {
unsigned i;
state = (unsigned char)(seed & 3);
for (i = 0; i < 32; ++i) sorted[i] = (unsigned char)(seed * 11 + i * 73);
for (i = 0; i < 4; ++i) counters[i] = 0;
}
void bench_run(void) {
unsigned i, j, round;
unsigned char value;
for (i = 1; i < 32; ++i) {
value = sorted[i]; j = i;
while (j != 0 && sorted[j - 1] > value) { sorted[j] = sorted[j - 1]; --j; }
sorted[j] = value;
}
for (round = 0; round < 8; ++round)
for (i = 0; i < 32; ++i) {
switch (state) {
case 0: counters[0] += sorted[i]; state = (unsigned char)((sorted[i] + round) & 3); break;
case 1: counters[1] ^= (unsigned)sorted[i] << (round & 3); state = (unsigned char)((i + round) & 3); break;
case 2: counters[2] += i + round; state = sorted[i] & 3; break;
default: counters[3] += sorted[i] ^ round; state = (unsigned char)((state + i) & 3); break;
}
}
}
void bench_collect(void) {
unsigned i;
begin_collect();
for (i = 0; i < 32; ++i) emit(sorted[i]);
for (i = 0; i < 4; ++i) emit_word(counters[i]);
emit(state);
}
#elif BENCH_CASE == 4
static unsigned char pixels[256], tile[64], positions[8];
void bench_setup(unsigned seed) {
unsigned i;
for (i = 0; i < 256; ++i) pixels[i] = (unsigned char)(seed + i * 7);
for (i = 0; i < 64; ++i) tile[i] = (unsigned char)(seed * 3 + i * 11);
for (i = 0; i < 8; ++i) positions[i] = (unsigned char)(seed + i * 23);
}
void bench_run(void) {
unsigned frame, sprite, x, y, left, top;
for (frame = 0; frame < 4; ++frame)
for (sprite = 0; sprite < 8; ++sprite) {
left = positions[sprite] & 15; top = positions[sprite] >> 4;
for (y = 0; y < 8; ++y)
for (x = 0; x < 8; ++x)
pixels[(((top + y) & 15) << 4) | ((left + x) & 15)] ^= tile[y * 8 + x];
positions[sprite] = (unsigned char)(positions[sprite] + sprite + frame + 1);
}
}
void bench_collect(void) {
unsigned i;
begin_collect();
for (i = 0; i < 256; ++i) emit(pixels[i]);
for (i = 0; i < 8; ++i) emit(positions[i]);
}
#elif BENCH_CASE == 5
static unsigned phases[4], steps[4];
static unsigned char samples[128];
void bench_setup(unsigned seed) {
unsigned i;
for (i = 0; i < 4; ++i) { phases[i] = seed * 113 + i * 997; steps[i] = 101 + (seed & 255) + i * 173; }
for (i = 0; i < 128; ++i) samples[i] = 0;
}
void bench_run(void) {
unsigned n, channel, mix, value;
for (n = 0; n < 128; ++n) {
mix = 0;
for (channel = 0; channel < 4; ++channel) {
phases[channel] += steps[channel];
value = phases[channel] >> 8;
mix += value < 128 ? value * 2u : (255u - value) * 2u;
}
samples[n] = (unsigned char)(mix >> 2);
}
}
void bench_collect(void) {
unsigned i;
begin_collect();
for (i = 0; i < 128; ++i) emit(samples[i]);
for (i = 0; i < 4; ++i) emit_word(phases[i]);
}
#elif BENCH_CASE == 6
static unsigned char axes[8], centers[8], history[32], buttons;
static unsigned totals[2];
static void capture(unsigned char x, unsigned char y, unsigned char cx, unsigned char cy) {
unsigned dx = x > cx ? x - cx : cx - x;
unsigned dy = y > cy ? y - cy : cy - y;
if (dx > 3) totals[0] += dx;
if (dy > 3) totals[1] += dy;
}
void bench_setup(unsigned seed) {
unsigned i;
for (i = 0; i < 8; ++i) { axes[i] = (unsigned char)(seed + i * 13); centers[i] = (unsigned char)(96 + (seed & 31) + i * 3); }
for (i = 0; i < 32; ++i) history[i] = 0;
totals[0] = totals[1] = 0; buttons = (unsigned char)seed;
}
void bench_run(void) {
unsigned n, index;
for (n = 0; n < 32; ++n) {
index = (n & 3) * 2;
capture(axes[index], axes[index + 1], centers[index], centers[index + 1]);
axes[index] = (unsigned char)(axes[index] + n + 1); axes[index + 1] = (unsigned char)(axes[index + 1] + n + 3);
buttons = (buttons << 1) | (buttons >> 7);
history[n] = buttons ^ axes[index] ^ axes[index + 1];
}
}
void bench_collect(void) {
unsigned i;
begin_collect();
for (i = 0; i < 8; ++i) { emit(axes[i]); emit(centers[i]); }
for (i = 0; i < 32; ++i) emit(history[i]);
emit_word(totals[0]); emit_word(totals[1]); emit(buttons);
}
#elif BENCH_CASE == 7
/* Eight synthetic DECB-style 32-byte entries; no filesystem or disk calls. */
static unsigned char sector[256];
static unsigned active, last_bytes, name_hash, granule_sum;
void bench_setup(unsigned seed) {
unsigned i, entry, base;
for (i = 0; i < 256; ++i) sector[i] = 0;
for (entry = 0; entry < 8; ++entry) {
base = entry * 32;
for (i = 0; i < 11; ++i) sector[base + i] = (unsigned char)('A' + ((seed + entry * 7 + i) % 26));
sector[base + 11] = (unsigned char)(entry & 3); sector[base + 12] = (unsigned char)((seed + entry * 9) % 68);
sector[base + 13] = (entry & 1) ? 255 : 0;
i = 1 + ((seed + entry * 17) & 255);
sector[base + 14] = (unsigned char)(i >> 8); sector[base + 15] = (unsigned char)i;
}
sector[32] = 0; sector[160] = 255;
active = last_bytes = name_hash = granule_sum = 0;
}
void bench_run(void) {
unsigned round, entry, i;
const unsigned char *p;
for (round = 0; round < 16; ++round)
for (entry = 0; entry < 8; ++entry) {
p = sector + entry * 32;
if (p[0] == 0 || p[0] == 255) continue;
++active; last_bytes += ((unsigned)p[14] << 8) | p[15]; granule_sum += p[12];
for (i = 0; i < 11; ++i) name_hash = (name_hash << 1) ^ p[i];
}
}
void bench_collect(void) {
unsigned i;
begin_collect();
for (i = 0; i < 256; ++i) emit(sector[i]);
emit_word(active); emit_word(last_bytes); emit_word(name_hash); emit_word(granule_sum);
}
#else
#error BENCH_CASE must be from 0 through 7
#endif
Evidence record
experiments/cmoc-benchmarks/workloads.c
From Windows Application to Portable Emulation Platform
10,102 bytes
SHA-256115a3eef8246a49dc94e7978dc709379468a2376fb87f1ca929b07699834406d