nr_dlsch_decoding.c 68.5 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 29 30 31 32 33 34 35 36 37
/*
 * 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.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.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 PHY/NR_UE_TRANSPORT/nr_dlsch_decoding.c
* \brief Top-level routines for decoding  Turbo-coded (DLSCH) transport channels from 36-212, V8.6 2009-03
* \author R. Knopp
* \date 2011
* \version 0.1
* \company Eurecom
* \email: knopp@eurecom.fr
* \note
* \warning
*/

#include "PHY/defs_nr_UE.h"
#include "PHY/phy_extern_nr_ue.h"
#include "PHY/CODING/coding_extern.h"
#include "PHY/CODING/coding_defs.h"
#include "PHY/NR_TRANSPORT/nr_transport_common_proto.h"
38
#include "PHY/NR_UE_TRANSPORT/nr_transport_proto_ue.h"
39
#include "PHY/NR_TRANSPORT/nr_dlsch.h"
40
#include "SCHED_NR_UE/defs.h"
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
#include "SIMULATION/TOOLS/sim.h"
#include "targets/RT/USER/nr-uesoftmodem.h"
#include "PHY/CODING/nrLDPC_decoder/nrLDPC_decoder.h"
#include "PHY/CODING/nrLDPC_decoder/nrLDPC_types.h"
//#define DEBUG_DLSCH_DECODING

#define OAI_LDPC_MAX_NUM_LLR 27000//26112 // NR_LDPC_NCOL_BG1*NR_LDPC_ZMAX

static int8_t llrProcBuf[OAI_LDPC_MAX_NUM_LLR] __attribute__ ((aligned(32)));

static uint64_t nb_total_decod =0;
static uint64_t nb_error_decod =0;

//extern double cpuf;

void free_nr_ue_dlsch(NR_UE_DLSCH_t *dlsch)
{

  int i,r;

  if (dlsch) {
    for (i=0; i<dlsch->Mdlharq; i++) {
      if (dlsch->harq_processes[i]) {
        if (dlsch->harq_processes[i]->b) {
          free16(dlsch->harq_processes[i]->b,MAX_DLSCH_PAYLOAD_BYTES);
          dlsch->harq_processes[i]->b = NULL;
        }

        for (r=0; r<MAX_NUM_NR_DLSCH_SEGMENTS; r++) {
70
          free16(dlsch->harq_processes[i]->c[r],1056);
71 72 73 74 75
          dlsch->harq_processes[i]->c[r] = NULL;
        }

        for (r=0; r<MAX_NUM_NR_DLSCH_SEGMENTS; r++)
          if (dlsch->harq_processes[i]->d[r]) {
76
            free16(dlsch->harq_processes[i]->d[r],(3*8448)*sizeof(short));
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
            dlsch->harq_processes[i]->d[r] = NULL;
          }

        free16(dlsch->harq_processes[i],sizeof(NR_DL_UE_HARQ_t));
        dlsch->harq_processes[i] = NULL;
      }
    }

    free16(dlsch,sizeof(NR_UE_DLSCH_t));
    dlsch = NULL;
  }
}

NR_UE_DLSCH_t *new_nr_ue_dlsch(uint8_t Kmimo,uint8_t Mdlharq,uint32_t Nsoft,uint8_t max_ldpc_iterations,uint8_t N_RB_DL, uint8_t abstraction_flag)
{

  NR_UE_DLSCH_t *dlsch;
  uint8_t exit_flag = 0,i,r;

  unsigned char bw_scaling =1;

  switch (N_RB_DL) {
  case 6:
    bw_scaling =16;
    break;

  case 25:
    bw_scaling =4;
    break;

  case 50:
    bw_scaling =2;
    break;

  default:
    bw_scaling =1;
    break;
  }

  dlsch = (NR_UE_DLSCH_t *)malloc16(sizeof(NR_UE_DLSCH_t));

  if (dlsch) {
    memset(dlsch,0,sizeof(NR_UE_DLSCH_t));
    dlsch->Kmimo = Kmimo;
    dlsch->Mdlharq = Mdlharq;
    dlsch->Nsoft = Nsoft;
    dlsch->max_ldpc_iterations = max_ldpc_iterations;

    for (i=0; i<Mdlharq; i++) {
      //      printf("new_ue_dlsch: Harq process %d\n",i);
      dlsch->harq_processes[i] = (NR_DL_UE_HARQ_t *)malloc16(sizeof(NR_DL_UE_HARQ_t));

      if (dlsch->harq_processes[i]) {
        memset(dlsch->harq_processes[i],0,sizeof(NR_DL_UE_HARQ_t));
        dlsch->harq_processes[i]->first_tx=1;
        dlsch->harq_processes[i]->b = (uint8_t*)malloc16(MAX_DLSCH_PAYLOAD_BYTES/bw_scaling);

        if (dlsch->harq_processes[i]->b)
          memset(dlsch->harq_processes[i]->b,0,MAX_DLSCH_PAYLOAD_BYTES/bw_scaling);
        else
          exit_flag=3;

        if (abstraction_flag == 0) {
          for (r=0; r<MAX_NUM_DLSCH_SEGMENTS/bw_scaling; r++) {
Hongzhi Wang's avatar
Hongzhi Wang committed
141
            dlsch->harq_processes[i]->c[r] = (uint8_t*)malloc16(1056);
142 143

            if (dlsch->harq_processes[i]->c[r])
Hongzhi Wang's avatar
Hongzhi Wang committed
144
              memset(dlsch->harq_processes[i]->c[r],0,1056);
145 146 147
            else
              exit_flag=2;

Hongzhi Wang's avatar
Hongzhi Wang committed
148
            dlsch->harq_processes[i]->d[r] = (short*)malloc16((3*8448)*sizeof(short));
149 150

            if (dlsch->harq_processes[i]->d[r])
Hongzhi Wang's avatar
Hongzhi Wang committed
151
              memset(dlsch->harq_processes[i]->d[r],0,(3*8448)*sizeof(short));
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
            else
              exit_flag=2;
          }
        }
      } else {
        exit_flag=1;
      }
    }

    if (exit_flag==0)
      return(dlsch);
  }

  printf("new_ue_dlsch with size %zu: exit_flag = %u\n",sizeof(NR_DL_UE_HARQ_t), exit_flag);
  free_nr_ue_dlsch(dlsch);

  return(NULL);
}

Hongzhi Wang's avatar
Hongzhi Wang committed
171
void nr_dlsch_unscrambling(int16_t* llr,
hongzhi wang's avatar
hongzhi wang committed
172
                         uint32_t size,
Hongzhi Wang's avatar
Hongzhi Wang committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
                         uint8_t q,
                         uint32_t Nid,
                         uint32_t n_RNTI) {

  uint8_t reset;
  uint32_t x1, x2, s=0;

  reset = 1;
  x2 = (n_RNTI<<15) + (q<<14) + Nid;

  for (int i=0; i<size; i++) {
    if ((i&0x1f)==0) {
      s = lte_gold_generic(&x1, &x2, reset);
      reset = 0;
    }
    if (((s>>(i&0x1f))&1)==1)
        llr[i] = -llr[i];
  }

}

