nr_rate_matching.c 19.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.1  (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.openairinterface.org/?page_id=698
 *
 * 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.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

/* file: nr_rate_matching.c
   purpose: Procedures for rate matching/interleaving for NR LDPC
   author: hongzhi.wang@tcl.com
*/

#include "PHY/defs_gNB.h"
#include "PHY/defs_nr_UE.h"
29
#include "PHY/CODING/coding_defs.h"
30 31 32 33 34 35 36 37 38 39 40

//#define RM_DEBUG 1

uint8_t index_k0[2][4] = {{0,17,33,56},{0,13,25,43}};

void nr_interleaving_ldpc(uint32_t E, uint8_t Qm, uint8_t *e,uint8_t *f)
{
  uint32_t EQm;

  EQm = E/Qm;
  memset(f,0,E*sizeof(uint8_t));
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
  uint8_t *e0,*e1,*e2,*e3,*e4,*e5,*e6,*e7;
  uint8_t *fp;
#if 0 //def __AVX2__
  __m256i tmp0,tmp1,tmp2,tmp0b,tmp1b,tmp3,tmp4,tmp5;
  __m256i *e0_256,*e1_256,*e2_256,*e3_256,*e4_256,*e5_256,*e6_256,*e7_256;

  __m256i *f_256=(__m256i *)f;

  uint8_t *fp2;
  switch(Qm) {
  case 2:
    e0=e;
    e1=e0+EQm;
    e0_256=(__m256i *)e0;
    e1_256=(__m256i *)e1;
    for (int k=0,j=0;j<EQm>>5;j++,k+=2) {
      f_256[k]   = _mm256_unpacklo_epi8(e0_256[j],e1_256[j]);
      f_256[k+1] = _mm256_unpackhi_epi8(e0_256[j],e1_256[j]); 
    }
    break;
  case 4:
    e0=e;
    e1=e0+EQm;
    e2=e1+EQm;
    e3=e2+EQm;
    e0_256=(__m256i *)e0;
    e1_256=(__m256i *)e1;
    e2_256=(__m256i *)e2;
    e3_256=(__m256i *)e3;
    for (int k=0,j=0;j<EQm>>5;j++,k+=4) {
      tmp0   = _mm256_unpacklo_epi8(e0_256[j],e1_256[j]); // e0(i) e1(i) e0(i+1) e1(i+1) .... e0(i+15) e1(i+15)
      tmp1   = _mm256_unpacklo_epi8(e2_256[j],e3_256[j]); // e2(i) e3(i) e2(i+1) e3(i+1) .... e2(i+15) e3(i+15)
      f_256[k]   = _mm256_unpacklo_epi8(tmp0,tmp1);   // e0(i) e1(i) e2(i) e3(i) ... e0(i+7) e1(i+7) e2(i+7) e3(i+7)
      f_256[k+1] = _mm256_unpackhi_epi8(tmp0,tmp1);   // e0(i+8) e1(i+8) e2(i+8) e3(i+8) ... e0(i+15) e1(i+15) e2(i+15) e3(i+15)
      tmp0   = _mm256_unpackhi_epi8(e0_256[j],e1_256[j]); // e0(i+16) e1(i+16) e0(i+17) e1(i+17) .... e0(i+31) e1(i+31)
      tmp1   = _mm256_unpackhi_epi8(e2_256[j],e3_256[j]); // e2(i+16) e3(i+16) e2(i+17) e3(i+17) .... e2(i+31) e3(i+31)
      f_256[k+2] = _mm256_unpacklo_epi8(tmp0,tmp1);
      f_256[k+3] = _mm256_unpackhi_epi8(tmp0,tmp1); 
    }
    break;
  case 6:
    e0=e;
    e1=e0+EQm;
    e2=e1+EQm;
    e3=e2+EQm;
    e4=e3+EQm;
    e5=e4+EQm;
    e0_256=(__m256i *)e0;
    e1_256=(__m256i *)e1;
    e2_256=(__m256i *)e2;
    e3_256=(__m256i *)e3;
    e4_256=(__m256i *)e4;
    e5_256=(__m256i *)e5;

    for (int j=0,k=0;j<EQm>>5;j++,k+=192) {
      fp  = f+k;
      fp2 = fp+96;

      tmp0   = _mm256_unpacklo_epi8(e0_256[j],e1_256[j]); // e0(i) e1(i) e0(i+1) e1(i+1) .... e0(i+15) e1(i+15)
      tmp1   = _mm256_unpacklo_epi8(e2_256[j],e3_256[j]); // e2(i) e3(i) e2(i+1) e3(i+1) .... e2(i+15) e3(i+15)
      tmp0b  = _mm256_unpacklo_epi16(tmp0,tmp1); // e0(i) e1(i) e2(i) e3(i) ... e0(i+7) e1(i+7) e2(i+7) e3(i+7)
      tmp1b  = _mm256_unpackhi_epi16(tmp0,tmp1); // e0(i+8) e1(i+8) e2(i+8) e3(i+8) ... e0(i+15) e1(i+15) e2(i+15) e3(i+15)
      tmp0   = _mm256_unpacklo_epi8(e4_256[j],e5_256[j]); // e4(i) e5(i) e4(i+1) e5(i+1) .... e4(i+15) e5(i+15)
      *((uint32_t*)fp)      = _mm256_extract_epi32(tmp0b,0);
      *((uint16_t*)(fp+4))  = _mm256_extract_epi16(tmp0,0);
      *((uint32_t*)(fp+6))  = _mm256_extract_epi32(tmp0b,1);
      *((uint16_t*)(fp+10)) = _mm256_extract_epi16(tmp0,1);
      *((uint32_t*)(fp+12)) = _mm256_extract_epi32(tmp0b,2);
      *((uint16_t*)(fp+16)) = _mm256_extract_epi16(tmp0,2);
      *((uint32_t*)(fp+18)) = _mm256_extract_epi32(tmp0b,3);
      *((uint16_t*)(fp+22)) = _mm256_extract_epi16(tmp0,3);
      *((uint32_t*)(fp+24)) = _mm256_extract_epi32(tmp0b,4);
      *((uint16_t*)(fp+26)) = _mm256_extract_epi16(tmp0,4);
      *((uint32_t*)(fp+30)) = _mm256_extract_epi32(tmp0b,5);
      *((uint16_t*)(fp+34)) = _mm256_extract_epi16(tmp0,5);
      *((uint32_t*)(fp+36)) = _mm256_extract_epi32(tmp0,6);
      *((uint16_t*)(fp+40)) = _mm256_extract_epi16(tmp0,6);
      *((uint32_t*)(fp+42)) = _mm256_extract_epi32(tmp0b,7);
      *((uint16_t*)(fp+46)) = _mm256_extract_epi16(tmp0,7);

      *((uint32_t*)(fp+48)) = _mm256_extract_epi32(tmp1b,0);
      *((uint16_t*)(fp+52)) = _mm256_extract_epi16(tmp0,8);
      *((uint32_t*)(fp+56)) = _mm256_extract_epi32(tmp1b,1);
      *((uint16_t*)(fp+60)) = _mm256_extract_epi16(tmp0,9);
      *((uint32_t*)(fp+62)) = _mm256_extract_epi32(tmp1b,2);
      *((uint16_t*)(fp+66)) = _mm256_extract_epi16(tmp0,10);
      *((uint32_t*)(fp+68)) = _mm256_extract_epi32(tmp1b,3);
      *((uint16_t*)(fp+72)) = _mm256_extract_epi16(tmp0,11);
      *((uint32_t*)(fp+74)) = _mm256_extract_epi32(tmp1b,4);
      *((uint16_t*)(fp+76)) = _mm256_extract_epi16(tmp0,12);
      *((uint32_t*)(fp+80)) = _mm256_extract_epi32(tmp1b,5);
      *((uint16_t*)(fp+82)) = _mm256_extract_epi16(tmp0,13);
      *((uint32_t*)(fp+86)) = _mm256_extract_epi32(tmp1b,6);
      *((uint16_t*)(fp+90)) = _mm256_extract_epi16(tmp0,14);
      *((uint32_t*)(fp+92)) = _mm256_extract_epi32(tmp1b,7);
      *((uint16_t*)(fp+94)) = _mm256_extract_epi16(tmp0,15);

      tmp0   = _mm256_unpackhi_epi8(e0_256[j],e1_256[j]); // e0(i+16) e1(i+16) e0(i+17) e1(i+17) .... e0(i+31) e1(i+31)
      tmp1   = _mm256_unpackhi_epi8(e2_256[j],e3_256[j]); // e2(i+16) e3(i+16) e2(i+17) e3(i+17) .... e2(i+31) e3(i+31)
      tmp0b  = _mm256_unpacklo_epi16(tmp0,tmp1); // e0(i+16) e1(i+16) e2(i+16) e3(i+16) ... e0(i+23) e1(i+23) e2(i+23) e3(i+23)
      tmp1b  = _mm256_unpackhi_epi16(tmp0,tmp1); // e0(i+24) e1(i+24) e2(i+24) e3(i+24) ... e0(i+31) e1(i+31) e2(i+31) e3(i+31)
      tmp0   = _mm256_unpackhi_epi8(e4_256[j],e5_256[j]); // e4(i+16) e5(i+16) e4(i+17) e5(i+17) .... e4(i+31) e5(i+31)
      *((uint32_t*)fp2)      = _mm256_extract_epi32(tmp0b,0);
      *((uint16_t*)(fp2+4))  = _mm256_extract_epi16(tmp0,0);
      *((uint32_t*)(fp2+6))  = _mm256_extract_epi32(tmp0b,1);
      *((uint16_t*)(fp2+10)) = _mm256_extract_epi16(tmp0,1);
      *((uint32_t*)(fp2+12)) = _mm256_extract_epi32(tmp0b,2);
      *((uint16_t*)(fp2+16)) = _mm256_extract_epi16(tmp0,2);
      *((uint32_t*)(fp2+18)) = _mm256_extract_epi32(tmp0b,3);
      *((uint16_t*)(fp2+22)) = _mm256_extract_epi16(tmp0,3);
      *((uint32_t*)(fp2+24)) = _mm256_extract_epi32(tmp0b,4);
      *((uint16_t*)(fp2+26)) = _mm256_extract_epi16(tmp0,4);
      *((uint32_t*)(fp2+30)) = _mm256_extract_epi32(tmp0b,5);
      *((uint16_t*)(fp2+34)) = _mm256_extract_epi16(tmp0,5);
      *((uint32_t*)(fp2+36)) = _mm256_extract_epi32(tmp0,6);
      *((uint16_t*)(fp2+40)) = _mm256_extract_epi16(tmp0,6);
      *((uint32_t*)(fp2+42)) = _mm256_extract_epi32(tmp0b,7);
      *((uint16_t*)(fp2+46)) = _mm256_extract_epi16(tmp0,7);

      *((uint32_t*)(fp2+48)) = _mm256_extract_epi32(tmp1b,0);
      *((uint16_t*)(fp2+52)) = _mm256_extract_epi16(tmp0,8);
      *((uint32_t*)(fp2+56)) = _mm256_extract_epi32(tmp1b,1);
      *((uint16_t*)(fp2+60)) = _mm256_extract_epi16(tmp0,9);
      *((uint32_t*)(fp2+62)) = _mm256_extract_epi32(tmp1b,2);
      *((uint16_t*)(fp2+66)) = _mm256_extract_epi16(tmp0,10);
      *((uint32_t*)(fp2+68)) = _mm256_extract_epi32(tmp1b,3);
      *((uint16_t*)(fp2+72)) = _mm256_extract_epi16(tmp0,11);
      *((uint32_t*)(fp2+74)) = _mm256_extract_epi32(tmp1b,4);
      *((uint16_t*)(fp2+76)) = _mm256_extract_epi16(tmp0,12);
      *((uint32_t*)(fp2+80)) = _mm256_extract_epi32(tmp1b,5);
      *((uint16_t*)(fp2+82)) = _mm256_extract_epi16(tmp0,13);
      *((uint32_t*)(fp2+86)) = _mm256_extract_epi32(tmp1b,6);
      *((uint16_t*)(fp2+90)) = _mm256_extract_epi16(tmp0,14);
      *((uint32_t*)(fp2+92)) = _mm256_extract_epi32(tmp1b,7);
      *((uint16_t*)(fp2+94)) = _mm256_extract_epi16(tmp0,15);
    }
    break;
  case 8:
    e0=e;
    e1=e0+EQm;
    e2=e1+EQm;
    e3=e2+EQm;
    e4=e3+EQm;
    e5=e4+EQm;
    e6=e5+EQm;
    e7=e6+EQm;

    e0_256=(__m256i *)e0;
    e1_256=(__m256i *)e1;
    e2_256=(__m256i *)e2;
    e3_256=(__m256i *)e3;
    e4_256=(__m256i *)e4;
    e5_256=(__m256i *)e5;
    e6_256=(__m256i *)e6;
    e7_256=(__m256i *)e7;
    for (int k=0,j=0;j<EQm>>5;j++,k+=8) {
      tmp0   = _mm256_unpacklo_epi8(e0_256[j],e1_256[j]); // e0(i) e1(i) e0(i+1) e1(i+1) .... e0(i+15) e1(i+15)
      tmp1   = _mm256_unpacklo_epi8(e2_256[j],e3_256[j]); // e2(i) e3(i) e2(i+1) e3(i+1) .... e2(i+15) e3(i+15)
      tmp2   = _mm256_unpacklo_epi8(e4_256[j],e5_256[j]); // e4(i) e5(i) e4(i+1) e5(i+1) .... e4(i+15) e5(i+15)
      tmp3   = _mm256_unpacklo_epi8(e6_256[j],e7_256[j]); // e6(i) e7(i) e6(i+1) e7(i+1) .... e6(i+15) e7(i+15)
      tmp4   = _mm256_unpacklo_epi16(tmp0,tmp1);  // e0(i) e1(i) e2(i) e3(i) ... e0(i+7) e1(i+7) e2(i+7) e3(i+7)
      tmp5   = _mm256_unpacklo_epi16(tmp2,tmp3);  // e4(i) e5(i) e6(i) e7(i) ... e4(i+7) e5(i+7) e6(i+7) e7(i+7)
      f_256[k]   = _mm256_unpacklo_epi16(tmp4,tmp5);  // e0(i) e1(i) e2(i) e3(i) e4(i) e5(i) e6(i) e7(i)... e0(i+3) e1(i+3) e2(i+3) e3(i+3) e4(i+3) e5(i+3) e6(i+3) e7(i+3))
      f_256[k+1] = _mm256_unpackhi_epi16(tmp4,tmp5);  // e0(i+4) e1(i+4) e2(i+4) e3(i+4) e4(i+4) e5(i+4) e6(i+4) e7(i+4)... e0(i+7) e1(i+7) e2(i+7) e3(i+7) e4(i+7) e5(i+7) e6(i+7) e7(i+7))

      tmp4   = _mm256_unpackhi_epi16(tmp0,tmp1);  // e0(i+8) e1(i+8) e2(i+8) e3(i+8) ... e0(i+15) e1(i+15) e2(i+15) e3(i+15)
      tmp5   = _mm256_unpackhi_epi16(tmp2,tmp3);  // e4(i+8) e5(i+8) e6(i+8) e7(i+8) ... e4(i+15) e5(i+15) e6(i+15) e7(i+15)
      f_256[k+2]   = _mm256_unpacklo_epi16(tmp4,tmp5);  // e0(i+8) e1(i+8) e2(i+8) e3(i+8) e4(i+8) e5(i+8) e6(i+8) e7(i+8)... e0(i+11) e1(i+11) e2(i+11) e3(i+11) e4(i+11) e5(i+11) e6(i+11) e7(i+11))
      f_256[k+3] = _mm256_unpackhi_epi16(tmp4,tmp5);  // e0(i+12) e1(i+12) e2(i+12) e3(i+12) e4(i+12) e5(i+12) e6(i+12) e7(i+12)... e0(i+15) e1(i+15) e2(i+15) e3(i+15) e4(i+15) e5(i+15) e6(i+15) e7(i+15))

      tmp0   = _mm256_unpackhi_epi8(e0_256[j],e1_256[j]); // e0(i+16) e1(i+16) e0(i+17) e1(i+17) .... e0(i+31) e1(i+31)
      tmp1   = _mm256_unpackhi_epi8(e2_256[j],e3_256[j]); // e2(i+16) e3(i+16) e2(i+17) e3(i+17) .... e2(i+31) e3(i+31)
      tmp2   = _mm256_unpackhi_epi8(e4_256[j],e5_256[j]); // e4(i+16) e5(i+16) e4(i+17) e5(i+17) .... e4(i+31) e5(i+31)
      tmp3   = _mm256_unpackhi_epi8(e6_256[j],e7_256[j]); // e6(i+16) e7(i+16) e6(i+17) e7(i+17) .... e6(i+31) e7(i+31)
      tmp4   = _mm256_unpacklo_epi16(tmp0,tmp1);  // e0(i+!6) e1(i+16) e2(i+16) e3(i+16) ... e0(i+23) e1(i+23) e2(i+23) e3(i+23)
      tmp5   = _mm256_unpacklo_epi16(tmp2,tmp3);  // e4(i+16) e5(i+16) e6(i+16) e7(i+16) ... e4(i+23) e5(i+23) e6(i+23) e7(i+23)
      f_256[k+4] = _mm256_unpacklo_epi16(tmp4,tmp5);  // e0(i+16) e1(i+16) e2(i+16) e3(i+16) e4(i+16) e5(i+16) e6(i+16) e7(i+16)... e0(i+19) e1(i+19) e2(i+19) e3(i+19) e4(i+19) e5(i+19) e6(i+19) e7(i+19))
      f_256[k+5] = _mm256_unpackhi_epi16(tmp4,tmp5);  // e0(i+20) e1(i+20) e2(i+20) e3(i+20) e4(i+20) e5(i+20) e6(i+20) e7(i+20)... e0(i+23) e1(i+23) e2(i+23) e3(i+23) e4(i+23) e5(i+23) e6(i+23) e7(i+23))

      tmp4   = _mm256_unpackhi_epi16(tmp0,tmp1);  // e0(i+24) e1(i+24) e2(i+24) e3(i+24) ... e0(i+31) e1(i+31) e2(i+31) e3(i+31)
      tmp5   = _mm256_unpackhi_epi16(tmp2,tmp3);  // e4(i+24) e5(i+24) e6(i+24) e7(i+24) ... e4(i+31) e5(i+31) e6(i+31) e7(i+31)
      f_256[k+6] = _mm256_unpacklo_epi16(tmp4,tmp5);  // e0(i+24) e1(i+24) e2(i+24) e3(i+24) e4(i+24) e5(i+24) e6(i+24) e7(i+24)... e0(i+27) e1(i+27) e2(i+27) e3(i+27) e4(i+27) e5(i+27) e6(i+27) e7(i+27))
      f_256[k+7] = _mm256_unpackhi_epi16(tmp4,tmp5);  // e0(i+28) e1(i+28) e2(i+28) e3(i+28) e4(i+28) e5(i+28) e6(i+28) e7(i+28)... e0(i+31) e1(i+31) e2(i+31) e3(i+31) e4(i+31) e5(i+31) e6(i+31) e7(i+31))
    }
    break;
  default: AssertFatal(1==0,"Should be here!\n");
  }
228

229 230 231 232 233
#else
  //original unoptimized loops
    /*
    for (int j = 0; j< EQm; j++,j2+=2){
      for (int i = 0; i< Qm; i++){
234 235
		  f[(i+j*Qm)] = e[(i*EQm + j)];
	  }
236 237 238 239 240 241 242 243
    }
    */

  fp=f;
  switch (Qm) {
  case 2:
    e0=e;
    e1=e0+EQm;
244
    for (int j = 0, j2 = 0; j< EQm; j++,j2+=2){
245 246 247 248 249 250 251 252 253 254
      fp=&f[j2];
      fp[0] = e0[j];
      fp[1] = e1[j];
    }
    break;
  case 4:
    e0=e;
    e1=e0+EQm;
    e2=e1+EQm;
    e3=e2+EQm;
255
    for (int j = 0, j2 = 0; j< EQm; j++,j2+=4){
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
      fp=&f[j2];
      fp[0] = e0[j];
      fp[1] = e1[j];
      fp[2] = e2[j];
      fp[3] = e3[j];
    }
    break;
  case 6:
    e0=e;
    e1=e0+EQm;
    e2=e1+EQm;
    e3=e2+EQm;
    e4=e3+EQm;
    e5=e4+EQm;
    fp = f;
    for (int j = 0; j< EQm; j++){
      *fp++ = e0[j];
      *fp++ = e1[j];
      *fp++ = e2[j];
      *fp++ = e3[j];
      *fp++ = e4[j];
      *fp++ = e5[j];
    }
    break;
  case 8:
    e0=e;
    e1=e0+EQm;
    e2=e1+EQm;
    e3=e2+EQm;
    e4=e3+EQm;
    e5=e4+EQm;
    e6=e5+EQm;
    e7=e6+EQm;
289
    for (int j = 0, j2 = 0; j< EQm; j++,j2+=8){
290 291 292 293 294 295 296 297 298 299 300 301
      fp=&f[j2];
      fp[0] = e0[j];
      fp[1] = e1[j];
      fp[2] = e2[j];
      fp[3] = e3[j];
      fp[4] = e4[j];
      fp[5] = e5[j];
      fp[6] = e6[j];
      fp[7] = e7[j];
    }
    break;
  default: AssertFatal(1==0,"Should never be here!\n");
302
  }
303
#endif
304 305 306
}


307 308


Hongzhi Wang's avatar
Hongzhi Wang committed
309
void nr_deinterleaving_ldpc(uint32_t E, uint8_t Qm, int16_t *e,int16_t *f)
310 311
{

312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
  int16_t *e1,*e2,*e3,*e4,*e5,*e6,*e7;
  switch(Qm) {
  case 2:
    e1=e+(E/2);
    for (int j = 0,j2=0; j< E/2; j+=2,j2+=4){
      e[j]  = f[j2];
      e1[j] = f[j2+1];
      e[j+1]  = f[j2+2];
      e1[j+1] = f[j2+3];
    }
    break;
  case 4:
    e1=e+(E/4);
    e2=e1+(E/4);
    e3=e2+(E/4);
    for (int j = 0,j2=0; j< E/4; j++,j2+=4){
      e[j]  = f[j2];
      e1[j] = f[j2+1];
      e2[j] = f[j2+2];
      e3[j] = f[j2+3];
    }
    break;
  case 6:
    e1=e+(E/6);
    e2=e1+(E/6);
    e3=e2+(E/6);
    e4=e3+(E/6);
    e5=e4+(E/6);
    for (int j = 0,j2=0; j< E/6; j++,j2+=6){
      e[j]  = f[j2];
      e1[j] = f[j2+1];
      e2[j] = f[j2+2];
      e3[j] = f[j2+3];
      e4[j] = f[j2+4];
      e5[j] = f[j2+5];
    }
    break;
  case 8:
350 351 352 353 354 355 356
    e1=e+(E/8);
    e2=e1+(E/8);
    e3=e2+(E/8);
    e4=e3+(E/8);
    e5=e4+(E/8);
    e6=e5+(E/8);
    e7=e6+(E/8);
357 358 359 360 361 362 363 364 365 366 367 368 369 370
    for (int j = 0,j2=0; j< E/8; j++,j2+=8){
      e[j]  = f[j2];
      e1[j] = f[j2+1];
      e2[j] = f[j2+2];
      e3[j] = f[j2+3];
      e4[j] = f[j2+4];
      e5[j] = f[j2+5];
      e6[j] = f[j2+6];
      e7[j] = f[j2+7];
    }
    break;
  default:
    AssertFatal(1==0,"Should not get here : Qm %d\n",Qm);
    break;
371
  }
372

373 374 375
}


376 377 378 379 380 381 382
int nr_rate_matching_ldpc(uint8_t Ilbrm,
                          uint32_t Tbslbrm,
                          uint8_t BG,
                          uint16_t Z,
                          uint8_t *w,
                          uint8_t *e,
                          uint8_t C,
383 384
			  uint32_t F,
			  uint32_t Foffset,
385 386
                          uint8_t rvidx,
                          uint32_t E)
387
{
388
  uint32_t Ncb,ind,k=0,Nref,N;
389

390 391 392 393
  if (C==0) {
    printf("nr_rate_matching: invalid parameters (C %d\n",C);
    return -1;
  }
394 395 396 397 398

  //Bit selection
  N = (BG==1)?(66*Z):(50*Z);

  if (Ilbrm == 0)
399
      Ncb = N;
400 401 402 403 404
  else {
      Nref = 3*Tbslbrm/(2*C); //R_LBRM = 2/3
      Ncb = min(N, Nref);
  }

405
  ind = (index_k0[BG-1][rvidx]*Ncb/N)*Z;
406 407

#ifdef RM_DEBUG
408
  printf("nr_rate_matching_ldpc: E %d, F %d, Foffset %d, k0 %d, Ncb %d, rvidx %d\n", E, F, Foffset,ind, Ncb, rvidx);
409
#endif
410 411 412 413 414 415 416 417 418 419
  AssertFatal(Foffset <= E,
              "Foffset %d > E %d "
              "(Ilbrm %d, Tbslbrm %d, Z %d, BG %d, C %d)\n",
              Foffset, E,
              Ilbrm, Tbslbrm, Z, BG, C);
  AssertFatal(Foffset <= Ncb,
              "Foffset %d > Ncb %d "
              "(Ilbrm %d, Tbslbrm %d, Z %d, BG %d, C %d)\n",
              Foffset, Ncb,
              Ilbrm, Tbslbrm, Z, BG, C);
420

421
  if (ind >= Foffset && ind < (F+Foffset)) ind = F+Foffset;
422

423 424
  if (ind < Foffset) { // case where we have some bits before the filler and the rest after
    memcpy((void*)e,(void*)(w+ind),Foffset-ind);
425

426 427
    if (E + F <= Ncb-ind) { // E+F doesn't contain all coded bits
      memcpy((void*)(e+Foffset-ind),(void*)(w+Foffset+F),E-Foffset+ind);
428 429 430 431 432 433 434 435
      k=E;
    }
    else {
      memcpy((void*)(e+Foffset-ind),(void*)(w+Foffset+F),Ncb-Foffset-F);
      k=Ncb-F-ind;
    }
  }
  else {
436
    if (E <= Ncb-ind) { //E+F doesn't contain all coded bits
437
      memcpy((void*)(e),(void*)(w+ind),E);
438 439 440
      k=E;
    }
    else {
441 442
      memcpy((void*)(e),(void*)(w+ind),Ncb-ind);
      k=Ncb-ind;
443
    }
444 445
  }

446
  while(k<E) { // case where we do repetitions (low mcs)
447 448 449
    for (ind=0; (ind<Ncb)&&(k<E); ind++) {

#ifdef RM_DEBUG
450
      printf("RM_TX k%d Ind: %d (%d)\n",k,ind,w[ind]);
451 452
#endif

453
      if (w[ind] != NR_NULL) e[k++]=w[ind];
454
    }
455
  }
456 457


458
  return 0;
459 460 461
}

int nr_rate_matching_ldpc_rx(uint8_t Ilbrm,
462 463 464
                             uint32_t Tbslbrm,
                             uint8_t BG,
                             uint16_t Z,
465 466 467 468 469
                             int16_t *w,
                             int16_t *soft_input,
                             uint8_t C,
                             uint8_t rvidx,
                             uint8_t clear,
470 471 472
                             uint32_t E,
			     uint32_t F,
			     uint32_t Foffset)
473
{
474
  uint32_t Ncb,ind,k,Nref,N;
475 476 477 478 479

#ifdef RM_DEBUG
  int nulled=0;
#endif

480 481 482
  if (C==0) {
    printf("nr_rate_matching: invalid parameters (C %d\n",C);
    return -1;
483 484 485 486 487 488
  }

  //Bit selection
  N = (BG==1)?(66*Z):(50*Z);

  if (Ilbrm == 0)
489
      Ncb = N;
490 491 492 493 494 495
  else {
      Nref = (3*Tbslbrm/(2*C)); //R_LBRM = 2/3
      Ncb = min(N, Nref);
  }

  ind = (index_k0[BG-1][rvidx]*Ncb/N)*Z;
496 497
//  AssertFatal(Foffset <= E,"Foffset %d > E %d\n",Foffset,E); 
//  AssertFatal(Foffset <= Ncb,"Foffset %d > Ncb %d\n",Foffset,Ncb); 
498 499

#ifdef RM_DEBUG
500
  printf("nr_rate_matching_ldpc_rx: Clear %d, E %d, k0 %d, Ncb %d, rvidx %d\n", clear, E, ind, Ncb, rvidx);
501 502
#endif

503
  if (clear==1) memset(w,0,Ncb*sizeof(int16_t));
504 505 506

  k=0;

507 508
  if (ind < Foffset)
    for (; (ind<Foffset)&&(k<E); ind++) {
509
#ifdef RM_DEBUG
510
      printf("RM_RX k%d Ind %d(before filler): %d (%d)=>",k,ind,w[ind],soft_input[k]);
511
#endif
512
      w[ind]+=soft_input[k++];
513
#ifdef RM_DEBUG
514 515
      printf("%d\n",w[ind]);
#endif
516
    }
517
  if (ind >= Foffset && ind < Foffset+F) ind=Foffset+F;
518

519 520 521 522 523 524 525
  for (; (ind<Ncb)&&(k<E); ind++) {
#ifdef RM_DEBUG
    printf("RM_RX k%d Ind %d(after filler) %d (%d)=>",k,ind,w[ind],soft_input[k]);
#endif
      w[ind] += soft_input[k++];
#ifdef RM_DEBUG
      printf("%d\n",w[ind]);
526 527 528 529
#endif
  }

  while(k<E) {
530
    for (ind=0; (ind<Foffset)&&(k<E); ind++) {
531
#ifdef RM_DEBUG
532
      printf("RM_RX k%d Ind %d(before filler) %d(%d)=>",k,ind,w[ind],soft_input[k]);
533
#endif
534
      w[ind]+=soft_input[k++];
535
#ifdef RM_DEBUG
536 537 538 539 540 541 542 543 544 545
      printf("%d\n",w[ind]);
#endif
    }
    for (ind=Foffset+F; (ind<Ncb)&&(k<E); ind++) {
#ifdef RM_DEBUG
      printf("RM_RX (after filler) k%d Ind: %d (%d)(soft in %d)=>",k,ind,w[ind],soft_input[k]);
#endif
      w[ind] += soft_input[k++];
#ifdef RM_DEBUG
      printf("%d\n",w[ind]);
546 547 548
#endif
    }
  }
549

550

551
  return 0;
552
}
553