
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include "utils.h"

BOOL SupportHPQ()
{
	LARGE_INTEGER fr;
	
    if(QueryPerformanceFrequency(&fr) == FALSE)
        return FALSE;

	return TRUE;
}

BOOL STTI(TimeInterval * t)
{
	ZeroMemory(t,sizeof(TimeInterval));
	
    if(QueryPerformanceFrequency(&(t->fr)) == FALSE)
        return FALSE;

	if(QueryPerformanceCounter(&(t->t1)) == FALSE)
        return FALSE;

	return TRUE;
}

BOOL SPTI(TimeInterval * t)
{
	LONGLONG i,i2;
	DWORD n,nfr,n3,n2,n1;
	
    if(QueryPerformanceCounter(&(t->t2)) == FALSE)
    {
		t->mks = 0;
		sprintf(t->str,"Not supported");
		return FALSE;
	}

	i = t->t1.QuadPart;
	i2 = t->t2.QuadPart;
	i2 = i2-i;
	if(i2 > 0xffffffff)
    {
		sprintf(t->str,"To big interval");
		t->mks = 0xffffffff;
		return TRUE;
	}

	i = t->fr.QuadPart;
	n = (DWORD)i2;
	nfr = (DWORD)i;
	n3 = n/nfr;
	t->mks = n3*1000000;
	n2 = n3*nfr;
	n-=n2;
	n*=1000;
	n2 = n/nfr;
	t->mks+=(n2*1000);
	n1 = n2*nfr;
	n-=n1;
	n*=1000;
	n1 = n/nfr;
	t->mks+=(n1);
	sprintf(t->str,"%d sec %d mls %d mcs",n3,n2,n1);

	return TRUE;
}

BOOL StartTICounter(TimeIntervalCounter * t) // Start
{
    ZeroMemory(t, sizeof(TimeIntervalCounter));
	
    if(QueryPerformanceFrequency(&(t->fr)) == FALSE)
        return FALSE;

	if(QueryPerformanceCounter(&(t->t1)) == FALSE)
        return FALSE;

	return TRUE;
}

BOOL PauseTICounter(TimeIntervalCounter * t) // Start
{
    if(QueryPerformanceCounter(&(t->t2)) == FALSE)
    {
		return FALSE;
	}

	t->total += (t->t2.QuadPart - t->t1.QuadPart);

    return TRUE;
}

BOOL ResumeTICounter(TimeIntervalCounter * t) // Start
{
    if(QueryPerformanceCounter(&(t->t1)) == FALSE)
        return FALSE;

    return TRUE;
}

BOOL StopTICounter(TimeIntervalCounter * t) // Stop
{
  	DWORD n,nfr,n3,n2,n1;
    LONGLONG i;
	
    if(QueryPerformanceCounter(&(t->t2)) == FALSE)
    {
		t->mks = 0;
		sprintf(t->str,"Not supported");
		return FALSE;
	}

	t->total += (t->t2.QuadPart - t->t1.QuadPart);

	if(t->total > 0xffffffff)
    {
		sprintf(t->str,"To big interval");
		t->mks = 0xffffffff;
		return TRUE;
	}

	i = t->fr.QuadPart;
	n = (DWORD)t->total;
	nfr = (DWORD)i;
	n3 = n/nfr;
	t->mks = n3*1000000;
	n2 = n3*nfr;
	n-=n2;
	n*=1000;
	n2 = n/nfr;
	t->mks+=(n2*1000);
	n1 = n2*nfr;
	n-=n1;
	n*=1000;
	n1 = n/nfr;
	t->mks+=(n1);
	sprintf(t->str,"%d sec %d mls %d mcs",n3,n2,n1);

    return TRUE;
}