194
uint32_t nr_dlsch_decoding(PHY_VARS_NR_UE *phy_vars_ue,
195 196 197 198 199
                         short *dlsch_llr,
                         NR_DL_FRAME_PARMS *frame_parms,
                         NR_UE_DLSCH_t *dlsch,
                         NR_DL_UE_HARQ_t *harq_process,
                         uint32_t frame,
200
						 uint16_t nb_symb_sch,
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
                         uint8_t nr_tti_rx,
                         uint8_t harq_pid,
                         uint8_t is_crnti,
                         uint8_t llr8_flag)
{

#if UE_TIMING_TRACE
  time_stats_t *dlsch_rate_unmatching_stats=&phy_vars_ue->dlsch_rate_unmatching_stats;
  time_stats_t *dlsch_turbo_decoding_stats=&phy_vars_ue->dlsch_turbo_decoding_stats;
  time_stats_t *dlsch_deinterleaving_stats=&phy_vars_ue->dlsch_deinterleaving_stats;
#endif
  uint32_t A,E;
  uint32_t G;
  uint32_t ret,offset;
  int32_t no_iteration_ldpc;
Hongzhi Wang's avatar
Hongzhi Wang committed
216
  //short dummy_w[MAX_NUM_DLSCH_SEGMENTS][3*(8448+64)];
217
  uint32_t r,r_offset=0,Kr=8424,Kr_bytes,K_bytes_F,err_flag=0;
218 219 220 221 222 223 224 225 226
  uint8_t crc_type;
  t_nrLDPC_dec_params decParams;
  t_nrLDPC_dec_params* p_decParams = &decParams;
  t_nrLDPC_time_stats procTime;
  t_nrLDPC_time_stats* p_procTime =&procTime ;
  int16_t z [68*384];
  int8_t l [68*384];
  //__m128i l;
  int16_t inv_d [68*384];
Hongzhi Wang's avatar
Hongzhi Wang committed
227
//  int16_t *p_invd =&inv_d;
228 229
  uint8_t kb, kc;
  uint8_t Ilbrm = 0;
230
  uint32_t Tbslbrm = 950984;
231 232
  uint16_t nb_rb = 30; //to update
  //uint16_t nb_symb_sch = 12;
233 234
  uint8_t nb_re_dmrs = 6;
  uint16_t length_dmrs = 1;
235 236

  uint32_t i,j;
Hongzhi Wang's avatar
Hongzhi Wang committed
237
//  uint32_t k;
238 239 240 241

  __m128i *pv = (__m128i*)&z;
  __m128i *pl = (__m128i*)&l;

242 243
  //NR_DL_UE_HARQ_t *harq_process = dlsch->harq_processes[0];

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
   if (!dlsch_llr) {
    printf("dlsch_decoding.c: NULL dlsch_llr pointer\n");
    return(dlsch->max_ldpc_iterations);
  }

  if (!harq_process) {
    printf("dlsch_decoding.c: NULL harq_process pointer\n");
    return(dlsch->max_ldpc_iterations);
  }

  if (!frame_parms) {
    printf("dlsch_decoding.c: NULL frame_parms pointer\n");
    return(dlsch->max_ldpc_iterations);
  }

259
  /*if (nr_tti_rx> (10*frame_parms->ttis_per_subframe-1)) {
260 261
    printf("dlsch_decoding.c: Illegal subframe index %d\n",nr_tti_rx);
    return(dlsch->max_ldpc_iterations);
262
  }*/
263

264
  /*if (harq_process->harq_ack.ack != 2) {
265 266
    LOG_D(PHY, "[UE %d] DLSCH @ SF%d : ACK bit is %d instead of DTX even before PDSCH is decoded!\n",
        phy_vars_ue->Mod_id, nr_tti_rx, harq_process->harq_ack.ack);
267
  }*/
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283

  //  nb_rb = dlsch->nb_rb;

  /*
  if (nb_rb > frame_parms->N_RB_DL) {
    printf("dlsch_decoding.c: Illegal nb_rb %d\n",nb_rb);
    return(max_ldpc_iterations);
    }*/

  /*harq_pid = dlsch->current_harq_pid[phy_vars_ue->current_thread_id[subframe]];
  if (harq_pid >= 8) {
    printf("dlsch_decoding.c: Illegal harq_pid %d\n",harq_pid);
    return(max_ldpc_iterations);
  }
  */

284 285
  nb_rb = harq_process->nb_rb;

286 287
  harq_process->trials[harq_process->round]++;

288 289
  harq_process->TBS = nr_compute_tbs(harq_process->mcs,nb_rb,nb_symb_sch,nb_re_dmrs,length_dmrs, harq_process->Nl);

290 291 292
  A = harq_process->TBS;
  ret = dlsch->max_ldpc_iterations;

293
  harq_process->G = nr_get_G(nb_rb, nb_symb_sch, nb_re_dmrs, length_dmrs, harq_process->Qm,harq_process->Nl);
294 295
  G = harq_process->G;

296
  LOG_I(PHY,"DLSCH Decoding, harq_pid %d TBS %d G %d mcs %d Nl %d nb_symb_sch %d nb_rb %d\n",harq_pid,A,G, harq_process->mcs, harq_process->Nl, nb_symb_sch,nb_rb);
297 298 299 300 301 302 303 304

  if (harq_process->round == 0) {
    // This is a new packet, so compute quantities regarding segmentation
    harq_process->B = A+24;
    nr_segmentation(NULL,
    	                    NULL,
    	                    harq_process->B,
    	                    &harq_process->C,
Hongzhi Wang's avatar
Hongzhi Wang committed
305 306
    	                    &harq_process->K,
     						&harq_process->Z,
307
    	                    &harq_process->F);
Hongzhi Wang's avatar
Hongzhi Wang committed
308
    						p_decParams->Z = harq_process->Z;
309

310 311 312 313 314
#ifdef DEBUG_DLSCH_DECODING
    printf("dlsch decoding nr segmentation Z %d\n", p_decParams->Z);
	if (!frame%100)
    	printf("K %d C %d Z %d nl %d \n", harq_process->K, harq_process->C, p_decParams->Z, harq_process->Nl);
#endif
315 316
  }

Hongzhi Wang's avatar
Hongzhi Wang committed
317
      kb = harq_process->K/harq_process->Z;
318 319 320 321 322 323 324 325 326 327 328
  	  if ( kb==22){
  		  p_decParams->BG = 1;
  		  p_decParams->R = 13;
  		  kc = 68;
  	  }
  	  else{
  		  p_decParams->BG = 2;
  		  p_decParams->R = 13;
  		  kc = 52;
  	  	  }

329
      p_decParams->numMaxIter = dlsch->max_ldpc_iterations;
330 331 332 333 334 335 336 337 338 339 340
      Kr = p_decParams->Z*kb;
      p_decParams->outMode= 0;


  err_flag = 0;
  r_offset = 0;

  unsigned char bw_scaling =1;

  switch (frame_parms->N_RB_DL) {

Hongzhi Wang's avatar
Hongzhi Wang committed
341
  case 106:
342 343 344 345 346 347 348 349
    bw_scaling =2;
    break;

  default:
    bw_scaling =1;
    break;
  }

Hongzhi Wang's avatar
Hongzhi Wang committed
350 351
  if (harq_process->C > MAX_NUM_NR_DLSCH_SEGMENTS/bw_scaling) {
    LOG_E(PHY,"Illegal harq_process->C %d > %d\n",harq_process->C,MAX_NUM_NR_DLSCH_SEGMENTS/bw_scaling);
352 353 354
    return((1+dlsch->max_ldpc_iterations));
  }
#ifdef DEBUG_DLSCH_DECODING
355
  printf("Segmentation: C %d, K %d\n",harq_process->C,harq_process->K);
356 357 358 359
#endif

  opp_enabled=1;

Hongzhi Wang's avatar
Hongzhi Wang committed
360
  Kr = harq_process->K;
361 362
  Kr_bytes = Kr>>3;

Hongzhi Wang's avatar
Hongzhi Wang committed
363 364
  K_bytes_F = Kr_bytes-(harq_process->F>>3);

365
  Tbslbrm = nr_compute_tbs(28,nb_rb,frame_parms->symbols_per_slot,0,0, harq_process->Nl);
366 367 368

  for (r=0; r<harq_process->C; r++) {

369
    //printf("start rx segment %d\n",r);
370

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
#if UE_TIMING_TRACE
    start_meas(dlsch_rate_unmatching_stats);
#endif

#ifdef DEBUG_DLSCH_DECODING
    LOG_D(PHY,"HARQ_PID %d Rate Matching Segment %d (coded bits %d,unpunctured/repeated bits %d, TBS %d, mod_order %d, nb_rb %d, Nl %d, rv %d, round %d)...\n",
          harq_pid,r, G,
          Kr*3,
          harq_process->TBS,
          harq_process->Qm,
          harq_process->nb_rb,
          harq_process->Nl,
          harq_process->rvidx,
          harq_process->round);
#endif

    if (nr_rate_matching_ldpc_rx(Ilbrm,
    		 	 	 	 	 	 Tbslbrm,
								 p_decParams->BG,
								 p_decParams->Z,
    							 G,
								 harq_process->w[r],
								 dlsch_llr+r_offset,
								 harq_process->C,
								 harq_process->rvidx,
								 (harq_process->round==0)?1:0,
								 harq_process->Qm,
								 harq_process->Nl,
								 r,
								 &E)==-1) {

#if UE_TIMING_TRACE
      stop_meas(dlsch_rate_unmatching_stats);
#endif
      LOG_E(PHY,"dlsch_decoding.c: Problem in rate_matching\n");
      return(dlsch->max_ldpc_iterations);
    } else
    {
#if UE_TIMING_TRACE
      stop_meas(dlsch_rate_unmatching_stats);
#endif
    }
    r_offset += E;

415 416 417
    //for (int i =0; i<16; i++)
    //    	printf("rx output ratematching w[%d]= %d r_offset %d\n", i,harq_process->w[r][i], r_offset);

418 419 420 421 422
#if UE_TIMING_TRACE
    start_meas(dlsch_deinterleaving_stats);
#endif
    nr_deinterleaving_ldpc(E,
    					   harq_process->Qm,
423
                           harq_process->d[r],
424 425
                           harq_process->w[r]);

426 427 428
    //for (int i =0; i<16; i++)
    //        	printf("rx output interleaving d[%d]= %d r_offset %d\n", i,harq_process->d[r][i], r_offset);

429 430 431 432
#if UE_TIMING_TRACE
    stop_meas(dlsch_deinterleaving_stats);
#endif
#ifdef DEBUG_DLSCH_DECODING
Hongzhi Wang's avatar
Hongzhi Wang committed
433

434 435
    if (r==0) {
              write_output("decoder_llr.m","decllr",dlsch_llr,G,1,0);
Hongzhi Wang's avatar
Hongzhi Wang committed
436
              write_output("decoder_in.m","dec",&harq_process->d[0][0],(3*8*Kr_bytes)+12,1,0);
437 438 439 440
    }

    printf("decoder input(segment %d) :",r);
    int i; for (i=0;i<(3*8*Kr_bytes)+12;i++)
Hongzhi Wang's avatar
Hongzhi Wang committed
441 442
      printf("%d : %d\n",i,harq_process->d[r][i]);
      printf("\n");
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
#endif


    //    printf("Clearing c, %p\n",harq_process->c[r]);
    memset(harq_process->c[r],0,Kr_bytes);

    //    printf("done\n");
    if (harq_process->C == 1)
      crc_type = CRC24_A;
    else
      crc_type = CRC24_B;

    if (err_flag == 0) {
/*
        LOG_I(PHY, "turbo algo Kr=%d cb_cnt=%d C=%d nbRB=%d crc_type %d TBSInput=%d TBSHarq=%d TBSplus24=%d mcs=%d Qm=%d RIV=%d round=%d maxIter %d\n",
                            Kr,r,harq_process->C,harq_process->nb_rb,crc_type,A,harq_process->TBS,
                            harq_process->B,harq_process->mcs,harq_process->Qm,harq_process->rvidx,harq_process->round,dlsch->max_ldpc_iterations);
*/

#if UE_TIMING_TRACE
        start_meas(dlsch_turbo_decoding_stats);
#endif

      //LOG_E(PHY,"AbsSubframe %d.%d Start turbo segment %d/%d A %d ",frame%1024,nr_tti_rx,r,harq_process->C-1, A);

      //printf("harq process dr iteration %d\n", p_decParams->numMaxIter);

      for (int cnt =0; cnt < (kc-2)*p_decParams->Z; cnt++){
471
            inv_d[cnt] = (1)*harq_process->d[r][cnt];
472 473
            }

474 475
      /*for (int cnt =0; cnt < 16; cnt++){
      printf("dr %d inv_d %d \n", harq_process->d[r][cnt], inv_d[cnt]);
476 477
      }

478
      printf(" \n");
479

480
      printf("end dr \n");
481
      for (int cnt =(50*p_decParams->Z-16) ; cnt < 50*p_decParams->Z; cnt++){
482
            printf("%d ", harq_process->d[r][cnt]);
483 484 485
            }
      printf(" \n");*/

Hongzhi Wang's avatar
Hongzhi Wang committed
486
		memset(pv,0,2*harq_process->Z*sizeof(int16_t));
487
        //memset(pl,0,2*p_decParams->Z*sizeof(int8_t));
Hongzhi Wang's avatar
Hongzhi Wang committed
488
    	memset((pv+K_bytes_F),127,harq_process->F*sizeof(int16_t));
489

490
      	for (i=((2*p_decParams->Z)>>3), j = 0; i < K_bytes_F+((2*p_decParams->Z)>>3); i++, j++)
491 492 493
      	{
      		pv[i]= _mm_loadu_si128((__m128i*)(&inv_d[8*j]));
      	}
494

495
		for (i=Kr_bytes+((2*p_decParams->Z)>>3),j=Kr_bytes; i < ((kc*p_decParams->Z)>>3); i++, j++)
496 497 498
		      	{
		      		pv[i]= _mm_loadu_si128((__m128i*)(&inv_d[8*j]));
		      	}
499
      	
500
		for (i=0, j=0; j < ((kc*p_decParams->Z)>>4);  i+=2, j++)
501 502 503 504 505 506 507 508 509 510
      	      	{
      				
      	      		pl[j] = _mm_packs_epi16(pv[i],pv[i+1]);
      	      		
      	      	}

		no_iteration_ldpc = nrLDPC_decoder(p_decParams,
						   (int8_t*)&pl[0],
						   llrProcBuf,
						   p_procTime);
511

Guy De Souza's avatar
Guy De Souza committed
512
		if (check_crc(llrProcBuf,harq_process->B,harq_process->F,crc_type)) {
513 514 515 516 517 518 519
		  printf("CRC OK\n");
		  ret = 2;
		}
		else {
		  printf("CRC NOK\n");
		  ret = 1+dlsch->max_ldpc_iterations;
		}
Guy De Souza's avatar
Guy De Souza committed
520
		
521 522

		nb_total_decod++;
523
		if (no_iteration_ldpc > dlsch->max_ldpc_iterations){
524 525
		  nb_error_decod++;
		}
526 527 528

		ret=no_iteration_ldpc;

529
		//if (!nb_total_decod%10000){
530
		//printf("Error number of iteration LPDC %d %ld/%ld \n", no_iteration_ldpc, nb_error_decod,nb_total_decod);fflush(stdout);
531
		//}
532
		//else
533
		//printf("OK number of iteration LPDC %d\n", no_iteration_ldpc);
534 535 536 537 538 539

		for (int m=0; m < Kr>>3; m ++)
		      	      	{
		      				harq_process->c[r][m]= (uint8_t) llrProcBuf[m];
		      	      	}

540
#ifdef DEBUG_DLSCH_DECODING
hongzhi wang's avatar
hongzhi wang committed
541
      //printf("output decoder %d %d %d %d %d \n", harq_process->c[r][0], harq_process->c[r][1], harq_process->c[r][2],harq_process->c[r][3], harq_process->c[r][4]);
Guy De Souza's avatar
Guy De Souza committed
542
      for (int k=0;k<A>>3;k++)
hongzhi wang's avatar
hongzhi wang committed
543
       printf("output decoder [%d] =  0x%02x \n", k, harq_process->c[r][k]);
544
      printf("no_iterations_ldpc %d (ret %d)\n",no_iteration_ldpc,ret);
545
      //write_output("dec_output.m","dec0",harq_process->c[0],Kr_bytes,1,4);
546
#endif
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562


#if UE_TIMING_TRACE
      stop_meas(dlsch_turbo_decoding_stats);
#endif
    }

    /*printf("Segmentation: C %d r %d, dlsch_rate_unmatching_stats %5.3f dlsch_deinterleaving_stats %5.3f  dlsch_turbo_decoding_stats %5.3f \n",
                  harq_process->C,
                  r,
                  dlsch_rate_unmatching_stats->p_time/(cpuf*1000.0),
                  dlsch_deinterleaving_stats->p_time/(cpuf*1000.0),
                  dlsch_turbo_decoding_stats->p_time/(cpuf*1000.0));*/


    if ((err_flag == 0) && (ret>=(1+dlsch->max_ldpc_iterations))) {// a Code segment is in error so break;
563
      LOG_D(PHY,"AbsSubframe %d.%d CRC failed, segment %d/%d \n",frame%1024,nr_tti_rx,r,harq_process->C-1);
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
      err_flag = 1;
    }
  }

  int32_t frame_rx_prev = frame;
  int32_t tti_rx_prev = nr_tti_rx - 1;
  if (tti_rx_prev < 0) {
    frame_rx_prev--;
    tti_rx_prev += 10*frame_parms->ttis_per_subframe;
  }
  frame_rx_prev = frame_rx_prev%1024;

  if (err_flag == 1) {
#if UE_DEBUG_TRACE
    LOG_I(PHY,"[UE %d] DLSCH: Setting NAK for SFN/SF %d/%d (pid %d, status %d, round %d, TBS %d, mcs %d) Kr %d r %d harq_process->round %d\n",
        phy_vars_ue->Mod_id, frame, nr_tti_rx, harq_pid,harq_process->status, harq_process->round,harq_process->TBS,harq_process->mcs,Kr,r,harq_process->round);
#endif
    harq_process->harq_ack.ack = 0;
    harq_process->harq_ack.harq_id = harq_pid;
    harq_process->harq_ack.send_harq_status = 1;
    harq_process->errors[harq_process->round]++;
    harq_process->round++;


    //    printf("Rate: [UE %d] DLSCH: Setting NACK for subframe %d (pid %d, round %d)\n",phy_vars_ue->Mod_id,subframe,harq_pid,harq_process->round);
    if (harq_process->round >= dlsch->Mdlharq) {
      harq_process->status = SCH_IDLE;
      harq_process->round  = 0;
    }
    if(is_crnti)
    {
    LOG_D(PHY,"[UE %d] DLSCH: Setting NACK for nr_tti_rx %d (pid %d, pid status %d, round %d/Max %d, TBS %d)\n",
               phy_vars_ue->Mod_id,nr_tti_rx,harq_pid,harq_process->status,harq_process->round,dlsch->Mdlharq,harq_process->TBS);
    }

    return((1+dlsch->max_ldpc_iterations));
  } else {
#if UE_DEBUG_TRACE
      LOG_I(PHY,"[UE %d] DLSCH: Setting ACK for nr_tti_rx %d TBS %d mcs %d nb_rb %d harq_process->round %d\n",
           phy_vars_ue->Mod_id,nr_tti_rx,harq_process->TBS,harq_process->mcs,harq_process->nb_rb, harq_process->round);
#endif

    harq_process->status = SCH_IDLE;
    harq_process->round  = 0;
    harq_process->harq_ack.ack = 1;
    harq_process->harq_ack.harq_id = harq_pid;
    harq_process->harq_ack.send_harq_status = 1;
    //LOG_I(PHY,"[UE %d] DLSCH: Setting ACK for SFN/SF %d/%d (pid %d, status %d, round %d, TBS %d, mcs %d)\n",
      //  phy_vars_ue->Mod_id, frame, subframe, harq_pid, harq_process->status, harq_process->round,harq_process->TBS,harq_process->mcs);

    if(is_crnti)
    {
    LOG_D(PHY,"[UE %d] DLSCH: Setting ACK for nr_tti_rx %d (pid %d, round %d, TBS %d)\n",phy_vars_ue->Mod_id,nr_tti_rx,harq_pid,harq_process->round,harq_process->TBS);
    }
    //LOG_D(PHY,"[UE %d] DLSCH: Setting ACK for subframe %d (pid %d, round %d)\n",phy_vars_ue->Mod_id,subframe,harq_pid,harq_process->round);

  }

  // Reassembly of Transport block here
  offset = 0;
Hongzhi Wang's avatar
Hongzhi Wang committed
624 625
  Kr = harq_process->K;
  Kr_bytes = Kr>>3;
626 627 628 629 630 631 632 633

  /*
  printf("harq_pid %d\n",harq_pid);
  printf("F %d, Fbytes %d\n",harq_process->F,harq_process->F>>3);
  printf("C %d\n",harq_process->C);
  */
  for (r=0; r<harq_process->C; r++) {

Hongzhi Wang's avatar
Hongzhi Wang committed
634
    memcpy(harq_process->b+offset,
635
             harq_process->c[r],
Hongzhi Wang's avatar
Hongzhi Wang committed
636 637 638 639 640 641 642 643 644 645 646 647 648
             Kr_bytes- - (harq_process->F>>3) -((harq_process->C>1)?3:0));
    offset += (Kr_bytes - (harq_process->F>>3) - ((harq_process->C>1)?3:0));

#ifdef DEBUG_DLSCH_DECODING
    printf("Segment %d : Kr= %d bytes\n",r,Kr_bytes);
    printf("copied %d bytes to b sequence (harq_pid %d)\n",
              (Kr_bytes - (harq_process->F>>3)-((harq_process->C>1)?3:0)),harq_pid);
              printf("b[0] = %x,c[%d] = %x\n",
              harq_process->b[offset],
              harq_process->F>>3,
              harq_process->c[r]);
#endif

649 650 651 652 653 654 655 656
  }

  dlsch->last_iteration_cnt = ret;

  return(ret);
}

