xref: /aosp_15_r20/external/flac/microbench/benchmark_residual.c (revision 600f14f40d737144c998e2ec7a483122d3776fbc)
1*600f14f4SXin Li /* libFLAC - Free Lossless Audio Codec library
2*600f14f4SXin Li  * Copyright (C) 2000-2009  Josh Coalson
3*600f14f4SXin Li  * Copyright (C) 2011-2023  Xiph.Org Foundation
4*600f14f4SXin Li  *
5*600f14f4SXin Li  * Redistribution and use in source and binary forms, with or without
6*600f14f4SXin Li  * modification, are permitted provided that the following conditions
7*600f14f4SXin Li  * are met:
8*600f14f4SXin Li  *
9*600f14f4SXin Li  * - Redistributions of source code must retain the above copyright
10*600f14f4SXin Li  * notice, this list of conditions and the following disclaimer.
11*600f14f4SXin Li  *
12*600f14f4SXin Li  * - Redistributions in binary form must reproduce the above copyright
13*600f14f4SXin Li  * notice, this list of conditions and the following disclaimer in the
14*600f14f4SXin Li  * documentation and/or other materials provided with the distribution.
15*600f14f4SXin Li  *
16*600f14f4SXin Li  * - Neither the name of the Xiph.org Foundation nor the names of its
17*600f14f4SXin Li  * contributors may be used to endorse or promote products derived from
18*600f14f4SXin Li  * this software without specific prior written permission.
19*600f14f4SXin Li  *
20*600f14f4SXin Li  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*600f14f4SXin Li  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*600f14f4SXin Li  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23*600f14f4SXin Li  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
24*600f14f4SXin Li  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25*600f14f4SXin Li  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26*600f14f4SXin Li  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27*600f14f4SXin Li  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28*600f14f4SXin Li  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29*600f14f4SXin Li  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30*600f14f4SXin Li  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31*600f14f4SXin Li  */
32*600f14f4SXin Li 
33*600f14f4SXin Li #ifdef HAVE_CONFIG_H
34*600f14f4SXin Li #  include <config.h>
35*600f14f4SXin Li #endif
36*600f14f4SXin Li 
37*600f14f4SXin Li #include <stdio.h>
38*600f14f4SXin Li #include <math.h>
39*600f14f4SXin Li #include <string.h>
40*600f14f4SXin Li #include "FLAC/ordinals.h"
41*600f14f4SXin Li #include "share/compat.h"
42*600f14f4SXin Li #include "private/bitmath.h"
43*600f14f4SXin Li #include "private/fixed.h"
44*600f14f4SXin Li #include "private/macros.h"
45*600f14f4SXin Li #include "FLAC/assert.h"
46*600f14f4SXin Li 
47*600f14f4SXin Li #include "util.h"
48*600f14f4SXin Li 
FLAC__fixed_compute_residual_shift(const FLAC__int32 data[],unsigned data_len,unsigned order,FLAC__int32 residual[])49*600f14f4SXin Li static void FLAC__fixed_compute_residual_shift(const FLAC__int32 data[], unsigned data_len, unsigned order, FLAC__int32 residual[])
50*600f14f4SXin Li {
51*600f14f4SXin Li 	const int idata_len = (int) data_len;
52*600f14f4SXin Li 	int i;
53*600f14f4SXin Li 
54*600f14f4SXin Li 	switch(order) {
55*600f14f4SXin Li 		case 0:
56*600f14f4SXin Li 			FLAC__ASSERT(sizeof(residual[0]) == sizeof(data[0]));
57*600f14f4SXin Li 			memcpy(residual, data, sizeof(residual[0])*data_len);
58*600f14f4SXin Li 			break;
59*600f14f4SXin Li 		case 1:
60*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
61*600f14f4SXin Li 				residual[i] = data[i] - data[i-1];
62*600f14f4SXin Li 			break;
63*600f14f4SXin Li 		case 2:
64*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
65*600f14f4SXin Li 				residual[i] = data[i] - (data[i-1] << 1) + data[i-2];
66*600f14f4SXin Li 			break;
67*600f14f4SXin Li 		case 3:
68*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
69*600f14f4SXin Li 				residual[i] = data[i] - (((data[i-1]-data[i-2])<<1) + (data[i-1]-data[i-2])) - data[i-3];
70*600f14f4SXin Li 			break;
71*600f14f4SXin Li 		case 4:
72*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
73*600f14f4SXin Li 				residual[i] = data[i] - ((data[i-1]+data[i-3])<<2) + ((data[i-2]<<2) + (data[i-2]<<1)) + data[i-4];
74*600f14f4SXin Li 			break;
75*600f14f4SXin Li 		default:
76*600f14f4SXin Li 			FLAC__ASSERT(0);
77*600f14f4SXin Li 	}
78*600f14f4SXin Li }
79*600f14f4SXin Li 
FLAC__fixed_compute_residual_mult(const FLAC__int32 data[],unsigned data_len,unsigned order,FLAC__int32 residual[])80*600f14f4SXin Li static void FLAC__fixed_compute_residual_mult(const FLAC__int32 data[], unsigned data_len, unsigned order, FLAC__int32 residual[])
81*600f14f4SXin Li {
82*600f14f4SXin Li 	const int idata_len = (int)data_len;
83*600f14f4SXin Li 	int i;
84*600f14f4SXin Li 
85*600f14f4SXin Li 	switch(order) {
86*600f14f4SXin Li 		case 0:
87*600f14f4SXin Li 			FLAC__ASSERT(sizeof(residual[0]) == sizeof(data[0]));
88*600f14f4SXin Li 			memcpy(residual, data, sizeof(residual[0])*data_len);
89*600f14f4SXin Li 			break;
90*600f14f4SXin Li 		case 1:
91*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
92*600f14f4SXin Li 				residual[i] = data[i] - data[i-1];
93*600f14f4SXin Li 			break;
94*600f14f4SXin Li 		case 2:
95*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
96*600f14f4SXin Li 				residual[i] = data[i] - 2*data[i-1] + data[i-2];
97*600f14f4SXin Li 			break;
98*600f14f4SXin Li 		case 3:
99*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
100*600f14f4SXin Li 				residual[i] = data[i] - 3*data[i-1] + 3*data[i-2] - data[i-3];
101*600f14f4SXin Li 			break;
102*600f14f4SXin Li 		case 4:
103*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
104*600f14f4SXin Li 				residual[i] = data[i] - 4*data[i-1] + 6*data[i-2] - 4*data[i-3] + data[i-4];
105*600f14f4SXin Li 			break;
106*600f14f4SXin Li 		default:
107*600f14f4SXin Li 			FLAC__ASSERT(0);
108*600f14f4SXin Li 	}
109*600f14f4SXin Li }
110*600f14f4SXin Li 
111*600f14f4SXin Li static FLAC__int32 data [200000] ;
112*600f14f4SXin Li static FLAC__int32 residual [200000] ;
113*600f14f4SXin Li 
114*600f14f4SXin Li static unsigned bench_order = 0 ;
115*600f14f4SXin Li 
116*600f14f4SXin Li static void
bench_shift(void)117*600f14f4SXin Li bench_shift (void)
118*600f14f4SXin Li {	FLAC__fixed_compute_residual_shift (data, ARRAY_LEN (data), bench_order, residual) ;
119*600f14f4SXin Li }
120*600f14f4SXin Li 
121*600f14f4SXin Li static void
bench_mult(void)122*600f14f4SXin Li bench_mult (void)
123*600f14f4SXin Li {	FLAC__fixed_compute_residual_mult (data, ARRAY_LEN (data), bench_order, residual) ;
124*600f14f4SXin Li }
125*600f14f4SXin Li 
126*600f14f4SXin Li int
main(void)127*600f14f4SXin Li main (void)
128*600f14f4SXin Li {	bench_stats	stats ;
129*600f14f4SXin Li 
130*600f14f4SXin Li 	puts ("") ;
131*600f14f4SXin Li 
132*600f14f4SXin Li 	for (bench_order = 2 ; bench_order <= 4 ; bench_order ++) {
133*600f14f4SXin Li 		memset (&stats, 0, sizeof (stats)) ;
134*600f14f4SXin Li 		stats.testfunc = bench_shift ;
135*600f14f4SXin Li 		stats.run_count = 100 ;
136*600f14f4SXin Li 		stats.loop_count = 10 ;
137*600f14f4SXin Li 
138*600f14f4SXin Li 		benchmark_stats (&stats) ;
139*600f14f4SXin Li 		printf ("shift order %u : %f %f %f %f\n", bench_order, stats.min_time, stats.median_time, stats.mean_time, stats.max_time) ;
140*600f14f4SXin Li 
141*600f14f4SXin Li 		memset (&stats, 0, sizeof (stats)) ;
142*600f14f4SXin Li 		stats.testfunc = bench_mult ;
143*600f14f4SXin Li 		stats.run_count = 100 ;
144*600f14f4SXin Li 		stats.loop_count = 10 ;
145*600f14f4SXin Li 
146*600f14f4SXin Li 		benchmark_stats (&stats) ;
147*600f14f4SXin Li 		printf ("mult  order %u : %f %f %f %f\n\n", bench_order, stats.min_time, stats.median_time, stats.mean_time, stats.max_time) ;
148*600f14f4SXin Li 	}
149*600f14f4SXin Li 
150*600f14f4SXin Li 	return 0 ;
151*600f14f4SXin Li }
152