OpenJPH: /build/openjph/src/OpenJPH/src/core/transform/ojph_transform_sse.cpp Source File

OpenJPH Open-source implementation of JPEG2000 Part-15

Loading...
Searching...
No Matches
ojph_transform_sse.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_sse.cpp
34// Author: Aous Naman
35// Date: 28 August 2019
36//***************************************************************************/
37
38#include "ojph_arch.h"
39#if defined(OJPH_ARCH_I386) || defined(OJPH_ARCH_X86_64)
40
41#include <cstdio>
42#include <xmmintrin.h>
43
44#include "ojph_defs.h"
45#include "ojph_mem.h"
46#include "ojph_params.h"
47#include "../codestream/ojph_params_local.h"
48
49#include "ojph_transform.h"
50#include "ojph_transform_local.h"
51
52namespace ojph {
53 namespace local {
54
56 static inline
57 void sse_deinterleave32(float* dpl, float* dph, float* sp, int width)
58 {
59 for (; width > 0; width -= 8, sp += 8, dpl += 4, dph += 4)
60 {
61 __m128 a = _mm_load_ps(sp);
62 __m128 b = _mm_load_ps(sp + 4);
63 __m128 c = _mm_shuffle_ps(a, b, _MM_SHUFFLE(2, 0, 2, 0));
64 __m128 d = _mm_shuffle_ps(a, b, _MM_SHUFFLE(3, 1, 3, 1));
65 _mm_store_ps(dpl, c);
66 _mm_store_ps(dph, d);
67 }
68 }
69
71 static inline
72 void sse_interleave32(float* dp, float* spl, float* sph, int width) \
73 {
74 for (; width > 0; width -= 8, dp += 8, spl += 4, sph += 4)
75 {
76 __m128 a = _mm_load_ps(spl);
77 __m128 b = _mm_load_ps(sph);
78 __m128 c = _mm_unpacklo_ps(a, b);
79 __m128 d = _mm_unpackhi_ps(a, b);
80 _mm_store_ps(dp, c);
81 _mm_store_ps(dp + 4, d);
82 }
83 }
84
86 static inline void sse_multiply_const(float* p, float f, int width)
87 {
88 __m128 factor = _mm_set1_ps(f);
89 for (; width > 0; width -= 4, p += 4)
90 {
91 __m128 s = _mm_load_ps(p);
92 _mm_store_ps(p, _mm_mul_ps(factor, s));
93 }
94 }
95
97 void sse_irv_vert_step(const lifting_step* s, const line_buf* sig,
98 const line_buf* other, const line_buf* aug,
99 ui32 repeat, bool synthesis)
100 {
101 float a = s->irv.Aatk;
102 if (synthesis)
103 a = -a;
104
105 __m128 factor = _mm_set1_ps(a);
106
107 float* dst = aug->f32;
108 const float* src1 = sig->f32, * src2 = other->f32;
109 int i = (int)repeat;
110 for ( ; i > 0; i -= 4, dst += 4, src1 += 4, src2 += 4)
111 {
112 __m128 s1 = _mm_load_ps(src1);
113 __m128 s2 = _mm_load_ps(src2);
114 __m128 d = _mm_load_ps(dst);
115 d = _mm_add_ps(d, _mm_mul_ps(factor, _mm_add_ps(s1, s2)));
116 _mm_store_ps(dst, d);
117 }
118 }
119
121 void sse_irv_vert_times_K(float K, const line_buf* aug, ui32 repeat)
122 {
123 sse_multiply_const(aug->f32, K, (int)repeat);
124 }
125
127 void sse_irv_horz_ana(const param_atk* atk, const line_buf* ldst,
128 const line_buf* hdst, const line_buf* src,
129 ui32 width, bool even)
130 {
131 if (width > 1)
132 {
133 // split src into ldst and hdst
134 {
135 float* dpl = even ? ldst->f32 : hdst->f32;
136 float* dph = even ? hdst->f32 : ldst->f32;
137 float* sp = src->f32;
138 int w = (int)width;
139 sse_deinterleave32(dpl, dph, sp, w);
140 }
141
142 // the actual horizontal transform
143 float* hp = hdst->f32, * lp = ldst->f32;
144 ui32 l_width = (width + (even ? 1 : 0)) >> 1; // low pass
145 ui32 h_width = (width + (even ? 0 : 1)) >> 1; // high pass
146 ui32 num_steps = atk->get_num_steps();
147 for (ui32 j = num_steps; j > 0; --j)
148 {
149 const lifting_step* s = atk->get_step(j - 1);
150 const float a = s->irv.Aatk;
151
152 // extension
153 lp[-1] = lp[0];
154 lp[l_width] = lp[l_width - 1];
155 // lifting step
156 const float* sp = lp;
157 float* dp = hp;
158 int i = (int)h_width;
159 __m128 f = _mm_set1_ps(a);
160 if (even)
161 {
162 for (; i > 0; i -= 4, sp += 4, dp += 4)
163 {
164 __m128 m = _mm_load_ps(sp);
165 __m128 n = _mm_loadu_ps(sp + 1);
166 __m128 p = _mm_load_ps(dp);
167 p = _mm_add_ps(p, _mm_mul_ps(f, _mm_add_ps(m, n)));
168 _mm_store_ps(dp, p);
169 }
170 }
171 else
172 {
173 for (; i > 0; i -= 4, sp += 4, dp += 4)
174 {
175 __m128 m = _mm_load_ps(sp);
176 __m128 n = _mm_loadu_ps(sp - 1);
177 __m128 p = _mm_load_ps(dp);
178 p = _mm_add_ps(p, _mm_mul_ps(f, _mm_add_ps(m, n)));
179 _mm_store_ps(dp, p);
180 }
181 }
182
183 // swap buffers
184 float* t = lp; lp = hp; hp = t;
185 even = !even;
186 ui32 w = l_width; l_width = h_width; h_width = w;
187 }
188
189 { // multiply by K or 1/K
190 float K = atk->get_K();
191 float K_inv = 1.0f / K;
192 sse_multiply_const(lp, K_inv, (int)l_width);
193 sse_multiply_const(hp, K, (int)h_width);
194 }
195 }
196 else {
197 if (even)
198 ldst->f32[0] = src->f32[0];
199 else
200 hdst->f32[0] = src->f32[0] * 2.0f;
201 }
202 }
203
205 void sse_irv_horz_syn(const param_atk* atk, const line_buf* dst,
206 const line_buf* lsrc, const line_buf* hsrc,
207 ui32 width, bool even)
208 {
209 if (width > 1)
210 {
211 bool ev = even;
212 float* oth = hsrc->f32, * aug = lsrc->f32;
213 ui32 aug_width = (width + (even ? 1 : 0)) >> 1; // low pass
214 ui32 oth_width = (width + (even ? 0 : 1)) >> 1; // high pass
215
216 { // multiply by K or 1/K
217 float K = atk->get_K();
218 float K_inv = 1.0f / K;
219 sse_multiply_const(aug, K, (int)aug_width);
220 sse_multiply_const(oth, K_inv, (int)oth_width);
221 }
222
223 // the actual horizontal transform
224 ui32 num_steps = atk->get_num_steps();
225 for (ui32 j = 0; j < num_steps; ++j)
226 {
227 const lifting_step* s = atk->get_step(j);
228 const float a = s->irv.Aatk;
229
230 // extension
231 oth[-1] = oth[0];
232 oth[oth_width] = oth[oth_width - 1];
233 // lifting step
234 const float* sp = oth;
235 float* dp = aug;
236 int i = (int)aug_width;
237 __m128 f = _mm_set1_ps(a);
238 if (ev)
239 {
240 for ( ; i > 0; i -= 4, sp += 4, dp += 4)
241 {
242 __m128 m = _mm_load_ps(sp);
243 __m128 n = _mm_loadu_ps(sp - 1);
244 __m128 p = _mm_load_ps(dp);
245 p = _mm_sub_ps(p, _mm_mul_ps(f, _mm_add_ps(m, n)));
246 _mm_store_ps(dp, p);
247 }
248 }
249 else
250 {
251 for ( ; i > 0; i -= 4, sp += 4, dp += 4)
252 {
253 __m128 m = _mm_load_ps(sp);
254 __m128 n = _mm_loadu_ps(sp + 1);
255 __m128 p = _mm_load_ps(dp);
256 p = _mm_sub_ps(p, _mm_mul_ps(f, _mm_add_ps(m, n)));
257 _mm_store_ps(dp, p);
258 }
259 }
260
261 // swap buffers
262 float* t = aug; aug = oth; oth = t;
263 ev = !ev;
264 ui32 w = aug_width; aug_width = oth_width; oth_width = w;
265 }
266
267 // combine both lsrc and hsrc into dst
268 {
269 float* dp = dst->f32;
270 float* spl = even ? lsrc->f32 : hsrc->f32;
271 float* sph = even ? hsrc->f32 : lsrc->f32;
272 int w = (int)width;
273 sse_interleave32(dp, spl, sph, w);
274 }
275 }
276 else {
277 if (even)
278 dst->f32[0] = lsrc->f32[0];
279 else
280 dst->f32[0] = hsrc->f32[0] * 0.5f;
281 }
282 }
283
284 } // !local
285} // !ojph
286
287#endif
ojph::local
Definition ojph_bitbuffer_read.h:52
ojph::local::sse_irv_vert_times_K
void sse_irv_vert_times_K(float K, const line_buf *aug, ui32 repeat)
ojph::local::sse_irv_vert_step
void sse_irv_vert_step(const lifting_step *s, const line_buf *sig, const line_buf *other, const line_buf *aug, ui32 repeat, bool synthesis)
ojph::local::sse_irv_horz_ana
void sse_irv_horz_ana(const param_atk *atk, const line_buf *ldst, const line_buf *hdst, const line_buf *src, ui32 width, bool even)
ojph::local::sse_irv_horz_syn
void sse_irv_horz_syn(const param_atk *atk, const line_buf *dst, const line_buf *lsrc, const line_buf *hsrc, ui32 width, bool even)
ojph
Definition ojph_img_io.h:52
ojph::ui32
uint32_t ui32
Definition ojph_defs.h:54
ojph_arch.h
ojph_defs.h
ojph_mem.h
ojph_params.h
ojph_params_local.h
ojph_transform.h
ojph_transform_local.h
ojph::local::lifting_step::irv_data::Aatk
float Aatk
Definition ojph_params_local.h:1069
ojph::local::param_atk
Definition ojph_params_local.h:1085
ojph::local::lifting_step
Definition ojph_params_local.h:1065
ojph::local::lifting_step::irv
irv_data irv
Definition ojph_params_local.h:1080