ulsch_decoding.c 55.8 KB
Newer Older
1
/*******************************************************************************
2
    OpenAirInterface
ghaddab's avatar
ghaddab committed
3
    Copyright(c) 1999 - 2014 Eurecom
4

ghaddab's avatar
ghaddab committed
5 6 7 8
    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
knopp's avatar
knopp committed
9
 
10

ghaddab's avatar
ghaddab committed
11 12 13 14
    OpenAirInterface is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
15

ghaddab's avatar
ghaddab committed
16
    You should have received a copy of the GNU General Public License
17 18
    along with OpenAirInterface.The full GNU General Public License is
   included in this distribution in the file called "COPYING". If not,
ghaddab's avatar
ghaddab committed
19
   see <http://www.gnu.org/licenses/>.
20 21

  Contact Information
ghaddab's avatar
ghaddab committed
22 23
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
24
  OpenAirInterface Dev  : openair4g-devel@lists.eurecom.fr
25

ghaddab's avatar
ghaddab committed
26
  Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE
27

ghaddab's avatar
ghaddab committed
28
 *******************************************************************************/
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

/*! \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
59

60 61 62
#include "UTIL/LOG/vcd_signal_dumper.h"
//#define DEBUG_ULSCH_DECODING

63 64
void free_eNB_ulsch(LTE_eNB_ULSCH_t *ulsch)
{
65 66 67 68

  int i,r;

  if (ulsch) {
69
    for (i=0; i<8; i++) {
70
      if (ulsch->harq_processes[i]) {
71 72 73 74 75 76
        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++) {
77 78 79
          free16(ulsch->harq_processes[i]->c[r],((r==0)?8:0) + 768);
          ulsch->harq_processes[i]->c[r] = NULL;
        }
80 81 82 83 84 85 86 87 88

        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;
89 90
      }
    }
91 92 93

    free16(ulsch,sizeof(LTE_eNB_ULSCH_t));
    ulsch = NULL;
94 95 96
  }
}

97
LTE_eNB_ULSCH_t *new_eNB_ulsch(uint8_t max_turbo_iterations,uint8_t N_RB_UL, uint8_t abstraction_flag)
98
{
99 100 101 102

  LTE_eNB_ULSCH_t *ulsch;
  uint8_t exit_flag = 0,i,r;
  unsigned char bw_scaling =1;
103 104 105

  switch (N_RB_UL) {
  case 6:
106 107
    bw_scaling =16;
    break;
108

109 110 111
  case 25:
    bw_scaling =4;
    break;
112 113

  case 50:
114 115
    bw_scaling =2;
    break;
116

117 118 119 120
  default:
    bw_scaling =1;
    break;
  }
121

122
  ulsch = (LTE_eNB_ULSCH_t *)malloc16(sizeof(LTE_eNB_ULSCH_t));
123

124
  if (ulsch) {
125
    memset(ulsch,0,sizeof(LTE_eNB_ULSCH_t));
126
    ulsch->max_turbo_iterations = max_turbo_iterations;
127
    ulsch->Mlimit = 4;
128

129
    for (i=0; i<8; i++) {
knopp's avatar
knopp committed
130
      //      printf("new_ue_ulsch: Harq process %d\n",i);
131
      ulsch->harq_processes[i] = (LTE_UL_eNB_HARQ_t *)malloc16(sizeof(LTE_UL_eNB_HARQ_t));
132

133
      if (ulsch->harq_processes[i]) {
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
        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;
          }

          ulsch->harq_processes[i]->subframe_scheduling_flag = 0;
        }
      } else {
        exit_flag=1;
162 163 164 165 166 167
      }
    }

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

169 170 171 172 173 174
  LOG_E(PHY,"new_ue_ulsch: exit_flag = %d\n",exit_flag);
  free_eNB_ulsch(ulsch);

  return(NULL);
}

175
void clean_eNb_ulsch(LTE_eNB_ULSCH_t *ulsch)
176
{
177 178 179 180 181 182

  unsigned char i;

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

184
    for (i=0; i<8; i++) {
185
      if (ulsch->harq_processes[i]) {
186 187 188 189 190 191
        //    ulsch->harq_processes[i]->Ndi = 0;
        ulsch->harq_processes[i]->status = 0;
        ulsch->harq_processes[i]->subframe_scheduling_flag = 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;
192 193 194 195 196 197 198
      }
    }

  }
}


199 200
uint8_t extract_cqi_crc(uint8_t *cqi,uint8_t CQI_LENGTH)
{
201 202 203 204

  uint8_t crc;

  crc = cqi[CQI_LENGTH>>3];
knopp's avatar
knopp committed
205
  //  printf("crc1: %x, shift %d\n",crc,CQI_LENGTH&0x7);
206 207 208
  crc = (crc<<(CQI_LENGTH&0x7));
  // clear crc bits
  //  ((char *)cqi)[CQI_LENGTH>>3] &= 0xff>>(8-(CQI_LENGTH&0x7));
knopp's avatar
knopp committed
209
  //  printf("crc2: %x, cqi0 %x\n",crc,cqi[1+(CQI_LENGTH>>3)]);
210 211 212 213 214 215 216 217 218 219 220
  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);

}



221 222 223 224 225 226 227 228 229 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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301



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];
  int Q_m = get_Qm_ul(ulsch_harq->mcs);
  int G = ulsch_harq->nb_rb * (12 * Q_m) * ulsch_harq->Nsymb_pusch;
  uint32_t E;
  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 {
      LOG_E(PHY,"ulsch_decoding: Illegal codeword size %d!!!\n",Kr_bytes);
      return(-1);
    }

    // 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;
302 303 304 305 306 307

    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));
    }
308 309 310
  }

  // go through second half of segments
311
  for (; r<(ulsch_harq->C); r++) {
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 354 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 390 391 392 393 394 395 396 397 398 399 400 401


    //    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
                                   get_Qm_ul(ulsch_harq->mcs),
                                   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);
    
402
    // Reassembly of Transport block here
403 404 405 406 407 408 409 410 411

    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;
      
412 413 414 415 416
      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));
      
417 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 449 450 451 452 453 454 455 456 457 458 459 460 461 462
      
    } else {
      break;
    }
    
  }

  return(ret);
}

extern int oai_exit;
void *td_thread(void *param) {
  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];
  int Q_m = get_Qm_ul(ulsch_harq->mcs);
  int G = ulsch_harq->nb_rb * (12 * Q_m) * ulsch_harq->Nsymb_pusch;
  unsigned int E;
463
  int Cby2;
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491

  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;

492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
  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);
    }
520 521

    pthread_mutex_unlock( &proc->mutex_td );
522
    Cby2 = ulsch_harq->C/2;
523
  }
524 525
  else {
    Cby2 = 1;
526 527 528
  }

  // go through first half of segments in main thread
529
  for (r=0; r<Cby2; r++) {
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 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

    //    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
                                   get_Qm_ul(ulsch_harq->mcs),
                                   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);
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 657 658 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 726 727 728 729 730 731 732 733 734 735 736
  // 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];
  int Q_m = get_Qm_ul(ulsch_harq->mcs);
  int G = ulsch_harq->nb_rb * (12 * Q_m) * ulsch_harq->Nsymb_pusch;
  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);
knopp's avatar
knopp committed
737

738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
    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
                                   get_Qm_ul(ulsch_harq->mcs),
                                   1,
                                   r,
                                   &E)==-1) {
      LOG_E(PHY,"ulsch_decoding.c: Problem in rate matching\n");
      return(-1);
    }
knopp's avatar
knopp committed
756

757 758
    stop_meas(&eNB->ulsch_rate_unmatching_stats);
    r_offset += E;
knopp's avatar
knopp committed
759

760 761 762 763 764
    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);
knopp's avatar
knopp committed
765

766 767 768 769 770 771 772
    if (ulsch_harq->C == 1)
      crc_type = CRC24_A;
    else
      crc_type = CRC24_B;
   
    start_meas(&eNB->ulsch_turbo_decoding_stats);
    
knopp's avatar
knopp committed
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
    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);
    
791
  // Reassembly of Transport block here
knopp's avatar
knopp committed
792

793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
    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;
knopp's avatar
knopp committed
815
    }
816
    
knopp's avatar
knopp committed
817 818
  }

819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
  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);
    }
  }
840

841 842 843 844 845 846
  *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);
knopp's avatar
knopp committed
847 848 849

}
  
knopp's avatar
knopp committed
850
unsigned int  ulsch_decoding(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc,
851 852 853 854 855
                             uint8_t UE_id,
                             uint8_t control_only_flag,
                             uint8_t Nbundled,
                             uint8_t llr8_flag)
{
856 857


858 859 860
  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];
861 862
  uint8_t harq_pid;
  unsigned short nb_rb;
863
  unsigned int A;
864 865 866
  uint8_t Q_m;
  unsigned int i,i2,q,j,j2;
  int iprime;
867 868
  unsigned int ret=0;

869
  //  uint8_t dummy_channel_output[(3*8*block_length)+12];
870
  int r,Kr;
871 872 873 874

  uint8_t *columnset;
  unsigned int sumKr=0;
  unsigned int Qprime,L,G,Q_CQI,Q_RI,H,Hprime,Hpp,Cmux,Rmux_prime,O_RCC;
875
  unsigned int Qprime_ACK,Qprime_RI,len_ACK=0,len_RI=0;
knopp's avatar
knopp committed
876
  //  uint8_t q_ACK[MAX_ACK_PAYLOAD],q_RI[MAX_RI_PAYLOAD];
877 878 879 880 881 882 883 884 885 886 887
  int metric,metric_new;
  uint8_t o_flip[8];
  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)];
  int16_t y[6*14*1200];
  uint8_t ytag[14*1200];
  //  uint8_t ytag2[6*14*1200],*ytag2_ptr;
  int16_t cseq[6*14*1200];
  int off;
888

knopp's avatar
knopp committed
889
  int subframe = proc->subframe_rx;
knopp's avatar
knopp committed
890
  LTE_UL_eNB_HARQ_t *ulsch_harq;
891

892

893

knopp's avatar
knopp committed
894
  harq_pid = subframe2harq_pid(frame_parms,proc->frame_rx,subframe);
895 896

  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_ULSCH_DECODING0+harq_pid,1);
897 898 899

  // 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
knopp's avatar
knopp committed
900
  ulsch_harq = ulsch->harq_processes[harq_pid];
901 902

  if (harq_pid==255) {
903
    LOG_E(PHY, "FATAL ERROR: illegal harq_pid, returning\n");
knopp's avatar
knopp committed
904
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_ULSCH_DECODING0+harq_pid,0);
905
    return -1;
906 907
  }

908 909
  if (ulsch_harq->Nsymb_pusch == 0) {
      LOG_E(PHY, "FATAL ERROR: harq_pid %d, Nsymb 0!\n",harq_pid);
knopp's avatar
knopp committed
910
      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_ULSCH_DECODING0+harq_pid,0); 
911
      return 1+ulsch->max_turbo_iterations;
912
  }
913

914

knopp's avatar
knopp committed
915
  nb_rb = ulsch_harq->nb_rb;
916

knopp's avatar
knopp committed
917
  A = ulsch_harq->TBS;
918

919

knopp's avatar
knopp committed
920 921
  Q_m = get_Qm_ul(ulsch_harq->mcs);
  G = nb_rb * (12 * Q_m) * ulsch_harq->Nsymb_pusch;
922 923 924


#ifdef DEBUG_ULSCH_DECODING
knopp's avatar
knopp committed
925
  printf("ulsch_decoding (Nid_cell %d, rnti %x, x2 %x): round %d, RV %d, mcs %d, O_RI %d, O_ACK %d, G %d, subframe %d\n",
926
      frame_parms->Nid_cell,ulsch->rnti,x2,
knopp's avatar
knopp committed
927 928 929 930 931
      ulsch_harq->round,
      ulsch_harq->rvidx,
      ulsch_harq->mcs,
      ulsch_harq->O_RI,
      ulsch_harq->O_ACK,
932 933
      G,
      subframe);
934
#endif
935

knopp's avatar
knopp committed
936
  if (ulsch_harq->round == 0) {
937
    // This is a new packet, so compute quantities regarding segmentation
knopp's avatar
knopp committed
938
    ulsch_harq->B = A+24;
939
    lte_segmentation(NULL,
940 941 942 943 944 945 946 947
                     NULL,
                     ulsch_harq->B,
                     &ulsch_harq->C,
                     &ulsch_harq->Cplus,
                     &ulsch_harq->Cminus,
                     &ulsch_harq->Kplus,
                     &ulsch_harq->Kminus,
                     &ulsch_harq->F);
948 949 950
    //  CLEAR LLR's HERE for first packet in process
  }

951
  //  printf("after segmentation c[%d] = %p\n",0,ulsch_harq->c[0]);
952 953

  sumKr = 0;
954 955

  for (r=0; r<ulsch_harq->C; r++) {
knopp's avatar
knopp committed
956 957
    if (r<ulsch_harq->Cminus)
      Kr = ulsch_harq->Kminus;
958
    else
knopp's avatar
knopp committed
959
      Kr = ulsch_harq->Kplus;
960

961 962
    sumKr += Kr;
  }
963

964
  if (sumKr==0) {
965
    LOG_N(PHY,"[eNB %d] ulsch_decoding.c: FATAL sumKr is 0!\n",eNB->Mod_id);
966
    LOG_D(PHY,"ulsch_decoding (Nid_cell %d, rnti %x, x2 %x): harq_pid %d round %d, RV %d, mcs %d, O_RI %d, O_ACK %d, G %d, subframe %d\n",
967 968 969 970 971 972 973 974 975
          frame_parms->Nid_cell,ulsch->rnti,x2,
          harq_pid,
          ulsch_harq->round,
          ulsch_harq->rvidx,
          ulsch_harq->mcs,
          ulsch_harq->O_RI,
          ulsch_harq->O_ACK,
          G,
          subframe);
gauthier's avatar
gauthier committed
976
    mac_xface->macphy_exit("ulsch_decoding.c: FATAL sumKr is 0!");
977 978
    return(-1);
  }
979

980
  // Compute Q_ri
knopp's avatar
knopp committed
981
  Qprime = ulsch_harq->O_RI*ulsch_harq->Msc_initial*ulsch_harq->Nsymb_initial * ulsch->beta_offset_ri_times8;
982 983 984 985 986 987

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

989 990 991 992 993 994 995 996 997 998
    if (Qprime > 4*nb_rb * 12)
      Qprime = 4*nb_rb * 12;
  }

  Q_RI = Q_m*Qprime;
  Qprime_RI = Qprime;


  // Compute Q_ack

knopp's avatar
knopp committed
999
  Qprime = ulsch_harq->O_ACK*ulsch_harq->Msc_initial*ulsch_harq->Nsymb_initial * ulsch->beta_offset_harqack_times8;
1000

1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
  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
knopp's avatar
knopp committed
1014
  printf("ulsch_decoding.c: Qprime_ACK %d, Msc_initial %d, Nsymb_initial %d, sumKr %d\n",
knopp's avatar
knopp committed
1015
      Qprime_ACK,ulsch_harq->Msc_initial,ulsch_harq->Nsymb_initial,sumKr);
1016
#endif
1017

1018
  // Compute Q_cqi
knopp's avatar
knopp committed
1019
  if (ulsch_harq->Or1 < 12)
1020
    L=0;
1021
  else
1022
    L=8;
1023

1024
  // NOTE: we have to handle the case where we have a very small number of bits (condition on pg. 26 36.212)
knopp's avatar
knopp committed
1025 1026
  if (ulsch_harq->Or1 > 0)
    Qprime = (ulsch_harq->Or1 + L) * ulsch_harq->Msc_initial*ulsch_harq->Nsymb_initial * ulsch->beta_offset_cqi_times8;
1027 1028 1029
  else
    Qprime=0;

1030
  if (Qprime > 0) {  // check if ceiling is larger than floor in Q' expression
1031 1032 1033 1034 1035 1036
    if ((Qprime % (8*sumKr)) > 0)
      Qprime = 1+(Qprime/(8*sumKr));
    else
      Qprime = Qprime/(8*sumKr);
  }

knopp's avatar
knopp committed
1037
  G = nb_rb * (12 * Q_m) * (ulsch_harq->Nsymb_pusch);
1038

1039

1040 1041

  Q_CQI = Q_m * Qprime;
knopp's avatar
knopp committed
1042
#ifdef DEBUG_ULSCH_DECODING
knopp's avatar
knopp committed
1043
  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);
knopp's avatar
knopp committed
1044
#endif
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055

  G = G - Q_RI - Q_CQI;

  if ((int)G < 0) {
    LOG_E(PHY,"FATAL: ulsch_decoding.c G < 0 (%d) : Q_RI %d, Q_CQI %d\n",G,Q_RI,Q_CQI);
    return(-1);
  }

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

1056

1057
  // Demultiplexing/Deinterleaving of PUSCH/ACK/RI/CQI
1058
  start_meas(&eNB->ulsch_demultiplexing_stats);
1059
  Hpp = Hprime + Qprime_RI;
1060

knopp's avatar
knopp committed
1061
  Cmux       = ulsch_harq->Nsymb_pusch;
1062
  Rmux_prime = Hpp/Cmux;
1063

1064 1065 1066
  // Clear "tag" interleaving matrix to allow for CQI/DATA identification
  memset(ytag,0,Cmux*Rmux_prime);

1067

1068 1069

  i=0;
knopp's avatar
knopp committed
1070
  memset(y,LTE_NULL,Q_m*Hpp);
1071 1072 1073 1074 1075

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


1076
  s = lte_gold_unscram(&x1, &x2, 1);
1077
  i2=0;
1078 1079

  for (i=0; i<((Hpp*Q_m)>>5); i++) {
1080
    /*
1081
    for (j=0; j<32; j++) {
1082 1083
      cseq[i2++] = (int16_t)((((s>>j)&1)<<1)-1);
    }
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
    */
