ulsch_decoding.c 54.1 KB
Newer Older
1 2 3 4 5
/*
 * 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
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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
 */
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

/*! \file PHY/LTE_TRANSPORT/ulsch_decoding.c
* \brief Top-level routines for decoding  the ULSCH transport channel from 36.212 V8.6 2009-03
* \author R. Knopp
* \date 2011
* \version 0.1
* \company Eurecom
* \email: knopp@eurecom.fr
* \note
* \warning
*/

//#include "defs.h"

#include "PHY/defs.h"
#include "PHY/extern.h"
#include "PHY/CODING/extern.h"
#include "extern.h"
#include "SCHED/extern.h"
#ifdef OPENAIR2
#include "LAYER2/MAC/defs.h"
#include "LAYER2/MAC/extern.h"
#include "RRC/LITE/extern.h"
#include "PHY_INTERFACE/extern.h"
#endif

#ifdef PHY_ABSTRACTION
#include "UTIL/OCG/OCG.h"
#include "UTIL/OCG/OCG_extern.h"
#endif
51

52 53 54
#include "UTIL/LOG/vcd_signal_dumper.h"
//#define DEBUG_ULSCH_DECODING

55 56
void free_eNB_ulsch(LTE_eNB_ULSCH_t *ulsch)
{
57 58 59 60

  int i,r;

  if (ulsch) {
61
    for (i=0; i<8; i++) {
62
      if (ulsch->harq_processes[i]) {
63 64 65 66 67 68
        if (ulsch->harq_processes[i]->b) {
          free16(ulsch->harq_processes[i]->b,MAX_ULSCH_PAYLOAD_BYTES);
          ulsch->harq_processes[i]->b = NULL;
        }

        for (r=0; r<MAX_NUM_ULSCH_SEGMENTS; r++) {
69 70 71
          free16(ulsch->harq_processes[i]->c[r],((r==0)?8:0) + 768);
          ulsch->harq_processes[i]->c[r] = NULL;
        }
72 73 74 75 76 77 78 79 80

        for (r=0; r<MAX_NUM_ULSCH_SEGMENTS; r++)
          if (ulsch->harq_processes[i]->d[r]) {
            free16(ulsch->harq_processes[i]->d[r],((3*8*6144)+12+96)*sizeof(short));
            ulsch->harq_processes[i]->d[r] = NULL;
          }

        free16(ulsch->harq_processes[i],sizeof(LTE_UL_eNB_HARQ_t));
        ulsch->harq_processes[i] = NULL;
81 82
      }
    }
83 84 85

    free16(ulsch,sizeof(LTE_eNB_ULSCH_t));
    ulsch = NULL;
86 87 88
  }
}

89
LTE_eNB_ULSCH_t *new_eNB_ulsch(uint8_t max_turbo_iterations,uint8_t N_RB_UL, uint8_t abstraction_flag)
90
{
91 92 93 94

  LTE_eNB_ULSCH_t *ulsch;
  uint8_t exit_flag = 0,i,r;
  unsigned char bw_scaling =1;
95 96 97

  switch (N_RB_UL) {
  case 6:
98 99
    bw_scaling =16;
    break;
100

101 102 103
  case 25:
    bw_scaling =4;
    break;
104 105

  case 50:
106 107
    bw_scaling =2;
    break;
108

109 110 111 112
  default:
    bw_scaling =1;
    break;
  }
113

114
  ulsch = (LTE_eNB_ULSCH_t *)malloc16(sizeof(LTE_eNB_ULSCH_t));
115

116
  if (ulsch) {
117
    memset(ulsch,0,sizeof(LTE_eNB_ULSCH_t));
118
    ulsch->max_turbo_iterations = max_turbo_iterations;
119
    ulsch->Mlimit = 4;
120

121
    for (i=0; i<8; i++) {
Raymond Knopp's avatar
Raymond Knopp committed
122
      //      printf("new_ue_ulsch: Harq process %d\n",i);
123
      ulsch->harq_processes[i] = (LTE_UL_eNB_HARQ_t *)malloc16(sizeof(LTE_UL_eNB_HARQ_t));
124

125
      if (ulsch->harq_processes[i]) {
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
        memset(ulsch->harq_processes[i],0,sizeof(LTE_UL_eNB_HARQ_t));
        ulsch->harq_processes[i]->b = (uint8_t*)malloc16(MAX_ULSCH_PAYLOAD_BYTES/bw_scaling);

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

        if (abstraction_flag==0) {
          for (r=0; r<MAX_NUM_ULSCH_SEGMENTS/bw_scaling; r++) {
            ulsch->harq_processes[i]->c[r] = (uint8_t*)malloc16(((r==0)?8:0) + 3+768);
            if (ulsch->harq_processes[i]->c[r])
              memset(ulsch->harq_processes[i]->c[r],0,((r==0)?8:0) + 3+768);
            else
              exit_flag=2;

            ulsch->harq_processes[i]->d[r] = (short*)malloc16(((3*8*6144)+12+96)*sizeof(short));

            if (ulsch->harq_processes[i]->d[r])
              memset(ulsch->harq_processes[i]->d[r],0,((3*8*6144)+12+96)*sizeof(short));
            else
              exit_flag=2;
          }
        }
      } else {
        exit_flag=1;
152 153 154 155 156 157
      }
    }

    if (exit_flag==0)
      return(ulsch);
  }
158

159 160 161 162 163 164
  LOG_E(PHY,"new_ue_ulsch: exit_flag = %d\n",exit_flag);
  free_eNB_ulsch(ulsch);

  return(NULL);
}

165
void clean_eNb_ulsch(LTE_eNB_ULSCH_t *ulsch)
166
{
167 168 169 170 171 172

  unsigned char i;

  //ulsch = (LTE_eNB_ULSCH_t *)malloc16(sizeof(LTE_eNB_ULSCH_t));
  if (ulsch) {
    ulsch->rnti = 0;
173

174
    for (i=0; i<8; i++) {
175
      if (ulsch->harq_processes[i]) {
176 177 178 179 180
        //    ulsch->harq_processes[i]->Ndi = 0;
        ulsch->harq_processes[i]->status = 0;
        //ulsch->harq_processes[i]->phich_active = 0; //this will be done later after transmission of PHICH
        ulsch->harq_processes[i]->phich_ACK = 0;
        ulsch->harq_processes[i]->round = 0;
181 182 183 184 185 186 187
      }
    }

  }
}


188 189
uint8_t extract_cqi_crc(uint8_t *cqi,uint8_t CQI_LENGTH)
{
190 191 192 193

  uint8_t crc;

  crc = cqi[CQI_LENGTH>>3];
Raymond Knopp's avatar
Raymond Knopp committed
194
  //  printf("crc1: %x, shift %d\n",crc,CQI_LENGTH&0x7);
195 196 197
  crc = (crc<<(CQI_LENGTH&0x7));
  // clear crc bits
  //  ((char *)cqi)[CQI_LENGTH>>3] &= 0xff>>(8-(CQI_LENGTH&0x7));
Raymond Knopp's avatar
Raymond Knopp committed
198
  //  printf("crc2: %x, cqi0 %x\n",crc,cqi[1+(CQI_LENGTH>>3)]);
199 200 201 202 203 204 205 206 207 208 209
  crc |= (cqi[1+(CQI_LENGTH>>3)])>>(8-(CQI_LENGTH&0x7));
  // clear crc bits
  //(((char *)cqi)[1+(CQI_LENGTH>>3)]) = 0;

  //  printf("crc : %x\n",crc);
  return(crc);

}



210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226