#ifdef UE_DLSCH_PARALLELISATION
657 658
uint32_t  nr_dlsch_decoding_mthread(PHY_VARS_NR_UE *phy_vars_ue,
			 UE_nr_rxtx_proc_t *proc,
659 660 661 662 663 664
                         int eNB_id,
                         short *dlsch_llr,
                         NR_DL_FRAME_PARMS *frame_parms,
                         NR_UE_DLSCH_t *dlsch,
                         NR_DL_UE_HARQ_t *harq_process,
                         uint32_t frame,
665
			 uint16_t nb_symb_sch,
666 667 668 669 670 671 672 673 674 675 676 677 678 679
                         uint8_t nr_tti_rx,
                         uint8_t harq_pid,
                         uint8_t is_crnti,
                         uint8_t llr8_flag)
{

#if UE_TIMING_TRACE
  time_stats_t *dlsch_rate_unmatching_stats=&phy_vars_ue->dlsch_rate_unmatching_stats;
  time_stats_t *dlsch_turbo_decoding_stats=&phy_vars_ue->dlsch_turbo_decoding_stats;
  time_stats_t *dlsch_deinterleaving_stats=&phy_vars_ue->dlsch_deinterleaving_stats;
#endif
  uint32_t A,E;
  uint32_t G;
  uint32_t ret,offset;
680 681
  //short dummy_w[MAX_NUM_DLSCH_SEGMENTS][3*(8448+64)];
  uint32_t r,r_offset=0,Kr,Kr_bytes,err_flag=0,K_bytes_F;
682 683 684 685 686 687 688 689 690 691
  uint8_t crc_type;
  //UE_rxtx_proc_t *proc = &phy_vars_ue->proc;
  int32_t no_iteration_ldpc;
  int Cby2;
  /*uint8_t C;
  uint8_t Qm;
  uint8_t Nl;
  uint8_t r_thread;
  uint32_t Er, Gp,GpmodC;*/
  t_nrLDPC_dec_params decParams;
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
  t_nrLDPC_dec_params* p_decParams = &decParams;
  t_nrLDPC_time_stats procTime;
  t_nrLDPC_time_stats* p_procTime =&procTime ;
  int16_t z [68*384];
  int8_t l [68*384];
  //__m128i l;
  int16_t inv_d [68*384];
  //int16_t *p_invd =&inv_d;
  uint8_t kb, kc;
  uint8_t Ilbrm = 0;
  uint32_t Tbslbrm = 950984;
  uint16_t nb_rb = 30; //to update
  //uint16_t nb_symb_sch = 12;
  uint8_t nb_re_dmrs = 6;
  uint16_t length_dmrs = 1;
707

708 709
  uint32_t i,j;
  //uint32_t k;
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729

    __m128i *pv = (__m128i*)&z;
    __m128i *pl = (__m128i*)&l;


  if (!dlsch_llr) {
    printf("dlsch_decoding.c: NULL dlsch_llr pointer\n");
    return(dlsch->max_ldpc_iterations);
  }

  if (!harq_process) {
    printf("dlsch_decoding.c: NULL harq_process pointer\n");
    return(dlsch->max_ldpc_iterations);
  }

  if (!frame_parms) {
    printf("dlsch_decoding.c: NULL frame_parms pointer\n");
    return(dlsch->max_ldpc_iterations);
  }

730
 /* if (nr_tti_rx> (10*frame_parms->ttis_per_subframe-1)) {
731 732 733 734 735 736 737
    printf("dlsch_decoding.c: Illegal subframe index %d\n",nr_tti_rx);
    return(dlsch->max_ldpc_iterations);
  }

  if (dlsch->harq_ack[nr_tti_rx].ack != 2) {
    LOG_D(PHY, "[UE %d] DLSCH @ SF%d : ACK bit is %d instead of DTX even before PDSCH is decoded!\n",
        phy_vars_ue->Mod_id, nr_tti_rx, dlsch->harq_ack[nr_tti_rx].ack);
738
  }*/
739 740 741 742 743 744 745 746 747 748 749 750 751 752

  /*
  if (nb_rb > frame_parms->N_RB_DL) {
    printf("dlsch_decoding.c: Illegal nb_rb %d\n",nb_rb);
    return(max_ldpc_iterations);
    }*/

  /*harq_pid = dlsch->current_harq_pid[phy_vars_ue->current_thread_id[subframe]];
  if (harq_pid >= 8) {
    printf("dlsch_decoding.c: Illegal harq_pid %d\n",harq_pid);
    return(max_ldpc_iterations);
  }
  */

753
  nb_rb = harq_process->nb_rb;
754 755
  harq_process->trials[harq_process->round]++;

756 757
  harq_process->TBS = nr_compute_tbs(harq_process->mcs,nb_rb,nb_symb_sch,nb_re_dmrs,length_dmrs, harq_process->Nl);

758 759 760 761
  A = harq_process->TBS; //2072 for QPSK 1/3

  ret = dlsch->max_ldpc_iterations;

762
  harq_process->G = nr_get_G(nb_rb, nb_symb_sch, nb_re_dmrs, length_dmrs, harq_process->Qm,harq_process->Nl);
763 764 765 766 767 768 769 770 771 772 773 774 775 776

  G = harq_process->G;

  proc->decoder_main_available = 1;
  proc->decoder_thread_available = 0;
  proc->decoder_thread_available1 = 0;
  //get_G(frame_parms,nb_rb,dlsch->rb_alloc,mod_order,num_pdcch_symbols,phy_vars_ue->frame,subframe);

  //  printf("DLSCH Decoding, harq_pid %d Ndi %d\n",harq_pid,harq_process->Ndi);

  if (harq_process->round == 0) {
      // This is a new packet, so compute quantities regarding segmentation
      harq_process->B = A+24;
      nr_segmentation(NULL,
777 778 779 780 781 782 783
          	          NULL,
          	          harq_process->B,
          	          &harq_process->C,
          	          &harq_process->K,
           		  &harq_process->Z,
          	          &harq_process->F);
          		  p_decParams->Z = harq_process->Z;
784 785 786

    }

787
 	kb = harq_process->K/harq_process->Z;
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
  	  if ( kb==22){
  		  p_decParams->BG = 1;
  		  p_decParams->R = 89;
  		  kc = 68;
  	  }
  	  else{
  		  p_decParams->BG = 2;
  		  p_decParams->R = 13;
  		  kc = 52;
  	  	  }

      p_decParams->numMaxIter = 2;
      Kr = p_decParams->Z*kb;
      p_decParams->outMode= 0;

  /*
  else {
    printf("dlsch_decoding.c: Ndi>0 not checked yet!!\n");
    return(max_ldpc_iterations);
  }
  */
  err_flag = 0;
  r_offset = 0;

  unsigned char bw_scaling =1;

  switch (frame_parms->N_RB_DL) {
815
  case 106:
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
    bw_scaling =2;
    break;

  default:
    bw_scaling =1;
    break;
  }

  if (harq_process->C > MAX_NUM_DLSCH_SEGMENTS/bw_scaling) {
    LOG_E(PHY,"Illegal harq_process->C %d > %d\n",harq_process->C,MAX_NUM_DLSCH_SEGMENTS/bw_scaling);
    return((1+dlsch->max_ldpc_iterations));
  }
#ifdef DEBUG_DLSCH_DECODING
  printf("Segmentation: C %d, Cminus %d, Kminus %d, Kplus %d\n",harq_process->C,harq_process->Cminus,harq_process->Kminus,harq_process->Kplus);
#endif

  opp_enabled=1;
if (harq_process->C>1) { // wakeup worker if more than 1 segment
     if (pthread_mutex_lock(&proc->mutex_dlsch_td) != 0) {
         LOG_E( PHY, "[SCHED][UE %d][Slot0] error locking mutex for UE dlsch td\n",phy_vars_ue->Mod_id );
         exit_fun("nothing to add");
     }

     /*Qm= harq_process->Qm;
     Nl=harq_process->Nl;
     r_thread = harq_process->C/2-1;
     C= harq_process->C;

     Gp = G/Nl/Qm;
      GpmodC = Gp%C;



      if (r_thread < (C-(GpmodC)))
        Er = Nl*Qm * (Gp/C);
      else
        Er = Nl*Qm * ((GpmodC==0?0:1) + (Gp/C));
     printf("mthread Er %d\n", Er);

     printf("mthread instance_cnt_dlsch_td %d\n",  proc->instance_cnt_dlsch_td);*/

     proc->instance_cnt_dlsch_td++;
               proc->eNB_id    = eNB_id;
               proc->harq_pid  = harq_pid;
               proc->llr8_flag = llr8_flag;
               //proc->r[0] = 1;

     if (proc->instance_cnt_dlsch_td == 0)
     {
     LOG_D(PHY,"unblock dlsch td processing thread blocked on instance_cnt_dlsch_td : %d \n", proc->instance_cnt_dlsch_td );
         if (pthread_cond_signal(&proc->cond_dlsch_td) != 0) {
             LOG_E( PHY, "[SCHED][UE %d][Slot0] ERROR pthread_cond_signal for UE dlsch td\n", phy_vars_ue->Mod_id);
             exit_fun("nothing to add");
         }
         if (pthread_mutex_unlock(&proc->mutex_dlsch_td) != 0) {
             LOG_E( PHY, "[SCHED][UE %d][Slot0] error unlocking mutex for UE dlsch td \n",phy_vars_ue->Mod_id );
             exit_fun("nothing to add");
         }

     } else
     {
         LOG_E( PHY, "[SCHED][UE %d] UE dlsch td thread busy (IC %d)!!\n", phy_vars_ue->Mod_id, proc->instance_cnt_dlsch_td);
         if (proc->instance_cnt_dlsch_td > 4)
             exit_fun("instance_cnt_dlsch_td > 4");
     }
     //AssertFatal(pthread_cond_signal(&proc->cond_slot1_dl_processing) ==0 ,"");
     AssertFatal(pthread_mutex_unlock(&proc->mutex_dlsch_td) ==0,"");

     if (harq_process->C>2) {
    	 if (pthread_mutex_lock(&proc->mutex_dlsch_td1) != 0) {
    	          LOG_E( PHY, "[SCHED][UE %d][Slot0] error locking mutex for UE dlsch td\n",phy_vars_ue->Mod_id );
    	          exit_fun("nothing to add");
    	      }

    	 proc->instance_cnt_dlsch_td1++;
                    proc->eNB_id    = eNB_id;
                    proc->harq_pid  = harq_pid;
                    proc->llr8_flag = llr8_flag;
                   // proc->Er = Er;

          if (proc->instance_cnt_dlsch_td1 == 0)
          {
          LOG_D(PHY,"unblock slot1 dl processing thread blocked on instance_cnt_dlsch_td : %d \n", proc->instance_cnt_dlsch_td1 );
              if (pthread_cond_signal(&proc->cond_dlsch_td1) != 0) {
                  LOG_E( PHY, "[SCHED][UE %d][Slot0] ERROR pthread_cond_signal for UE dlsch td\n", phy_vars_ue->Mod_id);
                  exit_fun("nothing to add");
              }
              if (pthread_mutex_unlock(&proc->mutex_dlsch_td1) != 0) {
                  LOG_E( PHY, "[SCHED][UE %d][Slot0] error unlocking mutex for UE dlsch td \n",phy_vars_ue->Mod_id );
                  exit_fun("nothing to add");
              }

          } else
          {
              LOG_E( PHY, "[SCHED][UE %d] UE dlsch td thread 1 busy (IC %d)!!\n", phy_vars_ue->Mod_id, proc->instance_cnt_dlsch_td1);
              if (proc->instance_cnt_dlsch_td1 > 4)
                  exit_fun("instance_cnt_dlsch_td1 > 4");
          }

          AssertFatal(pthread_mutex_unlock(&proc->mutex_dlsch_td1) ==0,"");

     }
	/*
	if (pthread_mutex_timedlock(&proc->mutex_td,&wait) != 0) {
       printf("[eNB] ERROR pthread_mutex_lock for TD thread (IC %d)\n", proc->instance_cnt_td);
       exit_fun( "error locking mutex_fep" );
       return -1;
     }

     if (proc->instance_cnt_td==0) {
       printf("[UE] TD thread busy\n");
       exit_fun("TD thread busy");
       pthread_mutex_unlock( &proc->mutex_td );
       return -1;
     }

     ++proc->instance_cnt_td;

     proc->tdp.UE        = phy_vars_ue;
     proc->tdp.eNB_id    = eNB_id;
     proc->tdp.harq_pid  = harq_pid;
     proc->tdp.llr8_flag = llr8_flag;

     printf("----- 2thread llr flag %d tdp flag %d\n",llr8_flag, proc->tdp.llr8_flag);


     // wakeup worker to do second half segments
     if (pthread_cond_signal(&proc->cond_td) != 0) {
       printf("[UE] ERROR pthread_cond_signal for td thread exit\n");
       exit_fun( "ERROR pthread_cond_signal" );
       return (1+dlsch->last_iteration_cnt);
     }

     pthread_mutex_unlock( &proc->mutex_td );*/


     Cby2 = 1; //harq_process->C/2;
     //proc->decoder_main_available = 1;
   }
   else {
     Cby2 = 1;
   }

  for (r=0; r<Cby2; r++) {

961
    Kr = harq_process->K;
962
    Kr_bytes = Kr>>3;
963
    K_bytes_F = Kr_bytes-(harq_process->F>>3);
964

965
    Tbslbrm = nr_compute_tbs(28,nb_rb,frame_parms->symbols_per_slot,0,0, harq_process->Nl);
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985

#if UE_TIMING_TRACE
    start_meas(dlsch_rate_unmatching_stats);
#endif

#ifdef DEBUG_DLSCH_DECODING
    LOG_D(PHY,"HARQ_PID %d Rate Matching Segment %d (coded bits %d,unpunctured/repeated bits %d, TBS %d, mod_order %d, nb_rb %d, Nl %d, rv %d, round %d)...\n",
          harq_pid,r, G,
          Kr*3,
          harq_process->TBS,
          harq_process->Qm,
          harq_process->nb_rb,
          harq_process->Nl,
          harq_process->rvidx,
          harq_process->round);
#endif

#ifdef DEBUG_DLSCH_DECODING
    printf(" in decoding dlsch->harq_processes[harq_pid]->rvidx = %d\n", dlsch->harq_processes[harq_pid]->rvidx);
#endif
986 987 988 989 990 991 992 993 994 995 996 997 998 999
    if (nr_rate_matching_ldpc_rx(Ilbrm,
         	 	 	 Tbslbrm,
    				 p_decParams->BG,
    				 p_decParams->Z,
        			 G,
    				 harq_process->w[r],
    				 dlsch_llr+r_offset,
    				 harq_process->C,
    				 harq_process->rvidx,
    				 (harq_process->round==0)?1:0,
    				 harq_process->Qm,
    				 harq_process->Nl,
    				 r,
    				 &E)==-1) {
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
#if UE_TIMING_TRACE
      stop_meas(dlsch_rate_unmatching_stats);
#endif
      LOG_E(PHY,"dlsch_decoding.c: Problem in rate_matching\n");
      return(dlsch->max_ldpc_iterations);
    } else
    {
#if UE_TIMING_TRACE
      stop_meas(dlsch_rate_unmatching_stats);
#endif
    }
    r_offset += E;
    //printf("main thread r_offset %d\n",r_offset);

    /*
    printf("Subblock deinterleaving, d %p w %p\n",
     harq_process->d[r],
     harq_process->w);
    */
#if UE_TIMING_TRACE
    start_meas(dlsch_deinterleaving_stats);
#endif
1022 1023 1024 1025
    nr_deinterleaving_ldpc(E,
        		   harq_process->Qm,
                           harq_process->d[r],
                           harq_process->w[r]);
1026 1027 1028 1029
#if UE_TIMING_TRACE
    stop_meas(dlsch_deinterleaving_stats);
#endif
#ifdef DEBUG_DLSCH_DECODING
1030

1031 1032 1033 1034 1035 1036
    if (r==0) {
              write_output("decoder_llr.m","decllr",dlsch_llr,G,1,0);
              write_output("decoder_in.m","dec",&harq_process->d[0][96],(3*8*Kr_bytes)+12,1,0);
    }

    printf("decoder input(segment %d) :",r);
1037 1038 1039
    for (int i=0;i<(3*8*Kr_bytes);i++)
      printf("%d : %d\n",i,harq_process->d[r][i]);
      printf("\n");
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
#endif


    //    printf("Clearing c, %p\n",harq_process->c[r]);
    memset(harq_process->c[r],0,Kr_bytes);

    //    printf("done\n");
    if (harq_process->C == 1)
      crc_type = CRC24_A;
    else
      crc_type = CRC24_B;

    //#ifndef __AVX2__
1053

1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
    if (err_flag == 0) {
/*
        LOG_I(PHY, "turbo algo Kr=%d cb_cnt=%d C=%d nbRB=%d crc_type %d TBSInput=%d TBSHarq=%d TBSplus24=%d mcs=%d Qm=%d RIV=%d round=%d maxIter %d\n",
                            Kr,r,harq_process->C,harq_process->nb_rb,crc_type,A,harq_process->TBS,
                            harq_process->B,harq_process->mcs,harq_process->Qm,harq_process->rvidx,harq_process->round,dlsch->max_ldpc_iterations);
*/
    	if (llr8_flag) {
    		AssertFatal (Kr >= 256, "turbo algo issue Kr=%d cb_cnt=%d C=%d nbRB=%d TBSInput=%d TBSHarq=%d TBSplus24=%d mcs=%d Qm=%d RIV=%d round=%d\n",
    				Kr,r,harq_process->C,harq_process->nb_rb,A,harq_process->TBS,harq_process->B,harq_process->mcs,harq_process->Qm,harq_process->rvidx,harq_process->round);
    	}
#if UE_TIMING_TRACE
        start_meas(dlsch_turbo_decoding_stats);
#endif
      LOG_D(PHY,"mthread AbsSubframe %d.%d Start turbo segment %d/%d \n",frame%1024,nr_tti_rx,r,harq_process->C-1);

		memset(pv,0,2*p_decParams->Z*sizeof(int16_t));
        //memset(pl,0,2*p_decParams->Z*sizeof(int8_t));
1071
		memset((pv+K_bytes_F),127,harq_process->F*sizeof(int16_t));
1072 1073


1074
      	for (i=((2*p_decParams->Z)>>3), j = 0; i < K_bytes_F+((2*p_decParams->Z)>>3); i++, j++)
1075 1076 1077
      	{
      		pv[i]= _mm_loadu_si128((__m128i*)(&inv_d[8*j]));
      	}
1078 1079 1080 1081 1082 1083 1084

		for (i=Kr_bytes+((2*p_decParams->Z)>>3),j=Kr_bytes; i < ((kc*p_decParams->Z)>>3); i++, j++)
		      	{
		      		pv[i]= _mm_loadu_si128((__m128i*)(&inv_d[8*j]));
		      	}

		for (i=0, j=0; j < ((kc*p_decParams->Z)>>4);  i+=2, j++)
1085
      	      	{
1086

1087
      	      		pl[j] = _mm_packs_epi16(pv[i],pv[i+1]);
1088

1089 1090 1091
      	      	}

		no_iteration_ldpc = nrLDPC_decoder(p_decParams,
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
						   (int8_t*)&pl[0],
						   llrProcBuf,
						   p_procTime);

		nb_total_decod++;
		if (no_iteration_ldpc > 10){
		  nb_error_decod++;
		  ret = 1+dlsch->max_ldpc_iterations;
		}
		else {
		  ret=2;
		}
		//if (!nb_total_decod%10000){
				printf("Error number of iteration LPDC %d %ld/%ld \n", no_iteration_ldpc, nb_error_decod,nb_total_decod);fflush(stdout);
		//}
1107 1108

		//else
1109
			//printf("OK number of iteration LPDC %d\n", no_iteration_ldpc);
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160

		for (int m=0; m < Kr>>3; m ++)
		      	      	{
		      				harq_process->c[r][m]= (uint8_t) llrProcBuf[m];
		      	      	}

		      	/*for (int u=0; u < Kr>>3; u ++)
		      	      	      	{
		      						ullrProcBuf[u]= (uint8_t) llrProcBuf[u];
		      	      	      	}


		      	printf("output unsigned ullrProcBuf \n");

		      	for (int j=0; j < Kr>>3; j ++)
		      	      	       	      	{

		      	      						printf(" %d \n", ullrProcBuf[j]);

		      	      	      	      	}
		     	printf(" \n");*/
		//printf("output channel decoder %d %d %d %d %d \n", harq_process->c[r][0], harq_process->c[r][1], harq_process->c[r][2],harq_process->c[r][3], harq_process->c[r][4]);

		 //printf("output decoder %d %d %d %d %d \n", harq_process->c[r][0], harq_process->c[r][1], harq_process->c[r][2],harq_process->c[r][3], harq_process->c[r][4]);


#if UE_TIMING_TRACE
      stop_meas(dlsch_turbo_decoding_stats);
#endif
    }


    if ((err_flag == 0) && (ret>=(1+dlsch->max_ldpc_iterations))) {// a Code segment is in error so break;
      LOG_D(PHY,"AbsSubframe %d.%d CRC failed, segment %d/%d \n",frame%1024,nr_tti_rx,r,harq_process->C-1);
      err_flag = 1;
    }
  }

  int32_t frame_rx_prev = frame;
  int32_t tti_rx_prev = nr_tti_rx - 1;
  if (tti_rx_prev < 0) {
    frame_rx_prev--;
    tti_rx_prev += 10*frame_parms->ttis_per_subframe;
  }
  frame_rx_prev = frame_rx_prev%1024;

  if (err_flag == 1) {
#if UE_DEBUG_TRACE
    LOG_I(PHY,"[UE %d] DLSCH: Setting NAK for SFN/SF %d/%d (pid %d, status %d, round %d, TBS %d, mcs %d) Kr %d r %d harq_process->round %d\n",
        phy_vars_ue->Mod_id, frame, nr_tti_rx, harq_pid,harq_process->status, harq_process->round,harq_process->TBS,harq_process->mcs,Kr,r,harq_process->round);
#endif
1161 1162 1163
    harq_process->harq_ack.ack = 0;
    harq_process->harq_ack.harq_id = harq_pid;
    harq_process->harq_ack.send_harq_status = 1;
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
    harq_process->errors[harq_process->round]++;
    harq_process->round++;


    //    printf("Rate: [UE %d] DLSCH: Setting NACK for subframe %d (pid %d, round %d)\n",phy_vars_ue->Mod_id,subframe,harq_pid,harq_process->round);
    if (harq_process->round >= dlsch->Mdlharq) {
      harq_process->status = SCH_IDLE;
      harq_process->round  = 0;
    }
    if(is_crnti)
    {
    LOG_D(PHY,"[UE %d] DLSCH: Setting NACK for nr_tti_rx %d (pid %d, pid status %d, round %d/Max %d, TBS %d)\n",
               phy_vars_ue->Mod_id,nr_tti_rx,harq_pid,harq_process->status,harq_process->round,dlsch->Mdlharq,harq_process->TBS);
    }

    return((1+dlsch->max_ldpc_iterations));
  } else {
#if UE_DEBUG_TRACE
      LOG_I(PHY,"[UE %d] DLSCH: Setting ACK for nr_tti_rx %d TBS %d mcs %d nb_rb %d\n",
           phy_vars_ue->Mod_id,nr_tti_rx,harq_process->TBS,harq_process->mcs,harq_process->nb_rb);
#endif

    harq_process->status = SCH_IDLE;
    harq_process->round  = 0;
1188 1189 1190
    harq_process->harq_ack.ack = 1;
    harq_process->harq_ack.harq_id = harq_pid;
    harq_process->harq_ack.send_harq_status = 1;
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
    //LOG_I(PHY,"[UE %d] DLSCH: Setting ACK for SFN/SF %d/%d (pid %d, status %d, round %d, TBS %d, mcs %d)\n",
      //  phy_vars_ue->Mod_id, frame, subframe, harq_pid, harq_process->status, harq_process->round,harq_process->TBS,harq_process->mcs);

    if(is_crnti)
    {
    LOG_D(PHY,"[UE %d] DLSCH: Setting ACK for nr_tti_rx %d (pid %d, round %d, TBS %d)\n",phy_vars_ue->Mod_id,nr_tti_rx,harq_pid,harq_process->round,harq_process->TBS);
    }
    //LOG_D(PHY,"[UE %d] DLSCH: Setting ACK for subframe %d (pid %d, round %d)\n",phy_vars_ue->Mod_id,subframe,harq_pid,harq_process->round);

  }

  // Reassembly of Transport block here
  offset = 0;

  /*
  printf("harq_pid %d\n",harq_pid);
  printf("F %d, Fbytes %d\n",harq_process->F,harq_process->F>>3);
  printf("C %d\n",harq_process->C);
  */
  uint32_t wait = 0;
  if (harq_process->C==2){
	  while((proc->decoder_thread_available == 0) )
  {
          usleep(1);
          wait++;
  }
  }
  else if ((harq_process->C==3) ){
	  while((proc->decoder_thread_available == 0) || (proc->decoder_thread_available1 == 0))
			  {
			            usleep(1);
			            wait++;
			    }
  }

  proc->decoder_main_available = 0;
  
  for (r=0; r<harq_process->C; r++) {

1230 1231
	  Kr = harq_process->K; //to check if same K in all segments
	  Kr_bytes = Kr>>3;
1232

1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
	    memcpy(harq_process->b+offset,
	             harq_process->c[r],
	             Kr_bytes- - (harq_process->F>>3) -((harq_process->C>1)?3:0));
	    offset += (Kr_bytes - (harq_process->F>>3) - ((harq_process->C>1)?3:0));

#ifdef DEBUG_DLSCH_DECODING
	    printf("Segment %d : Kr= %d bytes\n",r,Kr_bytes);
	    printf("copied %d bytes to b sequence (harq_pid %d)\n",
	              (Kr_bytes - (harq_process->F>>3)-((harq_process->C>1)?3:0)),harq_pid);
	              printf("b[0] = %x,c[%d] = %x\n",
	              harq_process->b[offset],
	              harq_process->F>>3,
	              harq_process->c[r]);
#endif
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
  }

  dlsch->last_iteration_cnt = ret;
  //proc->decoder_thread_available = 0;
  //proc->decoder_main_available = 0;
  
  //wait for worker to finish
  //wait_on_busy_condition(&proc->mutex_td,&proc->cond_td,&proc->instance_cnt_dlsch td,"dlsch td thread");

  //return( (ret>proc->tdp.ret) ? ret : proc->tdp.ret );

  return(ret);
}
#endif

