OpenJPH
Open-source implementation of JPEG2000 Part-15
ojph_codestream_avx2.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) 2022, Aous Naman
6// Copyright (c) 2022, Kakadu Software Pty Ltd, Australia
7// Copyright (c) 2022, 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_codestream_avx2.cpp
34// Author: Aous Naman
35// Date: 15 May 2022
36//***************************************************************************/
37
38#include <climits>
39#include <immintrin.h>
40#include "ojph_defs.h"
41
42namespace ojph {
43 namespace local {
44
47 {
48 __m128i x0 = _mm_loadu_si128((__m128i*)address);
49 __m128i x1 = _mm_loadu_si128((__m128i*)address + 1);
50 x0 = _mm_or_si128(x0, x1);
51 x1 = _mm_shuffle_epi32(x0, 0xEE); // x1 = x0[2,3,2,3]
52 x0 = _mm_or_si128(x0, x1);
53 x1 = _mm_shuffle_epi32(x0, 0x55); // x1 = x0[1,1,1,1]
54 x0 = _mm_or_si128(x0, x1);
55 ui32 t = (ui32)_mm_extract_epi32(x0, 0);
56 return t;
57 }
58
61 {
62 __m128i x0 = _mm_loadu_si128((__m128i*)address);
63 __m128i x1 = _mm_loadu_si128((__m128i*)address + 1);
64 x0 = _mm_or_si128(x0, x1);
65 x1 = _mm_shuffle_epi32(x0, 0xEE); // x1 = x0[2,3,2,3]
66 x0 = _mm_or_si128(x0, x1);
67 ui64 t = (ui64)_mm_extract_epi64(x0, 0);
68 return t;
69 }
70
72 void avx2_rev_tx_to_cb32(const void *sp, ui32 *dp, ui32 K_max,
73 float delta_inv, ui32 count, ui32* max_val)
74 {
75 ojph_unused(delta_inv);
76
77 // convert to sign and magnitude and keep max_val
78 ui32 shift = 31 - K_max;
79 __m256i m0 = _mm256_set1_epi32(INT_MIN);
80 __m256i tmax = _mm256_loadu_si256((__m256i*)max_val);
81 __m256i *p = (__m256i*)sp;
82 for (ui32 i = 0; i < count; i += 8, p += 1, dp += 8)
83 {
84 __m256i v = _mm256_loadu_si256(p);
85 __m256i sign = _mm256_and_si256(v, m0);
86 __m256i val = _mm256_abs_epi32(v);
87 val = _mm256_slli_epi32(val, (int)shift);
88 tmax = _mm256_or_si256(tmax, val);
89 val = _mm256_or_si256(val, sign);
90 _mm256_storeu_si256((__m256i*)dp, val);
91 }
92 _mm256_storeu_si256((__m256i*)max_val, tmax);
93 }
94
96 void avx2_irv_tx_to_cb32(const void *sp, ui32 *dp, ui32 K_max,
97 float delta_inv, ui32 count, ui32* max_val)
98 {
99 ojph_unused(K_max);
100
101 //quantize and convert to sign and magnitude and keep max_val
102 __m256 d = _mm256_set1_ps(delta_inv);
103 __m256i m0 = _mm256_set1_epi32(INT_MIN);
104 __m256i tmax = _mm256_loadu_si256((__m256i*)max_val);
105 float *p = (float*)sp;
106
107 for (ui32 i = 0; i < count; i += 8, p += 8, dp += 8)
108 {
109 __m256 vf = _mm256_loadu_ps(p);
110 vf = _mm256_mul_ps(vf, d); // multiply
111 __m256i val = _mm256_cvtps_epi32(vf); // convert to int
112 __m256i sign = _mm256_and_si256(val, m0); // get sign
113 val = _mm256_abs_epi32(val);
114 tmax = _mm256_or_si256(tmax, val);
115 val = _mm256_or_si256(val, sign);
116 _mm256_storeu_si256((__m256i*)dp, val);
117 }
118 _mm256_storeu_si256((__m256i*)max_val, tmax);
119 }
120
122 void avx2_rev_tx_from_cb32(const ui32 *sp, void *dp, ui32 K_max,
123 float delta, ui32 count)
124 {
125 ojph_unused(delta);
126 ui32 shift = 31 - K_max;
127 __m256i m1 = _mm256_set1_epi32(INT_MAX);
128 si32 *p = (si32*)dp;
129 for (ui32 i = 0; i < count; i += 8, sp += 8, p += 8)
130 {
131 __m256i v = _mm256_load_si256((__m256i*)sp);
132 __m256i val = _mm256_and_si256(v, m1);
133 val = _mm256_srli_epi32(val, (int)shift);
134 val = _mm256_sign_epi32(val, v);
135 _mm256_storeu_si256((__m256i*)p, val);
136 }
137 }
138
140 void avx2_irv_tx_from_cb32(const ui32 *sp, void *dp, ui32 K_max,
141 float delta, ui32 count)
142 {
143 ojph_unused(K_max);
144 __m256i m1 = _mm256_set1_epi32(INT_MAX);
145 __m256 d = _mm256_set1_ps(delta);
146 float *p = (float*)dp;
147 for (ui32 i = 0; i < count; i += 8, sp += 8, p += 8)
148 {
149 __m256i v = _mm256_load_si256((__m256i*)sp);
150 __m256i vali = _mm256_and_si256(v, m1);
151 __m256 valf = _mm256_cvtepi32_ps(vali);
152 valf = _mm256_mul_ps(valf, d);
153 __m256i sign = _mm256_andnot_si256(m1, v);
154 valf = _mm256_or_ps(valf, _mm256_castsi256_ps(sign));
155 _mm256_storeu_ps(p, valf);
156 }
157 }
158
160 void avx2_rev_tx_to_cb64(const void *sp, ui64 *dp, ui32 K_max,
161 float delta_inv, ui32 count, ui64* max_val)
162 {
163 ojph_unused(delta_inv);
164
165 // convert to sign and magnitude and keep max_val
166 ui32 shift = 63 - K_max;
167 __m256i m0 = _mm256_set1_epi64x(LLONG_MIN);
168 __m256i zero = _mm256_setzero_si256();
169 __m256i one = _mm256_set1_epi64x(1);
170 __m256i tmax = _mm256_loadu_si256((__m256i*)max_val);
171 __m256i *p = (__m256i*)sp;
172 for (ui32 i = 0; i < count; i += 4, p += 1, dp += 4)
173 {
174 __m256i v = _mm256_loadu_si256(p);
175 __m256i sign = _mm256_cmpgt_epi64(zero, v);
176 __m256i val = _mm256_xor_si256(v, sign); // negate 1's complement
177 __m256i ones = _mm256_and_si256(sign, one);
178 val = _mm256_add_epi64(val, ones); // 2's complement
179 sign = _mm256_and_si256(sign, m0);
180 val = _mm256_slli_epi64(val, (int)shift);
181 tmax = _mm256_or_si256(tmax, val);
182 val = _mm256_or_si256(val, sign);
183 _mm256_storeu_si256((__m256i*)dp, val);
184 }
185 _mm256_storeu_si256((__m256i*)max_val, tmax);
186 }
187
189 void avx2_rev_tx_from_cb64(const ui64 *sp, void *dp, ui32 K_max,
190 float delta, ui32 count)
191 {
192 ojph_unused(delta);
193
194 ui32 shift = 63 - K_max;
195 __m256i m1 = _mm256_set1_epi64x(LLONG_MAX);
196 __m256i zero = _mm256_setzero_si256();
197 __m256i one = _mm256_set1_epi64x(1);
198 si64 *p = (si64*)dp;
199 for (ui32 i = 0; i < count; i += 4, sp += 4, p += 4)
200 {
201 __m256i v = _mm256_load_si256((__m256i*)sp);
202 __m256i val = _mm256_and_si256(v, m1);
203 val = _mm256_srli_epi64(val, (int)shift);
204 __m256i sign = _mm256_cmpgt_epi64(zero, v);
205 val = _mm256_xor_si256(val, sign); // negate 1's complement
206 __m256i ones = _mm256_and_si256(sign, one);
207 val = _mm256_add_epi64(val, ones); // 2's complement
208 _mm256_storeu_si256((__m256i*)p, val);
209 }
210 }
211 }
212}
ui64 avx2_find_max_val64(ui64 *address)
void avx2_irv_tx_to_cb32(const void *sp, ui32 *dp, ui32 K_max, float delta_inv, ui32 count, ui32 *max_val)
void avx2_rev_tx_from_cb64(const ui64 *sp, void *dp, ui32 K_max, float delta, ui32 count)
ui32 avx2_find_max_val32(ui32 *address)
void avx2_rev_tx_to_cb32(const void *sp, ui32 *dp, ui32 K_max, float delta_inv, ui32 count, ui32 *max_val)
void avx2_rev_tx_to_cb64(const void *sp, ui64 *dp, ui32 K_max, float delta_inv, ui32 count, ui64 *max_val)
void avx2_rev_tx_from_cb32(const ui32 *sp, void *dp, ui32 K_max, float delta, ui32 count)
void avx2_irv_tx_from_cb32(const ui32 *sp, void *dp, ui32 K_max, float delta, ui32 count)
int64_t si64
Definition: ojph_defs.h:57
uint64_t ui64
Definition: ojph_defs.h:56
int32_t si32
Definition: ojph_defs.h:55
uint32_t ui32
Definition: ojph_defs.h:54
#define ojph_unused(x)
Definition: ojph_defs.h:78