1 /******************************************************************************
3 * Copyright (c) 2020 Intel.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 *******************************************************************************/
20 * @brief xRAN BFP compression/decompression U-plane implementation and interface functions
22 * @file xran_compression.cpp
23 * @ingroup group_source_xran
24 * @author Intel Corporation
27 #include "xran_compression.hpp"
28 #include "xran_bfp_utils.hpp"
29 #include "xran_bfp_byte_packing_utils.hpp"
30 #include "xran_compression.h"
33 #include <immintrin.h>
36 namespace BFP_UPlane_SNC
38 /// Namespace constants
39 const int k_numREReal = 24; /// 12 IQ pairs
42 /// Compute exponent value for a set of 16 RB from the maximum absolute value.
43 /// Max Abs operates in a loop, executing 4 RB per iteration. The results are
44 /// packed into the final output register.
46 computeExponent_16RB(const BlockFloatCompander::ExpandedData& dataIn, const __m512i totShiftBits)
48 __m512i maxAbs = __m512i();
49 const __m512i* rawData = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
50 /// Max Abs loop operates on 4RB at a time
52 for (int n = 0; n < 4; ++n)
54 /// Re-order and vertical max abs
55 auto maxAbsVert = BlockFloatCompander::maxAbsVertical4RB(rawData[3 * n + 0], rawData[3 * n + 1], rawData[3 * n + 2]);
56 /// Horizontal max abs
57 auto maxAbsHorz = BlockFloatCompander::horizontalMax4x16(maxAbsVert);
58 /// Pack these 4 values into maxAbs
59 maxAbs = BlockFloatCompander::slidePermute(maxAbsHorz, maxAbs, n);
61 /// Calculate exponent
62 const auto maxAbs32 = BlockFloatCompander::maskUpperWord(maxAbs);
63 return BlockFloatCompander::expLzCnt(maxAbs32, totShiftBits);
67 /// Compute exponent value for a set of 4 RB from the maximum absolute value.
68 /// Note that we do not need to perform any packing of result as we are only
69 /// computing 4 RB. The appropriate offset is taken later when extracting the
72 computeExponent_4RB(const BlockFloatCompander::ExpandedData& dataIn, const __m512i totShiftBits)
74 const __m512i* rawData = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
75 /// Re-order and vertical max abs
76 const auto maxAbsVert = BlockFloatCompander::maxAbsVertical4RB(rawData[0], rawData[1], rawData[2]);
77 /// Horizontal max abs
78 const auto maxAbsHorz = BlockFloatCompander::horizontalMax4x16(maxAbsVert);
79 /// Calculate exponent
80 const auto maxAbs = BlockFloatCompander::maskUpperWord(maxAbsHorz);
81 return BlockFloatCompander::expLzCnt(maxAbs, totShiftBits);
85 /// Compute exponent value for 1 RB from the maximum absolute value.
86 /// This works with horizontal max abs only, and needs to include a
87 /// step to select the final exponent from the 4 lanes.
89 computeExponent_1RB(const BlockFloatCompander::ExpandedData& dataIn, const __m512i totShiftBits)
91 const __m512i* rawData = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
93 const auto rawDataAbs = _mm512_abs_epi16(rawData[0]);
94 /// No need to do a full horizontal max operation here, just do a max IQ step,
95 /// compute the exponents and then use a reduce max over all exponent values. This
96 /// is the fastest way to handle a single RB.
97 const auto rawAbsIQSwap = _mm512_rol_epi32(rawDataAbs, BlockFloatCompander::k_numBitsIQ);
98 const auto maxAbsIQ = _mm512_max_epi16(rawDataAbs, rawAbsIQSwap);
99 /// Calculate exponent
100 const auto maxAbsIQ32 = BlockFloatCompander::maskUpperWord(maxAbsIQ);
101 const auto exps = BlockFloatCompander::expLzCnt(maxAbsIQ32, totShiftBits);
102 /// At this point we have exponent values for the maximum of each IQ pair.
103 /// Run a reduce max step to compute the maximum exponent value in the first
104 /// three lanes - this will give the desired exponent for this RB.
105 constexpr uint16_t k_expMsk = 0x0FFF;
106 return (uint8_t)_mm512_mask_reduce_max_epi32(k_expMsk, exps);
110 /// Apply compression to 1 RB
111 template<BlockFloatCompander::PackFunction networkBytePack>
113 applyCompressionN_1RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
114 const int numREOffset, const uint8_t thisExp, const int thisRBExpAddr, const uint64_t rbWriteMask)
116 /// Get AVX512 pointer aligned to desired RB
117 const __m512i* rawDataIn = reinterpret_cast<const __m512i*>(dataIn.dataExpanded + numREOffset);
118 /// Apply the exponent shift
119 const auto compData = _mm512_srai_epi16(*rawDataIn, thisExp);
120 /// Pack compressed data network byte order
121 const auto compDataBytePacked = networkBytePack(compData);
122 /// Store exponent first
123 dataOut->dataCompressed[thisRBExpAddr] = thisExp;
124 /// Store compressed data
125 _mm512_mask_storeu_epi8(dataOut->dataCompressed + thisRBExpAddr + 1, rbWriteMask, compDataBytePacked);
129 /// Apply 9, 10, or 12bit compression to 16 RB
130 template<BlockFloatCompander::PackFunction networkBytePack>
132 compressN_16RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
133 const __m512i totShiftBits, const int totNumBytesPerRB, const uint64_t rbWriteMask)
135 const auto exponents = computeExponent_16RB(dataIn, totShiftBits);
137 for (int n = 0; n < 16; ++n)
139 applyCompressionN_1RB<networkBytePack>(dataIn, dataOut, n * k_numREReal, ((uint8_t*)&exponents)[n * 4], n * totNumBytesPerRB, rbWriteMask);
144 /// Apply 9, 10, or 12bit compression to 4 RB
145 template<BlockFloatCompander::PackFunction networkBytePack>
147 compressN_4RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
148 const __m512i totShiftBits, const int totNumBytesPerRB, const uint64_t rbWriteMask)
150 const auto exponents = computeExponent_4RB(dataIn, totShiftBits);
152 for (int n = 0; n < 4; ++n)
154 applyCompressionN_1RB<networkBytePack>(dataIn, dataOut, n * k_numREReal, ((uint8_t*)&exponents)[n * 16], n * totNumBytesPerRB, rbWriteMask);
159 /// Apply 9, 10, or 12bit compression to 1 RB
160 template<BlockFloatCompander::PackFunction networkBytePack>
162 compressN_1RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
163 const __m512i totShiftBits, const int totNumBytesPerRB, const uint64_t rbWriteMask)
165 const auto thisExponent = computeExponent_1RB(dataIn, totShiftBits);
166 applyCompressionN_1RB<networkBytePack>(dataIn, dataOut, 0, thisExponent, 0, rbWriteMask);
170 /// Calls compression function specific to the number of RB to be executed. For 9, 10, or 12bit iqWidth.
171 template<BlockFloatCompander::PackFunction networkBytePack>
173 compressByAllocN(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
174 const __m512i totShiftBits, const int totNumBytesPerRB, const uint64_t rbWriteMask)
176 switch (dataIn.numBlocks)
179 compressN_16RB<networkBytePack>(dataIn, dataOut, totShiftBits, totNumBytesPerRB, rbWriteMask);
183 compressN_4RB<networkBytePack>(dataIn, dataOut, totShiftBits, totNumBytesPerRB, rbWriteMask);
187 compressN_1RB<networkBytePack>(dataIn, dataOut, totShiftBits, totNumBytesPerRB, rbWriteMask);
193 /// Apply compression to 1 RB
195 applyCompression8_1RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
196 const int numREOffset, const uint8_t thisExp, const int thisRBExpAddr)
198 /// Get AVX512 pointer aligned to desired RB
199 const __m512i* rawDataIn = reinterpret_cast<const __m512i*>(dataIn.dataExpanded + numREOffset);
200 /// Apply the exponent shift
201 const auto compData = _mm512_srai_epi16(*rawDataIn, thisExp);
202 /// Store exponent first
203 dataOut->dataCompressed[thisRBExpAddr] = thisExp;
204 /// Now have 1 RB worth of bytes separated into 3 chunks (1 per lane)
205 /// Use three offset stores to join
206 constexpr uint32_t k_rbMask = 0x00FFFFFF; // Write mask for 1RB (24 values)
207 _mm256_mask_storeu_epi8(dataOut->dataCompressed + thisRBExpAddr + 1, k_rbMask, _mm512_cvtepi16_epi8(compData));
211 /// 8bit RB compression loop for 16 RB
213 compress8_16RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut, const __m512i totShiftBits)
215 const auto exponents = computeExponent_16RB(dataIn, totShiftBits);
217 for (int n = 0; n < 16; ++n)
219 applyCompression8_1RB(dataIn, dataOut, n * k_numREReal, ((uint8_t*)&exponents)[n * 4], n * (k_numREReal + 1));
224 /// 8bit RB compression loop for 4 RB
226 compress8_4RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut, const __m512i totShiftBits)
228 const auto exponents = computeExponent_4RB(dataIn, totShiftBits);
230 for (int n = 0; n < 4; ++n)
232 applyCompression8_1RB(dataIn, dataOut, n * k_numREReal, ((uint8_t*)&exponents)[n * 16], n * (k_numREReal + 1));
237 /// 8bit RB compression loop for 4 RB
239 compress8_1RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut, const __m512i totShiftBits)
241 const auto thisExponent = computeExponent_1RB(dataIn, totShiftBits);
242 applyCompression8_1RB(dataIn, dataOut, 0, thisExponent, 0);
246 /// Calls compression function specific to the number of RB to be executed. For 8 bit iqWidth.
248 compressByAlloc8(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut, const __m512i totShiftBits)
250 switch (dataIn.numBlocks)
253 compress8_16RB(dataIn, dataOut, totShiftBits);
257 compress8_4RB(dataIn, dataOut, totShiftBits);
261 compress8_1RB(dataIn, dataOut, totShiftBits);
267 /// Apply compression to 1 RB
268 template<BlockFloatCompander::UnpackFunction networkByteUnpack>
270 applyExpansionN_1RB(const BlockFloatCompander::CompressedData& dataIn, BlockFloatCompander::ExpandedData* dataOut,
271 const int expAddr, const int thisRBAddr, const int maxExpShift)
273 /// Unpack network order packed data
274 const auto dataUnpacked = networkByteUnpack(dataIn.dataCompressed + expAddr + 1);
275 /// Apply exponent scaling (by appropriate arithmetic shift right)
276 const auto dataExpanded = _mm512_srai_epi16(dataUnpacked, maxExpShift - *(dataIn.dataCompressed + expAddr));
277 /// Write expanded data to output
278 static constexpr uint32_t k_WriteMask = 0x00FFFFFF;
279 _mm512_mask_storeu_epi16(dataOut->dataExpanded + thisRBAddr, k_WriteMask, dataExpanded);
283 /// Calls compression function specific to the number of RB to be executed. For 9, 10, or 12bit iqWidth.
284 template<BlockFloatCompander::UnpackFunction networkByteUnpack>
286 expandByAllocN(const BlockFloatCompander::CompressedData& dataIn, BlockFloatCompander::ExpandedData* dataOut,
287 const int totNumBytesPerRB, const int maxExpShift)
289 switch (dataIn.numBlocks)
293 for (int n = 0; n < 16; ++n)
295 applyExpansionN_1RB<networkByteUnpack>(dataIn, dataOut, n * totNumBytesPerRB, n * k_numREReal, maxExpShift);
301 for (int n = 0; n < 4; ++n)
303 applyExpansionN_1RB<networkByteUnpack>(dataIn, dataOut, n * totNumBytesPerRB, n * k_numREReal, maxExpShift);
308 applyExpansionN_1RB<networkByteUnpack>(dataIn, dataOut, 0, 0, maxExpShift);
314 /// Apply expansion to 1 RB and store
316 applyExpansion8_1RB(const BlockFloatCompander::CompressedData& dataIn, BlockFloatCompander::ExpandedData* dataOut,
317 const int expAddr, const int thisRBAddr)
319 const __m256i* rawDataIn = reinterpret_cast<const __m256i*>(dataIn.dataCompressed + expAddr + 1);
320 const auto compData16 = _mm512_cvtepi8_epi16(*rawDataIn);
321 const auto expData = _mm512_slli_epi16(compData16, *(dataIn.dataCompressed + expAddr));
322 constexpr uint8_t k_rbMask64 = 0b00111111; // 64b write mask for 1RB (24 int16 values)
323 _mm512_mask_storeu_epi64(dataOut->dataExpanded + thisRBAddr, k_rbMask64, expData);
327 /// Calls expansion function specific to the number of RB to be executed. For 8 bit iqWidth.
329 expandByAlloc8(const BlockFloatCompander::CompressedData& dataIn, BlockFloatCompander::ExpandedData* dataOut)
331 switch (dataIn.numBlocks)
335 for (int n = 0; n < 16; ++n)
337 applyExpansion8_1RB(dataIn, dataOut, n * (k_numREReal + 1), n * k_numREReal);
343 for (int n = 0; n < 4; ++n)
345 applyExpansion8_1RB(dataIn, dataOut, n * (k_numREReal + 1), n * k_numREReal);
350 applyExpansion8_1RB(dataIn, dataOut, 0, 0);
358 /// Main kernel function for compression. This version uses instructions available in Sunny Cove.
359 /// Starts by determining iqWidth specific parameters and functions.
361 BlockFloatCompander::BFPCompressUserPlaneAvxSnc(const ExpandedData& dataIn, CompressedData* dataOut)
363 /// Compensation for extra zeros in 32b leading zero count when computing exponent
364 const auto totShiftBits8 = _mm512_set1_epi32(25);
365 const auto totShiftBits9 = _mm512_set1_epi32(24);
366 const auto totShiftBits10 = _mm512_set1_epi32(23);
367 const auto totShiftBits12 = _mm512_set1_epi32(21);
369 /// Total number of compressed bytes per RB for each iqWidth option
370 constexpr int totNumBytesPerRB9 = 28;
371 constexpr int totNumBytesPerRB10 = 31;
372 constexpr int totNumBytesPerRB12 = 37;
374 /// Compressed data write mask for each iqWidth option
375 constexpr uint64_t rbWriteMask9 = 0x0000000007FFFFFF;
376 constexpr uint64_t rbWriteMask10 = 0x000000003FFFFFFF;
377 constexpr uint64_t rbWriteMask12 = 0x0000000FFFFFFFFF;
379 switch (dataIn.iqWidth)
382 BFP_UPlane_SNC::compressByAlloc8(dataIn, dataOut, totShiftBits8);
386 BFP_UPlane_SNC::compressByAllocN<BlockFloatCompander::networkBytePack9bSnc>(dataIn, dataOut, totShiftBits9, totNumBytesPerRB9, rbWriteMask9);
390 BFP_UPlane_SNC::compressByAllocN<BlockFloatCompander::networkBytePack10bSnc>(dataIn, dataOut, totShiftBits10, totNumBytesPerRB10, rbWriteMask10);
394 BFP_UPlane_SNC::compressByAllocN<BlockFloatCompander::networkBytePack12bSnc>(dataIn, dataOut, totShiftBits12, totNumBytesPerRB12, rbWriteMask12);
401 /// Main kernel function for expansion.
402 /// Starts by determining iqWidth specific parameters and functions.
404 BlockFloatCompander::BFPExpandUserPlaneAvxSnc(const CompressedData& dataIn, ExpandedData* dataOut)
406 constexpr int k_totNumBytesPerRB9 = 28;
407 constexpr int k_totNumBytesPerRB10 = 31;
408 constexpr int k_totNumBytesPerRB12 = 37;
410 constexpr int k_maxExpShift9 = 7;
411 constexpr int k_maxExpShift10 = 6;
412 constexpr int k_maxExpShift12 = 4;
414 switch (dataIn.iqWidth)
417 BFP_UPlane_SNC::expandByAlloc8(dataIn, dataOut);
421 BFP_UPlane_SNC::expandByAllocN<BlockFloatCompander::networkByteUnpack9bSnc>(dataIn, dataOut, k_totNumBytesPerRB9, k_maxExpShift9);
425 BFP_UPlane_SNC::expandByAllocN<BlockFloatCompander::networkByteUnpack10bSnc>(dataIn, dataOut, k_totNumBytesPerRB10, k_maxExpShift10);
429 BFP_UPlane_SNC::expandByAllocN<BlockFloatCompander::networkByteUnpack12bSnc>(dataIn, dataOut, k_totNumBytesPerRB12, k_maxExpShift12);