#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);
1104 1105
  }

1106

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

1109 1110 1111 1112
  if (frame_parms->Ncp == 0)
    columnset = cs_ri_normal;
  else
    columnset = cs_ri_extended;
1113 1114 1115 1116

  j=0;

  for (i=0; i<Qprime_RI; i++) {
1117
    r = Rmux_prime - 1 - (i>>2);
1118 1119
    /*
    for (q=0;q<Q_m;q++)
1120 1121
      ytag2[q+(Q_m*((r*Cmux) + columnset[j]))]  = q_RI[(q+(Q_m*i))%len_RI];
    */
1122
    off =((Rmux_prime*Q_m*columnset[j])+(r*Q_m));
1123
    cseq[off+1] = cseq[off];  // PUSCH_y
1124 1125

    for (q=2; q<Q_m; q++)
1126 1127 1128 1129 1130 1131
      cseq[off+q] = -1;    // PUSCH_x

    j=(j+3)&3;

  }

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

1134 1135 1136 1137 1138 1139 1140
  // HARQ-ACK Bits (Note these overwrite some bits)
  if (frame_parms->Ncp == 0)
    columnset = cs_ack_normal;
  else
    columnset = cs_ack_extended;

  j=0;
1141 1142

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

knopp's avatar
knopp committed
1146
    if (ulsch_harq->O_ACK == 1) {
1147
      if (ulsch->bundling==0)
1148 1149 1150 1151 1152 1153 1154
        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
1155
    }
