This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: modify gcc to handle byteorder issue automatically
- From: Theodore Papadopoulo <Theodore dot Papadopoulo at sophia dot inria dot fr>
- To: Gabriel Dos Reis <gdr at integrable-solutions dot net>
- Cc: Georgy Yunayev <Georgy dot Yunayev at kaspersky dot com>, gcc at gcc dot gnu dot org
- Date: Mon, 21 Jul 2003 15:30:48 +0200
- Subject: Re: modify gcc to handle byteorder issue automatically
I'm using something like the following...
Very crude but may serve as a basis.
The Little/Big endian detection is done by autoconf (file config.h).
#ifndef CPU_H
#define CPU_H
#ifdef HAVE_CONFIG_H
#include <config.h>
#ifndef WORDS_BIGENDIAN
#define WORDS_LITTLEENDIAN
#endif
#endif
#include <algorithm>
namespace Cpu {
typedef enum { BigEndian, LittleEndian } Endianness;
inline void
ChangeEndianness16(char* mem) {
std::swap(mem[0],mem[1]);
}
void
ChangeEndianness16(char* mem,const char* end) {
for (;mem<end;mem+=2)
Cpu::ChangeEndianness16(mem);
}
inline void
ChangeEndianness32(char* mem) {
std::swap(mem[0],mem[3]);
std::swap(mem[1],mem[2]);
}
void
ChangeEndianness32(char* mem,const char* end) {
for (;mem<end;mem+=4)
Cpu::ChangeEndianness32(mem);
}
# if (defined(WORDS_BIGENDIAN) && defined(WORDS_LITTLEENDIAN)) || \
!(defined(WORDS_BIGENDIAN) || defined(WORDS_LITTLEENDIAN))
# error exactly one of WORDS_BIGENDIAN or WORDS_LITTLEENDIAN must be specified.
# endif
# ifdef WORDS_BIGENDIAN
const Cpu::Endianness ENDIANNESS = Cpu::BigEndian;
# endif
# ifdef WORDS_LITTLEENDIAN
const Cpu::Endianness ENDIANNESS = Cpu::LittleEndian;
# endif
}
#endif // !CPU_H
--------------------------------------------------------------------
Theodore Papadopoulo
Email: Theodore.Papadopoulo@sophia.inria.fr Tel: (33) 04 92 38 76 01
--------------------------------------------------------------------