Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Examples

pbyteorder.h

Go to the documentation of this file.
00001 /*
00002  *   P::Classes - Portable C++ Application Framework
00003  *   Copyright (C) 2000-2004  Christian Prochnow <cproch@seculogix.de>
00004  *
00005  *   This library is free software; you can redistribute it and/or
00006  *   modify it under the terms of the GNU Lesser General Public
00007  *   License as published by the Free Software Foundation; either
00008  *   version 2 of the License, or (at your option) any later version.
00009  *
00010  *   This library is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *   Lesser General Public License for more details.
00014  *
00015  *   You should have received a copy of the GNU Lesser General Public
00016  *   License along with this library; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 #ifndef _pbyteorder_h_
00021 #define _pbyteorder_h_
00022 
00023 #include <pclasses/config.h>
00024 #include <pclasses/pexport.h>
00025 
00026 namespace P {
00027 
00028 inline uint16_t swab(uint16_t __x)
00029 {
00030   return ((uint16_t)(
00031          (((uint16_t)(__x) & (uint16_t)0x00ff) << 8) |
00032          (((uint16_t)(__x) & (uint16_t)0xff00) >> 8) ));
00033 }
00034 
00035 inline uint32_t swab(uint32_t __x)
00036 {
00037   return ((uint32_t)(
00038           (((uint32_t)(__x) & (uint32_t)0x000000ff) << 24) |
00039           (((uint32_t)(__x) & (uint32_t)0x0000ff00) <<  8) |
00040           (((uint32_t)(__x) & (uint32_t)0x00ff0000) >>  8) |
00041           (((uint32_t)(__x) & (uint32_t)0xff000000) >> 24) ));
00042 }
00043 
00044 inline uint64_t swab(uint64_t __x)
00045 {
00046   return ((uint64_t)(
00047           (uint64_t)(((uint64_t)(__x) & (uint64_t)P_U64BIT_CONSTANT(0x00000000000000ff)) << 56) |
00048           (uint64_t)(((uint64_t)(__x) & (uint64_t)P_U64BIT_CONSTANT(0x000000000000ff00)) << 40) |
00049           (uint64_t)(((uint64_t)(__x) & (uint64_t)P_U64BIT_CONSTANT(0x0000000000ff0000)) << 24) |
00050           (uint64_t)(((uint64_t)(__x) & (uint64_t)P_U64BIT_CONSTANT(0x00000000ff000000)) <<  8) |
00051           (uint64_t)(((uint64_t)(__x) & (uint64_t)P_U64BIT_CONSTANT(0x000000ff00000000)) >>  8) |
00052           (uint64_t)(((uint64_t)(__x) & (uint64_t)P_U64BIT_CONSTANT(0x0000ff0000000000)) >> 24) |
00053           (uint64_t)(((uint64_t)(__x) & (uint64_t)P_U64BIT_CONSTANT(0x00ff000000000000)) >> 40) |
00054           (uint64_t)(((uint64_t)(__x) & (uint64_t)P_U64BIT_CONSTANT(0xff00000000000000)) >> 56) ));
00055 }
00056 
00057 inline uint16_t cpu_to_le(uint16_t word)
00058 {
00059   #ifdef WORDS_BIGENDIAN
00060   return swab(word);
00061   #else
00062   return word;
00063   #endif
00064 }
00065 
00066 inline uint16_t le_to_cpu(uint16_t word)
00067 {
00068   #ifdef WORDS_BIGENDIAN
00069   return swab(word);
00070   #else
00071   return word;
00072   #endif
00073 }
00074 
00075 inline uint32_t cpu_to_le(uint32_t dword)
00076 {
00077   #ifdef WORDS_BIGENDIAN
00078   return swab(dword);
00079   #else
00080   return dword;
00081   #endif
00082 }
00083 
00084 inline uint32_t le_to_cpu(uint32_t dword)
00085 {
00086   #ifdef WORDS_BIGENDIAN
00087   return swab(dword);
00088   #else
00089   return dword;
00090   #endif
00091 }
00092 
00093 inline uint64_t cpu_to_le(uint64_t qword)
00094 {
00095   #ifdef WORDS_BIGENDIAN
00096   return swab(qword);
00097   #else
00098   return qword;
00099   #endif
00100 }
00101 
00102 inline uint64_t le_to_cpu(uint64_t qword)
00103 {
00104   #ifdef WORDS_BIGENDIAN
00105   return swab(qword);
00106   #else
00107   return qword;
00108   #endif
00109 }
00110 
00111 inline uint16_t cpu_to_be(uint16_t word)
00112 {
00113   #ifdef WORDS_BIGENDIAN
00114   return word;
00115   #else
00116   return swab(word);
00117   #endif
00118 }
00119 
00120 inline uint16_t be_to_cpu(uint16_t word)
00121 {
00122   #ifdef WORDS_BIGENDIAN
00123   return word;
00124   #else
00125   return swab(word);
00126   #endif
00127 }
00128 
00129 inline uint32_t cpu_to_be(uint32_t dword)
00130 {
00131   #ifdef WORDS_BIGENDIAN
00132   return dword;
00133   #else
00134   return swab(dword);
00135   #endif
00136 }
00137 
00138 inline uint32_t be_to_cpu(uint32_t dword)
00139 {
00140   #ifdef WORDS_BIGENDIAN
00141   return dword;
00142   #else
00143   return swab(dword);
00144   #endif
00145 }
00146 
00147 inline uint64_t cpu_to_be(uint64_t qword)
00148 {
00149   #ifdef WORDS_BIGENDIAN
00150   return qword;
00151   #else
00152   return swab(qword);
00153   #endif
00154 }
00155 
00156 inline uint64_t be_to_cpu(uint64_t qword)
00157 {
00158   #ifdef WORDS_BIGENDIAN
00159   return qword;
00160   #else
00161   return swab(qword);
00162   #endif
00163 }
00164 
00165 }
00166 
00167 #endif

Generated on Fri Mar 12 21:08:30 2004 for P::Classes by doxygen 1.3.3