int ulsch_decoding_data_2thread0(td_params* tdp) {

  PHY_VARS_eNB *eNB = tdp->eNB;
  int UE_id         = tdp->UE_id;
  int harq_pid      = tdp->harq_pid;
  int llr8_flag     = tdp->llr8_flag;

  unsigned int r,r_offset=0,Kr,Kr_bytes,iind;
  uint8_t crc_type;
  int offset = 0;
  int ret = 1;
  int16_t dummy_w[MAX_NUM_ULSCH_SEGMENTS][3*(6144+64)];
  LTE_eNB_ULSCH_t *ulsch = eNB->ulsch[UE_id];
  LTE_UL_eNB_HARQ_t *ulsch_harq = ulsch->harq_processes[harq_pid];
227
  int Q_m = ulsch_harq->Qm;
228
  int G = ulsch_harq->G;
Raymond Knopp's avatar
Raymond Knopp committed
229
  uint32_t E=0;
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
  uint32_t Gp,GpmodC,Nl=1;
  uint32_t C = ulsch_harq->C;

  uint8_t (*tc)(int16_t *y,
                uint8_t *,
                uint16_t,
                uint16_t,
                uint16_t,
                uint8_t,
                uint8_t,
                uint8_t,
                time_stats_t *,
                time_stats_t *,
                time_stats_t *,
                time_stats_t *,
                time_stats_t *,
                time_stats_t *,
                time_stats_t *);

  if (llr8_flag == 0)
    tc = phy_threegpplte_turbo_decoder16;
  else
    tc = phy_threegpplte_turbo_decoder8;



  // go through first half of segments to get r_offset
  for (r=0; r<(ulsch_harq->C/2); r++) {

    // Get Turbo interleaver parameters
    if (r<ulsch_harq->Cminus)
      Kr = ulsch_harq->Kminus;
    else
      Kr = ulsch_harq->Kplus;

    Kr_bytes = Kr>>3;

    if (Kr_bytes<=64)
      iind = (Kr_bytes-5);
    else if (Kr_bytes <=128)
      iind = 59 + ((Kr_bytes-64)>>1);
    else if (Kr_bytes <= 256)
      iind = 91 + ((Kr_bytes-128)>>2);
    else if (Kr_bytes <= 768)
      iind = 123 + ((Kr_bytes-256)>>3);
    else {
Raymond Knopp's avatar
Raymond Knopp committed
276
      AssertFatal(1==0,"ulsch_decoding: Illegal codeword size %d!!!\n",Kr_bytes);
277 278 279 280 281 282 283 284 285 286 287 288 289
    }

    // This is stolen from rate-matching algorithm to get the value of E
    
    Gp = G/Nl/Q_m;
    GpmodC = Gp%C;
    
    if (r < (C-(GpmodC)))
      E = Nl*Q_m * (Gp/C);
    else
      E = Nl*Q_m * ((GpmodC==0?0:1) + (Gp/C));
    
    r_offset += E;
290 291 292 293 294 295

    if (r==0) {
      offset = Kr_bytes - (ulsch_harq->F>>3) - ((ulsch_harq->C>1)?3:0);
    } else {
      offset += (Kr_bytes- ((ulsch_harq->C>1)?3:0));
    }
296 297 298
  }

  // go through second half of segments
299
  for (; r<(ulsch_harq->C); r++) {
300 301 302 303 304 305 306 307 308 309 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 350 351 352 353


    //    printf("before subblock deinterleaving c[%d] = %p\n",r,ulsch_harq->c[r]);
    // Get Turbo interleaver parameters
    if (r<ulsch_harq->Cminus)
      Kr = ulsch_harq->Kminus;
    else
      Kr = ulsch_harq->Kplus;

    Kr_bytes = Kr>>3;

    if (Kr_bytes<=64)
      iind = (Kr_bytes-5);
    else if (Kr_bytes <=128)
      iind = 59 + ((Kr_bytes-64)>>1);
    else if (Kr_bytes <= 256)
      iind = 91 + ((Kr_bytes-128)>>2);
    else if (Kr_bytes <= 768)
      iind = 123 + ((Kr_bytes-256)>>3);
    else {
      LOG_E(PHY,"ulsch_decoding: Illegal codeword size %d!!!\n",Kr_bytes);
      return(-1);
    }

#ifdef DEBUG_ULSCH_DECODING
    printf("f1 %d, f2 %d, F %d\n",f1f2mat_old[2*iind],f1f2mat_old[1+(2*iind)],(r==0) ? ulsch_harq->F : 0);
#endif

    memset(&dummy_w[r][0],0,3*(6144+64)*sizeof(short));
    ulsch_harq->RTC[r] = generate_dummy_w(4+(Kr_bytes*8),
                                          (uint8_t*)&dummy_w[r][0],
                                          (r==0) ? ulsch_harq->F : 0);

#ifdef DEBUG_ULSCH_DECODING
    printf("Rate Matching Segment %d (coded bits (G) %d,unpunctured/repeated bits %d, Q_m %d, nb_rb %d, Nl %d)...\n",
        r, G,
        Kr*3,
        Q_m,
        nb_rb,
        ulsch_harq->Nl);
#endif


    if (lte_rate_matching_turbo_rx(ulsch_harq->RTC[r],
                                   G,
                                   ulsch_harq->w[r],
                                   (uint8_t*) &dummy_w[r][0],
                                   ulsch_harq->e+r_offset,
                                   ulsch_harq->C,
                                   NSOFT,
                                   0,   //Uplink
                                   1,
                                   ulsch_harq->rvidx,
                                   (ulsch_harq->round==0)?1:0,  // clear
354
                                   ulsch_harq->Qm,
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
                                   1,
                                   r,
                                   &E)==-1) {
      LOG_E(PHY,"ulsch_decoding.c: Problem in rate matching\n");
      return(-1);
    }

    r_offset += E;

    sub_block_deinterleaving_turbo(4+Kr,
                                   &ulsch_harq->d[r][96],
                                   ulsch_harq->w[r]);

    if (ulsch_harq->C == 1)
      crc_type = CRC24_A;
    else
      crc_type = CRC24_B;
    
    
    ret = tc(&ulsch_harq->d[r][96],
	     ulsch_harq->c[r],
	     Kr,
	     f1f2mat_old[iind*2],
	     f1f2mat_old[(iind*2)+1],
	     ulsch->max_turbo_iterations,//MAX_TURBO_ITERATIONS,
	     crc_type,
	     (r==0) ? ulsch_harq->F : 0,
	     &eNB->ulsch_tc_init_stats,
	     &eNB->ulsch_tc_alpha_stats,
	     &eNB->ulsch_tc_beta_stats,
	     &eNB->ulsch_tc_gamma_stats,
	     &eNB->ulsch_tc_ext_stats,
	     &eNB->ulsch_tc_intl1_stats,
	     &eNB->ulsch_tc_intl2_stats);
    
390
    // Reassembly of Transport block here
391 392 393 394 395 396 397 398 399

    if (ret != (1+ulsch->max_turbo_iterations)) {
      if (r<ulsch_harq->Cminus)
	Kr = ulsch_harq->Kminus;
      else
	Kr = ulsch_harq->Kplus;
      
      Kr_bytes = Kr>>3;
      
400 401 402 403 404
      memcpy(ulsch_harq->b+offset,
	     ulsch_harq->c[r],
	     Kr_bytes - ((ulsch_harq->C>1)?3:0));
      offset += (Kr_bytes- ((ulsch_harq->C>1)?3:0));
      
405 406 407 408 409 410 411 412 413 414 415 416
      
    } else {
      break;
    }
    
  }

  return(ret);
}

extern int oai_exit;
void *td_thread(void *param) {
417
  pthread_setname_np( pthread_self(), "td processing");
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
  PHY_VARS_eNB *eNB = ((td_params*)param)->eNB;
  eNB_proc_t *proc  = &eNB->proc;

  while (!oai_exit) {

    if (wait_on_condition(&proc->mutex_td,&proc->cond_td,&proc->instance_cnt_td,"td thread")<0) break;  

    ((td_params*)param)->ret = ulsch_decoding_data_2thread0((td_params*)param);

    if (release_thread(&proc->mutex_td,&proc->instance_cnt_td,"td thread")<0) break;

    if (pthread_cond_signal(&proc->cond_td) != 0) {
      printf("[eNB] ERROR pthread_cond_signal for td thread exit\n");
      exit_fun( "ERROR pthread_cond_signal" );
      return(NULL);
    }
  }

  return(NULL);
}

int ulsch_decoding_data_2thread(PHY_VARS_eNB *eNB,int UE_id,int harq_pid,int llr8_flag) {

  eNB_proc_t *proc = &eNB->proc;
  unsigned int r,r_offset=0,Kr,Kr_bytes,iind;
  uint8_t crc_type;
  int offset = 0;
  int ret = 1;
  int16_t dummy_w[MAX_NUM_ULSCH_SEGMENTS][3*(6144+64)];
  LTE_eNB_ULSCH_t *ulsch = eNB->ulsch[UE_id];
  LTE_UL_eNB_HARQ_t *ulsch_harq = ulsch->harq_processes[harq_pid];
449

450
  int G = ulsch_harq->G;
451
  unsigned int E;
452
  int Cby2;
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480

  uint8_t (*tc)(int16_t *y,
                uint8_t *,
                uint16_t,
                uint16_t,
                uint16_t,
                uint8_t,
                uint8_t,
                uint8_t,
                time_stats_t *,
                time_stats_t *,
                time_stats_t *,
                time_stats_t *,
                time_stats_t *,
                time_stats_t *,
                time_stats_t *);

  struct timespec wait;

  wait.tv_sec=0;
  wait.tv_nsec=5000000L;


  if (llr8_flag == 0)
    tc = phy_threegpplte_turbo_decoder16;
  else
    tc = phy_threegpplte_turbo_decoder8;

481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
  if (ulsch_harq->C>1) { // wakeup worker if more than 1 segment
    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("[eNB] TD thread busy\n");
      exit_fun("TD thread busy");
      pthread_mutex_unlock( &proc->mutex_td );
      return -1;
    }
    
    ++proc->instance_cnt_td;
    
    proc->tdp.eNB       = eNB;
    proc->tdp.UE_id     = UE_id;
    proc->tdp.harq_pid  = harq_pid;
    proc->tdp.llr8_flag = llr8_flag;
    
    
    // wakeup worker to do second half segments 
    if (pthread_cond_signal(&proc->cond_td) != 0) {
      printf("[eNB] ERROR pthread_cond_signal for td thread exit\n");
      exit_fun( "ERROR pthread_cond_signal" );
      return (1+ulsch->max_turbo_iterations);
    }
509 510

    pthread_mutex_unlock( &proc->mutex_td );
511
    Cby2 = ulsch_harq->C/2;
512
  }
513 514
  else {
    Cby2 = 1;
515 516 517
  }

  // go through first half of segments in main thread
518
  for (r=0; r<Cby2; r++) {
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572

    //    printf("before subblock deinterleaving c[%d] = %p\n",r,ulsch_harq->c[r]);
    // Get Turbo interleaver parameters
    if (r<ulsch_harq->Cminus)
      Kr = ulsch_harq->Kminus;
    else
      Kr = ulsch_harq->Kplus;

    Kr_bytes = Kr>>3;

    if (Kr_bytes<=64)
      iind = (Kr_bytes-5);
    else if (Kr_bytes <=128)
      iind = 59 + ((Kr_bytes-64)>>1);
    else if (Kr_bytes <= 256)
      iind = 91 + ((Kr_bytes-128)>>2);
    else if (Kr_bytes <= 768)
      iind = 123 + ((Kr_bytes-256)>>3);
    else {
      LOG_E(PHY,"ulsch_decoding: Illegal codeword size %d!!!\n",Kr_bytes);
      return(-1);
    }

#ifdef DEBUG_ULSCH_DECODING
    printf("f1 %d, f2 %d, F %d\n",f1f2mat_old[2*iind],f1f2mat_old[1+(2*iind)],(r==0) ? ulsch_harq->F : 0);
#endif

    memset(&dummy_w[r][0],0,3*(6144+64)*sizeof(short));
    ulsch_harq->RTC[r] = generate_dummy_w(4+(Kr_bytes*8),
                                          (uint8_t*)&dummy_w[r][0],
                                          (r==0) ? ulsch_harq->F : 0);

#ifdef DEBUG_ULSCH_DECODING
    printf("Rate Matching Segment %d (coded bits (G) %d,unpunctured/repeated bits %d, Q_m %d, nb_rb %d, Nl %d)...\n",
        r, G,
        Kr*3,
        Q_m,
        nb_rb,
        ulsch_harq->Nl);
#endif

    start_meas(&eNB->ulsch_rate_unmatching_stats);

    if (lte_rate_matching_turbo_rx(ulsch_harq->RTC[r],
                                   G,
                                   ulsch_harq->w[r],
                                   (uint8_t*) &dummy_w[r][0],
                                   ulsch_harq->e+r_offset,
                                   ulsch_harq->C,
                                   NSOFT,
                                   0,   //Uplink
                                   1,
                                   ulsch_harq->rvidx,
                                   (ulsch_harq->round==0)?1:0,  // clear
573
                                   ulsch_harq->Qm,
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
                                   1,
                                   r,
                                   &E)==-1) {
      LOG_E(PHY,"ulsch_decoding.c: Problem in rate matching\n");
      return(-1);
    }

    stop_meas(&eNB->ulsch_rate_unmatching_stats);
    r_offset += E;

    start_meas(&eNB->ulsch_deinterleaving_stats);
    sub_block_deinterleaving_turbo(4+Kr,
                                   &ulsch_harq->d[r][96],
                                   ulsch_harq->w[r]);
    stop_meas(&eNB->ulsch_deinterleaving_stats);

    if (ulsch_harq->C == 1)
      crc_type = CRC24_A;
    else
      crc_type = CRC24_B;

    start_meas(&eNB->ulsch_turbo_decoding_stats);
    
    ret = tc(&ulsch_harq->d[r][96],
	     ulsch_harq->c[r],
	     Kr,
	     f1f2mat_old[iind*2],
	     f1f2mat_old[(iind*2)+1],
	     ulsch->max_turbo_iterations,//MAX_TURBO_ITERATIONS,
	     crc_type,
	     (r==0) ? ulsch_harq->F : 0,
	     &eNB->ulsch_tc_init_stats,
	     &eNB->ulsch_tc_alpha_stats,
	     &eNB->ulsch_tc_beta_stats,
	     &eNB->ulsch_tc_gamma_stats,
	     &eNB->ulsch_tc_ext_stats,
	     &eNB->ulsch_tc_intl1_stats,
	     &eNB->ulsch_tc_intl2_stats);
612

613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
  // Reassembly of Transport block here

    if (ret != (1+ulsch->max_turbo_iterations)) {
      if (r<ulsch_harq->Cminus)
	Kr = ulsch_harq->Kminus;
      else
	Kr = ulsch_harq->Kplus;
      
      Kr_bytes = Kr>>3;
      
      if (r==0) {
	memcpy(ulsch_harq->b,
	       &ulsch_harq->c[0][(ulsch_harq->F>>3)],
	       Kr_bytes - (ulsch_harq->F>>3) - ((ulsch_harq->C>1)?3:0));
	offset = Kr_bytes - (ulsch_harq->F>>3) - ((ulsch_harq->C>1)?3:0);
      } else {
	memcpy(ulsch_harq->b+offset,
	       ulsch_harq->c[r],
	       Kr_bytes - ((ulsch_harq->C>1)?3:0));
	offset += (Kr_bytes- ((ulsch_harq->C>1)?3:0));
      }
      
    } else {
      break;
    }
    stop_meas(&eNB->ulsch_turbo_decoding_stats);    
  }

   // wait for worker to finish

  wait_on_busy_condition(&proc->mutex_td,&proc->cond_td,&proc->instance_cnt_td,"td thread");  

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