1156

1157
#ifdef DEBUG_ULSCH_DECODING
knopp's avatar
knopp committed
1158
    printf("ulsch_decoding.c: ACK i %d, r %d, j %d, ColumnSet[j] %d\n",i,r,j,columnset[j]);
1159 1160 1161 1162 1163 1164 1165
#endif
    j=(j+3)&3;
  }



  i=0;
1166

1167 1168
  switch (Q_m) {
  case 2:
1169
    for (j=0; j<Cmux; j++) {
1170
      i2=j<<1;
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181

      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);
1182 1183
      }
    }
1184

1185
    break;
1186

1187
  case 4:
1188
    for (j=0; j<Cmux; j++) {
1189
      i2=j<<2;
1190 1191

      for (r=0; r<Rmux_prime; r++) {
1192
	/*
1193 1194 1195 1196 1197 1198 1199 1200 1201
        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);
1202
	*/
1203
	// slightly more optimized version (equivalent to above) for 16QAM to improve computational performance
1204 1205 1206
	*(__m64 *)&y[i2] = _mm_sign_pi16(*(__m64*)&ulsch_llr[i],*(__m64*)&cseq[i]);i+=4;i2+=(Cmux<<2);


1207 1208
      }
    }
1209 1210 1211

    break;

1212
  case 6:
