byteorder.h

Go to the documentation of this file.
00001 
00002 /*  $Id: byteorder.h 1270 2006-04-16 17:31:13Z andreradke $    */
00003 
00004 /******************************************************************************
00005 
00006     UserLand Frontier(tm) -- High performance Web content management,
00007     object database, system-level and Internet scripting environment,
00008     including source code editing and debugging.
00009 
00010     Copyright (C) 1992-2004 UserLand Software, Inc.
00011 
00012     This program is free software; you can redistribute it and/or modify
00013     it under the terms of the GNU General Public License as published by
00014     the Free Software Foundation; either version 2 of the License, or
00015     (at your option) any later version.
00016 
00017     This program is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020     GNU General Public License for more details.
00021 
00022     You should have received a copy of the GNU General Public License
00023     along with this program; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 
00026 ******************************************************************************/
00027 
00028 #ifndef byteorderinclude
00029 #define byteorderinclude
00030 
00031 /*
00032     2006-04-08 aradke: determine native byte order and define byte swapping macros
00033 */
00034 
00035 #if (defined(WIN32) || defined(__i386) || defined(__i386__)) && !defined(__LITTLE_ENDIAN__)
00036     #define __LITTLE_ENDIAN__   1
00037 #endif
00038 
00039 
00040 #if defined(__LITTLE_ENDIAN__) && ((__LITTLE_ENDIAN__ == 1) || !defined (__BIG_ENDIAN__))
00041 
00042     #define SWAP_BYTE_ORDER 1
00043 
00044     #define conditionallongswap(x) dolongswap(x)
00045     #define conditionalshortswap(x) doshortswap(x)
00046     #define conditionalenumswap(x) doshortswap(x)
00047     #define disklong(x) dolongswap(x)
00048     #define memlong(x) dolongswap(x)
00049     #define diskshort(x) doshortswap(x)
00050     #define memshort(x) doshortswap(x)
00051     #define disktomemshort(x) shortswap(x)
00052     #define disktomemlong(x)  longswap(x)
00053     #define memtodiskshort(x) shortswap(x)
00054     #define memtodisklong(x) longswap(x)
00055 
00056 #elif defined(__BIG_ENDIAN__) && ((__BIG_ENDIAN__ == 1) || !defined (__LITTLE_ENDIAN__))
00057 
00058     #undef SWAP_BYTE_ORDER
00059 
00060     #define conditionallongswap(x) x
00061     #define conditionalshortswap(x) x
00062     #define conditionalenumswap(x) x
00063     #define disklong(x) x
00064     #define memlong(x) x
00065     #define diskshort(x) x
00066     #define memshort(x) x
00067     #define disktomemshort(x)
00068     #define disktomemlong(x)
00069     #define memtodiskshort(x)
00070     #define memtodisklong(x)
00071 
00072 #else
00073 
00074     #error "Couldn't determine byte order of target architecture, update osincludes.h"
00075 
00076 #endif
00077 
00078 
00079 /*
00080     2006-04-16 aradke: Carbon resource manager resources are in big endian format.
00081         If we are running on an Intel Mac, we need to perform byte order conversion.
00082 */
00083 
00084 #if (defined(TARGET_API_MAC_CARBON) && (TARGET_API_MAC_CARBON == 1) && defined(SWAP_BYTE_ORDER))
00085 
00086     #define SWAP_REZ_BYTE_ORDER 1
00087 
00088     #define reztomemshort(x)    shortswap(x)
00089     #define reztomemlong(x)     longswap(x)
00090     #define reztomemrect(x)     do {shortswap((x).top); shortswap((x).left); shortswap((x).bottom); shortswap((x).right);} while(0)
00091     #define memtorezshort(x)    shortswap(x)
00092     #define memtorezlong(x)     longswap(x)
00093     #define memtorezrect(x)     do {shortswap((x).top); shortswap((x).left); shortswap((x).bottom); shortswap((x).right);} while(0)
00094 
00095 #else
00096 
00097     #undef SWAP_REZ_BYTE_ORDER
00098 
00099     #define reztomemshort(x)
00100     #define reztomemlong(x)
00101     #define reztomemrect(x)
00102     #define memtorezshort(x)
00103     #define memtorezlong(x)
00104     #define memtorezrect(x)
00105 
00106 #endif
00107 
00108 
00109 #define longswap(foo)   do {foo = dolongswap(foo);} while (0)
00110 #define shortswap(foo)  do {foo = doshortswap(foo);} while (0)
00111 
00112 
00113 #if (defined(TARGET_API_MAC_CARBON) && (TARGET_API_MAC_CARBON == 1) && defined(__GNUC__))
00114 
00115     /* using system sdk functions from CFByteOrder.h */
00116 
00117     #define dolongswap(foo)         CFSwapInt32(foo)
00118     #define doshortswap(foo)        CFSwapInt16(foo)
00119     
00120 #elif (defined(__i386__) && defined(__GNUC__))
00121 
00122     /* using AT&T x86 assembly code syntax */
00123 
00124     inline long dolongswap (long foo) {
00125 
00126         __asm__("mov foo,%eax\nbswap %eax\nmov %eax,foo\n");
00127 
00128         return (foo);
00129         } /*dolongswap*/
00130 
00131     inline short doshortswap (short foo) {
00132 
00133         __asm__("mov foo,%ax\n mov %al,%bh\nmov %ah,%bl\nmov %bx,foo");
00134 
00135         return (foo);
00136         } /*doshortswap*/
00137 
00138 #elif defined(WIN32)
00139 
00140     /* using Intel x86 assembly code syntax */
00141     
00142     __inline long dolongswap (long foo) {
00143 
00144         _asm
00145             {
00146             mov eax,foo
00147             bswap eax
00148             mov foo,eax
00149             }
00150 
00151         return (foo);
00152         } /*dolongswap*/
00153 
00154     __inline short doshortswap (short foo) {
00155 
00156         _asm
00157             {
00158             mov ax,foo
00159             mov bh,al
00160             mov bl,ah
00161             mov foo,bx
00162             }
00163         
00164         return (foo);
00165         } /*doshortswap*/
00166 
00167 #else
00168 
00169     /* portable code using only C operators */
00170     
00171     inline long dolongswap (long foo) {
00172 
00173         foo = ((((foo) >> 24) & 0x000000ff)
00174                 | (((foo) & 0x00ff0000) >> 8)
00175                 | (((foo) & 0x0000ff00) << 8)
00176                 | (((foo) & 0x000000ff) << 24));
00177         
00178         return (foo);
00179         } /*dolongswap*/
00180 
00181     inline short doshortswap (short foo) {
00182 
00183         foo = ((((foo) >> 8) & 0x00ff)
00184                 | (((foo) << 8) & 0xff00));
00185 
00186         return (foo);
00187         } /*doshortswap*/
00188 
00189 #endif
00190 
00191 
00192 #endif /* byteorderinclude */

Generated on Wed May 31 18:19:44 2006 for frontierkernel 10.1.10a by  doxygen 1.4.6