gml_BytePack.h

Go to the documentation of this file.
00001 /** @file gml_BytePack.h
00002  * 
00003  *    General functions to access bytes in memory words.
00004  * 
00005  *   Copyright (c) 2004 CLIPS-IMAG
00006  * 
00007  *   See the file "gml_LicenseTerms.txt" for information on usage and redistribution
00008  *   of this file, and for a DISCLAIMER OF ALL WARRANTIES.
00009  * 
00010  *   Created in May 2004 (JL)
00011  */
00012 #ifndef __GML_BYTEPACK__
00013 #define __GML_BYTEPACK__
00014 
00015 
00016 #include "gml/base/gml_Types.h"
00017 //#include "gml/math/gml_Math.h"
00018 
00019 #include <assert.h>
00020 
00021 /**
00022  *  gml_Pack_Bytes --
00023  *    Assemble the four argument bytes into the `word', so their order in
00024  *    memory is the same as in the argument list.
00025  * 
00026  */
00027 inline static
00028 void gml_Pack_Bytes (UInt32 &word, UInt8 const byte0, UInt8 const byte1, UInt8 const byte2, UInt8 const byte3)
00029 {
00030   #if BYTE_ORDER == BIG_ENDIAN
00031     word = byte0 << 24 | byte1 << 16 | byte2 << 8 | byte3;
00032   #else
00033     word = byte3 << 24 | byte2 << 16 | byte1 << 8 | byte0;
00034   #endif
00035 }
00036 
00037 /**
00038  *  gml_Unpack_Bytes --
00039  *    Extract the four argument bytes from the `word', respecting
00040  *    their order in memory.
00041  * 
00042  */
00043 inline static
00044 void gml_Unpack_Bytes (UInt32 const word, UInt8 &byte0, UInt8 &byte1, UInt8 &byte2, UInt8 &byte3)
00045 {
00046   #if BYTE_ORDER == BIG_ENDIAN
00047     byte0 = word >> 24;
00048     byte1 = word >> 16;
00049     byte2 = word >> 8;
00050     byte3 = word;
00051   #else
00052     byte0 = word;
00053     byte1 = word >> 8;
00054     byte2 = word >> 16;
00055     byte3 = word >> 24;
00056   #endif
00057 }
00058 
00059 /**
00060  *  gml_Unpack_Bytes --
00061  *    Place the four bytes in ``word`` into the lower byte of the four 
00062  *    arguments, respecting the memory order in ``word``.
00063  * 
00064  */
00065 inline static
00066 void gml_Unpack_Bytes (UInt32 const word, UInt32 &byte0, UInt32 &byte1, UInt32 &byte2, UInt32 &byte3)
00067 {
00068   #if BYTE_ORDER == BIG_ENDIAN
00069     byte0 = word >> 24;
00070     byte1 = word >> 16 & 0xFF;
00071     byte2 = word >> 8  & 0xFF;
00072     byte3 = word       & 0xFF;
00073   #else
00074     byte0 = word       & 0xFF;
00075     byte1 = word >> 8  & 0xFF;
00076     byte2 = word >> 16 & 0xFF;
00077     byte3 = word >> 24;
00078   #endif
00079 }
00080 
00081 /**
00082  *  gml_Pack_Bytes --
00083  * 
00084  */
00085 template <typename T>
00086 inline static 
00087 void gml_Pack_Bytes (T * const word, UInt8 const byte0, UInt8 const byte1, UInt8 const byte2, UInt8 const byte3)
00088 {
00089   UInt32 tmp;
00090   gml_Pack_Bytes (tmp, byte0, byte1, byte2, byte3);
00091   *((UInt32*)word) = tmp;
00092 }
00093 
00094 /**
00095  *  gml_Unpack_Bytes --
00096  * 
00097  */
00098 template <typename T>
00099 inline static
00100 void gml_Unpack_Bytes (T const * const word, UInt8 &byte0, UInt8 &byte1, UInt8 &byte2, UInt8 &byte3)
00101 {
00102   UInt32 tmp = *((UInt32*)word);
00103   gml_Unpack_Bytes (tmp, byte0, byte1, byte2, byte3);
00104 }
00105 
00106 /**
00107  *  gml_Pack_Bytes --
00108  * 
00109  */
00110 template <typename T>
00111 inline static 
00112 void gml_Pack_Bytes (T * const word, UInt32 const byte0, UInt32 const byte1, UInt32 const byte2, UInt32 const byte3)
00113 {
00114   UInt32 tmp;
00115   gml_Pack_Bytes (tmp, byte0, byte1, byte2, byte3);
00116   *((UInt32*)word) = tmp;
00117 }
00118 
00119 /**
00120  *  gml_Unpack_Bytes --
00121  * 
00122  */
00123 template <typename T>
00124 inline static
00125 void gml_Unpack_Bytes (T const * const word, UInt32 &byte0, UInt32 &byte1, UInt32 &byte2, UInt32 &byte3)
00126 {
00127   UInt32 const tmp = *((UInt32*)word);
00128   gml_Unpack_Bytes (tmp, byte0, byte1, byte2, byte3);
00129 }
00130 
00131 
00132 #endif /* __GML_BYTEPACK__ */
00133 
Generated on Tue Jun 12 14:03:27 2007 for gml by Doxygen 1.5.2.