]> git.cworth.org Git - vogl/blob - src/voglcore/lzma_CpuArch.h
Initial vogl checkin
[vogl] / src / voglcore / lzma_CpuArch.h
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 /* CpuArch.h\r
28 2008-08-05\r
29 Igor Pavlov\r
30 Public domain */\r
31 \r
32 #ifndef __CPUARCH_H\r
33 #define __CPUARCH_H\r
34 \r
35 /*\r
36 LITTLE_ENDIAN_UNALIGN means:\r
37   1) CPU is LITTLE_ENDIAN\r
38   2) it's allowed to make unaligned memory accesses\r
39 if LITTLE_ENDIAN_UNALIGN is not defined, it means that we don't know\r
40 about these properties of platform.\r
41 */\r
42 \r
43 #if defined(_M_IX86) || defined(_M_X64) || defined(_M_AMD64) || defined(__i386__) || defined(__x86_64__)\r
44 #define LITTLE_ENDIAN_UNALIGN\r
45 #endif\r
46 \r
47 #ifdef LITTLE_ENDIAN_UNALIGN\r
48 \r
49 #define GetUi16(p) (*(const UInt16 *)(p))\r
50 #define GetUi32(p) (*(const UInt32 *)(p))\r
51 #define GetUi64(p) (*(const UInt64 *)(p))\r
52 #define SetUi32(p, d) *(UInt32 *)(p) = (d);\r
53 \r
54 #else\r
55 \r
56 #define GetUi16(p) (((const Byte *)(p))[0] | ((UInt16)((const Byte *)(p))[1] << 8))\r
57 \r
58 #define GetUi32(p) (                         \\r
59     ((const Byte *)(p))[0] |                 \\r
60     ((UInt32)((const Byte *)(p))[1] << 8) |  \\r
61     ((UInt32)((const Byte *)(p))[2] << 16) | \\r
62     ((UInt32)((const Byte *)(p))[3] << 24))\r
63 \r
64 #define GetUi64(p) (GetUi32(p) | ((UInt64)GetUi32(((const Byte *)(p)) + 4) << 32))\r
65 \r
66 #define SetUi32(p, d)                         \\r
67     {                                         \\r
68         UInt32 _x_ = (d);                     \\r
69         ((Byte *)(p))[0] = (Byte)_x_;         \\r
70         ((Byte *)(p))[1] = (Byte)(_x_ >> 8);  \\r
71         ((Byte *)(p))[2] = (Byte)(_x_ >> 16); \\r
72         ((Byte *)(p))[3] = (Byte)(_x_ >> 24); \\r
73     }\r
74 \r
75 #endif\r
76 \r
77 #if defined(LITTLE_ENDIAN_UNALIGN) && defined(_WIN64) && (_MSC_VER >= 1300)\r
78 \r
79 #pragma intrinsic(_byteswap_ulong)\r
80 #pragma intrinsic(_byteswap_uint64)\r
81 #define GetBe32(p) _byteswap_ulong(*(const UInt32 *)(const Byte *)(p))\r
82 #define GetBe64(p) _byteswap_uint64(*(const UInt64 *)(const Byte *)(p))\r
83 \r
84 #else\r
85 \r
86 #define GetBe32(p) (                         \\r
87     ((UInt32)((const Byte *)(p))[0] << 24) | \\r
88     ((UInt32)((const Byte *)(p))[1] << 16) | \\r
89     ((UInt32)((const Byte *)(p))[2] << 8) |  \\r
90     ((const Byte *)(p))[3])\r
91 \r
92 #define GetBe64(p) (((UInt64)GetBe32(p) << 32) | GetBe32(((const Byte *)(p)) + 4))\r
93 \r
94 #endif\r
95 \r
96 #define GetBe16(p) (((UInt16)((const Byte *)(p))[0] << 8) | ((const Byte *)(p))[1])\r
97 \r
98 #endif\r