int ulsch_decoding_data(PHY_VARS_eNB *eNB,int UE_id,int harq_pid,int llr8_flag) {

  unsigned int r,r_offset=0,Kr,Kr_bytes,iind;
  uint8_t crc_type;
  int offset = 0;
  int ret = 1;
  int16_t dummy_w[MAX_NUM_ULSCH_SEGMENTS][3*(6144+64)];
  LTE_eNB_ULSCH_t *ulsch = eNB->ulsch[UE_id];
  LTE_UL_eNB_HARQ_t *ulsch_harq = ulsch->harq_processes[harq_pid];
657

658
  int G = ulsch_harq->G;
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
  unsigned int E;

  uint8_t (*tc)(int16_t *y,
                uint8_t *,
                uint16_t,
                uint16_t,
                uint16_t,
                uint8_t,
                uint8_t,
                uint8_t,
                time_stats_t *,
                time_stats_t *,
                time_stats_t *,
                time_stats_t *,
                time_stats_t *,
                time_stats_t *,
                time_stats_t *);

  if (llr8_flag == 0)
    tc = phy_threegpplte_turbo_decoder16;
  else
    tc = phy_threegpplte_turbo_decoder8;


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

    //    printf("before subblock deinterleaving c[%d] = %p\n",r,ulsch_harq->c[r]);
    // Get Turbo interleaver parameters
    if (r<ulsch_harq->Cminus)
      Kr = ulsch_harq->Kminus;
    else
      Kr = ulsch_harq->Kplus;

    Kr_bytes = Kr>>3;

    if (Kr_bytes<=64)
      iind = (Kr_bytes-5);
    else if (Kr_bytes <=128)
      iind = 59 + ((Kr_bytes-64)>>1);
    else if (Kr_bytes <= 256)
      iind = 91 + ((Kr_bytes-128)>>2);
    else if (Kr_bytes <= 768)
      iind = 123 + ((Kr_bytes-256)>>3);
    else {
      LOG_E(PHY,"ulsch_decoding: Illegal codeword size %d!!!\n",Kr_bytes);
      return(-1);
    }

#ifdef DEBUG_ULSCH_DECODING
    printf("f1 %d, f2 %d, F %d\n",f1f2mat_old[2*iind],f1f2mat_old[1+(2*iind)],(r==0) ? ulsch_harq->F : 0);
#endif

    memset(&dummy_w[r][0],0,3*(6144+64)*sizeof(short));
    ulsch_harq->RTC[r] = generate_dummy_w(4+(Kr_bytes*8),
                                          (uint8_t*)&dummy_w[r][0],
                                          (r==0) ? ulsch_harq->F : 0);

#ifdef DEBUG_ULSCH_DECODING
    printf("Rate Matching Segment %d (coded bits (G) %d,unpunctured/repeated bits %d, Q_m %d, nb_rb %d, Nl %d)...\n",
        r, G,
        Kr*3,
        Q_m,
        nb_rb,
        ulsch_harq->Nl);
#endif

    start_meas(&eNB->ulsch_rate_unmatching_stats);
Raymond Knopp's avatar
Raymond Knopp committed
726

727 728 729 730 731 732 733 734 735 736 737
    if (lte_rate_matching_turbo_rx(ulsch_harq->RTC[r],
                                   G,
                                   ulsch_harq->w[r],
                                   (uint8_t*) &dummy_w[r][0],
                                   ulsch_harq->e+r_offset,
                                   ulsch_harq->C,
                                   NSOFT,
                                   0,   //Uplink
                                   1,
                                   ulsch_harq->rvidx,
                                   (ulsch_harq->round==0)?1:0,  // clear
738
                                   ulsch_harq->Qm,
739 740 741 742 743 744
                                   1,
                                   r,
                                   &E)==-1) {
      LOG_E(PHY,"ulsch_decoding.c: Problem in rate matching\n");
      return(-1);
    }
Raymond Knopp's avatar
Raymond Knopp committed
745

746 747
    stop_meas(&eNB->ulsch_rate_unmatching_stats);
    r_offset += E;
Raymond Knopp's avatar
Raymond Knopp committed
748

749 750 751 752 753
    start_meas(&eNB->ulsch_deinterleaving_stats);
    sub_block_deinterleaving_turbo(4+Kr,
                                   &ulsch_harq->d[r][96],
                                   ulsch_harq->w[r]);
    stop_meas(&eNB->ulsch_deinterleaving_stats);
Raymond Knopp's avatar
Raymond Knopp committed
754

755 756 757 758 759 760 761
    if (ulsch_harq->C == 1)
      crc_type = CRC24_A;
    else
      crc_type = CRC24_B;
   
    start_meas(&eNB->ulsch_turbo_decoding_stats);
    
Raymond Knopp's avatar
Raymond Knopp committed
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
    ret = tc(&ulsch_harq->d[r][96],
	     ulsch_harq->c[r],
	     Kr,
	     f1f2mat_old[iind*2],
	     f1f2mat_old[(iind*2)+1],
	     ulsch->max_turbo_iterations,//MAX_TURBO_ITERATIONS,
	     crc_type,
	     (r==0) ? ulsch_harq->F : 0,
	     &eNB->ulsch_tc_init_stats,
	     &eNB->ulsch_tc_alpha_stats,
	     &eNB->ulsch_tc_beta_stats,
	     &eNB->ulsch_tc_gamma_stats,
	     &eNB->ulsch_tc_ext_stats,
	     &eNB->ulsch_tc_intl1_stats,
	     &eNB->ulsch_tc_intl2_stats);
    
    stop_meas(&eNB->ulsch_turbo_decoding_stats);
    
780
  // Reassembly of Transport block here
Raymond Knopp's avatar
Raymond Knopp committed
781

782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
    if (ret != (1+ulsch->max_turbo_iterations)) {
      if (r<ulsch_harq->Cminus)
	Kr = ulsch_harq->Kminus;
      else
	Kr = ulsch_harq->Kplus;
      
      Kr_bytes = Kr>>3;
      
      if (r==0) {
	memcpy(ulsch_harq->b,
	       &ulsch_harq->c[0][(ulsch_harq->F>>3)],
	       Kr_bytes - (ulsch_harq->F>>3) - ((ulsch_harq->C>1)?3:0));
	offset = Kr_bytes - (ulsch_harq->F>>3) - ((ulsch_harq->C>1)?3:0);
      } else {
	memcpy(ulsch_harq->b+offset,
	       ulsch_harq->c[r],
	       Kr_bytes - ((ulsch_harq->C>1)?3:0));
	offset += (Kr_bytes- ((ulsch_harq->C>1)?3:0));
      }
      
    } else {
      break;
Raymond Knopp's avatar
Raymond Knopp committed
804
    }
805
    
Raymond Knopp's avatar
Raymond Knopp committed
806 807
  }

808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
  return(ret);
}

static inline unsigned int lte_gold_unscram(unsigned int *x1, unsigned int *x2, unsigned char reset) __attribute__((always_inline));
static inline unsigned int lte_gold_unscram(unsigned int *x1, unsigned int *x2, unsigned char reset)
{
  int n;

  if (reset) {
    *x1 = 1+ (1<<31);
    *x2=*x2 ^ ((*x2 ^ (*x2>>1) ^ (*x2>>2) ^ (*x2>>3))<<31);

    // skip first 50 double words (1600 bits)
    //      printf("n=0 : x1 %x, x2 %x\n",x1,x2);
    for (n=1; n<50; n++) {
      *x1 = (*x1>>1) ^ (*x1>>4);
      *x1 = *x1 ^ (*x1<<31) ^ (*x1<<28);
      *x2 = (*x2>>1) ^ (*x2>>2) ^ (*x2>>3) ^ (*x2>>4);
      *x2 = *x2 ^ (*x2<<31) ^ (*x2<<30) ^ (*x2<<29) ^ (*x2<<28);
    }
  }
829

830 831 832 833 834 835
  *x1 = (*x1>>1) ^ (*x1>>4);
  *x1 = *x1 ^ (*x1<<31) ^ (*x1<<28);
  *x2 = (*x2>>1) ^ (*x2>>2) ^ (*x2>>3) ^ (*x2>>4);
  *x2 = *x2 ^ (*x2<<31) ^ (*x2<<30) ^ (*x2<<29) ^ (*x2<<28);
  return(*x1^*x2);
  //  printf("n=%d : c %x\n",n,x1^x2);
Raymond Knopp's avatar
Raymond Knopp committed
836 837 838

}
  
