123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- /*
- * SpanDSP - a series of DSP components for telephony
- *
- * saturated_tests.c
- *
- * Written by Steve Underwood <steveu@coppice.org>
- *
- * Copyright (C) 2004 Steve Underwood
- *
- * All rights reserved.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2, as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
- /*! \page saturated_tests_page Saturated arithmetic function tests
- \section saturated_tests_page_sec_1 What does it do?
- ???.
- \section saturated_tests_page_sec_2 How does it work?
- ???.
- */
- #if defined(HAVE_CONFIG_H)
- #include "config.h"
- #endif
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
- #include "spandsp.h"
- int main(int argc, char *argv[])
- {
- printf("Testing 16 bit saturation\n");
- if (saturate16(10000) != 10000
- ||
- saturate16(-10000) != -10000
- ||
- saturate16(32767) != 32767
- ||
- saturate16(-32768) != -32768
- ||
- saturate16(32768) != 32767
- ||
- saturate16(-32769) != -32768)
- {
- printf("Test failed.\n");
- exit(2);
- }
- printf("Testing 15 bit saturation\n");
- if (saturate15(10000) != 10000
- ||
- saturate15(-10000) != -10000
- ||
- saturate15(16383) != 16383
- ||
- saturate15(-16384) != -16384
- ||
- saturate15(16384) != 16383
- ||
- saturate15(-16385) != -16384)
- {
- printf("Test failed.\n");
- exit(2);
- }
- printf("Testing 16 bit unsigned saturation\n");
- if (saturateu16(10000) != 10000
- ||
- saturateu16(32767) != 32767
- ||
- saturateu16(65535) != 65535
- ||
- saturateu16(65536) != 65535)
- {
- printf("Test failed.\n");
- exit(2);
- }
- printf("Testing 8 bit unsigned saturation\n");
- if (saturateu8(100) != 100
- ||
- saturateu8(127) != 127
- ||
- saturateu8(255) != 255
- ||
- saturateu8(256) != 255)
- {
- printf("Test failed.\n");
- exit(2);
- }
- printf("Testing 16 bit saturation from float\n");
- if (fsaturatef(10000.0f) != 10000
- ||
- fsaturatef(-10000.0f) != -10000
- ||
- fsaturatef(32767.0f) != 32767
- ||
- fsaturatef(-32768.0f) != -32768
- ||
- fsaturatef(32768.0f) != 32767
- ||
- fsaturatef(-32769.0f) != -32768)
- {
- printf("Test failed.\n");
- exit(2);
- }
- printf("Testing 16 bit saturation from double\n");
- if (fsaturate(10000.0) != 10000
- ||
- fsaturate(-10000.0) != -10000
- ||
- fsaturate(32767.0) != 32767
- ||
- fsaturate(-32768.0) != -32768
- ||
- fsaturate(32768.0) != 32767
- ||
- fsaturate(-32769.0) != -32768)
- {
- printf("Test failed.\n");
- exit(2);
- }
- printf("Testing 16 bit fast saturation from float\n");
- if (ffastsaturatef(10000.0f) != 10000
- ||
- ffastsaturatef(-10000.0f) != -10000
- ||
- ffastsaturatef(32767.0f) != 32767
- ||
- ffastsaturatef(-32768.0f) != -32768
- ||
- ffastsaturatef(32768.0f) != 32767
- ||
- ffastsaturatef(-32769.0f) != -32768)
- {
- printf("Test failed.\n");
- exit(2);
- }
- printf("Testing 16 bit fast saturation from double\n");
- if (ffastsaturate(10000.0) != 10000
- ||
- ffastsaturate(-10000.0) != -10000
- ||
- ffastsaturate(32767.0) != 32767
- ||
- ffastsaturate(-32768.0) != -32768
- ||
- ffastsaturate(32768.0) != 32767
- ||
- ffastsaturate(-32769.0) != -32768)
- {
- printf("Test failed.\n");
- exit(2);
- }
- printf("Testing 16 bit float saturation from float\n");
- if (ffsaturatef(10000.0f) != 10000.0f
- ||
- ffsaturatef(-10000.0f) != -10000.0f
- ||
- ffsaturatef(32767.0f) != 32767.0f
- ||
- ffsaturatef(-32768.0f) != -32768.0f
- ||
- ffsaturatef(32768.0f) != 32767.0f
- ||
- ffsaturatef(-32769.0f) != -32768.0f)
- {
- printf("Test failed.\n");
- exit(2);
- }
- printf("Testing 16 bit double saturation from double\n");
- if (ffsaturate(10000.0) != 10000.0
- ||
- ffsaturate(-10000.0) != -10000.0
- ||
- ffsaturate(32767.0) != 32767.0
- ||
- ffsaturate(-32768.0) != -32768.0
- ||
- ffsaturate(32768.0) != 32767.0
- ||
- ffsaturate(-32769.0) != -32768.0)
- {
- printf("Test failed.\n");
- exit(2);
- }
- printf("Testing 16 bit add\n");
- if (sat_add16(10000, 10000) != 20000
- ||
- sat_add16(10000, -10000) != 0
- ||
- sat_add16(-10000, 10000) != 0
- ||
- sat_add16(-10000, -10000) != -20000
- ||
- sat_add16(-30000, -30000) != INT16_MIN
- ||
- sat_add16(30000, 30000) != INT16_MAX
- ||
- sat_add16(-32768, -32768) != INT16_MIN
- ||
- sat_add16(32767, 32767) != INT16_MAX)
- {
- printf("Test failed.\n");
- exit(2);
- }
- printf("Testing 32 bit add\n");
- if (sat_add32(10000, 10000) != 20000
- ||
- sat_add32(10000, -10000) != 0
- ||
- sat_add32(-10000, 10000) != 0
- ||
- sat_add32(-10000, -10000) != -20000
- ||
- sat_add32(-2000000000, -2000000000) != INT32_MIN
- ||
- sat_add32(2000000000, 2000000000) != INT32_MAX)
- {
- printf("Test failed.\n");
- exit(2);
- }
- printf("Testing 16 bit subtract\n");
- if (sat_sub16(10000, 10000) != 0
- ||
- sat_sub16(10000, -10000) != 20000
- ||
- sat_sub16(-10000, 10000) != -20000
- ||
- sat_sub16(-10000, -10000) != 0
- ||
- sat_sub16(-30000, 30000) != INT16_MIN
- ||
- sat_sub16(30000, -30000) != INT16_MAX
- ||
- sat_sub16(-32768, 32767) != INT16_MIN
- ||
- sat_sub16(32767, -32768) != INT16_MAX)
- {
- printf("Test failed.\n");
- exit(2);
- }
- printf("Testing 32 bit subtract\n");
- if (sat_sub32(10000, 10000) != 0
- ||
- sat_sub32(10000, -10000) != 20000
- ||
- sat_sub32(-10000, 10000) != -20000
- ||
- sat_sub32(-10000, -10000) != 0
- ||
- sat_sub32(-2000000000, 2000000000) != INT32_MIN
- ||
- sat_sub32(2000000000, -2000000000) != INT32_MAX)
- {
- printf("Test failed.\n");
- exit(2);
- }
- printf("Testing 16 x 16 => 16 bit multiply\n");
- if (sat_mul16(100, 100) != 0
- ||
- sat_mul16(255, 255) != 1
- ||
- sat_mul16(32767, -32768) != -32767
- ||
- sat_mul16(-32768, 32767) != -32767
- ||
- sat_mul16(32767, 32767) != 32766
- ||
- sat_mul16(-32768, -32768) != 32767)
- {
- printf("Test failed.\n");
- exit(2);
- }
- printf("Testing 16 x 16 => 32 bit multiply\n");
- if (sat_mul32_16(100, 100) != 20000
- ||
- sat_mul32_16(-100, 100) != -20000
- ||
- sat_mul32_16(32767, -32768) != -2147418112
- ||
- sat_mul32_16(-32768, 32767) != -2147418112
- ||
- sat_mul32_16(32767, 32767) != 2147352578
- ||
- sat_mul32_16(-32768, -32768) != INT32_MAX)
- {
- printf("Test failed.\n");
- exit(2);
- }
- printf("Testing 32 + 16 x 16 => 32 bit MAC\n");
- if (sat_mac32_16(123, 100, 100) != 123 + 20000
- ||
- sat_mac32_16(123, -100, 100) != 123 - 20000
- ||
- sat_mac32_16(123, 32767, -32768) != 123 - 2147418112
- ||
- sat_mac32_16(123, -32768, 32767) != 123 - 2147418112
- ||
- sat_mac32_16(123, 32767, 32767) != 123 + 2147352578
- ||
- sat_mac32_16(123, -32768, -32768) != INT32_MAX)
- {
- printf("Test failed.\n");
- exit(2);
- }
- printf("Testing 32 - 16 x 16 => 32 bit MSU\n");
- if (sat_msu32_16(123, 100, 100) != 123 - 20000
- ||
- sat_msu32_16(123, -100, 100) != 123 + 20000
- ||
- sat_msu32_16(123, 32767, -32768) != 123 + 2147418112
- ||
- sat_msu32_16(123, -32768, 32767) != 123 + 2147418112
- ||
- sat_msu32_16(123, 32767, 32767) != 123 - 2147352578
- ||
- sat_msu32_16(123, -32768, -32768) != 123 - INT32_MAX)
- {
- printf("Test failed.\n");
- exit(2);
- }
- printf("Testing 16 bit absolute\n");
- if (sat_abs16(10000) != 10000
- ||
- sat_abs16(-10000) != 10000
- ||
- sat_abs16(32767) != 32767
- ||
- sat_abs16(-32768) != 32767)
- {
- printf("Test failed.\n");
- exit(2);
- }
- printf("Tests passed.\n");
- return 0;
- }
- /*- End of function --------------------------------------------------------*/
- /*- End of file ------------------------------------------------------------*/
|