]> code.delx.au - pulseaudio/blob - src/tests/mult-s16-test.c
sink-input, source-output: Add hooks for mute changes
[pulseaudio] / src / tests / mult-s16-test.c
1 /***
2 This file is part of PulseAudio.
3
4 PulseAudio is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published
6 by the Free Software Foundation; either version 2.1 of the License,
7 or (at your option) any later version.
8
9 PulseAudio is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with PulseAudio; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <check.h>
25 #include <unistd.h>
26 #include <stdlib.h>
27 #include <math.h>
28
29 #include <pulse/rtclock.h>
30 #include <pulsecore/random.h>
31 #include <pulsecore/macro.h>
32
33 #include "runtime-test-util.h"
34
35 static inline int32_t pa_mult_s16_volume_32(int16_t v, int32_t cv) {
36 /* Multiplying the 32 bit volume factor with the
37 * 16 bit sample might result in an 48 bit value. We
38 * want to do without 64 bit integers and hence do
39 * the multiplication independently for the HI and
40 * LO part of the volume. */
41 int32_t hi = cv >> 16;
42 int32_t lo = cv & 0xFFFF;
43 return ((v * lo) >> 16) + (v * hi);
44 }
45
46 static inline int32_t pa_mult_s16_volume_64(int16_t v, int32_t cv) {
47 /* Multiply with 64 bit integers on 64 bit platforms */
48 return (v * (int64_t) cv) >> 16;
49 }
50
51 #define SAMPLES 1028
52 #define TIMES 10000
53 #define TIMES2 100
54
55 START_TEST (mult_s16_test) {
56 int16_t samples[SAMPLES];
57 int32_t volumes[SAMPLES];
58 int32_t sum1 = 0, sum2 = 0;
59 int i;
60
61 pa_random(samples, sizeof(samples));
62 pa_random(volumes, sizeof(volumes));
63
64 for (i = 0; i < SAMPLES; i++) {
65 int32_t a = pa_mult_s16_volume_32(samples[i], volumes[i]);
66 int32_t b = pa_mult_s16_volume_64(samples[i], volumes[i]);
67
68 if (a != b) {
69 pa_log_debug("%d: %d != %d", i, a, b);
70 fail();
71 }
72 }
73
74 PA_RUNTIME_TEST_RUN_START("32 bit mult", TIMES, TIMES2) {
75 for (i = 0; i < SAMPLES; i++) {
76 sum1 += pa_mult_s16_volume_32(samples[i], volumes[i]);
77 }
78 } PA_RUNTIME_TEST_RUN_STOP
79
80 PA_RUNTIME_TEST_RUN_START("64 bit mult", TIMES, TIMES2) {
81 for (i = 0; i < SAMPLES; i++)
82 sum2 += pa_mult_s16_volume_64(samples[i], volumes[i]);
83 } PA_RUNTIME_TEST_RUN_STOP
84
85 fail_unless(sum1 == sum2);
86 }
87 END_TEST
88
89 int main(int argc, char *argv[]) {
90 int failed = 0;
91 Suite *s;
92 TCase *tc;
93 SRunner *sr;
94
95 if (!getenv("MAKE_CHECK"))
96 pa_log_set_level(PA_LOG_DEBUG);
97
98 #if __WORDSIZE == 64 || ((ULONG_MAX) > (UINT_MAX))
99 pa_log_debug("This seems to be 64-bit code.");
100 #elif __WORDSIZE == 32
101 pa_log_debug("This seems to be 32-bit code.");
102 #else
103 pa_log_debug("Don't know if this is 32- or 64-bit code.");
104 #endif
105
106 s = suite_create("Mult-s16");
107 tc = tcase_create("mult-s16");
108 tcase_add_test(tc, mult_s16_test);
109 tcase_set_timeout(tc, 120);
110 suite_add_tcase(s, tc);
111
112 sr = srunner_create(s);
113 srunner_run_all(sr, CK_NORMAL);
114 failed = srunner_ntests_failed(sr);
115 srunner_free(sr);
116
117 return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
118 }