839
unsigned int  ulsch_decoding(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc,
840 841 842 843 844
                             uint8_t UE_id,
                             uint8_t control_only_flag,
                             uint8_t Nbundled,
                             uint8_t llr8_flag)
{
845 846


847 848 849
  int16_t *ulsch_llr = eNB->pusch_vars[UE_id]->llr;
  LTE_DL_FRAME_PARMS *frame_parms = &eNB->frame_parms;
  LTE_eNB_ULSCH_t *ulsch = eNB->ulsch[UE_id];
850 851
  uint8_t harq_pid;
  unsigned short nb_rb;
852
  unsigned int A;
853 854 855
  uint8_t Q_m;
  unsigned int i,i2,q,j,j2;
  int iprime;
856 857
  unsigned int ret=0;

858
  //  uint8_t dummy_channel_output[(3*8*block_length)+12];
859
  int r,Kr;
860 861 862 863

  uint8_t *columnset;
  unsigned int sumKr=0;
  unsigned int Qprime,L,G,Q_CQI,Q_RI,H,Hprime,Hpp,Cmux,Rmux_prime,O_RCC;
864
  unsigned int Qprime_ACK,Qprime_RI,len_ACK=0,len_RI=0;
Raymond Knopp's avatar
 
Raymond Knopp committed
865
  //  uint8_t q_ACK[MAX_ACK_PAYLOAD],q_RI[MAX_RI_PAYLOAD];
866 867 868 869 870
  int metric,metric_new;
  uint32_t x1, x2, s=0;
  int16_t ys,c;
  uint32_t wACK_idx;
  uint8_t dummy_w_cc[3*(MAX_CQI_BITS+8+32)];
871
  int16_t y[6*14*1200] __attribute__((aligned(32)));
872 873
  uint8_t ytag[14*1200];
  //  uint8_t ytag2[6*14*1200],*ytag2_ptr;
Cedric Roux's avatar
Cedric Roux committed
874
  int16_t cseq[6*14*1200] __attribute__((aligned(32)));
875
  int off;
876

877
  int frame = proc->frame_rx;
878
  int subframe = proc->subframe_rx;
Raymond Knopp's avatar
Raymond Knopp committed
879
  LTE_UL_eNB_HARQ_t *ulsch_harq;
880

881

882

883
  harq_pid = subframe2harq_pid(frame_parms,frame,subframe);
884 885

  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_ULSCH_DECODING0+harq_pid,1);
886 887 888

  // x1 is set in lte_gold_generic
  x2 = ((uint32_t)ulsch->rnti<<14) + ((uint32_t)subframe<<9) + frame_parms->Nid_cell; //this is c_init in 36.211 Sec 6.3.1
Raymond Knopp's avatar
Raymond Knopp committed
889
  ulsch_harq = ulsch->harq_processes[harq_pid];
890

891 892
  AssertFatal(harq_pid!=255,
              "FATAL ERROR: illegal harq_pid, returning\n");
893

894 895
  AssertFatal(ulsch_harq->Nsymb_pusch != 0,
              "FATAL ERROR: harq_pid %d, Nsymb 0!\n",harq_pid);
896

897

Raymond Knopp's avatar
Raymond Knopp committed
898
  nb_rb = ulsch_harq->nb_rb;
899

Raymond Knopp's avatar
Raymond Knopp committed
900
  A = ulsch_harq->TBS;
901

902

903
  Q_m = ulsch_harq->Qm;
Raymond Knopp's avatar
Raymond Knopp committed
904
  G = nb_rb * (12 * Q_m) * ulsch_harq->Nsymb_pusch;
905 906


Raymond Knopp's avatar
Raymond Knopp committed
907
  //#ifdef DEBUG_ULSCH_DECODING
908 909
  LOG_D(PHY,"[PUSCH %d] Frame %d, Subframe %d: ulsch_decoding (Nid_cell %d, rnti %x, x2 %x): A %d, round %d, RV %d, O_r1 %d, O_RI %d, O_ACK %d, G %d, Q_m %d Nsymb_pusch %d nb_rb %d\n",
      harq_pid,
Raymond Knopp's avatar
Raymond Knopp committed
910 911 912 913 914 915 916 917
	proc->frame_rx,subframe,
	frame_parms->Nid_cell,ulsch->rnti,x2,
	A,
	ulsch_harq->round,
	ulsch_harq->rvidx,
	ulsch_harq->Or1,
	ulsch_harq->O_RI,
	ulsch_harq->O_ACK,
918 919 920 921
	G,
        ulsch_harq->Qm,
        ulsch_harq->Nsymb_pusch,
        nb_rb);
Raymond Knopp's avatar
Raymond Knopp committed
922 923
	
  //#endif
924

Raymond Knopp's avatar
Raymond Knopp committed
925
  if (ulsch_harq->round == 0) {
926
    // This is a new packet, so compute quantities regarding segmentation
Raymond Knopp's avatar
Raymond Knopp committed
927
    ulsch_harq->B = A+24;
928
    lte_segmentation(NULL,
929 930 931 932 933 934 935 936
                     NULL,
                     ulsch_harq->B,
                     &ulsch_harq->C,
                     &ulsch_harq->Cplus,
                     &ulsch_harq->Cminus,
                     &ulsch_harq->Kplus,
                     &ulsch_harq->Kminus,
                     &ulsch_harq->F);
937 938 939
    //  CLEAR LLR's HERE for first packet in process
  }

940
  //  printf("after segmentation c[%d] = %p\n",0,ulsch_harq->c[0]);
941 942

  sumKr = 0;
943 944

  for (r=0; r<ulsch_harq->C; r++) {
Raymond Knopp's avatar
Raymond Knopp committed
945 946
    if (r<ulsch_harq->Cminus)
      Kr = ulsch_harq->Kminus;
947
    else
Raymond Knopp's avatar
Raymond Knopp committed
948
      Kr = ulsch_harq->Kplus;
949

950 951
    sumKr += Kr;
  }
952

953
  AssertFatal(sumKr>0,
Raymond Knopp's avatar
Raymond Knopp committed
954
	      "[eNB] ulsch_decoding.c: FATAL sumKr is 0! (Nid_cell %d, rnti %x, x2 %x): harq_pid %d round %d, RV %d, O_RI %d, O_ACK %d, G %d, subframe %d\n",
955 956 957 958 959 960 961 962 963
	      frame_parms->Nid_cell,ulsch->rnti,x2,
	      harq_pid,
	      ulsch_harq->round,
	      ulsch_harq->rvidx,
	      ulsch_harq->O_RI,
	      ulsch_harq->O_ACK,
	      G,
	      subframe);

964

965
  // Compute Q_ri
Raymond Knopp's avatar
Raymond Knopp committed
966
  Qprime = ulsch_harq->O_RI*ulsch_harq->Msc_initial*ulsch_harq->Nsymb_initial * ulsch->beta_offset_ri_times8;
967 968 969 970 971 972

  if (Qprime > 0 ) {
    if ((Qprime % (8*sumKr)) > 0)
      Qprime = 1+(Qprime/(8*sumKr));
    else
      Qprime = Qprime/(8*sumKr);
973

974 975 976 977 978 979 980 981 982 983
    if (Qprime > 4*nb_rb * 12)
      Qprime = 4*nb_rb * 12;
  }

  Q_RI = Q_m*Qprime;
  Qprime_RI = Qprime;


  // Compute Q_ack

Raymond Knopp's avatar
Raymond Knopp committed
984
  Qprime = ulsch_harq->O_ACK*ulsch_harq->Msc_initial*ulsch_harq->Nsymb_initial * ulsch->beta_offset_harqack_times8;
985

986 987 988 989 990 991 992 993 994 995 996 997 998
  if (Qprime > 0) {
    if ((Qprime % (8*sumKr)) > 0)
      Qprime = 1+(Qprime/(8*sumKr));
    else
      Qprime = Qprime/(8*sumKr);

    if (Qprime > (4*nb_rb * 12))
      Qprime = 4*nb_rb * 12;
  }

  //  Q_ACK = Qprime * Q_m;
  Qprime_ACK = Qprime;
#ifdef DEBUG_ULSCH_DECODING
Raymond Knopp's avatar
Raymond Knopp committed
999
  printf("ulsch_decoding.c: Qprime_ACK %d, Msc_initial %d, Nsymb_initial %d, sumKr %d\n",
Raymond Knopp's avatar
Raymond Knopp committed
1000
      Qprime_ACK,ulsch_harq->Msc_initial,ulsch_harq->Nsymb_initial,sumKr);
1001
#endif
1002

1003
  // Compute Q_cqi
Raymond Knopp's avatar
Raymond Knopp committed
1004
  if (ulsch_harq->Or1 < 12)
1005
    L=0;
1006
  else
1007
    L=8;
1008

1009
  // NOTE: we have to handle the case where we have a very small number of bits (condition on pg. 26 36.212)
Raymond Knopp's avatar
Raymond Knopp committed
1010 1011
  if (ulsch_harq->Or1 > 0)
    Qprime = (ulsch_harq->Or1 + L) * ulsch_harq->Msc_initial*ulsch_harq->Nsymb_initial * ulsch->beta_offset_cqi_times8;
1012 1013 1014
  else
    Qprime=0;

1015
  if (Qprime > 0) {  // check if ceiling is larger than floor in Q' expression
1016 1017 1018 1019 1020 1021
    if ((Qprime % (8*sumKr)) > 0)
      Qprime = 1+(Qprime/(8*sumKr));
    else
      Qprime = Qprime/(8*sumKr);
  }

Raymond Knopp's avatar
Raymond Knopp committed
1022
  G = nb_rb * (12 * Q_m) * (ulsch_harq->Nsymb_pusch);
1023

1024

1025 1026

  Q_CQI = Q_m * Qprime;
Raymond Knopp's avatar
Raymond Knopp committed
1027
#ifdef DEBUG_ULSCH_DECODING
Raymond Knopp's avatar
Raymond Knopp committed
1028
  printf("ulsch_decoding: G %d, Q_RI %d, Q_CQI %d (L %d, Or1 %d) O_ACK %d\n",G,Q_RI,Q_CQI,L,ulsch_harq->Or1,ulsch_harq->O_ACK);
Raymond Knopp's avatar
Raymond Knopp committed
1029
#endif
1030 1031

  G = G - Q_RI - Q_CQI;
1032
  ulsch_harq->G = G;
1033

1034 1035
  AssertFatal((int)G > 0,
              "FATAL: ulsch_decoding.c G < 0 (%d) : Q_RI %d, Q_CQI %d\n",G,Q_RI,Q_CQI);
1036 1037 1038 1039

  H = G + Q_CQI;
  Hprime = H/Q_m;

1040

1041
  // Demultiplexing/Deinterleaving of PUSCH/ACK/RI/CQI
1042
  start_meas(&eNB->ulsch_demultiplexing_stats);
1043
  Hpp = Hprime + Qprime_RI;
1044

Raymond Knopp's avatar
Raymond Knopp committed
1045
  Cmux       = ulsch_harq->Nsymb_pusch;
1046
  Rmux_prime = Hpp/Cmux;
1047

1048 1049 1050
  // Clear "tag" interleaving matrix to allow for CQI/DATA identification
  memset(ytag,0,Cmux*Rmux_prime);

1051

1052 1053

  i=0;
Raymond Knopp's avatar
 
Raymond Knopp committed
1054
  memset(y,LTE_NULL,Q_m*Hpp);
1055 1056 1057 1058 1059

  // read in buffer and unscramble llrs for everything but placeholder bits
  // llrs stored per symbol correspond to columns of interleaving matrix


