fceugx/source/fceultra/utils/endian.h

112 lines
2.8 KiB
C
Raw Normal View History

2009-07-17 19:27:04 +02:00
#ifndef __FCEU_ENDIAN
#define __FCEU_ENDIAN
#include <iosfwd>
#include <stdio.h>
2010-08-29 23:15:42 +02:00
#include "../types.h"
2010-12-06 03:46:57 +01:00
#include "../emufile.h"
class EMUFILE;
2010-08-29 23:15:42 +02:00
inline uint64 double_to_u64(double d) {
union {
uint64 a;
double b;
} fuxor;
fuxor.b = d;
return fuxor.a;
}
inline double u64_to_double(uint64 u) {
union {
uint64 a;
double b;
} fuxor;
fuxor.a = u;
return fuxor.b;
}
inline uint32 float_to_u32(float f) {
union {
uint32 a;
float b;
} fuxor;
fuxor.b = f;
return fuxor.a;
}
inline float u32_to_float(uint32 u) {
union {
uint32 a;
float b;
} fuxor;
fuxor.a = u;
return fuxor.b;
}
2009-07-17 19:27:04 +02:00
int write16le(uint16 b, FILE *fp);
int write32le(uint32 b, FILE *fp);
int write32le(uint32 b, std::ostream* os);
int write64le(uint64 b, std::ostream* os);
int read64le(uint64 *Bufo, std::istream *is);
int read32le(uint32 *Bufo, std::istream *is);
int read32le(uint32 *Bufo, FILE *fp);
int read16le(uint16 *Bufo, std::istream *is);
void FlipByteOrder(uint8 *src, uint32 count);
void FCEU_en32lsb(uint8 *, uint32);
void FCEU_en16lsb(uint8* buf, uint16 val);
uint64 FCEU_de64lsb(uint8 *morp);
uint32 FCEU_de32lsb(uint8 *morp);
uint16 FCEU_de16lsb(uint8 *morp);
2010-08-29 23:15:42 +02:00
//well. just for the sake of consistency
int write8le(uint8 b, EMUFILE *fp);
inline int write8le(uint8* b, EMUFILE *fp) { return write8le(*b,fp); }
int write16le(uint16 b, EMUFILE* os);
int write32le(uint32 b, EMUFILE* os);
int write64le(uint64 b, EMUFILE* os);
inline int write_double_le(double b, EMUFILE*is) { uint64 temp = double_to_u64(b); int ret = write64le(temp,is); return ret; }
int read8le(uint8 *Bufo, EMUFILE*is);
int read16le(uint16 *Bufo, EMUFILE*is);
2010-12-06 03:46:57 +01:00
inline int read16le(int16 *Bufo, EMUFILE*is) { return read16le((uint16*)Bufo,is); }
2010-08-29 23:15:42 +02:00
int read32le(uint32 *Bufo, EMUFILE*is);
2010-12-06 03:46:57 +01:00
inline int read32le(int32 *Bufo, EMUFILE*is) { return read32le((uint32*)Bufo,is); }
2010-08-29 23:15:42 +02:00
int read64le(uint64 *Bufo, EMUFILE*is);
inline int read_double_le(double *Bufo, EMUFILE*is) { uint64 temp; int ret = read64le(&temp,is); *Bufo = u64_to_double(temp); return ret; }
template<typename T>
int readle(T *Bufo, EMUFILE*is)
{
2016-09-18 05:43:24 +02:00
//CTASSERT(sizeof(T)==1||sizeof(T)==2||sizeof(T)==4||sizeof(T)==8);
2010-08-29 23:15:42 +02:00
switch(sizeof(T)) {
2010-12-06 03:46:57 +01:00
case 1: return read8le((uint8*)Bufo,is);
case 2: return read16le((uint16*)Bufo,is);
case 4: return read32le((uint32*)Bufo,is);
case 8: return read64le((uint64*)Bufo,is);
2010-08-29 23:15:42 +02:00
default:
return 0;
}
}
template<typename T>
int writele(T *Bufo, EMUFILE*os)
{
2016-09-18 05:43:24 +02:00
//CTASSERT(sizeof(T)==1||sizeof(T)==2||sizeof(T)==4||sizeof(T)==8);
2010-08-29 23:15:42 +02:00
switch(sizeof(T)) {
2012-06-27 01:14:39 +02:00
case 1: return write8le(*(uint8*)Bufo,os);
case 2: return write16le(*(uint16*)Bufo,os);
case 4: return write32le(*(uint32*)Bufo,os);
case 8: return write64le(*(uint64*)Bufo,os);
2010-08-29 23:15:42 +02:00
default:
return 0;
}
}
#endif //__FCEU_ENDIAN
2009-07-17 19:27:04 +02:00