Blame src/dsp/lossless_enc_sse41.c

Packit 9c6abc
// Copyright 2015 Google Inc. All Rights Reserved.
Packit 9c6abc
//
Packit 9c6abc
// Use of this source code is governed by a BSD-style license
Packit 9c6abc
// that can be found in the COPYING file in the root of the source
Packit 9c6abc
// tree. An additional intellectual property rights grant can be found
Packit 9c6abc
// in the file PATENTS. All contributing project authors may
Packit 9c6abc
// be found in the AUTHORS file in the root of the source tree.
Packit 9c6abc
// -----------------------------------------------------------------------------
Packit 9c6abc
//
Packit 9c6abc
// SSE4.1 variant of methods for lossless encoder
Packit 9c6abc
//
Packit 9c6abc
// Author: Skal (pascal.massimino@gmail.com)
Packit 9c6abc
Packit 9c6abc
#include "src/dsp/dsp.h"
Packit 9c6abc
Packit 9c6abc
#if defined(WEBP_USE_SSE41)
Packit 9c6abc
#include <assert.h>
Packit 9c6abc
#include <smmintrin.h>
Packit 9c6abc
#include "src/dsp/lossless.h"
Packit 9c6abc
Packit 9c6abc
// For sign-extended multiplying constants, pre-shifted by 5:
Packit 9c6abc
#define CST_5b(X)  (((int16_t)((uint16_t)(X) << 8)) >> 5)
Packit 9c6abc
Packit 9c6abc
//------------------------------------------------------------------------------
Packit 9c6abc
// Subtract-Green Transform
Packit 9c6abc
Packit 9c6abc
static void SubtractGreenFromBlueAndRed_SSE41(uint32_t* argb_data,
Packit 9c6abc
                                              int num_pixels) {
Packit 9c6abc
  int i;
Packit 9c6abc
  const __m128i kCstShuffle = _mm_set_epi8(-1, 13, -1, 13, -1, 9, -1, 9,
Packit 9c6abc
                                           -1,  5, -1,  5, -1, 1, -1, 1);
Packit 9c6abc
  for (i = 0; i + 4 <= num_pixels; i += 4) {
Packit 9c6abc
    const __m128i in = _mm_loadu_si128((__m128i*)&argb_data[i]);
Packit 9c6abc
    const __m128i in_0g0g = _mm_shuffle_epi8(in, kCstShuffle);
Packit 9c6abc
    const __m128i out = _mm_sub_epi8(in, in_0g0g);
Packit 9c6abc
    _mm_storeu_si128((__m128i*)&argb_data[i], out);
Packit 9c6abc
  }
Packit 9c6abc
  // fallthrough and finish off with plain-C
Packit 9c6abc
  if (i != num_pixels) {
Packit 9c6abc
    VP8LSubtractGreenFromBlueAndRed_C(argb_data + i, num_pixels - i);
Packit 9c6abc
  }
Packit 9c6abc
}
Packit 9c6abc
Packit 9c6abc
//------------------------------------------------------------------------------
Packit 9c6abc
// Color Transform
Packit 9c6abc
Packit 9c6abc
#define SPAN 8
Packit 9c6abc
static void CollectColorBlueTransforms_SSE41(const uint32_t* argb, int stride,
Packit 9c6abc
                                             int tile_width, int tile_height,
Packit 9c6abc
                                             int green_to_blue, int red_to_blue,
Packit 9c6abc
                                             int histo[]) {
Packit 9c6abc
  const __m128i mults_r = _mm_set1_epi16(CST_5b(red_to_blue));
Packit 9c6abc
  const __m128i mults_g = _mm_set1_epi16(CST_5b(green_to_blue));
Packit 9c6abc
  const __m128i mask_g = _mm_set1_epi16(0xff00);   // green mask
Packit 9c6abc
  const __m128i mask_gb = _mm_set1_epi32(0xffff);  // green/blue mask
Packit 9c6abc
  const __m128i mask_b = _mm_set1_epi16(0x00ff);   // blue mask
Packit 9c6abc
  const __m128i shuffler_lo = _mm_setr_epi8(-1, 2, -1, 6, -1, 10, -1, 14, -1,
Packit 9c6abc
                                            -1, -1, -1, -1, -1, -1, -1);
Packit 9c6abc
  const __m128i shuffler_hi = _mm_setr_epi8(-1, -1, -1, -1, -1, -1, -1, -1, -1,
Packit 9c6abc
                                            2, -1, 6, -1, 10, -1, 14);
Packit 9c6abc
  int y;
Packit 9c6abc
  for (y = 0; y < tile_height; ++y) {
Packit 9c6abc
    const uint32_t* const src = argb + y * stride;
Packit 9c6abc
    int i, x;
Packit 9c6abc
    for (x = 0; x + SPAN <= tile_width; x += SPAN) {
Packit 9c6abc
      uint16_t values[SPAN];
Packit 9c6abc
      const __m128i in0 = _mm_loadu_si128((__m128i*)&src[x + 0]);
Packit 9c6abc
      const __m128i in1 = _mm_loadu_si128((__m128i*)&src[x + SPAN / 2]);
Packit 9c6abc
      const __m128i r0 = _mm_shuffle_epi8(in0, shuffler_lo);
Packit 9c6abc
      const __m128i r1 = _mm_shuffle_epi8(in1, shuffler_hi);
Packit 9c6abc
      const __m128i r = _mm_or_si128(r0, r1);         // r 0
Packit 9c6abc
      const __m128i gb0 = _mm_and_si128(in0, mask_gb);
Packit 9c6abc
      const __m128i gb1 = _mm_and_si128(in1, mask_gb);
Packit 9c6abc
      const __m128i gb = _mm_packus_epi32(gb0, gb1);  // g b
Packit 9c6abc
      const __m128i g = _mm_and_si128(gb, mask_g);    // g 0
Packit 9c6abc
      const __m128i A = _mm_mulhi_epi16(r, mults_r);  // x dbr
Packit 9c6abc
      const __m128i B = _mm_mulhi_epi16(g, mults_g);  // x dbg
Packit 9c6abc
      const __m128i C = _mm_sub_epi8(gb, B);          // x b'
Packit 9c6abc
      const __m128i D = _mm_sub_epi8(C, A);           // x b''
Packit 9c6abc
      const __m128i E = _mm_and_si128(D, mask_b);     // 0 b''
Packit 9c6abc
      _mm_storeu_si128((__m128i*)values, E);
Packit 9c6abc
      for (i = 0; i < SPAN; ++i) ++histo[values[i]];
Packit 9c6abc
    }
Packit 9c6abc
  }
Packit 9c6abc
  {
Packit 9c6abc
    const int left_over = tile_width & (SPAN - 1);
Packit 9c6abc
    if (left_over > 0) {
Packit 9c6abc
      VP8LCollectColorBlueTransforms_C(argb + tile_width - left_over, stride,
Packit 9c6abc
                                       left_over, tile_height,
Packit 9c6abc
                                       green_to_blue, red_to_blue, histo);
Packit 9c6abc
    }
Packit 9c6abc
  }
Packit 9c6abc
}
Packit 9c6abc
Packit 9c6abc
static void CollectColorRedTransforms_SSE41(const uint32_t* argb, int stride,
Packit 9c6abc
                                            int tile_width, int tile_height,
Packit 9c6abc
                                            int green_to_red, int histo[]) {
Packit 9c6abc
  const __m128i mults_g = _mm_set1_epi16(CST_5b(green_to_red));
Packit 9c6abc
  const __m128i mask_g = _mm_set1_epi32(0x00ff00);  // green mask
Packit 9c6abc
  const __m128i mask = _mm_set1_epi16(0xff);
Packit 9c6abc
Packit 9c6abc
  int y;
Packit 9c6abc
  for (y = 0; y < tile_height; ++y) {
Packit 9c6abc
    const uint32_t* const src = argb + y * stride;
Packit 9c6abc
    int i, x;
Packit 9c6abc
    for (x = 0; x + SPAN <= tile_width; x += SPAN) {
Packit 9c6abc
      uint16_t values[SPAN];
Packit 9c6abc
      const __m128i in0 = _mm_loadu_si128((__m128i*)&src[x + 0]);
Packit 9c6abc
      const __m128i in1 = _mm_loadu_si128((__m128i*)&src[x + SPAN / 2]);
Packit 9c6abc
      const __m128i g0 = _mm_and_si128(in0, mask_g);  // 0 0  | g 0
Packit 9c6abc
      const __m128i g1 = _mm_and_si128(in1, mask_g);
Packit 9c6abc
      const __m128i g = _mm_packus_epi32(g0, g1);     // g 0
Packit 9c6abc
      const __m128i A0 = _mm_srli_epi32(in0, 16);     // 0 0  | x r
Packit 9c6abc
      const __m128i A1 = _mm_srli_epi32(in1, 16);
Packit 9c6abc
      const __m128i A = _mm_packus_epi32(A0, A1);     // x r
Packit 9c6abc
      const __m128i B = _mm_mulhi_epi16(g, mults_g);  // x dr
Packit 9c6abc
      const __m128i C = _mm_sub_epi8(A, B);           // x r'
Packit 9c6abc
      const __m128i D = _mm_and_si128(C, mask);       // 0 r'
Packit 9c6abc
      _mm_storeu_si128((__m128i*)values, D);
Packit 9c6abc
      for (i = 0; i < SPAN; ++i) ++histo[values[i]];
Packit 9c6abc
    }
Packit 9c6abc
  }
Packit 9c6abc
  {
Packit 9c6abc
    const int left_over = tile_width & (SPAN - 1);
Packit 9c6abc
    if (left_over > 0) {
Packit 9c6abc
      VP8LCollectColorRedTransforms_C(argb + tile_width - left_over, stride,
Packit 9c6abc
                                      left_over, tile_height, green_to_red,
Packit 9c6abc
                                      histo);
Packit 9c6abc
    }
Packit 9c6abc
  }
Packit 9c6abc
}
Packit 9c6abc
Packit 9c6abc
//------------------------------------------------------------------------------
Packit 9c6abc
// Entry point
Packit 9c6abc
Packit 9c6abc
extern void VP8LEncDspInitSSE41(void);
Packit 9c6abc
Packit 9c6abc
WEBP_TSAN_IGNORE_FUNCTION void VP8LEncDspInitSSE41(void) {
Packit 9c6abc
  VP8LSubtractGreenFromBlueAndRed = SubtractGreenFromBlueAndRed_SSE41;
Packit 9c6abc
  VP8LCollectColorBlueTransforms = CollectColorBlueTransforms_SSE41;
Packit 9c6abc
  VP8LCollectColorRedTransforms = CollectColorRedTransforms_SSE41;
Packit 9c6abc
}
Packit 9c6abc
Packit 9c6abc
#else  // !WEBP_USE_SSE41
Packit 9c6abc
Packit 9c6abc
WEBP_DSP_INIT_STUB(VP8LEncDspInitSSE41)
Packit 9c6abc
Packit 9c6abc
#endif  // WEBP_USE_SSE41