#ifdef UE_DLSCH_PARALLELISATION
#define FIFO_PRIORITY   39
1264
void *nr_dlsch_decoding_2thread0(void *arg)
1265
{
1266 1267 1268
    static __thread int UE_dlsch_td_retval;
    struct nr_rxtx_thread_data *rtd = arg;
    UE_nr_rxtx_proc_t *proc = rtd->proc;
1269
    PHY_VARS_NR_UE    *phy_vars_ue   = rtd->UE;
1270
    NR_DL_FRAME_PARMS *frame_parms = &phy_vars_ue->frame_parms;
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
    int llr8_flag1;
    int32_t no_iteration_ldpc;
    t_nrLDPC_dec_params decParams;
    t_nrLDPC_dec_params* p_decParams = &decParams;
    t_nrLDPC_time_stats procTime;
    t_nrLDPC_time_stats* p_procTime =&procTime ;
    int16_t z [68*384];
    int8_t l [68*384];
    //__m128i l;
    int16_t inv_d [68*384];
    int16_t *p_invd =&inv_d;
1282 1283 1284 1285 1286 1287 1288
    uint8_t kb, kc;
    uint8_t Ilbrm = 0;
    uint32_t Tbslbrm = 950984;
    uint16_t nb_rb = 30; //to update
    uint16_t nb_symb_sch = 12;
    uint8_t nb_re_dmrs = 6;
    uint16_t length_dmrs = 1;
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322

    uint32_t i,j;
    uint32_t k;

    __m128i *pv = (__m128i*)&z;
    __m128i *pl = (__m128i*)&l;

    proc->instance_cnt_dlsch_td=-1;
    proc->nr_tti_rx=proc->sub_frame_start;

    proc->decoder_thread_available = 0;
    
    char threadname[256];
    sprintf(threadname,"UE_thread_dlsch_td_%d", proc->sub_frame_start);

	cpu_set_t cpuset;
    CPU_ZERO(&cpuset);
    if ( (proc->sub_frame_start+1)%RX_NB_TH == 0 && threads.dlsch_td_one != -1 )
    	CPU_SET(threads.dlsch_td_one, &cpuset);
    if ( (proc->sub_frame_start+1)%RX_NB_TH == 1 && threads.dlsch_td_two != -1 )
    	CPU_SET(threads.dlsch_td_two, &cpuset);
    if ( (proc->sub_frame_start+1)%RX_NB_TH == 2 && threads.dlsch_td_three != -1 )
    	CPU_SET(threads.dlsch_td_three, &cpuset);



#if UE_TIMING_TRACE
  time_stats_t *dlsch_rate_unmatching_stats=&phy_vars_ue->dlsch_rate_unmatching_stats;
  time_stats_t *dlsch_turbo_decoding_stats=&phy_vars_ue->dlsch_turbo_decoding_stats;
  time_stats_t *dlsch_deinterleaving_stats=&phy_vars_ue->dlsch_deinterleaving_stats;
#endif
  uint32_t A,E;
  uint32_t G;
  uint32_t ret,offset;
1323 1324
//  short dummy_w[MAX_NUM_DLSCH_SEGMENTS][3*(8448+64)];
  uint32_t r,r_offset=0,Kr,Kr_bytes,err_flag=0,K_bytes_F;
1325
  uint8_t crc_type;
1326 1327 1328 1329
  uint8_t C,Cprime;
  uint8_t Qm;
  uint8_t Nl;
  //uint32_t Er;
1330

1331 1332
  init_thread(900000,1000000 , FIFO_PRIORITY-1, &cpuset, threadname);
  while (!oai_exit) {
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349

		//proc->decoder_thread_available = 1;
		
	        if (pthread_mutex_lock(&proc->mutex_dlsch_td) != 0) {
	            LOG_E( PHY, "[SCHED][UE] error locking mutex for UE dlsch td\n" );
	            exit_fun("nothing to add");
	        }
	        while (proc->instance_cnt_dlsch_td < 0) {
	            // most of the time, the thread is waiting here
	            pthread_cond_wait( &proc->cond_dlsch_td, &proc->mutex_dlsch_td );
	        }
	        if (pthread_mutex_unlock(&proc->mutex_dlsch_td) != 0) {
	            LOG_E( PHY, "[SCHED][UE] error unlocking mutex for UE dlsch_td \n" );
	            exit_fun("nothing to add");
	        }

	        uint32_t wait = 0;
1350 1351 1352 1353 1354
	        while(proc->decoder_main_available == 0)
	        {
	            usleep(1);
	            wait++;
	        }
1355

1356
	        //proc->decoder_thread_available = 0;
1357
	        //PHY_VARS_NR_UE *phy_vars_ue   		= tdp->UE;
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
	        int eNB_id         				= proc->eNB_id;
	        int harq_pid      				= proc->harq_pid;
	        llr8_flag1     					= proc->llr8_flag;
	        //r_offset						= proc->Er;
	        //UE_rxtx_proc_t *proc    		= tdp->proc;
	        int frame                       = proc->frame_rx;
	        int subframe      				= proc->nr_tti_rx;
	        NR_UE_DLSCH_t *dlsch 			= phy_vars_ue->dlsch[phy_vars_ue->current_thread_id[subframe]][eNB_id][0];
	        NR_DL_UE_HARQ_t *harq_process  = dlsch->harq_processes[harq_pid];
	        short *dlsch_llr 				= phy_vars_ue->pdsch_vars[phy_vars_ue->current_thread_id[subframe]][eNB_id]->llr[0];
	        //printf("2thread0 llr flag %d tdp flag %d\n",llr8_flag1, tdp->llr8_flag);
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382

  /*
  if (nb_rb > frame_parms->N_RB_DL) {
    printf("dlsch_decoding.c: Illegal nb_rb %d\n",nb_rb);
    return(max_ldpc_iterations);
    }*/

  /*harq_pid = dlsch->current_harq_pid[phy_vars_ue->current_thread_id[subframe]];
  if (harq_pid >= 8) {
    printf("dlsch_decoding.c: Illegal harq_pid %d\n",harq_pid);
    return(max_ldpc_iterations);
  }
  */

1383
  nb_rb = harq_process->nb_rb;
1384 1385 1386

  harq_process->trials[harq_process->round]++;

1387 1388
  harq_process->TBS = nr_compute_tbs(harq_process->mcs,nb_rb,nb_symb_sch,nb_re_dmrs,length_dmrs, harq_process->Nl);

1389 1390 1391 1392
  A = harq_process->TBS; //2072 for QPSK 1/3

  ret = dlsch->max_ldpc_iterations;

1393
  harq_process->G = nr_get_G(nb_rb, nb_symb_sch, nb_re_dmrs, length_dmrs, harq_process->Qm,harq_process->Nl);
1394 1395 1396 1397 1398
  G = harq_process->G;
    
  if (harq_process->round == 0) {
    // This is a new packet, so compute quantities regarding segmentation
    harq_process->B = A+24;
1399 1400 1401 1402 1403 1404 1405 1406 1407

    nr_segmentation(NULL,
    	                    NULL,
    	                    harq_process->B,
    	                    &harq_process->C,
    	                    &harq_process->K,
   			    &harq_process->Z,
    	                    &harq_process->F);
    	p_decParams->Z = harq_process->Z;
1408 1409 1410

    }

1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
  kb = harq_process->K/harq_process->Z;
	  if ( kb==22){
		  p_decParams->BG = 1;
		  p_decParams->R = 89;
		  kc = 68;
	  }
	  else{
		  p_decParams->BG = 2;
		  p_decParams->R = 13;
		  kc = 52;
	  	  }

  p_decParams->numMaxIter = 2;
  Kr = p_decParams->Z*kb;
  p_decParams->outMode= 0;
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439

  /*
  else {
    printf("dlsch_decoding.c: Ndi>0 not checked yet!!\n");
    return(max_ldpc_iterations);
  }
  */
  err_flag = 0;
  //r_offset = 0;

  /*
  unsigned char bw_scaling =1;

  switch (frame_parms->N_RB_DL) {
1440
  case 106:
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
    bw_scaling =2;
    break;

  default:
    bw_scaling =1;
    break;
  }

  if (harq_process->C > MAX_NUM_DLSCH_SEGMENTS/bw_scaling) {
    LOG_E(PHY,"Illegal harq_process->C %d > %d\n",harq_process->C,MAX_NUM_DLSCH_SEGMENTS/bw_scaling);
    return((1+dlsch->max_ldpc_iterations));
  }*/
#ifdef DEBUG_DLSCH_DECODING
  printf("Segmentation: C %d, Cminus %d, Kminus %d, Kplus %d\n",harq_process->C,harq_process->Cminus,harq_process->Kminus,harq_process->Kplus);
#endif

  opp_enabled=1;
  
  Qm= harq_process->Qm;
1460 1461 1462
  Nl=harq_process->Nl;
  //r_thread = harq_process->C/2-1;
  C= harq_process->C;
1463

1464
  Cprime = C; //assume CBGTI not present
1465

1466 1467 1468 1469
  if (r <= Cprime - ((G/(Nl*Qm))%Cprime) - 1)
	  r_offset = Nl*Qm*(G/(Nl*Qm*Cprime));
  else
	  r_offset = Nl*Qm*((G/(Nl*Qm*Cprime))+1);
1470 1471 1472 1473 1474 1475 1476

		//	printf("thread0 r_offset %d\n",r_offset);
  	       
  //for (r=(harq_process->C/2); r<harq_process->C; r++) {
  	     r=1; //(harq_process->C/2);


1477 1478 1479
  Kr = harq_process->K;
  Kr_bytes = Kr>>3;
  K_bytes_F = Kr_bytes-(harq_process->F>>3);
1480

1481
  Tbslbrm = nr_compute_tbs(28,nb_rb,frame_parms->symbols_per_slot,0,0, harq_process->Nl);
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501

#if UE_TIMING_TRACE
    start_meas(dlsch_rate_unmatching_stats);
#endif

#ifdef DEBUG_DLSCH_DECODING
    LOG_D(PHY,"HARQ_PID %d Rate Matching Segment %d (coded bits %d,unpunctured/repeated bits %d, TBS %d, mod_order %d, nb_rb %d, Nl %d, rv %d, round %d)...\n",
          harq_pid,r, G,
          Kr*3,
          harq_process->TBS,
          harq_process->Qm,
          harq_process->nb_rb,
          harq_process->Nl,
          harq_process->rvidx,
          harq_process->round);
#endif

#ifdef DEBUG_DLSCH_DECODING
    printf(" in decoding dlsch->harq_processes[harq_pid]->rvidx = %d\n", dlsch->harq_processes[harq_pid]->rvidx);
#endif
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
    if (nr_rate_matching_ldpc_rx(Ilbrm,
    	 	 	 	 Tbslbrm,
				 p_decParams->BG,
				 p_decParams->Z,
				 G,
				 harq_process->w[r],
				 dlsch_llr+r_offset,
				 harq_process->C,
				 harq_process->rvidx,
				 (harq_process->round==0)?1:0,
				 harq_process->Qm,
				 harq_process->Nl,
				 r,
				 &E)==-1) {
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
#if UE_TIMING_TRACE
      stop_meas(dlsch_rate_unmatching_stats);
#endif
      LOG_E(PHY,"dlsch_decoding.c: Problem in rate_matching\n");
      //return(dlsch->max_ldpc_iterations);
    } else
    {
#if UE_TIMING_TRACE
      stop_meas(dlsch_rate_unmatching_stats);
#endif
    }
    r_offset += E;

1529 1530 1531 1532
    //for (int i =0; i<16; i++)
    //    	printf("rx output ratematching w[%d]= %d r_offset %d\n", i,harq_process->w[r][i], r_offset);

#if UE_TIMING_TRACE
1533 1534
    start_meas(dlsch_deinterleaving_stats);
#endif
1535 1536 1537 1538
    nr_deinterleaving_ldpc(E,
    			   harq_process->Qm,
                           harq_process->d[r],
                           harq_process->w[r]);
1539 1540 1541 1542
#if UE_TIMING_TRACE
    stop_meas(dlsch_deinterleaving_stats);
#endif
#ifdef DEBUG_DLSCH_DECODING
1543

1544 1545
    if (r==0) {
              write_output("decoder_llr.m","decllr",dlsch_llr,G,1,0);
1546
              write_output("decoder_in.m","dec",&harq_process->d[0][0],(3*8*Kr_bytes)+12,1,0);
1547 1548 1549 1550
    }

    printf("decoder input(segment %d) :",r);
    int i; for (i=0;i<(3*8*Kr_bytes)+12;i++)
1551 1552
      printf("%d : %d\n",i,harq_process->d[r][i]);
      printf("\n");
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
#endif


    //    printf("Clearing c, %p\n",harq_process->c[r]);
    memset(harq_process->c[r],0,Kr_bytes);

    //    printf("done\n");
    if (harq_process->C == 1)
      crc_type = CRC24_A;
    else
      crc_type = CRC24_B;


#if 1
    if (err_flag == 0) {
/*
        LOG_I(PHY, "turbo algo Kr=%d cb_cnt=%d C=%d nbRB=%d crc_type %d TBSInput=%d TBSHarq=%d TBSplus24=%d mcs=%d Qm=%d RIV=%d round=%d maxIter %d\n",
                            Kr,r,harq_process->C,harq_process->nb_rb,crc_type,A,harq_process->TBS,
                            harq_process->B,harq_process->mcs,harq_process->Qm,harq_process->rvidx,harq_process->round,dlsch->max_ldpc_iterations);
*/
    	if (llr8_flag1) {
    		AssertFatal (Kr >= 256, "turbo algo issue Kr=%d cb_cnt=%d C=%d nbRB=%d TBSInput=%d TBSHarq=%d TBSplus24=%d mcs=%d Qm=%d RIV=%d round=%d\n",
    				Kr,r,harq_process->C,harq_process->nb_rb,A,harq_process->TBS,harq_process->B,harq_process->mcs,harq_process->Qm,harq_process->rvidx,harq_process->round);
    	}
#if UE_TIMING_TRACE
        start_meas(dlsch_turbo_decoding_stats);
#endif
//      LOG_D(PHY,"AbsSubframe %d.%d Start turbo segment %d/%d \n",frame%1024,subframe,r,harq_process->C-1);

1582 1583 1584
        for (int cnt =0; cnt < (kc-2)*p_decParams->Z; cnt++){
              inv_d[cnt] = (1)*harq_process->d[r][cnt];
              }
1585

1586 1587 1588
        memset(pv,0,2*p_decParams->Z*sizeof(int16_t));
        //memset(pl,0,2*p_decParams->Z*sizeof(int8_t));
        memset((pv+K_bytes_F),127,harq_process->F*sizeof(int16_t));
1589

1590
        for (i=((2*p_decParams->Z)>>3), j = 0; i < K_bytes_F+((2*p_decParams->Z)>>3); i++, j++)
1591 1592 1593
      	{
      		pv[i]= _mm_loadu_si128((__m128i*)(&inv_d[8*j]));
      	}
1594 1595 1596 1597 1598 1599 1600

		for (i=Kr_bytes+((2*p_decParams->Z)>>3),j=Kr_bytes; i < ((kc*p_decParams->Z)>>3); i++, j++)
		      	{
		      		pv[i]= _mm_loadu_si128((__m128i*)(&inv_d[8*j]));
		      	}

		for (i=0, j=0; j < ((kc*p_decParams->Z)>>4);  i+=2, j++)
1601
      	      	{
1602

1603
      	      		pl[j] = _mm_packs_epi16(pv[i],pv[i+1]);
1604

1605 1606 1607
      	      	}

		no_iteration_ldpc = nrLDPC_decoder(p_decParams,
1608 1609 1610
						   (int8_t*)&pl[0],
						   llrProcBuf,
						   p_procTime);
1611

1612
		if (no_iteration_ldpc > 10)
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
			printf("Error number of iteration LPDC %d\n", no_iteration_ldpc);
		//else
			//printf("OK number of iteration LPDC %d\n", no_iteration_ldpc);

		for (int m=0; m < Kr>>3; m ++)
		      	      	{
		      				harq_process->c[r][m]= (uint8_t) llrProcBuf[m];
		      	      	}

		      	/*for (int u=0; u < Kr>>3; u ++)
		      	      	      	{
		      						ullrProcBuf[u]= (uint8_t) llrProcBuf[u];
		      	      	      	}


		      	printf("output unsigned ullrProcBuf \n");

		      	for (int j=0; j < Kr>>3; j ++)
		      	      	       	      	{

		      	      						printf(" %d \n", ullrProcBuf[j]);

		      	      	      	      	}
		     	printf(" \n");*/
		
#if UE_TIMING_TRACE
      stop_meas(dlsch_turbo_decoding_stats);
#endif
    }

    if ((err_flag == 0) && (ret>=(1+dlsch->max_ldpc_iterations))) {// a Code segment is in error so break;
//      LOG_D(PHY,"AbsSubframe %d.%d CRC failed, segment %d/%d \n",frame%1024,subframe,r,harq_process->C-1);
      err_flag = 1;
    }
  //}

  /*int32_t frame_rx_prev = frame;
  int32_t subframe_rx_prev = subframe - 1;
  if (subframe_rx_prev < 0) {
    frame_rx_prev--;
    subframe_rx_prev += 10;
  }
  frame_rx_prev = frame_rx_prev%1024;*/
#if 0
  if (err_flag == 1) {
//#if UE_DEBUG_TRACE
    LOG_I(PHY,"[UE %d] THREAD 0 DLSCH: Setting NAK for SFN/SF %d/%d (pid %d, status %d, round %d, TBS %d, mcs %d) Kr %d r %d harq_process->round %d\n",
        phy_vars_ue->Mod_id, frame, subframe, harq_pid,harq_process->status, harq_process->round,harq_process->TBS,harq_process->mcs,Kr,r,harq_process->round);
//#endif
    dlsch->harq_ack[subframe].ack = 0;
    dlsch->harq_ack[subframe].harq_id = harq_pid;
    dlsch->harq_ack[subframe].send_harq_status = 1;
    harq_process->errors[harq_process->round]++;
    harq_process->round++;


    //    printf("Rate: [UE %d] DLSCH: Setting NACK for subframe %d (pid %d, round %d)\n",phy_vars_ue->Mod_id,subframe,harq_pid,harq_process->round);
    if (harq_process->round >= dlsch->Mdlharq) {
      harq_process->status = SCH_IDLE;
      harq_process->round  = 0;
    }
/*    if(is_crnti)
    {
    LOG_D(PHY,"[UE %d] DLSCH: Setting NACK for subframe %d (pid %d, pid status %d, round %d/Max %d, TBS %d)\n",
               phy_vars_ue->Mod_id,subframe,harq_pid,harq_process->status,harq_process->round,dlsch->Mdlharq,harq_process->TBS);
    }*/


    //return((1+dlsch->max_ldpc_iterations));
  } else {
#if UE_DEBUG_TRACE
      LOG_I(PHY,"[UE %d] THREAD 0 DLSCH: Setting ACK for subframe %d TBS %d mcs %d nb_rb %d\n",
           phy_vars_ue->Mod_id,subframe,harq_process->TBS,harq_process->mcs,harq_process->nb_rb);
#endif

    harq_process->status = SCH_IDLE;
    harq_process->round  = 0;
    dlsch->harq_ack[subframe].ack = 1;
    dlsch->harq_ack[subframe].harq_id = harq_pid;
    dlsch->harq_ack[subframe].send_harq_status = 1;
    //LOG_I(PHY,"[UE %d] DLSCH: Setting ACK for SFN/SF %d/%d (pid %d, status %d, round %d, TBS %d, mcs %d)\n",
      //  phy_vars_ue->Mod_id, frame, subframe, harq_pid, harq_process->status, harq_process->round,harq_process->TBS,harq_process->mcs);

/*    if(is_crnti)
    {
    LOG_D(PHY,"[UE %d] DLSCH: Setting ACK for subframe %d (pid %d, round %d, TBS %d)\n",phy_vars_ue->Mod_id,subframe,harq_pid,harq_process->round,harq_process->TBS);
    }
    LOG_D(PHY,"[UE %d] DLSCH: Setting ACK for subframe %d (pid %d, round %d)\n",phy_vars_ue->Mod_id,subframe,harq_pid,harq_process->round);

  }*/

  // Reassembly of Transport block here
  offset = 0;

  /*
  printf("harq_pid %d\n",harq_pid);
  printf("F %d, Fbytes %d\n",harq_process->F,harq_process->F>>3);
  printf("C %d\n",harq_process->C);
  */
  for (r=0; r<harq_process->C; r++) {
    if (r<harq_process->Cminus)
      Kr = harq_process->Kminus;
    else
      Kr = harq_process->Kplus;

    Kr_bytes = Kr>>3;

    //    printf("Segment %d : Kr= %d bytes\n",r,Kr_bytes);
    if (r==0) {
      memcpy(harq_process->b,
             &harq_process->c[0][(harq_process->F>>3)],
             Kr_bytes - (harq_process->F>>3)- ((harq_process->C>1)?3:0));
      offset = Kr_bytes - (harq_process->F>>3) - ((harq_process->C>1)?3:0);
      //            printf("copied %d bytes to b sequence (harq_pid %d)\n",
      //          Kr_bytes - (harq_process->F>>3),harq_pid);
      //          printf("b[0] = %x,c[%d] = %x\n",
      //      harq_process->b[0],
      //      harq_process->F>>3,
      //      harq_process->c[0][(harq_process->F>>3)]);
    } else {
      memcpy(harq_process->b+offset,
             harq_process->c[r],
             Kr_bytes- ((harq_process->C>1)?3:0));
      offset += (Kr_bytes - ((harq_process->C>1)?3:0));
    }
  }

  dlsch->last_iteration_cnt = ret;

  //return(ret);
  }
#endif

  proc->decoder_thread_available = 1;
  //proc->decoder_main_available = 0;
  
  if (pthread_mutex_lock(&proc->mutex_dlsch_td) != 0) {
              LOG_E( PHY, "[SCHED][UE] error locking mutex for UE RXTX\n" );
              exit_fun("noting to add");
          }
          proc->instance_cnt_dlsch_td--;
          if (pthread_mutex_unlock(&proc->mutex_dlsch_td) != 0) {
              LOG_E( PHY, "[SCHED][UE] error unlocking mutex for UE td1\n" );
              exit_fun("noting to add");
          }
      }

      // thread finished
          free(arg);
          return &UE_dlsch_td_retval;
}
#endif

