CDR_Base.inl

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // $Id: CDR_Base.inl 79641 2007-09-14 09:47:59Z johnnyw $
00004 
00005 #if defined (ACE_HAS_INTRINSIC_BYTESWAP)
00006 // Take advantage of MSVC++ byte swapping compiler intrinsics (found
00007 // in <stdlib.h>).
00008 # pragma intrinsic (_byteswap_ushort, _byteswap_ulong, _byteswap_uint64)
00009 #endif  /* ACE_HAS_INTRINSIC_BYTESWAP */
00010 
00011 #if defined (ACE_HAS_BSWAP_16) || defined (ACE_HAS_BSWAP_32) || defined (ACE_HAS_BSWAP_32)
00012 # include "ace/os_include/os_byteswap.h"
00013 #endif
00014 
00015 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00016 
00017 //
00018 // The ACE_CDR::swap_X and ACE_CDR::swap_X_array routines are broken
00019 // in 5 cases for optimization:
00020 //
00021 // * MSVC++ 7.1 or better
00022 //   => Compiler intrinsics
00023 //
00024 // * AMD64 CPU + gnu g++
00025 //   => gcc amd64 inline assembly.
00026 //
00027 // * x86 Pentium CPU + gnu g++
00028 //   (ACE_HAS_PENTIUM && __GNUG__)
00029 //   => gcc x86 inline assembly.
00030 //
00031 // * x86 Pentium CPU and (_MSC_VER) or BORLAND C++)
00032 //   (ACE_HAS_PENTIUM && ( _MSC_VER || __BORLANDC__ )
00033 //   => MSC x86 inline assembly.
00034 //
00035 // * 64 bit architecture
00036 //   (ACE_SIZEOF_LONG == 8)
00037 //   => shift/masks using 64bit words.
00038 //
00039 // * default
00040 //   (none of the above)
00041 //   => shift/masks using 32bit words.
00042 //
00043 //
00044 // Some things you could find useful to know if you intend to mess
00045 // with this optimizations for swaps:
00046 //
00047 //      * MSVC++ don't assume register values are conserved between
00048 //        statements. So you can clobber any register you want,
00049 //        whenever you want (well not *anyone* really, see manual).
00050 //        The MSVC++ optimizer will try to pick different registers
00051 //        for the C++ statements sorrounding your asm block, and if
00052 //        it's not possible will use the stack.
00053 //
00054 //      * If you clobber registers with asm statements in gcc, you
00055 //        better do it in an asm-only function, or save/restore them
00056 //        before/after in the stack. If not, sorrounding C statements
00057 //        could end using the same registers and big-badda-bum (been
00058 //        there, done that...). The big-badda-bum could happen *even
00059 //        if you specify the clobbered register in your asm's*.
00060 //        Even better, use gcc asm syntax for detecting the register
00061 //        asigned to a certain variable so you don't have to clobber any
00062 //        register directly.
00063 //
00064 
00065 ACE_INLINE void
00066 ACE_CDR::swap_2 (const char *orig, char* target)
00067 {
00068 #if defined (ACE_HAS_INTRINSIC_BYTESWAP)
00069   // Take advantage of MSVC++ compiler intrinsic byte swapping
00070   // function.
00071   *reinterpret_cast<unsigned short *> (target) =
00072     _byteswap_ushort (*reinterpret_cast<unsigned short const *> (orig));
00073 #elif defined (ACE_HAS_BSWAP_16)
00074   *reinterpret_cast<unsigned short *> (target) =
00075     bswap_16 (*reinterpret_cast<unsigned short const *> (orig));
00076 #elif defined(ACE_HAS_INTEL_ASSEMBLY)
00077   unsigned short a =
00078     *reinterpret_cast<const unsigned short*> (orig);
00079   asm( "rolw $8, %0" : "=r" (a) : "0" (a) );
00080   *reinterpret_cast<unsigned short*> (target) = a;
00081 #elif defined (ACE_HAS_PENTIUM) \
00082        && (defined(_MSC_VER) || defined(__BORLANDC__)) \
00083        && !defined(ACE_LACKS_INLINE_ASSEMBLY)
00084   __asm mov ebx, orig;
00085   __asm mov ecx, target;
00086   __asm mov ax, [ebx];
00087   __asm rol ax, 8;
00088   __asm mov [ecx], ax;
00089 #else
00090   register ACE_UINT16 usrc = * reinterpret_cast<const ACE_UINT16*> (orig);
00091   register ACE_UINT16* udst = reinterpret_cast<ACE_UINT16*> (target);
00092   *udst = (usrc << 8) | (usrc >> 8);
00093 #endif /* ACE_HAS_PENTIUM */
00094 }
00095 
00096 ACE_INLINE void
00097 ACE_CDR::swap_4 (const char* orig, char* target)
00098 {
00099 #if defined (ACE_HAS_INTRINSIC_BYTESWAP)
00100   // Take advantage of MSVC++ compiler intrinsic byte swapping
00101   // function.
00102   *reinterpret_cast<unsigned long *> (target) =
00103     _byteswap_ulong (*reinterpret_cast<unsigned long const *> (orig));
00104 #elif defined (ACE_HAS_BSWAP_32)
00105   *reinterpret_cast<unsigned int *> (target) =
00106     bswap_32 (*reinterpret_cast<unsigned int const *> (orig));
00107 #elif defined(ACE_HAS_INTEL_ASSEMBLY)
00108   // We have ACE_HAS_PENTIUM, so we know the sizeof's.
00109   register unsigned int j =
00110     *reinterpret_cast<const unsigned int*> (orig);
00111   asm ("bswap %1" : "=r" (j) : "0" (j));
00112   *reinterpret_cast<unsigned int*> (target) = j;
00113 #elif defined(ACE_HAS_PENTIUM) \
00114       && (defined(_MSC_VER) || defined(__BORLANDC__)) \
00115       && !defined(ACE_LACKS_INLINE_ASSEMBLY)
00116   __asm mov ebx, orig;
00117   __asm mov ecx, target;
00118   __asm mov eax, [ebx];
00119   __asm bswap eax;
00120   __asm mov [ecx], eax;
00121 #else
00122   register ACE_UINT32 x = * reinterpret_cast<const ACE_UINT32*> (orig);
00123   x = (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
00124   * reinterpret_cast<ACE_UINT32*> (target) = x;
00125 #endif /* ACE_HAS_INTRINSIC_BYTESWAP */
00126 }
00127 
00128 ACE_INLINE void
00129 ACE_CDR::swap_8 (const char* orig, char* target)
00130 {
00131 #if defined (ACE_HAS_INTRINSIC_BYTESWAP)
00132   // Take advantage of MSVC++ compiler intrinsic byte swapping
00133   // function.
00134   *reinterpret_cast<unsigned __int64 *> (target) =
00135     _byteswap_uint64 (*reinterpret_cast<unsigned __int64 const *> (orig));
00136 #elif defined (ACE_HAS_BSWAP_64)
00137   *reinterpret_cast<unsigned long long *> (target) =
00138     bswap_64 (*reinterpret_cast<unsigned long long const *> (orig));
00139 #elif (defined (__amd64__) || defined (__x86_64__)) && defined(__GNUG__)
00140   register unsigned long x =
00141     * reinterpret_cast<const unsigned long*> (orig);
00142   asm ("bswapq %1" : "=r" (x) : "0" (x));
00143   *reinterpret_cast<unsigned long*> (target) = x;
00144 #elif defined(ACE_HAS_PENTIUM) && defined(__GNUG__)
00145   register unsigned int i =
00146     *reinterpret_cast<const unsigned int*> (orig);
00147   register unsigned int j =
00148     *reinterpret_cast<const unsigned int*> (orig + 4);
00149   asm ("bswap %1" : "=r" (i) : "0" (i));
00150   asm ("bswap %1" : "=r" (j) : "0" (j));
00151   *reinterpret_cast<unsigned int*> (target + 4) = i;
00152   *reinterpret_cast<unsigned int*> (target) = j;
00153 #elif defined(ACE_HAS_PENTIUM) \
00154       && (defined(_MSC_VER) || defined(__BORLANDC__)) \
00155       && !defined(ACE_LACKS_INLINE_ASSEMBLY)
00156   __asm mov ecx, orig;
00157   __asm mov edx, target;
00158   __asm mov eax, [ecx];
00159   __asm mov ebx, 4[ecx];
00160   __asm bswap eax;
00161   __asm bswap ebx;
00162   __asm mov 4[edx], eax;
00163   __asm mov [edx], ebx;
00164 #elif ACE_SIZEOF_LONG == 8
00165   // 64 bit architecture.
00166   register unsigned long x =
00167     * reinterpret_cast<const unsigned long*> (orig);
00168   register unsigned long x84 = (x & 0x000000ff000000ffUL) << 24;
00169   register unsigned long x73 = (x & 0x0000ff000000ff00UL) << 8;
00170   register unsigned long x62 = (x & 0x00ff000000ff0000UL) >> 8;
00171   register unsigned long x51 = (x & 0xff000000ff000000UL) >> 24;
00172   x = (x84 | x73 | x62 | x51);
00173   x = (x << 32) | (x >> 32);
00174   *reinterpret_cast<unsigned long*> (target) = x;
00175 #else
00176   register ACE_UINT32 x =
00177     * reinterpret_cast<const ACE_UINT32*> (orig);
00178   register ACE_UINT32 y =
00179     * reinterpret_cast<const ACE_UINT32*> (orig + 4);
00180   x = (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
00181   y = (y << 24) | ((y & 0xff00) << 8) | ((y & 0xff0000) >> 8) | (y >> 24);
00182   * reinterpret_cast<ACE_UINT32*> (target) = y;
00183   * reinterpret_cast<ACE_UINT32*> (target + 4) = x;
00184 #endif /* ACE_HAS_INTRINSIC_BYTESWAP */
00185 }
00186 
00187 ACE_INLINE void
00188 ACE_CDR::swap_16 (const char* orig, char* target)
00189 {
00190   swap_8 (orig + 8, target);
00191   swap_8 (orig, target + 8);
00192 }
00193 
00194 ACE_INLINE size_t
00195 ACE_CDR::first_size (size_t minsize)
00196 {
00197   if (minsize == 0)
00198     return ACE_CDR::DEFAULT_BUFSIZE;
00199 
00200   size_t newsize = ACE_CDR::DEFAULT_BUFSIZE;
00201   while (newsize < minsize)
00202     {
00203       if (newsize < ACE_CDR::EXP_GROWTH_MAX)
00204         {
00205           // We grow exponentially at the beginning, this is fast and
00206           // reduces the number of allocations.
00207 
00208           // Quickly multiply by two using a bit shift.  This is
00209           // guaranteed to work since the variable is an unsigned
00210           // integer.
00211           newsize <<= 1;
00212         }
00213       else
00214         {
00215           // but continuing with exponential growth can result in over
00216           // allocations and easily yield an allocation failure.
00217           // So we grow linearly when the buffer is too big.
00218           newsize += ACE_CDR::LINEAR_GROWTH_CHUNK;
00219         }
00220     }
00221   return newsize;
00222 }
00223 
00224 ACE_INLINE size_t
00225 ACE_CDR::next_size (size_t minsize)
00226 {
00227   size_t newsize = ACE_CDR::first_size (minsize);
00228 
00229   if (newsize == minsize)
00230     {
00231       // If necessary increment the size
00232       if (newsize < ACE_CDR::EXP_GROWTH_MAX)
00233         // Quickly multiply by two using a bit shift.  This is
00234         // guaranteed to work since the variable is an unsigned
00235         // integer.
00236         newsize <<= 1;
00237       else
00238         newsize += ACE_CDR::LINEAR_GROWTH_CHUNK;
00239     }
00240 
00241   return newsize;
00242 }
00243 
00244 ACE_END_VERSIONED_NAMESPACE_DECL
00245 
00246 // ****************************************************************

Generated on Sun Jan 27 12:05:22 2008 for ACE by doxygen 1.3.6