arith.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Handy add/subtract functions to operate on chunks of shorts.
  3. * Feel free to add customizations for additional architectures
  4. *
  5. */
  6. /*
  7. * See http://www.asterisk.org for more information about
  8. * the Asterisk project. Please do not directly contact
  9. * any of the maintainers of this project for assistance;
  10. * the project provides a web site, mailing lists and IRC
  11. * channels for your use.
  12. *
  13. * This program is free software, distributed under the terms of
  14. * the GNU General Public License Version 2 as published by the
  15. * Free Software Foundation. See the LICENSE file included with
  16. * this program for more details.
  17. */
  18. #ifndef _DAHDI_ARITH_H
  19. #define _DAHDI_ARITH_H
  20. #ifdef CONFIG_DAHDI_MMX
  21. #ifdef DAHDI_CHUNKSIZE
  22. static inline void __ACSS(volatile short *dst, const short *src)
  23. {
  24. __asm__ __volatile__ (
  25. "movq 0(%0), %%mm0;\n"
  26. "movq 0(%1), %%mm1;\n"
  27. "movq 8(%0), %%mm2;\n"
  28. "movq 8(%1), %%mm3;\n"
  29. "paddsw %%mm1, %%mm0;\n"
  30. "paddsw %%mm3, %%mm2;\n"
  31. "movq %%mm0, 0(%0);\n"
  32. "movq %%mm2, 8(%0);\n"
  33. : "=r" (dst)
  34. : "r" (src), "0" (dst)
  35. : "memory"
  36. #ifdef CLOBBERMMX
  37. , "%mm0", "%mm1", "%mm2", "%mm3"
  38. #endif
  39. );
  40. }
  41. static inline void __SCSS(volatile short *dst, const short *src)
  42. {
  43. __asm__ __volatile__ (
  44. "movq 0(%0), %%mm0;\n"
  45. "movq 0(%1), %%mm1;\n"
  46. "movq 8(%0), %%mm2;\n"
  47. "movq 8(%1), %%mm3;\n"
  48. "psubsw %%mm1, %%mm0;\n"
  49. "psubsw %%mm3, %%mm2;\n"
  50. "movq %%mm0, 0(%0);\n"
  51. "movq %%mm2, 8(%0);\n"
  52. : "=r" (dst)
  53. : "r" (src), "0" (dst)
  54. : "memory"
  55. #ifdef CLOBBERMMX
  56. , "%mm0", "%mm1", "%mm2", "%mm3"
  57. #endif
  58. );
  59. }
  60. #if (DAHDI_CHUNKSIZE == 8)
  61. #define ACSS(a,b) __ACSS(a,b)
  62. #define SCSS(a,b) __SCSS(a,b)
  63. #elif (DAHDI_CHUNKSIZE > 8)
  64. static inline void ACSS(volatile short *dst, const short *src)
  65. {
  66. int x;
  67. for (x=0;x<DAHDI_CHUNKSIZE;x+=8)
  68. __ACSS(dst + x, src + x);
  69. }
  70. static inline void SCSS(volatile short *dst, const short *src)
  71. {
  72. int x;
  73. for (x=0;x<DAHDI_CHUNKSIZE;x+=8)
  74. __SCSS(dst + x, src + x);
  75. }
  76. #else
  77. #error No MMX for DAHDI_CHUNKSIZE < 8
  78. #endif
  79. #endif
  80. static inline int CONVOLVE(const int *coeffs, const short *hist, int len)
  81. {
  82. int sum;
  83. /* Divide length by 16 */
  84. len >>= 4;
  85. /* Clear our accumulator, mm4 */
  86. /*
  87. For every set of eight...
  88. Load 16 coefficients into four registers...
  89. Shift each word right 16 to make them shorts...
  90. Pack the resulting shorts into two registers...
  91. With the coefficients now in mm0 and mm2, load the
  92. history into mm1 and mm3...
  93. Multiply/add mm1 into mm0, and mm3 into mm2...
  94. Add mm2 into mm0 (without saturation, alas). Now we have two half-results.
  95. Accumulate in mm4 (again, without saturation, alas)
  96. */
  97. __asm__ (
  98. "pxor %%mm4, %%mm4;\n"
  99. "mov %1, %%edi;\n"
  100. "mov %2, %%esi;\n"
  101. "mov %3, %%ecx;\n"
  102. "1:"
  103. "movq 0(%%edi), %%mm0;\n"
  104. "movq 8(%%edi), %%mm1;\n"
  105. "movq 16(%%edi), %%mm2;\n"
  106. "movq 24(%%edi), %%mm3;\n"
  107. /* can't use 4/5 since 4 is the accumulator for us */
  108. "movq 32(%%edi), %%mm6;\n"
  109. "movq 40(%%edi), %%mm7;\n"
  110. "psrad $16, %%mm0;\n"
  111. "psrad $16, %%mm1;\n"
  112. "psrad $16, %%mm2;\n"
  113. "psrad $16, %%mm3;\n"
  114. "psrad $16, %%mm6;\n"
  115. "psrad $16, %%mm7;\n"
  116. "packssdw %%mm1, %%mm0;\n"
  117. "packssdw %%mm3, %%mm2;\n"
  118. "packssdw %%mm7, %%mm6;\n"
  119. "movq 0(%%esi), %%mm1;\n"
  120. "movq 8(%%esi), %%mm3;\n"
  121. "movq 16(%%esi), %%mm7;\n"
  122. "pmaddwd %%mm1, %%mm0;\n"
  123. "pmaddwd %%mm3, %%mm2;\n"
  124. "pmaddwd %%mm7, %%mm6;\n"
  125. "paddd %%mm6, %%mm4;\n"
  126. "paddd %%mm2, %%mm4;\n"
  127. "paddd %%mm0, %%mm4;\n"
  128. /* Come back and do for the last few bytes */
  129. "movq 48(%%edi), %%mm6;\n"
  130. "movq 56(%%edi), %%mm7;\n"
  131. "psrad $16, %%mm6;\n"
  132. "psrad $16, %%mm7;\n"
  133. "packssdw %%mm7, %%mm6;\n"
  134. "movq 24(%%esi), %%mm7;\n"
  135. "pmaddwd %%mm7, %%mm6;\n"
  136. "paddd %%mm6, %%mm4;\n"
  137. "add $64, %%edi;\n"
  138. "add $32, %%esi;\n"
  139. "dec %%ecx;\n"
  140. "jnz 1b;\n"
  141. "movq %%mm4, %%mm0;\n"
  142. "psrlq $32, %%mm0;\n"
  143. "paddd %%mm0, %%mm4;\n"
  144. "movd %%mm4, %0;\n"
  145. : "=r" (sum)
  146. : "r" (coeffs), "r" (hist), "r" (len)
  147. : "%ecx", "%edi", "%esi"
  148. );
  149. return sum;
  150. }
  151. static inline void UPDATE(volatile int *taps, const short *history, const int nsuppr, const int ntaps)
  152. {
  153. int i;
  154. int correction;
  155. for (i=0;i<ntaps;i++) {
  156. correction = history[i] * nsuppr;
  157. taps[i] += correction;
  158. }
  159. }
  160. static inline void UPDATE2(volatile int *taps, volatile short *taps_short, const short *history, const int nsuppr, const int ntaps)
  161. {
  162. int i;
  163. int correction;
  164. #if 0
  165. ntaps >>= 4;
  166. /* First, load up taps, */
  167. __asm__ (
  168. "pxor %%mm4, %%mm4;\n"
  169. "mov %0, %%edi;\n"
  170. "mov %1, %%esi;\n"
  171. "mov %3, %%ecx;\n"
  172. "1:"
  173. "jnz 1b;\n"
  174. "movq %%mm4, %%mm0;\n"
  175. "psrlq $32, %%mm0;\n"
  176. "paddd %%mm0, %%mm4;\n"
  177. "movd %%mm4, %0;\n"
  178. : "=r" (taps), "=r" (taps_short)
  179. : "r" (history), "r" (nsuppr), "r" (ntaps), "0" (taps)
  180. : "%ecx", "%edi", "%esi"
  181. );
  182. #endif
  183. #if 1
  184. for (i=0;i<ntaps;i++) {
  185. correction = history[i] * nsuppr;
  186. taps[i] += correction;
  187. taps_short[i] = taps[i] >> 16;
  188. }
  189. #endif
  190. }
  191. static inline int CONVOLVE2(const short *coeffs, const short *hist, int len)
  192. {
  193. int sum;
  194. /* Divide length by 16 */
  195. len >>= 4;
  196. /* Clear our accumulator, mm4 */
  197. /*
  198. For every set of eight...
  199. Load in eight coefficients and eight historic samples, multliply add and
  200. accumulate the result
  201. */
  202. __asm__ (
  203. "pxor %%mm4, %%mm4;\n"
  204. "mov %1, %%edi;\n"
  205. "mov %2, %%esi;\n"
  206. "mov %3, %%ecx;\n"
  207. "1:"
  208. "movq 0(%%edi), %%mm0;\n"
  209. "movq 8(%%edi), %%mm2;\n"
  210. "movq 0(%%esi), %%mm1;\n"
  211. "movq 8(%%esi), %%mm3;\n"
  212. "pmaddwd %%mm1, %%mm0;\n"
  213. "pmaddwd %%mm3, %%mm2;\n"
  214. "paddd %%mm2, %%mm4;\n"
  215. "paddd %%mm0, %%mm4;\n"
  216. "movq 16(%%edi), %%mm0;\n"
  217. "movq 24(%%edi), %%mm2;\n"
  218. "movq 16(%%esi), %%mm1;\n"
  219. "movq 24(%%esi), %%mm3;\n"
  220. "pmaddwd %%mm1, %%mm0;\n"
  221. "pmaddwd %%mm3, %%mm2;\n"
  222. "paddd %%mm2, %%mm4;\n"
  223. "paddd %%mm0, %%mm4;\n"
  224. "add $32, %%edi;\n"
  225. "add $32, %%esi;\n"
  226. "dec %%ecx;\n"
  227. "jnz 1b;\n"
  228. "movq %%mm4, %%mm0;\n"
  229. "psrlq $32, %%mm0;\n"
  230. "paddd %%mm0, %%mm4;\n"
  231. "movd %%mm4, %0;\n"
  232. : "=r" (sum)
  233. : "r" (coeffs), "r" (hist), "r" (len)
  234. : "%ecx", "%edi", "%esi"
  235. );
  236. return sum;
  237. }
  238. static inline short MAX16(const short *y, int len, int *pos)
  239. {
  240. int k;
  241. short max = 0;
  242. int bestpos = 0;
  243. for (k=0;k<len;k++) {
  244. if (max < y[k]) {
  245. bestpos = k;
  246. max = y[k];
  247. }
  248. }
  249. *pos = (len - 1 - bestpos);
  250. return max;
  251. }
  252. #else
  253. #ifdef DAHDI_CHUNKSIZE
  254. static inline void ACSS(short *dst, short *src)
  255. {
  256. int x;
  257. /* Add src to dst with saturation, storing in dst */
  258. #ifdef BFIN
  259. for (x = 0; x < DAHDI_CHUNKSIZE; x++)
  260. dst[x] = __builtin_bfin_add_fr1x16(dst[x], src[x]);
  261. #else
  262. int sum;
  263. for (x = 0; x < DAHDI_CHUNKSIZE; x++) {
  264. sum = dst[x] + src[x];
  265. if (sum > 32767)
  266. sum = 32767;
  267. else if (sum < -32768)
  268. sum = -32768;
  269. dst[x] = sum;
  270. }
  271. #endif
  272. }
  273. static inline void SCSS(short *dst, short *src)
  274. {
  275. int x;
  276. /* Subtract src from dst with saturation, storing in dst */
  277. #ifdef BFIN
  278. for (x = 0; x < DAHDI_CHUNKSIZE; x++)
  279. dst[x] = __builtin_bfin_sub_fr1x16(dst[x], src[x]);
  280. #else
  281. int sum;
  282. for (x = 0; x < DAHDI_CHUNKSIZE; x++) {
  283. sum = dst[x] - src[x];
  284. if (sum > 32767)
  285. sum = 32767;
  286. else if (sum < -32768)
  287. sum = -32768;
  288. dst[x] = sum;
  289. }
  290. #endif
  291. }
  292. #endif /* DAHDI_CHUNKSIZE */
  293. static inline int CONVOLVE(const int *coeffs, const short *hist, int len)
  294. {
  295. int x;
  296. int sum = 0;
  297. for (x=0;x<len;x++)
  298. sum += (coeffs[x] >> 16) * hist[x];
  299. return sum;
  300. }
  301. static inline int CONVOLVE2(const short *coeffs, const short *hist, int len)
  302. {
  303. int x;
  304. int sum = 0;
  305. for (x=0;x<len;x++)
  306. sum += coeffs[x] * hist[x];
  307. return sum;
  308. }
  309. static inline void UPDATE(int *taps, const short *history, const int nsuppr, const int ntaps)
  310. {
  311. int i;
  312. int correction;
  313. for (i=0;i<ntaps;i++) {
  314. correction = history[i] * nsuppr;
  315. taps[i] += correction;
  316. }
  317. }
  318. static inline void UPDATE2(int *taps, short *taps_short, const short *history, const int nsuppr, const int ntaps)
  319. {
  320. int i;
  321. int correction;
  322. for (i=0;i<ntaps;i++) {
  323. correction = history[i] * nsuppr;
  324. taps[i] += correction;
  325. taps_short[i] = taps[i] >> 16;
  326. }
  327. }
  328. static inline short MAX16(const short *y, int len, int *pos)
  329. {
  330. int k;
  331. short max = 0;
  332. int bestpos = 0;
  333. for (k=0;k<len;k++) {
  334. if (max < y[k]) {
  335. bestpos = k;
  336. max = y[k];
  337. }
  338. }
  339. *pos = (len - 1 - bestpos);
  340. return max;
  341. }
  342. #endif /* MMX */
  343. #endif /* _DAHDI_ARITH_H */