Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

/home/jeroen/FOX/fox/fox-1.7.33/include/fxendian.h
00001 /********************************************************************************
00002 *                                                                               *
00003 *                      B y t e   S w a p p i n g   S u p p o r t                *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 2010,2012 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or modify          *
00009 * it under the terms of the GNU Lesser General Public License as published by   *
00010 * the Free Software Foundation; either version 3 of the License, or             *
00011 * (at your option) any later version.                                           *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
00016 * GNU Lesser General Public License for more details.                           *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public License      *
00019 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
00020 ********************************************************************************/
00021 #ifndef FXENDIAN_H
00022 #define FXENDIAN_H
00023 
00024 
00025 namespace FX {
00026 
00027 
00028 // Bit reverse in a byte
00029 inline FXuchar reverse8(FXuchar x){
00030   x=((x<<1)&0xAA) | ((x>>1)&0x55);
00031   x=((x<<2)&0xCC) | ((x>>2)&0x33);
00032   return (x<<4) | (x>>4);
00033   }
00034 
00035 
00036 // Bit reverse in a unsigned short
00037 inline FXushort reverse16(FXushort x){
00038   x=((x<<1)&0xAAAA) | ((x>>1)&0x5555);
00039   x=((x<<2)&0xCCCC) | ((x>>2)&0x3333);
00040   x=((x<<4)&0xF0F0) | ((x>>4)&0x0F0F);
00041   return (x<<8) | (x>>8);
00042   }
00043 
00044 
00045 // Bit reverse in an unsigned integer
00046 inline FXuint reverse32(FXuint x){
00047   x=((x<<1)&0xAAAAAAAA) | ((x>>1)&0x55555555);
00048   x=((x<<2)&0xCCCCCCCC) | ((x>>2)&0x33333333);
00049   x=((x<<4)&0xF0F0F0F0) | ((x>>4)&0x0F0F0F0F);
00050   x=((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
00051   return (x<<16) | (x>>16);
00052   }
00053 
00054 
00055 // Bit reverse in an unsigned long
00056 inline FXulong reverse64(FXulong x){
00057   x=((x<< 1)&FXULONG(0xAAAAAAAAAAAAAAAA)) | ((x>> 1)&FXULONG(0x5555555555555555));
00058   x=((x<< 2)&FXULONG(0xCCCCCCCCCCCCCCCC)) | ((x>> 2)&FXULONG(0x3333333333333333));
00059   x=((x<< 4)&FXULONG(0xF0F0F0F0F0F0F0F0)) | ((x>> 4)&FXULONG(0x0F0F0F0F0F0F0F0F));
00060   x=((x<< 8)&FXULONG(0xFF00FF00FF00FF00)) | ((x>> 8)&FXULONG(0x00FF00FF00FF00FF));
00061   x=((x<<16)&FXULONG(0xFFFF0000FFFF0000)) | ((x>>16)&FXULONG(0x0000FFFF0000FFFF));
00062   return (x<<32) | (x>>32);
00063   }
00064 
00065 
00066 
00067 // Byte swap unsigned short
00068 inline FXushort swap16(FXushort x){
00069 #if (defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)))
00070   __asm__ __volatile__("rorw $8,%0\n\t" : "=r"(x) : "0"(x) : "cc");
00071   return x;
00072 #elif (_MSC_VER >= 1500)
00073   return _byteswap_ushort(x);
00074 #else
00075   return (x>>8) | (x<<8);
00076 #endif
00077   }
00078 
00079 
00080 // Byte swap unsiged int
00081 inline FXuint swap32(FXuint x){
00082 #if (defined(__GNUC__) && ((__GNUC__ >= 5) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3))))
00083   return __builtin_bswap32(x);
00084 #elif (defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)))
00085   __asm__ __volatile__("bswapl %0\n\t" : "=r"(x): "0"(x));
00086   return x;
00087 #elif (_MSC_VER >= 1500)
00088   return _byteswap_ulong(x);
00089 #else
00090   x=((x<<8)&0xFF00FF00)|((x>>8)&0x00FF00FF);
00091   return (x>>16)|(x<<16);
00092 #endif
00093   }
00094 
00095 
00096 // Byte swap unsigned long
00097 inline FXulong swap64(FXulong x){
00098 #if (defined(__GNUC__) && ((__GNUC__ >= 5) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3))))
00099   return __builtin_bswap64(x);
00100 #elif (defined(__GNUC__) && defined(__i386__))
00101   union { struct { FXuint l; FXuint h; } s; FXulong x; } n;
00102   n.x=x;
00103   __asm__ __volatile__("bswapl %0\n\t"
00104                        "bswapl %1\n\t"
00105                        "xchgl %0,%1\n\t" : "=r"(n.s.l), "=r" (n.s.h) : "0"(n.s.l), "1"(n.s.h));
00106   return n.x;
00107 #elif (defined(__GNUC__) && defined(__x86_64__))
00108   __asm__ __volatile__("bswapq %0\n\t" : "=r"(x) : "0"(x));
00109   return x;
00110 #elif (_MSC_VER >= 1500)
00111   return _byteswap_uint64(x);
00112 #else
00113   x=((x<< 8)&FXULONG(0xFF00FF00FF00FF00))|((x>> 8)&FXULONG(0x00FF00FF00FF00FF));
00114   x=((x<<16)&FXULONG(0xFFFF0000FFFF0000))|((x>>16)&FXULONG(0x0000FFFF0000FFFF));
00115   return (x>>32)|(x<<32);
00116 #endif
00117   }
00118 
00119 }
00120 
00121 #endif

Copyright © 1997-2011 Jeroen van der Zijp