2 This file is part of PulseAudio.
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.
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.
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
29 #include <pulse/rtclock.h>
30 #include <pulsecore/random.h>
31 #include <pulsecore/macro.h>
33 #define PA_CPU_TEST_RUN_START(l, t1, t2) \
36 int _times = (t1), _times2 = (t2); \
37 pa_usec_t _start, _stop; \
38 pa_usec_t _min = INT_MAX, _max = 0; \
39 double _s1 = 0, _s2 = 0; \
40 const char *_label = (l); \
42 for (_k = 0; _k < _times2; _k++) { \
43 _start = pa_rtclock_now(); \
44 for (_j = 0; _j < _times; _j++)
46 #define PA_CPU_TEST_RUN_STOP \
47 _stop = pa_rtclock_now(); \
49 if (_min > (_stop - _start)) _min = _stop - _start; \
50 if (_max < (_stop - _start)) _max = _stop - _start; \
51 _s1 += _stop - _start; \
52 _s2 += (_stop - _start) * (_stop - _start); \
54 pa_log_debug("%s: %llu usec (avg: %g, min = %llu, max = %llu, stddev = %g).", _label, \
55 (long long unsigned int)_s1, \
56 ((double)_s1 / _times2), \
57 (long long unsigned int)_min, \
58 (long long unsigned int)_max, \
59 sqrt(_times2 * _s2 - _s1 * _s1) / _times2); \
62 static inline int32_t pa_mult_s16_volume_32(int16_t v, int32_t cv) {
63 /* Multiplying the 32 bit volume factor with the
64 * 16 bit sample might result in an 48 bit value. We
65 * want to do without 64 bit integers and hence do
66 * the multiplication independently for the HI and
67 * LO part of the volume. */
68 int32_t hi = cv >> 16;
69 int32_t lo = cv & 0xFFFF;
70 return ((v * lo) >> 16) + (v * hi);
73 static inline int32_t pa_mult_s16_volume_64(int16_t v, int32_t cv) {
74 /* Multiply with 64 bit integers on 64 bit platforms */
75 return (v * (int64_t) cv) >> 16;
82 START_TEST (mult_s16_test) {
83 int16_t samples[SAMPLES];
84 int32_t volumes[SAMPLES];
85 int32_t sum1 = 0, sum2 = 0;
88 pa_random(samples, sizeof(samples));
89 pa_random(volumes, sizeof(volumes));
91 for (i = 0; i < SAMPLES; i++) {
92 int32_t a = pa_mult_s16_volume_32(samples[i], volumes[i]);
93 int32_t b = pa_mult_s16_volume_64(samples[i], volumes[i]);
96 pa_log_debug("%d: %d != %d", i, a, b);
101 PA_CPU_TEST_RUN_START("32 bit mult", TIMES, TIMES2) {
102 for (i = 0; i < SAMPLES; i++) {
103 sum1 += pa_mult_s16_volume_32(samples[i], volumes[i]);
105 } PA_CPU_TEST_RUN_STOP
107 PA_CPU_TEST_RUN_START("64 bit mult", TIMES, TIMES2) {
108 for (i = 0; i < SAMPLES; i++)
109 sum2 += pa_mult_s16_volume_64(samples[i], volumes[i]);
110 } PA_CPU_TEST_RUN_STOP
112 fail_unless(sum1 == sum2);
116 int main(int argc, char *argv[]) {
122 if (!getenv("MAKE_CHECK"))
123 pa_log_set_level(PA_LOG_DEBUG);
125 #if __WORDSIZE == 64 || ((ULONG_MAX) > (UINT_MAX))
126 pa_log_debug("This seems to be 64-bit code.");
127 #elif __WORDSIZE == 32
128 pa_log_debug("This seems to be 32-bit code.");
130 pa_log_debug("Don't know if this is 32- or 64-bit code.");
133 s = suite_create("Mult-s16");
134 tc = tcase_create("mult-s16");
135 tcase_add_test(tc, mult_s16_test);
136 tcase_set_timeout(tc, 120);
137 suite_add_tcase(s, tc);
139 sr = srunner_create(s);
140 srunner_run_all(sr, CK_NORMAL);
141 failed = srunner_ntests_failed(sr);
144 return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;