tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

SwizzleAVX2.cpp (3274B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "Swizzle.h"
      8 
      9 #include <immintrin.h>
     10 #include <tmmintrin.h>
     11 
     12 namespace mozilla::gfx {
     13 
     14 template <bool aSwapRB>
     15 void UnpackRowRGB24_SSSE3(const uint8_t* aSrc, uint8_t* aDst, int32_t aLength);
     16 
     17 template <bool aSwapRB>
     18 void UnpackRowRGB24_AVX2(const uint8_t* aSrc, uint8_t* aDst, int32_t aLength) {
     19  // Because this implementation will read an additional 8 bytes of data that
     20  // is ignored and masked over, we cannot use the accelerated version for the
     21  // last 1-10 pixels (3-30 bytes remaining) to guarantee we don't access memory
     22  // outside the buffer (we read in 32 byte chunks).
     23  if (aLength < 11) {
     24    UnpackRowRGB24_SSSE3<aSwapRB>(aSrc, aDst, aLength);
     25    return;
     26  }
     27 
     28  // Because we are expanding, we can only process the data back to front in
     29  // case we are performing this in place.
     30  int32_t alignedRow = (aLength - 4) & ~7;
     31  int32_t remainder = aLength - alignedRow;
     32 
     33  const uint8_t* src = aSrc + alignedRow * 3;
     34  uint8_t* dst = aDst + alignedRow * 4;
     35 
     36  // Handle any 3-10 remaining pixels.
     37  UnpackRowRGB24_SSSE3<aSwapRB>(src, dst, remainder);
     38 
     39  // Used to shuffle the two final 32-bit words which we ignore into the last
     40  // 32-bit word of each 128-bit lane, such that
     41  //   RGBR GBRG BRGB RGBR GBRG BRGB RGBR GBRG
     42  //   BRGB RGBR GBRG BRGB ZZZZ ZZZZ ZZZZ ZZZZ
     43  // becomes
     44  //   RGBR GBRG BRGB RGBR GBRG BRGB ZZZZ ZZZZ
     45  //   RGBR GBRG BRGB RGBR GBRG BRGB ZZZZ ZZZZ
     46  const __m256i discardMask = _mm256_set_epi32(7, 5, 4, 3, 6, 2, 1, 0);
     47 
     48  // Used to shuffle 8-bit words within a 128-bit lane, such that we transform
     49  //   RGBR GBRG BRGB RGBR GBRG BRGB ZZZZ ZZZZ
     50  // into
     51  //   RGBZ RGBZ RGBZ RGBZ RGBZ RGBZ RGBZ RGBZ
     52  // or
     53  //   BGRZ BGRZ BGRZ BGRZ BGRZ BGRZ BGRZ BGRZ
     54  const __m256i colorMask =
     55      aSwapRB ? _mm256_set_epi8(15, 9, 10, 11, 14, 6, 7, 8, 13, 3, 4, 5, 12, 0,
     56                                1, 2, 15, 9, 10, 11, 14, 6, 7, 8, 13, 3, 4, 5,
     57                                12, 0, 1, 2)
     58              : _mm256_set_epi8(15, 11, 10, 9, 14, 8, 7, 6, 13, 5, 4, 3, 12, 2,
     59                                1, 0, 15, 11, 10, 9, 14, 8, 7, 6, 13, 5, 4, 3,
     60                                12, 2, 1, 0);
     61 
     62  // Used to transform RGBZ/BGRZ to RGBX/BGRX, or force the alpha opaque.
     63  const __m256i alphaMask = _mm256_set1_epi32(0xFF000000);
     64 
     65  // Process all 8-pixel chunks as one vector.
     66  src -= 8 * 3;
     67  dst -= 8 * 4;
     68  while (src >= aSrc) {
     69    __m256i px = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(src));
     70    px = _mm256_permutevar8x32_epi32(px, discardMask);
     71    px = _mm256_shuffle_epi8(px, colorMask);
     72    px = _mm256_or_si256(px, alphaMask);
     73    _mm256_storeu_si256(reinterpret_cast<__m256i*>(dst), px);
     74    src -= 8 * 3;
     75    dst -= 8 * 4;
     76  }
     77 }
     78 
     79 // Force instantiation of swizzle variants here.
     80 template void UnpackRowRGB24_AVX2<false>(const uint8_t*, uint8_t*, int32_t);
     81 template void UnpackRowRGB24_AVX2<true>(const uint8_t*, uint8_t*, int32_t);
     82 
     83 }  // namespace mozilla::gfx