Blame thirdparty/libjpeg-turbo/libjpeg-turbo-2.0.6/simd/loongson/jdsample-mmi.c

shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Loongson MMI optimizations for libjpeg-turbo
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * Copyright (C) 2015, 2018, D. R. Commander.  All Rights Reserved.
shun-iwasawa 82a8f5
 * Copyright (C) 2016-2017, Loongson Technology Corporation Limited, BeiJing.
shun-iwasawa 82a8f5
 *                          All Rights Reserved.
shun-iwasawa 82a8f5
 * Authors:  ZhuChen     <zhuchen@loongson.cn></zhuchen@loongson.cn>
shun-iwasawa 82a8f5
 *           CaiWanwei   <caiwanwei@loongson.cn></caiwanwei@loongson.cn>
shun-iwasawa 82a8f5
 *           SunZhangzhi <sunzhangzhi-cq@loongson.cn></sunzhangzhi-cq@loongson.cn>
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * Based on the x86 SIMD extension for IJG JPEG library
shun-iwasawa 82a8f5
 * Copyright (C) 1999-2006, MIYASAKA Masaru.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * This software is provided 'as-is', without any express or implied
shun-iwasawa 82a8f5
 * warranty.  In no event will the authors be held liable for any damages
shun-iwasawa 82a8f5
 * arising from the use of this software.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * Permission is granted to anyone to use this software for any purpose,
shun-iwasawa 82a8f5
 * including commercial applications, and to alter it and redistribute it
shun-iwasawa 82a8f5
 * freely, subject to the following restrictions:
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * 1. The origin of this software must not be misrepresented; you must not
shun-iwasawa 82a8f5
 *    claim that you wrote the original software. If you use this software
shun-iwasawa 82a8f5
 *    in a product, an acknowledgment in the product documentation would be
shun-iwasawa 82a8f5
 *    appreciated but is not required.
shun-iwasawa 82a8f5
 * 2. Altered source versions must be plainly marked as such, and must not be
shun-iwasawa 82a8f5
 *    misrepresented as being the original software.
