#include "eclipseclass.h"
#include <stdio.h>
/* #include <iostream> */
#include <stdint.h>

// example
/* extern "C" int */
/* p_sumlist() */
/* { */
/*     int res; */
/*     long x, sum = 0; */
/*     EC_word list(EC_arg(1)); */
/*     EC_word car,cdr; */

/*     for ( ; list.is_list(car,cdr) == EC_succeed; list = cdr) */
/*     { */
/*         res = car.is_long(&x); */
/*         if (res != EC_succeed) return res; */
/*         sum += x; */
/*     } */
/*     res = list.is_nil(); */
/*     if (res != EC_succeed) return res; */
/*     return unify(EC_arg(2), EC_word(sum)); */
/* } */

extern "C" {
   __inline__ uint64_t rdtsc() {
   uint32_t lo, hi;
   /* We cannot use "=A", since this would use %rax on x86_64 */
   __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
   return (uint64_t)hi << 32 | lo;
   }
}

/* extern "C" { */
/*    __inline__ uint32_t rdtscl() { */
/*    uint32_t lo, hi; */
/*    /\* We cannot use "=A", since this would use %rax on x86_64 *\/ */
/*    __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi)); */
/*    return lo; */
/*    } */
/* } */

extern "C" int p_clock() 
{
  unsigned long long ticks;
  ticks = rdtsc();
  long long hi, lo;
  hi = ticks >> 32;
  lo = ticks ^ (hi << 32);
/*   std::cout << ticks << ", " << hi << ", " << lo << "\n"; */
  return (unify(EC_arg(1), EC_word((long)hi)) 
	  & unify(EC_arg(2), EC_word((long)lo)));
}


