#include <9pm/windows.h>
#include <9pm/u.h>
#include <9pm/libc.h>

static double fasttick;
static int init;
static int nofasttick;

vlong
nsec(void)
{
	LARGE_INTEGER ti, fi;
	FILETIME ft;
	uvlong t;
	
	if(!init){
		init = 1;
		if(QueryPerformanceFrequency(&fi) && fi.QuadPart != 0)
			fasttick = 1.e9/fi.QuadPart;
		else
			nofasttick = 1;
	}

	GetSystemTimeAsFileTime(&ft);
	t = (((uvlong)ft.dwLowDateTime) + (((uvlong)ft.dwHighDateTime)<<32))*100;

	if(!nofasttick && QueryPerformanceCounter(&ti)) {
		t += (uvlong)(((double)ti.QuadPart)*fasttick);
		t -= ((uvlong)GetTickCount())*1000000;
	}

	return t;
}