1213
    for (j=0; j<Cmux; j++) {
1214
      i2=j*6;
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229

      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);
1230 1231
      }
    }
1232

1233 1234 1235 1236
    break;
  }


1237

1238 1239

  if (i!=(H+Q_RI))
1240
    LOG_D(PHY,"ulsch_decoding.c: Error in input buffer length (j %d, H+Q_RI %d)\n",i,H+Q_RI);
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250

  // 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;

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

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

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

knopp's avatar
knopp committed
1267
  if (ulsch_harq->O_ACK == 2) {
1268 1269 1270 1271
    switch (Q_m) {
    case 2:
      len_ACK = 6;
      break;
1272

1273 1274 1275
    case 4:
      len_ACK = 12;
      break;
1276

1277 1278 1279 1280 1281
    case 6:
      len_ACK = 18;
      break;
    }
  }
1282

knopp's avatar
knopp committed
1283
  if (ulsch_harq->O_ACK > 2) {
1284 1285 1286 1287
    LOG_E(PHY,"ulsch_decoding: FATAL, ACK cannot be more than 2 bits yet\n");
    return(-1);
  }

1288
  for (i=0; i<len_ACK; i++)
knopp's avatar
knopp committed
1289
    ulsch_harq->q_ACK[i] = 0;
1290 1291


