llendianswizzle.h

Go to the documentation of this file.
00001 
00032 #ifndef LL_LLENDIANSWIZZLE_H
00033 #define LL_LLENDIANSWIZZLE_H
00034 
00035 /* This function is intended to be used for in-place swizzling, particularly after fread() of
00036         binary values from a file.  Such as:
00037         
00038         numRead = fread(scale.mV, sizeof(float), 3, fp);
00039         llendianswizzle(scale.mV, sizeof(float), 3);
00040         
00041         It assumes that the values in the file are LITTLE endian, so it's a no-op on a little endian machine.
00042         
00043         It keys off of typesize to do the correct swizzle, so make sure that typesize is the size of the native type.
00044         
00045         64-bit types are not yet handled.
00046 */
00047 
00048 #ifdef LL_LITTLE_ENDIAN
00049         // little endian is native for most things.
00050         inline void llendianswizzle(void *,int,int)
00051         {
00052                 // Nothing to do
00053         }
00054 #endif
00055 
00056 #ifdef LL_BIG_ENDIAN
00057         // big endian requires a bit of work.
00058         inline void llendianswizzle(void *p,int typesize, int count)
00059         {
00060                 int i;
00061                 switch(typesize)
00062                 {
00063                         case 2:
00064                         {
00065                                 U16 temp;
00066                                 for(i=count ;i!=0 ;i--)
00067                                 {
00068                                         temp = ((U16*)p)[0];
00069                                         ((U16*)p)[0] =  ((temp >> 8)  & 0x000000FF) | ((temp << 8)  & 0x0000FF00);
00070                                         p = (void*)(((U16*)p) + 1);
00071                                 }
00072                         }
00073                         break;
00074                         
00075                         case 4:
00076                         {
00077                                 U32 temp;
00078                                 for(i=count; i!=0; i--)
00079                                 {
00080                                         temp = ((U32*)p)[0];
00081                                         ((U32*)p)[0] =  
00082                                                         ((temp >> 24) & 0x000000FF) | 
00083                                                         ((temp >> 8)  & 0x0000FF00) | 
00084                                                         ((temp << 8)  & 0x00FF0000) |
00085                                                         ((temp << 24) & 0xFF000000);
00086                                         p = (void*)(((U32*)p) + 1);
00087                                 }
00088                         }
00089                         break;
00090                 }
00091                 
00092         }
00093 #endif
00094 
00095 // Use this when working with a single integral value you want swizzled
00096 
00097 #define llendianswizzleone(x) llendianswizzle(&(x), sizeof(x), 1)
00098 
00099 #endif // LL_LLENDIANSWIZZLE_H

Generated on Thu Jul 1 06:08:28 2010 for Second Life Viewer by  doxygen 1.4.7