rescaler_sse2.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // Copyright 2015 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // SSE2 Rescaling functions
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include "./dsp.h"
  14. #if defined(WEBP_USE_SSE2) && !defined(WEBP_REDUCE_SIZE)
  15. #include <emmintrin.h>
  16. #include <assert.h>
  17. #include "../utils/rescaler_utils.h"
  18. #include "../utils/utils.h"
  19. //------------------------------------------------------------------------------
  20. // Implementations of critical functions ImportRow / ExportRow
  21. #define ROUNDER (WEBP_RESCALER_ONE >> 1)
  22. #define MULT_FIX(x, y) (((uint64_t)(x) * (y) + ROUNDER) >> WEBP_RESCALER_RFIX)
  23. #define MULT_FIX_FLOOR(x, y) (((uint64_t)(x) * (y)) >> WEBP_RESCALER_RFIX)
  24. // input: 8 bytes ABCDEFGH -> output: A0E0B0F0C0G0D0H0
  25. static void LoadTwoPixels_SSE2(const uint8_t* const src, __m128i* out) {
  26. const __m128i zero = _mm_setzero_si128();
  27. const __m128i A = _mm_loadl_epi64((const __m128i*)(src)); // ABCDEFGH
  28. const __m128i B = _mm_unpacklo_epi8(A, zero); // A0B0C0D0E0F0G0H0
  29. const __m128i C = _mm_srli_si128(B, 8); // E0F0G0H0
  30. *out = _mm_unpacklo_epi16(B, C);
  31. }
  32. // input: 8 bytes ABCDEFGH -> output: A0B0C0D0E0F0G0H0
  33. static void LoadEightPixels_SSE2(const uint8_t* const src, __m128i* out) {
  34. const __m128i zero = _mm_setzero_si128();
  35. const __m128i A = _mm_loadl_epi64((const __m128i*)(src)); // ABCDEFGH
  36. *out = _mm_unpacklo_epi8(A, zero);
  37. }
  38. static void RescalerImportRowExpand_SSE2(WebPRescaler* const wrk,
  39. const uint8_t* src) {
  40. rescaler_t* frow = wrk->frow;
  41. const rescaler_t* const frow_end = frow + wrk->dst_width * wrk->num_channels;
  42. const int x_add = wrk->x_add;
  43. int accum = x_add;
  44. __m128i cur_pixels;
  45. // SSE2 implementation only works with 16b signed arithmetic at max.
  46. if (wrk->src_width < 8 || accum >= (1 << 15)) {
  47. WebPRescalerImportRowExpand_C(wrk, src);
  48. return;
  49. }
  50. assert(!WebPRescalerInputDone(wrk));
  51. assert(wrk->x_expand);
  52. if (wrk->num_channels == 4) {
  53. LoadTwoPixels_SSE2(src, &cur_pixels);
  54. src += 4;
  55. while (1) {
  56. const __m128i mult = _mm_set1_epi32(((x_add - accum) << 16) | accum);
  57. const __m128i out = _mm_madd_epi16(cur_pixels, mult);
  58. _mm_storeu_si128((__m128i*)frow, out);
  59. frow += 4;
  60. if (frow >= frow_end) break;
  61. accum -= wrk->x_sub;
  62. if (accum < 0) {
  63. LoadTwoPixels_SSE2(src, &cur_pixels);
  64. src += 4;
  65. accum += x_add;
  66. }
  67. }
  68. } else {
  69. int left;
  70. const uint8_t* const src_limit = src + wrk->src_width - 8;
  71. LoadEightPixels_SSE2(src, &cur_pixels);
  72. src += 7;
  73. left = 7;
  74. while (1) {
  75. const __m128i mult = _mm_cvtsi32_si128(((x_add - accum) << 16) | accum);
  76. const __m128i out = _mm_madd_epi16(cur_pixels, mult);
  77. assert(sizeof(*frow) == sizeof(uint32_t));
  78. WebPUint32ToMem((uint8_t*)frow, _mm_cvtsi128_si32(out));
  79. frow += 1;
  80. if (frow >= frow_end) break;
  81. accum -= wrk->x_sub;
  82. if (accum < 0) {
  83. if (--left) {
  84. cur_pixels = _mm_srli_si128(cur_pixels, 2);
  85. } else if (src <= src_limit) {
  86. LoadEightPixels_SSE2(src, &cur_pixels);
  87. src += 7;
  88. left = 7;
  89. } else { // tail
  90. cur_pixels = _mm_srli_si128(cur_pixels, 2);
  91. cur_pixels = _mm_insert_epi16(cur_pixels, src[1], 1);
  92. src += 1;
  93. left = 1;
  94. }
  95. accum += x_add;
  96. }
  97. }
  98. }
  99. assert(accum == 0);
  100. }
  101. static void RescalerImportRowShrink_SSE2(WebPRescaler* const wrk,
  102. const uint8_t* src) {
  103. const int x_sub = wrk->x_sub;
  104. int accum = 0;
  105. const __m128i zero = _mm_setzero_si128();
  106. const __m128i mult0 = _mm_set1_epi16(x_sub);
  107. const __m128i mult1 = _mm_set1_epi32(wrk->fx_scale);
  108. const __m128i rounder = _mm_set_epi32(0, ROUNDER, 0, ROUNDER);
  109. __m128i sum = zero;
  110. rescaler_t* frow = wrk->frow;
  111. const rescaler_t* const frow_end = wrk->frow + 4 * wrk->dst_width;
  112. if (wrk->num_channels != 4 || wrk->x_add > (x_sub << 7)) {
  113. WebPRescalerImportRowShrink_C(wrk, src);
  114. return;
  115. }
  116. assert(!WebPRescalerInputDone(wrk));
  117. assert(!wrk->x_expand);
  118. for (; frow < frow_end; frow += 4) {
  119. __m128i base = zero;
  120. accum += wrk->x_add;
  121. while (accum > 0) {
  122. const __m128i A = _mm_cvtsi32_si128(WebPMemToUint32(src));
  123. src += 4;
  124. base = _mm_unpacklo_epi8(A, zero);
  125. // To avoid overflow, we need: base * x_add / x_sub < 32768
  126. // => x_add < x_sub << 7. That's a 1/128 reduction ratio limit.
  127. sum = _mm_add_epi16(sum, base);
  128. accum -= x_sub;
  129. }
  130. { // Emit next horizontal pixel.
  131. const __m128i mult = _mm_set1_epi16(-accum);
  132. const __m128i frac0 = _mm_mullo_epi16(base, mult); // 16b x 16b -> 32b
  133. const __m128i frac1 = _mm_mulhi_epu16(base, mult);
  134. const __m128i frac = _mm_unpacklo_epi16(frac0, frac1); // frac is 32b
  135. const __m128i A0 = _mm_mullo_epi16(sum, mult0);
  136. const __m128i A1 = _mm_mulhi_epu16(sum, mult0);
  137. const __m128i B0 = _mm_unpacklo_epi16(A0, A1); // sum * x_sub
  138. const __m128i frow_out = _mm_sub_epi32(B0, frac); // sum * x_sub - frac
  139. const __m128i D0 = _mm_srli_epi64(frac, 32);
  140. const __m128i D1 = _mm_mul_epu32(frac, mult1); // 32b x 16b -> 64b
  141. const __m128i D2 = _mm_mul_epu32(D0, mult1);
  142. const __m128i E1 = _mm_add_epi64(D1, rounder);
  143. const __m128i E2 = _mm_add_epi64(D2, rounder);
  144. const __m128i F1 = _mm_shuffle_epi32(E1, 1 | (3 << 2));
  145. const __m128i F2 = _mm_shuffle_epi32(E2, 1 | (3 << 2));
  146. const __m128i G = _mm_unpacklo_epi32(F1, F2);
  147. sum = _mm_packs_epi32(G, zero);
  148. _mm_storeu_si128((__m128i*)frow, frow_out);
  149. }
  150. }
  151. assert(accum == 0);
  152. }
  153. //------------------------------------------------------------------------------
  154. // Row export
  155. // load *src as epi64, multiply by mult and store result in [out0 ... out3]
  156. static WEBP_INLINE void LoadDispatchAndMult_SSE2(const rescaler_t* const src,
  157. const __m128i* const mult,
  158. __m128i* const out0,
  159. __m128i* const out1,
  160. __m128i* const out2,
  161. __m128i* const out3) {
  162. const __m128i A0 = _mm_loadu_si128((const __m128i*)(src + 0));
  163. const __m128i A1 = _mm_loadu_si128((const __m128i*)(src + 4));
  164. const __m128i A2 = _mm_srli_epi64(A0, 32);
  165. const __m128i A3 = _mm_srli_epi64(A1, 32);
  166. if (mult != NULL) {
  167. *out0 = _mm_mul_epu32(A0, *mult);
  168. *out1 = _mm_mul_epu32(A1, *mult);
  169. *out2 = _mm_mul_epu32(A2, *mult);
  170. *out3 = _mm_mul_epu32(A3, *mult);
  171. } else {
  172. *out0 = A0;
  173. *out1 = A1;
  174. *out2 = A2;
  175. *out3 = A3;
  176. }
  177. }
  178. static WEBP_INLINE void ProcessRow_SSE2(const __m128i* const A0,
  179. const __m128i* const A1,
  180. const __m128i* const A2,
  181. const __m128i* const A3,
  182. const __m128i* const mult,
  183. uint8_t* const dst) {
  184. const __m128i rounder = _mm_set_epi32(0, ROUNDER, 0, ROUNDER);
  185. const __m128i mask = _mm_set_epi32(0xffffffffu, 0, 0xffffffffu, 0);
  186. const __m128i B0 = _mm_mul_epu32(*A0, *mult);
  187. const __m128i B1 = _mm_mul_epu32(*A1, *mult);
  188. const __m128i B2 = _mm_mul_epu32(*A2, *mult);
  189. const __m128i B3 = _mm_mul_epu32(*A3, *mult);
  190. const __m128i C0 = _mm_add_epi64(B0, rounder);
  191. const __m128i C1 = _mm_add_epi64(B1, rounder);
  192. const __m128i C2 = _mm_add_epi64(B2, rounder);
  193. const __m128i C3 = _mm_add_epi64(B3, rounder);
  194. const __m128i D0 = _mm_srli_epi64(C0, WEBP_RESCALER_RFIX);
  195. const __m128i D1 = _mm_srli_epi64(C1, WEBP_RESCALER_RFIX);
  196. #if (WEBP_RESCALER_RFIX < 32)
  197. const __m128i D2 =
  198. _mm_and_si128(_mm_slli_epi64(C2, 32 - WEBP_RESCALER_RFIX), mask);
  199. const __m128i D3 =
  200. _mm_and_si128(_mm_slli_epi64(C3, 32 - WEBP_RESCALER_RFIX), mask);
  201. #else
  202. const __m128i D2 = _mm_and_si128(C2, mask);
  203. const __m128i D3 = _mm_and_si128(C3, mask);
  204. #endif
  205. const __m128i E0 = _mm_or_si128(D0, D2);
  206. const __m128i E1 = _mm_or_si128(D1, D3);
  207. const __m128i F = _mm_packs_epi32(E0, E1);
  208. const __m128i G = _mm_packus_epi16(F, F);
  209. _mm_storel_epi64((__m128i*)dst, G);
  210. }
  211. static void RescalerExportRowExpand_SSE2(WebPRescaler* const wrk) {
  212. int x_out;
  213. uint8_t* const dst = wrk->dst;
  214. rescaler_t* const irow = wrk->irow;
  215. const int x_out_max = wrk->dst_width * wrk->num_channels;
  216. const rescaler_t* const frow = wrk->frow;
  217. const __m128i mult = _mm_set_epi32(0, wrk->fy_scale, 0, wrk->fy_scale);
  218. assert(!WebPRescalerOutputDone(wrk));
  219. assert(wrk->y_accum <= 0 && wrk->y_sub + wrk->y_accum >= 0);
  220. assert(wrk->y_expand);
  221. if (wrk->y_accum == 0) {
  222. for (x_out = 0; x_out + 8 <= x_out_max; x_out += 8) {
  223. __m128i A0, A1, A2, A3;
  224. LoadDispatchAndMult_SSE2(frow + x_out, NULL, &A0, &A1, &A2, &A3);
  225. ProcessRow_SSE2(&A0, &A1, &A2, &A3, &mult, dst + x_out);
  226. }
  227. for (; x_out < x_out_max; ++x_out) {
  228. const uint32_t J = frow[x_out];
  229. const int v = (int)MULT_FIX(J, wrk->fy_scale);
  230. dst[x_out] = (v > 255) ? 255u : (uint8_t)v;
  231. }
  232. } else {
  233. const uint32_t B = WEBP_RESCALER_FRAC(-wrk->y_accum, wrk->y_sub);
  234. const uint32_t A = (uint32_t)(WEBP_RESCALER_ONE - B);
  235. const __m128i mA = _mm_set_epi32(0, A, 0, A);
  236. const __m128i mB = _mm_set_epi32(0, B, 0, B);
  237. const __m128i rounder = _mm_set_epi32(0, ROUNDER, 0, ROUNDER);
  238. for (x_out = 0; x_out + 8 <= x_out_max; x_out += 8) {
  239. __m128i A0, A1, A2, A3, B0, B1, B2, B3;
  240. LoadDispatchAndMult_SSE2(frow + x_out, &mA, &A0, &A1, &A2, &A3);
  241. LoadDispatchAndMult_SSE2(irow + x_out, &mB, &B0, &B1, &B2, &B3);
  242. {
  243. const __m128i C0 = _mm_add_epi64(A0, B0);
  244. const __m128i C1 = _mm_add_epi64(A1, B1);
  245. const __m128i C2 = _mm_add_epi64(A2, B2);
  246. const __m128i C3 = _mm_add_epi64(A3, B3);
  247. const __m128i D0 = _mm_add_epi64(C0, rounder);
  248. const __m128i D1 = _mm_add_epi64(C1, rounder);
  249. const __m128i D2 = _mm_add_epi64(C2, rounder);
  250. const __m128i D3 = _mm_add_epi64(C3, rounder);
  251. const __m128i E0 = _mm_srli_epi64(D0, WEBP_RESCALER_RFIX);
  252. const __m128i E1 = _mm_srli_epi64(D1, WEBP_RESCALER_RFIX);
  253. const __m128i E2 = _mm_srli_epi64(D2, WEBP_RESCALER_RFIX);
  254. const __m128i E3 = _mm_srli_epi64(D3, WEBP_RESCALER_RFIX);
  255. ProcessRow_SSE2(&E0, &E1, &E2, &E3, &mult, dst + x_out);
  256. }
  257. }
  258. for (; x_out < x_out_max; ++x_out) {
  259. const uint64_t I = (uint64_t)A * frow[x_out]
  260. + (uint64_t)B * irow[x_out];
  261. const uint32_t J = (uint32_t)((I + ROUNDER) >> WEBP_RESCALER_RFIX);
  262. const int v = (int)MULT_FIX(J, wrk->fy_scale);
  263. dst[x_out] = (v > 255) ? 255u : (uint8_t)v;
  264. }
  265. }
  266. }
  267. static void RescalerExportRowShrink_SSE2(WebPRescaler* const wrk) {
  268. int x_out;
  269. uint8_t* const dst = wrk->dst;
  270. rescaler_t* const irow = wrk->irow;
  271. const int x_out_max = wrk->dst_width * wrk->num_channels;
  272. const rescaler_t* const frow = wrk->frow;
  273. const uint32_t yscale = wrk->fy_scale * (-wrk->y_accum);
  274. assert(!WebPRescalerOutputDone(wrk));
  275. assert(wrk->y_accum <= 0);
  276. assert(!wrk->y_expand);
  277. if (yscale) {
  278. const int scale_xy = wrk->fxy_scale;
  279. const __m128i mult_xy = _mm_set_epi32(0, scale_xy, 0, scale_xy);
  280. const __m128i mult_y = _mm_set_epi32(0, yscale, 0, yscale);
  281. for (x_out = 0; x_out + 8 <= x_out_max; x_out += 8) {
  282. __m128i A0, A1, A2, A3, B0, B1, B2, B3;
  283. LoadDispatchAndMult_SSE2(irow + x_out, NULL, &A0, &A1, &A2, &A3);
  284. LoadDispatchAndMult_SSE2(frow + x_out, &mult_y, &B0, &B1, &B2, &B3);
  285. {
  286. const __m128i D0 = _mm_srli_epi64(B0, WEBP_RESCALER_RFIX); // = frac
  287. const __m128i D1 = _mm_srli_epi64(B1, WEBP_RESCALER_RFIX);
  288. const __m128i D2 = _mm_srli_epi64(B2, WEBP_RESCALER_RFIX);
  289. const __m128i D3 = _mm_srli_epi64(B3, WEBP_RESCALER_RFIX);
  290. const __m128i E0 = _mm_sub_epi64(A0, D0); // irow[x] - frac
  291. const __m128i E1 = _mm_sub_epi64(A1, D1);
  292. const __m128i E2 = _mm_sub_epi64(A2, D2);
  293. const __m128i E3 = _mm_sub_epi64(A3, D3);
  294. const __m128i F2 = _mm_slli_epi64(D2, 32);
  295. const __m128i F3 = _mm_slli_epi64(D3, 32);
  296. const __m128i G0 = _mm_or_si128(D0, F2);
  297. const __m128i G1 = _mm_or_si128(D1, F3);
  298. _mm_storeu_si128((__m128i*)(irow + x_out + 0), G0);
  299. _mm_storeu_si128((__m128i*)(irow + x_out + 4), G1);
  300. ProcessRow_SSE2(&E0, &E1, &E2, &E3, &mult_xy, dst + x_out);
  301. }
  302. }
  303. for (; x_out < x_out_max; ++x_out) {
  304. const uint32_t frac = (int)MULT_FIX_FLOOR(frow[x_out], yscale);
  305. const int v = (int)MULT_FIX(irow[x_out] - frac, wrk->fxy_scale);
  306. dst[x_out] = (v > 255) ? 255u : (uint8_t)v;
  307. irow[x_out] = frac; // new fractional start
  308. }
  309. } else {
  310. const uint32_t scale = wrk->fxy_scale;
  311. const __m128i mult = _mm_set_epi32(0, scale, 0, scale);
  312. const __m128i zero = _mm_setzero_si128();
  313. for (x_out = 0; x_out + 8 <= x_out_max; x_out += 8) {
  314. __m128i A0, A1, A2, A3;
  315. LoadDispatchAndMult_SSE2(irow + x_out, NULL, &A0, &A1, &A2, &A3);
  316. _mm_storeu_si128((__m128i*)(irow + x_out + 0), zero);
  317. _mm_storeu_si128((__m128i*)(irow + x_out + 4), zero);
  318. ProcessRow_SSE2(&A0, &A1, &A2, &A3, &mult, dst + x_out);
  319. }
  320. for (; x_out < x_out_max; ++x_out) {
  321. const int v = (int)MULT_FIX(irow[x_out], scale);
  322. dst[x_out] = (v > 255) ? 255u : (uint8_t)v;
  323. irow[x_out] = 0;
  324. }
  325. }
  326. }
  327. #undef MULT_FIX_FLOOR
  328. #undef MULT_FIX
  329. #undef ROUNDER
  330. //------------------------------------------------------------------------------
  331. extern void WebPRescalerDspInitSSE2(void);
  332. WEBP_TSAN_IGNORE_FUNCTION void WebPRescalerDspInitSSE2(void) {
  333. WebPRescalerImportRowExpand = RescalerImportRowExpand_SSE2;
  334. WebPRescalerImportRowShrink = RescalerImportRowShrink_SSE2;
  335. WebPRescalerExportRowExpand = RescalerExportRowExpand_SSE2;
  336. WebPRescalerExportRowShrink = RescalerExportRowShrink_SSE2;
  337. }
  338. #else // !WEBP_USE_SSE2
  339. WEBP_DSP_INIT_STUB(WebPRescalerDspInitSSE2)
  340. #endif // WEBP_USE_SSE2