sad_neon.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Copyright (c) 2014 The WebM 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 <arm_neon.h>
  11. #include "./vpx_config.h"
  12. #include "vpx/vpx_integer.h"
  13. #include "vpx_dsp/arm/mem_neon.h"
  14. #include "vpx_dsp/arm/sum_neon.h"
  15. uint32_t vpx_sad4x4_neon(const uint8_t *src_ptr, int src_stride,
  16. const uint8_t *ref_ptr, int ref_stride) {
  17. const uint8x16_t src_u8 = load_unaligned_u8q(src_ptr, src_stride);
  18. const uint8x16_t ref_u8 = load_unaligned_u8q(ref_ptr, ref_stride);
  19. uint16x8_t abs = vabdl_u8(vget_low_u8(src_u8), vget_low_u8(ref_u8));
  20. abs = vabal_u8(abs, vget_high_u8(src_u8), vget_high_u8(ref_u8));
  21. return vget_lane_u32(horizontal_add_uint16x8(abs), 0);
  22. }
  23. uint32_t vpx_sad4x4_avg_neon(const uint8_t *src_ptr, int src_stride,
  24. const uint8_t *ref_ptr, int ref_stride,
  25. const uint8_t *second_pred) {
  26. const uint8x16_t src_u8 = load_unaligned_u8q(src_ptr, src_stride);
  27. const uint8x16_t ref_u8 = load_unaligned_u8q(ref_ptr, ref_stride);
  28. const uint8x16_t second_pred_u8 = vld1q_u8(second_pred);
  29. const uint8x16_t avg = vrhaddq_u8(ref_u8, second_pred_u8);
  30. uint16x8_t abs = vabdl_u8(vget_low_u8(src_u8), vget_low_u8(avg));
  31. abs = vabal_u8(abs, vget_high_u8(src_u8), vget_high_u8(avg));
  32. return vget_lane_u32(horizontal_add_uint16x8(abs), 0);
  33. }
  34. uint32_t vpx_sad4x8_neon(const uint8_t *src_ptr, int src_stride,
  35. const uint8_t *ref_ptr, int ref_stride) {
  36. int i;
  37. uint16x8_t abs = vdupq_n_u16(0);
  38. for (i = 0; i < 8; i += 4) {
  39. const uint8x16_t src_u8 = load_unaligned_u8q(src_ptr, src_stride);
  40. const uint8x16_t ref_u8 = load_unaligned_u8q(ref_ptr, ref_stride);
  41. src_ptr += 4 * src_stride;
  42. ref_ptr += 4 * ref_stride;
  43. abs = vabal_u8(abs, vget_low_u8(src_u8), vget_low_u8(ref_u8));
  44. abs = vabal_u8(abs, vget_high_u8(src_u8), vget_high_u8(ref_u8));
  45. }
  46. return vget_lane_u32(horizontal_add_uint16x8(abs), 0);
  47. }
  48. uint32_t vpx_sad4x8_avg_neon(const uint8_t *src_ptr, int src_stride,
  49. const uint8_t *ref_ptr, int ref_stride,
  50. const uint8_t *second_pred) {
  51. int i;
  52. uint16x8_t abs = vdupq_n_u16(0);
  53. for (i = 0; i < 8; i += 4) {
  54. const uint8x16_t src_u8 = load_unaligned_u8q(src_ptr, src_stride);
  55. const uint8x16_t ref_u8 = load_unaligned_u8q(ref_ptr, ref_stride);
  56. const uint8x16_t second_pred_u8 = vld1q_u8(second_pred);
  57. const uint8x16_t avg = vrhaddq_u8(ref_u8, second_pred_u8);
  58. src_ptr += 4 * src_stride;
  59. ref_ptr += 4 * ref_stride;
  60. second_pred += 16;
  61. abs = vabal_u8(abs, vget_low_u8(src_u8), vget_low_u8(avg));
  62. abs = vabal_u8(abs, vget_high_u8(src_u8), vget_high_u8(avg));
  63. }
  64. return vget_lane_u32(horizontal_add_uint16x8(abs), 0);
  65. }
  66. static INLINE uint16x8_t sad8x(const uint8_t *a, int a_stride, const uint8_t *b,
  67. int b_stride, const int height) {
  68. int i;
  69. uint16x8_t abs = vdupq_n_u16(0);
  70. for (i = 0; i < height; ++i) {
  71. const uint8x8_t a_u8 = vld1_u8(a);
  72. const uint8x8_t b_u8 = vld1_u8(b);
  73. a += a_stride;
  74. b += b_stride;
  75. abs = vabal_u8(abs, a_u8, b_u8);
  76. }
  77. return abs;
  78. }
  79. static INLINE uint16x8_t sad8x_avg(const uint8_t *a, int a_stride,
  80. const uint8_t *b, int b_stride,
  81. const uint8_t *c, const int height) {
  82. int i;
  83. uint16x8_t abs = vdupq_n_u16(0);
  84. for (i = 0; i < height; ++i) {
  85. const uint8x8_t a_u8 = vld1_u8(a);
  86. const uint8x8_t b_u8 = vld1_u8(b);
  87. const uint8x8_t c_u8 = vld1_u8(c);
  88. const uint8x8_t avg = vrhadd_u8(b_u8, c_u8);
  89. a += a_stride;
  90. b += b_stride;
  91. c += 8;
  92. abs = vabal_u8(abs, a_u8, avg);
  93. }
  94. return abs;
  95. }
  96. #define sad8xN(n) \
  97. uint32_t vpx_sad8x##n##_neon(const uint8_t *src, int src_stride, \
  98. const uint8_t *ref, int ref_stride) { \
  99. const uint16x8_t abs = sad8x(src, src_stride, ref, ref_stride, n); \
  100. return vget_lane_u32(horizontal_add_uint16x8(abs), 0); \
  101. } \
  102. \
  103. uint32_t vpx_sad8x##n##_avg_neon(const uint8_t *src, int src_stride, \
  104. const uint8_t *ref, int ref_stride, \
  105. const uint8_t *second_pred) { \
  106. const uint16x8_t abs = \
  107. sad8x_avg(src, src_stride, ref, ref_stride, second_pred, n); \
  108. return vget_lane_u32(horizontal_add_uint16x8(abs), 0); \
  109. }
  110. sad8xN(4);
  111. sad8xN(8);
  112. sad8xN(16);
  113. static INLINE uint16x8_t sad16x(const uint8_t *a, int a_stride,
  114. const uint8_t *b, int b_stride,
  115. const int height) {
  116. int i;
  117. uint16x8_t abs = vdupq_n_u16(0);
  118. for (i = 0; i < height; ++i) {
  119. const uint8x16_t a_u8 = vld1q_u8(a);
  120. const uint8x16_t b_u8 = vld1q_u8(b);
  121. a += a_stride;
  122. b += b_stride;
  123. abs = vabal_u8(abs, vget_low_u8(a_u8), vget_low_u8(b_u8));
  124. abs = vabal_u8(abs, vget_high_u8(a_u8), vget_high_u8(b_u8));
  125. }
  126. return abs;
  127. }
  128. static INLINE uint16x8_t sad16x_avg(const uint8_t *a, int a_stride,
  129. const uint8_t *b, int b_stride,
  130. const uint8_t *c, const int height) {
  131. int i;
  132. uint16x8_t abs = vdupq_n_u16(0);
  133. for (i = 0; i < height; ++i) {
  134. const uint8x16_t a_u8 = vld1q_u8(a);
  135. const uint8x16_t b_u8 = vld1q_u8(b);
  136. const uint8x16_t c_u8 = vld1q_u8(c);
  137. const uint8x16_t avg = vrhaddq_u8(b_u8, c_u8);
  138. a += a_stride;
  139. b += b_stride;
  140. c += 16;
  141. abs = vabal_u8(abs, vget_low_u8(a_u8), vget_low_u8(avg));
  142. abs = vabal_u8(abs, vget_high_u8(a_u8), vget_high_u8(avg));
  143. }
  144. return abs;
  145. }
  146. #define sad16xN(n) \
  147. uint32_t vpx_sad16x##n##_neon(const uint8_t *src, int src_stride, \
  148. const uint8_t *ref, int ref_stride) { \
  149. const uint16x8_t abs = sad16x(src, src_stride, ref, ref_stride, n); \
  150. return vget_lane_u32(horizontal_add_uint16x8(abs), 0); \
  151. } \
  152. \
  153. uint32_t vpx_sad16x##n##_avg_neon(const uint8_t *src, int src_stride, \
  154. const uint8_t *ref, int ref_stride, \
  155. const uint8_t *second_pred) { \
  156. const uint16x8_t abs = \
  157. sad16x_avg(src, src_stride, ref, ref_stride, second_pred, n); \
  158. return vget_lane_u32(horizontal_add_uint16x8(abs), 0); \
  159. }
  160. sad16xN(8);
  161. sad16xN(16);
  162. sad16xN(32);
  163. static INLINE uint16x8_t sad32x(const uint8_t *a, int a_stride,
  164. const uint8_t *b, int b_stride,
  165. const int height) {
  166. int i;
  167. uint16x8_t abs = vdupq_n_u16(0);
  168. for (i = 0; i < height; ++i) {
  169. const uint8x16_t a_lo = vld1q_u8(a);
  170. const uint8x16_t a_hi = vld1q_u8(a + 16);
  171. const uint8x16_t b_lo = vld1q_u8(b);
  172. const uint8x16_t b_hi = vld1q_u8(b + 16);
  173. a += a_stride;
  174. b += b_stride;
  175. abs = vabal_u8(abs, vget_low_u8(a_lo), vget_low_u8(b_lo));
  176. abs = vabal_u8(abs, vget_high_u8(a_lo), vget_high_u8(b_lo));
  177. abs = vabal_u8(abs, vget_low_u8(a_hi), vget_low_u8(b_hi));
  178. abs = vabal_u8(abs, vget_high_u8(a_hi), vget_high_u8(b_hi));
  179. }
  180. return abs;
  181. }
  182. static INLINE uint16x8_t sad32x_avg(const uint8_t *a, int a_stride,
  183. const uint8_t *b, int b_stride,
  184. const uint8_t *c, const int height) {
  185. int i;
  186. uint16x8_t abs = vdupq_n_u16(0);
  187. for (i = 0; i < height; ++i) {
  188. const uint8x16_t a_lo = vld1q_u8(a);
  189. const uint8x16_t a_hi = vld1q_u8(a + 16);
  190. const uint8x16_t b_lo = vld1q_u8(b);
  191. const uint8x16_t b_hi = vld1q_u8(b + 16);
  192. const uint8x16_t c_lo = vld1q_u8(c);
  193. const uint8x16_t c_hi = vld1q_u8(c + 16);
  194. const uint8x16_t avg_lo = vrhaddq_u8(b_lo, c_lo);
  195. const uint8x16_t avg_hi = vrhaddq_u8(b_hi, c_hi);
  196. a += a_stride;
  197. b += b_stride;
  198. c += 32;
  199. abs = vabal_u8(abs, vget_low_u8(a_lo), vget_low_u8(avg_lo));
  200. abs = vabal_u8(abs, vget_high_u8(a_lo), vget_high_u8(avg_lo));
  201. abs = vabal_u8(abs, vget_low_u8(a_hi), vget_low_u8(avg_hi));
  202. abs = vabal_u8(abs, vget_high_u8(a_hi), vget_high_u8(avg_hi));
  203. }
  204. return abs;
  205. }
  206. #define sad32xN(n) \
  207. uint32_t vpx_sad32x##n##_neon(const uint8_t *src, int src_stride, \
  208. const uint8_t *ref, int ref_stride) { \
  209. const uint16x8_t abs = sad32x(src, src_stride, ref, ref_stride, n); \
  210. return vget_lane_u32(horizontal_add_uint16x8(abs), 0); \
  211. } \
  212. \
  213. uint32_t vpx_sad32x##n##_avg_neon(const uint8_t *src, int src_stride, \
  214. const uint8_t *ref, int ref_stride, \
  215. const uint8_t *second_pred) { \
  216. const uint16x8_t abs = \
  217. sad32x_avg(src, src_stride, ref, ref_stride, second_pred, n); \
  218. return vget_lane_u32(horizontal_add_uint16x8(abs), 0); \
  219. }
  220. sad32xN(16);
  221. sad32xN(32);
  222. sad32xN(64);
  223. static INLINE uint32x4_t sad64x(const uint8_t *a, int a_stride,
  224. const uint8_t *b, int b_stride,
  225. const int height) {
  226. int i;
  227. uint16x8_t abs_0 = vdupq_n_u16(0);
  228. uint16x8_t abs_1 = vdupq_n_u16(0);
  229. for (i = 0; i < height; ++i) {
  230. const uint8x16_t a_0 = vld1q_u8(a);
  231. const uint8x16_t a_1 = vld1q_u8(a + 16);
  232. const uint8x16_t a_2 = vld1q_u8(a + 32);
  233. const uint8x16_t a_3 = vld1q_u8(a + 48);
  234. const uint8x16_t b_0 = vld1q_u8(b);
  235. const uint8x16_t b_1 = vld1q_u8(b + 16);
  236. const uint8x16_t b_2 = vld1q_u8(b + 32);
  237. const uint8x16_t b_3 = vld1q_u8(b + 48);
  238. a += a_stride;
  239. b += b_stride;
  240. abs_0 = vabal_u8(abs_0, vget_low_u8(a_0), vget_low_u8(b_0));
  241. abs_0 = vabal_u8(abs_0, vget_high_u8(a_0), vget_high_u8(b_0));
  242. abs_0 = vabal_u8(abs_0, vget_low_u8(a_1), vget_low_u8(b_1));
  243. abs_0 = vabal_u8(abs_0, vget_high_u8(a_1), vget_high_u8(b_1));
  244. abs_1 = vabal_u8(abs_1, vget_low_u8(a_2), vget_low_u8(b_2));
  245. abs_1 = vabal_u8(abs_1, vget_high_u8(a_2), vget_high_u8(b_2));
  246. abs_1 = vabal_u8(abs_1, vget_low_u8(a_3), vget_low_u8(b_3));
  247. abs_1 = vabal_u8(abs_1, vget_high_u8(a_3), vget_high_u8(b_3));
  248. }
  249. {
  250. const uint32x4_t sum = vpaddlq_u16(abs_0);
  251. return vpadalq_u16(sum, abs_1);
  252. }
  253. }
  254. static INLINE uint32x4_t sad64x_avg(const uint8_t *a, int a_stride,
  255. const uint8_t *b, int b_stride,
  256. const uint8_t *c, const int height) {
  257. int i;
  258. uint16x8_t abs_0 = vdupq_n_u16(0);
  259. uint16x8_t abs_1 = vdupq_n_u16(0);
  260. for (i = 0; i < height; ++i) {
  261. const uint8x16_t a_0 = vld1q_u8(a);
  262. const uint8x16_t a_1 = vld1q_u8(a + 16);
  263. const uint8x16_t a_2 = vld1q_u8(a + 32);
  264. const uint8x16_t a_3 = vld1q_u8(a + 48);
  265. const uint8x16_t b_0 = vld1q_u8(b);
  266. const uint8x16_t b_1 = vld1q_u8(b + 16);
  267. const uint8x16_t b_2 = vld1q_u8(b + 32);
  268. const uint8x16_t b_3 = vld1q_u8(b + 48);
  269. const uint8x16_t c_0 = vld1q_u8(c);
  270. const uint8x16_t c_1 = vld1q_u8(c + 16);
  271. const uint8x16_t c_2 = vld1q_u8(c + 32);
  272. const uint8x16_t c_3 = vld1q_u8(c + 48);
  273. const uint8x16_t avg_0 = vrhaddq_u8(b_0, c_0);
  274. const uint8x16_t avg_1 = vrhaddq_u8(b_1, c_1);
  275. const uint8x16_t avg_2 = vrhaddq_u8(b_2, c_2);
  276. const uint8x16_t avg_3 = vrhaddq_u8(b_3, c_3);
  277. a += a_stride;
  278. b += b_stride;
  279. c += 64;
  280. abs_0 = vabal_u8(abs_0, vget_low_u8(a_0), vget_low_u8(avg_0));
  281. abs_0 = vabal_u8(abs_0, vget_high_u8(a_0), vget_high_u8(avg_0));
  282. abs_0 = vabal_u8(abs_0, vget_low_u8(a_1), vget_low_u8(avg_1));
  283. abs_0 = vabal_u8(abs_0, vget_high_u8(a_1), vget_high_u8(avg_1));
  284. abs_1 = vabal_u8(abs_1, vget_low_u8(a_2), vget_low_u8(avg_2));
  285. abs_1 = vabal_u8(abs_1, vget_high_u8(a_2), vget_high_u8(avg_2));
  286. abs_1 = vabal_u8(abs_1, vget_low_u8(a_3), vget_low_u8(avg_3));
  287. abs_1 = vabal_u8(abs_1, vget_high_u8(a_3), vget_high_u8(avg_3));
  288. }
  289. {
  290. const uint32x4_t sum = vpaddlq_u16(abs_0);
  291. return vpadalq_u16(sum, abs_1);
  292. }
  293. }
  294. #define sad64xN(n) \
  295. uint32_t vpx_sad64x##n##_neon(const uint8_t *src, int src_stride, \
  296. const uint8_t *ref, int ref_stride) { \
  297. const uint32x4_t abs = sad64x(src, src_stride, ref, ref_stride, n); \
  298. return vget_lane_u32(horizontal_add_uint32x4(abs), 0); \
  299. } \
  300. \
  301. uint32_t vpx_sad64x##n##_avg_neon(const uint8_t *src, int src_stride, \
  302. const uint8_t *ref, int ref_stride, \
  303. const uint8_t *second_pred) { \
  304. const uint32x4_t abs = \
  305. sad64x_avg(src, src_stride, ref, ref_stride, second_pred, n); \
  306. return vget_lane_u32(horizontal_add_uint32x4(abs), 0); \
  307. }
  308. sad64xN(32);
  309. sad64xN(64);