resample_neon.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /* Copyright (C) 2007-2008 Jean-Marc Valin
  2. * Copyright (C) 2008 Thorvald Natvig
  3. * Copyright (C) 2011 Texas Instruments
  4. * author Jyri Sarha
  5. */
  6. /**
  7. @file resample_neon.h
  8. @brief Resampler functions (NEON version)
  9. */
  10. /*
  11. Redistribution and use in source and binary forms, with or without
  12. modification, are permitted provided that the following conditions
  13. are met:
  14. - Redistributions of source code must retain the above copyright
  15. notice, this list of conditions and the following disclaimer.
  16. - Redistributions in binary form must reproduce the above copyright
  17. notice, this list of conditions and the following disclaimer in the
  18. documentation and/or other materials provided with the distribution.
  19. - Neither the name of the Xiph.org Foundation nor the names of its
  20. contributors may be used to endorse or promote products derived from
  21. this software without specific prior written permission.
  22. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  26. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  30. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  31. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #ifdef FIXED_POINT
  35. #if defined(__aarch64__)
  36. static inline int32_t saturate_32bit_to_16bit(int32_t a) {
  37. int32_t ret;
  38. asm ("fmov s0, %w[a]\n"
  39. "sqxtn h0, s0\n"
  40. "sxtl v0.4s, v0.4h\n"
  41. "fmov %w[ret], s0\n"
  42. : [ret] "=r" (ret)
  43. : [a] "r" (a)
  44. : "v0" );
  45. return ret;
  46. }
  47. #elif defined(__thumb2__)
  48. static inline int32_t saturate_32bit_to_16bit(int32_t a) {
  49. int32_t ret;
  50. asm ("ssat %[ret], #16, %[a]"
  51. : [ret] "=r" (ret)
  52. : [a] "r" (a)
  53. : );
  54. return ret;
  55. }
  56. #else
  57. static inline int32_t saturate_32bit_to_16bit(int32_t a) {
  58. int32_t ret;
  59. asm ("vmov.s32 d0[0], %[a]\n"
  60. "vqmovn.s32 d0, q0\n"
  61. "vmov.s16 %[ret], d0[0]\n"
  62. : [ret] "=r" (ret)
  63. : [a] "r" (a)
  64. : "q0");
  65. return ret;
  66. }
  67. #endif
  68. #undef WORD2INT
  69. #define WORD2INT(x) (saturate_32bit_to_16bit(x))
  70. #define OVERRIDE_INNER_PRODUCT_SINGLE
  71. /* Only works when len % 4 == 0 and len >= 4 */
  72. #if defined(__aarch64__)
  73. static inline int32_t inner_product_single(const int16_t *a, const int16_t *b, unsigned int len)
  74. {
  75. int32_t ret;
  76. uint32_t remainder = len % 16;
  77. len = len - remainder;
  78. asm volatile (" cmp %w[len], #0\n"
  79. " b.ne 1f\n"
  80. " ld1 {v16.4h}, [%[b]], #8\n"
  81. " ld1 {v20.4h}, [%[a]], #8\n"
  82. " subs %w[remainder], %w[remainder], #4\n"
  83. " smull v0.4s, v16.4h, v20.4h\n"
  84. " b.ne 4f\n"
  85. " b 5f\n"
  86. "1:"
  87. " ld1 {v16.4h, v17.4h, v18.4h, v19.4h}, [%[b]], #32\n"
  88. " ld1 {v20.4h, v21.4h, v22.4h, v23.4h}, [%[a]], #32\n"
  89. " subs %w[len], %w[len], #16\n"
  90. " smull v0.4s, v16.4h, v20.4h\n"
  91. " smlal v0.4s, v17.4h, v21.4h\n"
  92. " smlal v0.4s, v18.4h, v22.4h\n"
  93. " smlal v0.4s, v19.4h, v23.4h\n"
  94. " b.eq 3f\n"
  95. "2:"
  96. " ld1 {v16.4h, v17.4h, v18.4h, v19.4h}, [%[b]], #32\n"
  97. " ld1 {v20.4h, v21.4h, v22.4h, v23.4h}, [%[a]], #32\n"
  98. " subs %w[len], %w[len], #16\n"
  99. " smlal v0.4s, v16.4h, v20.4h\n"
  100. " smlal v0.4s, v17.4h, v21.4h\n"
  101. " smlal v0.4s, v18.4h, v22.4h\n"
  102. " smlal v0.4s, v19.4h, v23.4h\n"
  103. " b.ne 2b\n"
  104. "3:"
  105. " cmp %w[remainder], #0\n"
  106. " b.eq 5f\n"
  107. "4:"
  108. " ld1 {v18.4h}, [%[b]], #8\n"
  109. " ld1 {v22.4h}, [%[a]], #8\n"
  110. " subs %w[remainder], %w[remainder], #4\n"
  111. " smlal v0.4s, v18.4h, v22.4h\n"
  112. " b.ne 4b\n"
  113. "5:"
  114. " saddlv d0, v0.4s\n"
  115. " sqxtn s0, d0\n"
  116. " sqrshrn h0, s0, #15\n"
  117. " sxtl v0.4s, v0.4h\n"
  118. " fmov %w[ret], s0\n"
  119. : [ret] "=r" (ret), [a] "+r" (a), [b] "+r" (b),
  120. [len] "+r" (len), [remainder] "+r" (remainder)
  121. :
  122. : "cc", "v0",
  123. "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23");
  124. return ret;
  125. }
  126. #else
  127. static inline int32_t inner_product_single(const int16_t *a, const int16_t *b, unsigned int len)
  128. {
  129. int32_t ret;
  130. uint32_t remainder = len % 16;
  131. len = len - remainder;
  132. asm volatile (" cmp %[len], #0\n"
  133. " bne 1f\n"
  134. " vld1.16 {d16}, [%[b]]!\n"
  135. " vld1.16 {d20}, [%[a]]!\n"
  136. " subs %[remainder], %[remainder], #4\n"
  137. " vmull.s16 q0, d16, d20\n"
  138. " beq 5f\n"
  139. " b 4f\n"
  140. "1:"
  141. " vld1.16 {d16, d17, d18, d19}, [%[b]]!\n"
  142. " vld1.16 {d20, d21, d22, d23}, [%[a]]!\n"
  143. " subs %[len], %[len], #16\n"
  144. " vmull.s16 q0, d16, d20\n"
  145. " vmlal.s16 q0, d17, d21\n"
  146. " vmlal.s16 q0, d18, d22\n"
  147. " vmlal.s16 q0, d19, d23\n"
  148. " beq 3f\n"
  149. "2:"
  150. " vld1.16 {d16, d17, d18, d19}, [%[b]]!\n"
  151. " vld1.16 {d20, d21, d22, d23}, [%[a]]!\n"
  152. " subs %[len], %[len], #16\n"
  153. " vmlal.s16 q0, d16, d20\n"
  154. " vmlal.s16 q0, d17, d21\n"
  155. " vmlal.s16 q0, d18, d22\n"
  156. " vmlal.s16 q0, d19, d23\n"
  157. " bne 2b\n"
  158. "3:"
  159. " cmp %[remainder], #0\n"
  160. " beq 5f\n"
  161. "4:"
  162. " vld1.16 {d16}, [%[b]]!\n"
  163. " vld1.16 {d20}, [%[a]]!\n"
  164. " subs %[remainder], %[remainder], #4\n"
  165. " vmlal.s16 q0, d16, d20\n"
  166. " bne 4b\n"
  167. "5:"
  168. " vaddl.s32 q0, d0, d1\n"
  169. " vadd.s64 d0, d0, d1\n"
  170. " vqmovn.s64 d0, q0\n"
  171. " vqrshrn.s32 d0, q0, #15\n"
  172. " vmov.s16 %[ret], d0[0]\n"
  173. : [ret] "=r" (ret), [a] "+r" (a), [b] "+r" (b),
  174. [len] "+r" (len), [remainder] "+r" (remainder)
  175. :
  176. : "cc", "q0",
  177. "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23");
  178. return ret;
  179. }
  180. #endif // !defined(__aarch64__)
  181. #elif defined(FLOATING_POINT)
  182. #if defined(__aarch64__)
  183. static inline int32_t saturate_float_to_16bit(float a) {
  184. int32_t ret;
  185. asm ("fcvtas s1, %s[a]\n"
  186. "sqxtn h1, s1\n"
  187. "sxtl v1.4s, v1.4h\n"
  188. "fmov %w[ret], s1\n"
  189. : [ret] "=r" (ret)
  190. : [a] "w" (a)
  191. : "v1");
  192. return ret;
  193. }
  194. #else
  195. static inline int32_t saturate_float_to_16bit(float a) {
  196. int32_t ret;
  197. asm ("vmov.f32 d0[0], %[a]\n"
  198. "vcvt.s32.f32 d0, d0, #15\n"
  199. "vqrshrn.s32 d0, q0, #15\n"
  200. "vmov.s16 %[ret], d0[0]\n"
  201. : [ret] "=r" (ret)
  202. : [a] "r" (a)
  203. : "q0");
  204. return ret;
  205. }
  206. #endif
  207. #undef WORD2INT
  208. #define WORD2INT(x) (saturate_float_to_16bit(x))
  209. #define OVERRIDE_INNER_PRODUCT_SINGLE
  210. /* Only works when len % 4 == 0 and len >= 4 */
  211. #if defined(__aarch64__)
  212. static inline float inner_product_single(const float *a, const float *b, unsigned int len)
  213. {
  214. float ret;
  215. uint32_t remainder = len % 16;
  216. len = len - remainder;
  217. asm volatile (" cmp %w[len], #0\n"
  218. " b.ne 1f\n"
  219. " ld1 {v16.4s}, [%[b]], #16\n"
  220. " ld1 {v20.4s}, [%[a]], #16\n"
  221. " subs %w[remainder], %w[remainder], #4\n"
  222. " fmul v1.4s, v16.4s, v20.4s\n"
  223. " b.ne 4f\n"
  224. " b 5f\n"
  225. "1:"
  226. " ld1 {v16.4s, v17.4s, v18.4s, v19.4s}, [%[b]], #64\n"
  227. " ld1 {v20.4s, v21.4s, v22.4s, v23.4s}, [%[a]], #64\n"
  228. " subs %w[len], %w[len], #16\n"
  229. " fmul v1.4s, v16.4s, v20.4s\n"
  230. " fmul v2.4s, v17.4s, v21.4s\n"
  231. " fmul v3.4s, v18.4s, v22.4s\n"
  232. " fmul v4.4s, v19.4s, v23.4s\n"
  233. " b.eq 3f\n"
  234. "2:"
  235. " ld1 {v16.4s, v17.4s, v18.4s, v19.4s}, [%[b]], #64\n"
  236. " ld1 {v20.4s, v21.4s, v22.4s, v23.4s}, [%[a]], #64\n"
  237. " subs %w[len], %w[len], #16\n"
  238. " fmla v1.4s, v16.4s, v20.4s\n"
  239. " fmla v2.4s, v17.4s, v21.4s\n"
  240. " fmla v3.4s, v18.4s, v22.4s\n"
  241. " fmla v4.4s, v19.4s, v23.4s\n"
  242. " b.ne 2b\n"
  243. "3:"
  244. " fadd v16.4s, v1.4s, v2.4s\n"
  245. " fadd v17.4s, v3.4s, v4.4s\n"
  246. " cmp %w[remainder], #0\n"
  247. " fadd v1.4s, v16.4s, v17.4s\n"
  248. " b.eq 5f\n"
  249. "4:"
  250. " ld1 {v18.4s}, [%[b]], #16\n"
  251. " ld1 {v22.4s}, [%[a]], #16\n"
  252. " subs %w[remainder], %w[remainder], #4\n"
  253. " fmla v1.4s, v18.4s, v22.4s\n"
  254. " b.ne 4b\n"
  255. "5:"
  256. " faddp v1.4s, v1.4s, v1.4s\n"
  257. " faddp %[ret].4s, v1.4s, v1.4s\n"
  258. : [ret] "=w" (ret), [a] "+r" (a), [b] "+r" (b),
  259. [len] "+r" (len), [remainder] "+r" (remainder)
  260. :
  261. : "cc", "v1", "v2", "v3", "v4",
  262. "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23");
  263. return ret;
  264. }
  265. #else
  266. static inline float inner_product_single(const float *a, const float *b, unsigned int len)
  267. {
  268. float ret;
  269. uint32_t remainder = len % 16;
  270. len = len - remainder;
  271. asm volatile (" cmp %[len], #0\n"
  272. " bne 1f\n"
  273. " vld1.32 {q4}, [%[b]]!\n"
  274. " vld1.32 {q8}, [%[a]]!\n"
  275. " subs %[remainder], %[remainder], #4\n"
  276. " vmul.f32 q0, q4, q8\n"
  277. " bne 4f\n"
  278. " b 5f\n"
  279. "1:"
  280. " vld1.32 {q4, q5}, [%[b]]!\n"
  281. " vld1.32 {q8, q9}, [%[a]]!\n"
  282. " vld1.32 {q6, q7}, [%[b]]!\n"
  283. " vld1.32 {q10, q11}, [%[a]]!\n"
  284. " subs %[len], %[len], #16\n"
  285. " vmul.f32 q0, q4, q8\n"
  286. " vmul.f32 q1, q5, q9\n"
  287. " vmul.f32 q2, q6, q10\n"
  288. " vmul.f32 q3, q7, q11\n"
  289. " beq 3f\n"
  290. "2:"
  291. " vld1.32 {q4, q5}, [%[b]]!\n"
  292. " vld1.32 {q8, q9}, [%[a]]!\n"
  293. " vld1.32 {q6, q7}, [%[b]]!\n"
  294. " vld1.32 {q10, q11}, [%[a]]!\n"
  295. " subs %[len], %[len], #16\n"
  296. " vmla.f32 q0, q4, q8\n"
  297. " vmla.f32 q1, q5, q9\n"
  298. " vmla.f32 q2, q6, q10\n"
  299. " vmla.f32 q3, q7, q11\n"
  300. " bne 2b\n"
  301. "3:"
  302. " vadd.f32 q4, q0, q1\n"
  303. " vadd.f32 q5, q2, q3\n"
  304. " cmp %[remainder], #0\n"
  305. " vadd.f32 q0, q4, q5\n"
  306. " beq 5f\n"
  307. "4:"
  308. " vld1.32 {q6}, [%[b]]!\n"
  309. " vld1.32 {q10}, [%[a]]!\n"
  310. " subs %[remainder], %[remainder], #4\n"
  311. " vmla.f32 q0, q6, q10\n"
  312. " bne 4b\n"
  313. "5:"
  314. " vadd.f32 d0, d0, d1\n"
  315. " vpadd.f32 d0, d0, d0\n"
  316. " vmov.f32 %[ret], d0[0]\n"
  317. : [ret] "=r" (ret), [a] "+r" (a), [b] "+r" (b),
  318. [len] "+l" (len), [remainder] "+l" (remainder)
  319. :
  320. : "cc", "q0", "q1", "q2", "q3",
  321. "q4", "q5", "q6", "q7", "q8", "q9", "q10", "q11");
  322. return ret;
  323. }
  324. #endif // defined(__aarch64__)
  325. #endif