/*
    Copyright 2011, Spyros Blanas.
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * CHANGELOG
 *  - changed `unsigned long long' declerations to uint64_t and added include
 *    for <stdint.h>. May 2012, Cagri.
 *
 */

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

#if !defined(__i386__) && !defined(__x86_64__) && !defined(__sparc__)
#warning No supported architecture found -- timers will return junk.
#endif

static __inline__ uint64_t curtick() {
  uint64_t tick;
#if defined(__i386__)
  unsigned long lo, hi;
  __asm__ __volatile__(".byte 0x0f, 0x31" : "=a"(lo), "=d"(hi));
  tick = (uint64_t)hi << 32 | lo;
#elif defined(__x86_64__)
  unsigned long lo, hi;
  __asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi));
  tick = (uint64_t)hi << 32 | lo;
#elif defined(__sparc__)
  __asm__ __volatile__("rd %%tick, %0" : "=r"(tick));
#endif
  return tick;
}

static __inline__ void startTimer(uint64_t *t) { *t = curtick(); }

static __inline__ void stopTimer(uint64_t *t) { *t = curtick() - *t; }

static __inline__ void accTimer(uint64_t *pretimer, uint64_t *acctimer) {
  *acctimer += curtick() - *pretimer;
}

static __inline__ void accTimerMulti(uint64_t *pretimer, uint64_t *acctimer,
                                     int multiplier) {
  *acctimer += (curtick() - *pretimer) * multiplier;
}
#ifdef __cplusplus
}
#endif