alpha_processing_sse41.c 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // Utilities for processing transparent channel, SSE4.1 variant.
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include "./dsp.h"
  14. #if defined(WEBP_USE_SSE41)
  15. #include <smmintrin.h>
  16. //------------------------------------------------------------------------------
  17. static int ExtractAlpha_SSE41(const uint8_t* WEBP_RESTRICT argb,
  18. int argb_stride, int width, int height,
  19. uint8_t* WEBP_RESTRICT alpha, int alpha_stride) {
  20. // alpha_and stores an 'and' operation of all the alpha[] values. The final
  21. // value is not 0xff if any of the alpha[] is not equal to 0xff.
  22. uint32_t alpha_and = 0xff;
  23. int i, j;
  24. const __m128i all_0xff = _mm_set1_epi32(~0u);
  25. __m128i all_alphas = all_0xff;
  26. // We must be able to access 3 extra bytes after the last written byte
  27. // 'src[4 * width - 4]', because we don't know if alpha is the first or the
  28. // last byte of the quadruplet.
  29. const int limit = (width - 1) & ~15;
  30. const __m128i kCstAlpha0 = _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1,
  31. -1, -1, -1, -1, 12, 8, 4, 0);
  32. const __m128i kCstAlpha1 = _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1,
  33. 12, 8, 4, 0, -1, -1, -1, -1);
  34. const __m128i kCstAlpha2 = _mm_set_epi8(-1, -1, -1, -1, 12, 8, 4, 0,
  35. -1, -1, -1, -1, -1, -1, -1, -1);
  36. const __m128i kCstAlpha3 = _mm_set_epi8(12, 8, 4, 0, -1, -1, -1, -1,
  37. -1, -1, -1, -1, -1, -1, -1, -1);
  38. for (j = 0; j < height; ++j) {
  39. const __m128i* src = (const __m128i*)argb;
  40. for (i = 0; i < limit; i += 16) {
  41. // load 64 argb bytes
  42. const __m128i a0 = _mm_loadu_si128(src + 0);
  43. const __m128i a1 = _mm_loadu_si128(src + 1);
  44. const __m128i a2 = _mm_loadu_si128(src + 2);
  45. const __m128i a3 = _mm_loadu_si128(src + 3);
  46. const __m128i b0 = _mm_shuffle_epi8(a0, kCstAlpha0);
  47. const __m128i b1 = _mm_shuffle_epi8(a1, kCstAlpha1);
  48. const __m128i b2 = _mm_shuffle_epi8(a2, kCstAlpha2);
  49. const __m128i b3 = _mm_shuffle_epi8(a3, kCstAlpha3);
  50. const __m128i c0 = _mm_or_si128(b0, b1);
  51. const __m128i c1 = _mm_or_si128(b2, b3);
  52. const __m128i d0 = _mm_or_si128(c0, c1);
  53. // store
  54. _mm_storeu_si128((__m128i*)&alpha[i], d0);
  55. // accumulate sixteen alpha 'and' in parallel
  56. all_alphas = _mm_and_si128(all_alphas, d0);
  57. src += 4;
  58. }
  59. for (; i < width; ++i) {
  60. const uint32_t alpha_value = argb[4 * i];
  61. alpha[i] = alpha_value;
  62. alpha_and &= alpha_value;
  63. }
  64. argb += argb_stride;
  65. alpha += alpha_stride;
  66. }
  67. // Combine the sixteen alpha 'and' into an 8-bit mask.
  68. alpha_and |= 0xff00u; // pretend the upper bits [8..15] were tested ok.
  69. alpha_and &= _mm_movemask_epi8(_mm_cmpeq_epi8(all_alphas, all_0xff));
  70. return (alpha_and == 0xffffu);
  71. }
  72. //------------------------------------------------------------------------------
  73. // Entry point
  74. extern void WebPInitAlphaProcessingSSE41(void);
  75. WEBP_TSAN_IGNORE_FUNCTION void WebPInitAlphaProcessingSSE41(void) {
  76. WebPExtractAlpha = ExtractAlpha_SSE41;
  77. }
  78. #else // !WEBP_USE_SSE41
  79. WEBP_DSP_INIT_STUB(WebPInitAlphaProcessingSSE41)
  80. #endif // WEBP_USE_SSE41