1704f3e2dc5808f57cd2be7218426badff0e7b32
1 /*****************************************************************************
2 * Copyright (C) 2013 x265 project
4 * Authors: Steve Borho <steve@borho.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
20 * This program is also available under a commercial proprietary license.
21 * For more information, contact us at license @ x265.com.
22 *****************************************************************************/
24 #ifndef _TESTHARNESS_H_
25 #define _TESTHARNESS_H_ 1
28 #include "primitives.h"
31 #pragma warning(disable: 4324) // structure was padded due to __declspec(align())
39 #define PIXEL_MAX ((1 << BIT_DEPTH) - 1)
41 #define SHORT_MAX 32767
42 #define SHORT_MIN -32767
43 #define UNSIGNED_SHORT_MAX 65535
47 extern const char* lumaPartStr
[NUM_LUMA_PARTITIONS
];
48 extern const char* const* chromaPartStr
[X265_CSP_COUNT
];
56 virtual ~TestHarness() {}
58 virtual bool testCorrectness(const EncoderPrimitives
& ref
, const EncoderPrimitives
& opt
) = 0;
60 virtual void measureSpeed(const EncoderPrimitives
& ref
, const EncoderPrimitives
& opt
) = 0;
62 virtual const char *getName() const = 0;
66 /* Temporary variables for stack checks */
76 #elif defined(__GNUC__)
77 /* fallback for older GCC/MinGW */
78 static inline uint32_t __rdtsc(void)
82 asm volatile("rdtsc" : "=a" (a
) ::"edx");
86 #endif // ifdef _MSC_VER
88 #define BENCH_RUNS 1000
90 // Adapted from checkasm.c, runs each optimized primitive four times, measures rdtsc
91 // and discards invalid times. Repeats 1000 times to get a good average. Then measures
92 // the C reference with fewer runs and reports X factor and average cycles.
93 #define REPORT_SPEEDUP(RUNOPT, RUNREF, ...) \
95 uint32_t cycles = 0; int runs = 0; \
96 RUNOPT(__VA_ARGS__); \
97 for (int ti = 0; ti < BENCH_RUNS; ti++) { \
98 uint32_t t0 = (uint32_t)__rdtsc(); \
99 RUNOPT(__VA_ARGS__); \
100 RUNOPT(__VA_ARGS__); \
101 RUNOPT(__VA_ARGS__); \
102 RUNOPT(__VA_ARGS__); \
103 uint32_t t1 = (uint32_t)__rdtsc() - t0; \
104 if (t1 * runs <= cycles * 4 && ti > 0) { cycles += t1; runs++; } \
106 uint32_t refcycles = 0; int refruns = 0; \
107 RUNREF(__VA_ARGS__); \
108 for (int ti = 0; ti < BENCH_RUNS / 4; ti++) { \
109 uint32_t t0 = (uint32_t)__rdtsc(); \
110 RUNREF(__VA_ARGS__); \
111 RUNREF(__VA_ARGS__); \
112 RUNREF(__VA_ARGS__); \
113 RUNREF(__VA_ARGS__); \
114 uint32_t t1 = (uint32_t)__rdtsc() - t0; \
115 if (t1 * refruns <= refcycles * 4 && ti > 0) { refcycles += t1; refruns++; } \
118 float optperf = (10.0f * cycles / runs) / 4; \
119 float refperf = (10.0f * refcycles / refruns) / 4; \
120 printf("\t%3.2fx ", refperf / optperf); \
121 printf("\t %-8.2lf \t %-8.2lf\n", optperf, refperf); \
126 int x265_stack_pagealign(int (*func
)(), int align
);
128 /* detect when callee-saved regs aren't saved
129 * needs an explicit asm check because it only sometimes crashes in normal use. */
130 intptr_t x265_checkasm_call(intptr_t (*func
)(), int *ok
, ...);
131 float x265_checkasm_call_float(float (*func
)(), int *ok
, ...);
133 #define x265_stack_pagealign(func, align) func()
138 /* Evil hack: detect incorrect assumptions that 32-bit ints are zero-extended to 64-bit.
139 * This is done by clobbering the stack with junk around the stack pointer and calling the
140 * assembly function through x265_checkasm_call with added dummy arguments which forces all
141 * real arguments to be passed on the stack and not in registers. For 32-bit argument the
142 * upper half of the 64-bit register location on the stack will now contain junk. Note that
143 * this is dependent on compiler behavior and that interrupts etc. at the wrong time may
144 * overwrite the junk written to the stack so there's no guarantee that it will always
145 * detect all functions that assumes zero-extension.
147 void x265_checkasm_stack_clobber(uint64_t clobber
, ...);
148 #define checked(func, ...) ( \
149 m_ok = 1, m_rand = (rand() & 0xffff) * 0x0001000100010001ULL, \
150 x265_checkasm_stack_clobber(m_rand, m_rand, m_rand, m_rand, m_rand, m_rand, m_rand, m_rand, \
151 m_rand, m_rand, m_rand, m_rand, m_rand, m_rand, m_rand, m_rand, \
152 m_rand, m_rand, m_rand, m_rand, m_rand), /* max_args+6 */ \
153 x265_checkasm_call((intptr_t(*)())func, &m_ok, 0, 0, 0, 0, __VA_ARGS__))
155 #define checked_float(func, ...) ( \
156 m_ok = 1, m_rand = (rand() & 0xffff) * 0x0001000100010001ULL, \
157 x265_checkasm_stack_clobber(m_rand, m_rand, m_rand, m_rand, m_rand, m_rand, m_rand, m_rand, \
158 m_rand, m_rand, m_rand, m_rand, m_rand, m_rand, m_rand, m_rand, \
159 m_rand, m_rand, m_rand, m_rand, m_rand), /* max_args+6 */ \
160 x265_checkasm_call_float((float(*)())func, &m_ok, 0, 0, 0, 0, __VA_ARGS__))
161 #define reportfail() if (!m_ok) { fprintf(stderr, "stack clobber check failed at %s:%d", __FILE__, __LINE__); abort(); }
163 #define checked(func, ...) x265_checkasm_call((intptr_t(*)())func, &m_ok, __VA_ARGS__);
164 #define checked_float(func, ...) x265_checkasm_call_float((float(*)())func, &m_ok, __VA_ARGS__);
167 #define checked(func, ...) func(__VA_ARGS__)
168 #define checked_float(func, ...) func(__VA_ARGS__)
173 #endif // ifndef _TESTHARNESS_H_