1292
  for (i=0; i<Qprime_ACK; i++) {
1293
    r = Rmux_prime -1 - (i>>2);
1294 1295

    for (q=0; q<Q_m; q++) {
1296
      if (y[q+(Q_m*((r*Cmux) + columnset[j]))]!=0)
1297
        ulsch_harq->q_ACK[(q+(Q_m*i))%len_ACK] += y[q+(Q_m*((r*Cmux) + columnset[j]))];
1298 1299
      y[q+(Q_m*((r*Cmux) + columnset[j]))]=0;  // NULL LLRs in ACK positions
    }
1300

1301 1302 1303
    j=(j+3)&3;
  }

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

1306
  // RI BITS
1307

knopp's avatar
knopp committed
1308
  if (ulsch_harq->O_RI == 1) {
1309 1310
    switch (Q_m) {
    case 2:
1311
      len_RI=2;
1312
      break;
1313

1314 1315 1316
    case 4:
      len_RI=4;
      break;
1317

1318 1319 1320 1321 1322 1323
    case 6:
      len_RI=6;
      break;
    }
  }

knopp's avatar
knopp committed
1324
  if (ulsch_harq->O_RI > 1) {
1325 1326 1327 1328
    LOG_E(PHY,"ulsch_decoding: FATAL, RI cannot be more than 1 bit yet\n");
    return(-1);
  }

