O-RAN E Maintenance Release contribution for ODULOW
[o-du/phy.git] / fhi_lib / lib / src / xran_compression.cpp
index f32c243..112caae 100644 (file)
-/******************************************************************************\r
-*\r
-*   Copyright (c) 2019 Intel.\r
-*\r
-*   Licensed under the Apache License, Version 2.0 (the "License");\r
-*   you may not use this file except in compliance with the License.\r
-*   You may obtain a copy of the License at\r
-*\r
-*       http://www.apache.org/licenses/LICENSE-2.0\r
-*\r
-*   Unless required by applicable law or agreed to in writing, software\r
-*   distributed under the License is distributed on an "AS IS" BASIS,\r
-*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
-*   See the License for the specific language governing permissions and\r
-*   limitations under the License.\r
-*\r
-*******************************************************************************/\r
-\r
-#include "xran_compression.hpp"\r
-#include "xran_compression.h"\r
-#include <complex>\r
-#include <algorithm>\r
-#include <immintrin.h>\r
-#include <limits.h>\r
-#include <cstring>\r
-\r
-static int16_t saturateAbs(int16_t inVal)\r
-{\r
-  int16_t result;\r
-  if (inVal == std::numeric_limits<short>::min())\r
-  {\r
-    result = std::numeric_limits<short>::max();\r
-  }\r
-  else\r
-  {\r
-    result = (int16_t)std::abs(inVal);\r
-  }\r
-  return result;\r
-}\r
-\r
-\r
-/// Compute exponent value for a set of RB from the maximum absolute value\r
-void\r
-computeExponent(const BlockFloatCompander::ExpandedData& dataIn, int8_t* expStore)\r
-{\r
-  __m512i maxAbs = __m512i();\r
-\r
-  /// Load data and find max(abs(RB))\r
-  const __m512i* rawData = reinterpret_cast<const __m512i*>(dataIn.dataExpanded);\r
-  constexpr int k_numRBPerLoop = 4;\r
-  constexpr int k_numInputLoopIts = BlockFloatCompander::k_numRB / k_numRBPerLoop;\r
-\r
-#pragma unroll(k_numInputLoopIts)\r
-  for (int n = 0; n < k_numInputLoopIts; ++n)\r
-  {\r
-    /// Re-order the next 4RB in input data into 3 registers\r
-    /// Input SIMD vectors are:\r
-    ///   [A A A A A A A A A A A A B B B B]\r
-    ///   [B B B B B B B B C C C C C C C C]\r
-    ///   [C C C C D D D D D D D D D D D D]\r
-    /// Re-ordered SIMD vectors are:\r
-    ///   [A A A A B B B B C C C C D D D D]\r
-    ///   [A A A A B B B B C C C C D D D D]\r
-    ///   [A A A A B B B B C C C C D D D D]\r
-    constexpr uint8_t k_msk1 = 0b11111100; // Copy first lane of src\r
-    constexpr int k_shuff1 = 0x41;\r
-    const auto z_w1 = _mm512_mask_shuffle_i64x2(rawData[3 * n + 0], k_msk1, rawData[3 * n + 1], rawData[3 * n + 2], k_shuff1);\r
-\r
-    constexpr uint8_t k_msk2 = 0b11000011; // Copy middle two lanes of src\r
-    constexpr int k_shuff2 = 0xB1;\r
-    const auto z_w2 = _mm512_mask_shuffle_i64x2(rawData[3 * n + 1], k_msk2, rawData[3 * n + 0], rawData[3 * n + 2], k_shuff2);\r
-\r
-    constexpr uint8_t k_msk3 = 0b00111111; // Copy last lane of src\r
-    constexpr int k_shuff3 = 0xBE;\r
-    const auto z_w3 = _mm512_mask_shuffle_i64x2(rawData[3 * n + 2], k_msk3, rawData[3 * n + 0], rawData[3 * n + 1], k_shuff3);\r
-\r
-    /// Perform max abs on these 3 registers\r
-    const auto abs16_1 = _mm512_abs_epi16(z_w1);\r
-    const auto abs16_2 = _mm512_abs_epi16(z_w2);\r
-    const auto abs16_3 = _mm512_abs_epi16(z_w3);\r
-    const auto maxAbs_12 = _mm512_max_epi16(abs16_1, abs16_2);\r
-    const auto maxAbs_123 = _mm512_max_epi16(maxAbs_12, abs16_3);\r
-\r
-    /// Perform horizontal max over each lane\r
-    /// Swap 64b in each lane and compute max\r
-    const auto k_perm64b = _mm512_set_epi64(6, 7, 4, 5, 2, 3, 0, 1);\r
-    auto maxAbsPerm = _mm512_permutexvar_epi64(k_perm64b, maxAbs_123);\r
-    auto maxAbsHorz = _mm512_max_epi16(maxAbs_123, maxAbsPerm);\r
-\r
-    /// Swap each pair of 32b in each lane and compute max\r
-    const auto k_perm32b = _mm512_set_epi32(14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1);\r
-    maxAbsPerm = _mm512_permutexvar_epi32(k_perm32b, maxAbsHorz);\r
-    maxAbsHorz = _mm512_max_epi16(maxAbsHorz, maxAbsPerm);\r
-\r
-    /// Swap each IQ pair in each lane (via 32b rotation) and compute max\r
-    maxAbsPerm = _mm512_rol_epi32(maxAbsHorz, BlockFloatCompander::k_numBitsIQ);\r
-    maxAbsHorz = _mm512_max_epi16(maxAbsHorz, maxAbsPerm);\r
-\r
-    /// Insert values into maxAbs\r
-    /// Use sliding mask to insert wanted values into maxAbs\r
-    /// Pairs of values will be inserted and corrected outside of loop\r
-    const auto k_select4RB = _mm512_set_epi32(28, 24, 20, 16, 28, 24, 20, 16,\r
-                                              28, 24, 20, 16, 28, 24, 20, 16);\r
-    constexpr uint16_t k_expMsk[k_numInputLoopIts] = { 0x000F, 0x00F0, 0x0F00, 0xF000 };\r
-    maxAbs = _mm512_mask_permutex2var_epi32(maxAbs, k_expMsk[n], k_select4RB, maxAbsHorz);\r
-  }\r
-\r
-  /// Convert to 32b by removing repeated values in maxAbs\r
-  const auto k_upperWordMask = _mm512_set_epi64(0x0000FFFF0000FFFF, 0x0000FFFF0000FFFF,\r
-                                                0x0000FFFF0000FFFF, 0x0000FFFF0000FFFF,\r
-                                                0x0000FFFF0000FFFF, 0x0000FFFF0000FFFF,\r
-                                                0x0000FFFF0000FFFF, 0x0000FFFF0000FFFF);\r
-  maxAbs = _mm512_and_epi64(maxAbs, k_upperWordMask);\r
-\r
-  /// Compute and store exponent\r
-  const auto totShiftBits = _mm512_set1_epi32(32 - dataIn.iqWidth + 1);\r
-  const auto lzCount = _mm512_lzcnt_epi32(maxAbs);\r
-  const auto exponent = _mm512_sub_epi32(totShiftBits, lzCount);\r
-  constexpr uint16_t k_expWriteMask = 0xFFFF;\r
-  _mm512_mask_cvtepi32_storeu_epi8(expStore, k_expWriteMask, exponent);\r
-}\r
-\r
-\r
-/// Pack compressed 9 bit data in network byte order\r
-/// See https://soco.intel.com/docs/DOC-2665619\r
-__m512i\r
-networkBytePack9b(const __m512i compData)\r
-{\r
-  /// Logical shift left to align network order byte parts\r
-  const __m512i k_shiftLeft = _mm512_set_epi64(0x0000000100020003, 0x0004000500060007,\r
-                                               0x0000000100020003, 0x0004000500060007,\r
-                                               0x0000000100020003, 0x0004000500060007,\r
-                                               0x0000000100020003, 0x0004000500060007);\r
-  auto compDataPacked = _mm512_sllv_epi16(compData, k_shiftLeft);\r
-\r
-  /// First epi8 shuffle of even indexed samples\r
-  const __m512i k_byteShuffleMask1 = _mm512_set_epi64(0x0000000000000000, 0x0C0D080904050001,\r
-                                                      0x0000000000000000, 0x0C0D080904050001,\r
-                                                      0x0000000000000000, 0x0C0D080904050001,\r
-                                                      0x0000000000000000, 0x0C0D080904050001);\r
-  constexpr uint64_t k_byteMask1 = 0x000000FF00FF00FF;\r
-  auto compDataShuff1 = _mm512_maskz_shuffle_epi8(k_byteMask1, compDataPacked, k_byteShuffleMask1);\r
-\r
-  /// Second epi8 shuffle of odd indexed samples\r
-  const __m512i k_byteShuffleMask2 = _mm512_set_epi64(0x000000000000000E, 0x0F0A0B0607020300,\r
-                                                      0x000000000000000E, 0x0F0A0B0607020300,\r
-                                                      0x000000000000000E, 0x0F0A0B0607020300,\r
-                                                      0x000000000000000E, 0x0F0A0B0607020300);\r
-  constexpr uint64_t k_byteMask2 = 0x000001FE01FE01FE;\r
-  auto compDataShuff2 = _mm512_maskz_shuffle_epi8(k_byteMask2, compDataPacked, k_byteShuffleMask2);\r
-\r
-  /// Ternary blend of the two shuffled results\r
-  const __m512i k_ternLogSelect = _mm512_set_epi64(0x00000000000000FF, 0x01FC07F01FC07F00,\r
-                                                   0x00000000000000FF, 0x01FC07F01FC07F00,\r
-                                                   0x00000000000000FF, 0x01FC07F01FC07F00,\r
-                                                   0x00000000000000FF, 0x01FC07F01FC07F00);\r
-  return _mm512_ternarylogic_epi64(compDataShuff1, compDataShuff2, k_ternLogSelect, 0xd8);\r
-}\r
-\r
-\r
-/// Pack compressed 10 bit data in network byte order\r
-/// See https://soco.intel.com/docs/DOC-2665619\r
-__m512i\r
-networkBytePack10b(const __m512i compData)\r
-{\r
-  /// Logical shift left to align network order byte parts\r
-  const __m512i k_shiftLeft = _mm512_set_epi64(0x0000000200040006, 0x0000000200040006,\r
-                                               0x0000000200040006, 0x0000000200040006,\r
-                                               0x0000000200040006, 0x0000000200040006,\r
-                                               0x0000000200040006, 0x0000000200040006);\r
-  auto compDataPacked = _mm512_sllv_epi16(compData, k_shiftLeft);\r
-\r
-  /// First epi8 shuffle of even indexed samples\r
-  const __m512i k_byteShuffleMask1 = _mm512_set_epi64(0x000000000000000C, 0x0D08090004050001,\r
-                                                      0x000000000000000C, 0x0D08090004050001,\r
-                                                      0x000000000000000C, 0x0D08090004050001,\r
-                                                      0x000000000000000C, 0x0D08090004050001);\r
-  constexpr uint64_t k_byteMask1 = 0x000001EF01EF01EF;\r
-  auto compDataShuff1 = _mm512_maskz_shuffle_epi8(k_byteMask1, compDataPacked, k_byteShuffleMask1);\r
-\r
-  /// Second epi8 shuffle of odd indexed samples\r
-  const __m512i k_byteShuffleMask2 = _mm512_set_epi64(0x0000000000000E0F, 0x0A0B000607020300,\r
-                                                      0x0000000000000E0F, 0x0A0B000607020300,\r
-                                                      0x0000000000000E0F, 0x0A0B000607020300,\r
-                                                      0x0000000000000E0F, 0x0A0B000607020300);\r
-  constexpr uint64_t k_byteMask2 = 0x000003DE03DE03DE;\r
-  auto compDataShuff2 = _mm512_maskz_shuffle_epi8(k_byteMask2, compDataPacked, k_byteShuffleMask2);\r
-\r
-  /// Ternary blend of the two shuffled results\r
-  const __m512i k_ternLogSelect = _mm512_set_epi64(0x000000000000FF03, 0xF03F00FF03F03F00,\r
-                                                   0x000000000000FF03, 0xF03F00FF03F03F00,\r
-                                                   0x000000000000FF03, 0xF03F00FF03F03F00,\r
-                                                   0x000000000000FF03, 0xF03F00FF03F03F00);\r
-  return _mm512_ternarylogic_epi64(compDataShuff1, compDataShuff2, k_ternLogSelect, 0xd8);\r
-}\r
-\r
-\r
-/// Pack compressed 12 bit data in network byte order\r
-/// See https://soco.intel.com/docs/DOC-2665619\r
-__m512i\r
-networkBytePack12b(const __m512i compData)\r
-{\r
-  /// Logical shift left to align network order byte parts\r
-  const __m512i k_shiftLeft = _mm512_set_epi64(0x0000000400000004, 0x0000000400000004,\r
-                                               0x0000000400000004, 0x0000000400000004,\r
-                                               0x0000000400000004, 0x0000000400000004,\r
-                                               0x0000000400000004, 0x0000000400000004);\r
-  auto compDataPacked = _mm512_sllv_epi16(compData, k_shiftLeft);\r
-\r
-  /// First epi8 shuffle of even indexed samples\r
-  const __m512i k_byteShuffleMask1 = _mm512_set_epi64(0x00000000000C0D00, 0x0809000405000001,\r
-                                                      0x00000000000C0D00, 0x0809000405000001,\r
-                                                      0x00000000000C0D00, 0x0809000405000001,\r
-                                                      0x00000000000C0D00, 0x0809000405000001);\r
-  constexpr uint64_t k_byteMask1 = 0x000006DB06DB06DB;\r
-  auto compDataShuff1 = _mm512_maskz_shuffle_epi8(k_byteMask1, compDataPacked, k_byteShuffleMask1);\r
-\r
-  /// Second epi8 shuffle of odd indexed samples\r
-  const __m512i k_byteShuffleMask2 = _mm512_set_epi64(0x000000000E0F000A, 0x0B00060700020300,\r
-                                                      0x000000000E0F000A, 0x0B00060700020300,\r
-                                                      0x000000000E0F000A, 0x0B00060700020300,\r
-                                                      0x000000000E0F000A, 0x0B00060700020300);\r
-  constexpr uint64_t k_byteMask2 = 0x00000DB60DB60DB6;\r
-  auto compDataShuff2 = _mm512_maskz_shuffle_epi8(k_byteMask2, compDataPacked, k_byteShuffleMask2);\r
-\r
-  /// Ternary blend of the two shuffled results\r
-  const __m512i k_ternLogSelect = _mm512_set_epi64(0x00000000FF0F00FF, 0x0F00FF0F00FF0F00,\r
-                                                   0x00000000FF0F00FF, 0x0F00FF0F00FF0F00,\r
-                                                   0x00000000FF0F00FF, 0x0F00FF0F00FF0F00,\r
-                                                   0x00000000FF0F00FF, 0x0F00FF0F00FF0F00);\r
-  return _mm512_ternarylogic_epi64(compDataShuff1, compDataShuff2, k_ternLogSelect, 0xd8);\r
-}\r
-\r
-\r
-/// Unpack compressed 9 bit data in network byte order\r
-/// See https://soco.intel.com/docs/DOC-2665619\r
-__m512i\r
-networkByteUnpack9b(const uint8_t* inData)\r
-{\r
-  /// Align chunks of compressed bytes into lanes to allow for expansion\r
-  const __m512i* rawDataIn = reinterpret_cast<const __m512i*>(inData);\r
-  const auto k_expPerm = _mm512_set_epi32(15, 14, 13, 12,  7,  6,  5,  4,\r
-                                           5,  4,  3,  2,  3,  2,  1,  0);\r
-  auto expData = _mm512_permutexvar_epi32(k_expPerm, *rawDataIn);\r
-\r
-  /// Byte shuffle to get all bits for each sample into 16b chunks\r
-  /// Due to previous permute to get chunks of bytes into each lane, there is\r
-  /// a different shuffle offset in each lane\r
-  const __m512i k_byteShuffleMask = _mm512_set_epi64(0x0F0E0D0C0B0A0908, 0x0706050403020100,\r
-                                                     0x090A080907080607, 0x0506040503040203,\r
-                                                     0x0809070806070506, 0x0405030402030102,\r
-                                                     0x0708060705060405, 0x0304020301020001);\r
-  expData = _mm512_shuffle_epi8(expData, k_byteShuffleMask);\r
-\r
-  /// Logical shift left to set sign bit\r
-  const __m512i k_slBits = _mm512_set_epi64(0x0007000600050004, 0x0003000200010000,\r
-                                            0x0007000600050004, 0x0003000200010000,\r
-                                            0x0007000600050004, 0x0003000200010000,\r
-                                            0x0007000600050004, 0x0003000200010000);\r
-  expData = _mm512_sllv_epi16(expData, k_slBits);\r
-\r
-  /// Mask to zero unwanted bits\r
-  const __m512i k_expMask = _mm512_set1_epi16(0xFF80);\r
-  return _mm512_and_epi64(expData, k_expMask);\r
-}\r
-\r
-\r
-/// Unpack compressed 10 bit data in network byte order\r
-/// See https://soco.intel.com/docs/DOC-2665619\r
-__m512i\r
-networkByteUnpack10b(const uint8_t* inData)\r
-{\r
-  /// Align chunks of compressed bytes into lanes to allow for expansion\r
-  const __m512i* rawDataIn = reinterpret_cast<const __m512i*>(inData);\r
-  const auto k_expPerm = _mm512_set_epi32(15, 14, 13, 12,  8,  7,  6,  5,\r
-                                           5,  4,  3,  2,  3,  2,  1,  0);\r
-  auto expData = _mm512_permutexvar_epi32(k_expPerm, *rawDataIn);\r
-\r
-  /// Byte shuffle to get all bits for each sample into 16b chunks\r
-  /// Due to previous permute to get chunks of bytes into each lane, lanes\r
-  /// 0 and 2 happen to be aligned, but lane 1 is offset by 2 bytes\r
-  const __m512i k_byteShuffleMask = _mm512_set_epi64(0x0809070806070506, 0x0304020301020001,\r
-                                                     0x0809070806070506, 0x0304020301020001,\r
-                                                     0x0A0B090A08090708, 0x0506040503040203,\r
-                                                     0x0809070806070506, 0x0304020301020001);\r
-  expData = _mm512_shuffle_epi8(expData, k_byteShuffleMask);\r
-\r
-  /// Logical shift left to set sign bit\r
-  const __m512i k_slBits = _mm512_set_epi64(0x0006000400020000, 0x0006000400020000,\r
-                                            0x0006000400020000, 0x0006000400020000,\r
-                                            0x0006000400020000, 0x0006000400020000,\r
-                                            0x0006000400020000, 0x0006000400020000);\r
-  expData = _mm512_sllv_epi16(expData, k_slBits);\r
-\r
-  /// Mask to zero unwanted bits\r
-  const __m512i k_expMask = _mm512_set1_epi16(0xFFC0);\r
-  return _mm512_and_epi64(expData, k_expMask);\r
-}\r
-\r
-\r
-/// Unpack compressed 12 bit data in network byte order\r
-/// See https://soco.intel.com/docs/DOC-2665619\r
-__m512i\r
-networkByteUnpack12b(const uint8_t* inData)\r
-{\r
-  /// Align chunks of compressed bytes into lanes to allow for expansion\r
-  const __m512i* rawDataIn = reinterpret_cast<const __m512i*>(inData);\r
-  const auto k_expPerm = _mm512_set_epi32(15, 14, 13, 12,  9,  8,  7,  6,\r
-                                           6,  5,  4,  3,  3,  2,  1,  0);\r
-  auto expData = _mm512_permutexvar_epi32(k_expPerm, *rawDataIn);\r
-\r
-  /// Byte shuffle to get all bits for each sample into 16b chunks\r
-  /// For 12b mantissa all lanes post-permute are aligned and require same shuffle offset\r
-  const __m512i k_byteShuffleMask = _mm512_set_epi64(0x0A0B090A07080607, 0x0405030401020001,\r
-                                                     0x0A0B090A07080607, 0x0405030401020001,\r
-                                                     0x0A0B090A07080607, 0x0405030401020001,\r
-                                                     0x0A0B090A07080607, 0x0405030401020001);\r
-  expData = _mm512_shuffle_epi8(expData, k_byteShuffleMask);\r
-\r
-  /// Logical shift left to set sign bit\r
-  const __m512i k_slBits = _mm512_set_epi64(0x0004000000040000, 0x0004000000040000,\r
-                                            0x0004000000040000, 0x0004000000040000,\r
-                                            0x0004000000040000, 0x0004000000040000,\r
-                                            0x0004000000040000, 0x0004000000040000);\r
-  expData = _mm512_sllv_epi16(expData, k_slBits);\r
-\r
-  /// Mask to zero unwanted bits\r
-  const __m512i k_expMask = _mm512_set1_epi16(0xFFF0);\r
-  return _mm512_and_epi64(expData, k_expMask);\r
-}\r
-\r
-\r
-/// 8 bit compression\r
-void\r
-BlockFloatCompander::BlockFloatCompress_8b_AVX512(const ExpandedData& dataIn, CompressedData* dataOut)\r
-{\r
-  /// Compute exponent and store for later use\r
-  int8_t storedExp[BlockFloatCompander::k_numRB] = {};\r
-  computeExponent(dataIn, storedExp);\r
-\r
-  /// Shift 1RB by corresponding exponent and write exponent and data to output\r
-#pragma unroll(BlockFloatCompander::k_numRB)\r
-  for (int n = 0; n < BlockFloatCompander::k_numRB; ++n)\r
-  {\r
-    const __m512i* rawDataIn = reinterpret_cast<const __m512i*>(dataIn.dataExpanded + n * BlockFloatCompander::k_numREReal);\r
-    auto compData = _mm512_srai_epi16(*rawDataIn, storedExp[n]);\r
-    auto thisRBExpAddr = n * (BlockFloatCompander::k_numREReal + 1);\r
-    /// Store exponent first\r
-    dataOut->dataCompressed[thisRBExpAddr] = storedExp[n];\r
-    /// Store compressed RB\r
-    constexpr uint32_t k_rbMask = 0x00FFFFFF; // Write mask for 1RB (24 values)\r
-    _mm256_mask_storeu_epi8(dataOut->dataCompressed + thisRBExpAddr + 1, k_rbMask, _mm512_cvtepi16_epi8(compData));\r
-  }\r
-}\r
-\r
-\r
-/// 9 bit compression\r
-void\r
-BlockFloatCompander::BlockFloatCompress_9b_AVX512(const ExpandedData& dataIn, CompressedData* dataOut)\r
-{\r
-  /// Compute exponent and store for later use\r
-  int8_t storedExp[BlockFloatCompander::k_numRB] = {};\r
-  computeExponent(dataIn, storedExp);\r
-\r
-  /// Shift 1RB by corresponding exponent and write exponent and data to output\r
-  /// Output data is packed exponent first followed by corresponding compressed RB\r
-#pragma unroll(BlockFloatCompander::k_numRB)\r
-  for (int n = 0; n < BlockFloatCompander::k_numRB; ++n)\r
-  {\r
-    /// Apply exponent shift\r
-    const __m512i* rawDataIn = reinterpret_cast<const __m512i*>(dataIn.dataExpanded + n * BlockFloatCompander::k_numREReal);\r
-    auto compData = _mm512_srai_epi16(*rawDataIn, storedExp[n]);\r
-\r
-    /// Pack compressed data network byte order\r
-    auto compDataBytePacked = networkBytePack9b(compData);\r
-\r
-    /// Store exponent first\r
-    constexpr int k_totNumBytesPerRB = 28;\r
-    auto thisRBExpAddr = n * k_totNumBytesPerRB;\r
-    dataOut->dataCompressed[thisRBExpAddr] = storedExp[n];\r
-\r
-    /// Now have 1 RB worth of bytes separated into 3 chunks (1 per lane)\r
-    /// Use three offset stores to join\r
-    constexpr uint16_t k_RbWriteMask = 0x01FF;\r
-    constexpr int k_numDataBytesPerLane = 9;\r
-    _mm_mask_storeu_epi8(dataOut->dataCompressed + thisRBExpAddr + 1, k_RbWriteMask, _mm512_extracti64x2_epi64(compDataBytePacked, 0));\r
-    _mm_mask_storeu_epi8(dataOut->dataCompressed + thisRBExpAddr + 1 + k_numDataBytesPerLane, k_RbWriteMask, _mm512_extracti64x2_epi64(compDataBytePacked, 1));\r
-    _mm_mask_storeu_epi8(dataOut->dataCompressed + thisRBExpAddr + 1 + (2 * k_numDataBytesPerLane), k_RbWriteMask, _mm512_extracti64x2_epi64(compDataBytePacked, 2));\r
-  }\r
-}\r
-\r
-\r
-/// 10 bit compression\r
-void\r
-BlockFloatCompander::BlockFloatCompress_10b_AVX512(const ExpandedData& dataIn, CompressedData* dataOut)\r
-{\r
-  /// Compute exponent and store for later use\r
-  int8_t storedExp[BlockFloatCompander::k_numRB] = {};\r
-  computeExponent(dataIn, storedExp);\r
-\r
-  /// Shift 1RB by corresponding exponent and write exponent and data to output\r
-  /// Output data is packed exponent first followed by corresponding compressed RB\r
-#pragma unroll(BlockFloatCompander::k_numRB)\r
-  for (int n = 0; n < BlockFloatCompander::k_numRB; ++n)\r
-  {\r
-    /// Apply exponent shift\r
-    const __m512i* rawDataIn = reinterpret_cast<const __m512i*>(dataIn.dataExpanded + n * BlockFloatCompander::k_numREReal);\r
-    auto compData = _mm512_srai_epi16(*rawDataIn, storedExp[n]);\r
-\r
-    /// Pack compressed data network byte order\r
-    auto compDataBytePacked = networkBytePack10b(compData);\r
-\r
-    /// Store exponent first\r
-    constexpr int k_totNumBytesPerRB = 31;\r
-    auto thisRBExpAddr = n * k_totNumBytesPerRB;\r
-    dataOut->dataCompressed[thisRBExpAddr] = storedExp[n];\r
-\r
-    /// Now have 1 RB worth of bytes separated into 3 chunks (1 per lane)\r
-    /// Use three offset stores to join\r
-    constexpr uint16_t k_RbWriteMask = 0x03FF;\r
-    constexpr int k_numDataBytesPerLane = 10;\r
-    _mm_mask_storeu_epi8(dataOut->dataCompressed + thisRBExpAddr + 1, k_RbWriteMask, _mm512_extracti64x2_epi64(compDataBytePacked, 0));\r
-    _mm_mask_storeu_epi8(dataOut->dataCompressed + thisRBExpAddr + 1 + k_numDataBytesPerLane, k_RbWriteMask, _mm512_extracti64x2_epi64(compDataBytePacked, 1));\r
-    _mm_mask_storeu_epi8(dataOut->dataCompressed + thisRBExpAddr + 1 + (2 * k_numDataBytesPerLane), k_RbWriteMask, _mm512_extracti64x2_epi64(compDataBytePacked, 2));\r
-  }\r
-}\r
-\r
-\r
-/// 12 bit compression\r
-void\r
-BlockFloatCompander::BlockFloatCompress_12b_AVX512(const ExpandedData& dataIn, CompressedData* dataOut)\r
-{\r
-  /// Compute exponent and store for later use\r
-  int8_t storedExp[BlockFloatCompander::k_numRB] = {};\r
-  computeExponent(dataIn, storedExp);\r
-\r
-  /// Shift 1RB by corresponding exponent and write exponent and data to output\r
-  /// Output data is packed exponent first followed by corresponding compressed RB\r
-#pragma unroll(BlockFloatCompander::k_numRB)\r
-  for (int n = 0; n < BlockFloatCompander::k_numRB; ++n)\r
-  {\r
-    /// Apply exponent shift\r
-    const __m512i* rawDataIn = reinterpret_cast<const __m512i*>(dataIn.dataExpanded + n * BlockFloatCompander::k_numREReal);\r
-    auto compData = _mm512_srai_epi16(*rawDataIn, storedExp[n]);\r
-\r
-    /// Pack compressed data network byte order\r
-    auto compDataBytePacked = networkBytePack12b(compData);\r
-\r
-    /// Store exponent first\r
-    constexpr int k_totNumBytesPerRB = 37;\r
-    auto thisRBExpAddr = n * k_totNumBytesPerRB;\r
-    dataOut->dataCompressed[thisRBExpAddr] = storedExp[n];\r
-\r
-    /// Now have 1 RB worth of bytes separated into 3 chunks (1 per lane)\r
-    /// Use three offset stores to join\r
-    constexpr uint16_t k_RbWriteMask = 0x0FFF;\r
-    constexpr int k_numDataBytesPerLane = 12;\r
-    _mm_mask_storeu_epi8(dataOut->dataCompressed + thisRBExpAddr + 1, k_RbWriteMask, _mm512_extracti64x2_epi64(compDataBytePacked, 0));\r
-    _mm_mask_storeu_epi8(dataOut->dataCompressed + thisRBExpAddr + 1 + k_numDataBytesPerLane, k_RbWriteMask, _mm512_extracti64x2_epi64(compDataBytePacked, 1));\r
-    _mm_mask_storeu_epi8(dataOut->dataCompressed + thisRBExpAddr + 1 + (2 * k_numDataBytesPerLane), k_RbWriteMask, _mm512_extracti64x2_epi64(compDataBytePacked, 2));\r
-  }\r
-}\r
-\r
-\r
-/// 8 bit expansion\r
-void\r
-BlockFloatCompander::BlockFloatExpand_8b_AVX512(const CompressedData& dataIn, ExpandedData* dataOut)\r
-{\r
-#pragma unroll(BlockFloatCompander::k_numRB)\r
-  for (int n = 0; n < BlockFloatCompander::k_numRB; ++n)\r
-  {\r
-    /// Expand 1RB of data\r
-    auto expAddr = n * (BlockFloatCompander::k_numREReal + 1);\r
-    const __m256i* rawDataIn = reinterpret_cast<const __m256i*>(dataIn.dataCompressed + expAddr + 1);\r
-    const auto compData16 = _mm512_cvtepi8_epi16(*rawDataIn);\r
-    const auto expData = _mm512_slli_epi16(compData16, *(dataIn.dataCompressed + expAddr));\r
-    /// Write expanded data to output\r
-    constexpr uint8_t k_rbMask64 = 0b00111111; // 64b write mask for 1RB (24 int16 values)\r
-    _mm512_mask_storeu_epi64(dataOut->dataExpanded + n * BlockFloatCompander::k_numREReal, k_rbMask64, expData);\r
-  }\r
-}\r
-\r
-\r
-/// 9 bit expansion\r
-void\r
-BlockFloatCompander::BlockFloatExpand_9b_AVX512(const CompressedData& dataIn, ExpandedData* dataOut)\r
-{\r
-#pragma unroll(BlockFloatCompander::k_numRB)\r
-  for (int n = 0; n < BlockFloatCompander::k_numRB; ++n)\r
-  {\r
-    constexpr int k_totNumBytesPerRB = 28;\r
-    auto expAddr = n * k_totNumBytesPerRB;\r
-\r
-    /// Unpack network order packed data\r
-    auto expData = networkByteUnpack9b(dataIn.dataCompressed + expAddr + 1);\r
-\r
-    /// Apply exponent scaling (by appropriate arithmetic shift right)\r
-    constexpr int k_maxExpShift = 7;\r
-    expData = _mm512_srai_epi16(expData, k_maxExpShift - *(dataIn.dataCompressed + expAddr));\r
-\r
-    /// Write expanded data to output\r
-    static constexpr uint32_t k_WriteMask = 0x00FFFFFF;\r
-    _mm512_mask_storeu_epi16(dataOut->dataExpanded + n * BlockFloatCompander::k_numREReal, k_WriteMask, expData);\r
-  }\r
-}\r
-\r
-\r
-/// 10 bit expansion\r
-void\r
-BlockFloatCompander::BlockFloatExpand_10b_AVX512(const CompressedData& dataIn, ExpandedData* dataOut)\r
-{\r
-#pragma unroll(BlockFloatCompander::k_numRB)\r
-  for (int n = 0; n < BlockFloatCompander::k_numRB; ++n)\r
-  {\r
-    constexpr int k_totNumBytesPerRB = 31;\r
-    auto expAddr = n * k_totNumBytesPerRB;\r
-\r
-    /// Unpack network order packed data\r
-    auto expData = networkByteUnpack10b(dataIn.dataCompressed + expAddr + 1);\r
-\r
-    /// Apply exponent scaling (by appropriate arithmetic shift right)\r
-    constexpr int k_maxExpShift = 6;\r
-    expData = _mm512_srai_epi16(expData, k_maxExpShift - *(dataIn.dataCompressed + expAddr));\r
-\r
-    /// Write expanded data to output\r
-    static constexpr uint32_t k_WriteMask = 0x00FFFFFF;\r
-    _mm512_mask_storeu_epi16(dataOut->dataExpanded + n * BlockFloatCompander::k_numREReal, k_WriteMask, expData);\r
-  }\r
-}\r
-\r
-\r
-/// 12 bit expansion\r
-void\r
-BlockFloatCompander::BlockFloatExpand_12b_AVX512(const CompressedData& dataIn, ExpandedData* dataOut)\r
-{\r
-#pragma unroll(BlockFloatCompander::k_numRB)\r
-  for (int n = 0; n < BlockFloatCompander::k_numRB; ++n)\r
-  {\r
-    constexpr int k_totNumBytesPerRB = 37;\r
-    auto expAddr = n * k_totNumBytesPerRB;\r
-\r
-    /// Unpack network order packed data\r
-    auto expData = networkByteUnpack12b(dataIn.dataCompressed + expAddr + 1);\r
-\r
-    /// Apply exponent scaling (by appropriate arithmetic shift right)\r
-    constexpr int k_maxExpShift = 4;\r
-    expData = _mm512_srai_epi16(expData, k_maxExpShift - *(dataIn.dataCompressed + expAddr));\r
-\r
-    /// Write expanded data to output\r
-    static constexpr uint32_t k_WriteMask = 0x00FFFFFF;\r
-    _mm512_mask_storeu_epi16(dataOut->dataExpanded + n * BlockFloatCompander::k_numREReal, k_WriteMask, expData);\r
-  }\r
-}\r
-\r
-\r
-/// Reference compression\r
-void\r
-BlockFloatCompander::BlockFloatCompress_Basic(const ExpandedData& dataIn, CompressedData* dataOut)\r
-{\r
-  int dataOutIdx = 0;\r
-  int16_t iqMask = (int16_t)((1 << dataIn.iqWidth) - 1);\r
-  int byteShiftUnits = dataIn.iqWidth - 8;\r
-\r
-  for (int rb = 0; rb < BlockFloatCompander::k_numRB; ++rb)\r
-  {\r
-    /// Find max abs value for this RB\r
-    int16_t maxAbs = 0;\r
-    for (int re = 0; re < BlockFloatCompander::k_numREReal; ++re)\r
-    {\r
-      auto dataIdx = rb * BlockFloatCompander::k_numREReal + re;\r
-      auto dataAbs = saturateAbs(dataIn.dataExpanded[dataIdx]);\r
-      maxAbs = std::max(maxAbs, dataAbs);\r
-    }\r
-\r
-    // Find exponent and insert into byte stream\r
-    auto thisExp = (uint8_t)(std::max(0,(16 - dataIn.iqWidth + 1 - __lzcnt16(maxAbs))));\r
-    dataOut->dataCompressed[dataOutIdx++] = thisExp;\r
-\r
-    /// ARS data by exponent and pack bytes in Network order\r
-    /// This uses a sliding buffer where one or more bytes are\r
-    /// extracted after the insertion of each compressed sample\r
-    static constexpr int k_byteMask = 0xFF;\r
-    int byteShiftVal = -8;\r
-    int byteBuffer = { 0 };\r
-    for (int re = 0; re < BlockFloatCompander::k_numREReal; ++re)\r
-    {\r
-      auto dataIdxIn = rb * BlockFloatCompander::k_numREReal + re;\r
-      auto thisRE = dataIn.dataExpanded[dataIdxIn] >> thisExp;\r
-      byteBuffer = (byteBuffer << dataIn.iqWidth) + (int)(thisRE & iqMask);\r
-\r
-      byteShiftVal += (8 + byteShiftUnits);\r
-      while (byteShiftVal >= 0)\r
-      {\r
-        auto thisByte = (uint8_t)((byteBuffer >> byteShiftVal) & k_byteMask);\r
-        dataOut->dataCompressed[dataOutIdx++] = thisByte;\r
-        byteShiftVal -= 8;\r
-      }\r
-    }\r
-  }\r
-  dataOut->iqWidth = dataIn.iqWidth;\r
-}\r
-\r
-/// Reference expansion\r
-void\r
-BlockFloatCompander::BlockFloatExpand_Basic(const CompressedData& dataIn, ExpandedData* dataOut)\r
-{\r
-  uint32_t iqMask = (uint32_t)(UINT_MAX - ((1 << (32 - dataIn.iqWidth)) - 1));\r
-  uint32_t byteBuffer = { 0 };\r
-  int numBytesPerRB = (3 * dataIn.iqWidth) + 1;\r
-  int bitPointer = 0;\r
-  int dataIdxOut = 0;\r
-\r
-  for (int rb = 0; rb < BlockFloatCompander::k_numRB; ++rb)\r
-  {\r
-    auto expIdx = rb * numBytesPerRB;\r
-    auto signExtShift = 32 - dataIn.iqWidth - dataIn.dataCompressed[expIdx];\r
-\r
-    for (int b = 0; b < numBytesPerRB - 1; ++b)\r
-    {\r
-      auto dataIdxIn = (expIdx + 1) + b;\r
-      auto thisByte = (uint16_t)dataIn.dataCompressed[dataIdxIn];\r
-      byteBuffer = (uint32_t)((byteBuffer << 8) + thisByte);\r
-      bitPointer += 8;\r
-      while (bitPointer >= dataIn.iqWidth)\r
-      {\r
-        /// byteBuffer currently has enough data in it to extract a sample\r
-        /// Shift left first to set sign bit at MSB, then shift right to\r
-        /// sign extend down to iqWidth. Finally recast to int16.\r
-        int32_t thisSample32 = (int32_t)((byteBuffer << (32 - bitPointer)) & iqMask);\r
-        int16_t thisSample = (int16_t)(thisSample32 >> signExtShift);\r
-        bitPointer -= dataIn.iqWidth;\r
-        dataOut->dataExpanded[dataIdxOut++] = thisSample;\r
-      }\r
-    }\r
-  }\r
-}\r
-\r
-/// Reference compression\r
-void\r
-BlockFloatCompanderBFW::BlockFloatCompress_Basic(const BlockFloatCompanderBFW::ExpandedData& dataIn, BlockFloatCompanderBFW::CompressedData* dataOut)\r
-{\r
-  int dataOutIdx = 0;\r
-  int16_t iqMask = (int16_t)((1 << dataIn.iqWidth) - 1);\r
-  int byteShiftUnits = dataIn.iqWidth - 8;\r
-\r
-  for (int rb = 0; rb < BlockFloatCompanderBFW::k_numRB; ++rb)\r
-  {\r
-    /// Find max abs value for this RB\r
-    int16_t maxAbs = 0;\r
-    for (int re = 0; re < BlockFloatCompanderBFW::k_numREReal; ++re)\r
-    {\r
-      auto dataIdx = rb * BlockFloatCompanderBFW::k_numREReal + re;\r
-      auto dataAbs = saturateAbs(dataIn.dataExpanded[dataIdx]);\r
-      maxAbs = std::max(maxAbs, dataAbs);\r
-    }\r
-\r
-    // Find exponent and insert into byte stream\r
-    auto thisExp = (uint8_t)(std::max(0,(16 - dataIn.iqWidth + 1 - __lzcnt16(maxAbs))));\r
-    dataOut->dataCompressed[dataOutIdx++] = thisExp;\r
-\r
-    /// ARS data by exponent and pack bytes in Network order\r
-    /// This uses a sliding buffer where one or more bytes are\r
-    /// extracted after the insertion of each compressed sample\r
-    static constexpr int k_byteMask = 0xFF;\r
-    int byteShiftVal = -8;\r
-    int byteBuffer = { 0 };\r
-    for (int re = 0; re < BlockFloatCompanderBFW::k_numREReal; ++re)\r
-    {\r
-      auto dataIdxIn = rb * BlockFloatCompanderBFW::k_numREReal + re;\r
-      auto thisRE = dataIn.dataExpanded[dataIdxIn] >> thisExp;\r
-      byteBuffer = (byteBuffer << dataIn.iqWidth) + (int)(thisRE & iqMask);\r
-\r
-      byteShiftVal += (8 + byteShiftUnits);\r
-      while (byteShiftVal >= 0)\r
-      {\r
-        auto thisByte = (uint8_t)((byteBuffer >> byteShiftVal) & k_byteMask);\r
-        dataOut->dataCompressed[dataOutIdx++] = thisByte;\r
-        byteShiftVal -= 8;\r
-      }\r
-    }\r
-  }\r
-  dataOut->iqWidth = dataIn.iqWidth;\r
-}\r
-\r
-/// Reference expansion\r
-void\r
-BlockFloatCompanderBFW::BlockFloatExpand_Basic(const BlockFloatCompanderBFW::CompressedData& dataIn, BlockFloatCompanderBFW::ExpandedData* dataOut)\r
-{\r
-  uint32_t iqMask = (uint32_t)(UINT_MAX - ((1 << (32 - dataIn.iqWidth)) - 1));\r
-  uint32_t byteBuffer = { 0 };\r
-  int numBytesPerRB = (3 * dataIn.iqWidth) + 1;\r
-  int bitPointer = 0;\r
-  int dataIdxOut = 0;\r
-\r
-  for (int rb = 0; rb < BlockFloatCompanderBFW::k_numRB; ++rb)\r
-  {\r
-    auto expIdx = rb * numBytesPerRB;\r
-    auto signExtShift = 32 - dataIn.iqWidth - dataIn.dataCompressed[expIdx];\r
-\r
-    for (int b = 0; b < numBytesPerRB - 1; ++b)\r
-    {\r
-      auto dataIdxIn = (expIdx + 1) + b;\r
-      auto thisByte = (uint16_t)dataIn.dataCompressed[dataIdxIn];\r
-      byteBuffer = (uint32_t)((byteBuffer << 8) + thisByte);\r
-      bitPointer += 8;\r
-      while (bitPointer >= dataIn.iqWidth)\r
-      {\r
-        /// byteBuffer currently has enough data in it to extract a sample\r
-        /// Shift left first to set sign bit at MSB, then shift right to\r
-        /// sign extend down to iqWidth. Finally recast to int16.\r
-        int32_t thisSample32 = (int32_t)((byteBuffer << (32 - bitPointer)) & iqMask);\r
-        int16_t thisSample = (int16_t)(thisSample32 >> signExtShift);\r
-        bitPointer -= dataIn.iqWidth;\r
-        dataOut->dataExpanded[dataIdxOut++] = thisSample;\r
-      }\r
-    }\r
-  }\r
-}\r
-\r
-#define RB_NUM_ROUNDUP(rb) \\r
-    (BlockFloatCompander::k_numRB * ((rb + BlockFloatCompander::k_numRB - 1) / BlockFloatCompander::k_numRB))\r
-\r
-\r
-/** callback function type for Symbol packet */\r
-typedef void (*xran_bfp_compress_fn)(const BlockFloatCompander::ExpandedData& dataIn,\r
-                                     BlockFloatCompander::CompressedData* dataOut);\r
-\r
-int32_t\r
-xranlib_compress_avx512(const struct xranlib_compress_request *request,\r
-                        struct xranlib_compress_response *response)\r
-{\r
-    BlockFloatCompander::ExpandedData expandedDataInput;\r
-    BlockFloatCompander::CompressedData compressedDataOut;\r
-    xran_bfp_compress_fn com_fn = NULL;\r
-    int16_t numRBs = request->numRBs;\r
-    int16_t len = 0;\r
-\r
-    switch (request->iqWidth){\r
-        case 8:\r
-            expandedDataInput.iqWidth = 8;\r
-            com_fn = BlockFloatCompander::BlockFloatCompress_8b_AVX512;\r
-            break;\r
-        case 9:\r
-            expandedDataInput.iqWidth = 9;\r
-            com_fn = BlockFloatCompander::BlockFloatCompress_9b_AVX512;\r
-            break;\r
-        case 10:\r
-            expandedDataInput.iqWidth = 10;\r
-            com_fn = BlockFloatCompander::BlockFloatCompress_10b_AVX512;\r
-            break;\r
-        case 12:\r
-            expandedDataInput.iqWidth = 12;\r
-            com_fn = BlockFloatCompander::BlockFloatCompress_12b_AVX512;\r
-            break;\r
-        default:\r
-            expandedDataInput.iqWidth = request->iqWidth;\r
-            com_fn = BlockFloatCompander::BlockFloatCompress_Basic;\r
-            break;\r
-    }\r
-\r
-    for (int16_t block_idx = 0;\r
-        block_idx < RB_NUM_ROUNDUP(numRBs)/BlockFloatCompander::k_numRB /*+ 1*/; /*  16 RBs at time */\r
-        block_idx++) {\r
-\r
-        expandedDataInput.dataExpanded =\r
-            &request->data_in[block_idx*BlockFloatCompander::k_numSampsExpanded];\r
-        compressedDataOut.dataCompressed =\r
-            (uint8_t*)&response->data_out[len];\r
-\r
-        com_fn(expandedDataInput, &compressedDataOut);\r
-        len  += ((3 * expandedDataInput.iqWidth) + 1) * std::min((int16_t)BlockFloatCompander::k_numRB,(int16_t)numRBs);\r
-    }\r
-\r
-    response->len =  ((3 * expandedDataInput.iqWidth) + 1) * numRBs;\r
-\r
-    return 0;\r
-}\r
-\r
-/** callback function type for Symbol packet */\r
-typedef void (*xran_bfp_compress_bfw_fn)(const BlockFloatCompanderBFW::ExpandedData& dataIn, BlockFloatCompanderBFW::CompressedData* dataOut);\r
-\r
-int32_t\r
-xranlib_compress_avx512_bfw(const struct xranlib_compress_request *request,\r
-                        struct xranlib_compress_response *response)\r
-{\r
-    BlockFloatCompanderBFW::ExpandedData expandedDataInput;\r
-    BlockFloatCompanderBFW::CompressedData compressedDataKern;\r
-    xran_bfp_compress_bfw_fn com_fn = NULL;\r
-\r
-#if 0\r
-    for (int m = 0; m < BlockFloatCompander::k_numRB; ++m){\r
-        for (int n = 0; n < BlockFloatCompander::k_numREReal; ++n){\r
-            expandedDataInput.dataExpanded[m*BlockFloatCompander::k_numREReal+n] =\r
-                request->data_in[m*BlockFloatCompander::k_numREReal+n];\r
-        }\r
-    }\r
-#endif\r
-\r
-    expandedDataInput.dataExpanded = request->data_in;\r
-    compressedDataKern.dataCompressed = (uint8_t*)response->data_out;\r
-\r
-    com_fn = BlockFloatCompanderBFW::BlockFloatCompress_Basic;\r
-    switch (request->iqWidth){\r
-        case 8:\r
-            expandedDataInput.iqWidth = 8;\r
-            break;\r
-        case 9:\r
-            expandedDataInput.iqWidth = 9;\r
-            //com_fn = BlockFloatCompanderBFW::BlockFloatExpand_9b_AVX512\r
-            break;\r
-        case 10:\r
-            expandedDataInput.iqWidth = 10;\r
-            break;\r
-        case 12:\r
-            expandedDataInput.iqWidth = 12;\r
-            break;\r
-        default:\r
-            printf("bfwIqWidth is not supported %d\n", request->iqWidth);\r
-            return -1;\r
-            break;\r
-    }\r
-\r
-    com_fn(expandedDataInput, &compressedDataKern);\r
-    response->len = ((BlockFloatCompanderBFW::k_numRE/16*4*expandedDataInput.iqWidth)+1)*BlockFloatCompanderBFW::k_numRB;\r
-\r
-    return 0;\r
-}\r
-\r
-/** callback function type for Symbol packet */\r
-typedef void (*xran_bfp_decompress_fn)(const BlockFloatCompander::CompressedData& dataIn, BlockFloatCompander::ExpandedData* dataOut);\r
-\r
-\r
-int32_t\r
-xranlib_decompress_avx512(const struct xranlib_decompress_request *request,\r
-    struct xranlib_decompress_response *response)\r
-{\r
-\r
-    BlockFloatCompander::CompressedData compressedDataInput;\r
-    BlockFloatCompander::ExpandedData expandedDataOut;\r
-\r
-    xran_bfp_decompress_fn decom_fn = NULL;\r
-    int16_t numRBs = request->numRBs;\r
-    int16_t len = 0;\r
-\r
-    switch (request->iqWidth){\r
-        case 8:\r
-            compressedDataInput.iqWidth = 8;\r
-            decom_fn = BlockFloatCompander::BlockFloatExpand_8b_AVX512;\r
-            break;\r
-        case 9:\r
-            compressedDataInput.iqWidth = 9;\r
-            decom_fn = BlockFloatCompander::BlockFloatExpand_9b_AVX512;\r
-            break;\r
-        case 10:\r
-            compressedDataInput.iqWidth = 10;\r
-            decom_fn = BlockFloatCompander::BlockFloatExpand_10b_AVX512;\r
-            break;\r
-        case 12:\r
-            compressedDataInput.iqWidth = 12;\r
-            decom_fn = BlockFloatCompander::BlockFloatExpand_12b_AVX512;\r
-            break;\r
-        default:\r
-            compressedDataInput.iqWidth = request->iqWidth;\r
-            decom_fn = BlockFloatCompander::BlockFloatExpand_Basic;\r
-            break;\r
-    }\r
-\r
-    for (int16_t block_idx = 0;\r
-        block_idx < RB_NUM_ROUNDUP(numRBs)/BlockFloatCompander::k_numRB;\r
-        block_idx++) {\r
-\r
-        compressedDataInput.dataCompressed = (uint8_t*)&request->data_in[block_idx*(((3 * compressedDataInput.iqWidth ) + 1) * BlockFloatCompander::k_numRB)];\r
-        expandedDataOut.dataExpanded = &response->data_out[len];\r
-\r
-        decom_fn(compressedDataInput, &expandedDataOut);\r
-        len  += std::min((int16_t)BlockFloatCompander::k_numSampsExpanded, (int16_t)(numRBs*BlockFloatCompander::k_numREReal));\r
-    }\r
-\r
-    response->len = numRBs * BlockFloatCompander::k_numREReal* sizeof(int16_t);\r
-\r
-    return 0;\r
-}\r
+/******************************************************************************
+*
+*   Copyright (c) 2020 Intel.
+*
+*   Licensed under the Apache License, Version 2.0 (the "License");
+*   you may not use this file except in compliance with the License.
+*   You may obtain a copy of the License at
+*
+*       http://www.apache.org/licenses/LICENSE-2.0
+*
+*   Unless required by applicable law or agreed to in writing, software
+*   distributed under the License is distributed on an "AS IS" BASIS,
+*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+*   See the License for the specific language governing permissions and
+*   limitations under the License.
+*
+*******************************************************************************/
+
+/**
+ * @brief xRAN BFP compression/decompression U-plane implementation and interface functions
+ *
+ * @file xran_compression.cpp
+ * @ingroup group_source_xran
+ * @author Intel Corporation
+ **/
+
+#include "xran_compression.hpp"
+#include "xran_compression.h"
+#include "xran_mod_compression.h"
+#include "xran_fh_o_du.h"
+#include <complex>
+#include <algorithm>
+#include <immintrin.h>
+#include <limits.h>
+#include <cstring>
+
+using namespace BlockFloatCompander;
+
+/** callback function type for Symbol packet */
+typedef void (*xran_bfp_compress_fn)(const BlockFloatCompander::ExpandedData& dataIn,
+                                     BlockFloatCompander::CompressedData* dataOut);
+
+/** callback function type for Symbol packet */
+typedef void (*xran_bfp_decompress_fn)(const BlockFloatCompander::CompressedData& dataIn, BlockFloatCompander::ExpandedData* dataOut);
+
+int32_t
+xranlib_compress(const struct xranlib_compress_request *request,
+                        struct xranlib_compress_response *response)
+  {
+    if (request->compMethod == XRAN_COMPMETHOD_MODULATION)
+    {
+        struct xranlib_5gnr_mod_compression_request mod_request;
+        struct xranlib_5gnr_mod_compression_response mod_response;
+        mod_request.data_in = request->data_in;
+        mod_request.unit = request->ScaleFactor;
+        mod_request.modulation = (enum xran_modulation_order)(request->iqWidth * 2);
+        mod_request.num_symbols = request->numRBs * XRAN_NUM_OF_SC_PER_RB;
+        mod_request.re_mask = request->reMask;
+        mod_response.data_out = response->data_out;
+        response->len = (request->numRBs * XRAN_NUM_OF_SC_PER_RB * request->iqWidth * 2) >> 3;
+
+        return xranlib_5gnr_mod_compression(&mod_request, &mod_response);
+  }
+    else{
+        if(_may_i_use_cpu_feature(_FEATURE_AVX512IFMA52)) {
+            return xranlib_compress_avxsnc(request,response);
+        } else {
+            return xranlib_compress_avx512(request,response);
+    }
+  }
+  }
+
+int32_t
+xranlib_decompress(const struct xranlib_decompress_request *request,
+    struct xranlib_decompress_response *response)
+  {
+    if (request->compMethod == XRAN_COMPMETHOD_MODULATION)
+    {
+        struct xranlib_5gnr_mod_decompression_request mod_request;
+        struct xranlib_5gnr_mod_decompression_response mod_response;
+        mod_request.data_in = request->data_in;
+        mod_request.unit = request->ScaleFactor;
+        mod_request.modulation = (enum xran_modulation_order)(request->iqWidth * 2);
+        mod_request.num_symbols = request->numRBs * XRAN_NUM_OF_SC_PER_RB;
+        mod_request.re_mask = request->reMask;
+        mod_response.data_out = response->data_out;
+        response->len = request->numRBs * XRAN_NUM_OF_SC_PER_RB * 4;
+
+        return xranlib_5gnr_mod_decompression(&mod_request, &mod_response);
+      }
+    else{
+        if(_may_i_use_cpu_feature(_FEATURE_AVX512IFMA52)) {
+            return xranlib_decompress_avxsnc(request,response);
+        } else {
+            return xranlib_decompress_avx512(request,response);
+    }
+  }
+  }
+
+int32_t
+xranlib_compress_bfw(const struct xranlib_compress_request *request,
+                        struct xranlib_compress_response *response)
+    {
+    if(_may_i_use_cpu_feature(_FEATURE_AVX512IFMA52)) {
+        return xranlib_compress_avxsnc_bfw(request,response);
+    } else {
+        return xranlib_compress_avx512_bfw(request,response);
+    }
+  }
+
+int32_t
+xranlib_decompress_bfw(const struct xranlib_decompress_request *request,
+    struct xranlib_decompress_response *response)
+  {
+    if(_may_i_use_cpu_feature(_FEATURE_AVX512IFMA52)) {
+        return xranlib_decompress_avxsnc_bfw(request,response);
+    } else {
+        return xranlib_decompress_avx512_bfw(request,response);
+  }
+}
+
+int32_t
+xranlib_compress_avx512(const struct xranlib_compress_request *request,
+                        struct xranlib_compress_response *response)
+{
+    BlockFloatCompander::ExpandedData expandedDataInput;
+    BlockFloatCompander::CompressedData compressedDataOut;
+    xran_bfp_compress_fn com_fn = NULL;
+    uint16_t totalRBs = request->numRBs;
+    uint16_t remRBs   = totalRBs;
+    int16_t len = 0;
+    int16_t block_idx_bytes = 0;
+
+    switch (request->iqWidth) {
+        case 8:
+        case 9:
+        case 10:
+        case 12:
+            com_fn = BlockFloatCompander::BFPCompressUserPlaneAvx512;
+            break;
+        default:
+            com_fn = BlockFloatCompander::BFPCompressRef;
+            break;
+    }
+
+    expandedDataInput.iqWidth = request->iqWidth;
+    expandedDataInput.numDataElements =  24;
+
+    while (remRBs){
+        expandedDataInput.dataExpanded   = &request->data_in[block_idx_bytes];
+        compressedDataOut.dataCompressed = (uint8_t*)&response->data_out[len];
+        if(remRBs >= 16){
+            expandedDataInput.numBlocks = 16;
+            com_fn(expandedDataInput, &compressedDataOut);
+            len  += ((3 * expandedDataInput.iqWidth) + 1) * std::min((int16_t)BlockFloatCompander::k_maxNumBlocks,(int16_t)16);
+            block_idx_bytes += 16*expandedDataInput.numDataElements;
+            remRBs -= 16;
+        }else if(remRBs >= 4){
+            expandedDataInput.numBlocks = 4;
+            com_fn(expandedDataInput, &compressedDataOut);
+            len  += ((3 * expandedDataInput.iqWidth) + 1) * std::min((int16_t)BlockFloatCompander::k_maxNumBlocks,(int16_t)4);
+            block_idx_bytes +=4*expandedDataInput.numDataElements;
+            remRBs -=4;
+        }else if (remRBs >= 1){
+            expandedDataInput.numBlocks = 1;
+            com_fn(expandedDataInput, &compressedDataOut);
+            len  += ((3 * expandedDataInput.iqWidth) + 1) * std::min((int16_t)BlockFloatCompander::k_maxNumBlocks,(int16_t)1);
+            block_idx_bytes +=1*expandedDataInput.numDataElements;
+            remRBs = remRBs - 1;
+        }
+    }
+
+    response->len =  ((3 * expandedDataInput.iqWidth) + 1) * totalRBs;
+
+    return 0;
+}
+
+int32_t
+xranlib_decompress_avx512(const struct xranlib_decompress_request *request,
+    struct xranlib_decompress_response *response)
+{
+    BlockFloatCompander::CompressedData compressedDataInput;
+    BlockFloatCompander::ExpandedData expandedDataOut;
+
+    xran_bfp_decompress_fn decom_fn = NULL;
+    uint16_t totalRBs = request->numRBs;
+    uint16_t remRBs   = totalRBs;
+    int16_t len = 0;
+    int16_t block_idx_bytes = 0;
+
+    switch (request->iqWidth) {
+    case 8:
+    case 9:
+    case 10:
+    case 12:
+        decom_fn = BlockFloatCompander::BFPExpandUserPlaneAvx512;
+        break;
+    default:
+        decom_fn = BlockFloatCompander::BFPExpandRef;
+        break;
+    }
+
+    compressedDataInput.iqWidth         =  request->iqWidth;
+    compressedDataInput.numDataElements =  24;
+
+    while(remRBs) {
+        compressedDataInput.dataCompressed = (uint8_t*)&request->data_in[block_idx_bytes];
+        expandedDataOut.dataExpanded       = &response->data_out[len];
+        if(remRBs >= 16){
+            compressedDataInput.numBlocks = 16;
+            decom_fn(compressedDataInput, &expandedDataOut);
+            len  += 16*compressedDataInput.numDataElements;
+            block_idx_bytes  += ((3 * compressedDataInput.iqWidth) + 1) * std::min((int16_t)BlockFloatCompander::k_maxNumBlocks,(int16_t)16);
+            remRBs -= 16;
+        }else if(remRBs >= 4){
+            compressedDataInput.numBlocks = 4;
+            decom_fn(compressedDataInput, &expandedDataOut);
+            len  += 4*compressedDataInput.numDataElements;
+            block_idx_bytes  += ((3 * compressedDataInput.iqWidth) + 1) * std::min((int16_t)BlockFloatCompander::k_maxNumBlocks,(int16_t)4);
+            remRBs -=4;
+        }else if (remRBs >= 1){
+            compressedDataInput.numBlocks = 1;
+            decom_fn(compressedDataInput, &expandedDataOut);
+            len  += 1*compressedDataInput.numDataElements;
+            block_idx_bytes  += ((3 * compressedDataInput.iqWidth) + 1) * std::min((int16_t)BlockFloatCompander::k_maxNumBlocks,(int16_t)1);
+            remRBs = remRBs - 1;
+        }
+    }
+
+    response->len = totalRBs * compressedDataInput.numDataElements * sizeof(int16_t);
+
+    return 0;
+}
+
+int32_t
+xranlib_compress_avx512_bfw(const struct xranlib_compress_request *request,
+                        struct xranlib_compress_response *response)
+{
+    BlockFloatCompander::ExpandedData expandedDataInput;
+    BlockFloatCompander::CompressedData compressedDataOut;
+    xran_bfp_compress_fn com_fn = NULL;
+
+    if (request->numRBs != 1){
+        printf("Unsupported numRBs %d\n", request->numRBs);
+        return -1;
+    }
+
+    switch (request->iqWidth) {
+        case 8:
+        case 9:
+        case 10:
+        case 12:
+        switch (request->numDataElements) {
+            case 16:
+                com_fn = BlockFloatCompander::BFPCompressCtrlPlane8Avx512;
+                break;
+            case 32:
+                com_fn = BlockFloatCompander::BFPCompressCtrlPlane16Avx512;
+                break;
+            case 64:
+                com_fn = BlockFloatCompander::BFPCompressCtrlPlane32Avx512;
+                break;
+            case 128:
+                com_fn = BlockFloatCompander::BFPCompressCtrlPlane64Avx512;
+                break;
+            case 24:
+            default:
+                printf("Unsupported numDataElements %d\n", request->numDataElements);
+                return -1;
+                break;
+        }
+        break;
+    default:
+        printf("Unsupported iqWidth %d\n", request->iqWidth);
+        return -1;
+        break;
+    }
+
+    expandedDataInput.iqWidth         = request->iqWidth;
+    expandedDataInput.numDataElements = request->numDataElements;
+    expandedDataInput.numBlocks       = 1;
+    expandedDataInput.dataExpanded    = &request->data_in[0];
+    compressedDataOut.dataCompressed  = (uint8_t*)&response->data_out[0];
+
+    com_fn(expandedDataInput, &compressedDataOut);
+
+    response->len =  (((expandedDataInput.numDataElements  * expandedDataInput.iqWidth) >> 3) + 1)
+                            * request->numRBs;
+
+    return 0;
+}
+
+int32_t
+xranlib_decompress_avx512_bfw(const struct xranlib_decompress_request *request,
+                        struct xranlib_decompress_response *response)
+{
+    BlockFloatCompander::CompressedData compressedDataInput;
+    BlockFloatCompander::ExpandedData expandedDataOut;
+    xran_bfp_decompress_fn decom_fn = NULL;
+
+    if (request->numRBs != 1){
+        printf("Unsupported numRBs %d\n", request->numRBs);
+        return -1;
+    }
+
+    switch (request->iqWidth) {
+        case 8:
+        case 9:
+        case 10:
+        case 12:
+        switch (request->numDataElements) {
+            case 16:
+                decom_fn = BlockFloatCompander::BFPExpandCtrlPlane8Avx512;
+                break;
+            case 32:
+                decom_fn = BlockFloatCompander::BFPExpandCtrlPlane16Avx512;
+                break;
+            case 64:
+                decom_fn = BlockFloatCompander::BFPExpandCtrlPlane32Avx512;
+                break;
+            case 128:
+                decom_fn = BlockFloatCompander::BFPExpandCtrlPlane64Avx512;
+                break;
+            case 24:
+            default:
+                printf("Unsupported numDataElements %d\n", request->numDataElements);
+                return -1;
+                break;
+        }
+        break;
+    default:
+        printf("Unsupported iqWidth %d\n", request->iqWidth);
+        return -1;
+        break;
+    }
+
+    compressedDataInput.iqWidth         = request->iqWidth;
+    compressedDataInput.numDataElements = request->numDataElements;
+    compressedDataInput.numBlocks       = 1;
+    compressedDataInput.dataCompressed  = (uint8_t*)&request->data_in[0];
+    expandedDataOut.dataExpanded        = &response->data_out[0];
+
+    decom_fn(compressedDataInput, &expandedDataOut);
+
+    response->len = request->numRBs * compressedDataInput.numDataElements * sizeof(int16_t);
+
+    return 0;
+}
+