1060
  s = lte_gold_unscram(&x1, &x2, 1);
1061
  i2=0;
1062 1063

  for (i=0; i<((Hpp*Q_m)>>5); i++) {
1064
    /*
1065
    for (j=0; j<32; j++) {
1066 1067
      cseq[i2++] = (int16_t)((((s>>j)&1)<<1)-1);
    }
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
    */
#if defined(__x86_64__) || defined(__i386__)
#ifndef __AVX2__
    ((__m128i*)cseq)[i2++] = ((__m128i*)unscrambling_lut)[(s&65535)<<1];
    ((__m128i*)cseq)[i2++] = ((__m128i*)unscrambling_lut)[1+((s&65535)<<1)];
    s>>=16;
    ((__m128i*)cseq)[i2++] = ((__m128i*)unscrambling_lut)[(s&65535)<<1];
    ((__m128i*)cseq)[i2++] = ((__m128i*)unscrambling_lut)[1+((s&65535)<<1)];
#else
    ((__m256i*)cseq)[i2++] = ((__m256i*)unscrambling_lut)[s&65535];
    ((__m256i*)cseq)[i2++] = ((__m256i*)unscrambling_lut)[(s>>16)&65535];
#endif
#elif defined(__arm__)
    ((int16x8_t*)cseq)[i2++] = ((int16x8_t*)unscrambling_lut)[(s&65535)<<1];
    ((int16x8_t*)cseq)[i2++] = ((int16x8_t*)unscrambling_lut)[1+((s&65535)<<1)];
    s>>=16;
    ((int16x8_t*)cseq)[i2++] = ((int16x8_t*)unscrambling_lut)[(s&65535)<<1];
    ((int16x8_t*)cseq)[i2++] = ((int16x8_t*)unscrambling_lut)[1+((s&65535)<<1)];
#endif
    s = lte_gold_unscram(&x1, &x2, 0);
1088 1089
  }

1090

1091 1092
  //  printf("after unscrambling c[%d] = %p\n",0,ulsch_harq->c[0]);

1093 1094 1095 1096
  if (frame_parms->Ncp == 0)
    columnset = cs_ri_normal;
  else
    columnset = cs_ri_extended;
1097 1098 1099 1100

  j=0;

  for (i=0; i<Qprime_RI; i++) {
1101
    r = Rmux_prime - 1 - (i>>2);
1102 1103
    /*
    for (q=0;q<Q_m;q++)
1104 1105
      ytag2[q+(Q_m*((r*Cmux) + columnset[j]))]  = q_RI[(q+(Q_m*i))%len_RI];
    */
1106
    off =((Rmux_prime*Q_m*columnset[j])+(r*Q_m));
1107
    cseq[off+1] = cseq[off];  // PUSCH_y
1108 1109

    for (q=2; q<Q_m; q++)
1110 1111 1112 1113 1114 1115
      cseq[off+q] = -1;    // PUSCH_x

    j=(j+3)&3;

  }

1116 1117
  //  printf("after RI c[%d] = %p\n",0,ulsch_harq->c[0]);

1118 1119 1120 1121 1122 1123 1124
  // HARQ-ACK Bits (Note these overwrite some bits)
  if (frame_parms->Ncp == 0)
    columnset = cs_ack_normal;
  else
    columnset = cs_ack_extended;

  j=0;
1125 1126

  for (i=0; i<Qprime_ACK; i++) {
1127
    r = Rmux_prime - 1 - (i>>2);
1128 1129
    off =((Rmux_prime*Q_m*columnset[j])+(r*Q_m));

Raymond Knopp's avatar
Raymond Knopp committed
1130
    if (ulsch_harq->O_ACK == 1) {
1131
      if (ulsch->bundling==0)
1132 1133 1134 1135 1136 1137 1138
        cseq[off+1] = cseq[off];  // PUSCH_y

      for (q=2; q<Q_m; q++)
        cseq[off+q] = -1;    // PUSCH_x
    } else if (ulsch_harq->O_ACK == 2) {
      for (q=2; q<Q_m; q++)
        cseq[off+q] = -1;    // PUSCH_x
1139
    }
1140

1141
#ifdef DEBUG_ULSCH_DECODING
Raymond Knopp's avatar
Raymond Knopp committed
1142
    printf("ulsch_decoding.c: ACK i %d, r %d, j %d, ColumnSet[j] %d\n",i,r,j,columnset[j]);
1143 1144 1145 1146 1147 1148 1149
#endif
    j=(j+3)&3;
  }



  i=0;
1150

1151 1152
  switch (Q_m) {
  case 2:
1153
    for (j=0; j<Cmux; j++) {
1154
      i2=j<<1;
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165

      for (r=0; r<Rmux_prime; r++) {
        c = cseq[i];
        //  printf("ulsch %d: %d * ",i,c);
        y[i2++] = c*ulsch_llr[i++];
        //  printf("%d\n",ulsch_llr[i-1]);
        c = cseq[i];
        //  printf("ulsch %d: %d * ",i,c);
        y[i2] = c*ulsch_llr[i++];
        //  printf("%d\n",ulsch_llr[i-1]);
        i2=(i2+(Cmux<<1)-1);
1166 1167
      }
    }
1168

1169
    break;
1170

1171
  case 4:
1172
    for (j=0; j<Cmux; j++) {
1173
      i2=j<<2;
1174 1175

      for (r=0; r<Rmux_prime; r++) {
1176
	/*
1177 1178 1179 1180 1181 1182 1183 1184 1185
        c = cseq[i];
        y[i2++] = c*ulsch_llr[i++];
        c = cseq[i];
        y[i2++] = c*ulsch_llr[i++];
        c = cseq[i];
        y[i2++] = c*ulsch_llr[i++];
        c = cseq[i];
        y[i2] = c*ulsch_llr[i++];
        i2=(i2+(Cmux<<2)-3);
1186
	*/
1187
	// slightly more optimized version (equivalent to above) for 16QAM to improve computational performance
1188 1189 1190
	*(__m64 *)&y[i2] = _mm_sign_pi16(*(__m64*)&ulsch_llr[i],*(__m64*)&cseq[i]);i+=4;i2+=(Cmux<<2);


1191 1192
      }
    }
1193 1194 1195

    break;

1196
  case 6:
1197
    for (j=0; j<Cmux; j++) {
1198
      i2=j*6;
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213

      for (r=0; r<Rmux_prime; r++) {
        c = cseq[i];
        y[i2++] = c*ulsch_llr[i++];
        c = cseq[i];
        y[i2++] = c*ulsch_llr[i++];
        c = cseq[i];
        y[i2++] = c*ulsch_llr[i++];
        c = cseq[i];
        y[i2++] = c*ulsch_llr[i++];
        c = cseq[i];
        y[i2++] = c*ulsch_llr[i++];
        c = cseq[i];
        y[i2] = c*ulsch_llr[i++];
        i2=(i2+(Cmux*6)-5);
1214 1215
      }
    }
1216

1217 1218 1219 1220
    break;
  }


1221

1222 1223

  if (i!=(H+Q_RI))
1224
    LOG_D(PHY,"ulsch_decoding.c: Error in input buffer length (j %d, H+Q_RI %d)\n",i,H+Q_RI);
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234

  // HARQ-ACK Bits (LLRs are nulled in overwritten bits after copying HARQ-ACK LLR)

  if (frame_parms->Ncp == 0)
    columnset = cs_ack_normal;
  else
    columnset = cs_ack_extended;

  j=0;

Raymond Knopp's avatar
Raymond Knopp committed
1235
  if (ulsch_harq->O_ACK == 1) {
1236 1237 1238 1239
    switch (Q_m) {
    case 2:
      len_ACK = 2;
      break;
1240

1241 1242 1243
    case 4:
      len_ACK = 4;
      break;
1244

1245 1246 1247 1248 1249
    case 6:
      len_ACK = 6;
      break;
    }
  }
1250

Raymond Knopp's avatar
Raymond Knopp committed
1251
  if (ulsch_harq->O_ACK == 2) {
1252 1253 1254 1255
    switch (Q_m) {
    case 2:
      len_ACK = 6;
      break;
1256

1257 1258 1259
    case 4:
      len_ACK = 12;
      break;
1260

1261 1262 1263 1264 1265
    case 6:
      len_ACK = 18;
      break;
    }
  }
1266

Raymond Knopp's avatar
Raymond Knopp committed
1267
  if (ulsch_harq->O_ACK > 2) {
1268
    LOG_E(PHY,"ulsch_decoding: FATAL, ACK cannot be more than 2 bits yet O_ACK:%d SFN/SF:%04d%d UE_id:%d rnti:%x\n",ulsch_harq->O_ACK,proc->frame_rx,proc->subframe_rx,UE_id,ulsch->rnti);
1269 1270 1271
    return(-1);
  }

1272
  for (i=0; i<len_ACK; i++)
Raymond Knopp's avatar
Raymond Knopp committed
1273
    ulsch_harq->q_ACK[i] = 0;
1274 1275


1276
  for (i=0; i<Qprime_ACK; i++) {
1277
    r = Rmux_prime -1 - (i>>2);
1278 1279

    for (q=0; q<Q_m; q++) {
1280
      if (y[q+(Q_m*((r*Cmux) + columnset[j]))]!=0)
1281
        ulsch_harq->q_ACK[(q+(Q_m*i))%len_ACK] += y[q+(Q_m*((r*Cmux) + columnset[j]))];
1282 1283
      y[q+(Q_m*((r*Cmux) + columnset[j]))]=0;  // NULL LLRs in ACK positions
    }
1284

1285 1286 1287
    j=(j+3)&3;
  }

1288
  //  printf("after ACKNAK c[%d] = %p\n",0,ulsch_harq->c[0]);
1289

1290
  // RI BITS
1291

Raymond Knopp's avatar
Raymond Knopp committed
1292
  if (ulsch_harq->O_RI == 1) {
1293 1294
    switch (Q_m) {
    case 2:
1295
      len_RI=2;
1296
      break;
1297

1298 1299 1300
    case 4:
      len_RI=4;
      break;
1301

1302 1303 1304 1305 1306 1307
    case 6:
      len_RI=6;
      break;
    }
  }

Raymond Knopp's avatar
Raymond Knopp committed
1308
  if (ulsch_harq->O_RI > 1) {
1309 1310 1311 1312
    LOG_E(PHY,"ulsch_decoding: FATAL, RI cannot be more than 1 bit yet\n");
    return(-1);
  }

1313
  for (i=0; i<len_RI; i++)
Raymond Knopp's avatar
Raymond Knopp committed
1314
    ulsch_harq->q_RI[i] = 0;
1315

1316 1317 1318 1319
  if (frame_parms->Ncp == 0)
    columnset = cs_ri_normal;
  else
    columnset = cs_ri_extended;
