]> git.cworth.org Git - vogl/blob - src/voglcore/lzma_BraIA64.cpp
Initial vogl checkin
[vogl] / src / voglcore / lzma_BraIA64.cpp
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 /* BraIA64.c -- Converter for IA-64 code
28 2008-10-04 : Igor Pavlov : Public domain */
29 #include "vogl_core.h"
30 #include "lzma_Bra.h"
31
32 namespace vogl
33 {
34
35     static const Byte kBranchTable[32] =
36         {
37             0, 0, 0, 0, 0, 0, 0, 0,
38             0, 0, 0, 0, 0, 0, 0, 0,
39             4, 4, 6, 6, 0, 0, 7, 7,
40             4, 4, 0, 0, 4, 4, 0, 0
41         };
42
43     SizeT IA64_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
44     {
45         SizeT i;
46         if (size < 16)
47             return 0;
48         size -= 16;
49         for (i = 0; i <= size; i += 16)
50         {
51             UInt32 instrTemplate = data[i] & 0x1F;
52             UInt32 mask = kBranchTable[instrTemplate];
53             UInt32 bitPos = 5;
54             int slot;
55             for (slot = 0; slot < 3; slot++, bitPos += 41)
56             {
57                 UInt32 bytePos, bitRes;
58                 UInt64 instruction, instNorm;
59                 int j;
60                 if (((mask >> slot) & 1) == 0)
61                     continue;
62                 bytePos = (bitPos >> 3);
63                 bitRes = bitPos & 0x7;
64                 instruction = 0;
65                 for (j = 0; j < 6; j++)
66                     instruction += (UInt64)data[i + j + bytePos] << (8 * j);
67
68                 instNorm = instruction >> bitRes;
69                 if (((instNorm >> 37) & 0xF) == 0x5 && ((instNorm >> 9) & 0x7) == 0)
70                 {
71                     UInt32 src = (UInt32)((instNorm >> 13) & 0xFFFFF);
72                     UInt32 dest;
73                     src |= ((UInt32)(instNorm >> 36) & 1) << 20;
74
75                     src <<= 4;
76
77                     if (encoding)
78                         dest = ip + (UInt32)i + src;
79                     else
80                         dest = src - (ip + (UInt32)i);
81
82                     dest >>= 4;
83
84                     instNorm &= ~((UInt64)(0x8FFFFF) << 13);
85                     instNorm |= ((UInt64)(dest & 0xFFFFF) << 13);
86                     instNorm |= ((UInt64)(dest & 0x100000) << (36 - 20));
87
88                     instruction &= (1 << bitRes) - 1;
89                     instruction |= (instNorm << bitRes);
90                     for (j = 0; j < 6; j++)
91                         data[i + j + bytePos] = (Byte)(instruction >> (8 * j));
92                 }
93             }
94         }
95         return i;
96     }
97 }