2
0

saturated_tests.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * saturated_tests.c
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2004 Steve Underwood
  9. *
  10. * All rights reserved.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2, as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. /*! \page saturated_tests_page Saturated arithmetic function tests
  26. \section saturated_tests_page_sec_1 What does it do?
  27. ???.
  28. \section saturated_tests_page_sec_2 How does it work?
  29. ???.
  30. */
  31. #if defined(HAVE_CONFIG_H)
  32. #include "config.h"
  33. #endif
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <string.h>
  37. #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
  38. #include "spandsp.h"
  39. int main(int argc, char *argv[])
  40. {
  41. printf("Testing 16 bit saturation\n");
  42. if (saturate16(10000) != 10000
  43. ||
  44. saturate16(-10000) != -10000
  45. ||
  46. saturate16(32767) != 32767
  47. ||
  48. saturate16(-32768) != -32768
  49. ||
  50. saturate16(32768) != 32767
  51. ||
  52. saturate16(-32769) != -32768)
  53. {
  54. printf("Test failed.\n");
  55. exit(2);
  56. }
  57. printf("Testing 15 bit saturation\n");
  58. if (saturate15(10000) != 10000
  59. ||
  60. saturate15(-10000) != -10000
  61. ||
  62. saturate15(16383) != 16383
  63. ||
  64. saturate15(-16384) != -16384
  65. ||
  66. saturate15(16384) != 16383
  67. ||
  68. saturate15(-16385) != -16384)
  69. {
  70. printf("Test failed.\n");
  71. exit(2);
  72. }
  73. printf("Testing 16 bit unsigned saturation\n");
  74. if (saturateu16(10000) != 10000
  75. ||
  76. saturateu16(32767) != 32767
  77. ||
  78. saturateu16(65535) != 65535
  79. ||
  80. saturateu16(65536) != 65535)
  81. {
  82. printf("Test failed.\n");
  83. exit(2);
  84. }
  85. printf("Testing 8 bit unsigned saturation\n");
  86. if (saturateu8(100) != 100
  87. ||
  88. saturateu8(127) != 127
  89. ||
  90. saturateu8(255) != 255
  91. ||
  92. saturateu8(256) != 255)
  93. {
  94. printf("Test failed.\n");
  95. exit(2);
  96. }
  97. printf("Testing 16 bit saturation from float\n");
  98. if (fsaturatef(10000.0f) != 10000
  99. ||
  100. fsaturatef(-10000.0f) != -10000
  101. ||
  102. fsaturatef(32767.0f) != 32767
  103. ||
  104. fsaturatef(-32768.0f) != -32768
  105. ||
  106. fsaturatef(32768.0f) != 32767
  107. ||
  108. fsaturatef(-32769.0f) != -32768)
  109. {
  110. printf("Test failed.\n");
  111. exit(2);
  112. }
  113. printf("Testing 16 bit saturation from double\n");
  114. if (fsaturate(10000.0) != 10000
  115. ||
  116. fsaturate(-10000.0) != -10000
  117. ||
  118. fsaturate(32767.0) != 32767
  119. ||
  120. fsaturate(-32768.0) != -32768
  121. ||
  122. fsaturate(32768.0) != 32767
  123. ||
  124. fsaturate(-32769.0) != -32768)
  125. {
  126. printf("Test failed.\n");
  127. exit(2);
  128. }
  129. printf("Testing 16 bit fast saturation from float\n");
  130. if (ffastsaturatef(10000.0f) != 10000
  131. ||
  132. ffastsaturatef(-10000.0f) != -10000
  133. ||
  134. ffastsaturatef(32767.0f) != 32767
  135. ||
  136. ffastsaturatef(-32768.0f) != -32768
  137. ||
  138. ffastsaturatef(32768.0f) != 32767
  139. ||
  140. ffastsaturatef(-32769.0f) != -32768)
  141. {
  142. printf("Test failed.\n");
  143. exit(2);
  144. }
  145. printf("Testing 16 bit fast saturation from double\n");
  146. if (ffastsaturate(10000.0) != 10000
  147. ||
  148. ffastsaturate(-10000.0) != -10000
  149. ||
  150. ffastsaturate(32767.0) != 32767
  151. ||
  152. ffastsaturate(-32768.0) != -32768
  153. ||
  154. ffastsaturate(32768.0) != 32767
  155. ||
  156. ffastsaturate(-32769.0) != -32768)
  157. {
  158. printf("Test failed.\n");
  159. exit(2);
  160. }
  161. printf("Testing 16 bit float saturation from float\n");
  162. if (ffsaturatef(10000.0f) != 10000.0f
  163. ||
  164. ffsaturatef(-10000.0f) != -10000.0f
  165. ||
  166. ffsaturatef(32767.0f) != 32767.0f
  167. ||
  168. ffsaturatef(-32768.0f) != -32768.0f
  169. ||
  170. ffsaturatef(32768.0f) != 32767.0f
  171. ||
  172. ffsaturatef(-32769.0f) != -32768.0f)
  173. {
  174. printf("Test failed.\n");
  175. exit(2);
  176. }
  177. printf("Testing 16 bit double saturation from double\n");
  178. if (ffsaturate(10000.0) != 10000.0
  179. ||
  180. ffsaturate(-10000.0) != -10000.0
  181. ||
  182. ffsaturate(32767.0) != 32767.0
  183. ||
  184. ffsaturate(-32768.0) != -32768.0
  185. ||
  186. ffsaturate(32768.0) != 32767.0
  187. ||
  188. ffsaturate(-32769.0) != -32768.0)
  189. {
  190. printf("Test failed.\n");
  191. exit(2);
  192. }
  193. printf("Testing 16 bit add\n");
  194. if (sat_add16(10000, 10000) != 20000
  195. ||
  196. sat_add16(10000, -10000) != 0
  197. ||
  198. sat_add16(-10000, 10000) != 0
  199. ||
  200. sat_add16(-10000, -10000) != -20000
  201. ||
  202. sat_add16(-30000, -30000) != INT16_MIN
  203. ||
  204. sat_add16(30000, 30000) != INT16_MAX
  205. ||
  206. sat_add16(-32768, -32768) != INT16_MIN
  207. ||
  208. sat_add16(32767, 32767) != INT16_MAX)
  209. {
  210. printf("Test failed.\n");
  211. exit(2);
  212. }
  213. printf("Testing 32 bit add\n");
  214. if (sat_add32(10000, 10000) != 20000
  215. ||
  216. sat_add32(10000, -10000) != 0
  217. ||
  218. sat_add32(-10000, 10000) != 0
  219. ||
  220. sat_add32(-10000, -10000) != -20000
  221. ||
  222. sat_add32(-2000000000, -2000000000) != INT32_MIN
  223. ||
  224. sat_add32(2000000000, 2000000000) != INT32_MAX)
  225. {
  226. printf("Test failed.\n");
  227. exit(2);
  228. }
  229. printf("Testing 16 bit subtract\n");
  230. if (sat_sub16(10000, 10000) != 0
  231. ||
  232. sat_sub16(10000, -10000) != 20000
  233. ||
  234. sat_sub16(-10000, 10000) != -20000
  235. ||
  236. sat_sub16(-10000, -10000) != 0
  237. ||
  238. sat_sub16(-30000, 30000) != INT16_MIN
  239. ||
  240. sat_sub16(30000, -30000) != INT16_MAX
  241. ||
  242. sat_sub16(-32768, 32767) != INT16_MIN
  243. ||
  244. sat_sub16(32767, -32768) != INT16_MAX)
  245. {
  246. printf("Test failed.\n");
  247. exit(2);
  248. }
  249. printf("Testing 32 bit subtract\n");
  250. if (sat_sub32(10000, 10000) != 0
  251. ||
  252. sat_sub32(10000, -10000) != 20000
  253. ||
  254. sat_sub32(-10000, 10000) != -20000
  255. ||
  256. sat_sub32(-10000, -10000) != 0
  257. ||
  258. sat_sub32(-2000000000, 2000000000) != INT32_MIN
  259. ||
  260. sat_sub32(2000000000, -2000000000) != INT32_MAX)
  261. {
  262. printf("Test failed.\n");
  263. exit(2);
  264. }
  265. printf("Testing 16 x 16 => 16 bit multiply\n");
  266. if (sat_mul16(100, 100) != 0
  267. ||
  268. sat_mul16(255, 255) != 1
  269. ||
  270. sat_mul16(32767, -32768) != -32767
  271. ||
  272. sat_mul16(-32768, 32767) != -32767
  273. ||
  274. sat_mul16(32767, 32767) != 32766
  275. ||
  276. sat_mul16(-32768, -32768) != 32767)
  277. {
  278. printf("Test failed.\n");
  279. exit(2);
  280. }
  281. printf("Testing 16 x 16 => 32 bit multiply\n");
  282. if (sat_mul32_16(100, 100) != 20000
  283. ||
  284. sat_mul32_16(-100, 100) != -20000
  285. ||
  286. sat_mul32_16(32767, -32768) != -2147418112
  287. ||
  288. sat_mul32_16(-32768, 32767) != -2147418112
  289. ||
  290. sat_mul32_16(32767, 32767) != 2147352578
  291. ||
  292. sat_mul32_16(-32768, -32768) != INT32_MAX)
  293. {
  294. printf("Test failed.\n");
  295. exit(2);
  296. }
  297. printf("Testing 32 + 16 x 16 => 32 bit MAC\n");
  298. if (sat_mac32_16(123, 100, 100) != 123 + 20000
  299. ||
  300. sat_mac32_16(123, -100, 100) != 123 - 20000
  301. ||
  302. sat_mac32_16(123, 32767, -32768) != 123 - 2147418112
  303. ||
  304. sat_mac32_16(123, -32768, 32767) != 123 - 2147418112
  305. ||
  306. sat_mac32_16(123, 32767, 32767) != 123 + 2147352578
  307. ||
  308. sat_mac32_16(123, -32768, -32768) != INT32_MAX)
  309. {
  310. printf("Test failed.\n");
  311. exit(2);
  312. }
  313. printf("Testing 32 - 16 x 16 => 32 bit MSU\n");
  314. if (sat_msu32_16(123, 100, 100) != 123 - 20000
  315. ||
  316. sat_msu32_16(123, -100, 100) != 123 + 20000
  317. ||
  318. sat_msu32_16(123, 32767, -32768) != 123 + 2147418112
  319. ||
  320. sat_msu32_16(123, -32768, 32767) != 123 + 2147418112
  321. ||
  322. sat_msu32_16(123, 32767, 32767) != 123 - 2147352578
  323. ||
  324. sat_msu32_16(123, -32768, -32768) != 123 - INT32_MAX)
  325. {
  326. printf("Test failed.\n");
  327. exit(2);
  328. }
  329. printf("Testing 16 bit absolute\n");
  330. if (sat_abs16(10000) != 10000
  331. ||
  332. sat_abs16(-10000) != 10000
  333. ||
  334. sat_abs16(32767) != 32767
  335. ||
  336. sat_abs16(-32768) != 32767)
  337. {
  338. printf("Test failed.\n");
  339. exit(2);
  340. }
  341. printf("Tests passed.\n");
  342. return 0;
  343. }
  344. /*- End of function --------------------------------------------------------*/
  345. /*- End of file ------------------------------------------------------------*/