-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathhavoc.cpp
More file actions
executable file
·211 lines (163 loc) · 5.55 KB
/
Copy pathhavoc.cpp
File metadata and controls
executable file
·211 lines (163 loc) · 5.55 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
Copyright (C) 2016 British Broadcasting Corporation, Parabola Research
and Queen Mary University of London.
This file is part of the Turing codec.
The Turing codec is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation.
The Turing codec is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
Commercial support and intellectual property rights for
the Turing codec are also available under a proprietary license.
For more information, contact us at info @ turingcodec.org.
*/
#include "pred_inter.h"
#include "pred_intra.h"
#include "transform.h"
#include "sad.h"
#include "ssd.h"
#include "diff.h"
#include "quantize.h"
#include "hadamard.h"
#include "havoc.h"
#include "Jit.h"
#include <stdint.h>
#include <type_traits>
#include <string>
#ifdef WIN32
#include <Windows.h>
#endif
#ifdef _MSC_VER
#include <intrin.h>
#endif
#ifdef __GNUC__
static void __cpuidex(int cpuInfo[4], int function_id, int subfunction_id)
{
__asm__ __volatile__("cpuid" :
"=a" ((cpuInfo)[0]),
"=b" ((cpuInfo)[1]),
"=c" ((cpuInfo)[2]),
"=d" ((cpuInfo)[3])
:
"0" (function_id),
"2" (subfunction_id));
}
static uint64_t _xgetbv(uint32_t index)
{
uint32_t eax, edx;
__asm__ __volatile__("xgetbv" :
"=a" (eax),
"=d" (edx)
:
"c" (index));
return ((uint64_t)edx << 32) | eax;
}
#endif
static int bit_is_set(int value, int n)
{
return value & (1 << n);
}
havoc_instruction_set havoc_instruction_set_support()
{
std::underlying_type<havoc_instruction_set>::type mask = HAVOC_C_REF | HAVOC_C_OPT;
enum { eax = 0, ebx = 1, ecx = 2, edx = 3 };
int cpuInfo[4]; // eax ... edx
__cpuidex(cpuInfo, 0, 0);
const int max_standard_level = cpuInfo[0];
if (max_standard_level == 0) return (havoc_instruction_set)mask;
__cpuidex(cpuInfo, 1, 0);
if (bit_is_set(cpuInfo[edx], 26)) mask |= HAVOC_SSE2;
if (bit_is_set(cpuInfo[ecx], 1)) mask |= HAVOC_SSE3;
if (bit_is_set(cpuInfo[ecx], 9)) mask |= HAVOC_SSSE3;
if (bit_is_set(cpuInfo[ecx], 19)) mask |= HAVOC_SSE41;
if (bit_is_set(cpuInfo[ecx], 20)) mask |= HAVOC_SSE42;
if (bit_is_set(cpuInfo[ecx], 23)) mask |= HAVOC_POPCNT;
if (bit_is_set(cpuInfo[ecx], 28) && bit_is_set(cpuInfo[ecx], 27))
{
uint64_t xcr0 = _xgetbv(0);
if ((xcr0 & 0x2) && (xcr0 & 0x4))
{
mask |= HAVOC_AVX;
if (max_standard_level >= 7)
{
__cpuidex(cpuInfo, 7, 0);
if (bit_is_set(cpuInfo[ebx], 5))
mask |= HAVOC_AVX2;
}
}
}
__cpuidex(cpuInfo, 0x80000000, 0);
uint32_t cap = static_cast<uint32_t>(cpuInfo[eax]);
if (cap >= 0x80000001)
{
__cpuidex(cpuInfo, 0x80000001, 0);
if (cpuInfo[ecx] & 0x00000020)
mask |= HAVOC_LZCNT;
}
return (havoc_instruction_set)mask;
}
void havoc_print_instruction_set_support(FILE *f, havoc_instruction_set mask)
{
f = stdout;
fprintf(f, "havoc processor instruction set support:\n");
#define X(value, name, description) fprintf(f, "[%c] " #name " (" description ")\n", ((1 << value) & mask) ? 'x' : ' ');
HAVOC_INSTRUCTION_SET_XMACRO
#undef X
fprintf(f, "\n");
}
havoc_code havoc_new_code(havoc_instruction_set mask, int size)
{
havoc_code code;
code.implementation = new Jit::Buffer(size, mask);
return code;
}
void havoc_delete_code(havoc_code code)
{
delete (Jit::Buffer *)code.implementation;
}
extern "C" int do_measure_speed;
int havoc_main(int argc, const char *argv[])
{
std::cout << "HAVOC [Handcoded Assembly for VideO Codecs] self test";
std::cout << "Performs the following checks:\n";
std::cout << "\t1) Retrieve information on the machine's instruction set\n";
std::cout << "\t2) Run some basic computations of SSD, transform, etc. and check the results for errors\n";
std::cout << "";
if (argc == 2)
{
if (argv[1] != std::string("--no-measure-speed"))
{
std::cout << "Usage: " << argv[0] << "[--no-measure-speed]\n";
return -1;
}
do_measure_speed = 0;
}
havoc_instruction_set mask = havoc_instruction_set_support();
#ifdef WIN32
if (!SetProcessAffinityMask(GetCurrentProcess(), 1))
std::cout << "** SetProcessAffinityMask() failed **\n\n";
#endif
havoc_print_instruction_set_support(stdout, mask);
int error_count = 0;
havoc::testSubtractBi<uint8_t>(&error_count, mask);
havoc::testSubtractBi<uint16_t>(&error_count, mask);
havoc_test_sad_multiref(&error_count, mask);
havoc_test_sad(&error_count, mask);
havoc_test_ssd(&error_count, mask);
havoc::intra::test<uint8_t>(&error_count, mask);
havoc::intra::test<uint16_t>(&error_count, mask);
havoc_test_hadamard_satd(&error_count, mask);
havoc_test_quantize_inverse(&error_count, mask);
havoc_test_quantize(&error_count, mask);
havoc_test_quantize_reconstruct(&error_count, mask);
havoc_test_pred_uni(&error_count, mask);
havoc_test_pred_bi(&error_count, mask);
havoc::test_inverse_transform_add<uint8_t>(&error_count, mask);
havoc::test_inverse_transform_add<uint16_t>(&error_count, mask);
havoc::test_transform(&error_count, mask);
printf("\n");
printf("havoc self test: %d errors\n", error_count);
return error_count;
}