/*
 * benchmark.c - OpenVMS on QEMU TCG performance benchmark
 *
 * Tests: integer, floating point, memory, string, and I/O performance.
 * Build on VMS:  CC benchmark.c
 *                LINK benchmark
 *                RUN benchmark
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>

#define ITERATIONS 10000000

static double elapsed(clock_t start, clock_t end)
{
    return (double)(end - start) / CLOCKS_PER_SEC;
}

/* Integer arithmetic: add, multiply, divide, modulo, bit ops */
static void bench_integer(void)
{
    volatile long a = 17, b = 31, c = 0;
    clock_t t0 = clock();
    int i;

    for (i = 0; i < ITERATIONS; i++) {
        c = a + b;
        c = a * b;
        c = (a * 1000007) / (b + 1);
        c = (a * 999983) % (b + 3);
        c = (a ^ b) & 0x7FFFFFFF;
        a = c + 13;
        b = c ^ 37;
        if (b == 0) b = 1;
    }

    printf("  Integer arithmetic : %8.3f sec  (%d M iterations)\n",
           elapsed(t0, clock()), ITERATIONS / 1000000);
}

/* Floating point: trig, sqrt, log, exp */
static void bench_float(void)
{
    volatile double x = 1.0, y = 0.0;
    clock_t t0 = clock();
    int i;

    for (i = 0; i < ITERATIONS; i++) {
        y = sin(x) * cos(x);
        y += sqrt(x + 1.0);
        y += log(x + 1.0);
        y += exp(x * 0.0000001);
        x = fabs(y) + 0.001;
    }

    printf("  Floating point     : %8.3f sec  (%d M iterations)\n",
           elapsed(t0, clock()), ITERATIONS / 1000000);
}

/* Memory bandwidth: sequential write + read of a large buffer */
static void bench_memory(void)
{
    int size = 4 * 1024 * 1024; /* 4 MB */
    int passes = 20;
    volatile long *buf;
    volatile long sink = 0;
    clock_t t0;
    int p, i, count;

    buf = (volatile long *)malloc(size);
    if (!buf) {
        printf("  Memory bandwidth   : FAILED (malloc)\n");
        return;
    }

    count = size / sizeof(long);
    t0 = clock();

    for (p = 0; p < passes; p++) {
        for (i = 0; i < count; i++)
            buf[i] = i;
        for (i = 0; i < count; i++)
            sink += buf[i];
    }

    printf("  Memory bandwidth   : %8.3f sec  (%d MB x %d passes = %d MB)\n",
           elapsed(t0, clock()),
           size / (1024 * 1024), passes * 2,
           (size / (1024 * 1024)) * passes * 2);

    free((void *)buf);
}

/* String operations: memcpy, memset, strcmp, strlen */
static void bench_string(void)
{
    int bufsize = 8192;
    char *a, *b;
    volatile int sink = 0;
    clock_t t0;
    int i;
    int iters = ITERATIONS / 10;

    a = (char *)malloc(bufsize);
    b = (char *)malloc(bufsize);
    if (!a || !b) {
        printf("  String operations  : FAILED (malloc)\n");
        return;
    }

    memset(a, 'A', bufsize - 1);
    a[bufsize - 1] = '\0';

    t0 = clock();

    for (i = 0; i < iters; i++) {
        memcpy(b, a, bufsize);
        memset(b, 'B', bufsize / 2);
        sink += strcmp(a, b);
        sink += strlen(b);
    }

    printf("  String operations  : %8.3f sec  (%d K iterations, %d KB buffers)\n",
           elapsed(t0, clock()), iters / 1000, bufsize / 1024);

    free(a);
    free(b);
}

/* Sieve of Eratosthenes - mixed integer + memory access pattern */
static void bench_sieve(void)
{
    int limit = 2000000;
    int passes = 10;
    char *sieve;
    volatile int count = 0;
    clock_t t0;
    int p, i, j;

    sieve = (char *)malloc(limit);
    if (!sieve) {
        printf("  Sieve (primes)     : FAILED (malloc)\n");
        return;
    }

    t0 = clock();

    for (p = 0; p < passes; p++) {
        memset(sieve, 1, limit);
        sieve[0] = sieve[1] = 0;
        count = 0;
        for (i = 2; i * i < limit; i++) {
            if (sieve[i]) {
                for (j = i * i; j < limit; j += i)
                    sieve[j] = 0;
            }
        }
        for (i = 0; i < limit; i++)
            if (sieve[i]) count++;
    }

    printf("  Sieve (primes)     : %8.3f sec  (%d passes, %d primes < %dM)\n",
           elapsed(t0, clock()), passes, count, limit / 1000000);

    free(sieve);
}

/* File I/O: write and read back a temporary file */
static void bench_fileio(void)
{
    char *fname = "benchmark_tmp.dat";
    int blocksize = 4096;
    int blocks = 1000;
    char *buf;
    FILE *f;
    clock_t t0;
    int i;
    long total;

    buf = (char *)malloc(blocksize);
    if (!buf) {
        printf("  File I/O           : FAILED (malloc)\n");
        return;
    }
    memset(buf, 'X', blocksize);
    total = (long)blocksize * blocks;

    t0 = clock();

    f = fopen(fname, "w");
    if (!f) {
        printf("  File I/O           : FAILED (fopen write)\n");
        free(buf);
        return;
    }
    for (i = 0; i < blocks; i++)
        fwrite(buf, 1, blocksize, f);
    fclose(f);

    f = fopen(fname, "r");
    if (!f) {
        printf("  File I/O           : FAILED (fopen read)\n");
        free(buf);
        return;
    }
    for (i = 0; i < blocks; i++)
        fread(buf, 1, blocksize, f);
    fclose(f);

    printf("  File I/O           : %8.3f sec  (write+read %ld KB, %d KB blocks)\n",
           elapsed(t0, clock()), total / 1024, blocksize / 1024);

    remove(fname);
    free(buf);
}

int main(void)
{
    clock_t total_start, total_end;

    printf("\n");
    printf("==========================================================\n");
    printf("  OpenVMS QEMU TCG Benchmark\n");
    printf("  Host: Apple Silicon (arm64) / QEMU max-insns-per-tb=1\n");
    printf("==========================================================\n\n");

    total_start = clock();

    bench_integer();
    bench_float();
    bench_memory();
    bench_string();
    bench_sieve();
    bench_fileio();

    total_end = clock();

    printf("\n  --------------------------------------------------------\n");
    printf("  TOTAL              : %8.3f sec\n", elapsed(total_start, total_end));
    printf("  --------------------------------------------------------\n\n");

    return 0;
}
