// delay.hpp
#if defined(__GNUC__) && defined(i386)
#include <stddef.h>
//#elif defined(WIN32) || defined(_WIN32)
//#include <windows.h>
#else
#include <time.h>
#endif

void delay(size_t ns)
{
#if defined(__GNUC__) && defined(i386)         // 精度为纳秒, gcc编译器
  size_t clk, now;
  asm volatile("rdtsc":"=a"(clk)::"edx");
  do
  {
    asm volatile("rep;nop":::"memory");
    asm volatile("rdtsc":"=a"(now)::"edx");
  }while(now - clk < ns);
//#elif defined(_WIN32) || defined(WIN32)        // 精度为毫秒, Win32系列的系统
  //Sleep(DWORD(ns / 1000000));
#elif defined (LINUX) || defined (__FreeBSD__) // 精度为纳秒, 符合POSIX的系统
  struct timespec req = {0, ns};
  struct timespec rem, * a = &req, * b = &rem;
  while(clock_nanosleep(CLOCK_REALTIME, 0, a, b) && EINTR = errno)
  {
    struct timespec* tmp = a;
    a = b;
    b = tmp;
  }
#else
  for(size_t i = 0; i < ns; ++i) {}
#endif
}
