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