1329
  for (i=0; i<len_RI; i++)
knopp's avatar
knopp committed
1330
    ulsch_harq->q_RI[i] = 0;
1331

1332 1333 1334 1335
  if (frame_parms->Ncp == 0)
    columnset = cs_ri_normal;
  else
    columnset = cs_ri_extended;
1336 1337 1338 1339

  j=0;

  for (i=0; i<Qprime_RI; i++) {
1340
    r = Rmux_prime -1 - (i>>2);
1341 1342

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

1345 1346 1347 1348
    ytag[(r*Cmux) + columnset[j]] = LTE_NULL;
    j=(j+3)&3;
  }

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

1351
  // CQI and Data bits
1352 1353 1354
  j=0;
  j2=0;

1355
  //  r=0;
1356 1357 1358 1359 1360 1361
  if (Q_RI>0) {
    for (i=0; i<(Q_CQI/Q_m); i++) {
      
      while (ytag[j]==LTE_NULL) {
	j++;
	j2+=Q_m;
1362
      }
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
      
      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;
      }
      
1376 1377
      j2+=Q_m;
    }
1378
    
1379
        
1380 1381
    switch (Q_m) {
    case 2:
1382
      for (iprime=0; iprime<G;) {
1383 1384 1385 1386 1387 1388 1389 1390
	while (ytag[j]==LTE_NULL) {
	  j++;
	  j2+=2;
	}
	
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	
1391
      }
1392 1393 1394 1395 1396
      
      
      break;
      
    case 4:
1397
      for (iprime=0; iprime<G;) {
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
	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++];
	
1408
      }
