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
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 uint16_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 /// Now have 1 register worth of bytes separated into 2 chunks (1 per lane)
131 /// Use two offset stores to join
132 const auto thisOutRegAddr1 = outBlockAddr + 1;
133 _mm_mask_storeu_epi8(thisOutRegAddr1, rbWriteMask, _mm512_extracti64x2_epi64(compDataBytePacked, 0));
134 _mm_mask_storeu_epi8(thisOutRegAddr1 + iqWidth, rbWriteMask, _mm512_extracti64x2_epi64(compDataBytePacked, 1));
137 /// Apply compression to two compression blocks
138 template<BlockFloatCompander::PackFunction networkBytePack>
140 applyCompressionN_2RB(const __m512i* dataIn, uint8_t* outBlockAddr,
141 const int totNumBytesPerBlock, const int iqWidth, const uint8_t* theseExps, const uint16_t rbWriteMask)
143 /// Store exponents first
144 *outBlockAddr = theseExps[0];
145 *(outBlockAddr + totNumBytesPerBlock) = theseExps[4];
146 /// Apply the exponent shift
147 /// First Store the two exponent values in one register
148 __m512i thisExp = __m512i();
149 constexpr uint32_t k_firstExpMask = 0x0000FFFF;
150 thisExp = _mm512_mask_set1_epi16(thisExp, k_firstExpMask, theseExps[0]);
151 constexpr uint32_t k_secondExpMask = 0xFFFF0000;
152 thisExp = _mm512_mask_set1_epi16(thisExp, k_secondExpMask, theseExps[4]);
153 const auto compData = _mm512_srav_epi16(*dataIn, thisExp);
154 /// Pack compressed data network byte order
155 const auto compDataBytePacked = networkBytePack(compData);
156 /// Now have 1 register worth of bytes separated into 4 chunks (1 per lane)
157 /// Use four offset stores to join
158 const auto thisOutRegAddr1 = outBlockAddr + 1;
159 const auto thisOutRegAddr2 = outBlockAddr + totNumBytesPerBlock + 1;
160 _mm_mask_storeu_epi8(thisOutRegAddr1, rbWriteMask, _mm512_extracti64x2_epi64(compDataBytePacked, 0));
161 _mm_mask_storeu_epi8(thisOutRegAddr1 + iqWidth, rbWriteMask, _mm512_extracti64x2_epi64(compDataBytePacked, 1));
162 _mm_mask_storeu_epi8(thisOutRegAddr2, rbWriteMask, _mm512_extracti64x2_epi64(compDataBytePacked, 2));
163 _mm_mask_storeu_epi8(thisOutRegAddr2 + iqWidth, rbWriteMask, _mm512_extracti64x2_epi64(compDataBytePacked, 3));
166 /// Derive and apply 9, 10, or 12bit compression to 16 compression blocks
167 template<BlockFloatCompander::PackFunction networkBytePack>
169 compressN_16RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
170 const __m512i totShiftBits, const int totNumBytesPerBlock, const uint16_t rbWriteMask)
172 const auto exponents = computeExponent_16RB(dataIn, totShiftBits);
173 const __m512i* dataInAddr = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
175 for (int n = 0; n < 8; ++n)
177 applyCompressionN_2RB<networkBytePack>(dataInAddr + n, dataOut->dataCompressed + n * 2 * totNumBytesPerBlock, totNumBytesPerBlock, dataIn.iqWidth, ((uint8_t*)&exponents) + n * 8, rbWriteMask);
181 /// Derive and apply 9, 10, or 12bit compression to 4 compression blocks
182 template<BlockFloatCompander::PackFunction networkBytePack>
184 compressN_4RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
185 const __m512i totShiftBits, const int totNumBytesPerBlock, const uint16_t rbWriteMask)
187 const auto exponents = computeExponent_4RB(dataIn, totShiftBits);
188 const __m512i* dataInAddr = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
190 for (int n = 0; n < 2; ++n)
192 applyCompressionN_2RB<networkBytePack>(dataInAddr + n, dataOut->dataCompressed + n * 2 * totNumBytesPerBlock, totNumBytesPerBlock, dataIn.iqWidth, ((uint8_t*)&exponents) + n * 8, rbWriteMask);;
196 /// Derive and apply 9, 10, or 12bit compression to 1 RB
197 template<BlockFloatCompander::PackFunction networkBytePack>
199 compressN_1RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
200 const __m512i totShiftBits, const int totNumBytesPerBlock, const uint16_t rbWriteMask)
202 const auto thisExponent = computeExponent_1RB(dataIn, totShiftBits);
203 const __m512i* dataInAddr = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
204 applyCompressionN_1RB<networkBytePack>(dataInAddr, dataOut->dataCompressed, dataIn.iqWidth, thisExponent, rbWriteMask);
207 /// Calls compression function specific to the number of blocks to be executed. For 9, 10, or 12bit iqWidth.
208 template<BlockFloatCompander::PackFunction networkBytePack>
210 compressByAllocN(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
211 const __m512i totShiftBits, const int totNumBytesPerBlock, const uint16_t rbWriteMask)
213 switch (dataIn.numBlocks)
216 compressN_16RB<networkBytePack>(dataIn, dataOut, totShiftBits, totNumBytesPerBlock, rbWriteMask);
220 compressN_4RB<networkBytePack>(dataIn, dataOut, totShiftBits, totNumBytesPerBlock, rbWriteMask);
224 compressN_1RB<networkBytePack>(dataIn, dataOut, totShiftBits, totNumBytesPerBlock, rbWriteMask);
231 /// Apply 8b compression to 1 compression block.
233 applyCompression8_1RB(const __m256i* dataIn, uint8_t* outBlockAddr, const uint8_t thisExp)
235 /// Store exponent first
236 *outBlockAddr = thisExp;
237 /// Apply the exponent shift
238 const auto compData = _mm256_srai_epi16(*dataIn, thisExp);
239 /// Truncate to 8bit and store
240 constexpr uint16_t k_writeMask = 0xFFFF;
241 _mm_mask_storeu_epi8(outBlockAddr + 1, k_writeMask, _mm256_cvtepi16_epi8(compData));
244 /// Derive and apply 8b compression to 16 compression blocks
246 compress8_16RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut, const __m512i totShiftBits)
248 const auto exponents = computeExponent_16RB(dataIn, totShiftBits);
249 const __m256i* dataInAddr = reinterpret_cast<const __m256i*>(dataIn.dataExpanded);
251 for (int n = 0; n < 16; ++n)
253 applyCompression8_1RB(dataInAddr + n, dataOut->dataCompressed + n * (k_numDataElements + 1), ((uint8_t*)&exponents)[n * 4]);
257 /// Derive and apply 8b compression to 4 compression blocks
259 compress8_4RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut, const __m512i totShiftBits)
261 const auto exponents = computeExponent_4RB(dataIn, totShiftBits);
262 const __m256i* dataInAddr = reinterpret_cast<const __m256i*>(dataIn.dataExpanded);
264 for (int n = 0; n < 4; ++n)
266 applyCompression8_1RB(dataInAddr + n, dataOut->dataCompressed + n * (k_numDataElements + 1), ((uint8_t*)&exponents)[n * 4]);
270 /// Derive and apply 8b compression to 1 compression block
272 compress8_1RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut, const __m512i totShiftBits)
274 const auto thisExponent = computeExponent_1RB(dataIn, totShiftBits);
275 const __m256i* dataInAddr = reinterpret_cast<const __m256i*>(dataIn.dataExpanded);
276 applyCompression8_1RB(dataInAddr, dataOut->dataCompressed, thisExponent);
279 /// Calls compression function specific to the number of RB to be executed. For 8 bit iqWidth.
281 compressByAlloc8(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut, const __m512i totShiftBits)
283 switch (dataIn.numBlocks)
286 compress8_16RB(dataIn, dataOut, totShiftBits);
290 compress8_4RB(dataIn, dataOut, totShiftBits);
294 compress8_1RB(dataIn, dataOut, totShiftBits);
300 /// Expand 1 compression block
301 template<BlockFloatCompander::UnpackFunction256 networkByteUnpack>
303 applyExpansionN_1RB(const uint8_t* expAddr, __m256i* dataOutAddr, const int maxExpShift)
305 const auto thisExpShift = maxExpShift - *expAddr;
306 /// Unpack network order packed data
307 const auto inDataUnpacked = networkByteUnpack(expAddr + 1);
308 /// Apply exponent scaling (by appropriate arithmetic shift right)
309 const auto expandedData = _mm256_srai_epi16(inDataUnpacked, thisExpShift);
310 /// Write expanded data to output
311 static constexpr uint8_t k_WriteMask = 0x0F;
312 _mm256_mask_storeu_epi64(dataOutAddr, k_WriteMask, expandedData);
315 /// Calls expansion function specific to the number of blocks to be executed. For 9, 10, or 12bit iqWidth.
316 template<BlockFloatCompander::UnpackFunction256 networkByteUnpack>
317 void expandByAllocN(const BlockFloatCompander::CompressedData& dataIn, BlockFloatCompander::ExpandedData* dataOut,
318 const int totNumBytesPerBlock, const int maxExpShift)
320 __m256i* dataOutAddr = reinterpret_cast<__m256i*>(dataOut->dataExpanded);
321 switch (dataIn.numBlocks)
325 for (int n = 0; n < 16; ++n)
327 applyExpansionN_1RB<networkByteUnpack>(dataIn.dataCompressed + n * totNumBytesPerBlock, dataOutAddr + n, maxExpShift);
333 for (int n = 0; n < 4; ++n)
335 applyExpansionN_1RB<networkByteUnpack>(dataIn.dataCompressed + n * totNumBytesPerBlock, dataOutAddr + n, maxExpShift);
340 applyExpansionN_1RB<networkByteUnpack>(dataIn.dataCompressed, dataOutAddr, maxExpShift);
346 /// Apply expansion to 2 compression block
348 applyExpansion8_1RB(const uint8_t* expAddr, __m256i* dataOutAddr)
350 const __m128i* rawDataIn = reinterpret_cast<const __m128i*>(expAddr + 1);
351 const auto compData16 = _mm256_cvtepi8_epi16(*rawDataIn);
352 const auto expData = _mm256_slli_epi16(compData16, *expAddr);
353 static constexpr uint8_t k_WriteMask = 0x0F;
354 _mm256_mask_storeu_epi64(dataOutAddr, k_WriteMask, expData);
357 /// Calls expansion function specific to the number of RB to be executed. For 8 bit iqWidth.
359 expandByAlloc8(const BlockFloatCompander::CompressedData& dataIn, BlockFloatCompander::ExpandedData* dataOut)
361 __m256i* dataOutAddr = reinterpret_cast<__m256i*>(dataOut->dataExpanded);
362 switch (dataIn.numBlocks)
366 for (int n = 0; n < 16; ++n)
368 applyExpansion8_1RB(dataIn.dataCompressed + n * (k_numDataElements + 1), dataOutAddr + n);
374 for (int n = 0; n < 4; ++n)
376 applyExpansion8_1RB(dataIn.dataCompressed + n * (k_numDataElements + 1), dataOutAddr + n);
381 applyExpansion8_1RB(dataIn.dataCompressed, dataOutAddr);
388 /// Main kernel function for 8 antenna C-plane compression.
389 /// Starts by determining iqWidth specific parameters and functions.
391 BlockFloatCompander::BFPCompressCtrlPlane8Avx512(const ExpandedData& dataIn, CompressedData* dataOut)
393 /// Compensation for extra zeros in 32b leading zero count when computing exponent
394 const auto totShiftBits8 = _mm512_set1_epi32(25);
395 const auto totShiftBits9 = _mm512_set1_epi32(24);
396 const auto totShiftBits10 = _mm512_set1_epi32(23);
397 const auto totShiftBits12 = _mm512_set1_epi32(21);
399 /// Total number of data bytes per compression block is (iqWidth * numElements / 8) + 1
400 const auto totNumBytesPerBlock = ((BFP_CPlane_8::k_numDataElements * dataIn.iqWidth) >> 3) + 1;
402 /// Compressed data write mask for each iqWidth option
403 constexpr uint16_t rbWriteMask9 = 0x01FF;
404 constexpr uint16_t rbWriteMask10 = 0x03FF;
405 constexpr uint16_t rbWriteMask12 = 0x0FFF;
407 switch (dataIn.iqWidth)
410 BFP_CPlane_8::compressByAlloc8(dataIn, dataOut, totShiftBits8);
414 BFP_CPlane_8::compressByAllocN<BlockFloatCompander::networkBytePack9b>(dataIn, dataOut, totShiftBits9, totNumBytesPerBlock, rbWriteMask9);
418 BFP_CPlane_8::compressByAllocN<BlockFloatCompander::networkBytePack10b>(dataIn, dataOut, totShiftBits10, totNumBytesPerBlock, rbWriteMask10);
422 BFP_CPlane_8::compressByAllocN<BlockFloatCompander::networkBytePack12b>(dataIn, dataOut, totShiftBits12, totNumBytesPerBlock, rbWriteMask12);
428 /// Main kernel function for 8 antenna C-plane expansion.
429 /// Starts by determining iqWidth specific parameters and functions.
431 BlockFloatCompander::BFPExpandCtrlPlane8Avx512(const CompressedData& dataIn, ExpandedData* dataOut)
433 constexpr int k_maxExpShift9 = 7;
434 constexpr int k_maxExpShift10 = 6;
435 constexpr int k_maxExpShift12 = 4;
437 /// Total number of data bytes per compression block is (iqWidth * numElements / 8) + 1
438 const auto totNumBytesPerBlock = ((BFP_CPlane_8::k_numDataElements * dataIn.iqWidth) >> 3) + 1;
440 switch (dataIn.iqWidth)
443 BFP_CPlane_8::expandByAlloc8(dataIn, dataOut);
447 BFP_CPlane_8::expandByAllocN<BlockFloatCompander::networkByteUnpack9b256>(dataIn, dataOut, totNumBytesPerBlock, k_maxExpShift9);
451 BFP_CPlane_8::expandByAllocN<BlockFloatCompander::networkByteUnpack10b256>(dataIn, dataOut, totNumBytesPerBlock, k_maxExpShift10);
455 BFP_CPlane_8::expandByAllocN<BlockFloatCompander::networkByteUnpack12b256>(dataIn, dataOut, totNumBytesPerBlock, k_maxExpShift12);