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 for C-plane with 16T16R
22 * @file xran_bfp_cplane16.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"
32 #include <immintrin.h>
35 namespace BFP_CPlane_16_SNC
37 /// Namespace constants
38 const int k_numDataElements = 32; /// 16 IQ pairs
41 maxAbsOneBlock(const __m512i* inData)
43 /// Compute abs of input data
44 const auto thisRegAbs = _mm512_abs_epi16(*inData);
45 /// Horizontal max across register
46 return BlockFloatCompander::horizontalMax1x32(thisRegAbs);
49 /// Compute exponent value for a set of 16 RB from the maximum absolute value.
51 computeExponent_16RB(const BlockFloatCompander::ExpandedData& dataIn, const __m512i totShiftBits)
53 __m512i maxAbs = __m512i();
54 const __m512i* dataInAddr = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
56 for (int n = 0; n < 16; ++n)
58 ((uint32_t*)&maxAbs)[n] = maxAbsOneBlock(dataInAddr + n);
60 /// Calculate exponent
61 return BlockFloatCompander::expLzCnt(maxAbs, totShiftBits);
64 /// Compute exponent value for a set of 4 RB from the maximum absolute value.
66 computeExponent_4RB(const BlockFloatCompander::ExpandedData& dataIn, const __m512i totShiftBits)
68 __m512i maxAbs = __m512i();
69 const __m512i* dataInAddr = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
71 for (int n = 0; n < 4; ++n)
73 ((uint32_t*)&maxAbs)[n] = maxAbsOneBlock(dataInAddr + n);
75 /// Calculate exponent
76 return BlockFloatCompander::expLzCnt(maxAbs, totShiftBits);
79 /// Compute exponent value for 1 RB from the maximum absolute value.
81 computeExponent_1RB(const BlockFloatCompander::ExpandedData& dataIn, const __m512i totShiftBits)
83 __m512i maxAbs = __m512i();
84 const __m512i* dataInAddr = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
85 ((uint32_t*)&maxAbs)[0] = maxAbsOneBlock(dataInAddr);
86 /// Calculate exponent
87 const auto exps = BlockFloatCompander::expLzCnt(maxAbs, totShiftBits);
88 return ((uint8_t*)&exps)[0];
93 /// Apply compression to one compression block
94 template<BlockFloatCompander::PackFunction networkBytePack>
96 applyCompressionN_1RB(const __m512i* dataIn, uint8_t* outBlockAddr,
97 const int iqWidth, const uint8_t thisExp, const uint64_t rbWriteMask)
99 /// Store exponent first
100 *outBlockAddr = thisExp;
101 /// Apply the exponent shift
102 const auto compData = _mm512_srai_epi16(*dataIn, thisExp);
103 /// Pack compressed data network byte order
104 const auto compDataBytePacked = networkBytePack(compData);
105 /// Store compressed data
106 _mm512_mask_storeu_epi8(outBlockAddr + 1, rbWriteMask, compDataBytePacked);
109 /// Derive and apply 9, 10, or 12bit compression to 16 compression blocks
110 template<BlockFloatCompander::PackFunction networkBytePack>
112 compressN_16RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
113 const __m512i totShiftBits, const int totNumBytesPerBlock, const uint64_t rbWriteMask)
115 const auto exponents = computeExponent_16RB(dataIn, totShiftBits);
116 const __m512i* dataInAddr = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
118 for (int n = 0; n < 16; ++n)
120 applyCompressionN_1RB<networkBytePack>(dataInAddr + n, dataOut->dataCompressed + n * totNumBytesPerBlock, dataIn.iqWidth, ((uint8_t*)&exponents)[n * 4], rbWriteMask);
124 /// Derive and apply 9, 10, or 12bit compression to 4 compression blocks
125 template<BlockFloatCompander::PackFunction networkBytePack>
127 compressN_4RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
128 const __m512i totShiftBits, const int totNumBytesPerBlock, const uint64_t rbWriteMask)
130 const auto exponents = computeExponent_4RB(dataIn, totShiftBits);
131 const __m512i* dataInAddr = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
133 for (int n = 0; n < 4; ++n)
135 applyCompressionN_1RB<networkBytePack>(dataInAddr + n, dataOut->dataCompressed + n * totNumBytesPerBlock, dataIn.iqWidth, ((uint8_t*)&exponents)[n * 4], rbWriteMask);
139 /// Derive and apply 9, 10, or 12bit compression to 1 RB
140 template<BlockFloatCompander::PackFunction networkBytePack>
142 compressN_1RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
143 const __m512i totShiftBits, const int totNumBytesPerBlock, const uint64_t rbWriteMask)
145 const auto thisExponent = computeExponent_1RB(dataIn, totShiftBits);
146 const __m512i* dataInAddr = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
147 applyCompressionN_1RB<networkBytePack>(dataInAddr, dataOut->dataCompressed, dataIn.iqWidth, thisExponent, rbWriteMask);
150 /// Calls compression function specific to the number of blocks to be executed. For 9, 10, or 12bit iqWidth.
151 template<BlockFloatCompander::PackFunction networkBytePack>
153 compressByAllocN(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
154 const __m512i totShiftBits, const int totNumBytesPerBlock, const uint64_t rbWriteMask)
156 switch (dataIn.numBlocks)
159 compressN_16RB<networkBytePack>(dataIn, dataOut, totShiftBits, totNumBytesPerBlock, rbWriteMask);
163 compressN_4RB<networkBytePack>(dataIn, dataOut, totShiftBits, totNumBytesPerBlock, rbWriteMask);
167 compressN_1RB<networkBytePack>(dataIn, dataOut, totShiftBits, totNumBytesPerBlock, rbWriteMask);
174 /// Apply 8b compression to 1 compression block.
176 applyCompression8_1RB(const __m512i* dataIn, uint8_t* outBlockAddr, const uint8_t thisExp)
178 /// Store exponent first
179 *outBlockAddr = thisExp;
180 /// Apply the exponent shift
181 const auto compData = _mm512_srai_epi16(*dataIn, thisExp);
182 /// Truncate to 8bit and store
183 constexpr uint32_t k_writeMask = 0xFFFFFFFF;
184 _mm256_mask_storeu_epi8(outBlockAddr + 1, k_writeMask, _mm512_cvtepi16_epi8(compData));
187 /// Derive and apply 8b compression to 16 compression blocks
189 compress8_16RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut, const __m512i totShiftBits)
191 const auto exponents = computeExponent_16RB(dataIn, totShiftBits);
192 const __m512i* dataInAddr = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
194 for (int n = 0; n < 16; ++n)
196 applyCompression8_1RB(dataInAddr + n, dataOut->dataCompressed + n * (k_numDataElements + 1), ((uint8_t*)&exponents)[n * 4]);
200 /// Derive and apply 8b compression to 4 compression blocks
202 compress8_4RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut, const __m512i totShiftBits)
204 const auto exponents = computeExponent_4RB(dataIn, totShiftBits);
205 const __m512i* dataInAddr = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
207 for (int n = 0; n < 4; ++n)
209 applyCompression8_1RB(dataInAddr + n, dataOut->dataCompressed + n * (k_numDataElements + 1), ((uint8_t*)&exponents)[n * 4]);
213 /// Derive and apply 8b compression to 1 compression block
215 compress8_1RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut, const __m512i totShiftBits)
217 const auto thisExponent = computeExponent_1RB(dataIn, totShiftBits);
218 const __m512i* dataInAddr = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
219 applyCompression8_1RB(dataInAddr, dataOut->dataCompressed, thisExponent);
222 /// Calls compression function specific to the number of RB to be executed. For 8 bit iqWidth.
224 compressByAlloc8(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut, const __m512i totShiftBits)
226 switch (dataIn.numBlocks)
229 compress8_16RB(dataIn, dataOut, totShiftBits);
233 compress8_4RB(dataIn, dataOut, totShiftBits);
237 compress8_1RB(dataIn, dataOut, totShiftBits);
244 /// Expand 1 compression block
245 template<BlockFloatCompander::UnpackFunction networkByteUnpack>
247 applyExpansionN_1RB(const uint8_t* expAddr, __m512i* dataOutAddr, const int maxExpShift)
249 const auto thisExpShift = maxExpShift - *expAddr;
250 /// Unpack network order packed data
251 const auto inDataUnpacked = networkByteUnpack(expAddr + 1);
252 /// Apply exponent scaling (by appropriate arithmetic shift right)
253 const auto expandedData = _mm512_srai_epi16(inDataUnpacked, thisExpShift);
254 /// Write expanded data to output
255 static constexpr uint8_t k_WriteMask = 0xFF;
256 _mm512_mask_storeu_epi64(dataOutAddr, k_WriteMask, expandedData);
259 /// Calls expansion function specific to the number of blocks to be executed. For 9, 10, or 12bit iqWidth.
260 template<BlockFloatCompander::UnpackFunction networkByteUnpack>
262 expandByAllocN(const BlockFloatCompander::CompressedData& dataIn, BlockFloatCompander::ExpandedData* dataOut,
263 const int totNumBytesPerBlock, const int maxExpShift)
265 __m512i* dataOutAddr = reinterpret_cast<__m512i*>(dataOut->dataExpanded);
266 switch (dataIn.numBlocks)
270 for (int n = 0; n < 16; ++n)
272 applyExpansionN_1RB<networkByteUnpack>(dataIn.dataCompressed + n * totNumBytesPerBlock, dataOutAddr + n, maxExpShift);
278 for (int n = 0; n < 4; ++n)
280 applyExpansionN_1RB<networkByteUnpack>(dataIn.dataCompressed + n * totNumBytesPerBlock, dataOutAddr + n, maxExpShift);
285 applyExpansionN_1RB<networkByteUnpack>(dataIn.dataCompressed, dataOutAddr, maxExpShift);
291 /// Apply expansion to 1 compression block
293 applyExpansion8_1RB(const uint8_t* expAddr, __m512i* dataOutAddr)
295 const __m256i* rawDataIn = reinterpret_cast<const __m256i*>(expAddr + 1);
296 const auto compData16 = _mm512_cvtepi8_epi16(*rawDataIn);
297 const auto expData = _mm512_slli_epi16(compData16, *expAddr);
298 static constexpr uint8_t k_WriteMask = 0xFF;
299 _mm512_mask_storeu_epi64(dataOutAddr, k_WriteMask, expData);
302 /// Calls expansion function specific to the number of RB to be executed. For 8 bit iqWidth.
304 expandByAlloc8(const BlockFloatCompander::CompressedData& dataIn, BlockFloatCompander::ExpandedData* dataOut)
306 __m512i* dataOutAddr = reinterpret_cast<__m512i*>(dataOut->dataExpanded);
307 switch (dataIn.numBlocks)
311 for (int n = 0; n < 16; ++n)
313 applyExpansion8_1RB(dataIn.dataCompressed + n * (k_numDataElements + 1), dataOutAddr + n);
319 for (int n = 0; n < 4; ++n)
321 applyExpansion8_1RB(dataIn.dataCompressed + n * (k_numDataElements + 1), dataOutAddr + n);
326 applyExpansion8_1RB(dataIn.dataCompressed, dataOutAddr);
333 /// Main kernel function for 16 antenna C-plane compression.
334 /// Starts by determining iqWidth specific parameters and functions.
336 BlockFloatCompander::BFPCompressCtrlPlane16AvxSnc(const ExpandedData& dataIn, CompressedData* dataOut)
338 /// Compensation for extra zeros in 32b leading zero count when computing exponent
339 const auto totShiftBits8 = _mm512_set1_epi32(25);
340 const auto totShiftBits9 = _mm512_set1_epi32(24);
341 const auto totShiftBits10 = _mm512_set1_epi32(23);
342 const auto totShiftBits12 = _mm512_set1_epi32(21);
344 /// Total number of data bytes per compression block is (iqWidth * numElements / 8) + 1
345 const auto totNumBytesPerBlock = ((BFP_CPlane_16_SNC::k_numDataElements * dataIn.iqWidth) >> 3) + 1;
347 /// Compressed data write mask for each iqWidth option
348 constexpr uint64_t rbWriteMask9 = 0x0000000FFFFFFFFF;
349 constexpr uint64_t rbWriteMask10 = 0x000000FFFFFFFFFF;
350 constexpr uint64_t rbWriteMask12 = 0x0000FFFFFFFFFFFF;
352 switch (dataIn.iqWidth)
355 BFP_CPlane_16_SNC::compressByAlloc8(dataIn, dataOut, totShiftBits8);
359 BFP_CPlane_16_SNC::compressByAllocN<BlockFloatCompander::networkBytePack9bSnc>(dataIn, dataOut, totShiftBits9, totNumBytesPerBlock, rbWriteMask9);
363 BFP_CPlane_16_SNC::compressByAllocN<BlockFloatCompander::networkBytePack10bSnc>(dataIn, dataOut, totShiftBits10, totNumBytesPerBlock, rbWriteMask10);
367 BFP_CPlane_16_SNC::compressByAllocN<BlockFloatCompander::networkBytePack12bSnc>(dataIn, dataOut, totShiftBits12, totNumBytesPerBlock, rbWriteMask12);
373 /// Main kernel function for 16 antenna C-plane expansion.
374 /// Starts by determining iqWidth specific parameters and functions.
376 BlockFloatCompander::BFPExpandCtrlPlane16AvxSnc(const CompressedData& dataIn, ExpandedData* dataOut)
378 constexpr int k_maxExpShift9 = 7;
379 constexpr int k_maxExpShift10 = 6;
380 constexpr int k_maxExpShift12 = 4;
382 /// Total number of data bytes per compression block is (iqWidth * numElements / 8) + 1
383 const auto totNumBytesPerBlock = ((BFP_CPlane_16_SNC::k_numDataElements * dataIn.iqWidth) >> 3) + 1;
385 switch (dataIn.iqWidth)
388 BFP_CPlane_16_SNC::expandByAlloc8(dataIn, dataOut);
392 BFP_CPlane_16_SNC::expandByAllocN<BlockFloatCompander::networkByteUnpack9bSnc>(dataIn, dataOut, totNumBytesPerBlock, k_maxExpShift9);
396 BFP_CPlane_16_SNC::expandByAllocN<BlockFloatCompander::networkByteUnpack10bSnc>(dataIn, dataOut, totNumBytesPerBlock, k_maxExpShift10);
400 BFP_CPlane_16_SNC::expandByAllocN<BlockFloatCompander::networkByteUnpack12bSnc>(dataIn, dataOut, totNumBytesPerBlock, k_maxExpShift12);