blob: cdf89b38618af4ce344abe8d99dcc02ea7ebd71d [file] [log] [blame]
/*
* Utility routines
*
* (C) Copyright 2001-2007 Geert Uytterhoeven
*
* This file is subject to the terms and conditions of the GNU General Public
* License. See the file COPYING in the main directory of this archive for
* more details.
*/
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#include "types.h"
#include "fb.h"
#include "util.h"
#define TXT_MESSAGE TXT_GREEN
#define TXT_WARNING TXT_YELLOW
#define TXT_ERROR TXT_RED
#define TXT_DEBUG TXT_BLUE
#define TXT_FATAL TXT_RED
/*
* Print a prefixed message
*/
static void PrintMessage(const char *prefix, const char *fmt, va_list ap)
{
fflush(stdout);
fputs(prefix, stderr);
vfprintf(stderr, fmt, ap);
fputs(TXT_NORMAL, stderr);
}
/*
* Print a message
*/
void Message(const char *fmt, ...)
{
va_list ap;
if (!Opt_Quiet) {
va_start(ap, fmt);
PrintMessage(TXT_MESSAGE, fmt, ap);
va_end(ap);
}
}
/*
* Print a warning message
*/
void Warning(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
PrintMessage(TXT_WARNING, fmt, ap);
va_end(ap);
}
/*
* Print an error message
*/
void Error(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
PrintMessage(TXT_ERROR, fmt, ap);
va_end(ap);
}
/*
* Print a debug message
*/
void Debug(const char *fmt, ...)
{
va_list ap;
if (Opt_Debug) {
va_start(ap, fmt);
PrintMessage(TXT_DEBUG, fmt, ap);
va_end(ap);
}
}
/*
* Print a fatal error message
*/
void Fatal(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
PrintMessage(TXT_FATAL, fmt, ap);
va_end(ap);
fb_cleanup();
exit(1);
}
/*
* Wait for a keypress
*/
void wait_for_key(int timeout)
{
/* FIXME: no keypress handling yet */
sleep(2);
}
/*
* Wait some milliseconds
*/
void wait_ms(int ms)
{
struct timespec req;
req.tv_sec = ms/1000;
req.tv_nsec = (ms % 1000)*1000000;
nanosleep(&req, NULL);
}
/*
* Benchmark a routine
*/
static uint64_t get_ticks(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (uint64_t)tv.tv_sec*1000000 + tv.tv_usec;
}
double benchmark(void (*func)(unsigned long n, void *data), void *data)
{
uint64_t ticks;
unsigned long n = 1;
printf("Benchmarking... ");
while (n <<= 1) {
ticks = get_ticks();
func(n, data);
ticks = get_ticks() - ticks;
if (ticks >= 500000)
break;
}
if (!n) {
printf("CPU too fast :-)\n");
return -1;
}
return 1e6*n/ticks;
}