vector_int_tests.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * complex_vector_int_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 int32_t vec_dot_prodi16_dumb(const int16_t x[], const int16_t y[], int n)
  34. {
  35. int32_t z;
  36. int i;
  37. z = 0;
  38. for (i = 0; i < n; i++)
  39. z += (int32_t) x[i]*(int32_t) y[i];
  40. return z;
  41. }
  42. /*- End of function --------------------------------------------------------*/
  43. static int test_vec_dot_prodi16(void)
  44. {
  45. int i;
  46. int32_t za;
  47. int32_t zb;
  48. int16_t x[99];
  49. int16_t y[99];
  50. for (i = 0; i < 99; i++)
  51. {
  52. x[i] = rand();
  53. y[i] = rand();
  54. }
  55. for (i = 1; i < 99; i++)
  56. {
  57. za = vec_dot_prodi16(x, y, i);
  58. zb = vec_dot_prodi16_dumb(x, y, i);
  59. if (za != zb)
  60. {
  61. printf("Tests failed\n");
  62. exit(2);
  63. }
  64. }
  65. return 0;
  66. }
  67. /*- End of function --------------------------------------------------------*/
  68. static int32_t vec_min_maxi16_dumb(const int16_t x[], int n, int16_t out[])
  69. {
  70. int i;
  71. int16_t min;
  72. int16_t max;
  73. int16_t temp;
  74. int32_t z;
  75. max = INT16_MIN;
  76. min = INT16_MAX;
  77. for (i = 0; i < n; i++)
  78. {
  79. temp = x[i];
  80. if (temp > max)
  81. max = temp;
  82. /*endif*/
  83. if (temp < min)
  84. min = temp;
  85. /*endif*/
  86. }
  87. /*endfor*/
  88. out[0] = max;
  89. out[1] = min;
  90. z = abs(min);
  91. if (z > max)
  92. return z;
  93. return max;
  94. }
  95. /*- End of function --------------------------------------------------------*/
  96. static int test_vec_min_maxi16(void)
  97. {
  98. int i;
  99. int32_t za;
  100. int32_t zb;
  101. int16_t x[99];
  102. int16_t outa[2];
  103. int16_t outb[2];
  104. for (i = 0; i < 99; i++)
  105. x[i] = rand();
  106. x[42] = -32768;
  107. za = vec_min_maxi16_dumb(x, 99, outa);
  108. zb = vec_min_maxi16(x, 99, outb);
  109. if (za != zb
  110. ||
  111. outa[0] != outb[0]
  112. ||
  113. outa[1] != outb[1])
  114. {
  115. printf("Tests failed\n");
  116. exit(2);
  117. }
  118. return 0;
  119. }
  120. /*- End of function --------------------------------------------------------*/
  121. static int test_vec_circular_dot_prodi16(void)
  122. {
  123. int i;
  124. int j;
  125. int pos;
  126. int len;
  127. int32_t za;
  128. int32_t zb;
  129. int16_t x[99];
  130. int16_t y[99];
  131. /* Verify that we can do circular sample buffer "dot" linear coefficient buffer
  132. operations properly, by doing two sub-dot products. */
  133. for (i = 0; i < 99; i++)
  134. {
  135. x[i] = rand();
  136. y[i] = rand();
  137. }
  138. len = 95;
  139. for (pos = 0; pos < len; pos++)
  140. {
  141. za = vec_circular_dot_prodi16(x, y, len, pos);
  142. zb = 0;
  143. for (i = 0; i < len; i++)
  144. {
  145. j = (pos + i) % len;
  146. zb += (int32_t) x[j]*(int32_t) y[i];
  147. }
  148. if (za != zb)
  149. {
  150. printf("Tests failed\n");
  151. exit(2);
  152. }
  153. }
  154. return 0;
  155. }
  156. /*- End of function --------------------------------------------------------*/
  157. int main(int argc, char *argv[])
  158. {
  159. test_vec_dot_prodi16();
  160. test_vec_min_maxi16();
  161. test_vec_circular_dot_prodi16();
  162. printf("Tests passed.\n");
  163. return 0;
  164. }
  165. /*- End of function --------------------------------------------------------*/
  166. /*- End of file ------------------------------------------------------------*/