1320 1321 1322 1323

  j=0;

  for (i=0; i<Qprime_RI; i++) {
1324
    r = Rmux_prime -1 - (i>>2);
1325 1326

    for (q=0; q<Q_m; q++)
Raymond Knopp's avatar
Raymond Knopp committed
1327
      ulsch_harq->q_RI[(q+(Q_m*i))%len_RI] += y[q+(Q_m*((r*Cmux) + columnset[j]))];
1328

1329 1330 1331 1332
    ytag[(r*Cmux) + columnset[j]] = LTE_NULL;
    j=(j+3)&3;
  }

1333 1334
  //  printf("after RI2 c[%d] = %p\n",0,ulsch_harq->c[0]);

1335
  // CQI and Data bits
1336 1337 1338
  j=0;
  j2=0;

1339
  //  r=0;
1340 1341 1342 1343 1344 1345
  if (Q_RI>0) {
    for (i=0; i<(Q_CQI/Q_m); i++) {
      
      while (ytag[j]==LTE_NULL) {
	j++;
	j2+=Q_m;
1346
      }
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
      
      for (q=0; q<Q_m; q++) {
	//      ys = y[q+(Q_m*((r*Cmux)+j))];
	ys = y[q+j2];
	
	if (ys>127)
	  ulsch_harq->q[q+(Q_m*i)] = 127;
	else if (ys<-128)
	  ulsch_harq->q[q+(Q_m*i)] = -128;
	else
	  ulsch_harq->q[q+(Q_m*i)] = ys;
      }
      
1360 1361
      j2+=Q_m;
    }
1362
    
1363
        
1364 1365
    switch (Q_m) {
    case 2:
1366
      for (iprime=0; iprime<G;) {
1367 1368 1369 1370 1371 1372 1373 1374
	while (ytag[j]==LTE_NULL) {
	  j++;
	  j2+=2;
	}
	
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	
1375
      }
1376 1377 1378 1379 1380
      
      
      break;
      
    case 4:
1381
      for (iprime=0; iprime<G;) {
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
	while (ytag[j]==LTE_NULL) {
	  j++;
	  j2+=4;
	}
	
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	
1392
      }
1393 1394 1395 1396
      
      break;
      
    case 6:
1397
      for (iprime=0; iprime<G;) {
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
	while (ytag[j]==LTE_NULL) {
	  j++;
	  j2+=6;
	}
	
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	
1410
      }
1411 1412
      
      break;
1413

1414
    }
1415 1416 1417
    

  } // Q_RI>0
1418
  else {
1419

1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
    for (i=0; i<(Q_CQI/Q_m); i++) {
      
      for (q=0; q<Q_m; q++) {
	ys = y[q+j2];
	if (ys>127)
	  ulsch_harq->q[q+(Q_m*i)] = 127;
	else if (ys<-128)
	  ulsch_harq->q[q+(Q_m*i)] = -128;
	else
	  ulsch_harq->q[q+(Q_m*i)] = ys;
1430
      }
1431 1432
      
      j2+=Q_m;
1433
    }
1434
    /* To be improved according to alignment of j2
1435
#if defined(__x86_64__)||defined(__i386__)
1436
#ifndef __AVX2__
1437 1438 1439 1440 1441 1442 1443 1444 1445
    for (iprime=0; iprime<G;iprime+=8,j2+=8)
      *((__m128i *)&ulsch_harq->e[iprime]) = *((__m128i *)&y[j2]);
#else
    for (iprime=0; iprime<G;iprime+=16,j2+=16)
      *((__m256i *)&ulsch_harq->e[iprime]) = *((__m256i *)&y[j2]);
#endif
#elif defined(__arm__)
    for (iprime=0; iprime<G;iprime+=8,j2+=8)
      *((int16x8_t *)&ulsch_harq->e[iprime]) = *((int16x8_t *)&y[j2]);
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
#endif
    */
    int16_t *yp,*ep;
    for (iprime=0,yp=&y[j2],ep=&ulsch_harq->e[0]; 
	 iprime<G;
	 iprime+=8,j2+=8,ep+=8,yp+=8) {
      ep[0] = yp[0];
      ep[1] = yp[1];
      ep[2] = yp[2];
      ep[3] = yp[3];
      ep[4] = yp[4];
      ep[5] = yp[5];
      ep[6] = yp[6];
      ep[7] = yp[7];
    }
1461
  }
1462 1463
    
   
1464 1465
  stop_meas(&eNB->ulsch_demultiplexing_stats);

1466 1467
  //  printf("after ACKNAK2 c[%d] = %p (iprime %d, G %d)\n",0,ulsch_harq->c[0],iprime,G);

1468 1469
  // Do CQI/RI/HARQ-ACK Decoding first and pass to MAC

1470
  // HARQ-ACK
1471
  wACK_idx = (ulsch->bundling==0) ? 4 : ((Nbundled-1)&3);
1472

Raymond Knopp's avatar
Raymond Knopp committed
1473
  if (ulsch_harq->O_ACK == 1) {
1474 1475 1476 1477 1478 1479 1480
    ulsch_harq->q_ACK[0] *= wACK_RX[wACK_idx][0];
    ulsch_harq->q_ACK[0] += (ulsch->bundling==0) ? ulsch_harq->q_ACK[1]*wACK_RX[wACK_idx][0] : ulsch_harq->q_ACK[1]*wACK_RX[wACK_idx][1];

    if (ulsch_harq->q_ACK[0] < 0)
      ulsch_harq->o_ACK[0] = 0;
    else
      ulsch_harq->o_ACK[0] = 1;
1481
  }
1482

Raymond Knopp's avatar
Raymond Knopp committed
1483
  if (ulsch_harq->O_ACK == 2) {
1484 1485 1486
    switch (Q_m) {

    case 2:
Raymond Knopp's avatar
Raymond Knopp committed
1487 1488 1489
      ulsch_harq->q_ACK[0] = ulsch_harq->q_ACK[0]*wACK_RX[wACK_idx][0] + ulsch_harq->q_ACK[3]*wACK_RX[wACK_idx][1];
      ulsch_harq->q_ACK[1] = ulsch_harq->q_ACK[1]*wACK_RX[wACK_idx][0] + ulsch_harq->q_ACK[4]*wACK_RX[wACK_idx][1];
      ulsch_harq->q_ACK[2] = ulsch_harq->q_ACK[2]*wACK_RX[wACK_idx][0] + ulsch_harq->q_ACK[5]*wACK_RX[wACK_idx][1];
1490 1491
      break;
    case 4:
Raymond Knopp's avatar
Raymond Knopp committed
1492 1493 1494
      ulsch_harq->q_ACK[0] = ulsch_harq->q_ACK[0]*wACK_RX[wACK_idx][0] + ulsch_harq->q_ACK[5]*wACK_RX[wACK_idx][1];
      ulsch_harq->q_ACK[1] = ulsch_harq->q_ACK[1]*wACK_RX[wACK_idx][0] + ulsch_harq->q_ACK[8]*wACK_RX[wACK_idx][1];
      ulsch_harq->q_ACK[2] = ulsch_harq->q_ACK[4]*wACK_RX[wACK_idx][0] + ulsch_harq->q_ACK[9]*wACK_RX[wACK_idx][1];
1495 1496
      break;
    case 6:
Raymond Knopp's avatar
Raymond Knopp committed
1497
      ulsch_harq->q_ACK[0] =  ulsch_harq->q_ACK[0]*wACK_RX[wACK_idx][0] + ulsch_harq->q_ACK[7]*wACK_RX[wACK_idx][1];
1498 1499
      ulsch_harq->q_ACK[1] =  ulsch_harq->q_ACK[1]*wACK_RX[wACK_idx][0] + ulsch_harq->q_ACK[12]*wACK_RX[wACK_idx][1];
      ulsch_harq->q_ACK[2] =  ulsch_harq->q_ACK[6]*wACK_RX[wACK_idx][0] + ulsch_harq->q_ACK[13]*wACK_RX[wACK_idx][1];
1500 1501
      break;
    }
1502

Raymond Knopp's avatar
Raymond Knopp committed
1503 1504 1505 1506
    ulsch_harq->o_ACK[0] = 1;
    ulsch_harq->o_ACK[1] = 1;
    metric     = ulsch_harq->q_ACK[0]+ulsch_harq->q_ACK[1]-ulsch_harq->q_ACK[2];
    metric_new = -ulsch_harq->q_ACK[0]+ulsch_harq->q_ACK[1]+ulsch_harq->q_ACK[2];
1507 1508

    if (metric_new > metric) {
Raymond Knopp's avatar
Raymond Knopp committed
1509 1510
      ulsch_harq->o_ACK[0]=0;
      ulsch_harq->o_ACK[1]=1;
1511 1512
      metric = metric_new;
    }
1513

Raymond Knopp's avatar
Raymond Knopp committed
1514
    metric_new = ulsch_harq->q_ACK[0]-ulsch_harq->q_ACK[1]+ulsch_harq->q_ACK[2];
1515 1516 1517


    if (metric_new > metric) {
Raymond Knopp's avatar
Raymond Knopp committed
1518 1519
      ulsch_harq->o_ACK[0] = 1;
      ulsch_harq->o_ACK[1] = 0;
1520 1521
      metric = metric_new;
    }
1522

Raymond Knopp's avatar
Raymond Knopp committed
1523
    metric_new = -ulsch_harq->q_ACK[0]-ulsch_harq->q_ACK[1]-ulsch_harq->q_ACK[2];
1524 1525

    if (metric_new > metric) {
Raymond Knopp's avatar
Raymond Knopp committed
1526 1527
      ulsch_harq->o_ACK[0] = 0;
      ulsch_harq->o_ACK[1] = 0;
1528 1529 1530 1531 1532 1533
      metric = metric_new;
    }
  }

  // RI

1534
  // rank 1
Raymond Knopp's avatar
Raymond Knopp committed
1535
  if ((ulsch_harq->O_RI == 1) && (Qprime_RI > 0)) {
1536
    ulsch_harq->o_RI[0] = ((ulsch_harq->q_RI[0] + ulsch_harq->q_RI[Q_m/2]) > 0) ? 0 : 1;
1537
  }
1538

1539 1540
  // CQI

1541
  //  printf("before cqi c[%d] = %p\n",0,ulsch_harq->c[0]);
1542
  ulsch_harq->cqi_crc_status = 0;