shun-iwasawa 82a8f5
 * 3. This notice may not be removed or altered from any source distribution.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* CHROMA UPSAMPLING */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#include "jsimd_mmi.h"
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
enum const_index {
shun-iwasawa 82a8f5
  index_PW_THREE,
shun-iwasawa 82a8f5
  index_PW_SEVEN,
shun-iwasawa 82a8f5
  index_PW_EIGHT,
shun-iwasawa 82a8f5
};
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static uint64_t const_value[] = {
shun-iwasawa 82a8f5
  _uint64_set_pi16(3, 3, 3, 3),
shun-iwasawa 82a8f5
  _uint64_set_pi16(7, 7, 7, 7),
shun-iwasawa 82a8f5
  _uint64_set_pi16(8, 8, 8, 8),
shun-iwasawa 82a8f5
};
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define PW_THREE  get_const_value(index_PW_THREE)
shun-iwasawa 82a8f5
#define PW_SEVEN  get_const_value(index_PW_SEVEN)
shun-iwasawa 82a8f5
#define PW_EIGHT  get_const_value(index_PW_EIGHT)
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define PROCESS_ROW(r) { \
shun-iwasawa 82a8f5
  mm7 = _mm_load_si64((__m64 *)outptr##r);      /* mm7=IntrL=( 0 1 2 3) */ \
shun-iwasawa 82a8f5
  mm3 = _mm_load_si64((__m64 *)outptr##r + 1);  /* mm3=IntrH=( 4 5 6 7) */ \
shun-iwasawa 82a8f5
  \
shun-iwasawa 82a8f5
  mm0 = mm7; \
shun-iwasawa 82a8f5
  mm4 = mm3; \
shun-iwasawa 82a8f5
  mm0 = _mm_srli_si64(mm0, 2 * BYTE_BIT);                   /* mm0=( 1 2 3 -) */ \
shun-iwasawa 82a8f5
  mm4 = _mm_slli_si64(mm4, (SIZEOF_MMWORD - 2) * BYTE_BIT); /* mm4=( - - - 4) */ \
shun-iwasawa 82a8f5
  mm5 = mm7; \
shun-iwasawa 82a8f5
  mm6 = mm3; \
shun-iwasawa 82a8f5
  mm5 = _mm_srli_si64(mm5, (SIZEOF_MMWORD - 2) * BYTE_BIT); /* mm5=( 3 - - -) */ \
shun-iwasawa 82a8f5
  mm6 = _mm_slli_si64(mm6, 2 * BYTE_BIT);                   /* mm6=( - 4 5 6) */ \
shun-iwasawa 82a8f5
  \
shun-iwasawa 82a8f5
  mm0 = _mm_or_si64(mm0, mm4);                /* mm0=( 1 2 3 4) */ \
shun-iwasawa 82a8f5
  mm5 = _mm_or_si64(mm5, mm6);                /* mm5=( 3 4 5 6) */ \
shun-iwasawa 82a8f5
  \
shun-iwasawa 82a8f5
  mm1 = mm7; \
shun-iwasawa 82a8f5
  mm2 = mm3; \
shun-iwasawa 82a8f5
  mm1 = _mm_slli_si64(mm1, 2 * BYTE_BIT);     /* mm1=( - 0 1 2) */ \
shun-iwasawa 82a8f5
  mm2 = _mm_srli_si64(mm2, 2 * BYTE_BIT);     /* mm2=( 5 6 7 -) */ \
shun-iwasawa 82a8f5
  mm4 = mm3; \
shun-iwasawa 82a8f5
  mm4 = _mm_srli_si64(mm4, (SIZEOF_MMWORD - 2) * BYTE_BIT); /* mm4=( 7 - - -) */ \
shun-iwasawa 82a8f5
  \
shun-iwasawa 82a8f5
  mm1 = _mm_or_si64(mm1, wk[r]);              /* mm1=(-1 0 1 2) */ \
shun-iwasawa 82a8f5
  mm2 = _mm_or_si64(mm2, wk[r + 2]);          /* mm2=( 5 6 6 8) */ \
shun-iwasawa 82a8f5
  \
shun-iwasawa 82a8f5
  wk[r] = mm4; \
shun-iwasawa 82a8f5
  \
shun-iwasawa 82a8f5
  mm7 = _mm_mullo_pi16(mm7, PW_THREE); \
shun-iwasawa 82a8f5
  mm3 = _mm_mullo_pi16(mm3, PW_THREE); \
shun-iwasawa 82a8f5
  mm1 = _mm_add_pi16(mm1, PW_EIGHT); \
shun-iwasawa 82a8f5
  mm5 = _mm_add_pi16(mm5, PW_EIGHT); \
shun-iwasawa 82a8f5
  mm0 = _mm_add_pi16(mm0, PW_SEVEN); \
shun-iwasawa 82a8f5
  mm2 = _mm_add_pi16(mm2, PW_SEVEN); \
shun-iwasawa 82a8f5
  \
shun-iwasawa 82a8f5
  mm1 = _mm_add_pi16(mm1, mm7); \
shun-iwasawa 82a8f5
  mm5 = _mm_add_pi16(mm5, mm3); \
shun-iwasawa 82a8f5
  mm1 = _mm_srli_pi16(mm1, 4);                /* mm1=OutrLE=( 0  2  4  6) */ \
shun-iwasawa 82a8f5
  mm5 = _mm_srli_pi16(mm5, 4);                /* mm5=OutrHE=( 8 10 12 14) */ \
shun-iwasawa 82a8f5
  mm0 = _mm_add_pi16(mm0, mm7); \
shun-iwasawa 82a8f5
  mm2 = _mm_add_pi16(mm2, mm3); \
shun-iwasawa 82a8f5
  mm0 = _mm_srli_pi16(mm0, 4);                /* mm0=OutrLO=( 1  3  5  7) */ \
shun-iwasawa 82a8f5
  mm2 = _mm_srli_pi16(mm2, 4);                /* mm2=OutrHO=( 9 11 13 15) */ \
shun-iwasawa 82a8f5
  \
shun-iwasawa 82a8f5
  mm0 = _mm_slli_pi16(mm0, BYTE_BIT); \
shun-iwasawa 82a8f5
  mm2 = _mm_slli_pi16(mm2, BYTE_BIT); \
shun-iwasawa 82a8f5
  mm1 = _mm_or_si64(mm1, mm0);     /* mm1=OutrL=( 0  1  2  3  4  5  6  7) */ \
shun-iwasawa 82a8f5
  mm5 = _mm_or_si64(mm5, mm2);     /* mm5=OutrH=( 8  9 10 11 12 13 14 15) */ \
shun-iwasawa 82a8f5
  \
shun-iwasawa 82a8f5
  _mm_store_si64((__m64 *)outptr##r, mm1); \
shun-iwasawa 82a8f5
  _mm_store_si64((__m64 *)outptr##r + 1, mm5); \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
void jsimd_h2v2_fancy_upsample_mmi(int max_v_samp_factor,
shun-iwasawa 82a8f5
                                   JDIMENSION downsampled_width,
shun-iwasawa 82a8f5
                                   JSAMPARRAY input_data,
shun-iwasawa 82a8f5
                                   JSAMPARRAY *output_data_ptr)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  JSAMPARRAY output_data = *output_data_ptr;
shun-iwasawa 82a8f5
  JSAMPROW inptr_1, inptr0, inptr1, outptr0, outptr1;
shun-iwasawa 82a8f5
  int inrow, outrow, incol, tmp, tmp1;
shun-iwasawa 82a8f5
  __m64 mm0, mm1, mm2, mm3 = 0.0, mm4, mm5, mm6, mm7 = 0.0;
shun-iwasawa 82a8f5
  __m64 wk[4], mm_tmp;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (inrow = 0, outrow = 0; outrow < max_v_samp_factor; inrow++) {
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    inptr_1 = input_data[inrow - 1];
shun-iwasawa 82a8f5
    inptr0 = input_data[inrow];
shun-iwasawa 82a8f5
    inptr1 = input_data[inrow + 1];
shun-iwasawa 82a8f5
    outptr0 = output_data[outrow++];
shun-iwasawa 82a8f5
    outptr1 = output_data[outrow++];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (downsampled_width & 7) {
shun-iwasawa 82a8f5
      tmp = (downsampled_width - 1) * sizeof(JSAMPLE);
shun-iwasawa 82a8f5
      tmp1 =  downsampled_width * sizeof(JSAMPLE);
shun-iwasawa 82a8f5
      asm("daddu  $8, %3, %6\r\n"
shun-iwasawa 82a8f5
          "lb     $9, ($8)\r\n"
shun-iwasawa 82a8f5
          "daddu  $8, %3, %7\r\n"
shun-iwasawa 82a8f5
          "sb     $9, ($8)\r\n"
shun-iwasawa 82a8f5
          "daddu  $8, %4, %6\r\n"
shun-iwasawa 82a8f5
          "lb     $9, ($8)\r\n"
shun-iwasawa 82a8f5
          "daddu  $8, %4, %7\r\n"
shun-iwasawa 82a8f5
          "sb     $9, ($8)\r\n"
shun-iwasawa 82a8f5
          "daddu  $8, %5, %6\r\n"
shun-iwasawa 82a8f5
          "lb     $9, ($8)\r\n"
shun-iwasawa 82a8f5
          "daddu  $8, %5, %7\r\n"
shun-iwasawa 82a8f5
          "sb     $9, ($8)\r\n"
shun-iwasawa 82a8f5
          : "=m" (*inptr_1), "=m" (*inptr0), "=m" (*inptr1)
shun-iwasawa 82a8f5
          : "r" (inptr_1), "r" (inptr0), "r" (inptr1), "r" (tmp), "r" (tmp1)
shun-iwasawa 82a8f5
          : "$8", "$9"
shun-iwasawa 82a8f5
         );
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* process the first column block */
shun-iwasawa 82a8f5
    mm0 = _mm_load_si64((__m64 *)inptr0);     /* mm0 = row[ 0][0] */
shun-iwasawa 82a8f5
    mm1 = _mm_load_si64((__m64 *)inptr_1);    /* mm1 = row[-1][0] */
shun-iwasawa 82a8f5
    mm2 = _mm_load_si64((__m64 *)inptr1);     /* mm2 = row[ 1][0] */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mm3 = _mm_xor_si64(mm3, mm3);             /* mm3 = (all 0's) */
shun-iwasawa 82a8f5
    mm4 = mm0;
shun-iwasawa 82a8f5
    mm0 = _mm_unpacklo_pi8(mm0, mm3);         /* mm0 = row[ 0][0]( 0 1 2 3) */
shun-iwasawa 82a8f5
    mm4 = _mm_unpackhi_pi8(mm4, mm3);         /* mm4 = row[ 0][0]( 4 5 6 7) */
shun-iwasawa 82a8f5
    mm5 = mm1;
shun-iwasawa 82a8f5
    mm1 = _mm_unpacklo_pi8(mm1, mm3);         /* mm1 = row[-1][0]( 0 1 2 3) */
shun-iwasawa 82a8f5
    mm5 = _mm_unpackhi_pi8(mm5, mm3);         /* mm5 = row[-1][0]( 4 5 6 7) */
shun-iwasawa 82a8f5
    mm6 = mm2;
shun-iwasawa 82a8f5
    mm2 = _mm_unpacklo_pi8(mm2, mm3);         /* mm2 = row[+1][0]( 0 1 2 3) */
shun-iwasawa 82a8f5
    mm6 = _mm_unpackhi_pi8(mm6, mm3);         /* mm6 = row[+1][0]( 4 5 6 7) */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mm0 = _mm_mullo_pi16(mm0, PW_THREE);
shun-iwasawa 82a8f5
    mm4 = _mm_mullo_pi16(mm4, PW_THREE);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mm7 = _mm_cmpeq_pi8(mm7, mm7);
shun-iwasawa 82a8f5
    mm7 = _mm_srli_si64(mm7, (SIZEOF_MMWORD - 2) * BYTE_BIT);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mm1 = _mm_add_pi16(mm1, mm0);             /* mm1=Int0L=( 0 1 2 3) */
shun-iwasawa 82a8f5
    mm5 = _mm_add_pi16(mm5, mm4);             /* mm5=Int0H=( 4 5 6 7) */
shun-iwasawa 82a8f5
    mm2 = _mm_add_pi16(mm2, mm0);             /* mm2=Int1L=( 0 1 2 3) */
shun-iwasawa 82a8f5
    mm6 = _mm_add_pi16(mm6, mm4);             /* mm6=Int1H=( 4 5 6 7) */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    _mm_store_si64((__m64 *)outptr0, mm1);      /* temporarily save */
shun-iwasawa 82a8f5
    _mm_store_si64((__m64 *)outptr0 + 1, mm5);  /* the intermediate data */
shun-iwasawa 82a8f5
    _mm_store_si64((__m64 *)outptr1, mm2);
shun-iwasawa 82a8f5
    _mm_store_si64((__m64 *)outptr1 + 1, mm6);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    mm1 = _mm_and_si64(mm1, mm7);             /* mm1=( 0 - - -) */
shun-iwasawa 82a8f5
    mm2 = _mm_and_si64(mm2, mm7);             /* mm2=( 0 - - -) */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    wk[0] = mm1;
shun-iwasawa 82a8f5
    wk[1] = mm2;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    for (incol = downsampled_width; incol > 0;
shun-iwasawa 82a8f5
         incol -= 8, inptr_1 += 8, inptr0 += 8, inptr1 += 8,
shun-iwasawa 82a8f5
         outptr0 += 16, outptr1 += 16) {
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      if (incol > 8) {
shun-iwasawa 82a8f5
        /* process the next column block */
shun-iwasawa 82a8f5
        mm0 = _mm_load_si64((__m64 *)inptr0 + 1);   /* mm0 = row[ 0][1] */
shun-iwasawa 82a8f5
        mm1 = _mm_load_si64((__m64 *)inptr_1 + 1);  /* mm1 = row[-1][1] */
shun-iwasawa 82a8f5
        mm2 = _mm_load_si64((__m64 *)inptr1 + 1);   /* mm2 = row[+1][1] */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        mm3 = _mm_setzero_si64();             /* mm3 = (all 0's) */
shun-iwasawa 82a8f5
        mm4 = mm0;
shun-iwasawa 82a8f5
        mm0 = _mm_unpacklo_pi8(mm0, mm3);     /* mm0 = row[ 0][1]( 0 1 2 3) */
shun-iwasawa 82a8f5
        mm4 = _mm_unpackhi_pi8(mm4, mm3);     /* mm4 = row[ 0][1]( 4 5 6 7) */
shun-iwasawa 82a8f5
        mm5 = mm1;
shun-iwasawa 82a8f5
        mm1 = _mm_unpacklo_pi8(mm1, mm3);     /* mm1 = row[-1][1]( 0 1 2 3) */
shun-iwasawa 82a8f5
        mm5 = _mm_unpackhi_pi8(mm5, mm3);     /* mm5 = row[-1][1]( 4 5 6 7) */
shun-iwasawa 82a8f5
        mm6 = mm2;
shun-iwasawa 82a8f5
        mm2 = _mm_unpacklo_pi8(mm2, mm3);     /* mm2 = row[+1][1]( 0 1 2 3) */
shun-iwasawa 82a8f5
        mm6 = _mm_unpackhi_pi8(mm6, mm3);     /* mm6 = row[+1][1]( 4 5 6 7) */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        mm0 = _mm_mullo_pi16(mm0, PW_THREE);
shun-iwasawa 82a8f5
        mm4 = _mm_mullo_pi16(mm4, PW_THREE);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        mm1 = _mm_add_pi16(mm1, mm0);         /* mm1 = Int0L = ( 0 1 2 3) */
shun-iwasawa 82a8f5
        mm5 = _mm_add_pi16(mm5, mm4);         /* mm5 = Int0H = ( 4 5 6 7) */
shun-iwasawa 82a8f5
        mm2 = _mm_add_pi16(mm2, mm0);         /* mm2 = Int1L = ( 0 1 2 3) */
shun-iwasawa 82a8f5
        mm6 = _mm_add_pi16(mm6, mm4);         /* mm6 = Int1H = ( 4 5 6 7) */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        _mm_store_si64((__m64 *)outptr0 + 2, mm1);  /* temporarily save */
shun-iwasawa 82a8f5
        _mm_store_si64((__m64 *)outptr0 + 3, mm5);  /* the intermediate data */
shun-iwasawa 82a8f5
        _mm_store_si64((__m64 *)outptr1 + 2, mm2);
shun-iwasawa 82a8f5
        _mm_store_si64((__m64 *)outptr1 + 3, mm6);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        mm1 = _mm_slli_si64(mm1, (SIZEOF_MMWORD - 2) * BYTE_BIT); /* mm1=( - - - 0) */
shun-iwasawa 82a8f5
        mm2 = _mm_slli_si64(mm2, (SIZEOF_MMWORD - 2) * BYTE_BIT); /* mm2=( - - - 0) */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        wk[2] = mm1;
shun-iwasawa 82a8f5
        wk[3] = mm2;
shun-iwasawa 82a8f5
      } else {
shun-iwasawa 82a8f5
        /* process the last column block */
shun-iwasawa 82a8f5
        mm1 = _mm_cmpeq_pi8(mm1, mm1);
shun-iwasawa 82a8f5
        mm1 = _mm_slli_si64(mm1, (SIZEOF_MMWORD - 2) * BYTE_BIT);
shun-iwasawa 82a8f5
        mm2 = mm1;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        mm_tmp = _mm_load_si64((__m64 *)outptr0 + 1);
shun-iwasawa 82a8f5
        mm1 = _mm_and_si64(mm1, mm_tmp);      /* mm1=( - - - 7) */
shun-iwasawa 82a8f5
        mm_tmp = _mm_load_si64((__m64 *)outptr1 + 1);
shun-iwasawa 82a8f5
        mm2 = _mm_and_si64(mm2, mm_tmp);      /* mm2=( - - - 7) */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        wk[2] = mm1;
shun-iwasawa 82a8f5
        wk[3] = mm2;
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      /* process the upper row */
shun-iwasawa 82a8f5
      PROCESS_ROW(0)
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
      /* process the lower row */
shun-iwasawa 82a8f5
      PROCESS_ROW(1)
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}