jcsample-mmi.c 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Loongson MMI optimizations for libjpeg-turbo
  3. *
  4. * Copyright (C) 2015, 2018-2019, D. R. Commander. All Rights Reserved.
  5. * Copyright (C) 2016-2017, Loongson Technology Corporation Limited, BeiJing.
  6. * All Rights Reserved.
  7. * Authors: ZhuChen <zhuchen@loongson.cn>
  8. * CaiWanwei <caiwanwei@loongson.cn>
  9. * SunZhangzhi <sunzhangzhi-cq@loongson.cn>
  10. *
  11. * Based on the x86 SIMD extension for IJG JPEG library
  12. * Copyright (C) 1999-2006, MIYASAKA Masaru.
  13. *
  14. * This software is provided 'as-is', without any express or implied
  15. * warranty. In no event will the authors be held liable for any damages
  16. * arising from the use of this software.
  17. *
  18. * Permission is granted to anyone to use this software for any purpose,
  19. * including commercial applications, and to alter it and redistribute it
  20. * freely, subject to the following restrictions:
  21. *
  22. * 1. The origin of this software must not be misrepresented; you must not
  23. * claim that you wrote the original software. If you use this software
  24. * in a product, an acknowledgment in the product documentation would be
  25. * appreciated but is not required.
  26. * 2. Altered source versions must be plainly marked as such, and must not be
  27. * misrepresented as being the original software.
  28. * 3. This notice may not be removed or altered from any source distribution.
  29. */
  30. /* CHROMA DOWNSAMPLING */
  31. #include "jsimd_mmi.h"
  32. #include "jcsample.h"
  33. void jsimd_h2v2_downsample_mmi(JDIMENSION image_width, int max_v_samp_factor,
  34. JDIMENSION v_samp_factor,
  35. JDIMENSION width_in_blocks,
  36. JSAMPARRAY input_data, JSAMPARRAY output_data)
  37. {
  38. int inrow, outrow, outcol;
  39. JDIMENSION output_cols = width_in_blocks * DCTSIZE;
  40. JSAMPROW inptr0, inptr1, outptr;
  41. __m64 bias, mask = 0.0, thisavg, nextavg, avg;
  42. __m64 this0o, this0e, this0, this0sum, next0o, next0e, next0, next0sum;
  43. __m64 this1o, this1e, this1, this1sum, next1o, next1e, next1, next1sum;
  44. expand_right_edge(input_data, max_v_samp_factor, image_width,
  45. output_cols * 2);
  46. bias = _mm_set1_pi32((1 << 17) + 1); /* 0x00020001 (32-bit bias pattern) */
  47. /* bias={1, 2, 1, 2} (16-bit) */
  48. mask = _mm_cmpeq_pi16(mask, mask);
  49. mask = _mm_srli_pi16(mask, BYTE_BIT); /* {0xFF 0x00 0xFF 0x00 ..} */
  50. for (inrow = 0, outrow = 0; outrow < v_samp_factor;
  51. inrow += 2, outrow++) {
  52. inptr0 = input_data[inrow];
  53. inptr1 = input_data[inrow + 1];
  54. outptr = output_data[outrow];
  55. for (outcol = output_cols; outcol > 0;
  56. outcol -= 8, inptr0 += 16, inptr1 += 16, outptr += 8) {
  57. this0 = _mm_load_si64((__m64 *)&inptr0[0]);
  58. this1 = _mm_load_si64((__m64 *)&inptr1[0]);
  59. next0 = _mm_load_si64((__m64 *)&inptr0[8]);
  60. next1 = _mm_load_si64((__m64 *)&inptr1[8]);
  61. this0o = _mm_and_si64(this0, mask);
  62. this0e = _mm_srli_pi16(this0, BYTE_BIT);
  63. this1o = _mm_and_si64(this1, mask);
  64. this1e = _mm_srli_pi16(this1, BYTE_BIT);
  65. this0sum = _mm_add_pi16(this0o, this0e);
  66. this1sum = _mm_add_pi16(this1o, this1e);
  67. next0o = _mm_and_si64(next0, mask);
  68. next0e = _mm_srli_pi16(next0, BYTE_BIT);
  69. next1o = _mm_and_si64(next1, mask);
  70. next1e = _mm_srli_pi16(next1, BYTE_BIT);
  71. next0sum = _mm_add_pi16(next0o, next0e);
  72. next1sum = _mm_add_pi16(next1o, next1e);
  73. thisavg = _mm_add_pi16(this0sum, this1sum);
  74. nextavg = _mm_add_pi16(next0sum, next1sum);
  75. thisavg = _mm_add_pi16(thisavg, bias);
  76. nextavg = _mm_add_pi16(nextavg, bias);
  77. thisavg = _mm_srli_pi16(thisavg, 2);
  78. nextavg = _mm_srli_pi16(nextavg, 2);
  79. avg = _mm_packs_pu16(thisavg, nextavg);
  80. _mm_store_si64((__m64 *)&outptr[0], avg);
  81. }
  82. }
  83. }