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

Generated on Sat Apr 9 20:11:25 2005 for Mango by doxygen 1.3.6