10 // Generate random coefficients in the range [-15..15].
11 void gen_random_coeffs(int16_t* dst, size_t len)
13 // Standard NR LCG (we avoid rand() to get consistent behavior across platforms).
14 static uint32_t seed = 1234;
15 for (unsigned i = 0; i < len; ++i) {
16 seed = seed * 1664525U + 1013904223U;
18 dst[i] = (uint8_t)(seed >> 27) & 0x7;
20 dst[i] = -((uint8_t)(seed >> 27) & 0x7);
25 // Test that the input is pretty close to the reference for random inputs.
26 // (If the reference funtion is given in, this becomes a simple test of its
28 void test_random_inputs(idct_func_t* idct)
30 int16_t coeff[DCTSIZE2];
31 uint32_t quant[DCTSIZE2];
32 uint8_t output[DCTSIZE2];
33 uint8_t reference[DCTSIZE2];
35 // Unit quantization (ie., no scaling).
36 for (unsigned i = 0; i < DCTSIZE2; ++i) {
40 for (unsigned i = 0; i < 1000; ++i) {
41 gen_random_coeffs(coeff, DCTSIZE2);
43 (*idct)(coeff, quant, output);
44 (idct_reference)(coeff, quant, reference);
46 // Find the RMS difference.
48 for (unsigned i = 0; i < DCTSIZE2; ++i) {
49 diff_squared += (output[i] - reference[i]) * (output[i] - reference[i]);
52 assert(diff_squared <= 5);
56 // Test that a single DC coefficient becomes spread out to all blocks.
57 void test_dc_becomes_spread_out(idct_func_t* idct)
59 int16_t coeff[DCTSIZE2] = { 0 };
60 uint32_t quant[DCTSIZE2];
61 uint8_t output[DCTSIZE2];
63 // Unit quantization (ie., no scaling).
64 for (unsigned i = 0; i < DCTSIZE2; ++i) {
68 for (unsigned i = 0; i < 255*8; ++i) {
69 uint32_t reference_value = i / 8;
72 (*idct)(coeff, quant, output);
74 for (unsigned i = 0; i < DCTSIZE2; ++i) {
75 assert(abs(output[i] - reference_value) <= 1);
80 double timediff(const struct timeval* a, const struct timeval* b)
82 return (double)(b->tv_sec - a->tv_sec) +
83 (double)(b->tv_usec - a->tv_usec) * 1e-6;
86 void test_performance(idct_func_t* idct)
88 const unsigned num_runs = (idct == idct_reference) ? 5000 : 5000000;
90 int16_t coeff[DCTSIZE2];
91 uint32_t quant[DCTSIZE2];
92 uint8_t output[DCTSIZE2];
94 gen_random_coeffs(coeff, DCTSIZE2);
96 // Unit quantization (ie., no scaling).
97 for (unsigned i = 0; i < DCTSIZE2; ++i) {
101 start_benchmark_timer();
103 for (unsigned i = 0; i < num_runs; ++i) {
104 (*idct)(coeff, quant, output);
107 double diff = stop_benchmark_timer();
108 printf("%u runs in %.2f CPU seconds = %.2f IDCTs/sec\n",
109 num_runs, diff, num_runs / diff);
112 void test_all_idct(idct_func_t* idct)
114 printf(" test_dc_becomes_spread_out()\n");
115 test_dc_becomes_spread_out(idct);
117 printf(" test_random_inputs()\n");
118 test_random_inputs(idct);
120 printf(" performance test: ");
121 test_performance(idct);
126 printf("idct_reference:\n");
127 test_all_idct(idct_reference);
129 printf("idct_float:\n");
130 test_all_idct(idct_float);
132 printf("All tests pass.\n");