1409 1410 1411 1412
      
      break;
      
    case 6:
1413
      for (iprime=0; iprime<G;) {
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
	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++];
	
1426
      }
1427 1428
      
      break;
1429

1430
    }
1431 1432 1433
    

  } // Q_RI>0
1434
  else {
1435

1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
    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;
1446
      }
1447 1448
      
      j2+=Q_m;
1449
    }
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462

#if defined(__x86_64__)||defined(__i386__)
#ifndef __AVX2
    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]);
#endif 
1463
  }
1464 1465 1466

  stop_meas(&eNB->ulsch_demultiplexing_stats);

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

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

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

knopp's avatar
knopp committed
1474
  if (ulsch_harq->O_ACK == 1) {
1475 1476 1477 1478 1479 1480 1481
    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;
1482
  }
1483

knopp's avatar
knopp committed
1484
  if (ulsch_harq->O_ACK == 2) {
1485 1486 1487
    switch (Q_m) {

    case 2:
knopp's avatar
knopp committed
1488 1489 1490
      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];
1491 1492
      break;
    case 4:
knopp's avatar
knopp committed
1493 1494 1495
      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];
1496 1497
      break;
    case 6:
knopp's avatar
knopp committed
1498
      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];
1499 1500
      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];
1501 1502
      break;
    }
1503

knopp's avatar
knopp committed
1504 1505 1506 1507
    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];
1508 1509

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

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


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

knopp's avatar
knopp committed
1524
    metric_new = -ulsch_harq->q_ACK[0]-ulsch_harq->q_ACK[1]-ulsch_harq->q_ACK[2];
1525 1526

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

  // RI

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