vector_float_tests.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * vector_float_tests.c
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2006 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. #if defined(HAVE_CONFIG_H)
  26. #include "config.h"
  27. #endif
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <fcntl.h>
  31. #include <string.h>
  32. #include "spandsp.h"
  33. static void vec_copyf_dumb(float z[], const float x[], int n)
  34. {
  35. int i;
  36. for (i = 0; i < n; i++)
  37. z[i] = x[i];
  38. }
  39. /*- End of function --------------------------------------------------------*/
  40. static int test_vec_copyf(void)
  41. {
  42. int i;
  43. float x[100];
  44. float za[100];
  45. float zb[100];
  46. printf("Testing vec_copyf()\n");
  47. for (i = 0; i < 99; i++)
  48. {
  49. x[i] = i;
  50. za[i] = -0.5f;
  51. zb[i] = -0.5f;
  52. }
  53. vec_copyf_dumb(za + 3, x + 1, 0);
  54. vec_copyf(zb + 3, x + 1, 0);
  55. for (i = 0; i < 99; i++)
  56. {
  57. if (za[i] != zb[i])
  58. {
  59. printf("vec_copyf() - %d %f %f\n", i, za[i], zb[i]);
  60. printf("Tests failed\n");
  61. exit(2);
  62. }
  63. }
  64. vec_copyf_dumb(za + 3, x + 1, 1);
  65. vec_copyf(zb + 3, x + 1, 1);
  66. for (i = 0; i < 99; i++)
  67. {
  68. if (za[i] != zb[i])
  69. {
  70. printf("vec_copyf() - %d %f %f\n", i, za[i], zb[i]);
  71. printf("Tests failed\n");
  72. exit(2);
  73. }
  74. }
  75. vec_copyf_dumb(za + 3, x + 1, 29);
  76. vec_copyf(zb + 3, x + 1, 29);
  77. for (i = 0; i < 99; i++)
  78. {
  79. if (za[i] != zb[i])
  80. {
  81. printf("vec_copyf() - %d %f %f\n", i, za[i], zb[i]);
  82. printf("Tests failed\n");
  83. exit(2);
  84. }
  85. }
  86. return 0;
  87. }
  88. /*- End of function --------------------------------------------------------*/
  89. static void vec_negatef_dumb(float z[], const float x[], int n)
  90. {
  91. int i;
  92. for (i = 0; i < n; i++)
  93. z[i] = -x[i];
  94. }
  95. /*- End of function --------------------------------------------------------*/
  96. static int test_vec_negatef(void)
  97. {
  98. int i;
  99. float x[100];
  100. float za[100];
  101. float zb[100];
  102. printf("Testing vec_negatef()\n");
  103. for (i = 0; i < 99; i++)
  104. {
  105. x[i] = i;
  106. za[i] = -0.5f;
  107. zb[i] = -0.5f;
  108. }
  109. vec_negatef_dumb(za + 3, x + 1, 0);
  110. vec_negatef(zb + 3, x + 1, 0);
  111. for (i = 0; i < 99; i++)
  112. {
  113. if (za[i] != zb[i])
  114. {
  115. printf("vec_negatef() - %d %f %f\n", i, za[i], zb[i]);
  116. printf("Tests failed\n");
  117. exit(2);
  118. }
  119. }
  120. vec_negatef_dumb(za + 3, x + 1, 1);
  121. vec_negatef(zb + 3, x + 1, 1);
  122. for (i = 0; i < 99; i++)
  123. {
  124. if (za[i] != zb[i])
  125. {
  126. printf("vec_megatef() - %d %f %f\n", i, za[i], zb[i]);
  127. printf("Tests failed\n");
  128. exit(2);
  129. }
  130. }
  131. vec_negatef_dumb(za + 3, x + 1, 29);
  132. vec_negatef(zb + 3, x + 1, 29);
  133. for (i = 0; i < 99; i++)
  134. {
  135. printf("C %d %f %f %f\n", i, x[i], za[i], zb[i]);
  136. if (za[i] != zb[i])
  137. {
  138. printf("vec_negatef() - %d %f %f\n", i, za[i], zb[i]);
  139. printf("Tests failed\n");
  140. exit(2);
  141. }
  142. }
  143. return 0;
  144. }
  145. /*- End of function --------------------------------------------------------*/
  146. static void vec_zerof_dumb(float z[], int n)
  147. {
  148. int i;
  149. for (i = 0; i < n; i++)
  150. z[i] = 0.0f;
  151. }
  152. /*- End of function --------------------------------------------------------*/
  153. static int test_vec_zerof(void)
  154. {
  155. int i;
  156. float za[100];
  157. float zb[100];
  158. printf("Testing vec_zerof()\n");
  159. for (i = 0; i < 99; i++)
  160. {
  161. za[i] = -1.0f;
  162. zb[i] = -1.0f;
  163. }
  164. vec_zerof_dumb(za + 3, 0);
  165. vec_zerof(zb + 3, 0);
  166. for (i = 0; i < 99; i++)
  167. {
  168. if (za[i] != zb[i])
  169. {
  170. printf("vec_zerof() - %d %f %f\n", i, za[i], zb[i]);
  171. printf("Tests failed\n");
  172. exit(2);
  173. }
  174. }
  175. vec_zerof_dumb(za + 3, 1);
  176. vec_zerof(zb + 3, 1);
  177. for (i = 0; i < 99; i++)
  178. {
  179. if (za[i] != zb[i])
  180. {
  181. printf("vec_zerof() - %d %f %f\n", i, za[i], zb[i]);
  182. printf("Tests failed\n");
  183. exit(2);
  184. }
  185. }
  186. vec_zerof_dumb(za + 3, 29);
  187. vec_zerof(zb + 3, 29);
  188. for (i = 0; i < 99; i++)
  189. {
  190. if (za[i] != zb[i])
  191. {
  192. printf("vec_zerof() - %d %f %f\n", i, za[i], zb[i]);
  193. printf("Tests failed\n");
  194. exit(2);
  195. }
  196. }
  197. return 0;
  198. }
  199. /*- End of function --------------------------------------------------------*/
  200. static void vec_setf_dumb(float z[], float x, int n)
  201. {
  202. int i;
  203. for (i = 0; i < n; i++)
  204. z[i] = x;
  205. }
  206. /*- End of function --------------------------------------------------------*/
  207. static int test_vec_setf(void)
  208. {
  209. int i;
  210. float za[100];
  211. float zb[100];
  212. printf("Testing vec_setf()\n");
  213. for (i = 0; i < 99; i++)
  214. {
  215. za[i] = -1.0f;
  216. zb[i] = -1.0f;
  217. }
  218. vec_setf_dumb(za + 3, 42.0f, 0);
  219. vec_setf(zb + 3, 42.0f, 0);
  220. for (i = 0; i < 99; i++)
  221. {
  222. if (za[i] != zb[i])
  223. {
  224. printf("vec_setf() - %d %f %f\n", i, za[i], zb[i]);
  225. printf("Tests failed\n");
  226. exit(2);
  227. }
  228. }
  229. vec_setf_dumb(za + 3, 42.0f, 1);
  230. vec_setf(zb + 3, 42.0f, 1);
  231. for (i = 0; i < 99; i++)
  232. {
  233. if (za[i] != zb[i])
  234. {
  235. printf("vec_setf() - %d %f %f\n", i, za[i], zb[i]);
  236. printf("Tests failed\n");
  237. exit(2);
  238. }
  239. }
  240. vec_setf_dumb(za + 3, 42.0f, 29);
  241. vec_setf(zb + 3, 42.0f, 29);
  242. for (i = 0; i < 99; i++)
  243. {
  244. if (za[i] != zb[i])
  245. {
  246. printf("vec_setf() - %d %f %f\n", i, za[i], zb[i]);
  247. printf("Tests failed\n");
  248. exit(2);
  249. }
  250. }
  251. return 0;
  252. }
  253. /*- End of function --------------------------------------------------------*/
  254. static double vec_dot_prod_dumb(const double x[], const double y[], int n)
  255. {
  256. int i;
  257. double z;
  258. z = 0.0;
  259. for (i = 0; i < n; i++)
  260. z += x[i]*y[i];
  261. return z;
  262. }
  263. /*- End of function --------------------------------------------------------*/
  264. static int test_vec_dot_prod(void)
  265. {
  266. int i;
  267. double x[100];
  268. double y[100];
  269. double zsa;
  270. double zsb;
  271. double ratio;
  272. printf("Testing vec_dot_prod()\n");
  273. for (i = 0; i < 99; i++)
  274. {
  275. x[i] = rand();
  276. y[i] = rand();
  277. }
  278. for (i = 1; i < 99; i++)
  279. {
  280. zsa = vec_dot_prod(x, y, i);
  281. zsb = vec_dot_prod_dumb(x, y, i);
  282. ratio = zsa/zsb;
  283. if (ratio < 0.9999 || ratio > 1.0001)
  284. {
  285. printf("vec_dot_prod() - %f %f\n", zsa, zsb);
  286. printf("Tests failed\n");
  287. exit(2);
  288. }
  289. }
  290. return 0;
  291. }
  292. /*- End of function --------------------------------------------------------*/
  293. static float vec_dot_prodf_dumb(const float x[], const float y[], int n)
  294. {
  295. int i;
  296. float z;
  297. z = 0.0;
  298. for (i = 0; i < n; i++)
  299. z += x[i]*y[i];
  300. return z;
  301. }
  302. /*- End of function --------------------------------------------------------*/
  303. static int test_vec_dot_prodf(void)
  304. {
  305. int i;
  306. float x[100];
  307. float y[100];
  308. float zsa;
  309. float zsb;
  310. float ratio;
  311. printf("Testing vec_dot_prodf()\n");
  312. for (i = 0; i < 99; i++)
  313. {
  314. x[i] = rand();
  315. y[i] = rand();
  316. }
  317. for (i = 1; i < 99; i++)
  318. {
  319. zsa = vec_dot_prodf(x, y, i);
  320. zsb = vec_dot_prodf_dumb(x, y, i);
  321. ratio = zsa/zsb;
  322. if (ratio < 0.9999f || ratio > 1.0001f)
  323. {
  324. printf("vec_dot_prodf() - %e %e\n", zsa, zsb);
  325. printf("Tests failed\n");
  326. exit(2);
  327. }
  328. }
  329. return 0;
  330. }
  331. /*- End of function --------------------------------------------------------*/
  332. static void vec_addf_dumb(float z[], const float x[], const float y[], int n)
  333. {
  334. int i;
  335. for (i = 0; i < n; i++)
  336. z[i] = x[i] + y[i];
  337. }
  338. /*- End of function --------------------------------------------------------*/
  339. static int test_vec_addf(void)
  340. {
  341. int i;
  342. int j;
  343. float x[100];
  344. float y[100];
  345. float zsa[100];
  346. float zsb[100];
  347. float ratio;
  348. printf("Testing vec_addf()\n");
  349. for (i = 0; i < 99; i++)
  350. {
  351. x[i] = rand();
  352. y[i] = rand();
  353. }
  354. for (i = 1; i < 90; i++)
  355. {
  356. /* Force address misalignment, to check this works OK */
  357. vec_addf(zsa + 1, x + 1, y + 1, i);
  358. vec_addf_dumb(zsb + 1, x + 1, y + 1, i);
  359. for (j = 1; j <= i; j++)
  360. {
  361. ratio = zsa[j]/zsb[j];
  362. if (ratio < 0.9999f || ratio > 1.0001f)
  363. {
  364. printf("vec_mulf() - %d %e %e\n", j, zsa[j], zsb[j]);
  365. printf("Tests failed\n");
  366. exit(2);
  367. }
  368. }
  369. }
  370. return 0;
  371. }
  372. /*- End of function --------------------------------------------------------*/
  373. static void vec_subf_dumb(float z[], const float x[], const float y[], int n)
  374. {
  375. int i;
  376. for (i = 0; i < n; i++)
  377. z[i] = x[i] - y[i];
  378. }
  379. /*- End of function --------------------------------------------------------*/
  380. static int test_vec_subf(void)
  381. {
  382. int i;
  383. int j;
  384. float x[100];
  385. float y[100];
  386. float zsa[100];
  387. float zsb[100];
  388. float ratio;
  389. printf("Testing vec_subf()\n");
  390. for (i = 0; i < 99; i++)
  391. {
  392. x[i] = rand();
  393. y[i] = rand();
  394. }
  395. for (i = 1; i < 90; i++)
  396. {
  397. /* Force address misalignment, to check this works OK */
  398. vec_subf(zsa + 1, x + 1, y + 1, i);
  399. vec_subf_dumb(zsb + 1, x + 1, y + 1, i);
  400. for (j = 1; j <= i; j++)
  401. {
  402. ratio = zsa[j]/zsb[j];
  403. if (ratio < 0.9999f || ratio > 1.0001f)
  404. {
  405. printf("vec_mulf() - %d %e %e\n", j, zsa[j], zsb[j]);
  406. printf("Tests failed\n");
  407. exit(2);
  408. }
  409. }
  410. }
  411. return 0;
  412. }
  413. /*- End of function --------------------------------------------------------*/
  414. static void vec_mulf_dumb(float z[], const float x[], const float y[], int n)
  415. {
  416. int i;
  417. for (i = 0; i < n; i++)
  418. z[i] = x[i]*y[i];
  419. }
  420. /*- End of function --------------------------------------------------------*/
  421. static int test_vec_mulf(void)
  422. {
  423. int i;
  424. int j;
  425. float x[100];
  426. float y[100];
  427. float zsa[100];
  428. float zsb[100];
  429. float ratio;
  430. printf("Testing vec_mulf()\n");
  431. for (i = 0; i < 99; i++)
  432. {
  433. x[i] = rand();
  434. y[i] = rand();
  435. }
  436. for (i = 1; i < 90; i++)
  437. {
  438. /* Force address misalignment, to check this works OK */
  439. vec_mulf(zsa + 1, x + 1, y + 1, i);
  440. vec_mulf_dumb(zsb + 1, x + 1, y + 1, i);
  441. for (j = 1; j <= i; j++)
  442. {
  443. ratio = zsa[j]/zsb[j];
  444. if (ratio < 0.9999f || ratio > 1.0001f)
  445. {
  446. printf("vec_mulf() - %d %e %e\n", j, zsa[j], zsb[j]);
  447. printf("Tests failed\n");
  448. exit(2);
  449. }
  450. }
  451. }
  452. return 0;
  453. }
  454. /*- End of function --------------------------------------------------------*/
  455. #define LMS_LEAK_RATE 0.9999f
  456. static void vec_lmsf_dumb(const float x[], float y[], int n, float error)
  457. {
  458. int i;
  459. for (i = 0; i < n; i++)
  460. {
  461. /* Leak a little to tame uncontrolled wandering */
  462. y[i] = y[i]*LMS_LEAK_RATE + x[i]*error;
  463. }
  464. }
  465. /*- End of function --------------------------------------------------------*/
  466. static int test_vec_lmsf(void)
  467. {
  468. int i;
  469. int j;
  470. float x[100];
  471. float ya[100];
  472. float yb[100];
  473. float ratio;
  474. printf("Testing vec_lmsf()\n");
  475. for (i = 0; i < 99; i++)
  476. {
  477. x[i] = rand();
  478. ya[i] =
  479. yb[i] = rand();
  480. }
  481. for (i = 1; i < 99; i++)
  482. {
  483. vec_lmsf(x, ya, i, 0.1f);
  484. vec_lmsf_dumb(x, yb, i, 0.1f);
  485. for (j = 0; j < i; j++)
  486. {
  487. ratio = ya[j]/yb[j];
  488. if (ratio < 0.9999f || ratio > 1.0001f)
  489. {
  490. printf("vec_lmsf() - %d %e %e\n", j, ya[j], yb[j]);
  491. printf("Tests failed\n");
  492. exit(2);
  493. }
  494. }
  495. }
  496. return 0;
  497. }
  498. /*- End of function --------------------------------------------------------*/
  499. static void vec_scaledxy_addf_dumb(float z[], const float x[], float x_scale, const float y[], float y_scale, int n)
  500. {
  501. int i;
  502. for (i = 0; i < n; i++)
  503. z[i] = x[i]*x_scale + y[i]*y_scale;
  504. }
  505. /*- End of function --------------------------------------------------------*/
  506. static int test_vec_scaledxy_addf(void)
  507. {
  508. int i;
  509. int j;
  510. float x[100];
  511. float y[100];
  512. float za[100];
  513. float zb[100];
  514. float ratio;
  515. printf("Testing vec_scaledxy_addf()\n");
  516. for (i = 0; i < 99; i++)
  517. {
  518. x[i] = rand();
  519. y[i] = rand();
  520. }
  521. for (i = 1; i < 99; i++)
  522. {
  523. vec_scaledxy_addf(za, x, 2.5f, y, 1.5f, i);
  524. vec_scaledxy_addf_dumb(zb, x, 2.5f, y, 1.5f, i);
  525. for (j = 0; j < i; j++)
  526. {
  527. ratio = za[j]/zb[j];
  528. if (ratio < 0.9999f || ratio > 1.0001f)
  529. {
  530. printf("vec_scaledxy_addf() - %d %e %e\n", j, za[j], zb[j]);
  531. printf("Tests failed\n");
  532. exit(2);
  533. }
  534. }
  535. }
  536. return 0;
  537. }
  538. /*- End of function --------------------------------------------------------*/
  539. static void vec_scaledy_addf_dumb(float z[], const float x[], const float y[], float y_scale, int n)
  540. {
  541. int i;
  542. for (i = 0; i < n; i++)
  543. z[i] = x[i] + y[i]*y_scale;
  544. }
  545. /*- End of function --------------------------------------------------------*/
  546. static int test_vec_scaledy_addf(void)
  547. {
  548. int i;
  549. int j;
  550. float x[100];
  551. float y[100];
  552. float za[100];
  553. float zb[100];
  554. float ratio;
  555. printf("Testing vec_scaledy_addf()\n");
  556. for (i = 0; i < 99; i++)
  557. {
  558. x[i] = rand();
  559. y[i] = rand();
  560. }
  561. for (i = 1; i < 99; i++)
  562. {
  563. vec_scaledy_addf(za, x, y, 1.5f, i);
  564. vec_scaledy_addf_dumb(zb, x, y, 1.5f, i);
  565. for (j = 0; j < i; j++)
  566. {
  567. ratio = za[j]/zb[j];
  568. if (ratio < 0.9999f || ratio > 1.0001f)
  569. {
  570. printf("vec_scaledy_addf() - %d %e %e\n", j, za[j], zb[j]);
  571. printf("Tests failed\n");
  572. exit(2);
  573. }
  574. }
  575. }
  576. return 0;
  577. }
  578. /*- End of function --------------------------------------------------------*/
  579. int main(int argc, char *argv[])
  580. {
  581. test_vec_copyf();
  582. test_vec_negatef();
  583. test_vec_zerof();
  584. test_vec_setf();
  585. test_vec_addf();
  586. test_vec_subf();
  587. test_vec_mulf();
  588. test_vec_scaledxy_addf();
  589. test_vec_scaledy_addf();
  590. test_vec_dot_prod();
  591. test_vec_dot_prodf();
  592. test_vec_lmsf();
  593. printf("Tests passed.\n");
  594. return 0;
  595. }
  596. /*- End of function --------------------------------------------------------*/
  597. /*- End of file ------------------------------------------------------------*/