* INTC Contribution to the O-RAN F Release for O-DU Low
[o-du/phy.git] / fhi_lib / lib / src / xran_bfp_cplane8.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
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 uint16_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     /// 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));
135   }
136
137   /// Apply compression to two compression blocks
138   template<BlockFloatCompander::PackFunction networkBytePack>
139   inline void
140   applyCompressionN_2RB(const __m512i* dataIn, uint8_t* outBlockAddr,
141                         const int totNumBytesPerBlock, const int iqWidth, const uint8_t* theseExps, const uint16_t rbWriteMask)
142   {
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));
164   }
165
166   /// Derive and apply 9, 10, or 12bit compression to 16 compression blocks
167   template<BlockFloatCompander::PackFunction networkBytePack>
168   inline void
169   compressN_16RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
170                  const __m512i totShiftBits, const int totNumBytesPerBlock, const uint16_t rbWriteMask)
171   {
172     const auto exponents = computeExponent_16RB(dataIn, totShiftBits);
173     const __m512i* dataInAddr = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
174 #pragma unroll(8)
175     for (int n = 0; n < 8; ++n)
176     {
177       applyCompressionN_2RB<networkBytePack>(dataInAddr + n, dataOut->dataCompressed + n * 2 * totNumBytesPerBlock, totNumBytesPerBlock, dataIn.iqWidth, ((uint8_t*)&exponents) + n * 8, rbWriteMask);
178     }
179   }
180
181   /// Derive and apply 9, 10, or 12bit compression to 4 compression blocks
182   template<BlockFloatCompander::PackFunction networkBytePack>
183   inline void
184   compressN_4RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
185                 const __m512i totShiftBits, const int totNumBytesPerBlock, const uint16_t rbWriteMask)
186   {
187     const auto exponents = computeExponent_4RB(dataIn, totShiftBits);
188     const __m512i* dataInAddr = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);
189 #pragma unroll(2)
190     for (int n = 0; n < 2; ++n)
191     {
192       applyCompressionN_2RB<networkBytePack>(dataInAddr + n, dataOut->dataCompressed + n * 2 * totNumBytesPerBlock, totNumBytesPerBlock, dataIn.iqWidth, ((uint8_t*)&exponents) + n * 8, rbWriteMask);;
193     }
194   }
195
196   /// Derive and apply 9, 10, or 12bit compression to 1 RB
197   template<BlockFloatCompander::PackFunction networkBytePack>
198   inline void
199   compressN_1RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
200                 const __m512i totShiftBits, const int totNumBytesPerBlock, const uint16_t rbWriteMask)
201   {
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);
205   }
206
207   /// Calls compression function specific to the number of blocks to be executed. For 9, 10, or 12bit iqWidth.
208   template<BlockFloatCompander::PackFunction networkBytePack>
209   inline void
210   compressByAllocN(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut,
211                    const __m512i totShiftBits, const int totNumBytesPerBlock, const uint16_t rbWriteMask)
212   {
213     switch (dataIn.numBlocks)
214     {
215     case 16:
216       compressN_16RB<networkBytePack>(dataIn, dataOut, totShiftBits, totNumBytesPerBlock, rbWriteMask);
217       break;
218
219     case 4:
220       compressN_4RB<networkBytePack>(dataIn, dataOut, totShiftBits, totNumBytesPerBlock, rbWriteMask);
221       break;
222
223     case 1:
224       compressN_1RB<networkBytePack>(dataIn, dataOut, totShiftBits, totNumBytesPerBlock, rbWriteMask);
225       break;
226     }
227   }
228
229
230
231   /// Apply 8b compression to 1 compression block.
232   inline void
233   applyCompression8_1RB(const __m256i* dataIn, uint8_t* outBlockAddr, const uint8_t thisExp)
234   {
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));
242   }
243
244   /// Derive and apply 8b compression to 16 compression blocks
245   inline void
246   compress8_16RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut, const __m512i totShiftBits)
247   {
248     const __m512i exponents = computeExponent_16RB(dataIn, totShiftBits);
249     const __m256i* dataInAddr = reinterpret_cast<const __m256i*>(dataIn.dataExpanded);
250 #pragma unroll(16)
251     for (int n = 0; n < 16; ++n)
252     {
253       applyCompression8_1RB(dataInAddr + n, dataOut->dataCompressed + n * (k_numDataElements + 1), ((uint8_t*)&exponents)[n * 4]);
254     }
255   }
256
257   /// Derive and apply 8b compression to 4 compression blocks
258   inline void
259   compress8_4RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut, const __m512i totShiftBits)
260   {
261     const __m512i exponents = computeExponent_4RB(dataIn, totShiftBits);
262     const __m256i* dataInAddr = reinterpret_cast<const __m256i*>(dataIn.dataExpanded);
263 #pragma unroll(4)
264     for (int n = 0; n < 4; ++n)
265     {
266       applyCompression8_1RB(dataInAddr + n, dataOut->dataCompressed + n * (k_numDataElements + 1), ((uint8_t*)&exponents)[n * 4]);
267     }
268   }
269
270   /// Derive and apply 8b compression to 1 compression block
271   inline void
272   compress8_1RB(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut, const __m512i totShiftBits)
273   {
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);
277   }
278
279   /// Calls compression function specific to the number of RB to be executed. For 8 bit iqWidth.
280   inline void
281   compressByAlloc8(const BlockFloatCompander::ExpandedData& dataIn, BlockFloatCompander::CompressedData* dataOut, const __m512i totShiftBits)
282   {
283     switch (dataIn.numBlocks)
284     {
285     case 16:
286       compress8_16RB(dataIn, dataOut, totShiftBits);
287       break;
288
289     case 4:
290       compress8_4RB(dataIn, dataOut, totShiftBits);
291       break;
292
293     case 1:
294       compress8_1RB(dataIn, dataOut, totShiftBits);
295       break;
296     }
297   }
298
299
300   /// Expand 1 compression block
301   template<BlockFloatCompander::UnpackFunction256 networkByteUnpack>
302   inline void
303   applyExpansionN_1RB(const uint8_t* expAddr, __m256i* dataOutAddr, const int maxExpShift)
304   {
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);
313   }
314
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)
319   {
320     __m256i* dataOutAddr = reinterpret_cast<__m256i*>(dataOut->dataExpanded);
321     switch (dataIn.numBlocks)
322     {
323     case 16:
324 #pragma unroll(16)
325       for (int n = 0; n < 16; ++n)
326       {
327         applyExpansionN_1RB<networkByteUnpack>(dataIn.dataCompressed + n * totNumBytesPerBlock, dataOutAddr + n, maxExpShift);
328       }
329       break;
330
331     case 4:
332 #pragma unroll(4)
333       for (int n = 0; n < 4; ++n)
334       {
335         applyExpansionN_1RB<networkByteUnpack>(dataIn.dataCompressed + n * totNumBytesPerBlock, dataOutAddr + n, maxExpShift);
336       }
337       break;
338
339     case 1:
340       applyExpansionN_1RB<networkByteUnpack>(dataIn.dataCompressed, dataOutAddr, maxExpShift);
341       break;
342     }
343   }
344
345
346   /// Apply expansion to 2 compression block
347   inline void
348   applyExpansion8_1RB(const uint8_t* expAddr, __m256i* dataOutAddr)
349   {
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);
355   }
356
357   /// Calls expansion function specific to the number of RB to be executed. For 8 bit iqWidth.
358   void
359   expandByAlloc8(const BlockFloatCompander::CompressedData& dataIn, BlockFloatCompander::ExpandedData* dataOut)
360   {
361     __m256i* dataOutAddr = reinterpret_cast<__m256i*>(dataOut->dataExpanded);
362     switch (dataIn.numBlocks)
363     {
364     case 16:
365 #pragma unroll(16)
366       for (int n = 0; n < 16; ++n)
367       {
368         applyExpansion8_1RB(dataIn.dataCompressed + n * (k_numDataElements + 1), dataOutAddr + n);
369       }
370       break;
371
372     case 4:
373 #pragma unroll(4)
374       for (int n = 0; n < 4; ++n)
375       {
376         applyExpansion8_1RB(dataIn.dataCompressed + n * (k_numDataElements + 1), dataOutAddr + n);
377       }
378       break;
379
380     case 1:
381       applyExpansion8_1RB(dataIn.dataCompressed, dataOutAddr);
382       break;
383     }
384   }
385 }
386
387
388 /// Main kernel function for 8 antenna C-plane compression.
389 /// Starts by determining iqWidth specific parameters and functions.
390 void
391 BlockFloatCompander::BFPCompressCtrlPlane8Avx512(const ExpandedData& dataIn, CompressedData* dataOut)
392 {
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);
398
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;
401
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;
406
407   switch (dataIn.iqWidth)
408   {
409   case 8:
410     BFP_CPlane_8::compressByAlloc8(dataIn, dataOut, totShiftBits8);
411     break;
412
413   case 9:
414     BFP_CPlane_8::compressByAllocN<BlockFloatCompander::networkBytePack9b>(dataIn, dataOut, totShiftBits9, totNumBytesPerBlock, rbWriteMask9);
415     break;
416
417   case 10:
418     BFP_CPlane_8::compressByAllocN<BlockFloatCompander::networkBytePack10b>(dataIn, dataOut, totShiftBits10, totNumBytesPerBlock, rbWriteMask10);
419     break;
420
421   case 12:
422     BFP_CPlane_8::compressByAllocN<BlockFloatCompander::networkBytePack12b>(dataIn, dataOut, totShiftBits12, totNumBytesPerBlock, rbWriteMask12);
423     break;
424   }
425 }
426
427
428 /// Main kernel function for 8 antenna C-plane expansion.
429 /// Starts by determining iqWidth specific parameters and functions.
430 void
431 BlockFloatCompander::BFPExpandCtrlPlane8Avx512(const CompressedData& dataIn, ExpandedData* dataOut)
432 {
433   constexpr int k_maxExpShift9 = 7;
434   constexpr int k_maxExpShift10 = 6;
435   constexpr int k_maxExpShift12 = 4;
436
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;
439
440   switch (dataIn.iqWidth)
441   {
442   case 8:
443     BFP_CPlane_8::expandByAlloc8(dataIn, dataOut);
444     break;
445
446   case 9:
447     BFP_CPlane_8::expandByAllocN<BlockFloatCompander::networkByteUnpack9b256>(dataIn, dataOut, totNumBytesPerBlock, k_maxExpShift9);
448     break;
449
450   case 10:
451     BFP_CPlane_8::expandByAllocN<BlockFloatCompander::networkByteUnpack10b256>(dataIn, dataOut, totNumBytesPerBlock, k_maxExpShift10);
452     break;
453
454   case 12:
455     BFP_CPlane_8::expandByAllocN<BlockFloatCompander::networkByteUnpack12b256>(dataIn, dataOut, totNumBytesPerBlock, k_maxExpShift12);
456     break;
457   }
458 }