#ifdef UE_DLSCH_PARALLELISATION
#define FIFO_PRIORITY   39
1768
void *nr_dlsch_decoding_2thread1(void *arg)
1769 1770
{
	static __thread int UE_dlsch_td_retval1;
1771 1772
    struct nr_rxtx_thread_data *rtd = arg;
    UE_nr_rxtx_proc_t *proc = rtd->proc;
1773
    PHY_VARS_NR_UE    *phy_vars_ue   = rtd->UE;
1774
    NR_DL_FRAME_PARMS *frame_parms = &phy_vars_ue->frame_parms;
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785

    int llr8_flag1;
    int32_t no_iteration_ldpc;
    t_nrLDPC_dec_params decParams;
    t_nrLDPC_dec_params* p_decParams = &decParams;
    t_nrLDPC_time_stats procTime;
    t_nrLDPC_time_stats* p_procTime =&procTime ;
    int16_t z [68*384];
    int8_t l [68*384];
    //__m128i l;
    int16_t inv_d [68*384];
1786 1787 1788 1789 1790 1791 1792 1793
    //int16_t *p_invd =&inv_d;
    uint8_t kb, kc;
    uint8_t Ilbrm = 0;
    uint32_t Tbslbrm = 950984;
    uint16_t nb_rb = 30; //to update
    uint16_t nb_symb_sch = 12;
    uint8_t nb_re_dmrs = 6;
    uint16_t length_dmrs = 1;
1794 1795

    uint32_t i,j;
1796
    //uint32_t k;
1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825

    __m128i *pv = (__m128i*)&z;
    __m128i *pl = (__m128i*)&l;

    proc->instance_cnt_dlsch_td1=-1;
    proc->nr_tti_rx=proc->sub_frame_start;

    printf("start thread 1\n");
    proc->decoder_thread_available1 = 0;

    char threadname[256];
    sprintf(threadname,"UE_thread_dlsch_td1_%d", proc->sub_frame_start);

	cpu_set_t cpuset;
    CPU_ZERO(&cpuset);
    if ( (proc->sub_frame_start+1)%RX_NB_TH == 0 && threads.dlsch_td_one != -1 )
    	CPU_SET(threads.dlsch_td_one, &cpuset);
    if ( (proc->sub_frame_start+1)%RX_NB_TH == 1 && threads.dlsch_td_two != -1 )
    	CPU_SET(threads.dlsch_td_two, &cpuset);
    if ( (proc->sub_frame_start+1)%RX_NB_TH == 2 && threads.dlsch_td_three != -1 )
    	CPU_SET(threads.dlsch_td_three, &cpuset);



#if UE_TIMING_TRACE
  time_stats_t *dlsch_rate_unmatching_stats=&phy_vars_ue->dlsch_rate_unmatching_stats;
  time_stats_t *dlsch_turbo_decoding_stats=&phy_vars_ue->dlsch_turbo_decoding_stats;
  time_stats_t *dlsch_deinterleaving_stats=&phy_vars_ue->dlsch_deinterleaving_stats;
#endif
1826 1827 1828 1829 1830 1831 1832 1833 1834
    uint32_t A,E;
    uint32_t G;
    uint32_t ret,offset;
    uint32_t r,r_offset=0,Kr,Kr_bytes,err_flag=0,K_bytes_F;
    uint8_t crc_type;
  	uint8_t C,Cprime;
    uint8_t Qm;
   	uint8_t Nl;
    //uint32_t Er;
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860

	init_thread(900000,1000000 , FIFO_PRIORITY-1, &cpuset, threadname);
	printf("2thread1 oai_exit %d\n", oai_exit);
	while (!oai_exit) {
	        if (pthread_mutex_lock(&proc->mutex_dlsch_td1) != 0) {
	            LOG_E( PHY, "[SCHED][UE] error locking mutex for UE dlsch td\n" );
	            exit_fun("nothing to add");
	        }
	        while (proc->instance_cnt_dlsch_td1 < 0) {
	            // most of the time, the thread is waiting here
	            pthread_cond_wait( &proc->cond_dlsch_td1, &proc->mutex_dlsch_td1 );
	        }
	        if (pthread_mutex_unlock(&proc->mutex_dlsch_td1) != 0) {
	            LOG_E( PHY, "[SCHED][UE] error unlocking mutex for UE dlsch_td \n" );
	            exit_fun("nothing to add");
	        }

	        //printf("2thread1 main available %d\n", proc->decoder_main_available);

	        uint32_t wait = 0;
	        	          while(proc->decoder_main_available == 0)
	        	          {
	        	                  usleep(1);
	        	                  wait++;
	        	          }
	        	
1861
	        //proc->decoder_thread_available1 = 0;
1862
	        //PHY_VARS_NR_UE *phy_vars_ue   		= tdp->UE;
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
	        int eNB_id         				= proc->eNB_id;
	        int harq_pid      				= proc->harq_pid;
	        llr8_flag1     					= proc->llr8_flag;
	        //r_offset						= proc->Er;
	        //UE_rxtx_proc_t *proc    		= tdp->proc;
	        int frame                       = proc->frame_rx;
	        int subframe      				= proc->nr_tti_rx;
	        NR_UE_DLSCH_t *dlsch 			= phy_vars_ue->dlsch[phy_vars_ue->current_thread_id[subframe]][eNB_id][0];
	        NR_DL_UE_HARQ_t *harq_process  = dlsch->harq_processes[harq_pid];
	        short *dlsch_llr 				= phy_vars_ue->pdsch_vars[phy_vars_ue->current_thread_id[subframe]][eNB_id]->llr[0];
	        //printf("2thread0 llr flag %d tdp flag %d\n",llr8_flag1, tdp->llr8_flag);
	        //printf("2thread1 nr_tti_tx %d subframe %d SF thread id %d r_offset %d\n", proc->nr_tti_rx, subframe, phy_vars_ue->current_thread_id[subframe], r_offset);
1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888

  /*
  if (nb_rb > frame_parms->N_RB_DL) {
    printf("dlsch_decoding.c: Illegal nb_rb %d\n",nb_rb);
    return(max_ldpc_iterations);
    }*/

  /*harq_pid = dlsch->current_harq_pid[phy_vars_ue->current_thread_id[subframe]];
  if (harq_pid >= 8) {
    printf("dlsch_decoding.c: Illegal harq_pid %d\n",harq_pid);
    return(max_ldpc_iterations);
  }
  */

1889
  nb_rb = harq_process->nb_rb;
1890 1891

  harq_process->trials[harq_process->round]++;
1892
  harq_process->TBS = nr_compute_tbs(harq_process->mcs,nb_rb,nb_symb_sch,nb_re_dmrs,length_dmrs, harq_process->Nl);
1893 1894 1895 1896 1897

  A = harq_process->TBS; //2072 for QPSK 1/3

  ret = dlsch->max_ldpc_iterations;

1898
  harq_process->G = nr_get_G(nb_rb, nb_symb_sch, nb_re_dmrs, length_dmrs, harq_process->Qm,harq_process->Nl);
1899 1900 1901 1902 1903 1904 1905 1906 1907

  G = harq_process->G;
  //get_G(frame_parms,nb_rb,dlsch->rb_alloc,mod_order,num_pdcch_symbols,phy_vars_ue->frame,subframe);

    //printf("DLSCH Decoding,  A %d harq_pid %d G %d\n",A, harq_pid,harq_process->G);

  if (harq_process->round == 0) {
    // This is a new packet, so compute quantities regarding segmentation
    harq_process->B = A+24;
1908 1909 1910 1911 1912 1913 1914 1915
    nr_segmentation(NULL,
        	          NULL,
        	          harq_process->B,
        	          &harq_process->C,
        	          &harq_process->K,
         		  &harq_process->Z,
        	          &harq_process->F);
        			  p_decParams->Z = harq_process->Z;
1916 1917 1918

    }

1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933
    kb = harq_process->K/harq_process->Z;
  	  if ( kb==22){
  		  p_decParams->BG = 1;
  		  p_decParams->R = 89;
  		  kc = 68;
  	  }
  	  else{
  		  p_decParams->BG = 2;
  		  p_decParams->R = 13;
  		  kc = 52;
  	  	  }

    p_decParams->numMaxIter = 2;
    Kr = p_decParams->Z*kb;
    p_decParams->outMode= 0;
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946
  /*
  else {
    printf("dlsch_decoding.c: Ndi>0 not checked yet!!\n");
    return(max_ldpc_iterations);
  }
  */
  err_flag = 0;
  //r_offset = 0;

  /*
  unsigned char bw_scaling =1;

  switch (frame_parms->N_RB_DL) {
1947
  case 106:
1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
    bw_scaling =2;
    break;

  default:
    bw_scaling =1;
    break;
  }

  if (harq_process->C > MAX_NUM_DLSCH_SEGMENTS/bw_scaling) {
    LOG_E(PHY,"Illegal harq_process->C %d > %d\n",harq_process->C,MAX_NUM_DLSCH_SEGMENTS/bw_scaling);
    return((1+dlsch->max_ldpc_iterations));
  }*/
#ifdef DEBUG_DLSCH_DECODING
  printf("Segmentation: C %d, Cminus %d, Kminus %d, Kplus %d\n",harq_process->C,harq_process->Cminus,harq_process->Kminus,harq_process->Kplus);
#endif

  opp_enabled=1;
  
  Qm= harq_process->Qm;
1967 1968 1969
  Nl=harq_process->Nl;
  //r_thread = harq_process->C/2-1;
  C= harq_process->C;
1970

1971
  Cprime = C; //assume CBGTI not present
1972

1973 1974 1975 1976
  if (r <= Cprime - ((G/(Nl*Qm))%Cprime) - 1)
	  r_offset = Nl*Qm*(G/(Nl*Qm*Cprime));
  else
	  r_offset = Nl*Qm*((G/(Nl*Qm*Cprime))+1);
1977 1978 1979 1980 1981 1982 1983 1984 1985

  	       //printf("sub thread r_offset %d\n", r_offset);

  //for (r=(harq_process->C/2); r<harq_process->C; r++) {
  	     r=2; //(harq_process->C/2);
  	   r_offset = r*r_offset;
	 //printf("thread1 r=%d r_offset %d \n",r, r_offset);


1986 1987 1988
  	  Kr = harq_process->K;
  	  Kr_bytes = Kr>>3;
  	  K_bytes_F = Kr_bytes-(harq_process->F>>3);
1989

1990
  	  Tbslbrm = nr_compute_tbs(28,nb_rb,frame_parms->symbols_per_slot,0,0, harq_process->Nl);
1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010

#if UE_TIMING_TRACE
    start_meas(dlsch_rate_unmatching_stats);
#endif

#ifdef DEBUG_DLSCH_DECODING
    LOG_D(PHY,"HARQ_PID %d Rate Matching Segment %d (coded bits %d,unpunctured/repeated bits %d, TBS %d, mod_order %d, nb_rb %d, Nl %d, rv %d, round %d)...\n",
          harq_pid,r, G,
          Kr*3,
          harq_process->TBS,
          harq_process->Qm,
          harq_process->nb_rb,
          harq_process->Nl,
          harq_process->rvidx,
          harq_process->round);
#endif

#ifdef DEBUG_DLSCH_DECODING
    printf(" in decoding dlsch->harq_processes[harq_pid]->rvidx = %d\n", dlsch->harq_processes[harq_pid]->rvidx);
#endif
2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024
    if (nr_rate_matching_ldpc_rx(Ilbrm,
     	 	 	 	 Tbslbrm,
				 p_decParams->BG,
				 p_decParams->Z,
    				 G,
				 harq_process->w[r],
				 dlsch_llr+r_offset,
				 harq_process->C,
				 harq_process->rvidx,
				 (harq_process->round==0)?1:0,
				 harq_process->Qm,
				 harq_process->Nl,
				 r,
				 &E)==-1) {
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045
#if UE_TIMING_TRACE
      stop_meas(dlsch_rate_unmatching_stats);
#endif
      LOG_E(PHY,"dlsch_decoding.c: Problem in rate_matching\n");
      //return(dlsch->max_ldpc_iterations);
    }