-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgolomb.cpp
More file actions
164 lines (129 loc) · 2.88 KB
/
golomb.cpp
File metadata and controls
164 lines (129 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "golomb.h"
#include "egtables.h"
#include "math.h"
namespace evx {
uint16 encode_unsigned_golomb_value(uint8 input, uint8 *count)
{
uint16 result = EVX_UEXP_GOLOMB_CODES[input];
if (count)
{
*count = EVX_UEXP_GOLOMB_SIZE_LUT[input];
}
return result;
}
uint32 encode_signed_golomb_value(int8 input, uint8 *count)
{
uint8 index = input;
uint32 result = EVX_SEXP_GOLOMB_CODES[index];
if (count)
{
*count = EVX_SEXP_GOLOMB_SIZE_LUT[index];
}
return result;
}
uint32 encode_unsigned_golomb_value(uint16 input, uint8 * count)
{
if (input < 256)
{
return encode_unsigned_golomb_value((uint8) input, count);
}
uint32 result = 0;
uint32 value = input + 1;
uint8 bit_count = evx_required_bits(value);
while (value)
{
result <<= 1;
result |= value & 0x1;
value >>= 1;
}
result <<= bit_count - 1;
bit_count <<= 1;
bit_count -= 1;
if (count)
{
*count = bit_count;
}
return result;
}
uint32 encode_signed_golomb_value(int16 input, uint8 *count)
{
if (input >= -128 && input < 128)
{
return encode_signed_golomb_value((int8) input, count);
}
uint32 result = 0;
uint32 value = (!input ? 1 : (abs((int32) input) << 1) | ((input >> 15) & 0x1));
uint8 bit_count = evx_required_bits(value);
while (value)
{
result <<= 1;
result |= value & 0x1;
value >>= 1;
}
result <<= bit_count - 1;
bit_count <<= 1;
bit_count -= 1;
if (count)
{
*count = bit_count;
}
return result;
}
uint16 decode_unsigned_golomb_value(uint32 input, uint8 *count)
{
uint16 result = 0;
uint8 zero_count = 0;
uint8 bit_count = 0;
while (!(input & 0x1))
{
zero_count++;
input >>= 1;
}
bit_count = zero_count + 1;
for (uint8 i = 0; i < bit_count; i++)
{
result <<= 1;
result |= input & 0x1;
input >>= 1;
}
result -= 1;
if (count)
{
*count = bit_count + zero_count;
}
return result;
}
int16 decode_signed_golomb_value(uint32 input, uint8 *count)
{
int16 result = 0;
uint8 zero_count = 0;
uint8 bit_count = 0;
int16 sign = 0;
while (!(input & 0x1))
{
zero_count++;
input >>= 1;
}
bit_count = zero_count + 1;
for (uint8 i = 0; i < bit_count; i++)
{
result <<= 1;
result |= input & 0x1;
input >>= 1;
}
// Remove the lowest bit as our sign bit.
sign = 1 - 2 * (result & 0x1);
result = sign * ((result >> 1) & 0x7FFF);
// Defend against overflow on min int16.
bit_count += zero_count;
if (bit_count > 0x20)
{
result |= 0x8000;
}
if (count)
{
*count = bit_count + zero_count;
}
return result;
}
} // namespace evx