* INTC Contribution to the O-RAN F Release for O-DU Low
[o-du/phy.git] / fhi_lib / lib / src / xran_bfp_cplane8_snc.cpp
1 /******************************************************************************
2 *
3 *   Copyright (c) 2020 Intel.
4 *
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
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 *******************************************************************************/
18
19 /**
20  * @brief xRAN BFP compression/decompression for C-plane with 8T8R
21  *
22  * @file xran_bfp_cplane8.cpp
23  * @ingroup group_source_xran
24  * @author Intel Corporation
25  **/
26
27 #include "xran_compression.hpp"
28 #include "xran_bfp_utils.hpp"
29 #include "xran_bfp_byte_packing_utils.hpp"
30 #include <complex>
31 #include <algorithm>
32 #include <immintrin.h>
33
34
35 namespace BFP_CPlane_8_SNC
36 {
37   /// Namespace constants
38   const int k_numDataElements = 16; /// 16 IQ pairs
39
40   inline __m512i
41   maxAbsOneReg(const __m512i maxAbs, const __m512i* inData, const int pairNum)
42   {
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
46     /// each pair.
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);
71   }
72
73   /// Compute exponent value for a set of 16 RB from the maximum absolute value.
74   inline __m512i
75   computeExponent_16RB(const BlockFloatCompander::ExpandedData& dataIn, const __m512i totShiftBits)
76   {
77     __m512i maxAbs = __m512i();
78     const __m512i* dataInAddr = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
79 #pragma unroll(8)
80     for (int n = 0; n < 8; ++n)
81     {
82       maxAbs = maxAbsOneReg(maxAbs, dataInAddr + n, n);
83     }
84     /// Calculate exponent
85     return BlockFloatCompander::expLzCnt(maxAbs, totShiftBits);
86   }
87
88   /// Compute exponent value for a set of 4 RB from the maximum absolute value.
89   inline __m512i
90   computeExponent_4RB(const BlockFloatCompander::ExpandedData& dataIn, const __m512i totShiftBits)
91   {
92     __m512i maxAbs = __m512i();
93     const __m512i* dataInAddr = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
94 #pragma unroll(2)
95     for (int n = 0; n < 2; ++n)
96     {
97       maxAbs = maxAbsOneReg(maxAbs, dataInAddr + n, n);
98     }
99     /// Calculate exponent
100     return BlockFloatCompander::expLzCnt(maxAbs, totShiftBits);
101   }
102
103   /// Compute exponent value for 1 RB from the maximum absolute value.
104   inline uint8_t
105   computeExponent_1RB(const BlockFloatCompander::ExpandedData& dataIn, const __m512i totShiftBits)
106   {
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];
113   }
114
115
116
117   /// Apply compression to one compression block
118   template<BlockFloatCompander::PackFunction networkBytePack>
119   inline void
120   applyCompressionN_1RB(const __m512i* dataIn, uint8_t* outBlockAddr,
121                         const int iqWidth, const uint8_t thisExp, const uint32_t rbWriteMask)
122   {
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));
132   }
133
134   /// Apply compression to two compression blocks
135   template<BlockFloatCompander::PackFunction networkBytePack>
136   inline void
137   applyCompressionN_2RB(const __m512i* dataIn, uint8_t* outBlockAddr,
138                         const int totNumBytesPerBlock, const int iqWidth, const uint8_t* theseExps, const uint32_t rbWriteMask)
139   {
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));
157   }
158
159   /// Derive and apply 9, 10, or 12bit compression to 16 compression blocks
160   template<BlockFloatCompander::PackFunction networkBytePack>
161   inline void
162   compressN_16RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
163                  const __m512i totShiftBits, const int totNumBytesPerBlock, const uint32_t rbWriteMask)
164   {
165     const auto exponents = computeExponent_16RB(dataIn, totShiftBits);
166     const __m512i* dataInAddr = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
167 #pragma unroll(8)
168     for (int n = 0; n < 8; ++n)
169     {
170       applyCompressionN_2RB<networkBytePack>(dataInAddr + n, dataOut->dataCompressed + n * 2 * totNumBytesPerBlock, totNumBytesPerBlock, dataIn.iqWidth, ((uint8_t*)&exponents) + n * 8, rbWriteMask);
171     }
172   }
173
174   /// Derive and apply 9, 10, or 12bit compression to 4 compression blocks
175   template<BlockFloatCompander::PackFunction networkBytePack>
176   inline void
177   compressN_4RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
178                 const __m512i totShiftBits, const int totNumBytesPerBlock, const uint32_t rbWriteMask)
179   {
180     const auto exponents = computeExponent_4RB(dataIn, totShiftBits);
181     const __m512i* dataInAddr = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
182 #pragma unroll(2)
183     for (int n = 0; n < 2; ++n)
184     {
185       applyCompressionN_2RB<networkBytePack>(dataInAddr + n, dataOut->dataCompressed + n * 2 * totNumBytesPerBlock, totNumBytesPerBlock, dataIn.iqWidth, ((uint8_t*)&exponents) + n * 8, rbWriteMask);;
186     }
187   }
188
189   /// Derive and apply 9, 10, or 12bit compression to 1 RB
190   template<BlockFloatCompander::PackFunction networkBytePack>
191   inline void
192   compressN_1RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
193                 const __m512i totShiftBits, const int totNumBytesPerBlock, const uint32_t rbWriteMask)
194   {
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);
198   }
199
200   /// Calls compression function specific to the number of blocks to be executed. For 9, 10, or 12bit iqWidth.
201   template<BlockFloatCompander::PackFunction networkBytePack>
202   inline void
203   compressByAllocN(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
204                    const __m512i totShiftBits, const int totNumBytesPerBlock, const uint32_t rbWriteMask)
205   {
206     switch (dataIn.numBlocks)
207     {
208     case 16:
209       compressN_16RB<networkBytePack>(dataIn, dataOut, totShiftBits, totNumBytesPerBlock, rbWriteMask);
210       break;
211
212     case 4:
213       compressN_4RB<networkBytePack>(dataIn, dataOut, totShiftBits, totNumBytesPerBlock, rbWriteMask);
214       break;
215
216     case 1:
217       compressN_1RB<networkBytePack>(dataIn, dataOut, totShiftBits, totNumBytesPerBlock, rbWriteMask);
218       break;
219     }
220   }
221
222
223
224   /// Apply 8b compression to 1 compression block.
225   inline void
226   applyCompression8_1RB(const __m256i* dataIn, uint8_t* outBlockAddr, const uint8_t thisExp)
227   {
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));
235   }
236
237   /// Derive and apply 8b compression to 16 compression blocks
238   inline void
239   compress8_16RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut, const __m512i totShiftBits)
240   {
241     const __m512i exponents = computeExponent_16RB(dataIn, totShiftBits);
242     const __m256i* dataInAddr = reinterpret_cast<const __m256i*>(dataIn.dataExpanded);
243 #pragma unroll(16)
244     for (int n = 0; n < 16; ++n)
245     {
246       applyCompression8_1RB(dataInAddr + n, dataOut->dataCompressed + n * (k_numDataElements + 1), ((uint8_t*)&exponents)[n * 4]);
247     }
248   }
249
250   /// Derive and apply 8b compression to 4 compression blocks
251   inline void
252   compress8_4RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut, const __m512i totShiftBits)
253   {
254     const __m512i exponents = computeExponent_4RB(dataIn, totShiftBits);
255     const __m256i* dataInAddr = reinterpret_cast<const __m256i*>(dataIn.dataExpanded);
256 #pragma unroll(4)
257     for (int n = 0; n < 4; ++n)
258     {
259       applyCompression8_1RB(dataInAddr + n, dataOut->dataCompressed + n * (k_numDataElements + 1), ((uint8_t*)&exponents)[n * 4]);
260     }
261   }
262
263   /// Derive and apply 8b compression to 1 compression block
264   inline void
265   compress8_1RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut, const __m512i totShiftBits)
266   {
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);
270   }
271
272   /// Calls compression function specific to the number of RB to be executed. For 8 bit iqWidth.
273   inline void
274   compressByAlloc8(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut, const __m512i totShiftBits)
275   {
276     switch (dataIn.numBlocks)
277     {
278     case 16:
279       compress8_16RB(dataIn, dataOut, totShiftBits);
280       break;
281
282     case 4:
283       compress8_4RB(dataIn, dataOut, totShiftBits);
284       break;
285
286     case 1:
287       compress8_1RB(dataIn, dataOut, totShiftBits);
288       break;
289     }
290   }
291
292
293   /// Expand 1 compression block
294   template<BlockFloatCompander::UnpackFunction256 networkByteUnpack>
295   inline void
296   applyExpansionN_1RB(const uint8_t* expAddr, __m256i* dataOutAddr, const int maxExpShift)
297   {
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);
306   }
307
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)
312   {
313     __m256i* dataOutAddr = reinterpret_cast<__m256i*>(dataOut->dataExpanded);
314     switch (dataIn.numBlocks)
315     {
316     case 16:
317 #pragma unroll(16)
318       for (int n = 0; n < 16; ++n)
319       {
320         applyExpansionN_1RB<networkByteUnpack>(dataIn.dataCompressed + n * totNumBytesPerBlock, dataOutAddr + n, maxExpShift);
321       }
322       break;
323
324     case 4:
325 #pragma unroll(4)
326       for (int n = 0; n < 4; ++n)
327       {
328         applyExpansionN_1RB<networkByteUnpack>(dataIn.dataCompressed + n * totNumBytesPerBlock, dataOutAddr + n, maxExpShift);
329       }
330       break;
331
332     case 1:
333       applyExpansionN_1RB<networkByteUnpack>(dataIn.dataCompressed, dataOutAddr, maxExpShift);
334       break;
335     }
336   }
337
338
339   /// Apply expansion to 2 compression block
340   inline void
341   applyExpansion8_1RB(const uint8_t* expAddr, __m256i* dataOutAddr)
342   {
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);
348   }
349
350   /// Calls expansion function specific to the number of RB to be executed. For 8 bit iqWidth.
351   void
352   expandByAlloc8(const BlockFloatCompander::CompressedData& dataIn, BlockFloatCompander::ExpandedData* dataOut)
353   {
354     __m256i* dataOutAddr = reinterpret_cast<__m256i*>(dataOut->dataExpanded);
355     switch (dataIn.numBlocks)
356     {
357     case 16:
358 #pragma unroll(16)
359       for (int n = 0; n < 16; ++n)
360       {
361         applyExpansion8_1RB(dataIn.dataCompressed + n * (k_numDataElements + 1), dataOutAddr + n);
362       }
363       break;
364
365     case 4:
366 #pragma unroll(4)
367       for (int n = 0; n < 4; ++n)
368       {
369         applyExpansion8_1RB(dataIn.dataCompressed + n * (k_numDataElements + 1), dataOutAddr + n);
370       }
371       break;
372
373     case 1:
374       applyExpansion8_1RB(dataIn.dataCompressed, dataOutAddr);
375       break;
376     }
377   }
378 }
379
380
381 /// Main kernel function for 8 antenna C-plane compression.
382 /// Starts by determining iqWidth specific parameters and functions.
383 void
384 BlockFloatCompander::BFPCompressCtrlPlane8AvxSnc(const ExpandedData& dataIn, CompressedData* dataOut)
385 {
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);
391
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;
394
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;
400
401   switch (dataIn.iqWidth)
402   {
403   case 8:
404     BFP_CPlane_8_SNC::compressByAlloc8(dataIn, dataOut, totShiftBits8);
405     break;
406
407   case 9:
408     BFP_CPlane_8_SNC::compressByAllocN<BlockFloatCompander::networkBytePack9bSncB>(dataIn, dataOut, totShiftBits9, totNumBytesPerBlock, rbWriteMask9);
409     break;
410
411   case 10:
412     BFP_CPlane_8_SNC::compressByAllocN<BlockFloatCompander::networkBytePack10bSncB>(dataIn, dataOut, totShiftBits10, totNumBytesPerBlock, rbWriteMask10);
413     break;
414
415   case 12:
416     BFP_CPlane_8_SNC::compressByAllocN<BlockFloatCompander::networkBytePack12bSncB>(dataIn, dataOut, totShiftBits12, totNumBytesPerBlock, rbWriteMask12);
417     break;
418   }
419 }
420
421
422 /// Main kernel function for 8 antenna C-plane expansion.
423 /// Starts by determining iqWidth specific parameters and functions.
424 void
425 BlockFloatCompander::BFPExpandCtrlPlane8AvxSnc(const CompressedData& dataIn, ExpandedData* dataOut)
426 {
427   constexpr int k_maxExpShift9 = 7;
428   constexpr int k_maxExpShift10 = 6;
429   constexpr int k_maxExpShift12 = 4;
430
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;
433
434   switch (dataIn.iqWidth)
435   {
436   case 8:
437     BFP_CPlane_8_SNC::expandByAlloc8(dataIn, dataOut);
438     break;
439
440   case 9:
441     BFP_CPlane_8_SNC::expandByAllocN<BlockFloatCompander::networkByteUnpack9b256Snc>(dataIn, dataOut, totNumBytesPerBlock, k_maxExpShift9);
442     break;
443
444   case 10:
445     BFP_CPlane_8_SNC::expandByAllocN<BlockFloatCompander::networkByteUnpack10b256Snc>(dataIn, dataOut, totNumBytesPerBlock, k_maxExpShift10);
446     break;
447
448   case 12:
449     BFP_CPlane_8_SNC::expandByAllocN<BlockFloatCompander::networkByteUnpack12b256Snc>(dataIn, dataOut, totNumBytesPerBlock, k_maxExpShift12);
450     break;
451   }
452 }