]>
code.delx.au - pulseaudio/blob - src/tests/mult-s16-test.c
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 #include "runtime-test-util.h"
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
);
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;
55 START_TEST (mult_s16_test
) {
56 int16_t samples
[SAMPLES
];
57 int32_t volumes
[SAMPLES
];
58 int32_t sum1
= 0, sum2
= 0;
61 pa_random(samples
, sizeof(samples
));
62 pa_random(volumes
, sizeof(volumes
));
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
]);
69 pa_log_debug("%d: %d != %d", i
, a
, b
);
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
]);
78 } PA_RUNTIME_TEST_RUN_STOP
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
85 fail_unless(sum1
== sum2
);
89 int main(int argc
, char *argv
[]) {
95 if (!getenv("MAKE_CHECK"))
96 pa_log_set_level(PA_LOG_DEBUG
);
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.");
103 pa_log_debug("Don't know if this is 32- or 64-bit code.");
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
);
112 sr
= srunner_create(s
);
113 srunner_run_all(sr
, CK_NORMAL
);
114 failed
= srunner_ntests_failed(sr
);
117 return (failed
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;