/* Collection of code snippets by Arne Vajhøj */
/* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "high_res_timer.h"

void printres(TIMECOUNT_T t1, TIMECOUNT_T t2, int n1, int n2, char *ops)
{
    double xperf;
    xperf = (double)n1 * (double)n2 / ((t2 - t1) * 1.0 / UNITS_PER_SECOND);
    printf("%.4f million %s per second\n", xperf / 1000000, ops);
}

#define NINT 10000
#define NFP 1000
#define NSTR 100
#define N 1000000

void testint(int scale)
{
    int i, j;
    int nintscale, sum;
    TIMECOUNT_T t1, t2;
    nintscale = NINT / scale;
    t1 = GET_TIMECOUNT;
    for(i = 0; i < nintscale; i++)
    {
        sum = i;
        for(j = 0; j < N; j++)
        {
            sum = ((sum + 1) * 2 + 1) / 2;
        }
        if(sum != (i + N))
        {
            printf("Integer test error\n");
            exit(0);
        }
    }
    t2 = GET_TIMECOUNT;
    printres(t1, t2, nintscale, N, "integer operations");
}

void testfp(int scale)
{
    int i, j;
    int nfpscale;
    double sum;
    TIMECOUNT_T t1, t2;
    nfpscale = NFP / scale;
    t1 = GET_TIMECOUNT;
    for(i = 0; i < nfpscale; i++)
    {
        sum = i;
        for(j = 0; j < N; j++)
        {
            sum = ((sum + 1) * 2 + 1) / 2;
        }
        if(fabs(sum - (i + 1.5 * N)) > 1)
        {
            printf("Floating point test error\n");
            exit(0);
        }
    }
    t2 = GET_TIMECOUNT;
    printres(t1, t2, nfpscale, N, "floating point operations");
}

#define ALFA "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define VMS_FACTOR 100

void teststr(int scale)
{
    int i, j;
    int ix, ix1, ix2;
    int nstrscale;
    char s[1000], buf[N+1];
    TIMECOUNT_T t1, t2;
    nstrscale = NSTR / scale;
    t1 = GET_TIMECOUNT;
    for(i = 0; i < nstrscale * VMS_FACTOR; i++)
    {
        strcpy(buf, "");
        for(j = 0; j < N / VMS_FACTOR; j = j + 10)
        {
            strcpy(s, ALFA);
            strcat(s, ALFA);
            ix = (i + j) % strlen(ALFA);
            strncat(buf + j, s + ix, 1);
            strncat(buf + j, s + ix + 1, 2);
            strncat(buf + j, s + ix + 3, 3);
            strncat(buf + j, s + ix + 6 , 4);
        }
        ix1 = (N / VMS_FACTOR) / 3;
        ix2 = 2 * (N / VMS_FACTOR) / 3; 
        if(strlen(buf) != (N / VMS_FACTOR) || buf[ix1] != ALFA[(i + ix1) % strlen(ALFA)] || buf[ix2] != ALFA[(i + ix2) % strlen(ALFA)])
        {
            printf("String test error\n");
            exit(0);
        }
    }
    t2 = GET_TIMECOUNT;
    printres(t1, t2, nstrscale, N / 10, "string operations");
}

#define REP 10

int main(int argc, char *argv[])
{
    int i;
    int scale;
    printf("C:\n");
    if(argc > 1)
    {
        scale = atoi(argv[1]);
    }
    else
    {
        scale = 1;
    }
    for(i = 0; i < REP; i++)
    {
        testint(scale);
    }
    for(i = 0; i < REP; i++)
    {
        testfp(scale);
    }
    for(i = 0; i < REP; i++)
    {
        teststr(scale);
    }
    return 0;
}
