scale_neon.cc 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. /*
  2. * Copyright 2011 The LibYuv Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "libyuv/row.h"
  11. #ifdef __cplusplus
  12. namespace libyuv {
  13. extern "C" {
  14. #endif
  15. // This module is for GCC Neon.
  16. #if !defined(LIBYUV_DISABLE_NEON) && defined(__ARM_NEON__) && \
  17. !defined(__aarch64__)
  18. // NEON downscalers with interpolation.
  19. // Provided by Fritz Koenig
  20. // Read 32x1 throw away even pixels, and write 16x1.
  21. void ScaleRowDown2_NEON(const uint8* src_ptr,
  22. ptrdiff_t src_stride,
  23. uint8* dst,
  24. int dst_width) {
  25. (void)src_stride;
  26. asm volatile(
  27. "1: \n"
  28. // load even pixels into q0, odd into q1
  29. "vld2.8 {q0, q1}, [%0]! \n"
  30. "subs %2, %2, #16 \n" // 16 processed per loop
  31. "vst1.8 {q1}, [%1]! \n" // store odd pixels
  32. "bgt 1b \n"
  33. : "+r"(src_ptr), // %0
  34. "+r"(dst), // %1
  35. "+r"(dst_width) // %2
  36. :
  37. : "q0", "q1" // Clobber List
  38. );
  39. }
  40. // Read 32x1 average down and write 16x1.
  41. void ScaleRowDown2Linear_NEON(const uint8* src_ptr,
  42. ptrdiff_t src_stride,
  43. uint8* dst,
  44. int dst_width) {
  45. (void)src_stride;
  46. asm volatile(
  47. "1: \n"
  48. "vld2.8 {q0, q1}, [%0]! \n" // load 32 pixels
  49. "subs %2, %2, #16 \n" // 16 processed per loop
  50. "vrhadd.u8 q0, q0, q1 \n" // rounding half add
  51. "vst1.8 {q0}, [%1]! \n"
  52. "bgt 1b \n"
  53. : "+r"(src_ptr), // %0
  54. "+r"(dst), // %1
  55. "+r"(dst_width) // %2
  56. :
  57. : "q0", "q1" // Clobber List
  58. );
  59. }
  60. // Read 32x2 average down and write 16x1.
  61. void ScaleRowDown2Box_NEON(const uint8* src_ptr,
  62. ptrdiff_t src_stride,
  63. uint8* dst,
  64. int dst_width) {
  65. asm volatile(
  66. // change the stride to row 2 pointer
  67. "add %1, %0 \n"
  68. "1: \n"
  69. "vld1.8 {q0, q1}, [%0]! \n" // load row 1 and post inc
  70. "vld1.8 {q2, q3}, [%1]! \n" // load row 2 and post inc
  71. "subs %3, %3, #16 \n" // 16 processed per loop
  72. "vpaddl.u8 q0, q0 \n" // row 1 add adjacent
  73. "vpaddl.u8 q1, q1 \n"
  74. "vpadal.u8 q0, q2 \n" // row 2 add adjacent +
  75. // row1
  76. "vpadal.u8 q1, q3 \n"
  77. "vrshrn.u16 d0, q0, #2 \n" // downshift, round and
  78. // pack
  79. "vrshrn.u16 d1, q1, #2 \n"
  80. "vst1.8 {q0}, [%2]! \n"
  81. "bgt 1b \n"
  82. : "+r"(src_ptr), // %0
  83. "+r"(src_stride), // %1
  84. "+r"(dst), // %2
  85. "+r"(dst_width) // %3
  86. :
  87. : "q0", "q1", "q2", "q3" // Clobber List
  88. );
  89. }
  90. void ScaleRowDown4_NEON(const uint8* src_ptr,
  91. ptrdiff_t src_stride,
  92. uint8* dst_ptr,
  93. int dst_width) {
  94. (void)src_stride;
  95. asm volatile(
  96. "1: \n"
  97. "vld4.8 {d0, d1, d2, d3}, [%0]! \n" // src line 0
  98. "subs %2, %2, #8 \n" // 8 processed per loop
  99. "vst1.8 {d2}, [%1]! \n"
  100. "bgt 1b \n"
  101. : "+r"(src_ptr), // %0
  102. "+r"(dst_ptr), // %1
  103. "+r"(dst_width) // %2
  104. :
  105. : "q0", "q1", "memory", "cc");
  106. }
  107. void ScaleRowDown4Box_NEON(const uint8* src_ptr,
  108. ptrdiff_t src_stride,
  109. uint8* dst_ptr,
  110. int dst_width) {
  111. const uint8* src_ptr1 = src_ptr + src_stride;
  112. const uint8* src_ptr2 = src_ptr + src_stride * 2;
  113. const uint8* src_ptr3 = src_ptr + src_stride * 3;
  114. asm volatile(
  115. "1: \n"
  116. "vld1.8 {q0}, [%0]! \n" // load up 16x4
  117. "vld1.8 {q1}, [%3]! \n"
  118. "vld1.8 {q2}, [%4]! \n"
  119. "vld1.8 {q3}, [%5]! \n"
  120. "subs %2, %2, #4 \n"
  121. "vpaddl.u8 q0, q0 \n"
  122. "vpadal.u8 q0, q1 \n"
  123. "vpadal.u8 q0, q2 \n"
  124. "vpadal.u8 q0, q3 \n"
  125. "vpaddl.u16 q0, q0 \n"
  126. "vrshrn.u32 d0, q0, #4 \n" // divide by 16 w/rounding
  127. "vmovn.u16 d0, q0 \n"
  128. "vst1.32 {d0[0]}, [%1]! \n"
  129. "bgt 1b \n"
  130. : "+r"(src_ptr), // %0
  131. "+r"(dst_ptr), // %1
  132. "+r"(dst_width), // %2
  133. "+r"(src_ptr1), // %3
  134. "+r"(src_ptr2), // %4
  135. "+r"(src_ptr3) // %5
  136. :
  137. : "q0", "q1", "q2", "q3", "memory", "cc");
  138. }
  139. // Down scale from 4 to 3 pixels. Use the neon multilane read/write
  140. // to load up the every 4th pixel into a 4 different registers.
  141. // Point samples 32 pixels to 24 pixels.
  142. void ScaleRowDown34_NEON(const uint8* src_ptr,
  143. ptrdiff_t src_stride,
  144. uint8* dst_ptr,
  145. int dst_width) {
  146. (void)src_stride;
  147. asm volatile(
  148. "1: \n"
  149. "vld4.8 {d0, d1, d2, d3}, [%0]! \n" // src line 0
  150. "subs %2, %2, #24 \n"
  151. "vmov d2, d3 \n" // order d0, d1, d2
  152. "vst3.8 {d0, d1, d2}, [%1]! \n"
  153. "bgt 1b \n"
  154. : "+r"(src_ptr), // %0
  155. "+r"(dst_ptr), // %1
  156. "+r"(dst_width) // %2
  157. :
  158. : "d0", "d1", "d2", "d3", "memory", "cc");
  159. }
  160. void ScaleRowDown34_0_Box_NEON(const uint8* src_ptr,
  161. ptrdiff_t src_stride,
  162. uint8* dst_ptr,
  163. int dst_width) {
  164. asm volatile(
  165. "vmov.u8 d24, #3 \n"
  166. "add %3, %0 \n"
  167. "1: \n"
  168. "vld4.8 {d0, d1, d2, d3}, [%0]! \n" // src line 0
  169. "vld4.8 {d4, d5, d6, d7}, [%3]! \n" // src line 1
  170. "subs %2, %2, #24 \n"
  171. // filter src line 0 with src line 1
  172. // expand chars to shorts to allow for room
  173. // when adding lines together
  174. "vmovl.u8 q8, d4 \n"
  175. "vmovl.u8 q9, d5 \n"
  176. "vmovl.u8 q10, d6 \n"
  177. "vmovl.u8 q11, d7 \n"
  178. // 3 * line_0 + line_1
  179. "vmlal.u8 q8, d0, d24 \n"
  180. "vmlal.u8 q9, d1, d24 \n"
  181. "vmlal.u8 q10, d2, d24 \n"
  182. "vmlal.u8 q11, d3, d24 \n"
  183. // (3 * line_0 + line_1) >> 2
  184. "vqrshrn.u16 d0, q8, #2 \n"
  185. "vqrshrn.u16 d1, q9, #2 \n"
  186. "vqrshrn.u16 d2, q10, #2 \n"
  187. "vqrshrn.u16 d3, q11, #2 \n"
  188. // a0 = (src[0] * 3 + s[1] * 1) >> 2
  189. "vmovl.u8 q8, d1 \n"
  190. "vmlal.u8 q8, d0, d24 \n"
  191. "vqrshrn.u16 d0, q8, #2 \n"
  192. // a1 = (src[1] * 1 + s[2] * 1) >> 1
  193. "vrhadd.u8 d1, d1, d2 \n"
  194. // a2 = (src[2] * 1 + s[3] * 3) >> 2
  195. "vmovl.u8 q8, d2 \n"
  196. "vmlal.u8 q8, d3, d24 \n"
  197. "vqrshrn.u16 d2, q8, #2 \n"
  198. "vst3.8 {d0, d1, d2}, [%1]! \n"
  199. "bgt 1b \n"
  200. : "+r"(src_ptr), // %0
  201. "+r"(dst_ptr), // %1
  202. "+r"(dst_width), // %2
  203. "+r"(src_stride) // %3
  204. :
  205. : "q0", "q1", "q2", "q3", "q8", "q9", "q10", "q11", "d24", "memory",
  206. "cc");
  207. }
  208. void ScaleRowDown34_1_Box_NEON(const uint8* src_ptr,
  209. ptrdiff_t src_stride,
  210. uint8* dst_ptr,
  211. int dst_width) {
  212. asm volatile(
  213. "vmov.u8 d24, #3 \n"
  214. "add %3, %0 \n"
  215. "1: \n"
  216. "vld4.8 {d0, d1, d2, d3}, [%0]! \n" // src line 0
  217. "vld4.8 {d4, d5, d6, d7}, [%3]! \n" // src line 1
  218. "subs %2, %2, #24 \n"
  219. // average src line 0 with src line 1
  220. "vrhadd.u8 q0, q0, q2 \n"
  221. "vrhadd.u8 q1, q1, q3 \n"
  222. // a0 = (src[0] * 3 + s[1] * 1) >> 2
  223. "vmovl.u8 q3, d1 \n"
  224. "vmlal.u8 q3, d0, d24 \n"
  225. "vqrshrn.u16 d0, q3, #2 \n"
  226. // a1 = (src[1] * 1 + s[2] * 1) >> 1
  227. "vrhadd.u8 d1, d1, d2 \n"
  228. // a2 = (src[2] * 1 + s[3] * 3) >> 2
  229. "vmovl.u8 q3, d2 \n"
  230. "vmlal.u8 q3, d3, d24 \n"
  231. "vqrshrn.u16 d2, q3, #2 \n"
  232. "vst3.8 {d0, d1, d2}, [%1]! \n"
  233. "bgt 1b \n"
  234. : "+r"(src_ptr), // %0
  235. "+r"(dst_ptr), // %1
  236. "+r"(dst_width), // %2
  237. "+r"(src_stride) // %3
  238. :
  239. : "r4", "q0", "q1", "q2", "q3", "d24", "memory", "cc");
  240. }
  241. #define HAS_SCALEROWDOWN38_NEON
  242. static uvec8 kShuf38 = {0, 3, 6, 8, 11, 14, 16, 19, 22, 24, 27, 30, 0, 0, 0, 0};
  243. static uvec8 kShuf38_2 = {0, 8, 16, 2, 10, 17, 4, 12,
  244. 18, 6, 14, 19, 0, 0, 0, 0};
  245. static vec16 kMult38_Div6 = {65536 / 12, 65536 / 12, 65536 / 12, 65536 / 12,
  246. 65536 / 12, 65536 / 12, 65536 / 12, 65536 / 12};
  247. static vec16 kMult38_Div9 = {65536 / 18, 65536 / 18, 65536 / 18, 65536 / 18,
  248. 65536 / 18, 65536 / 18, 65536 / 18, 65536 / 18};
  249. // 32 -> 12
  250. void ScaleRowDown38_NEON(const uint8* src_ptr,
  251. ptrdiff_t src_stride,
  252. uint8* dst_ptr,
  253. int dst_width) {
  254. (void)src_stride;
  255. asm volatile(
  256. "vld1.8 {q3}, [%3] \n"
  257. "1: \n"
  258. "vld1.8 {d0, d1, d2, d3}, [%0]! \n"
  259. "subs %2, %2, #12 \n"
  260. "vtbl.u8 d4, {d0, d1, d2, d3}, d6 \n"
  261. "vtbl.u8 d5, {d0, d1, d2, d3}, d7 \n"
  262. "vst1.8 {d4}, [%1]! \n"
  263. "vst1.32 {d5[0]}, [%1]! \n"
  264. "bgt 1b \n"
  265. : "+r"(src_ptr), // %0
  266. "+r"(dst_ptr), // %1
  267. "+r"(dst_width) // %2
  268. : "r"(&kShuf38) // %3
  269. : "d0", "d1", "d2", "d3", "d4", "d5", "memory", "cc");
  270. }
  271. // 32x3 -> 12x1
  272. void OMITFP ScaleRowDown38_3_Box_NEON(const uint8* src_ptr,
  273. ptrdiff_t src_stride,
  274. uint8* dst_ptr,
  275. int dst_width) {
  276. const uint8* src_ptr1 = src_ptr + src_stride * 2;
  277. asm volatile(
  278. "vld1.16 {q13}, [%5] \n"
  279. "vld1.8 {q14}, [%6] \n"
  280. "vld1.8 {q15}, [%7] \n"
  281. "add %3, %0 \n"
  282. "1: \n"
  283. // d0 = 00 40 01 41 02 42 03 43
  284. // d1 = 10 50 11 51 12 52 13 53
  285. // d2 = 20 60 21 61 22 62 23 63
  286. // d3 = 30 70 31 71 32 72 33 73
  287. "vld4.8 {d0, d1, d2, d3}, [%0]! \n"
  288. "vld4.8 {d4, d5, d6, d7}, [%3]! \n"
  289. "vld4.8 {d16, d17, d18, d19}, [%4]! \n"
  290. "subs %2, %2, #12 \n"
  291. // Shuffle the input data around to get align the data
  292. // so adjacent data can be added. 0,1 - 2,3 - 4,5 - 6,7
  293. // d0 = 00 10 01 11 02 12 03 13
  294. // d1 = 40 50 41 51 42 52 43 53
  295. "vtrn.u8 d0, d1 \n"
  296. "vtrn.u8 d4, d5 \n"
  297. "vtrn.u8 d16, d17 \n"
  298. // d2 = 20 30 21 31 22 32 23 33
  299. // d3 = 60 70 61 71 62 72 63 73
  300. "vtrn.u8 d2, d3 \n"
  301. "vtrn.u8 d6, d7 \n"
  302. "vtrn.u8 d18, d19 \n"
  303. // d0 = 00+10 01+11 02+12 03+13
  304. // d2 = 40+50 41+51 42+52 43+53
  305. "vpaddl.u8 q0, q0 \n"
  306. "vpaddl.u8 q2, q2 \n"
  307. "vpaddl.u8 q8, q8 \n"
  308. // d3 = 60+70 61+71 62+72 63+73
  309. "vpaddl.u8 d3, d3 \n"
  310. "vpaddl.u8 d7, d7 \n"
  311. "vpaddl.u8 d19, d19 \n"
  312. // combine source lines
  313. "vadd.u16 q0, q2 \n"
  314. "vadd.u16 q0, q8 \n"
  315. "vadd.u16 d4, d3, d7 \n"
  316. "vadd.u16 d4, d19 \n"
  317. // dst_ptr[3] = (s[6 + st * 0] + s[7 + st * 0]
  318. // + s[6 + st * 1] + s[7 + st * 1]
  319. // + s[6 + st * 2] + s[7 + st * 2]) / 6
  320. "vqrdmulh.s16 q2, q2, q13 \n"
  321. "vmovn.u16 d4, q2 \n"
  322. // Shuffle 2,3 reg around so that 2 can be added to the
  323. // 0,1 reg and 3 can be added to the 4,5 reg. This
  324. // requires expanding from u8 to u16 as the 0,1 and 4,5
  325. // registers are already expanded. Then do transposes
  326. // to get aligned.
  327. // q2 = xx 20 xx 30 xx 21 xx 31 xx 22 xx 32 xx 23 xx 33
  328. "vmovl.u8 q1, d2 \n"
  329. "vmovl.u8 q3, d6 \n"
  330. "vmovl.u8 q9, d18 \n"
  331. // combine source lines
  332. "vadd.u16 q1, q3 \n"
  333. "vadd.u16 q1, q9 \n"
  334. // d4 = xx 20 xx 30 xx 22 xx 32
  335. // d5 = xx 21 xx 31 xx 23 xx 33
  336. "vtrn.u32 d2, d3 \n"
  337. // d4 = xx 20 xx 21 xx 22 xx 23
  338. // d5 = xx 30 xx 31 xx 32 xx 33
  339. "vtrn.u16 d2, d3 \n"
  340. // 0+1+2, 3+4+5
  341. "vadd.u16 q0, q1 \n"
  342. // Need to divide, but can't downshift as the the value
  343. // isn't a power of 2. So multiply by 65536 / n
  344. // and take the upper 16 bits.
  345. "vqrdmulh.s16 q0, q0, q15 \n"
  346. // Align for table lookup, vtbl requires registers to
  347. // be adjacent
  348. "vmov.u8 d2, d4 \n"
  349. "vtbl.u8 d3, {d0, d1, d2}, d28 \n"
  350. "vtbl.u8 d4, {d0, d1, d2}, d29 \n"
  351. "vst1.8 {d3}, [%1]! \n"
  352. "vst1.32 {d4[0]}, [%1]! \n"
  353. "bgt 1b \n"
  354. : "+r"(src_ptr), // %0
  355. "+r"(dst_ptr), // %1
  356. "+r"(dst_width), // %2
  357. "+r"(src_stride), // %3
  358. "+r"(src_ptr1) // %4
  359. : "r"(&kMult38_Div6), // %5
  360. "r"(&kShuf38_2), // %6
  361. "r"(&kMult38_Div9) // %7
  362. : "q0", "q1", "q2", "q3", "q8", "q9", "q13", "q14", "q15", "memory",
  363. "cc");
  364. }
  365. // 32x2 -> 12x1
  366. void ScaleRowDown38_2_Box_NEON(const uint8* src_ptr,
  367. ptrdiff_t src_stride,
  368. uint8* dst_ptr,
  369. int dst_width) {
  370. asm volatile(
  371. "vld1.16 {q13}, [%4] \n"
  372. "vld1.8 {q14}, [%5] \n"
  373. "add %3, %0 \n"
  374. "1: \n"
  375. // d0 = 00 40 01 41 02 42 03 43
  376. // d1 = 10 50 11 51 12 52 13 53
  377. // d2 = 20 60 21 61 22 62 23 63
  378. // d3 = 30 70 31 71 32 72 33 73
  379. "vld4.8 {d0, d1, d2, d3}, [%0]! \n"
  380. "vld4.8 {d4, d5, d6, d7}, [%3]! \n"
  381. "subs %2, %2, #12 \n"
  382. // Shuffle the input data around to get align the data
  383. // so adjacent data can be added. 0,1 - 2,3 - 4,5 - 6,7
  384. // d0 = 00 10 01 11 02 12 03 13
  385. // d1 = 40 50 41 51 42 52 43 53
  386. "vtrn.u8 d0, d1 \n"
  387. "vtrn.u8 d4, d5 \n"
  388. // d2 = 20 30 21 31 22 32 23 33
  389. // d3 = 60 70 61 71 62 72 63 73
  390. "vtrn.u8 d2, d3 \n"
  391. "vtrn.u8 d6, d7 \n"
  392. // d0 = 00+10 01+11 02+12 03+13
  393. // d2 = 40+50 41+51 42+52 43+53
  394. "vpaddl.u8 q0, q0 \n"
  395. "vpaddl.u8 q2, q2 \n"
  396. // d3 = 60+70 61+71 62+72 63+73
  397. "vpaddl.u8 d3, d3 \n"
  398. "vpaddl.u8 d7, d7 \n"
  399. // combine source lines
  400. "vadd.u16 q0, q2 \n"
  401. "vadd.u16 d4, d3, d7 \n"
  402. // dst_ptr[3] = (s[6] + s[7] + s[6+st] + s[7+st]) / 4
  403. "vqrshrn.u16 d4, q2, #2 \n"
  404. // Shuffle 2,3 reg around so that 2 can be added to the
  405. // 0,1 reg and 3 can be added to the 4,5 reg. This
  406. // requires expanding from u8 to u16 as the 0,1 and 4,5
  407. // registers are already expanded. Then do transposes
  408. // to get aligned.
  409. // q2 = xx 20 xx 30 xx 21 xx 31 xx 22 xx 32 xx 23 xx 33
  410. "vmovl.u8 q1, d2 \n"
  411. "vmovl.u8 q3, d6 \n"
  412. // combine source lines
  413. "vadd.u16 q1, q3 \n"
  414. // d4 = xx 20 xx 30 xx 22 xx 32
  415. // d5 = xx 21 xx 31 xx 23 xx 33
  416. "vtrn.u32 d2, d3 \n"
  417. // d4 = xx 20 xx 21 xx 22 xx 23
  418. // d5 = xx 30 xx 31 xx 32 xx 33
  419. "vtrn.u16 d2, d3 \n"
  420. // 0+1+2, 3+4+5
  421. "vadd.u16 q0, q1 \n"
  422. // Need to divide, but can't downshift as the the value
  423. // isn't a power of 2. So multiply by 65536 / n
  424. // and take the upper 16 bits.
  425. "vqrdmulh.s16 q0, q0, q13 \n"
  426. // Align for table lookup, vtbl requires registers to
  427. // be adjacent
  428. "vmov.u8 d2, d4 \n"
  429. "vtbl.u8 d3, {d0, d1, d2}, d28 \n"
  430. "vtbl.u8 d4, {d0, d1, d2}, d29 \n"
  431. "vst1.8 {d3}, [%1]! \n"
  432. "vst1.32 {d4[0]}, [%1]! \n"
  433. "bgt 1b \n"
  434. : "+r"(src_ptr), // %0
  435. "+r"(dst_ptr), // %1
  436. "+r"(dst_width), // %2
  437. "+r"(src_stride) // %3
  438. : "r"(&kMult38_Div6), // %4
  439. "r"(&kShuf38_2) // %5
  440. : "q0", "q1", "q2", "q3", "q13", "q14", "memory", "cc");
  441. }
  442. void ScaleAddRows_NEON(const uint8* src_ptr,
  443. ptrdiff_t src_stride,
  444. uint16* dst_ptr,
  445. int src_width,
  446. int src_height) {
  447. const uint8* src_tmp;
  448. asm volatile(
  449. "1: \n"
  450. "mov %0, %1 \n"
  451. "mov r12, %5 \n"
  452. "veor q2, q2, q2 \n"
  453. "veor q3, q3, q3 \n"
  454. "2: \n"
  455. // load 16 pixels into q0
  456. "vld1.8 {q0}, [%0], %3 \n"
  457. "vaddw.u8 q3, q3, d1 \n"
  458. "vaddw.u8 q2, q2, d0 \n"
  459. "subs r12, r12, #1 \n"
  460. "bgt 2b \n"
  461. "vst1.16 {q2, q3}, [%2]! \n" // store pixels
  462. "add %1, %1, #16 \n"
  463. "subs %4, %4, #16 \n" // 16 processed per loop
  464. "bgt 1b \n"
  465. : "=&r"(src_tmp), // %0
  466. "+r"(src_ptr), // %1
  467. "+r"(dst_ptr), // %2
  468. "+r"(src_stride), // %3
  469. "+r"(src_width), // %4
  470. "+r"(src_height) // %5
  471. :
  472. : "memory", "cc", "r12", "q0", "q1", "q2", "q3" // Clobber List
  473. );
  474. }
  475. // TODO(Yang Zhang): Investigate less load instructions for
  476. // the x/dx stepping
  477. #define LOAD2_DATA8_LANE(n) \
  478. "lsr %5, %3, #16 \n" \
  479. "add %6, %1, %5 \n" \
  480. "add %3, %3, %4 \n" \
  481. "vld2.8 {d6[" #n "], d7[" #n "]}, [%6] \n"
  482. // The NEON version mimics this formula (from row_common.cc):
  483. // #define BLENDER(a, b, f) (uint8)((int)(a) +
  484. // ((((int)((f)) * ((int)(b) - (int)(a))) + 0x8000) >> 16))
  485. void ScaleFilterCols_NEON(uint8* dst_ptr,
  486. const uint8* src_ptr,
  487. int dst_width,
  488. int x,
  489. int dx) {
  490. int dx_offset[4] = {0, 1, 2, 3};
  491. int* tmp = dx_offset;
  492. const uint8* src_tmp = src_ptr;
  493. asm volatile (
  494. "vdup.32 q0, %3 \n" // x
  495. "vdup.32 q1, %4 \n" // dx
  496. "vld1.32 {q2}, [%5] \n" // 0 1 2 3
  497. "vshl.i32 q3, q1, #2 \n" // 4 * dx
  498. "vmul.s32 q1, q1, q2 \n"
  499. // x , x + 1 * dx, x + 2 * dx, x + 3 * dx
  500. "vadd.s32 q1, q1, q0 \n"
  501. // x + 4 * dx, x + 5 * dx, x + 6 * dx, x + 7 * dx
  502. "vadd.s32 q2, q1, q3 \n"
  503. "vshl.i32 q0, q3, #1 \n" // 8 * dx
  504. "1: \n"
  505. LOAD2_DATA8_LANE(0)
  506. LOAD2_DATA8_LANE(1)
  507. LOAD2_DATA8_LANE(2)
  508. LOAD2_DATA8_LANE(3)
  509. LOAD2_DATA8_LANE(4)
  510. LOAD2_DATA8_LANE(5)
  511. LOAD2_DATA8_LANE(6)
  512. LOAD2_DATA8_LANE(7)
  513. "vmov q10, q1 \n"
  514. "vmov q11, q2 \n"
  515. "vuzp.16 q10, q11 \n"
  516. "vmovl.u8 q8, d6 \n"
  517. "vmovl.u8 q9, d7 \n"
  518. "vsubl.s16 q11, d18, d16 \n"
  519. "vsubl.s16 q12, d19, d17 \n"
  520. "vmovl.u16 q13, d20 \n"
  521. "vmovl.u16 q10, d21 \n"
  522. "vmul.s32 q11, q11, q13 \n"
  523. "vmul.s32 q12, q12, q10 \n"
  524. "vrshrn.s32 d18, q11, #16 \n"
  525. "vrshrn.s32 d19, q12, #16 \n"
  526. "vadd.s16 q8, q8, q9 \n"
  527. "vmovn.s16 d6, q8 \n"
  528. "vst1.8 {d6}, [%0]! \n" // store pixels
  529. "vadd.s32 q1, q1, q0 \n"
  530. "vadd.s32 q2, q2, q0 \n"
  531. "subs %2, %2, #8 \n" // 8 processed per loop
  532. "bgt 1b \n"
  533. : "+r"(dst_ptr), // %0
  534. "+r"(src_ptr), // %1
  535. "+r"(dst_width), // %2
  536. "+r"(x), // %3
  537. "+r"(dx), // %4
  538. "+r"(tmp), // %5
  539. "+r"(src_tmp) // %6
  540. :
  541. : "memory", "cc", "q0", "q1", "q2", "q3",
  542. "q8", "q9", "q10", "q11", "q12", "q13"
  543. );
  544. }
  545. #undef LOAD2_DATA8_LANE
  546. // 16x2 -> 16x1
  547. void ScaleFilterRows_NEON(uint8* dst_ptr,
  548. const uint8* src_ptr,
  549. ptrdiff_t src_stride,
  550. int dst_width,
  551. int source_y_fraction) {
  552. asm volatile(
  553. "cmp %4, #0 \n"
  554. "beq 100f \n"
  555. "add %2, %1 \n"
  556. "cmp %4, #64 \n"
  557. "beq 75f \n"
  558. "cmp %4, #128 \n"
  559. "beq 50f \n"
  560. "cmp %4, #192 \n"
  561. "beq 25f \n"
  562. "vdup.8 d5, %4 \n"
  563. "rsb %4, #256 \n"
  564. "vdup.8 d4, %4 \n"
  565. // General purpose row blend.
  566. "1: \n"
  567. "vld1.8 {q0}, [%1]! \n"
  568. "vld1.8 {q1}, [%2]! \n"
  569. "subs %3, %3, #16 \n"
  570. "vmull.u8 q13, d0, d4 \n"
  571. "vmull.u8 q14, d1, d4 \n"
  572. "vmlal.u8 q13, d2, d5 \n"
  573. "vmlal.u8 q14, d3, d5 \n"
  574. "vrshrn.u16 d0, q13, #8 \n"
  575. "vrshrn.u16 d1, q14, #8 \n"
  576. "vst1.8 {q0}, [%0]! \n"
  577. "bgt 1b \n"
  578. "b 99f \n"
  579. // Blend 25 / 75.
  580. "25: \n"
  581. "vld1.8 {q0}, [%1]! \n"
  582. "vld1.8 {q1}, [%2]! \n"
  583. "subs %3, %3, #16 \n"
  584. "vrhadd.u8 q0, q1 \n"
  585. "vrhadd.u8 q0, q1 \n"
  586. "vst1.8 {q0}, [%0]! \n"
  587. "bgt 25b \n"
  588. "b 99f \n"
  589. // Blend 50 / 50.
  590. "50: \n"
  591. "vld1.8 {q0}, [%1]! \n"
  592. "vld1.8 {q1}, [%2]! \n"
  593. "subs %3, %3, #16 \n"
  594. "vrhadd.u8 q0, q1 \n"
  595. "vst1.8 {q0}, [%0]! \n"
  596. "bgt 50b \n"
  597. "b 99f \n"
  598. // Blend 75 / 25.
  599. "75: \n"
  600. "vld1.8 {q1}, [%1]! \n"
  601. "vld1.8 {q0}, [%2]! \n"
  602. "subs %3, %3, #16 \n"
  603. "vrhadd.u8 q0, q1 \n"
  604. "vrhadd.u8 q0, q1 \n"
  605. "vst1.8 {q0}, [%0]! \n"
  606. "bgt 75b \n"
  607. "b 99f \n"
  608. // Blend 100 / 0 - Copy row unchanged.
  609. "100: \n"
  610. "vld1.8 {q0}, [%1]! \n"
  611. "subs %3, %3, #16 \n"
  612. "vst1.8 {q0}, [%0]! \n"
  613. "bgt 100b \n"
  614. "99: \n"
  615. "vst1.8 {d1[7]}, [%0] \n"
  616. : "+r"(dst_ptr), // %0
  617. "+r"(src_ptr), // %1
  618. "+r"(src_stride), // %2
  619. "+r"(dst_width), // %3
  620. "+r"(source_y_fraction) // %4
  621. :
  622. : "q0", "q1", "d4", "d5", "q13", "q14", "memory", "cc");
  623. }
  624. void ScaleARGBRowDown2_NEON(const uint8* src_ptr,
  625. ptrdiff_t src_stride,
  626. uint8* dst,
  627. int dst_width) {
  628. (void)src_stride;
  629. asm volatile(
  630. "1: \n"
  631. "vld4.32 {d0, d2, d4, d6}, [%0]! \n" // load 8 ARGB pixels.
  632. "vld4.32 {d1, d3, d5, d7}, [%0]! \n" // load next 8 ARGB
  633. "subs %2, %2, #8 \n" // 8 processed per loop
  634. "vmov q2, q1 \n" // load next 8 ARGB
  635. "vst2.32 {q2, q3}, [%1]! \n" // store odd pixels
  636. "bgt 1b \n"
  637. : "+r"(src_ptr), // %0
  638. "+r"(dst), // %1
  639. "+r"(dst_width) // %2
  640. :
  641. : "memory", "cc", "q0", "q1", "q2", "q3" // Clobber List
  642. );
  643. }
  644. // 46: f964 018d vld4.32 {d16,d18,d20,d22}, [r4]!
  645. // 4a: 3e04 subs r6, #4
  646. // 4c: f964 118d vld4.32 {d17,d19,d21,d23}, [r4]!
  647. // 50: ef64 21f4 vorr q9, q10, q10
  648. // 54: f942 038d vst2.32 {d16-d19}, [r2]!
  649. // 58: d1f5 bne.n 46 <ScaleARGBRowDown2_C+0x46>
  650. void ScaleARGBRowDown2Linear_NEON(const uint8* src_argb,
  651. ptrdiff_t src_stride,
  652. uint8* dst_argb,
  653. int dst_width) {
  654. (void)src_stride;
  655. asm volatile(
  656. "1: \n"
  657. "vld4.32 {d0, d2, d4, d6}, [%0]! \n" // load 8 ARGB pixels.
  658. "vld4.32 {d1, d3, d5, d7}, [%0]! \n" // load next 8 ARGB
  659. "subs %2, %2, #8 \n" // 8 processed per loop
  660. "vrhadd.u8 q0, q0, q1 \n" // rounding half add
  661. "vrhadd.u8 q1, q2, q3 \n" // rounding half add
  662. "vst2.32 {q0, q1}, [%1]! \n"
  663. "bgt 1b \n"
  664. : "+r"(src_argb), // %0
  665. "+r"(dst_argb), // %1
  666. "+r"(dst_width) // %2
  667. :
  668. : "memory", "cc", "q0", "q1", "q2", "q3" // Clobber List
  669. );
  670. }
  671. void ScaleARGBRowDown2Box_NEON(const uint8* src_ptr,
  672. ptrdiff_t src_stride,
  673. uint8* dst,
  674. int dst_width) {
  675. asm volatile(
  676. // change the stride to row 2 pointer
  677. "add %1, %1, %0 \n"
  678. "1: \n"
  679. "vld4.8 {d0, d2, d4, d6}, [%0]! \n" // load 8 ARGB pixels.
  680. "vld4.8 {d1, d3, d5, d7}, [%0]! \n" // load next 8 ARGB
  681. "subs %3, %3, #8 \n" // 8 processed per loop.
  682. "vpaddl.u8 q0, q0 \n" // B 16 bytes -> 8 shorts.
  683. "vpaddl.u8 q1, q1 \n" // G 16 bytes -> 8 shorts.
  684. "vpaddl.u8 q2, q2 \n" // R 16 bytes -> 8 shorts.
  685. "vpaddl.u8 q3, q3 \n" // A 16 bytes -> 8 shorts.
  686. "vld4.8 {d16, d18, d20, d22}, [%1]! \n" // load 8 more ARGB
  687. "vld4.8 {d17, d19, d21, d23}, [%1]! \n" // load last 8 ARGB
  688. "vpadal.u8 q0, q8 \n" // B 16 bytes -> 8 shorts.
  689. "vpadal.u8 q1, q9 \n" // G 16 bytes -> 8 shorts.
  690. "vpadal.u8 q2, q10 \n" // R 16 bytes -> 8 shorts.
  691. "vpadal.u8 q3, q11 \n" // A 16 bytes -> 8 shorts.
  692. "vrshrn.u16 d0, q0, #2 \n" // round and pack to bytes
  693. "vrshrn.u16 d1, q1, #2 \n"
  694. "vrshrn.u16 d2, q2, #2 \n"
  695. "vrshrn.u16 d3, q3, #2 \n"
  696. "vst4.8 {d0, d1, d2, d3}, [%2]! \n"
  697. "bgt 1b \n"
  698. : "+r"(src_ptr), // %0
  699. "+r"(src_stride), // %1
  700. "+r"(dst), // %2
  701. "+r"(dst_width) // %3
  702. :
  703. : "memory", "cc", "q0", "q1", "q2", "q3", "q8", "q9", "q10", "q11");
  704. }
  705. // Reads 4 pixels at a time.
  706. // Alignment requirement: src_argb 4 byte aligned.
  707. void ScaleARGBRowDownEven_NEON(const uint8* src_argb,
  708. ptrdiff_t src_stride,
  709. int src_stepx,
  710. uint8* dst_argb,
  711. int dst_width) {
  712. (void)src_stride;
  713. asm volatile(
  714. "mov r12, %3, lsl #2 \n"
  715. "1: \n"
  716. "vld1.32 {d0[0]}, [%0], r12 \n"
  717. "vld1.32 {d0[1]}, [%0], r12 \n"
  718. "vld1.32 {d1[0]}, [%0], r12 \n"
  719. "vld1.32 {d1[1]}, [%0], r12 \n"
  720. "subs %2, %2, #4 \n" // 4 pixels per loop.
  721. "vst1.8 {q0}, [%1]! \n"
  722. "bgt 1b \n"
  723. : "+r"(src_argb), // %0
  724. "+r"(dst_argb), // %1
  725. "+r"(dst_width) // %2
  726. : "r"(src_stepx) // %3
  727. : "memory", "cc", "r12", "q0");
  728. }
  729. // Reads 4 pixels at a time.
  730. // Alignment requirement: src_argb 4 byte aligned.
  731. void ScaleARGBRowDownEvenBox_NEON(const uint8* src_argb,
  732. ptrdiff_t src_stride,
  733. int src_stepx,
  734. uint8* dst_argb,
  735. int dst_width) {
  736. asm volatile(
  737. "mov r12, %4, lsl #2 \n"
  738. "add %1, %1, %0 \n"
  739. "1: \n"
  740. "vld1.8 {d0}, [%0], r12 \n" // 4 2x2 blocks -> 2x1
  741. "vld1.8 {d1}, [%1], r12 \n"
  742. "vld1.8 {d2}, [%0], r12 \n"
  743. "vld1.8 {d3}, [%1], r12 \n"
  744. "vld1.8 {d4}, [%0], r12 \n"
  745. "vld1.8 {d5}, [%1], r12 \n"
  746. "vld1.8 {d6}, [%0], r12 \n"
  747. "vld1.8 {d7}, [%1], r12 \n"
  748. "vaddl.u8 q0, d0, d1 \n"
  749. "vaddl.u8 q1, d2, d3 \n"
  750. "vaddl.u8 q2, d4, d5 \n"
  751. "vaddl.u8 q3, d6, d7 \n"
  752. "vswp.8 d1, d2 \n" // ab_cd -> ac_bd
  753. "vswp.8 d5, d6 \n" // ef_gh -> eg_fh
  754. "vadd.u16 q0, q0, q1 \n" // (a+b)_(c+d)
  755. "vadd.u16 q2, q2, q3 \n" // (e+f)_(g+h)
  756. "vrshrn.u16 d0, q0, #2 \n" // first 2 pixels.
  757. "vrshrn.u16 d1, q2, #2 \n" // next 2 pixels.
  758. "subs %3, %3, #4 \n" // 4 pixels per loop.
  759. "vst1.8 {q0}, [%2]! \n"
  760. "bgt 1b \n"
  761. : "+r"(src_argb), // %0
  762. "+r"(src_stride), // %1
  763. "+r"(dst_argb), // %2
  764. "+r"(dst_width) // %3
  765. : "r"(src_stepx) // %4
  766. : "memory", "cc", "r12", "q0", "q1", "q2", "q3");
  767. }
  768. // TODO(Yang Zhang): Investigate less load instructions for
  769. // the x/dx stepping
  770. #define LOAD1_DATA32_LANE(dn, n) \
  771. "lsr %5, %3, #16 \n" \
  772. "add %6, %1, %5, lsl #2 \n" \
  773. "add %3, %3, %4 \n" \
  774. "vld1.32 {" #dn "[" #n "]}, [%6] \n"
  775. void ScaleARGBCols_NEON(uint8* dst_argb,
  776. const uint8* src_argb,
  777. int dst_width,
  778. int x,
  779. int dx) {
  780. int tmp;
  781. const uint8* src_tmp = src_argb;
  782. asm volatile(
  783. "1: \n"
  784. // clang-format off
  785. LOAD1_DATA32_LANE(d0, 0)
  786. LOAD1_DATA32_LANE(d0, 1)
  787. LOAD1_DATA32_LANE(d1, 0)
  788. LOAD1_DATA32_LANE(d1, 1)
  789. LOAD1_DATA32_LANE(d2, 0)
  790. LOAD1_DATA32_LANE(d2, 1)
  791. LOAD1_DATA32_LANE(d3, 0)
  792. LOAD1_DATA32_LANE(d3, 1)
  793. // clang-format on
  794. "vst1.32 {q0, q1}, [%0]! \n" // store pixels
  795. "subs %2, %2, #8 \n" // 8 processed per loop
  796. "bgt 1b \n"
  797. : "+r"(dst_argb), // %0
  798. "+r"(src_argb), // %1
  799. "+r"(dst_width), // %2
  800. "+r"(x), // %3
  801. "+r"(dx), // %4
  802. "=&r"(tmp), // %5
  803. "+r"(src_tmp) // %6
  804. :
  805. : "memory", "cc", "q0", "q1");
  806. }
  807. #undef LOAD1_DATA32_LANE
  808. // TODO(Yang Zhang): Investigate less load instructions for
  809. // the x/dx stepping
  810. #define LOAD2_DATA32_LANE(dn1, dn2, n) \
  811. "lsr %5, %3, #16 \n" \
  812. "add %6, %1, %5, lsl #2 \n" \
  813. "add %3, %3, %4 \n" \
  814. "vld2.32 {" #dn1 "[" #n "], " #dn2 "[" #n "]}, [%6] \n"
  815. void ScaleARGBFilterCols_NEON(uint8* dst_argb,
  816. const uint8* src_argb,
  817. int dst_width,
  818. int x,
  819. int dx) {
  820. int dx_offset[4] = {0, 1, 2, 3};
  821. int* tmp = dx_offset;
  822. const uint8* src_tmp = src_argb;
  823. asm volatile (
  824. "vdup.32 q0, %3 \n" // x
  825. "vdup.32 q1, %4 \n" // dx
  826. "vld1.32 {q2}, [%5] \n" // 0 1 2 3
  827. "vshl.i32 q9, q1, #2 \n" // 4 * dx
  828. "vmul.s32 q1, q1, q2 \n"
  829. "vmov.i8 q3, #0x7f \n" // 0x7F
  830. "vmov.i16 q15, #0x7f \n" // 0x7F
  831. // x , x + 1 * dx, x + 2 * dx, x + 3 * dx
  832. "vadd.s32 q8, q1, q0 \n"
  833. "1: \n"
  834. // d0, d1: a
  835. // d2, d3: b
  836. LOAD2_DATA32_LANE(d0, d2, 0)
  837. LOAD2_DATA32_LANE(d0, d2, 1)
  838. LOAD2_DATA32_LANE(d1, d3, 0)
  839. LOAD2_DATA32_LANE(d1, d3, 1)
  840. "vshrn.i32 d22, q8, #9 \n"
  841. "vand.16 d22, d22, d30 \n"
  842. "vdup.8 d24, d22[0] \n"
  843. "vdup.8 d25, d22[2] \n"
  844. "vdup.8 d26, d22[4] \n"
  845. "vdup.8 d27, d22[6] \n"
  846. "vext.8 d4, d24, d25, #4 \n"
  847. "vext.8 d5, d26, d27, #4 \n" // f
  848. "veor.8 q10, q2, q3 \n" // 0x7f ^ f
  849. "vmull.u8 q11, d0, d20 \n"
  850. "vmull.u8 q12, d1, d21 \n"
  851. "vmull.u8 q13, d2, d4 \n"
  852. "vmull.u8 q14, d3, d5 \n"
  853. "vadd.i16 q11, q11, q13 \n"
  854. "vadd.i16 q12, q12, q14 \n"
  855. "vshrn.i16 d0, q11, #7 \n"
  856. "vshrn.i16 d1, q12, #7 \n"
  857. "vst1.32 {d0, d1}, [%0]! \n" // store pixels
  858. "vadd.s32 q8, q8, q9 \n"
  859. "subs %2, %2, #4 \n" // 4 processed per loop
  860. "bgt 1b \n"
  861. : "+r"(dst_argb), // %0
  862. "+r"(src_argb), // %1
  863. "+r"(dst_width), // %2
  864. "+r"(x), // %3
  865. "+r"(dx), // %4
  866. "+r"(tmp), // %5
  867. "+r"(src_tmp) // %6
  868. :
  869. : "memory", "cc", "q0", "q1", "q2", "q3", "q8", "q9",
  870. "q10", "q11", "q12", "q13", "q14", "q15"
  871. );
  872. }
  873. #undef LOAD2_DATA32_LANE
  874. #endif // defined(__ARM_NEON__) && !defined(__aarch64__)
  875. #ifdef __cplusplus
  876. } // extern "C"
  877. } // namespace libyuv
  878. #endif