1543
  if (Q_CQI>0) {
Raymond Knopp's avatar
Raymond Knopp committed
1544
    memset((void *)&dummy_w_cc[0],0,3*(ulsch_harq->Or1+8+32));
1545

Raymond Knopp's avatar
Raymond Knopp committed
1546
    O_RCC = generate_dummy_w_cc(ulsch_harq->Or1+8,
1547 1548 1549
                                &dummy_w_cc[0]);


1550
    lte_rate_matching_cc_rx(O_RCC,
1551 1552 1553 1554 1555
                            Q_CQI,
                            ulsch_harq->o_w,
                            dummy_w_cc,
                            ulsch_harq->q);

Raymond Knopp's avatar
Raymond Knopp committed
1556
    sub_block_deinterleaving_cc((unsigned int)(ulsch_harq->Or1+8),
1557 1558 1559
                                &ulsch_harq->o_d[96],
                                &ulsch_harq->o_w[0]);

Cedric Roux's avatar
Cedric Roux committed
1560 1561
    memset(ulsch_harq->o,0,(7+8+ulsch_harq->Or1) / 8);
    phy_viterbi_lte_sse2(ulsch_harq->o_d+96,ulsch_harq->o,8+ulsch_harq->Or1);
1562

Cedric Roux's avatar
Cedric Roux committed
1563
    if (extract_cqi_crc(ulsch_harq->o,ulsch_harq->Or1) == (crc8(ulsch_harq->o,ulsch_harq->Or1)>>24))
Raymond Knopp's avatar
Raymond Knopp committed
1564
      ulsch_harq->cqi_crc_status = 1;
1565 1566

#ifdef DEBUG_ULSCH_DECODING
Raymond Knopp's avatar
Raymond Knopp committed
1567
    printf("ulsch_decoding: Or1=%d\n",ulsch_harq->Or1);
1568 1569

    for (i=0; i<1+((8+ulsch_harq->Or1)/8); i++)
Raymond Knopp's avatar
Raymond Knopp committed
1570
      printf("ulsch_decoding: O[%d] %d\n",i,ulsch_harq->o[i]);
1571

Raymond Knopp's avatar
Raymond Knopp committed
1572
    if (ulsch_harq->cqi_crc_status == 1)
Cedric Roux's avatar
Cedric Roux committed
1573
      printf("RX CQI CRC OK (%x)\n",extract_cqi_crc(ulsch_harq->o,ulsch_harq->Or1));
1574
    else
Cedric Roux's avatar
Cedric Roux committed
1575
      printf("RX CQI CRC NOT OK (%x)\n",extract_cqi_crc(ulsch_harq->o,ulsch_harq->Or1));
1576

1577 1578 1579
#endif
  }

Cedric Roux's avatar
Cedric Roux committed
1580
  LOG_D(PHY,"frame %d subframe %d O_ACK:%d o_ACK[]=%d:%d:%d:%d\n",frame,subframe,ulsch_harq->O_ACK,ulsch_harq->o_ACK[0],ulsch_harq->o_ACK[1],ulsch_harq->o_ACK[2],ulsch_harq->o_ACK[3]);
1581

1582
  // Do ULSCH Decoding for data portion
1583

1584
  ret = eNB->td(eNB,UE_id,harq_pid,llr8_flag);
1585

1586
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_ULSCH_DECODING0+harq_pid,0);
1587

1588 1589 1590 1591 1592 1593
  return(ret);
}

#ifdef PHY_ABSTRACTION

#ifdef PHY_ABSTRACTION_UL
1594 1595
int ulsch_abstraction(double* sinr_dB, uint8_t TM, uint8_t mcs,uint16_t nrb, uint16_t frb)
{
1596

1597
  int index,ii;
1598 1599 1600 1601 1602 1603
  double sinr_eff = 0;
  int rb_count = 0;
  int offset;
  double bler = 0;
  TM = TM-1;
  sinr_eff = sinr_dB[frb]; //the single sinr_eff value we calculated with MMSE FDE formula in init_snr_up function
1604 1605


1606 1607 1608
  sinr_eff *= 10;
  sinr_eff = floor(sinr_eff);
  sinr_eff /= 10;
1609

1610
  LOG_D(PHY,"[ABSTRACTION] sinr_eff after rounding = %f\n",sinr_eff);
1611

1612 1613 1614 1615 1616 1617 1618
  for (index = 0; index < 16; index++) {
    if(index == 0) {
      if (sinr_eff < sinr_bler_map_up[mcs][0][index]) {
        bler = 1;
        break;
      }
    }
1619

1620
    if (sinr_eff == sinr_bler_map_up[mcs][0][index]) {
1621
      bler = sinr_bler_map_up[mcs][1][index];
1622 1623
    }
  }
1624

1625
#ifdef USER_MODE // need to be adapted for the emulation in the kernel space 
1626 1627 1628

  if (uniformrandom() < bler) {
    LOG_I(OCM,"abstraction_decoding failed (mcs=%d, sinr_eff=%f, bler=%f)\n",mcs,sinr_eff,bler);
1629
    return(0);
1630
  } else {
1631 1632 1633
    LOG_I(OCM,"abstraction_decoding successful (mcs=%d, sinr_eff=%f, bler=%f)\n",mcs,sinr_eff,bler);
    return(1);
  }
1634

1635 1636 1637 1638 1639 1640 1641 1642 1643
#endif
}







