Coverage Report

Created: 2025-07-18 06:23

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