OpenJPH Open-source implementation of JPEG2000 Part-15

Loading...
Searching...
No Matches
ojph_transform_avx.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_avx.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 <immintrin.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
52 namespace ojph {
53 namespace local {
54
56 static inline void avx_multiply_const(float* p, float f, int width)
57 {
58 __m256 factor = _mm256_set1_ps(f);
59 for (; width > 0; width -= 8, p += 8)
60 {
61 __m256 s = _mm256_load_ps(p);
62 _mm256_store_ps(p, _mm256_mul_ps(factor, s));
63 }
64 }
65
67 static inline
68 void avx_deinterleave32(float* dpl, float* dph, float* sp, int width)
69 {
70 for (; width > 0; width -= 16, sp += 16, dpl += 8, dph += 8)
71 {
72 __m256 a = _mm256_load_ps(sp);
73 __m256 b = _mm256_load_ps(sp + 8);
74 __m256 c = _mm256_permute2f128_ps(a, b, (2 << 4) | (0));
75 __m256 d = _mm256_permute2f128_ps(a, b, (3 << 4) | (1));
76 __m256 e = _mm256_shuffle_ps(c, d, _MM_SHUFFLE(2, 0, 2, 0));
77 __m256 f = _mm256_shuffle_ps(c, d, _MM_SHUFFLE(3, 1, 3, 1));
78 _mm256_store_ps(dpl, e);
79 _mm256_store_ps(dph, f);
80 }
81 }
82
84 static inline
85 void avx_interleave32(float* dp, float* spl, float* sph, int width)
86 {
87 for (; width > 0; width -= 16, dp += 16, spl += 8, sph += 8)
88 {
89 __m256 a = _mm256_load_ps(spl);
90 __m256 b = _mm256_load_ps(sph);
91 __m256 c = _mm256_unpacklo_ps(a, b);
92 __m256 d = _mm256_unpackhi_ps(a, b);
93 __m256 e = _mm256_permute2f128_ps(c, d, (2 << 4) | (0));
94 __m256 f = _mm256_permute2f128_ps(c, d, (3 << 4) | (1));
95 _mm256_store_ps(dp, e);
96 _mm256_store_ps(dp + 8, f);
97 }
98 }
99
101 void avx_irv_vert_step(const lifting_step* s, const line_buf* sig,
102 const line_buf* other, const line_buf* aug,
103 ui32 repeat, bool synthesis)
104 {
105 float a = s->irv.Aatk;
106 if (synthesis)
107 a = -a;
108
109 __m256 factor = _mm256_set1_ps(a);
110
111 float* dst = aug->f32;
112 const float* src1 = sig->f32, * src2 = other->f32;
113 int i = (int)repeat;
114 for ( ; i > 0; i -= 8, dst += 8, src1 += 8, src2 += 8)
115 {
116 __m256 s1 = _mm256_load_ps(src1);
117 __m256 s2 = _mm256_load_ps(src2);
118 __m256 d = _mm256_load_ps(dst);
119 d = _mm256_add_ps(d, _mm256_mul_ps(factor, _mm256_add_ps(s1, s2)));
120 _mm256_store_ps(dst, d);
121 }
122 }
123
125 void avx_irv_vert_times_K(float K, const line_buf* aug, ui32 repeat)
126 {
127 avx_multiply_const(aug->f32, K, (int)repeat);
128 }
129
131 void avx_irv_horz_ana(const param_atk* atk, const line_buf* ldst,
132 const line_buf* hdst, const line_buf* src,
133 ui32 width, bool even)
134 {
135 if (width > 1)
136 {
137 // split src into ldst and hdst
138 {
139 float* dpl = even ? ldst->f32 : hdst->f32;
140 float* dph = even ? hdst->f32 : ldst->f32;
141 float* sp = src->f32;
142 int w = (int)width;
143 avx_deinterleave32(dpl, dph, sp, w);
144 }
145
146 // the actual horizontal transform
147 float* hp = hdst->f32, * lp = ldst->f32;
148 ui32 l_width = (width + (even ? 1 : 0)) >> 1; // low pass
149 ui32 h_width = (width + (even ? 0 : 1)) >> 1; // high pass
150 ui32 num_steps = atk->get_num_steps();
151 for (ui32 j = num_steps; j > 0; --j)
152 {
153 const lifting_step* s = atk->get_step(j - 1);
154 const float a = s->irv.Aatk;
155
156 // extension
157 lp[-1] = lp[0];
158 lp[l_width] = lp[l_width - 1];
159 // lifting step
160 const float* sp = lp;
161 float* dp = hp;
162 int i = (int)h_width;
163 __m256 f = _mm256_set1_ps(a);
164 if (even)
165 {
166 for (; i > 0; i -= 8, sp += 8, dp += 8)
167 {
168 __m256 m = _mm256_load_ps(sp);
169 __m256 n = _mm256_loadu_ps(sp + 1);
170 __m256 p = _mm256_load_ps(dp);
171 p = _mm256_add_ps(p, _mm256_mul_ps(f, _mm256_add_ps(m, n)));
172 _mm256_store_ps(dp, p);
173 }
174 }
175 else
176 {
177 for (; i > 0; i -= 8, sp += 8, dp += 8)
178 {
179 __m256 m = _mm256_load_ps(sp);
180 __m256 n = _mm256_loadu_ps(sp - 1);
181 __m256 p = _mm256_load_ps(dp);
182 p = _mm256_add_ps(p, _mm256_mul_ps(f, _mm256_add_ps(m, n)));
183 _mm256_store_ps(dp, p);
184 }
185 }
186
187 // swap buffers
188 float* t = lp; lp = hp; hp = t;
189 even = !even;
190 ui32 w = l_width; l_width = h_width; h_width = w;
191 }
192
193 { // multiply by K or 1/K
194 float K = atk->get_K();
195 float K_inv = 1.0f / K;
196 avx_multiply_const(lp, K_inv, (int)l_width);
197 avx_multiply_const(hp, K, (int)h_width);
198 }
199 }
200 else {
201 if (even)
202 ldst->f32[0] = src->f32[0];
203 else
204 hdst->f32[0] = src->f32[0] * 2.0f;
205 }
206 }
207
209 void avx_irv_horz_syn(const param_atk* atk, const line_buf* dst,
210 const line_buf* lsrc, const line_buf* hsrc,
211 ui32 width, bool even)
212 {
213 if (width > 1)
214 {
215 bool ev = even;
216 float* oth = hsrc->f32, * aug = lsrc->f32;
217 ui32 aug_width = (width + (even ? 1 : 0)) >> 1; // low pass
218 ui32 oth_width = (width + (even ? 0 : 1)) >> 1; // high pass
219
220 { // multiply by K or 1/K
221 float K = atk->get_K();
222 float K_inv = 1.0f / K;
223 avx_multiply_const(aug, K, (int)aug_width);
224 avx_multiply_const(oth, K_inv, (int)oth_width);
225 }
226
227 // the actual horizontal transform
228 ui32 num_steps = atk->get_num_steps();
229 for (ui32 j = 0; j < num_steps; ++j)
230 {
231 const lifting_step* s = atk->get_step(j);
232 const float a = s->irv.Aatk;
233
234 // extension
235 oth[-1] = oth[0];
236 oth[oth_width] = oth[oth_width - 1];
237 // lifting step
238 const float* sp = oth;
239 float* dp = aug;
240 int i = (int)aug_width;
241 __m256 f = _mm256_set1_ps(a);
242 if (ev)
243 {
244 for (; i > 0; i -= 8, sp += 8, dp += 8)
245 {
246 __m256 m = _mm256_load_ps(sp);
247 __m256 n = _mm256_loadu_ps(sp - 1);
248 __m256 p = _mm256_load_ps(dp);
249 p = _mm256_sub_ps(p, _mm256_mul_ps(f, _mm256_add_ps(m, n)));
250 _mm256_store_ps(dp, p);
251 }
252 }
253 else
254 {
255 for (; i > 0; i -= 8, sp += 8, dp += 8)
256 {
257 __m256 m = _mm256_load_ps(sp);
258 __m256 n = _mm256_loadu_ps(sp + 1);
259 __m256 p = _mm256_load_ps(dp);
260 p = _mm256_sub_ps(p, _mm256_mul_ps(f, _mm256_add_ps(m, n)));
261 _mm256_store_ps(dp, p);
262 }
263 }
264
265 // swap buffers
266 float* t = aug; aug = oth; oth = t;
267 ev = !ev;
268 ui32 w = aug_width; aug_width = oth_width; oth_width = w;
269 }
270
271 // combine both lsrc and hsrc into dst
272 {
273 float* dp = dst->f32;
274 float* spl = even ? lsrc->f32 : hsrc->f32;
275 float* sph = even ? hsrc->f32 : lsrc->f32;
276 int w = (int)width;
277 avx_interleave32(dp, spl, sph, w);
278 }
279 }
280 else {
281 if (even)
282 dst->f32[0] = lsrc->f32[0];
283 else
284 dst->f32[0] = hsrc->f32[0] * 0.5f;
285 }
286 }
287
288 } // !local
289} // !ojph
290
291#endif
ojph::local
Definition ojph_bitbuffer_read.h:52
ojph::local::avx_irv_horz_syn
void avx_irv_horz_syn(const param_atk *atk, const line_buf *dst, const line_buf *lsrc, const line_buf *hsrc, ui32 width, bool even)
ojph::local::avx_irv_vert_step
void avx_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::avx_irv_horz_ana
void avx_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::avx_irv_vert_times_K
void avx_irv_vert_times_K(float K, const line_buf *aug, ui32 repeat)
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:1055
ojph::local::param_atk
Definition ojph_params_local.h:1071
ojph::local::lifting_step
Definition ojph_params_local.h:1051
ojph::local::lifting_step::irv
irv_data irv
Definition ojph_params_local.h:1066