1644 1645
int ulsch_abstraction_MIESM(double* sinr_dB,uint8_t TM, uint8_t mcs,uint16_t nrb, uint16_t frb)
{
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
  int index;
  double sinr_eff = 0;
  double sinr_db1 = 0;
  double sinr_db2 = 0;
  double SI=0;
  double RBIR=0;
  int rb_count = 0;
  int offset, M=0;
  double bler = 0;
  int start,middle,end;
  TM = TM-1;
1657

1658 1659
  for (offset = frb; offset <= (frb + nrb -1); offset++) {

1660
    rb_count++;
1661

1662
    //we need to do the table lookups here for the mutual information corresponding to the certain sinr_dB.
1663

1664 1665
    sinr_db1 = sinr_dB[offset*2];
    sinr_db2 = sinr_dB[offset*2+1];
1666

Raymond Knopp's avatar
Raymond Knopp committed
1667
    printf("sinr_db1=%f\n,sinr_db2=%f\n",sinr_db1,sinr_db2);
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
    //rounding up for the table lookup
    sinr_db1 *= 10;
    sinr_db2 *= 10;

    sinr_db1 = floor(sinr_db1);
    sinr_db2 = floor(sinr_db2);

    if ((int)sinr_db1%2) {
      sinr_db1 += 1;
    }

    if ((int)sinr_db2%2) {
      sinr_db2 += 1;
    }

    sinr_db1 /= 10;
    sinr_db2 /= 10;

    if(mcs<10) {
      //for sinr_db1
      for (index = 0; index < 162; index++) {
        if (sinr_db1 < MI_map_4qam[0][0]) {
          SI += (MI_map_4qam[1][0]/beta1_dlsch_MI[TM][mcs]);
          M +=2;
          break;
        }

        if (sinr_db1 > MI_map_4qam[0][161]) {
          SI += (MI_map_4qam[1][161]/beta1_dlsch_MI[TM][mcs]);
          M +=2;
          break;
        }

        if (sinr_db1 == MI_map_4qam[0][index]) {
          SI += (MI_map_4qam[1][index]/beta1_dlsch_MI[TM][mcs]);
          M +=2;
          break;
        }
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 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792

      //for sinr_db2
      for (index = 0; index < 162; index++) {
        if (sinr_db2 < MI_map_4qam[0][0]) {
          SI += (MI_map_4qam[1][0]/beta1_dlsch_MI[TM][mcs]);
          M +=2;
          break;
        }

        if (sinr_db2 > MI_map_4qam[0][161]) {
          SI += (MI_map_4qam[1][161]/beta1_dlsch_MI[TM][mcs]);
          M +=2;
          break;
        }

        if (sinr_db2 == MI_map_4qam[0][index]) {
          SI += (MI_map_4qam[1][index]/beta1_dlsch_MI[TM][mcs]);
          M +=2;
          break;
        }
      }

    } else if(mcs>9 && mcs<17) {
      //for sinr_db1
      for (index = 0; index < 197; index++) {
        if (sinr_db1 < MI_map_16qam[0][0]) {
          SI += (MI_map_16qam[1][0]/beta1_dlsch_MI[TM][mcs]);
          M +=4;
          break;
        }

        if (sinr_db1 > MI_map_16qam[0][196]) {
          SI += (MI_map_16qam[1][196]/beta1_dlsch_MI[TM][mcs]);
          M +=4;
          break;
        }

        if (sinr_db1 == MI_map_16qam[0][index]) {
          SI += (MI_map_16qam[1][index]/beta1_dlsch_MI[TM][mcs]);
          M +=4;
          break;
        }
      }

      //for sinr_db2
      for (index = 0; index < 197; index++) {
        if (sinr_db2 < MI_map_16qam[0][0]) {
          SI += (MI_map_16qam[1][0]/beta1_dlsch_MI[TM][mcs]);
          M +=4;
          break;
        }

        if (sinr_db2 > MI_map_16qam[0][196]) {
          SI += (MI_map_16qam[1][196]/beta1_dlsch_MI[TM][mcs]);
          M +=4;
          break;
        }

        if (sinr_db2 == MI_map_16qam[0][index]) {
          SI += (MI_map_16qam[1][index]/beta1_dlsch_MI[TM][mcs]);
          M +=4;
          break;
        }
      }

    } else if(mcs>16 && mcs<22) {
      //for sinr_db1
      for (index = 0; index < 227; index++) {
        if (sinr_db1 < MI_map_64qam[0][0]) {
          SI += (MI_map_64qam[1][0]/beta1_dlsch_MI[TM][mcs]);
          M +=6;
          break;
        }

        if (sinr_db1 > MI_map_64qam[0][226]) {
          SI += (MI_map_64qam[1][226]/beta1_dlsch_MI[TM][mcs]);
          M +=6;
          break;
        }

        if (sinr_db1 == MI_map_64qam[0][index]) {
          SI += (MI_map_64qam[1][index]/beta1_dlsch_MI[TM][mcs]);
          M +=6;
          break;
        }
1793 1794 1795
      }

      //for sinr_db2
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813
      for (index = 0; index < 227; index++) {
        if (sinr_db2 < MI_map_64qam[0][0]) {
          SI += (MI_map_64qam[1][0]/beta1_dlsch_MI[TM][mcs]);
          M +=6;
          break;
        }

        if (sinr_db2 > MI_map_64qam[0][226]) {
          SI += (MI_map_64qam[1][226]/beta1_dlsch_MI[TM][mcs]);
          M +=6;
          break;
        }

        if (sinr_db2 == MI_map_64qam[0][index]) {
          SI += (MI_map_64qam[1][index]/beta1_dlsch_MI[TM][mcs]);
          M +=6;
          break;
        }
1814 1815
      }
    }
1816 1817 1818
  }

  // }
1819 1820

  RBIR = SI/M;
1821

1822 1823
  //Now RBIR->SINR_effective Mapping
  //binary search method is performed here
1824
  if(mcs<10) {
1825 1826 1827
    start = 0;
    end = 161;
    middle = end/2;
1828 1829

    if (RBIR <= MI_map_4qam[2][start]) {
1830
      sinr_eff =  MI_map_4qam[0][start];
1831
    } else {
1832
      if (RBIR >= MI_map_4qam[2][end])
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842
        sinr_eff =  MI_map_4qam[0][end];
      else {
        //while((end-start > 1) && (RBIR >= MI_map_4qam[2]))
        if (RBIR < MI_map_4qam[2][middle]) {
          end = middle;
          middle = end/2;
        } else {
          start = middle;
          middle = (end-middle)/2;
        }
1843
      }
1844 1845 1846 1847 1848 1849

      for (; end>start; end--) {
        if ((RBIR < MI_map_4qam[2][end]) && (RBIR >  MI_map_4qam[2][end-2])) {
          sinr_eff = MI_map_4qam[0][end-1];
          break;
        }
1850
      }
1851 1852 1853
    }

    sinr_eff = sinr_eff * beta2_dlsch_MI[TM][mcs];
1854 1855 1856
  }


1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877

  else if (mcs>9 && mcs<17) {

    start = 0;
    end = 196;
    middle = end/2;

    if (RBIR <= MI_map_16qam[2][start]) {
      sinr_eff =  MI_map_16qam[0][start];
    } else {
      if (RBIR >= MI_map_16qam[2][end])
        sinr_eff =  MI_map_16qam[0][end];
      else {
        //while((end-start > 1) && (RBIR >= MI_map_4qam[2]))
        if (RBIR < MI_map_16qam[2][middle]) {
          end = middle;
          middle = end/2;
        } else {
          start = middle;
          middle = (end-middle)/2;
        }
1878
      }
1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891

      for (; end>start; end--) {
        if ((RBIR < MI_map_16qam[2][end]) && (RBIR >  MI_map_16qam[2][end-2])) {
          sinr_eff = MI_map_16qam[0][end-1];
          break;
        }
      }
    }

    sinr_eff = sinr_eff * beta2_dlsch_MI[TM][mcs];
  } else if (mcs>16) {
    start = 0;
    end = 226;
1892
    middle = end/2;
1893 1894

    if (RBIR <= MI_map_64qam[2][start]) {
1895
      sinr_eff =  MI_map_64qam[0][start];
1896
    } else {
1897
      if (RBIR >= MI_map_64qam[2][end])
1898 1899 1900 1901 1902 1903 1904 1905 1906 1907
        sinr_eff =  MI_map_64qam[0][end];
      else {
        //while((end-start > 1) && (RBIR >= MI_map_4qam[2]))
        if (RBIR < MI_map_64qam[2][middle]) {
          end = middle;
          middle = end/2;
        } else {
          start = middle;
          middle = (end-middle)/2;
        }
1908
      }
1909 1910 1911 1912 1913 1914

      for (; end>start; end--) {
        if ((RBIR < MI_map_64qam[2][end]) && (RBIR >  MI_map_64qam[2][end-2])) {
          sinr_eff = MI_map_64qam[0][end-1];
          break;
        }
1915
      }
1916 1917 1918 1919
    }

    sinr_eff = sinr_eff * beta2_dlsch_MI[TM][mcs];
  }
1920

Raymond Knopp's avatar
Raymond Knopp committed
1921
  printf("SINR_Eff = %e\n",sinr_eff);
1922

1923
  sinr_eff *= 10;
1924 1925 1926 1927 1928
  sinr_eff = floor(sinr_eff);
  // if ((int)sinr_eff%2) {
  //   sinr_eff += 1;
  // }
  sinr_eff /= 10;
Raymond Knopp's avatar
Raymond Knopp committed
1929
  printf("sinr_eff after rounding = %f\n",sinr_eff);
1930

1931
  for (index = 0; index < 16; index++) {
1932 1933 1934 1935 1936 1937
    if(index == 0) {
      if (sinr_eff < sinr_bler_map_up[mcs][0][index]) {
        bler = 1;
        break;
      }
    }
1938

1939
    if (sinr_eff == sinr_bler_map_up[mcs][0][index]) {
1940
      bler = sinr_bler_map_up[mcs][1][index];
1941
    }
1942 1943
  }

1944
#ifdef USER_MODE // need to be adapted for the emulation in the kernel space 
1945 1946

  if (uniformrandom() < bler) {
Raymond Knopp's avatar
Raymond Knopp committed
1947
    printf("abstraction_decoding failed (mcs=%d, sinr_eff=%f, bler=%f)\n",mcs,sinr_eff,bler);
1948
    return(0);
1949
  } else {
Raymond Knopp's avatar
Raymond Knopp committed
1950
    printf("abstraction_decoding successful (mcs=%d, sinr_eff=%f, bler=%f)\n",mcs,sinr_eff,bler);
1951 1952
    return(1);
  }
1953

1954 1955 1956 1957 1958 1959
#endif

}

#endif

1960
uint32_t ulsch_decoding_emul(PHY_VARS_eNB *eNB, eNB_rxtx_proc_t *proc,
1961 1962 1963
                             uint8_t UE_index,
                             uint16_t *crnti)
{
1964 1965 1966

  uint8_t UE_id;
  uint16_t rnti;
1967
  int subframe = proc->subframe_rx;
Raymond Knopp's avatar
 
Raymond Knopp committed
1968
  uint8_t harq_pid;
1969
  uint8_t CC_id = eNB->CC_id;
Raymond Knopp's avatar
 
Raymond Knopp committed
1970

1971
  harq_pid = subframe2harq_pid(&eNB->frame_parms,proc->frame_rx,subframe);
1972

1973
  rnti = eNB->ulsch[UE_index]->rnti;
1974
#ifdef DEBUG_PHY
1975
  LOG_D(PHY,"[eNB %d] ulsch_decoding_emul : subframe %d UE_index %d harq_pid %d rnti %x\n",eNB->Mod_id,subframe,UE_index,harq_pid,rnti);
1976
#endif
1977 1978

  for (UE_id=0; UE_id<NB_UE_INST; UE_id++) {
1979
    if (rnti == PHY_vars_UE_g[UE_id][CC_id]->pdcch_vars[PHY_vars_UE_g[UE_id][CC_id]->current_thread_id[subframe]][0]->crnti)
1980
      break;
1981

1982 1983 1984 1985
  }

  if (UE_id==NB_UE_INST) {
    LOG_W(PHY,"[eNB %d] ulsch_decoding_emul: FATAL, didn't find UE with rnti %x (UE index %d)\n",
1986 1987
          eNB->Mod_id, rnti, UE_index);
    return(1+eNB->ulsch[UE_id]->max_turbo_iterations);
1988
  } else {
1989
    LOG_D(PHY,"[eNB %d] Found UE with rnti %x => UE_id %d\n",eNB->Mod_id, rnti, UE_id);
1990 1991
  }

1992
  if (PHY_vars_UE_g[UE_id][CC_id]->ulsch[0]->harq_processes[harq_pid]->status == CBA_ACTIVE) {
1993
    *crnti = rnti;
1994
    PHY_vars_UE_g[UE_id][CC_id]->ulsch[0]->harq_processes[harq_pid]->status=IDLE;
1995
  } else
1996
    *crnti = 0x0;
1997

1998 1999 2000
  if (1) {
    LOG_D(PHY,"ulsch_decoding_emul abstraction successful\n");

2001 2002 2003
    memcpy(eNB->ulsch[UE_index]->harq_processes[harq_pid]->b,
           PHY_vars_UE_g[UE_id][CC_id]->ulsch[0]->harq_processes[harq_pid]->b,
           eNB->ulsch[UE_index]->harq_processes[harq_pid]->TBS>>3);
2004 2005 2006

    // get local ue's ack
    if ((UE_index >= oai_emulation.info.first_ue_local) ||(UE_index <(oai_emulation.info.first_ue_local+oai_emulation.info.nb_ue_local))) {
2007
      get_ack(&eNB->frame_parms,
hbilel's avatar
hbilel committed
2008
              PHY_vars_UE_g[UE_id][CC_id]->dlsch[0][0][0]->harq_ack,
hbilel's avatar
hbilel committed
2009 2010
              proc->subframe_tx,
              proc->subframe_rx,
2011
              eNB->ulsch[UE_index]->harq_processes[harq_pid]->o_ACK,0);
2012
    } else { // get remote UEs' ack
2013 2014
      eNB->ulsch[UE_index]->harq_processes[harq_pid]->o_ACK[0] = PHY_vars_UE_g[UE_id][CC_id]->ulsch[0]->o_ACK[0];
      eNB->ulsch[UE_index]->harq_processes[harq_pid]->o_ACK[1] = PHY_vars_UE_g[UE_id][CC_id]->ulsch[0]->o_ACK[1];
2015 2016 2017
    }

    // Do abstraction of PUSCH feedback
2018
#ifdef DEBUG_PHY
2019
    LOG_D(PHY,"[eNB %d][EMUL] ue index %d UE_id %d: subframe %d : o_ACK (%d %d), cqi (val %d, len %d)\n",
2020 2021 2022 2023
          eNB->Mod_id,UE_index, UE_id, subframe,eNB->ulsch[UE_index]->harq_processes[harq_pid]->o_ACK[0],
          eNB->ulsch[UE_index]->harq_processes[harq_pid]->o_ACK[1],
          ((HLC_subband_cqi_rank1_2A_5MHz *)PHY_vars_UE_g[UE_id][CC_id]->ulsch[0]->o)->cqi1,
          PHY_vars_UE_g[UE_id][CC_id]->ulsch[0]->O);
2024
#endif
2025

2026 2027
    eNB->ulsch[UE_index]->harq_processes[harq_pid]->Or1 = PHY_vars_UE_g[UE_id][CC_id]->ulsch[0]->O;
    eNB->ulsch[UE_index]->harq_processes[harq_pid]->Or2 = PHY_vars_UE_g[UE_id][CC_id]->ulsch[0]->O;
2028

2029 2030 2031
    eNB->ulsch[UE_index]->harq_processes[harq_pid]->uci_format = PHY_vars_UE_g[UE_id][CC_id]->ulsch[0]->uci_format;
    memcpy(eNB->ulsch[UE_index]->harq_processes[harq_pid]->o,PHY_vars_UE_g[UE_id][CC_id]->ulsch[0]->o,MAX_CQI_BYTES);
    memcpy(eNB->ulsch[UE_index]->harq_processes[harq_pid]->o_RI,PHY_vars_UE_g[UE_id][CC_id]->ulsch[0]->o_RI,2);
2032

2033
    eNB->ulsch[UE_index]->harq_processes[harq_pid]->cqi_crc_status = 1;
2034 2035 2036

    return(1);
  } else {
2037
    LOG_W(PHY,"[eNB %d] ulsch_decoding_emul abstraction failed for UE %d\n",eNB->Mod_id,UE_index);
2038

2039
    eNB->ulsch[UE_index]->harq_processes[harq_pid]->cqi_crc_status = 0;
2040 2041

    // retransmission
2042
    return(1+eNB->ulsch[UE_index]->max_turbo_iterations);
2043 2044 2045 2046
  }

}
#endif