OpenJPH
Open-source implementation of JPEG2000 Part-15
ojph_transform_sse2.cpp
Go to the documentation of this file.
1 //***************************************************************************/
2 // This software is released under the 2-Clause BSD license, included
3 // below.
4 //
5 // Copyright (c) 2019, Aous Naman
6 // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia
7 // Copyright (c) 2019, The University of New South Wales, Australia
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are
11 // met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 //***************************************************************************/
32 // This file is part of the OpenJPH software implementation.
33 // File: ojph_transform_sse2.cpp
34 // Author: Aous Naman
35 // Date: 28 August 2019
36 //***************************************************************************/
37 
38 #include <cstdio>
39 
40 #include "ojph_defs.h"
41 #include "ojph_arch.h"
42 #include "ojph_mem.h"
43 #include "ojph_transform.h"
44 #include "ojph_transform_local.h"
45 
46 #ifdef OJPH_COMPILER_MSVC
47 #include <intrin.h>
48 #else
49 #include <x86intrin.h>
50 #endif
51 
52 namespace ojph {
53  namespace local {
54 
57  const line_buf* line_src2,
58  line_buf *line_dst, ui32 repeat)
59  {
60  si32 *dst = line_dst->i32;
61  const si32 *src1 = line_src1->i32, *src2 = line_src2->i32;
62 
63  for (ui32 i = (repeat + 3) >> 2; i > 0; --i, dst+=4, src1+=4, src2+=4)
64  {
65  __m128i s1 = _mm_load_si128((__m128i*)src1);
66  __m128i s2 = _mm_load_si128((__m128i*)src2);
67  __m128i d = _mm_load_si128((__m128i*)dst);
68  s1 = _mm_srai_epi32(_mm_add_epi32(s1, s2), 1);
69  d = _mm_sub_epi32(d, s1);
70  _mm_store_si128((__m128i*)dst, d);
71  }
72  }
73 
75  void sse2_rev_vert_wvlt_fwd_update(const line_buf* line_src1,
76  const line_buf* line_src2,
77  line_buf *line_dst, ui32 repeat)
78  {
79  si32 *dst = line_dst->i32;
80  const si32 *src1 = line_src1->i32, *src2 = line_src2->i32;
81 
82  __m128i offset = _mm_set1_epi32(2);
83  for (ui32 i = (repeat + 3) >> 2; i > 0; --i, dst+=4, src1+=4, src2+=4)
84  {
85  __m128i s1 = _mm_load_si128((__m128i*)src1);
86  s1 = _mm_add_epi32(s1, offset);
87  __m128i s2 = _mm_load_si128((__m128i*)src2);
88  s2 = _mm_add_epi32(s2, s1);
89  __m128i d = _mm_load_si128((__m128i*)dst);
90  d = _mm_add_epi32(d, _mm_srai_epi32(s2, 2));
91  _mm_store_si128((__m128i*)dst, d);
92  }
93  }
94 
96  void sse2_rev_horz_wvlt_fwd_tx(line_buf *line_src, line_buf *line_ldst,
97  line_buf *line_hdst, ui32 width, bool even)
98  {
99  if (width > 1)
100  {
101  si32 *src = line_src->i32;
102  si32 *ldst = line_ldst->i32, *hdst = line_hdst->i32;
103 
104  const ui32 L_width = (width + (even ? 1 : 0)) >> 1;
105  const ui32 H_width = (width + (even ? 0 : 1)) >> 1;
106 
107  // extension
108  src[-1] = src[1];
109  src[width] = src[width-2];
110  // predict
111  const si32* sp = src + (even ? 1 : 0);
112  si32 *dph = hdst;
113  for (ui32 i = (H_width + 3) >> 2; i > 0; --i, dph+=4)
114  { //this is doing twice the work it needs to do
115  //it can be definitely written better
116  __m128i s1 = _mm_loadu_si128((__m128i*)(sp-1));
117  __m128i s2 = _mm_loadu_si128((__m128i*)(sp+1));
118  __m128i d = _mm_loadu_si128((__m128i*)sp);
119  s1 = _mm_srai_epi32(_mm_add_epi32(s1, s2), 1);
120  __m128i d1 = _mm_sub_epi32(d, s1);
121  sp += 4;
122  s1 = _mm_loadu_si128((__m128i*)(sp-1));
123  s2 = _mm_loadu_si128((__m128i*)(sp+1));
124  d = _mm_loadu_si128((__m128i*)sp);
125  s1 = _mm_srai_epi32(_mm_add_epi32(s1, s2), 1);
126  __m128i d2 = _mm_sub_epi32(d, s1);
127  sp += 4;
128  d = _mm_castps_si128(_mm_shuffle_ps(
129  _mm_castsi128_ps(d1), _mm_castsi128_ps(d2), 0x88));
130  _mm_store_si128((__m128i*)dph, d);
131  }
132 
133  // extension
134  hdst[-1] = hdst[0];
135  hdst[H_width] = hdst[H_width-1];
136  // update
137  sp = src + (even ? 0 : 1);
138  const si32* sph = hdst + (even ? 0 : 1);
139  si32 *dpl = ldst;
140  __m128i offset = _mm_set1_epi32(2);
141  for (ui32 i = (L_width + 3) >> 2; i > 0; --i, sp+=8, sph+=4, dpl+=4)
142  {
143  __m128i s1 = _mm_loadu_si128((__m128i*)(sph-1));
144  s1 = _mm_add_epi32(s1, offset);
145  __m128i s2 = _mm_loadu_si128((__m128i*)sph);
146  s2 = _mm_add_epi32(s2, s1);
147  __m128i d1 = _mm_loadu_si128((__m128i*)sp);
148  __m128i d2 = _mm_loadu_si128((__m128i*)sp + 1);
149  __m128i d = _mm_castps_si128(_mm_shuffle_ps(
150  _mm_castsi128_ps(d1), _mm_castsi128_ps(d2), 0x88));
151  d = _mm_add_epi32(d, _mm_srai_epi32(s2, 2));
152  _mm_store_si128((__m128i*)dpl, d);
153  }
154  }
155  else
156  {
157  if (even)
158  line_ldst->i32[0] = line_src->i32[0];
159  else
160  line_hdst->i32[0] = line_src->i32[0] << 1;
161  }
162  }
163 
166  const line_buf* line_src2,
167  line_buf *line_dst, ui32 repeat)
168  {
169  si32 *dst = line_dst->i32;
170  const si32 *src1 = line_src1->i32, *src2 = line_src2->i32;
171 
172  for (ui32 i = (repeat + 3) >> 2; i > 0; --i, dst+=4, src1+=4, src2+=4)
173  {
174  __m128i s1 = _mm_load_si128((__m128i*)src1);
175  __m128i s2 = _mm_load_si128((__m128i*)src2);
176  __m128i d = _mm_load_si128((__m128i*)dst);
177  s1 = _mm_srai_epi32(_mm_add_epi32(s1, s2), 1);
178  d = _mm_add_epi32(d, s1);
179  _mm_store_si128((__m128i*)dst, d);
180  }
181  }
182 
185  const line_buf* line_src2,
186  line_buf *line_dst, ui32 repeat)
187  {
188  si32 *dst = line_dst->i32;
189  const si32 *src1 = line_src1->i32, *src2 = line_src2->i32;
190 
191  __m128i offset = _mm_set1_epi32(2);
192  for (ui32 i = (repeat + 3) >> 2; i > 0; --i, dst+=4, src1+=4, src2+=4)
193  {
194  __m128i s1 = _mm_load_si128((__m128i*)src1);
195  s1 = _mm_add_epi32(s1, offset);
196  __m128i s2 = _mm_load_si128((__m128i*)src2);
197  s2 = _mm_add_epi32(s2, s1);
198  __m128i d = _mm_load_si128((__m128i*)dst);
199  d = _mm_sub_epi32(d, _mm_srai_epi32(s2, 2));
200  _mm_store_si128((__m128i*)dst, d);
201  }
202  }
203 
205  void sse2_rev_horz_wvlt_bwd_tx(line_buf *line_dst, line_buf *line_lsrc,
206  line_buf *line_hsrc, ui32 width, bool even)
207  {
208  if (width > 1)
209  {
210  si32 *lsrc = line_lsrc->i32, *hsrc = line_hsrc->i32;
211  si32 *dst = line_dst->i32;
212 
213  const ui32 L_width = (width + (even ? 1 : 0)) >> 1;
214  const ui32 H_width = (width + (even ? 0 : 1)) >> 1;
215 
216  // extension
217  hsrc[-1] = hsrc[0];
218  hsrc[H_width] = hsrc[H_width-1];
219  //inverse update
220  const si32 *sph = hsrc + (even ? 0 : 1);
221  si32 *spl = lsrc;
222  __m128i offset = _mm_set1_epi32(2);
223  for (ui32 i = (L_width + 3) >> 2; i > 0; --i, sph+=4, spl+=4)
224  {
225  __m128i s1 = _mm_loadu_si128((__m128i*)(sph-1));
226  s1 = _mm_add_epi32(s1, offset);
227  __m128i s2 = _mm_loadu_si128((__m128i*)sph);
228  s2 = _mm_add_epi32(s2, s1);
229  __m128i d = _mm_load_si128((__m128i*)spl);
230  d = _mm_sub_epi32(d, _mm_srai_epi32(s2, 2));
231  _mm_store_si128((__m128i*)spl, d);
232  }
233 
234  // extension
235  lsrc[-1] = lsrc[0];
236  lsrc[L_width] = lsrc[L_width - 1];
237  // inverse predict and combine
238  si32 *dp = dst + (even ? 0 : -1);
239  spl = lsrc + (even ? 0 : -1);
240  sph = hsrc;
241  ui32 width = L_width + (even ? 0 : 1);
242  for (ui32 i = (width + 3) >> 2; i > 0; --i, sph+=4, spl+=4, dp+=8)
243  {
244  __m128i s1 = _mm_loadu_si128((__m128i*)spl);
245  __m128i s2 = _mm_loadu_si128((__m128i*)(spl+1));
246  __m128i d = _mm_load_si128((__m128i*)sph);
247  s2 = _mm_srai_epi32(_mm_add_epi32(s1, s2), 1);
248  d = _mm_add_epi32(d, s2);
249  _mm_storeu_si128((__m128i*)dp, _mm_unpacklo_epi32(s1, d));
250  _mm_storeu_si128((__m128i*)dp + 1, _mm_unpackhi_epi32(s1, d));
251  }
252  }
253  else
254  {
255  if (even)
256  line_dst->i32[0] = line_lsrc->i32[0];
257  else
258  line_dst->i32[0] = line_hsrc->i32[0] >> 1;
259  }
260  }
261  }
262 }
void sse2_rev_horz_wvlt_fwd_tx(line_buf *src, line_buf *ldst, line_buf *hdst, ui32 width, bool even)
void sse2_rev_vert_wvlt_fwd_update(const line_buf *src1, const line_buf *src2, line_buf *dst, ui32 repeat)
void sse2_rev_horz_wvlt_bwd_tx(line_buf *dst, line_buf *lsrc, line_buf *hsrc, ui32 width, bool even)
void sse2_rev_vert_wvlt_bwd_predict(const line_buf *src1, const line_buf *src2, line_buf *dst, ui32 repeat)
void sse2_rev_vert_wvlt_fwd_predict(const line_buf *src1, const line_buf *src2, line_buf *dst, ui32 repeat)
void sse2_rev_vert_wvlt_bwd_update(const line_buf *src1, const line_buf *src2, line_buf *dst, ui32 repeat)
int32_t si32
Definition: ojph_defs.h:55
uint32_t ui32
Definition: ojph_defs.h:54
si32 * i32
Definition: ojph_mem.h:155