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 8T8R
22 * @file xran_bfp_cplane8.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_8_SNC
37 /// Namespace constants
38 const int k_numDataElements = 16; /// 16 IQ pairs
41 maxAbsOneReg(const __m512i maxAbs, const __m512i* inData, const int pairNum)
43 /// Compute abs of input data
44 const auto thisRegAbs = _mm512_abs_epi16(*inData);
45 /// Swap each IQ pair in each lane (via 32b rotation) and compute max of
47 const auto maxRot16 = _mm512_rol_epi32(thisRegAbs, BlockFloatCompander::k_numBitsIQ);
48 const auto maxAbsIQ = _mm512_max_epi16(thisRegAbs, maxRot16);
49 /// Convert to 32b values
50 const auto maxAbsIQ32 = BlockFloatCompander::maskUpperWord(maxAbsIQ);
51 /// Swap 32b in each 64b chunk via rotation and compute 32b max
52 /// Results in blocks of 64b with 4 repeated 16b max values
53 const auto maxRot32 = _mm512_rol_epi64(maxAbsIQ32, BlockFloatCompander::k_numBitsIQPair);
54 const auto maxAbs32 = _mm512_max_epi32(maxAbsIQ32, maxRot32);
55 /// First 64b permute and max
56 /// Results in blocks of 128b with 8 repeated 16b max values
57 constexpr uint8_t k_perm64A = 0xB1;
58 const auto maxPerm64A = _mm512_permutex_epi64(maxAbs32, k_perm64A);
59 const auto maxAbs64 = _mm512_max_epi64(maxAbs32, maxPerm64A);
60 /// Second 64b permute and max
61 /// Results in blocks of 256b with 16 repeated 16b max values
62 constexpr uint8_t k_perm64B = 0x4E;
63 const auto maxPerm64B = _mm512_permutex_epi64(maxAbs64, k_perm64B);
64 const auto maxAbs128 = _mm512_max_epi64(maxAbs64, maxPerm64B);
65 /// Now register contains repeated max values for two compression blocks
66 /// Permute the desired results into maxAbs
67 const auto k_selectVals = _mm512_set_epi32(24, 16, 24, 16, 24, 16, 24, 16,
68 24, 16, 24, 16, 24, 16, 24, 16);
69 constexpr uint16_t k_2ValsMsk[8] = { 0x0003, 0x000C, 0x0030, 0x00C0, 0x0300, 0x0C00, 0x3000, 0xC000 };
70 return _mm512_mask_permutex2var_epi32(maxAbs, k_2ValsMsk[pairNum], k_selectVals, maxAbs128);
73 /// Compute exponent value for a set of 16 RB from the maximum absolute value.
75 computeExponent_16RB(const BlockFloatCompander::ExpandedData& dataIn, const __m512i totShiftBits)
77 __m512i maxAbs = __m512i();
78 const __m512i* dataInAddr = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
80 for (int n = 0; n < 8; ++n)
82 maxAbs = maxAbsOneReg(maxAbs, dataInAddr + n, n);
84 /// Calculate exponent
85 return BlockFloatCompander::expLzCnt(maxAbs, totShiftBits);
88 /// Compute exponent value for a set of 4 RB from the maximum absolute value.
90 computeExponent_4RB(const BlockFloatCompander::ExpandedData& dataIn, const __m512i totShiftBits)
92 __m512i maxAbs = __m512i();
93 const __m512i* dataInAddr = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
95 for (int n = 0; n < 2; ++n)
97 maxAbs = maxAbsOneReg(maxAbs, dataInAddr + n, n);
99 /// Calculate exponent
100 return BlockFloatCompander::expLzCnt(maxAbs, totShiftBits);
103 /// Compute exponent value for 1 RB from the maximum absolute value.
105 computeExponent_1RB(const BlockFloatCompander::ExpandedData& dataIn, const __m512i totShiftBits)
107 __m512i maxAbs = __m512i();
108 const __m512i* dataInAddr = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
109 maxAbs = maxAbsOneReg(maxAbs, dataInAddr, 0);
110 /// Calculate exponent
111 const auto exps = BlockFloatCompander::expLzCnt(maxAbs, totShiftBits);
112 return ((uint8_t*)&exps)[0];
117 /// Apply compression to one compression block
118 template<BlockFloatCompander::PackFunction networkBytePack>
120 applyCompressionN_1RB(const __m512i* dataIn, uint8_t* outBlockAddr,
121 const int iqWidth, const uint8_t thisExp, const uint32_t rbWriteMask)
123 /// Store exponents first
124 *outBlockAddr = thisExp;
125 /// Apply the exponent shift
126 /// First Store the two exponent values in one register
127 const auto compData = _mm512_srai_epi16(*dataIn, thisExp);
128 /// Pack compressed data network byte order
129 const auto compDataBytePacked = networkBytePack(compData);
130 /// Store compressed data
131 _mm256_mask_storeu_epi8(outBlockAddr + 1, rbWriteMask, _mm512_extracti64x4_epi64(compDataBytePacked, 0));
134 /// Apply compression to two compression blocks
135 template<BlockFloatCompander::PackFunction networkBytePack>
137 applyCompressionN_2RB(const __m512i* dataIn, uint8_t* outBlockAddr,
138 const int totNumBytesPerBlock, const int iqWidth, const uint8_t* theseExps, const uint32_t rbWriteMask)
140 /// Store exponents first
141 *outBlockAddr = theseExps[0];
142 *(outBlockAddr + totNumBytesPerBlock) = theseExps[4];
143 /// Apply the exponent shift
144 /// First Store the two exponent values in one register
145 __m512i thisExp = __m512i();
146 constexpr uint32_t k_firstExpMask = 0x0000FFFF;
147 thisExp = _mm512_mask_set1_epi16(thisExp, k_firstExpMask, theseExps[0]);
148 constexpr uint32_t k_secondExpMask = 0xFFFF0000;
149 thisExp = _mm512_mask_set1_epi16(thisExp, k_secondExpMask, theseExps[4]);
150 const auto compData = _mm512_srav_epi16(*dataIn, thisExp);
151 /// Pack compressed data network byte order
152 const auto compDataBytePacked = networkBytePack(compData);
153 /// Output of network byte packing has each compression block packed in each half register
154 /// Store compressed data
155 _mm256_mask_storeu_epi8(outBlockAddr + 1, rbWriteMask, _mm512_extracti64x4_epi64(compDataBytePacked, 0));
156 _mm256_mask_storeu_epi8(outBlockAddr + totNumBytesPerBlock + 1, rbWriteMask, _mm512_extracti64x4_epi64(compDataBytePacked, 1));
159 /// Derive and apply 9, 10, or 12bit compression to 16 compression blocks
160 template<BlockFloatCompander::PackFunction networkBytePack>
162 compressN_16RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
163 const __m512i totShiftBits, const int totNumBytesPerBlock, const uint32_t rbWriteMask)
165 const auto exponents = computeExponent_16RB(dataIn, totShiftBits);
166 const __m512i* dataInAddr = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
168 for (int n = 0; n < 8; ++n)
170 applyCompressionN_2RB<networkBytePack>(dataInAddr + n, dataOut->dataCompressed + n * 2 * totNumBytesPerBlock, totNumBytesPerBlock, dataIn.iqWidth, ((uint8_t*)&exponents) + n * 8, rbWriteMask);
174 /// Derive and apply 9, 10, or 12bit compression to 4 compression blocks
175 template<BlockFloatCompander::PackFunction networkBytePack>
177 compressN_4RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
178 const __m512i totShiftBits, const int totNumBytesPerBlock, const uint32_t rbWriteMask)
180 const auto exponents = computeExponent_4RB(dataIn, totShiftBits);
181 const __m512i* dataInAddr = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
183 for (int n = 0; n < 2; ++n)
185 applyCompressionN_2RB<networkBytePack>(dataInAddr + n, dataOut->dataCompressed + n * 2 * totNumBytesPerBlock, totNumBytesPerBlock, dataIn.iqWidth, ((uint8_t*)&exponents) + n * 8, rbWriteMask);;
189 /// Derive and apply 9, 10, or 12bit compression to 1 RB
190 template<BlockFloatCompander::PackFunction networkBytePack>
192 compressN_1RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
193 const __m512i totShiftBits, const int totNumBytesPerBlock, const uint32_t rbWriteMask)
195 const auto thisExponent = computeExponent_1RB(dataIn, totShiftBits);
196 const __m512i* dataInAddr = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
197 applyCompressionN_1RB<networkBytePack>(dataInAddr, dataOut->dataCompressed, dataIn.iqWidth, thisExponent, rbWriteMask);
200 /// Calls compression function specific to the number of blocks to be executed. For 9, 10, or 12bit iqWidth.
201 template<BlockFloatCompander::PackFunction networkBytePack>
203 compressByAllocN(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
204 const __m512i totShiftBits, const int totNumBytesPerBlock, const uint32_t rbWriteMask)
206 switch (dataIn.numBlocks)
209 compressN_16RB<networkBytePack>(dataIn, dataOut, totShiftBits, totNumBytesPerBlock, rbWriteMask);
213 compressN_4RB<networkBytePack>(dataIn, dataOut, totShiftBits, totNumBytesPerBlock, rbWriteMask);
217 compressN_1RB<networkBytePack>(dataIn, dataOut, totShiftBits, totNumBytesPerBlock, rbWriteMask);
224 /// Apply 8b compression to 1 compression block.
226 applyCompression8_1RB(const __m256i* dataIn, uint8_t* outBlockAddr, const uint8_t thisExp)
228 /// Store exponent first
229 *outBlockAddr = thisExp;
230 /// Apply the exponent shift
231 const auto compData = _mm256_srai_epi16(*dataIn, thisExp);
232 /// Truncate to 8bit and store
233 constexpr uint16_t k_writeMask = 0xFFFF;
234 _mm_mask_storeu_epi8(outBlockAddr + 1, k_writeMask, _mm256_cvtepi16_epi8(compData));
237 /// Derive and apply 8b compression to 16 compression blocks
239 compress8_16RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut, const __m512i totShiftBits)
241 const __m512i exponents = computeExponent_16RB(dataIn, totShiftBits);
242 const __m256i* dataInAddr = reinterpret_cast<const __m256i*>(dataIn.dataExpanded);
244 for (int n = 0; n < 16; ++n)
246 applyCompression8_1RB(dataInAddr + n, dataOut->dataCompressed + n * (k_numDataElements + 1), ((uint8_t*)&exponents)[n * 4]);
250 /// Derive and apply 8b compression to 4 compression blocks
252 compress8_4RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut, const __m512i totShiftBits)
254 const __m512i exponents = computeExponent_4RB(dataIn, totShiftBits);
255 const __m256i* dataInAddr = reinterpret_cast<const __m256i*>(dataIn.dataExpanded);
257 for (int n = 0; n < 4; ++n)
259 applyCompression8_1RB(dataInAddr + n, dataOut->dataCompressed + n * (k_numDataElements + 1), ((uint8_t*)&exponents)[n * 4]);
263 /// Derive and apply 8b compression to 1 compression block
265 compress8_1RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut, const __m512i totShiftBits)
267 const auto thisExponent = computeExponent_1RB(dataIn, totShiftBits);
268 const __m256i* dataInAddr = reinterpret_cast<const __m256i*>(dataIn.dataExpanded);
269 applyCompression8_1RB(dataInAddr, dataOut->dataCompressed, thisExponent);
272 /// Calls compression function specific to the number of RB to be executed. For 8 bit iqWidth.
274 compressByAlloc8(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut, const __m512i totShiftBits)
276 switch (dataIn.numBlocks)
279 compress8_16RB(dataIn, dataOut, totShiftBits);
283 compress8_4RB(dataIn, dataOut, totShiftBits);
287 compress8_1RB(dataIn, dataOut, totShiftBits);
293 /// Expand 1 compression block
294 template<BlockFloatCompander::UnpackFunction256 networkByteUnpack>
296 applyExpansionN_1RB(const uint8_t* expAddr, __m256i* dataOutAddr, const int maxExpShift)
298 const auto thisExpShift = maxExpShift - *expAddr;
299 /// Unpack network order packed data
300 const auto inDataUnpacked = networkByteUnpack(expAddr + 1);
301 /// Apply exponent scaling (by appropriate arithmetic shift right)
302 const auto expandedData = _mm256_srai_epi16(inDataUnpacked, thisExpShift);
303 /// Write expanded data to output
304 static constexpr uint8_t k_WriteMask = 0x0F;
305 _mm256_mask_storeu_epi64(dataOutAddr, k_WriteMask, expandedData);
308 /// Calls expansion function specific to the number of blocks to be executed. For 9, 10, or 12bit iqWidth.
309 template<BlockFloatCompander::UnpackFunction256 networkByteUnpack>
310 void expandByAllocN(const BlockFloatCompander::CompressedData& dataIn, BlockFloatCompander::ExpandedData* dataOut,
311 const int totNumBytesPerBlock, const int maxExpShift)
313 __m256i* dataOutAddr = reinterpret_cast<__m256i*>(dataOut->dataExpanded);
314 switch (dataIn.numBlocks)
318 for (int n = 0; n < 16; ++n)
320 applyExpansionN_1RB<networkByteUnpack>(dataIn.dataCompressed + n * totNumBytesPerBlock, dataOutAddr + n, maxExpShift);
326 for (int n = 0; n < 4; ++n)
328 applyExpansionN_1RB<networkByteUnpack>(dataIn.dataCompressed + n * totNumBytesPerBlock, dataOutAddr + n, maxExpShift);
333 applyExpansionN_1RB<networkByteUnpack>(dataIn.dataCompressed, dataOutAddr, maxExpShift);
339 /// Apply expansion to 2 compression block
341 applyExpansion8_1RB(const uint8_t* expAddr, __m256i* dataOutAddr)
343 const __m128i* rawDataIn = reinterpret_cast<const __m128i*>(expAddr + 1);
344 const auto compData16 = _mm256_cvtepi8_epi16(*rawDataIn);
345 const auto expData = _mm256_slli_epi16(compData16, *expAddr);
346 static constexpr uint8_t k_WriteMask = 0x0F;
347 _mm256_mask_storeu_epi64(dataOutAddr, k_WriteMask, expData);
350 /// Calls expansion function specific to the number of RB to be executed. For 8 bit iqWidth.
352 expandByAlloc8(const BlockFloatCompander::CompressedData& dataIn, BlockFloatCompander::ExpandedData* dataOut)
354 __m256i* dataOutAddr = reinterpret_cast<__m256i*>(dataOut->dataExpanded);
355 switch (dataIn.numBlocks)
359 for (int n = 0; n < 16; ++n)
361 applyExpansion8_1RB(dataIn.dataCompressed + n * (k_numDataElements + 1), dataOutAddr + n);
367 for (int n = 0; n < 4; ++n)
369 applyExpansion8_1RB(dataIn.dataCompressed + n * (k_numDataElements + 1), dataOutAddr + n);
374 applyExpansion8_1RB(dataIn.dataCompressed, dataOutAddr);
381 /// Main kernel function for 8 antenna C-plane compression.
382 /// Starts by determining iqWidth specific parameters and functions.
384 BlockFloatCompander::BFPCompressCtrlPlane8AvxSnc(const ExpandedData& dataIn, CompressedData* dataOut)
386 /// Compensation for extra zeros in 32b leading zero count when computing exponent
387 const auto totShiftBits8 = _mm512_set1_epi32(25);
388 const auto totShiftBits9 = _mm512_set1_epi32(24);
389 const auto totShiftBits10 = _mm512_set1_epi32(23);
390 const auto totShiftBits12 = _mm512_set1_epi32(21);
392 /// Total number of data bytes per compression block is (iqWidth * numElements / 8) + 1
393 const auto totNumBytesPerBlock = ((BFP_CPlane_8_SNC::k_numDataElements * dataIn.iqWidth) >> 3) + 1;
395 /// Compressed data write mask for each iqWidth option
396 /// Compressed data write mask for each iqWidth option
397 constexpr uint32_t rbWriteMask9 = 0x0003FFFF;
398 constexpr uint32_t rbWriteMask10 = 0x000FFFFF;
399 constexpr uint32_t rbWriteMask12 = 0x00FFFFFF;
401 switch (dataIn.iqWidth)
404 BFP_CPlane_8_SNC::compressByAlloc8(dataIn, dataOut, totShiftBits8);
408 BFP_CPlane_8_SNC::compressByAllocN<BlockFloatCompander::networkBytePack9bSncB>(dataIn, dataOut, totShiftBits9, totNumBytesPerBlock, rbWriteMask9);
412 BFP_CPlane_8_SNC::compressByAllocN<BlockFloatCompander::networkBytePack10bSncB>(dataIn, dataOut, totShiftBits10, totNumBytesPerBlock, rbWriteMask10);
416 BFP_CPlane_8_SNC::compressByAllocN<BlockFloatCompander::networkBytePack12bSncB>(dataIn, dataOut, totShiftBits12, totNumBytesPerBlock, rbWriteMask12);
422 /// Main kernel function for 8 antenna C-plane expansion.
423 /// Starts by determining iqWidth specific parameters and functions.
425 BlockFloatCompander::BFPExpandCtrlPlane8AvxSnc(const CompressedData& dataIn, ExpandedData* dataOut)
427 constexpr int k_maxExpShift9 = 7;
428 constexpr int k_maxExpShift10 = 6;
429 constexpr int k_maxExpShift12 = 4;
431 /// Total number of data bytes per compression block is (iqWidth * numElements / 8) + 1
432 const auto totNumBytesPerBlock = ((BFP_CPlane_8_SNC::k_numDataElements * dataIn.iqWidth) >> 3) + 1;
434 switch (dataIn.iqWidth)
437 BFP_CPlane_8_SNC::expandByAlloc8(dataIn, dataOut);
441 BFP_CPlane_8_SNC::expandByAllocN<BlockFloatCompander::networkByteUnpack9b256Snc>(dataIn, dataOut, totNumBytesPerBlock, k_maxExpShift9);
445 BFP_CPlane_8_SNC::expandByAllocN<BlockFloatCompander::networkByteUnpack10b256Snc>(dataIn, dataOut, totNumBytesPerBlock, k_maxExpShift10);
449 BFP_CPlane_8_SNC::expandByAllocN<BlockFloatCompander::networkByteUnpack12b256Snc>(dataIn, dataOut, totNumBytesPerBlock, k_maxExpShift12);