es40_endian.h

Go to the documentation of this file.
00001 /* ES40 emulator.
00002  * Copyright (C) 2007-2008 by the ES40 Emulator Project
00003  *
00004  * WWW    : http://sourceforge.net/projects/es40
00005  * E-mail : camiel@camicom.com
00006  * 
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  * 
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00020  * 
00021  * Although this is not required, the author would appreciate being notified of, 
00022  * and receiving any modifications you may make to the source code that might serve
00023  * the general public.
00024  */
00025 
00061 #if !defined(INCLUDED_ENDIAN_H)
00062 #define INCLUDED_ENDIAN_H
00063 
00064 #if !defined(ES40_LITTLE_ENDIAN) && !defined(ES40_BIG_ENDIAN)
00065 #if defined(_WIN32) || defined(__VMS)
00066 #define ES40_LITTLE_ENDIAN
00067 #else // defined (_WIN32) || defined(__VMS)
00068 #include <sys/param.h>
00069 
00070 #if !defined(__BYTE_ORDER) && defined(BYTE_ORDER)
00071 #define __BYTE_ORDER  BYTE_ORDER
00072 #if !defined(__BIG_ENDIAN) && defined(BIG_ENDIAN)
00073 #define __BIG_ENDIAN  BIG_ENDIAN
00074 #endif
00075 #if !defined(__LITTLE_ENDIAN) && defined(LITTLE_ENDIAN)
00076 #define __LITTLE_ENDIAN LITTLE_ENDIAN
00077 #endif
00078 #endif
00079 #if (defined(__BYTE_ORDER) && (__BYTE_ORDER == __BIG_ENDIAN)) || defined(sparc)
00080 #define ES40_BIG_ENDIAN
00081 #else // assume little endian
00082 #define ES40_LITTLE_ENDIAN
00083 #endif
00084 #endif // defined (_WIN32) || defined(__VMS)
00085 #endif // !defined(ES40_LITTLE_ENDIAN) && !defined(ES40_BIG_ENDIAN)
00086 #define swap_64(x)                                          \
00087     (                                                       \
00088       (((x) & U64(0x00000000000000ff)) << 56) |             \
00089         (((x) & U64(0x000000000000ff00)) << 40) |           \
00090           (((x) & U64(0x0000000000ff0000)) << 24) |         \
00091             (((x) & U64(0x00000000ff000000)) << 8) |        \
00092               (((x) & U64(0x000000ff00000000)) >> 8) |      \
00093                 (((x) & U64(0x0000ff0000000000)) >> 24) |   \
00094                   (((x) & U64(0x00ff000000000000)) >> 40) | \
00095                     (((x) & U64(0xff00000000000000)) >> 56) \
00096     )
00097 #define swap_32(x)                                             \
00098     (                                                          \
00099       (((x) & 0x000000ff) << 24) | (((x) & 0x0000ff00) << 8) | \
00100         (((x) & 0x00ff0000) >> 8) | (((x) & 0xff000000) >> 24) \
00101     )
00102 #define swap_16(x)  ((((x) & 0x00ff) << 8) | (((x) & 0xff00) >> 8))
00103 #define swap_8(x)   ((x) & 0xff)
00104 #if defined(ES40_BIG_ENDIAN)
00105 #define endian_64(x)  swap_64(x)
00106 #define endian_32(x)  swap_32(x)
00107 #define endian_16(x)  swap_16(x)
00108 #define endian_8(x)   swap_8(x)
00109 #else // defined(ES40_BIG_ENDIAN)
00110 #define endian_64(x)  (x)
00111 #define endian_32(x)  ((x) & 0xffffffff)
00112 #define endian_16(x)  ((x) & 0xffff)
00113 #define endian_8(x)   ((x) & 0xff)
00114 #endif // defined(ES40_BIG_ENDIAN)
00115 inline u64 endian_bits(u64 x, int numbits)
00116 {
00117   switch(numbits)
00118   {
00119   case 64:  return endian_64(x);
00120   case 32:  return endian_32(x);
00121   case 16:  return endian_16(x);
00122   case 8:   return endian_8(x);
00123   default:  FAILURE(InvalidArgument, "Weird numbits in endian_bits");
00124   }
00125 }
00126 #endif //INCLUDED_ENDIAN_H

SourceForge.net Logo
Project space on SourceForge.net