#include <9pm/windows.h>
#include <9pm/u.h>
#include <9pm/libc.h>
#include <9pm/libsec.h>
#include "dat.h"
#include "fns.h"

static double fasttick;
void
todinit(void)
{
	LARGE_INTEGER fi;

	if(QueryPerformanceFrequency(&fi) && fi.QuadPart != 0)
		fasttick = 1.e9/fi.QuadPart;
}

long
seconds(void)
{
	uvlong t;
	FILETIME ft;

	GetSystemTimeAsFileTime(&ft);
	t = ((uvlong)ft.dwLowDateTime) + (((uvlong)ft.dwHighDateTime)<<32);
	t -= (vlong)10000000*134774*24*60*60;
	t /= 10000;
	return t;
}

vlong
nsec(void)
{
	LARGE_INTEGER ti;
	FILETIME ft;
	uvlong t;

	GetSystemTimeAsFileTime(&ft);
	t = ((uvlong)ft.dwLowDateTime) + (((uvlong)ft.dwHighDateTime)<<32);
	t -= (vlong)10000000*134774*24*60*60;
	t *= 100;

	/*
	 * RSC: I don't know where this came from.  Is it worth it?
	 */
	if(fasttick != 0 && QueryPerformanceCounter(&ti)) {
		t += (uvlong)(((double)ti.QuadPart)*fasttick);
		t -= ((uvlong)GetTickCount())*1000000;
	}

	return t;
}

void
delay(int ms)
{
	Sleep(ms);
}
