/* Copyright (C) 2002 Sony Computer Entertainment America */
/*        All Rights Reserved.  SCEA Confidential         */

#ifndef LIBIMAGE_UTIL_H
#define LIBIMAGE_UTIL_H

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <eetypes.h>
#include <eeregs.h> // for timer code

#ifdef __cplusplus
extern "C" {
#endif

  // these are wrappers of memalign() and free()
  // re-implement them if you wish to use your own memory management
void* mymemalign(int alignment, int size);
void myfree(void* data);

#ifdef _DEBUG
#undef ERROR
#define ERROR printf
#undef LOG
#define LOG printf
#else
#undef LOG
#define LOG
#undef ERROR
#define ERROR
#endif

// math
#define RAD2DEG (180.0f/M_PI)
#define DEG2RAD (M_PI/180.0f)

#define myabs(x) (((x)>0) ? (x):(-(x)))
#define max(a,b) (((a > (b)) ? (a) : (b)))
#define min(a,b) (((a < (b)) ? (a) : (b)))
#define limit(x,a,b) (x)=min(max((a),(x)),(b))

#define UNCACHED(x) ((u_int)(x)|0x20000000)
#define UNCACHED_ACCEL(x) ((u_int)(x)|0x30000000)
#define _prefetch0(addr) asm volatile ("pref 0, 0(%0)": :"r"(addr))
#define _prefetch64(addr) asm volatile ("pref 0, 64(%0)": :"r"(addr))
#define _prefetch128(addr) asm volatile ("pref 0, 128(%0)": :"r"(addr))
#define _prefetch768(addr) asm volatile ("pref 0, 768(%0)": :"r"(addr))

  // generic prefetch does not work for CW???
#define _prefetch(offset, addr) asm volatile ("pref 0, %1(%0)": :"r"(addr),"I"(offset))

// fast copy
u_long128* quadmemset(u_long128* dest, u_long128 val, int quadcount);
u_long128* quadmemcpy(u_long128* dest, u_long128* src, int quadcount);
// fast packing functions
u_long128 packquadw(u_int val);
u_long128 packquadh(u_short val);
u_long128 packquadb(u_char val);

// timer
#define timerTicksPerFrame 9500

typedef struct {
  tT_MODE t0Mode;
  tT_COUNT t0Count;
} EETimer;

void timerInit(EETimer* t);
void timerStart(EETimer* t);
int timerStop(EETimer* t);

// kernel
int CreateSimpleSema();
int StartSimpleThread( void *entry, int priority);

int readFile(const char *filename, unsigned char **data, int *size);


#ifdef __cplusplus
}
#endif

#endif // LIBIMAGE_UTIL_H
