Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

ByteSwap.d

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003         @file ByteSwap.d
00004         
00005         Copyright (C) 2004 Kris Bell
00006         
00007         This software is provided 'as-is', without any express or implied
00008         warranty. In no event will the authors be held liable for damages
00009         of any kind arising from the use of this software.
00010         
00011         Permission is hereby granted to anyone to use this software for any 
00012         purpose, including commercial applications, and to alter it and/or 
00013         redistribute it freely, subject to the following restrictions:
00014         
00015         1. The origin of this software must not be misrepresented; you must 
00016            not claim that you wrote the original software. If you use this 
00017            software in a product, an acknowledgment within documentation of 
00018            said product would be appreciated but is not required.
00019 
00020         2. Altered source versions must be plainly marked as such, and must 
00021            not be misrepresented as being the original software.
00022 
00023         3. This notice may not be removed or altered from any distribution
00024            of the source.
00025 
00026 
00027                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00028 
00029 
00030         @version        Initial version, October 2004      
00031         @author         Kris
00032 
00033 
00034 *******************************************************************************/
00035 
00036 module mango.base.ByteSwap;
00037 
00038 /*******************************************************************************
00039 
00040         Reverse byte order for specific datum sizes. Note that the
00041         byte-swap approach avoids alignment issues, so is probably
00042         faster overall than a traditional 'shift' implementation.
00043 
00044 *******************************************************************************/
00045 
00046 struct ByteSwap
00047 {       
00048         version (DigitalMars) version (X86) version (D_InlineAsm)
00049         {
00050         /***********************************************************************
00051         
00052         ***********************************************************************/
00053 
00054         final static void swap16 (void *dst, uint count)
00055         {
00056                 asm {
00057                     mov  ESI, dword ptr [dst];
00058                     mov  ECX, dword ptr [count];
00059                     test ECX, ECX;
00060                     jmp  test;
00061         loop:
00062                     mov  AL, byte ptr [ESI+0];
00063                     xchg AL, byte ptr [ESI+1];
00064                     mov  byte ptr [ESI+0], AL;
00065                     add  ESI, 2;
00066                     sub  ECX, 2;
00067         test:
00068                     jne  loop;
00069                     }
00070         }
00071 
00072         /***********************************************************************
00073         
00074         ***********************************************************************/
00075 
00076         final static void swap32 (void *dst, uint count)
00077         {
00078                 asm {
00079                     mov  ESI, dword ptr [dst];
00080                     mov  ECX, dword ptr [count];
00081                     test ECX, ECX;
00082                     jmp  test;
00083         loop:
00084                     mov  AL, byte ptr [ESI+0];
00085                     xchg AL, byte ptr [ESI+3];
00086                     mov  byte ptr [ESI+0], AL;
00087                     mov  AL, byte ptr [ESI+1];
00088                     xchg AL, byte ptr [ESI+2];
00089                     mov  byte ptr [ESI+1], AL;
00090                     add  ESI, 4;
00091                     sub  ECX, 4;
00092         test:
00093                     jne  loop;
00094                     }
00095         }
00096 
00097         /***********************************************************************
00098         
00099         ***********************************************************************/
00100 
00101         final static void swap64 (void *dst, uint count)
00102         {
00103                 asm {
00104                     mov  ESI, dword ptr [dst];
00105                     mov  ECX, dword ptr [count];
00106                     test ECX, ECX;
00107                     jmp  test;
00108         loop:
00109                     mov  AL, byte ptr [ESI+0];
00110                     xchg AL, byte ptr [ESI+7];
00111                     mov  byte ptr [ESI+0], AL;
00112                     mov  AL, byte ptr [ESI+1];
00113                     xchg AL, byte ptr [ESI+6];
00114                     mov  byte ptr [ESI+1], AL;
00115                     mov  AL, byte ptr [ESI+2];
00116                     xchg AL, byte ptr [ESI+5];
00117                     mov  byte ptr [ESI+2], AL;
00118                     mov  AL, byte ptr [ESI+3];
00119                     xchg AL, byte ptr [ESI+4];
00120                     mov  byte ptr [ESI+3], AL;
00121                     add  ESI, 8;
00122                     sub  ECX, 8;
00123         test:
00124                     jne  loop;
00125                     }
00126         }
00127 
00128         /***********************************************************************
00129         
00130         ***********************************************************************/
00131 
00132         final static void swap80 (void *dst, uint count)
00133         {
00134                 asm {
00135                     mov  ESI, dword ptr [dst];
00136                     mov  ECX, dword ptr [count];
00137                     test ECX, ECX;
00138                     jmp  test;
00139         loop:
00140                     mov  AL, byte ptr [ESI+0];
00141                     xchg AL, byte ptr [ESI+9];
00142                     mov  byte ptr [ESI+0], AL;
00143                     mov  AL, byte ptr [ESI+1];
00144                     xchg AL, byte ptr [ESI+8];
00145                     mov  byte ptr [ESI+1], AL;
00146                     mov  AL, byte ptr [ESI+2];
00147                     xchg AL, byte ptr [ESI+7];
00148                     mov  byte ptr [ESI+2], AL;
00149                     mov  AL, byte ptr [ESI+3];
00150                     xchg AL, byte ptr [ESI+6];
00151                     mov  byte ptr [ESI+3], AL;
00152                     mov  AL, byte ptr [ESI+4];
00153                     xchg AL, byte ptr [ESI+5];
00154                     mov  byte ptr [ESI+4], AL;
00155                     add  ESI, 10;
00156                     sub  ECX, 10;
00157         test:
00158                     jne  loop;
00159                     }
00160         }
00161         }
00162 
00163      else
00164 
00165         {
00166         /***********************************************************************
00167         
00168         ***********************************************************************/
00169 
00170         final static void swap16 (void *dst, uint count)
00171         {
00172                 ubyte* p = cast(ubyte*) dst;
00173                 while (count)
00174                       {
00175                       byte b = p[0];
00176                       p[0] = p[1];
00177                       p[1] = b;
00178 
00179                       p += short.sizeof;
00180                       count -= short.sizeof;
00181                       }
00182         }
00183 
00184         /***********************************************************************
00185         
00186         ***********************************************************************/
00187 
00188         final static void swap32 (void *dst, uint count)
00189         {
00190                 ubyte* p = cast(ubyte*) dst;
00191                 while (count)
00192                       {
00193                       ubyte b = p[0];
00194                       p[0] = p[3];
00195                       p[3] = b;
00196 
00197                       b = p[1];
00198                       p[1] = p[2];
00199                       p[2] = b;
00200 
00201                       p += int.sizeof;
00202                       count -= int.sizeof;
00203                       }
00204         }
00205 
00206         /***********************************************************************
00207         
00208         ***********************************************************************/
00209 
00210         final static void swap64 (void *dst, uint count)
00211         {
00212                 ubyte* p = cast(ubyte*) dst;
00213                 while (count)
00214                       {
00215                       ubyte b = p[0];
00216                       p[0] = p[7];
00217                       p[7] = b;
00218 
00219                       b = p[1];
00220                       p[1] = p[6];
00221                       p[6] = b;
00222         
00223                       b = p[2];
00224                       p[2] = p[5];
00225                       p[5] = b;
00226         
00227                       b = p[3];
00228                       p[3] = p[4];
00229                       p[3] = b;
00230 
00231                       p += long.sizeof;
00232                       count -= long.sizeof;
00233                       }
00234         }
00235 
00236         /***********************************************************************
00237         
00238         ***********************************************************************/
00239 
00240         final static void swap80 (void *dst, uint count)
00241         {
00242                 ubyte* p = cast(ubyte*) dst;
00243                 while (count)
00244                       {
00245                       ubyte b = p[0];
00246                       p[0] = p[9];
00247                       p[9] = b;
00248 
00249                       b = p[1];
00250                       p[1] = p[8];
00251                       p[8] = b;
00252 
00253                       b = p[2];
00254                       p[2] = p[7];
00255                       p[7] = b;
00256         
00257                       b = p[3];
00258                       p[3] = p[6];
00259                       p[6] = b;
00260         
00261                       b = p[4];
00262                       p[4] = p[5];
00263                       p[5] = b;
00264 
00265                       p += real.sizeof;
00266                       count -= real.sizeof;
00267                       }
00268         }
00269 
00270         }
00271 }

Generated on Sun Nov 7 19:06:49 2004 for Mango by doxygen 1.3.6