Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | 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         4. Derivative works are permitted, but they must carry this notice
00027            in full and credit the original source.
00028 
00029 
00030                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00031 
00032 
00033         @version        Initial version; October 2004
00034 
00035         Feb 20th 2005 - Asm version thrown away by Aleksey Bobnev :))
00036                         non-asm version now makes use of bswap intrinsic
00037 
00038         @author         Kris
00039                         Aleksey Bobnev
00040 
00041 *******************************************************************************/
00042 
00043 module mango.sys.ByteSwap;
00044 
00045 import std.intrinsic;
00046 
00047 /*******************************************************************************
00048 
00049         Reverse byte order for specific datum sizes. Note that the
00050         byte-swap approach avoids alignment issues, so is probably
00051         faster overall than a traditional 'shift' implementation.
00052 
00053 *******************************************************************************/
00054 
00055 struct ByteSwap
00056 {       
00057         /***********************************************************************
00058         
00059         ***********************************************************************/
00060 
00061         final static void swap16 (void *dst, uint count)
00062         {
00063                 ubyte* p = cast(ubyte*) dst;
00064                 while (count)
00065                       {
00066                       ubyte b = p[0];
00067                       p[0] = p[1];
00068                       p[1] = b;
00069 
00070                       p += short.sizeof;
00071                       count -= short.sizeof;
00072                       }
00073         }
00074 
00075         /***********************************************************************
00076         
00077         ***********************************************************************/
00078 
00079         final static void swap32 (void *dst, uint count)
00080         {
00081                 uint* p = cast(uint*) dst;
00082                 while (count)
00083                       {
00084                       *p = bswap(*p);
00085                       p ++;
00086                       count -= int.sizeof;
00087                       }
00088         }
00089 
00090         /***********************************************************************
00091         
00092         ***********************************************************************/
00093 
00094         final static void swap64 (void *dst, uint count)
00095         {
00096                 uint* p = cast(uint*) dst;
00097                 while (count)
00098                       {
00099                       uint i = p[0];
00100                       p[0] = bswap(p[1]);
00101                       p[1] = bswap(i);
00102 
00103                       p += (long.sizeof / int.sizeof);
00104                       count -= long.sizeof;
00105                       }
00106         }
00107 
00108         /***********************************************************************
00109         
00110         ***********************************************************************/
00111 
00112         final static void swap80 (void *dst, uint count)
00113         {
00114                 ubyte* p = cast(ubyte*) dst;
00115                 while (count)
00116                       {
00117                       ubyte b = p[0];
00118                       p[0] = p[9];
00119                       p[9] = b;
00120 
00121                       b = p[1];
00122                       p[1] = p[8];
00123                       p[8] = b;
00124 
00125                       b = p[2];
00126                       p[2] = p[7];
00127                       p[7] = b;
00128         
00129                       b = p[3];
00130                       p[3] = p[6];
00131                       p[6] = b;
00132         
00133                       b = p[4];
00134                       p[4] = p[5];
00135                       p[5] = b;
00136 
00137                       p += real.sizeof;
00138                       count -= real.sizeof;
00139                       }
00140         }
00141 }

Generated on Mon Nov 14 10:59:36 2005 for Mango by  doxygen 1.4.0