PageRenderTime 17ms CodeModel.GetById 2ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/indra/llcommon/llendianswizzle.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 94 lines | 44 code | 9 blank | 41 comment | 2 complexity | 49256c3633ea8330933a4a02b3f39235 MD5 | raw file
 1/** 
 2 * @file llendianswizzle.h
 3 * @brief Functions for in-place bit swizzling
 4 *
 5 * $LicenseInfo:firstyear=2002&license=viewerlgpl$
 6 * Second Life Viewer Source Code
 7 * Copyright (C) 2010, Linden Research, Inc.
 8 * 
 9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation;
12 * version 2.1 of the License only.
13 * 
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * Lesser General Public License for more details.
18 * 
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 * 
23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
24 * $/LicenseInfo$
25 */
26
27#ifndef LL_LLENDIANSWIZZLE_H
28#define LL_LLENDIANSWIZZLE_H
29
30/* This function is intended to be used for in-place swizzling, particularly after fread() of
31	binary values from a file.  Such as:
32	
33	numRead = fread(scale.mV, sizeof(float), 3, fp);
34	llendianswizzle(scale.mV, sizeof(float), 3);
35	
36	It assumes that the values in the file are LITTLE endian, so it's a no-op on a little endian machine.
37	
38	It keys off of typesize to do the correct swizzle, so make sure that typesize is the size of the native type.
39	
40	64-bit types are not yet handled.
41*/
42
43#ifdef LL_LITTLE_ENDIAN
44	// little endian is native for most things.
45	inline void llendianswizzle(void *,int,int)
46	{
47		// Nothing to do
48	}
49#endif
50
51#ifdef LL_BIG_ENDIAN
52	// big endian requires a bit of work.
53	inline void llendianswizzle(void *p,int typesize, int count)
54	{
55		int i;
56		switch(typesize)
57		{
58			case 2:
59			{
60				U16 temp;
61				for(i=count ;i!=0 ;i--)
62				{
63					temp = ((U16*)p)[0];
64					((U16*)p)[0] = 	((temp >> 8)  & 0x000000FF) | ((temp << 8)  & 0x0000FF00);
65					p = (void*)(((U16*)p) + 1);
66				}
67			}
68			break;
69			
70			case 4:
71			{
72				U32 temp;
73				for(i=count; i!=0; i--)
74				{
75					temp = ((U32*)p)[0];
76					((U32*)p)[0] = 	
77							((temp >> 24) & 0x000000FF) | 
78							((temp >> 8)  & 0x0000FF00) | 
79							((temp << 8)  & 0x00FF0000) |
80							((temp << 24) & 0xFF000000);
81					p = (void*)(((U32*)p) + 1);
82				}
83			}
84			break;
85		}
86		
87	}
88#endif
89
90// Use this when working with a single integral value you want swizzled
91
92#define llendianswizzleone(x) llendianswizzle(&(x), sizeof(x), 1)
93
94#endif // LL_LLENDIANSWIZZLE_H