dci.c 96 KB
Newer Older
1
/*******************************************************************************
ghaddab's avatar
ghaddab committed
2 3
    OpenAirInterface 
    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.
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 17 18 19
    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is 
   included in this distribution in the file called "COPYING". If not, 
   see <http://www.gnu.org/licenses/>.
20 21

  Contact Information
ghaddab's avatar
ghaddab committed
22 23 24 25 26
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
  OpenAirInterface Dev  : openair4g-devel@eurecom.fr
  
  Address      : Eurecom, Compus SophiaTech 450, route des chappes, 06451 Biot, 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

/*! \file PHY/LTE_TRANSPORT/dci.c
* \brief Implements PDCCH physical channel TX/RX procedures (36.211) and DCI encoding/decoding (36.212/36.213). Current LTE compliance V8.6 2009-03.
* \author R. Knopp
* \date 2011
* \version 0.1
* \company Eurecom
* \email: knopp@eurecom.fr
* \note
* \warning
*/
#ifdef USER_MODE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#endif
#include "PHY/defs.h"
#include "PHY/extern.h"
#include "SCHED/defs.h"
48
#include "SIMULATION/TOOLS/defs.h" // for taus 
49 50 51 52 53 54 55
#include <emmintrin.h>
#include <xmmintrin.h>
#ifdef __SSE3__
#include <pmmintrin.h>
#include <tmmintrin.h>
#endif

56
#include "assertions.h"
57 58 59 60 61 62 63 64 65 66 67 68 69

//#define DEBUG_DCI_ENCODING 1
//#define DEBUG_DCI_DECODING 1
//#define DEBUG_PHY
 
//#undef ALL_AGGREGATION

#ifndef __SSE3__
__m128i zero2;
#define _mm_abs_epi16(xmmx) _mm_xor_si128((xmmx),_mm_cmpgt_epi16(zero2,(xmmx)))
#define _mm_sign_epi16(xmmx,xmmy) _mm_xor_si128((xmmx),_mm_cmpgt_epi16(zero2,(xmmy)))
#endif

70 71
//extern uint16_t phich_reg[MAX_NUM_PHICH_GROUPS][3];
//extern uint16_t pcfich_reg[4];
72

73
uint32_t check_phich_reg(LTE_DL_FRAME_PARMS *frame_parms,uint32_t kprime,uint8_t lprime,uint8_t mi) {
74

75 76 77 78
  uint16_t i;
  uint16_t Ngroup_PHICH = (frame_parms->phich_config_common.phich_resource*frame_parms->N_RB_DL)/48;
  uint16_t mprime;
  uint16_t *pcfich_reg = frame_parms->pcfich_reg;
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

  if ((lprime>0) && (frame_parms->Ncp==0) )
    return(0);

  //  printf("check_phich_reg : mi %d\n",mi);

  // compute REG based on symbol
  if ((lprime == 0)||
      ((lprime==1)&&(frame_parms->nb_antennas_tx_eNB == 4)))
    mprime = kprime/6;
  else
    mprime = kprime>>2;

  // check if PCFICH uses mprime
  if ((lprime==0) && 
      ((mprime == pcfich_reg[0]) ||
       (mprime == pcfich_reg[1]) ||
       (mprime == pcfich_reg[2]) ||
       (mprime == pcfich_reg[3]))) {
#ifdef DEBUG_DCI_ENCODING
    msg("[PHY] REG %d allocated to PCFICH\n",mprime);
#endif
    return(1);
  }

  // handle Special subframe case for TDD !!!

  //  printf("Checking phich_reg %d\n",mprime);
  if (mi > 0) {
    if (((frame_parms->phich_config_common.phich_resource*frame_parms->N_RB_DL)%48) > 0)
      Ngroup_PHICH++;
    
    if (frame_parms->Ncp == 1) {
      Ngroup_PHICH<<=1;
    }
    
    
    
    for (i=0;i<Ngroup_PHICH;i++) {
      if ((mprime == frame_parms->phich_reg[i][0]) || 
	  (mprime == frame_parms->phich_reg[i][1]) || 
	  (mprime == frame_parms->phich_reg[i][2]))  {
#ifdef DEBUG_DCI_ENCODING
	msg("[PHY] REG %d (lprime %d) allocated to PHICH\n",mprime,lprime);
#endif
	return(1);
      }
    }
  }
  return(0);
}

131
uint16_t extract_crc(uint8_t *dci,uint8_t dci_len) {
132

133 134
  uint16_t crc16;
  //  uint8_t i;
135 136

  /*
137 138
  uint8_t crc;
  crc = ((uint16_t *)dci)[DCI_LENGTH>>4];
139 140 141
  printf("crc1: %x, shift %d (DCI_LENGTH %d)\n",crc,DCI_LENGTH&0xf,DCI_LENGTH);
  crc = (crc>>(DCI_LENGTH&0xf));
  // clear crc bits
142 143 144
  ((uint16_t *)dci)[DCI_LENGTH>>4] &= (0xffff>>(16-(DCI_LENGTH&0xf)));
  printf("crc2: %x, dci0 %x\n",crc,((int16_t *)dci)[DCI_LENGTH>>4]);
  crc |= (((uint16_t *)dci)[1+(DCI_LENGTH>>4)])<<(16-(DCI_LENGTH&0xf));
145
  // clear crc bits
146
  (((uint16_t *)dci)[1+(DCI_LENGTH>>4)]) = 0;
147 148 149 150 151 152 153
  printf("extract_crc: crc %x\n",crc);
  */
#ifdef DEBUG_DCI_DECODING  
  msg("dci_crc (%x,%x,%x), dci_len&0x7=%d\n",dci[dci_len>>3],dci[1+(dci_len>>3)],dci[2+(dci_len>>3)],
	 dci_len&0x7);
#endif
  if ((dci_len&0x7) > 0) {
154 155
    ((uint8_t *)&crc16)[0] = dci[1+(dci_len>>3)]<<(dci_len&0x7) | dci[2+(dci_len>>3)]>>(8-(dci_len&0x7));
    ((uint8_t *)&crc16)[1] = dci[(dci_len>>3)]<<(dci_len&0x7) | dci[1+(dci_len>>3)]>>(8-(dci_len&0x7));
156 157
  }
  else {
158 159
    ((uint8_t *)&crc16)[0] = dci[1+(dci_len>>3)];
    ((uint8_t *)&crc16)[1] = dci[(dci_len>>3)];
160 161 162 163 164 165 166 167 168
  }

#ifdef DEBUG_DCI_DECODING  
  msg("dci_crc =>%x\n",crc16);
#endif

  //  dci[(dci_len>>3)]&=(0xffff<<(dci_len&0xf));
  //  dci[(dci_len>>3)+1] = 0;
  //  dci[(dci_len>>3)+2] = 0;
169
  return((uint16_t)crc16);
170 171 172 173 174

}



175 176
static uint8_t d[3*(MAX_DCI_SIZE_BITS + 16) + 96];
static uint8_t w[3*3*(MAX_DCI_SIZE_BITS+16)];
177

178 179 180 181 182
void dci_encoding(uint8_t *a,
		  uint8_t A,
		  uint16_t E,
		  uint8_t *e,
		  uint16_t rnti) {
183 184


185 186
  uint8_t D = (A + 16);
  uint32_t RCC;
187 188

#ifdef DEBUG_DCI_ENCODING
189
  int32_t i;
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
#endif
  // encode dci 

#ifdef DEBUG_DCI_ENCODING
  msg("Doing DCI encoding for %d bits, e %p, rnti %x\n",A,e,rnti);
#endif

  memset((void *)d,LTE_NULL,96);

  ccodelte_encode(A,2,a,d+96,rnti);

#ifdef DEBUG_DCI_ENCODING
  for (i=0;i<16+A;i++)
    msg("%d : (%d,%d,%d)\n",i,*(d+96+(3*i)),*(d+97+(3*i)),*(d+98+(3*i)));
#endif
  
#ifdef DEBUG_DCI_ENCODING
  msg("Doing DCI interleaving for %d coded bits, e %p\n",D*3,e);
#endif
  RCC = sub_block_interleaving_cc(D,d+96,w);

#ifdef DEBUG_DCI_ENCODING
  msg("Doing DCI rate matching for %d channel bits, RCC %d, e %p\n",E,RCC,e);
#endif
  lte_rate_matching_cc(RCC,E,w,e);


}


220 221 222 223 224
uint8_t *generate_dci0(uint8_t *dci,
		  uint8_t *e,
		  uint8_t DCI_LENGTH,
		  uint8_t aggregation_level,
		  uint16_t rnti) {
225
  
226 227
  uint16_t coded_bits;
  uint8_t dci_flip[8];
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

  if (aggregation_level>3) {
    msg("dci.c: generate_dci FATAL, illegal aggregation_level %d\n",aggregation_level);
    return NULL;
  }

  coded_bits = 72 * (1<<aggregation_level);
  /*  

#ifdef DEBUG_DCI_ENCODING
  for (i=0;i<1+((DCI_LENGTH+16)/8);i++)
    msg("i %d : %x\n",i,dci[i]);
#endif
  */
  if (DCI_LENGTH<=32){
    dci_flip[0] = dci[3];
    dci_flip[1] = dci[2];
    dci_flip[2] = dci[1];
    dci_flip[3] = dci[0];   
  }
  else {
    dci_flip[0] = dci[7];
    dci_flip[1] = dci[6];
    dci_flip[2] = dci[5];
    dci_flip[3] = dci[4];
    dci_flip[4] = dci[3];
    dci_flip[5] = dci[2];
    dci_flip[6] = dci[1];
    dci_flip[7] = dci[0];
  }
	
  dci_encoding(dci_flip,DCI_LENGTH,coded_bits,e,rnti);

  return(e+coded_bits);
}

264
uint32_t Y;
265 266 267 268 269 270 271 272

#define CCEBITS 72
#define CCEPERSYMBOL 33  // This is for 1200 RE
#define CCEPERSYMBOL0 22  // This is for 1200 RE
#define DCI_BITS_MAX ((2*CCEPERSYMBOL+CCEPERSYMBOL0)*CCEBITS)
#define Msymb (DCI_BITS_MAX/2)
//#define Mquad (Msymb/4)

273
static uint32_t bitrev_cc_dci[32] = {1,17,9,25,5,21,13,29,3,19,11,27,7,23,15,31,0,16,8,24,4,20,12,28,2,18,10,26,6,22,14,30};
274 275
static mod_sym_t wtemp[2][Msymb];

276
void pdcch_interleaving(LTE_DL_FRAME_PARMS *frame_parms,mod_sym_t **z, mod_sym_t **wbar,uint8_t n_symbols_pdcch,uint8_t mi) {
277 278

  mod_sym_t *wptr,*wptr2,*zptr;
279 280 281 282
  uint32_t Mquad = get_nquad(n_symbols_pdcch,frame_parms,mi);
  uint32_t RCC = (Mquad>>5), ND;
  uint32_t row,col,Kpi,index;
  int32_t i,k,a;
283
#ifdef RM_DEBUG
284
  int32_t nulled=0;
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 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
#endif
  //  msg("[PHY] PDCCH Interleaving Mquad %d (Nsymb %d)\n",Mquad,n_symbols_pdcch);
  if ((Mquad&0x1f) > 0)
    RCC++;
  Kpi = (RCC<<5);
  ND = Kpi - Mquad;

  k=0;
  for (col=0;col<32;col++) {
    index = bitrev_cc_dci[col];

    for (row=0;row<RCC;row++) {
      //msg("col %d, index %d, row %d\n",col,index,row);
      if (index>=ND) {
	for (a=0;a<frame_parms->nb_antennas_tx_eNB;a++){
	  //msg("a %d k %d\n",a,k);

	  wptr = &wtemp[a][k<<2];
	  zptr = &z[a][(index-ND)<<2];

	  //msg("wptr=%p, zptr=%p\n",wptr,zptr);

	  wptr[0] = zptr[0];
	  wptr[1] = zptr[1];
	  wptr[2] = zptr[2];
	  wptr[3] = zptr[3];
	}
	k++;
      }
      index+=32;
    }
  }

  // permutation
  for (i=0;i<Mquad;i++) {

    for (a=0;a<frame_parms->nb_antennas_tx_eNB;a++) {
      
      //wptr  = &wtemp[a][i<<2];
      //wptr2 = &wbar[a][((i+frame_parms->Nid_cell)%Mquad)<<2];
      wptr = &wtemp[a][((i+frame_parms->Nid_cell)%Mquad)<<2];
      wptr2 = &wbar[a][i<<2];
      wptr2[0] = wptr[0];
      wptr2[1] = wptr[1];
      wptr2[2] = wptr[2];
      wptr2[3] = wptr[3];
    }
  }
}

335
void pdcch_demapping(uint16_t *llr,uint16_t *wbar,LTE_DL_FRAME_PARMS *frame_parms,uint8_t num_pdcch_symbols,uint8_t mi) {
336

337 338 339
  uint32_t i, lprime;
  uint16_t kprime,kprime_mod12,mprime,symbol_offset,tti_offset,tti_offset0;
  int16_t re_offset,re_offset0;
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

  // This is the REG allocation algorithm from 36-211, second part of Section 6.8.5

  int Msymb2;

  switch (frame_parms->N_RB_DL) {
  case 100:
    Msymb2 = Msymb;
    break;
  case 75:
    Msymb2 = 3*Msymb/4;
    break;
  case 50:
    Msymb2 = Msymb>>1;
    break;
  case 25:
    Msymb2 = Msymb>>2;
    break;
  case 15:
    Msymb2 = Msymb*15/100;
    break;
  case 6:
    Msymb2 = Msymb*6/100;
    break;
  default:
    Msymb2 = Msymb>>2;
    break;
  }
  mprime=0;


  re_offset = 0;
  re_offset0 = 0; // counter for symbol with pilots (extracted outside!)
  
  for (kprime=0;kprime<frame_parms->N_RB_DL*12;kprime++) {
    for (lprime=0;lprime<num_pdcch_symbols;lprime++) {

377
      symbol_offset = (uint32_t)frame_parms->N_RB_DL*12*lprime;
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
  
      tti_offset = symbol_offset + re_offset;
      tti_offset0 = symbol_offset + re_offset0;
      // if REG is allocated to PHICH, skip it
      if (check_phich_reg(frame_parms,kprime,lprime,mi) == 1) {
	//msg("dci_demapping : skipping REG %d\n",(lprime==0)?kprime/6 : kprime>>2);
	if ((lprime == 0)&&((kprime%6)==0))
	  re_offset0+=4;
      }
      else {  // not allocated to PHICH/PCFICH
	//	msg("dci_demapping: REG %d\n",(lprime==0)?kprime/6 : kprime>>2);
	if (lprime == 0) {
	  // first symbol, or second symbol+4 TX antennas skip pilots
	  kprime_mod12 = kprime%12;
	  if ((kprime_mod12 == 0) || (kprime_mod12 == 6)) {
	    // kprime represents REG	    

	    for (i=0;i<4;i++) {
	      wbar[mprime] = llr[tti_offset0+i];
#ifdef DEBUG_DCI_DECODING
	      msg("[PHY] PDCCH demapping mprime %d => %d (symbol %d re %d) -> (%d,%d)\n",mprime,tti_offset0,symbol_offset,re_offset0,*(char*)&wbar[mprime],*(1+(char*)&wbar[mprime]));
#endif
	      mprime++;
	      re_offset0++;
	    }
	  }
	}
	else if ((lprime==1)&&(frame_parms->nb_antennas_tx_eNB == 4)) {  
	  // LATER!!!!
	}
	else { // no pilots in this symbol
	  kprime_mod12 = kprime%12;
	  if ((kprime_mod12 == 0) || (kprime_mod12 == 4) || (kprime_mod12 == 8)) {
	    // kprime represents REG	    
	    for (i=0;i<4;i++) {
	      wbar[mprime] = llr[tti_offset+i];
#ifdef DEBUG_DCI_DECODING
	      msg("[PHY] PDCCH demapping mprime %d => %d (symbol %d re %d) -> (%d,%d)\n",mprime,tti_offset,symbol_offset,re_offset+i,*(char*)&wbar[mprime],*(1+(char*)&wbar[mprime]));
#endif
		mprime++;
	    }
	  }  // is representative
	} // no pilots case	
      } // not allocated to PHICH/PCFICH

      // Stop when all REGs are copied in
      if (mprime>=Msymb2)
	break;
    } //lprime loop
    re_offset++;

  } // kprime loop
}

432 433
static uint16_t wtemp_rx[Msymb];
void pdcch_deinterleaving(LTE_DL_FRAME_PARMS *frame_parms,uint16_t *z, uint16_t *wbar,uint8_t number_pdcch_symbols,uint8_t mi) {
434

435
  uint16_t *wptr,*zptr,*wptr2;
436

437 438 439 440
  uint16_t Mquad=get_nquad(number_pdcch_symbols,frame_parms,mi);
  uint32_t RCC = (Mquad>>5), ND;
  uint32_t row,col,Kpi,index;
  int32_t i,k;
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498


  //  printf("Mquad %d, RCC %d\n",Mquad,RCC);

  if (!z) {
    msg("dci.c: pdcch_deinterleaving: FATAL z is Null\n");
    return;
  }
  // undo permutation
  for (i=0;i<Mquad;i++) {
    //wptr = &wtemp_rx[i<<2];
    //wptr2 = &wbar[((i+frame_parms->Nid_cell)%Mquad)<<2];
    wptr = &wtemp_rx[((i+frame_parms->Nid_cell)%Mquad)<<2];
    wptr2 = &wbar[i<<2];

    wptr[0] = wptr2[0];
    wptr[1] = wptr2[1];
    wptr[2] = wptr2[2];
    wptr[3] = wptr2[3];
    /*
    msg("pdcch_deinterleaving (%p,%p): quad %d -> (%d,%d %d,%d %d,%d %d,%d)\n",wptr,wptr2,i,
	((char*)wptr2)[0],
	((char*)wptr2)[1],
	((char*)wptr2)[2],
	((char*)wptr2)[3],
	((char*)wptr2)[4],
	((char*)wptr2)[5],
	((char*)wptr2)[6],
	((char*)wptr2)[7]);
    */	

  }

  if ((Mquad&0x1f) > 0)
    RCC++;
  Kpi = (RCC<<5);
  ND = Kpi - Mquad;

  k=0;
  for (col=0;col<32;col++) {
    index = bitrev_cc_dci[col];

    for (row=0;row<RCC;row++) {
      //      printf("row %d, index %d, Nd %d\n",row,index,ND);
      if (index>=ND) {



	wptr = &wtemp_rx[k<<2];
	zptr = &z[(index-ND)<<2];
	
	zptr[0] = wptr[0];
	zptr[1] = wptr[1];
	zptr[2] = wptr[2];
	zptr[3] = wptr[3];

	/*
	printf("deinterleaving ; k %d, index-Nd %d  => (%d,%d,%d,%d,%d,%d,%d,%d)\n",k,(index-ND),
499 500 501 502 503 504 505 506
	       ((int8_t *)wptr)[0],
	       ((int8_t *)wptr)[1],
	       ((int8_t *)wptr)[2],
	       ((int8_t *)wptr)[3],
	       ((int8_t *)wptr)[4],
	       ((int8_t *)wptr)[5],
	       ((int8_t *)wptr)[6],
	       ((int8_t *)wptr)[7]);
507 508 509 510 511 512 513 514 515 516 517 518
	*/
	k++;
      }
      index+=32;
            
    }
  }

  for (i=0;i<Mquad;i++) {
    zptr = &z[i<<2];
    /*
    printf("deinterleaving ; quad %d  => (%d,%d,%d,%d,%d,%d,%d,%d)\n",i,
519 520 521 522 523 524 525 526
	   ((int8_t *)zptr)[0],
	   ((int8_t *)zptr)[1],
	   ((int8_t *)zptr)[2],
	   ((int8_t *)zptr)[3],
	   ((int8_t *)zptr)[4],
	   ((int8_t *)zptr)[5],
	   ((int8_t *)zptr)[6],
	   ((int8_t *)zptr)[7]);
527 528 529 530 531 532
    */
  }
  
}


533 534 535 536 537 538 539
int32_t pdcch_qpsk_qpsk_llr(LTE_DL_FRAME_PARMS *frame_parms,
			 int32_t **rxdataF_comp,
			 int32_t **rxdataF_comp_i,
			 int32_t **rho_i,
			 int16_t *pdcch_llr16,
			 int16_t *pdcch_llr8in,
			 uint8_t symbol) {
540 541 542 543 544

  __m128i *rxF=(__m128i*)&rxdataF_comp[0][(symbol*frame_parms->N_RB_DL*12)];
  __m128i *rxF_i=(__m128i*)&rxdataF_comp_i[0][(symbol*frame_parms->N_RB_DL*12)];
  __m128i *rho=(__m128i*)&rho_i[0][(symbol*frame_parms->N_RB_DL*12)];
  __m128i *llr128;
545
  int32_t i;
546
  char *pdcch_llr8;
547
  int16_t *pdcch_llr;
548 549 550 551 552 553 554 555 556 557 558 559
  pdcch_llr8 = (char *)&pdcch_llr8in[symbol*frame_parms->N_RB_DL*12];
  pdcch_llr = &pdcch_llr16[symbol*frame_parms->N_RB_DL*12];

  //  printf("dlsch_qpsk_qpsk: symbol %d\n",symbol);
  
  llr128 = (__m128i*)pdcch_llr;

  if (!llr128) {
    msg("dlsch_qpsk_qpsk_llr: llr is null, symbol %d\n",symbol);
    return -1;
  }

560 561 562 563
  qpsk_qpsk((int16_t *)rxF,
	    (int16_t *)rxF_i,
	    (int16_t *)llr128,
	    (int16_t *)rho,
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
	    frame_parms->N_RB_DL*12);

  //prepare for Viterbi which accepts 8 bit, but prefers 4 bit, soft input.
  for (i=0;i<(frame_parms->N_RB_DL*24);i++) {
    if (*pdcch_llr>7)
      *pdcch_llr8=7;
    else if (*pdcch_llr<-8)
      *pdcch_llr8=-8;
    else
      *pdcch_llr8 = (char)(*pdcch_llr);

    pdcch_llr++;
    pdcch_llr8++;
  }

  return(0);
}


583 584
int32_t pdcch_llr(LTE_DL_FRAME_PARMS *frame_parms,
		   int32_t **rxdataF_comp,
585
		   char *pdcch_llr,
586
		   uint8_t symbol) {
587

588 589
  int16_t *rxF= (int16_t*) &rxdataF_comp[0][(symbol*frame_parms->N_RB_DL*12)];
  int32_t i;
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
  char *pdcch_llr8;

  pdcch_llr8 = &pdcch_llr[2*symbol*frame_parms->N_RB_DL*12];
 
  if (!pdcch_llr8) {
    msg("pdcch_qpsk_llr: llr is null, symbol %d\n",symbol);
    return(-1);
  }
  //    msg("pdcch qpsk llr for symbol %d (pos %d), llr offset %d\n",symbol,(symbol*frame_parms->N_RB_DL*12),pdcch_llr8-pdcch_llr);

  for (i=0;i<(frame_parms->N_RB_DL*((symbol==0) ? 16 : 24));i++) {

    if (*rxF>31)
      *pdcch_llr8=31;
    else if (*rxF<-32)
      *pdcch_llr8=-32;
    else
      *pdcch_llr8 = (char)(*rxF);

    //    printf("%d %d => %d\n",i,*rxF,*pdcch_llr8);
    rxF++;
    pdcch_llr8++;
  }

  return(0);

}

__m128i avg128P;

//compute average channel_level on each (TX,RX) antenna pair
621
void pdcch_channel_level(int32_t **dl_ch_estimates_ext,
622
			 LTE_DL_FRAME_PARMS *frame_parms,
623 624
			 int32_t *avg,
			 uint8_t nb_rb) {
625

626 627
  int16_t rb;
  uint8_t aatx,aarx;
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
  __m128i *dl_ch128;
  

  for (aatx=0;aatx<frame_parms->nb_antennas_tx_eNB;aatx++)
    for (aarx=0;aarx<frame_parms->nb_antennas_rx;aarx++) {
      //clear average level
      avg128P = _mm_xor_si128(avg128P,avg128P);
      dl_ch128=(__m128i *)&dl_ch_estimates_ext[(aatx<<1)+aarx][frame_parms->N_RB_DL*12];

      for (rb=0;rb<nb_rb;rb++) {
    
	avg128P = _mm_add_epi32(avg128P,_mm_madd_epi16(dl_ch128[0],dl_ch128[0]));
	avg128P = _mm_add_epi32(avg128P,_mm_madd_epi16(dl_ch128[1],dl_ch128[1]));
	avg128P = _mm_add_epi32(avg128P,_mm_madd_epi16(dl_ch128[2],dl_ch128[2]));

	dl_ch128+=3;	
	/*
	  if (rb==0) {
	  print_shorts("dl_ch128",&dl_ch128[0]);
	  print_shorts("dl_ch128",&dl_ch128[1]);
	  print_shorts("dl_ch128",&dl_ch128[2]);
	  }
	*/
      }

653 654 655 656
      avg[(aatx<<1)+aarx] = (((int32_t*)&avg128P)[0] + 
			     ((int32_t*)&avg128P)[1] + 
			     ((int32_t*)&avg128P)[2] + 
			     ((int32_t*)&avg128P)[3])/(nb_rb*12);
657 658 659 660 661 662 663 664 665 666 667

      //            msg("Channel level : %d\n",avg[(aatx<<1)+aarx]);
    }
  _mm_empty();
  _m_empty();

}

__m128i mmtmpPD0,mmtmpPD1,mmtmpPD2,mmtmpPD3;

void pdcch_dual_stream_correlation(LTE_DL_FRAME_PARMS *frame_parms,
668 669 670 671 672
				   uint8_t symbol,
				   int32_t **dl_ch_estimates_ext,
				   int32_t **dl_ch_estimates_ext_i,
				   int32_t **dl_ch_rho_ext,
				   uint8_t output_shift) {
673

674
  uint16_t rb;
675
  __m128i *dl_ch128,*dl_ch128i,*dl_ch_rho128;
676
  uint8_t aarx;
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 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765

  //  printf("dlsch_dual_stream_correlation: symbol %d\n",symbol);


  for (aarx=0;aarx<frame_parms->nb_antennas_rx;aarx++) {

    dl_ch128          = (__m128i *)&dl_ch_estimates_ext[aarx][symbol*frame_parms->N_RB_DL*12];
    dl_ch128i         = (__m128i *)&dl_ch_estimates_ext_i[aarx][symbol*frame_parms->N_RB_DL*12];
    dl_ch_rho128      = (__m128i *)&dl_ch_rho_ext[aarx][symbol*frame_parms->N_RB_DL*12];


    for (rb=0;rb<frame_parms->N_RB_DL;rb++) {
      // multiply by conjugated channel
      mmtmpPD0 = _mm_madd_epi16(dl_ch128[0],dl_ch128i[0]);
      //	print_ints("re",&mmtmpPD0);
      
      // mmtmpD0 contains real part of 4 consecutive outputs (32-bit)
      mmtmpPD1 = _mm_shufflelo_epi16(dl_ch128[0],_MM_SHUFFLE(2,3,0,1));
      mmtmpPD1 = _mm_shufflehi_epi16(mmtmpPD1,_MM_SHUFFLE(2,3,0,1));
      mmtmpPD1 = _mm_sign_epi16(mmtmpPD1,*(__m128i*)&conjugate[0]);
      //	print_ints("im",&mmtmpPD1);
      mmtmpPD1 = _mm_madd_epi16(mmtmpPD1,dl_ch128i[0]);
      // mmtmpD1 contains imag part of 4 consecutive outputs (32-bit)
      mmtmpPD0 = _mm_srai_epi32(mmtmpPD0,output_shift);
      //	print_ints("re(shift)",&mmtmpPD0);
      mmtmpPD1 = _mm_srai_epi32(mmtmpPD1,output_shift);
      //	print_ints("im(shift)",&mmtmpPD1);
      mmtmpPD2 = _mm_unpacklo_epi32(mmtmpPD0,mmtmpPD1);
      mmtmpPD3 = _mm_unpackhi_epi32(mmtmpPD0,mmtmpPD1);
      //       	print_ints("c0",&mmtmpPD2);
      //	print_ints("c1",&mmtmpPD3);
      dl_ch_rho128[0] = _mm_packs_epi32(mmtmpPD2,mmtmpPD3);
      
      //print_shorts("rx:",dl_ch128_2);
      //print_shorts("ch:",dl_ch128);
      //print_shorts("pack:",rho128);
      
      // multiply by conjugated channel
      mmtmpPD0 = _mm_madd_epi16(dl_ch128[1],dl_ch128i[1]);
      // mmtmpPD0 contains real part of 4 consecutive outputs (32-bit)
      mmtmpPD1 = _mm_shufflelo_epi16(dl_ch128[1],_MM_SHUFFLE(2,3,0,1));
      mmtmpPD1 = _mm_shufflehi_epi16(mmtmpPD1,_MM_SHUFFLE(2,3,0,1));
      mmtmpPD1 = _mm_sign_epi16(mmtmpPD1,*(__m128i*)conjugate);
      mmtmpPD1 = _mm_madd_epi16(mmtmpPD1,dl_ch128i[1]);
      // mmtmpPD1 contains imag part of 4 consecutive outputs (32-bit)
      mmtmpPD0 = _mm_srai_epi32(mmtmpPD0,output_shift);
      mmtmpPD1 = _mm_srai_epi32(mmtmpPD1,output_shift);
      mmtmpPD2 = _mm_unpacklo_epi32(mmtmpPD0,mmtmpPD1);
      mmtmpPD3 = _mm_unpackhi_epi32(mmtmpPD0,mmtmpPD1);
      
      
      dl_ch_rho128[1] =_mm_packs_epi32(mmtmpPD2,mmtmpPD3);
      //print_shorts("rx:",dl_ch128_2+1);
      //print_shorts("ch:",dl_ch128+1);
      //print_shorts("pack:",rho128+1);	
      // multiply by conjugated channel
      mmtmpPD0 = _mm_madd_epi16(dl_ch128[2],dl_ch128i[2]);
      // mmtmpPD0 contains real part of 4 consecutive outputs (32-bit)
      mmtmpPD1 = _mm_shufflelo_epi16(dl_ch128[2],_MM_SHUFFLE(2,3,0,1));
      mmtmpPD1 = _mm_shufflehi_epi16(mmtmpPD1,_MM_SHUFFLE(2,3,0,1));
      mmtmpPD1 = _mm_sign_epi16(mmtmpPD1,*(__m128i*)conjugate);
      mmtmpPD1 = _mm_madd_epi16(mmtmpPD1,dl_ch128i[2]);
      // mmtmpPD1 contains imag part of 4 consecutive outputs (32-bit)
      mmtmpPD0 = _mm_srai_epi32(mmtmpPD0,output_shift);
      mmtmpPD1 = _mm_srai_epi32(mmtmpPD1,output_shift);
      mmtmpPD2 = _mm_unpacklo_epi32(mmtmpPD0,mmtmpPD1);
      mmtmpPD3 = _mm_unpackhi_epi32(mmtmpPD0,mmtmpPD1);
      
      dl_ch_rho128[2] = _mm_packs_epi32(mmtmpPD2,mmtmpPD3);
      //print_shorts("rx:",dl_ch128_2+2);
      //print_shorts("ch:",dl_ch128+2);
      //print_shorts("pack:",rho128+2);
      
      dl_ch128+=3;
      dl_ch128i+=3;
      dl_ch_rho128+=3;
      
    }	
    
  }
  
  _mm_empty();
  _m_empty();
  
  
}


void pdcch_detection_mrc_i(LTE_DL_FRAME_PARMS *frame_parms,
766 767 768 769 770
			 int32_t **rxdataF_comp,
			 int32_t **rxdataF_comp_i,
			 int32_t **rho,
			 int32_t **rho_i,
			 uint8_t symbol) {
771

772
  uint8_t aatx;
773 774

  __m128i *rxdataF_comp128_0,*rxdataF_comp128_1,*rxdataF_comp128_i0,*rxdataF_comp128_i1,*rho128_0,*rho128_1,*rho128_i0,*rho128_i1;
775
  int32_t i;
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809

  if (frame_parms->nb_antennas_rx>1) {
    for (aatx=0;aatx<frame_parms->nb_antennas_tx_eNB;aatx++) {
      //if (frame_parms->mode1_flag && (aatx>0)) break;

      rxdataF_comp128_0   = (__m128i *)&rxdataF_comp[(aatx<<1)][symbol*frame_parms->N_RB_DL*12];  
      rxdataF_comp128_1   = (__m128i *)&rxdataF_comp[(aatx<<1)+1][symbol*frame_parms->N_RB_DL*12];

      // MRC on each re of rb on MF output
      for (i=0;i<frame_parms->N_RB_DL*3;i++) {
	rxdataF_comp128_0[i] = _mm_adds_epi16(_mm_srai_epi16(rxdataF_comp128_0[i],1),_mm_srai_epi16(rxdataF_comp128_1[i],1));
      }
    }
    rho128_0 = (__m128i *) &rho[0][symbol*frame_parms->N_RB_DL*12];
    rho128_1 = (__m128i *) &rho[1][symbol*frame_parms->N_RB_DL*12];
    for (i=0;i<frame_parms->N_RB_DL*3;i++) {
      rho128_0[i] = _mm_adds_epi16(_mm_srai_epi16(rho128_0[i],1),_mm_srai_epi16(rho128_1[i],1));
    }
    rho128_i0 = (__m128i *) &rho_i[0][symbol*frame_parms->N_RB_DL*12];
    rho128_i1 = (__m128i *) &rho_i[1][symbol*frame_parms->N_RB_DL*12];
    rxdataF_comp128_i0   = (__m128i *)&rxdataF_comp_i[0][symbol*frame_parms->N_RB_DL*12];  
    rxdataF_comp128_i1   = (__m128i *)&rxdataF_comp_i[1][symbol*frame_parms->N_RB_DL*12];
      // MRC on each re of rb on MF and rho
    for (i=0;i<frame_parms->N_RB_DL*3;i++) {
      rxdataF_comp128_i0[i] = _mm_adds_epi16(_mm_srai_epi16(rxdataF_comp128_i0[i],1),_mm_srai_epi16(rxdataF_comp128_i1[i],1));
      rho128_i0[i]          = _mm_adds_epi16(_mm_srai_epi16(rho128_i0[i],1),_mm_srai_epi16(rho128_i1[i],1));
    }
  }
  _mm_empty();
  _m_empty();

}


810 811 812 813 814
void pdcch_extract_rbs_single(int32_t **rxdataF,
			      int32_t **dl_ch_estimates,
			      int32_t **rxdataF_ext,
			      int32_t **dl_ch_estimates_ext,
			      uint8_t symbol,
815 816 817
			      LTE_DL_FRAME_PARMS *frame_parms) {


818 819 820
  uint16_t rb,nb_rb=0;
  uint8_t i,j,aarx;
  int32_t *dl_ch0,*dl_ch0_ext,*rxF,*rxF_ext;
821 822 823
  

  int nushiftmod3 = frame_parms->nushift%3;
824
  uint8_t symbol_mod;
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855

  symbol_mod = (symbol>=(7-frame_parms->Ncp)) ? symbol-(7-frame_parms->Ncp) : symbol;
#ifdef DEBUG_DCI_DECODING
  msg("[PHY] extract_rbs_single: symbol_mod %d\n",symbol_mod);
#endif
  for (aarx=0;aarx<frame_parms->nb_antennas_rx;aarx++) {
    
    dl_ch0     = &dl_ch_estimates[aarx][5+(symbol*(frame_parms->ofdm_symbol_size))];
    dl_ch0_ext = &dl_ch_estimates_ext[aarx][symbol*(frame_parms->N_RB_DL*12)];

    rxF_ext   = &rxdataF_ext[aarx][symbol*(frame_parms->N_RB_DL*12)];

#ifndef NEW_FFT    
    rxF       = &rxdataF[aarx][(frame_parms->first_carrier_offset + (symbol*(frame_parms->ofdm_symbol_size)))*2];
#else
    rxF       = &rxdataF[aarx][(frame_parms->first_carrier_offset + (symbol*(frame_parms->ofdm_symbol_size)))];
#endif    
    if ((frame_parms->N_RB_DL&1) == 0)  { // even number of RBs
      for (rb=0;rb<frame_parms->N_RB_DL;rb++) {
	
	// For second half of RBs skip DC carrier
	if (rb==(frame_parms->N_RB_DL>>1)) {
#ifndef NEW_FFT
	  rxF       = &rxdataF[aarx][(1 + (symbol*(frame_parms->ofdm_symbol_size)))*2];
#else
	  rxF       = &rxdataF[aarx][(1 + (symbol*(frame_parms->ofdm_symbol_size)))];
#endif
	  //dl_ch0++; 
	}
	
	if (symbol_mod>0) {
856
	  memcpy(dl_ch0_ext,dl_ch0,12*sizeof(int32_t));
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
	  for (i=0;i<12;i++) {
#ifndef NEW_FFT
	    rxF_ext[i]=rxF[i<<1];
#else
	    rxF_ext[i]=rxF[i];
#endif
	  }
	  nb_rb++;
	  dl_ch0_ext+=12;
	  rxF_ext+=12;
	  
	  dl_ch0+=12;
#ifndef NEW_FFT
	  rxF+=24;
#else
	  rxF+=12;
#endif
	}
	else {
	  j=0;
	  for (i=0;i<12;i++) {
	    if ((i!=nushiftmod3) &&
		(i!=(nushiftmod3+3)) &&
		(i!=(nushiftmod3+6)) &&
		(i!=(nushiftmod3+9))) {
#ifndef NEW_FFT
	      rxF_ext[j]=rxF[i<<1];
#else
	      rxF_ext[j]=rxF[i];
#endif
	      //	      	      	      printf("extract rb %d, re %d => (%d,%d)\n",rb,i,*(short *)&rxF_ext[j],*(1+(short*)&rxF_ext[j]));
	      dl_ch0_ext[j++]=dl_ch0[i];
	      //	      	      printf("ch %d => (%d,%d)\n",i,*(short *)&dl_ch0[i],*(1+(short*)&dl_ch0[i]));
	    }
	  }
	  nb_rb++;
	  dl_ch0_ext+=8;
	  rxF_ext+=8;
	  
	  dl_ch0+=12;
#ifndef NEW_FFT
	  rxF+=24;
#else
	  rxF+=12;
#endif
	}
      }
    }
    else {  // Odd number of RBs
      for (rb=0;rb<frame_parms->N_RB_DL>>1;rb++) {

	if (symbol_mod>0) {
909
	  memcpy(dl_ch0_ext,dl_ch0,12*sizeof(int32_t));
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
	  for (i=0;i<12;i++)
#ifndef NEW_FFT
	    rxF_ext[i]=rxF[i<<1];
#else
	    rxF_ext[i]=rxF[i];
#endif
	  nb_rb++;
	  dl_ch0_ext+=12;
	  rxF_ext+=12;
	  
	  dl_ch0+=12;
#ifndef NEW_FFT
	  rxF+=24;
#else
	  rxF+=12;
#endif
	}
	else {
	  j=0;
	  for (i=0;i<12;i++) {
	    if ((i!=nushiftmod3) &&
		(i!=(nushiftmod3+3)) &&
		(i!=(nushiftmod3+6)) &&
		(i!=(nushiftmod3+9))) {
#ifndef NEW_FFT
	      rxF_ext[j]=rxF[i<<1];
#else
	      rxF_ext[j]=rxF[i];
#endif
	      //	      	      	      printf("extract rb %d, re %d => (%d,%d)\n",rb,i,*(short *)&rxF_ext[j],*(1+(short*)&rxF_ext[j]));
	      dl_ch0_ext[j++]=dl_ch0[i];
	      //	      	      printf("ch %d => (%d,%d)\n",i,*(short *)&dl_ch0[i],*(1+(short*)&dl_ch0[i]));
	    }
	  }
	  nb_rb++;
	  dl_ch0_ext+=8;
	  rxF_ext+=8;
	  
	  dl_ch0+=12;
#ifndef NEW_FFT
	  rxF+=24;
#else
	  rxF+=12;
#endif
	}
      }
      // Do middle RB (around DC)
      //	msg("dlch_ext %d\n",dl_ch0_ext-&dl_ch_estimates_ext[aarx][0]);      

      if (symbol_mod==0) {
	j=0;
	for (i=0;i<6;i++) {
	  if ((i!=nushiftmod3) &&
	      (i!=(nushiftmod3+3))){
	    dl_ch0_ext[j]=dl_ch0[i];
#ifndef NEW_FFT
	    rxF_ext[j++]=rxF[i<<1];
#else
	    rxF_ext[j++]=rxF[i];
#endif
	    //	    	      printf("**extract rb %d, re %d => (%d,%d)\n",rb,i,*(short *)&rxF_ext[j-1],*(1+(short*)&rxF_ext[j-1]));
	  }
	}
#ifndef NEW_FFT
	rxF       = &rxdataF[aarx][((symbol*(frame_parms->ofdm_symbol_size)))*2];
#else
	rxF       = &rxdataF[aarx][((symbol*(frame_parms->ofdm_symbol_size)))];
#endif
	for (;i<12;i++) {
	  if ((i!=(nushiftmod3+6)) &&
	      (i!=(nushiftmod3+9))){
	    dl_ch0_ext[j]=dl_ch0[i];
#ifndef NEW_FFT
	    rxF_ext[j++]=rxF[(1+i-6)<<1];
#else
	    rxF_ext[j++]=rxF[(1+i-6)];
#endif
	    //	    	      printf("**extract rb %d, re %d => (%d,%d)\n",rb,i,*(short *)&rxF_ext[j-1],*(1+(short*)&rxF_ext[j-1]));
	  }
	}
      
	
	nb_rb++;
	dl_ch0_ext+=8;
	rxF_ext+=8;
	dl_ch0+=12;
#ifndef NEW_FFT
	rxF+=14;
#else
	rxF+=7;
#endif
	rb++;
      }
      else {
	for (i=0;i<6;i++) {
	  dl_ch0_ext[i]=dl_ch0[i];
#ifndef NEW_FFT
	  rxF_ext[i]=rxF[i<<1];
#else
	  rxF_ext[i]=rxF[i];
#endif
	}
#ifndef NEW_FFT
	rxF       = &rxdataF[aarx][((symbol*(frame_parms->ofdm_symbol_size)))*2];
#else
	rxF       = &rxdataF[aarx][((symbol*(frame_parms->ofdm_symbol_size)))];
#endif
	for (;i<12;i++) {
	  dl_ch0_ext[i]=dl_ch0[i];
#ifndef NEW_FFT
	  rxF_ext[i]=rxF[(1+i-6)<<1];
#else
	  rxF_ext[i]=rxF[(1+i-6)];
#endif
	}
      
	
	nb_rb++;
	dl_ch0_ext+=12;
	rxF_ext+=12;
	dl_ch0+=12;
#ifndef NEW_FFT
	rxF+=14;
#else
	rxF+=7;
#endif
	rb++;
      }

      for (;rb<frame_parms->N_RB_DL;rb++) {
	if (symbol_mod > 0) {
1041
	  memcpy(dl_ch0_ext,dl_ch0,12*sizeof(int32_t));
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
	  for (i=0;i<12;i++)
#ifndef NEW_FFT
	    rxF_ext[i]=rxF[i<<1];
#else
	    rxF_ext[i]=rxF[i];
#endif
	  nb_rb++;
	  dl_ch0_ext+=12;
	  rxF_ext+=12;
	  
	  dl_ch0+=12;
#ifndef NEW_FFT
	  rxF+=24;
#else
	  rxF+=12;
#endif
	}
	else {
	  j=0;
	  for (i=0;i<12;i++) {
	    if ((i!=(nushiftmod3)) &&
		(i!=(nushiftmod3+3)) &&
		(i!=(nushiftmod3+6)) &&
		(i!=(nushiftmod3+9))) {
#ifndef NEW_FFT
	      rxF_ext[j]=rxF[i<<1];
#else
	      rxF_ext[j]=rxF[i];
#endif
	      //	      	      printf("extract rb %d, re %d => (%d,%d)\n",rb,i,*(short *)&rxF_ext[j],*(1+(short*)&rxF_ext[j]));
	      dl_ch0_ext[j++]=dl_ch0[i];
	    }
	  }
	  nb_rb++;
	  dl_ch0_ext+=8;
	  rxF_ext+=8;
	  
	  dl_ch0+=12;
#ifndef NEW_FFT
	  rxF+=24;
#else
	  rxF+=12;
#endif
	}
      }
    }
  }

  _mm_empty();
  _m_empty();

}

1095 1096 1097 1098 1099
void pdcch_extract_rbs_dual(int32_t **rxdataF,
			    int32_t **dl_ch_estimates,
			    int32_t **rxdataF_ext,
			    int32_t **dl_ch_estimates_ext,
			    uint8_t symbol,
1100 1101 1102
			    LTE_DL_FRAME_PARMS *frame_parms) {
  

1103 1104 1105 1106
  uint16_t rb,nb_rb=0;
  uint8_t i,aarx,j;
  int32_t *dl_ch0,*dl_ch0_ext,*dl_ch1,*dl_ch1_ext,*rxF,*rxF_ext;
  uint8_t symbol_mod;
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
  int nushiftmod3 = frame_parms->nushift%3;

  symbol_mod = (symbol>=(7-frame_parms->Ncp)) ? symbol-(7-frame_parms->Ncp) : symbol;

  for (aarx=0;aarx<frame_parms->nb_antennas_rx;aarx++) {
    
    dl_ch0     = &dl_ch_estimates[aarx][5+(symbol*(frame_parms->ofdm_symbol_size))];
    dl_ch0_ext = &dl_ch_estimates_ext[aarx][symbol*(frame_parms->N_RB_DL*12)];
    dl_ch1     = &dl_ch_estimates[2+aarx][5+(symbol*(frame_parms->ofdm_symbol_size))];
    dl_ch1_ext = &dl_ch_estimates_ext[2+aarx][symbol*(frame_parms->N_RB_DL*12)];

    //    msg("pdcch extract_rbs: rxF_ext pos %d\n",symbol*(frame_parms->N_RB_DL*12));
    rxF_ext   = &rxdataF_ext[aarx][symbol*(frame_parms->N_RB_DL*12)];

#ifndef NEW_FFT    
    rxF       = &rxdataF[aarx][(frame_parms->first_carrier_offset + (symbol*(frame_parms->ofdm_symbol_size)))*2];
#else
    rxF       = &rxdataF[aarx][(frame_parms->first_carrier_offset + (symbol*(frame_parms->ofdm_symbol_size)))];
#endif    
    if ((frame_parms->N_RB_DL&1) == 0)  // even number of RBs
      for (rb=0;rb<frame_parms->N_RB_DL;rb++) {
	
	// For second half of RBs skip DC carrier
	if (rb==(frame_parms->N_RB_DL>>1)) {
#ifndef NEW_FFT
	  rxF       = &rxdataF[aarx][(1 + (symbol*(frame_parms->ofdm_symbol_size)))*2];
#else
	  rxF       = &rxdataF[aarx][(1 + (symbol*(frame_parms->ofdm_symbol_size)))];
#endif
	  //dl_ch0++;
	  //dl_ch1++;
	}
	
	if (symbol_mod>0) {
1141 1142
	  memcpy(dl_ch0_ext,dl_ch0,12*sizeof(int32_t));
	  memcpy(dl_ch1_ext,dl_ch1,12*sizeof(int32_t));
1143 1144 1145
	  /*
	    msg("rb %d\n",rb);
	    for (i=0;i<12;i++)
1146
	    msg("(%d %d)",((int16_t *)dl_ch)[i<<1],((int16_t*)dl_ch)[1+(i<<1)]);
1147 1148 1149 1150 1151 1152 1153 1154 1155
	    msg("\n");*/
	  
	  for (i=0;i<12;i++) {
#ifndef NEW_FFT
	    rxF_ext[i]=rxF[i<<1];
#else
	    rxF_ext[i]=rxF[i];
#endif
	    //	  	      msg("%d : (%d,%d)\n",(rxF+(2*i)-&rxdataF[aarx][( (symbol*(frame_parms->ofdm_symbol_size)))*2])/2,
1156
	    //   ((int16_t*)&rxF[i<<1])[0],((int16_t*)&rxF[i<<1])[0]);
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
	  }
	  nb_rb++;
	  dl_ch0_ext+=12;
	  dl_ch1_ext+=12;
	  rxF_ext+=12;
	}
	else {
	  j=0;
	  for (i=0;i<12;i++) {
	    if ((i!=nushiftmod3) &&
		(i!=nushiftmod3+3) &&
		(i!=nushiftmod3+6) &&
		(i!=nushiftmod3+9)) {
#ifndef NEW_FFT
	      rxF_ext[j]=rxF[i<<1];
#else
	      rxF_ext[j]=rxF[i];
#endif
	      //	      	      	      printf("extract rb %d, re %d => (%d,%d)\n",rb,i,*(short *)&rxF_ext[j],*(1+(short*)&rxF_ext[j]));
	      dl_ch0_ext[j++]=dl_ch0[i];
	      dl_ch1_ext[j++]=dl_ch1[i];
	    }
	  }
	  nb_rb++;
	  dl_ch0_ext+=8;
	  rxF_ext+=8;
	  
	  dl_ch0+=12;
#ifndef NEW_FFT
	  rxF+=24;
#else
	  rxF+=12;
#endif

	}
	dl_ch0+=12;
	dl_ch1+=12;
#ifndef NEW_FFT
	rxF+=24;
#else
	rxF+=12;
#endif
      }
  
    else {  // Odd number of RBs
      for (rb=0;rb<frame_parms->N_RB_DL>>1;rb++) {

	//	msg("rb %d: %d\n",rb,rxF-&rxdataF[aarx][(symbol*(frame_parms->ofdm_symbol_size))*2]);

	if (symbol_mod>0) {
1207 1208
	  memcpy(dl_ch0_ext,dl_ch0,12*sizeof(int32_t));
	  memcpy(dl_ch1_ext,dl_ch1,12*sizeof(int32_t));
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
	  for (i=0;i<12;i++)
#ifndef NEW_FFT
	    rxF_ext[i]=rxF[i<<1];
#else
	    rxF_ext[i]=rxF[i];
#endif
	  nb_rb++;
	  dl_ch0_ext+=12;
	  dl_ch1_ext+=12;
	  rxF_ext+=12;
	  
	  dl_ch0+=12;
	  dl_ch1+=12;
#ifndef NEW_FFT
	  rxF+=24;
#else
	  rxF+=12;
#endif
	  
	}
	else {
	  j=0;
	  for (i=0;i<12;i++) {
	    if ((i!=nushiftmod3) &&
		(i!=nushiftmod3+3) &&
		(i!=nushiftmod3+6) &&
		(i!=nushiftmod3+9)) {
#ifndef NEW_FFT
	      rxF_ext[j]=rxF[i<<1];
#else
	      rxF_ext[j]=rxF[i];
#endif
	      //	      	      	      printf("extract rb %d, re %d => (%d,%d)\n",rb,i,*(short *)&rxF_ext[j],*(1+(short*)&rxF_ext[j]));
	      dl_ch0_ext[j]=dl_ch0[i];
	      dl_ch1_ext[j++]=dl_ch1[i];
	      //	      	      printf("ch %d => (%d,%d)\n",i,*(short *)&dl_ch0[i],*(1+(short*)&dl_ch0[i]));
	    }
	  }
	  nb_rb++;
	  dl_ch0_ext+=8;
	  dl_ch1_ext+=8;
	  rxF_ext+=8;
	  

	  dl_ch0+=12;
	  dl_ch1+=12;
#ifndef NEW_FFT
	  rxF+=24;
#else
	  rxF+=12;
#endif
	}
      }      
	// Do middle RB (around DC)

      if (symbol_mod > 0) {
	for (i=0;i<6;i++) {
	  dl_ch0_ext[i]=dl_ch0[i];
	  dl_ch1_ext[i]=dl_ch1[i];
#ifndef NEW_FFT
	  rxF_ext[i]=rxF[i<<1];
#else
	  rxF_ext[i]=rxF[i];
#endif
	}
#ifndef NEW_FFT	
	rxF       = &rxdataF[aarx][((symbol*(frame_parms->ofdm_symbol_size)))*2];
#else
	rxF       = &rxdataF[aarx][((symbol*(frame_parms->ofdm_symbol_size)))];
#endif
	for (;i<12;i++) {
	  dl_ch0_ext[i]=dl_ch0[i];
	  dl_ch1_ext[i]=dl_ch1[i];
#ifndef NEW_FFT
	  rxF_ext[i]=rxF[(1+i)<<1];
#else
	  rxF_ext[i]=rxF[(1+i)];
#endif
	}

	nb_rb++;
	dl_ch0_ext+=12;
	dl_ch1_ext+=12;
	rxF_ext+=12;
	
	dl_ch0+=12;
	dl_ch1+=12;
#ifndef NEW_FFT
	rxF+=14;
#else
	rxF+=7;
#endif
	rb++;
      }
      else {
	j=0;
	for (i=0;i<6;i++) {
	  if ((i!=nushiftmod3) &&
	      (i!=nushiftmod3+3)){
	    dl_ch0_ext[j]=dl_ch0[i];
	    dl_ch1_ext[j]=dl_ch1[i];
#ifndef NEW_FFT
	    rxF_ext[j++]=rxF[i<<1];
#else
	    rxF_ext[j++]=rxF[i];
#endif
	    //	    	      printf("**extract rb %d, re %d => (%d,%d)\n",rb,i,*(short *)&rxF_ext[j-1],*(1+(short*)&rxF_ext[j-1]));
	  }
	}
#ifndef NEW_FFT
	rxF       = &rxdataF[aarx][((symbol*(frame_parms->ofdm_symbol_size)))*2];
#else
	rxF       = &rxdataF[aarx][((symbol*(frame_parms->ofdm_symbol_size)))];
#endif
	for (;i<12;i++) {
	  if ((i!=nushiftmod3+6) &&
	      (i!=nushiftmod3+9)){
	    dl_ch0_ext[j]=dl_ch0[i];
	    dl_ch1_ext[j]=dl_ch1[i];
#ifndef NEW_FFT
	    rxF_ext[j++]=rxF[(1+i-6)<<1];
#else
	    rxF_ext[j++]=rxF[(1+i-6)];
#endif
	    //	    	      printf("**extract rb %d, re %d => (%d,%d)\n",rb,i,*(short *)&rxF_ext[j-1],*(1+(short*)&rxF_ext[j-1]));
	  }
	}
      
	
	nb_rb++;
	dl_ch0_ext+=8;
	dl_ch1_ext+=8;
	rxF_ext+=8;
	dl_ch0+=12;
	dl_ch1+=12;
#ifndef NEW_FFT
	rxF+=14;
#else
	rxF+=7;
#endif
	rb++;
      }

      for (;rb<frame_parms->N_RB_DL;rb++) {

	if (symbol_mod>0) {
	  //	msg("rb %d: %d\n",rb,rxF-&rxdataF[aarx][(symbol*(frame_parms->ofdm_symbol_size))*2]);
1356 1357
	  memcpy(dl_ch0_ext,dl_ch0,12*sizeof(int32_t));
	  memcpy(dl_ch1_ext,dl_ch1,12*sizeof(int32_t));
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
	  for (i=0;i<12;i++)
#ifndef NEW_FFT
	    rxF_ext[i]=rxF[i<<1];
#else
	    rxF_ext[i]=rxF[i];
#endif
	  nb_rb++;
	  dl_ch0_ext+=12;
	  dl_ch1_ext+=12;
	  rxF_ext+=12;
	  
	  dl_ch0+=12;
	  dl_ch1+=12;
#ifndef NEW_FFT
	  rxF+=24;
#else
	  rxF+=12;
#endif	  
	  
	}
	else {
	  j=0;
	  for (i=0;i<12;i++) {
	    if ((i!=nushiftmod3) &&
		(i!=nushiftmod3+3) &&
		(i!=nushiftmod3+6) &&
		(i!=nushiftmod3+9)) {
#ifndef NEW_FFT
	      rxF_ext[j]=rxF[i<<1];
#else
	      rxF_ext[j]=rxF[i];
#endif
	      //	      	      printf("extract rb %d, re %d => (%d,%d)\n",rb,i,*(short *)&rxF_ext[j],*(1+(short*)&rxF_ext[j]));
	      dl_ch0_ext[j]=dl_ch0[i];
	      dl_ch1_ext[j++]=dl_ch1[i];
	    }
	  }
	  nb_rb++;
	  dl_ch0_ext+=8;
	  dl_ch1_ext+=8;
	  rxF_ext+=8;
	  
	  dl_ch0+=12;
	  dl_ch1+=12;
#ifndef NEW_FFT
	  rxF+=24;
#else
	  rxF+=12;
#endif
	}
      }
    }
  }
  _mm_empty();
  _m_empty();
  

}


1418 1419 1420 1421
void pdcch_channel_compensation(int32_t **rxdataF_ext,
				int32_t **dl_ch_estimates_ext,
				int32_t **rxdataF_comp,
				int32_t **rho,
1422
				LTE_DL_FRAME_PARMS *frame_parms,
1423 1424
				uint8_t symbol,
				uint8_t output_shift) {
1425

1426
  uint16_t rb;
1427 1428
  __m128i *dl_ch128,*rxdataF128,*rxdataF_comp128;
  __m128i *dl_ch128_2, *rho128;
1429
  uint8_t aatx,aarx,pilots=0;
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615



#ifndef __SSE3__
  zero2 = _mm_xor_si128(zero2,zero2);
#endif

#ifdef DEBUG_DCI_DECODING
  msg("[PHY] PDCCH comp: symbol %d\n",symbol);
#endif

  if (symbol==0)
    pilots=1;

  for (aatx=0;aatx<frame_parms->nb_antennas_tx_eNB;aatx++) {
    //if (frame_parms->mode1_flag && aatx>0) break; //if mode1_flag is set then there is only one stream to extract, independent of nb_antennas_tx_eNB

    for (aarx=0;aarx<frame_parms->nb_antennas_rx;aarx++) {

      dl_ch128          = (__m128i *)&dl_ch_estimates_ext[(aatx<<1)+aarx][symbol*frame_parms->N_RB_DL*12];
      rxdataF128        = (__m128i *)&rxdataF_ext[aarx][symbol*frame_parms->N_RB_DL*12];
      rxdataF_comp128   = (__m128i *)&rxdataF_comp[(aatx<<1)+aarx][symbol*frame_parms->N_RB_DL*12];


      for (rb=0;rb<frame_parms->N_RB_DL;rb++) {
	
	// multiply by conjugated channel
	mmtmpPD0 = _mm_madd_epi16(dl_ch128[0],rxdataF128[0]);
	//	print_ints("re",&mmtmpPD0);
	
	// mmtmpPD0 contains real part of 4 consecutive outputs (32-bit)
	mmtmpPD1 = _mm_shufflelo_epi16(dl_ch128[0],_MM_SHUFFLE(2,3,0,1));
	mmtmpPD1 = _mm_shufflehi_epi16(mmtmpPD1,_MM_SHUFFLE(2,3,0,1));
	mmtmpPD1 = _mm_sign_epi16(mmtmpPD1,*(__m128i*)&conjugate[0]);
	//	print_ints("im",&mmtmpPD1);
	mmtmpPD1 = _mm_madd_epi16(mmtmpPD1,rxdataF128[0]);
	// mmtmpPD1 contains imag part of 4 consecutive outputs (32-bit)
	mmtmpPD0 = _mm_srai_epi32(mmtmpPD0,output_shift);
	//	print_ints("re(shift)",&mmtmpPD0);
	mmtmpPD1 = _mm_srai_epi32(mmtmpPD1,output_shift);
	//	print_ints("im(shift)",&mmtmpPD1);
	mmtmpPD2 = _mm_unpacklo_epi32(mmtmpPD0,mmtmpPD1);
	mmtmpPD3 = _mm_unpackhi_epi32(mmtmpPD0,mmtmpPD1);
	//       	print_ints("c0",&mmtmpPD2);
	//	print_ints("c1",&mmtmpPD3);
	rxdataF_comp128[0] = _mm_packs_epi32(mmtmpPD2,mmtmpPD3);
	//	print_shorts("rx:",rxdataF128);
	//	print_shorts("ch:",dl_ch128);
	//	print_shorts("pack:",rxdataF_comp128);

	// multiply by conjugated channel
	mmtmpPD0 = _mm_madd_epi16(dl_ch128[1],rxdataF128[1]);
	// mmtmpPD0 contains real part of 4 consecutive outputs (32-bit)
	mmtmpPD1 = _mm_shufflelo_epi16(dl_ch128[1],_MM_SHUFFLE(2,3,0,1));
	mmtmpPD1 = _mm_shufflehi_epi16(mmtmpPD1,_MM_SHUFFLE(2,3,0,1));
	mmtmpPD1 = _mm_sign_epi16(mmtmpPD1,*(__m128i*)conjugate);
	mmtmpPD1 = _mm_madd_epi16(mmtmpPD1,rxdataF128[1]);
	// mmtmpPD1 contains imag part of 4 consecutive outputs (32-bit)
	mmtmpPD0 = _mm_srai_epi32(mmtmpPD0,output_shift);
	mmtmpPD1 = _mm_srai_epi32(mmtmpPD1,output_shift);
	mmtmpPD2 = _mm_unpacklo_epi32(mmtmpPD0,mmtmpPD1);
	mmtmpPD3 = _mm_unpackhi_epi32(mmtmpPD0,mmtmpPD1);
	
	rxdataF_comp128[1] = _mm_packs_epi32(mmtmpPD2,mmtmpPD3);
	//	print_shorts("rx:",rxdataF128+1);
	//	print_shorts("ch:",dl_ch128+1);
	//	print_shorts("pack:",rxdataF_comp128+1);	
	// multiply by conjugated channel
	if (pilots == 0) {
	  mmtmpPD0 = _mm_madd_epi16(dl_ch128[2],rxdataF128[2]);
	  // mmtmpPD0 contains real part of 4 consecutive outputs (32-bit)
	  mmtmpPD1 = _mm_shufflelo_epi16(dl_ch128[2],_MM_SHUFFLE(2,3,0,1));
	  mmtmpPD1 = _mm_shufflehi_epi16(mmtmpPD1,_MM_SHUFFLE(2,3,0,1));
	  mmtmpPD1 = _mm_sign_epi16(mmtmpPD1,*(__m128i*)conjugate);
	  mmtmpPD1 = _mm_madd_epi16(mmtmpPD1,rxdataF128[2]);
	  // mmtmpPD1 contains imag part of 4 consecutive outputs (32-bit)
	  mmtmpPD0 = _mm_srai_epi32(mmtmpPD0,output_shift);
	  mmtmpPD1 = _mm_srai_epi32(mmtmpPD1,output_shift);
	  mmtmpPD2 = _mm_unpacklo_epi32(mmtmpPD0,mmtmpPD1);
	  mmtmpPD3 = _mm_unpackhi_epi32(mmtmpPD0,mmtmpPD1);
	  
	  rxdataF_comp128[2] = _mm_packs_epi32(mmtmpPD2,mmtmpPD3);
	}
	//	print_shorts("rx:",rxdataF128+2);
	//	print_shorts("ch:",dl_ch128+2);
	//      	print_shorts("pack:",rxdataF_comp128+2);
      
	if (pilots==0) {
	  dl_ch128+=3;
	  rxdataF128+=3;
	  rxdataF_comp128+=3;
	}
	else {
	  dl_ch128+=2;
	  rxdataF128+=2;
	  rxdataF_comp128+=2;
	}
      }
    }
  }
  

  if (rho) {

    for (aarx=0;aarx<frame_parms->nb_antennas_rx;aarx++) {
      rho128        = (__m128i *)&rho[aarx][symbol*frame_parms->N_RB_DL*12];
      dl_ch128      = (__m128i *)&dl_ch_estimates_ext[aarx][symbol*frame_parms->N_RB_DL*12];
      dl_ch128_2    = (__m128i *)&dl_ch_estimates_ext[2+aarx][symbol*frame_parms->N_RB_DL*12];

      for (rb=0;rb<frame_parms->N_RB_DL;rb++) {
	// multiply by conjugated channel
	mmtmpPD0 = _mm_madd_epi16(dl_ch128[0],dl_ch128_2[0]);
	//	print_ints("re",&mmtmpD0);
	
	// mmtmpD0 contains real part of 4 consecutive outputs (32-bit)
	mmtmpPD1 = _mm_shufflelo_epi16(dl_ch128[0],_MM_SHUFFLE(2,3,0,1));
	mmtmpPD1 = _mm_shufflehi_epi16(mmtmpPD1,_MM_SHUFFLE(2,3,0,1));
	mmtmpPD1 = _mm_sign_epi16(mmtmpPD1,*(__m128i*)&conjugate[0]);
	//	print_ints("im",&mmtmpPD1);
	mmtmpPD1 = _mm_madd_epi16(mmtmpPD1,dl_ch128_2[0]);
	// mmtmpPD1 contains imag part of 4 consecutive outputs (32-bit)
	mmtmpPD0 = _mm_srai_epi32(mmtmpPD0,output_shift);
	//	print_ints("re(shift)",&mmtmpD0);
	mmtmpPD1 = _mm_srai_epi32(mmtmpPD1,output_shift);
	//	print_ints("im(shift)",&mmtmpD1);
	mmtmpPD2 = _mm_unpacklo_epi32(mmtmpPD0,mmtmpPD1);
	mmtmpPD3 = _mm_unpackhi_epi32(mmtmpPD0,mmtmpPD1);
	//       	print_ints("c0",&mmtmpPD2);
	//	print_ints("c1",&mmtmpPD3);
	rho128[0] = _mm_packs_epi32(mmtmpPD2,mmtmpPD3);

	//print_shorts("rx:",dl_ch128_2);
	//print_shorts("ch:",dl_ch128);
	//print_shorts("pack:",rho128);
	
	// multiply by conjugated channel
	mmtmpPD0 = _mm_madd_epi16(dl_ch128[1],dl_ch128_2[1]);
	// mmtmpD0 contains real part of 4 consecutive outputs (32-bit)
	mmtmpPD1 = _mm_shufflelo_epi16(dl_ch128[1],_MM_SHUFFLE(2,3,0,1));
	mmtmpPD1 = _mm_shufflehi_epi16(mmtmpPD1,_MM_SHUFFLE(2,3,0,1));
	mmtmpPD1 = _mm_sign_epi16(mmtmpPD1,*(__m128i*)conjugate);
	mmtmpPD1 = _mm_madd_epi16(mmtmpPD1,dl_ch128_2[1]);
	// mmtmpD1 contains imag part of 4 consecutive outputs (32-bit)
	mmtmpPD0 = _mm_srai_epi32(mmtmpPD0,output_shift);
	mmtmpPD1 = _mm_srai_epi32(mmtmpPD1,output_shift);
	mmtmpPD2 = _mm_unpacklo_epi32(mmtmpPD0,mmtmpPD1);
	mmtmpPD3 = _mm_unpackhi_epi32(mmtmpPD0,mmtmpPD1);


	rho128[1] =_mm_packs_epi32(mmtmpPD2,mmtmpPD3);
	//print_shorts("rx:",dl_ch128_2+1);
	//print_shorts("ch:",dl_ch128+1);
	//print_shorts("pack:",rho128+1);	
	// multiply by conjugated channel
	mmtmpPD0 = _mm_madd_epi16(dl_ch128[2],dl_ch128_2[2]);
	// mmtmpPD0 contains real part of 4 consecutive outputs (32-bit)
	mmtmpPD1 = _mm_shufflelo_epi16(dl_ch128[2],_MM_SHUFFLE(2,3,0,1));
	mmtmpPD1 = _mm_shufflehi_epi16(mmtmpPD1,_MM_SHUFFLE(2,3,0,1));
	mmtmpPD1 = _mm_sign_epi16(mmtmpPD1,*(__m128i*)conjugate);
	mmtmpPD1 = _mm_madd_epi16(mmtmpPD1,dl_ch128_2[2]);
	// mmtmpPD1 contains imag part of 4 consecutive outputs (32-bit)
	mmtmpPD0 = _mm_srai_epi32(mmtmpPD0,output_shift);
	mmtmpPD1 = _mm_srai_epi32(mmtmpPD1,output_shift);
	mmtmpPD2 = _mm_unpacklo_epi32(mmtmpPD0,mmtmpPD1);
	mmtmpPD3 = _mm_unpackhi_epi32(mmtmpPD0,mmtmpPD1);
	
	rho128[2] = _mm_packs_epi32(mmtmpPD2,mmtmpPD3);
	//print_shorts("rx:",dl_ch128_2+2);
	//print_shorts("ch:",dl_ch128+2);
	//print_shorts("pack:",rho128+2);
	
	dl_ch128+=3;
	dl_ch128_2+=3;
	rho128+=3;
	
      }	
    }

  }

  _mm_empty();
  _m_empty();

}     

void pdcch_detection_mrc(LTE_DL_FRAME_PARMS *frame_parms,
1616 1617
			 int32_t **rxdataF_comp,
			 uint8_t symbol) {
1618

1619
  uint8_t aatx;
1620 1621

  __m128i *rxdataF_comp128_0,*rxdataF_comp128_1;
1622
  int32_t i;
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639

  if (frame_parms->nb_antennas_rx>1) {
    for (aatx=0;aatx<frame_parms->nb_antennas_tx_eNB;aatx++) {
      rxdataF_comp128_0   = (__m128i *)&rxdataF_comp[(aatx<<1)][symbol*frame_parms->N_RB_DL*12];  
      rxdataF_comp128_1   = (__m128i *)&rxdataF_comp[(aatx<<1)+1][symbol*frame_parms->N_RB_DL*12];  
      // MRC on each re of rb
      for (i=0;i<frame_parms->N_RB_DL*3;i++) {
	rxdataF_comp128_0[i] = _mm_adds_epi16(_mm_srai_epi16(rxdataF_comp128_0[i],1),_mm_srai_epi16(rxdataF_comp128_1[i],1));
      }
    }
  }
  _mm_empty();
  _m_empty();

}

void pdcch_siso(LTE_DL_FRAME_PARMS *frame_parms,
1640 1641
		int32_t **rxdataF_comp,
		uint8_t l) {
1642 1643


1644
  uint8_t rb,re,jj,ii;
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659

  jj=0;
  ii=0;
  for (rb=0;rb<frame_parms->N_RB_DL;rb++) {

    for (re=0;re<12;re++) {
      
      rxdataF_comp[0][jj++] = rxdataF_comp[0][ii];
      ii++;
    }
  }
}


void pdcch_alamouti(LTE_DL_FRAME_PARMS *frame_parms,
1660 1661
		    int32_t **rxdataF_comp,
		    uint8_t symbol){
1662 1663


1664 1665 1666
  int16_t *rxF0,*rxF1;
  uint8_t rb,re;
  int32_t jj=(symbol*frame_parms->N_RB_DL*12);
1667

1668 1669
  rxF0     = (int16_t*)&rxdataF_comp[0][jj];  //tx antenna 0  h0*y
  rxF1     = (int16_t*)&rxdataF_comp[2][jj];  //tx antenna 1  h1*y
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692

  for (rb=0;rb<frame_parms->N_RB_DL;rb++) {

    for (re=0;re<12;re+=2) {

      // Alamouti RX combining
      
      rxF0[0] = rxF0[0] + rxF1[2];
      rxF0[1] = rxF0[1] - rxF1[3];

      rxF0[2] = rxF0[2] - rxF1[0];
      rxF0[3] = rxF0[3] + rxF1[1];
 
      rxF0+=4;
      rxF1+=4;
    }
  }

  _mm_empty();
  _m_empty();
  
}

1693
int32_t avgP[4];
1694

1695
int32_t rx_pdcch(LTE_UE_COMMON *lte_ue_common_vars,
1696 1697
	     LTE_UE_PDCCH **lte_ue_pdcch_vars,
	     LTE_DL_FRAME_PARMS *frame_parms,
1698 1699
	     uint8_t subframe,
	     uint8_t eNB_id,
1700
	     MIMO_mode_t mimo_mode,
1701
	     uint8_t is_secondary_ue) {
1702

1703
  uint8_t log2_maxh,aatx,aarx;
1704
#ifdef MU_RECEIVER
1705
  uint8_t eNB_id_i=eNB_id+1;//add 1 to eNB_id to separate from wanted signal, chosen as the B/F'd pilots from the SeNB are shifted by 1
1706
#endif
1707 1708 1709
  int32_t avgs,s;
  uint8_t n_pdcch_symbols = 3; //lte_ue_pdcch_vars[eNB_id]->num_pdcch_symbols;
  uint8_t mi = get_mi(frame_parms,subframe);
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

  //  printf("In rx_pdcch, subframe %d,  eNB_id %d\n",subframe,eNB_id);

  for (s=0;s<n_pdcch_symbols;s++) {
      if (is_secondary_ue == 1) {
	pdcch_extract_rbs_single(lte_ue_common_vars->rxdataF,
				 lte_ue_common_vars->dl_ch_estimates[eNB_id+1], //add 1 to eNB_id to compensate for the shifted B/F'd pilots from the SeNB
				 lte_ue_pdcch_vars[eNB_id]->rxdataF_ext,
				 lte_ue_pdcch_vars[eNB_id]->dl_ch_estimates_ext,
				 s,
				 frame_parms);
#ifdef MU_RECEIVER
	pdcch_extract_rbs_single(lte_ue_common_vars->rxdataF,
				 lte_ue_common_vars->dl_ch_estimates[eNB_id_i - 1],//subtract 1 to eNB_id_i to compensate for the non-shifted pilots from the PeNB
				 lte_ue_pdcch_vars[eNB_id_i]->rxdataF_ext,//shift by two to simulate transmission from a second antenna
				 lte_ue_pdcch_vars[eNB_id_i]->dl_ch_estimates_ext,//shift by two to simulate transmission from a second antenna
				 s,
				 frame_parms);
#endif //MU_RECEIVER
      } else if (frame_parms->nb_antennas_tx_eNB>1) {
	pdcch_extract_rbs_dual(lte_ue_common_vars->rxdataF,
			       lte_ue_common_vars->dl_ch_estimates[eNB_id],
			       lte_ue_pdcch_vars[eNB_id]->rxdataF_ext,
			       lte_ue_pdcch_vars[eNB_id]->dl_ch_estimates_ext,
			       s,
			       frame_parms);
      } else {
	pdcch_extract_rbs_single(lte_ue_common_vars->rxdataF,
				 lte_ue_common_vars->dl_ch_estimates[eNB_id],
				 lte_ue_pdcch_vars[eNB_id]->rxdataF_ext,
				 lte_ue_pdcch_vars[eNB_id]->dl_ch_estimates_ext,
				 s,
				 frame_parms);
      }
  }
  pdcch_channel_level(lte_ue_pdcch_vars[eNB_id]->dl_ch_estimates_ext,
		      frame_parms,
		      avgP,
		      frame_parms->N_RB_DL);

  avgs = 0;
  for (aatx=0;aatx<frame_parms->nb_antennas_tx_eNB;aatx++)
    for (aarx=0;aarx<frame_parms->nb_antennas_rx;aarx++)
      avgs = cmax(avgs,avgP[(aarx<<1)+aatx]);
  
1755
  log2_maxh = (log2_approx(avgs)/2) + 2 + frame_parms->nb_antennas_rx - 1;
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
#ifdef DEBUG_PHY
  msg("[PDCCH] log2_maxh = %d (%d,%d)\n",log2_maxh,avgP[0],avgs);
#endif


  for (s=0;s<n_pdcch_symbols;s++) {
    pdcch_channel_compensation(lte_ue_pdcch_vars[eNB_id]->rxdataF_ext,
			       lte_ue_pdcch_vars[eNB_id]->dl_ch_estimates_ext,
			       lte_ue_pdcch_vars[eNB_id]->rxdataF_comp,
			       (aatx>1) ? lte_ue_pdcch_vars[eNB_id]->rho : NULL,
			       frame_parms,
			       s,
			       log2_maxh); // log2_maxh+I0_shift


#ifdef DEBUG_PHY
knopp's avatar
knopp committed
1772 1773
    if (subframe==5)
      write_output("rxF_comp_d.m","rxF_c_d",&lte_ue_pdcch_vars[eNB_id]->rxdataF_comp[0][s*frame_parms->N_RB_DL*12],frame_parms->N_RB_DL*12,1,1);
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836
#endif

#ifdef MU_RECEIVER
    if (is_secondary_ue) {
      //get MF output for interfering stream
      pdcch_channel_compensation(lte_ue_pdcch_vars[eNB_id_i]->rxdataF_ext,
				 lte_ue_pdcch_vars[eNB_id_i]->dl_ch_estimates_ext,
				 lte_ue_pdcch_vars[eNB_id_i]->rxdataF_comp,
				 (aatx>1) ? lte_ue_pdcch_vars[eNB_id_i]->rho : NULL,
				 frame_parms,
				 s,
				 log2_maxh); // log2_maxh+I0_shift
#ifdef DEBUG_PHY
	write_output("rxF_comp_i.m","rxF_c_i",&lte_ue_pdcch_vars[eNB_id_i]->rxdataF_comp[0][s*frame_parms->N_RB_DL*12],frame_parms->N_RB_DL*12,1,1);
#endif
      pdcch_dual_stream_correlation(frame_parms,
				    s,
				    lte_ue_pdcch_vars[eNB_id]->dl_ch_estimates_ext,
				    lte_ue_pdcch_vars[eNB_id_i]->dl_ch_estimates_ext,
				    lte_ue_pdcch_vars[eNB_id]->dl_ch_rho_ext,
				    log2_maxh);
    }
#endif //MU_RECEIVER
    

    if (frame_parms->nb_antennas_rx > 1) {
#ifdef MU_RECEIVER
      if (is_secondary_ue) {
	pdcch_detection_mrc_i(frame_parms,
			      lte_ue_pdcch_vars[eNB_id]->rxdataF_comp,
			      lte_ue_pdcch_vars[eNB_id_i]->rxdataF_comp,
			      lte_ue_pdcch_vars[eNB_id]->rho,
			      lte_ue_pdcch_vars[eNB_id]->dl_ch_rho_ext,
			      s);
#ifdef DEBUG_PHY
	write_output("rxF_comp_d.m","rxF_c_d",&lte_ue_pdcch_vars[eNB_id]->rxdataF_comp[0][s*frame_parms->N_RB_DL*12],frame_parms->N_RB_DL*12,1,1);
	write_output("rxF_comp_i.m","rxF_c_i",&lte_ue_pdcch_vars[eNB_id_i]->rxdataF_comp[0][s*frame_parms->N_RB_DL*12],frame_parms->N_RB_DL*12,1,1);
#endif
      } else 
#endif //MU_RECEIVER
	pdcch_detection_mrc(frame_parms,
			    lte_ue_pdcch_vars[eNB_id]->rxdataF_comp,
			    s);
      
    }
  
    if (mimo_mode == SISO) 
      pdcch_siso(frame_parms,lte_ue_pdcch_vars[eNB_id]->rxdataF_comp,s);
    else
      pdcch_alamouti(frame_parms,lte_ue_pdcch_vars[eNB_id]->rxdataF_comp,s);

    
#ifdef MU_RECEIVER
    if (is_secondary_ue) {
      pdcch_qpsk_qpsk_llr(frame_parms,
			  lte_ue_pdcch_vars[eNB_id]->rxdataF_comp,
			  lte_ue_pdcch_vars[eNB_id_i]->rxdataF_comp,
			  lte_ue_pdcch_vars[eNB_id]->dl_ch_rho_ext,
			  lte_ue_pdcch_vars[eNB_id]->llr16, //subsequent function require 16 bit llr, but output must be 8 bit (actually clipped to 4, because of the Viterbi decoder)
			  lte_ue_pdcch_vars[eNB_id]->llr,
			  s);

#ifdef DEBUG_PHY
knopp's avatar
knopp committed
1837
      if (subframe==5) {
1838 1839
	write_output("llr8_seq.m","llr8",&lte_ue_pdcch_vars[eNB_id]->llr[s*frame_parms->N_RB_DL*12],frame_parms->N_RB_DL*12,1,4);
	write_output("llr16_seq.m","llr16",&lte_ue_pdcch_vars[eNB_id]->llr16[s*frame_parms->N_RB_DL*12],frame_parms->N_RB_DL*12,1,4);
knopp's avatar
knopp committed
1840
      }
1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
#endif
    } 
    else {
#endif //MU_RECEIVER
      pdcch_llr(frame_parms,
		lte_ue_pdcch_vars[eNB_id]->rxdataF_comp,
		(char *)lte_ue_pdcch_vars[eNB_id]->llr,
		s);
#ifdef DEBUG_PHY
      write_output("llr8_seq.m","llr8",&lte_ue_pdcch_vars[eNB_id]->llr[s*frame_parms->N_RB_DL*12],frame_parms->N_RB_DL*12,1,4);
#endif
#ifdef MU_RECEIVER
    }
#endif //MU_RECEIVER

  }

  // decode pcfich here
  n_pdcch_symbols = rx_pcfich(frame_parms,
			      subframe,
			      lte_ue_pdcch_vars[eNB_id],
			      mimo_mode);
  if (n_pdcch_symbols>3)
    n_pdcch_symbols=1;

#ifdef DEBUG_DCI_DECODING
  msg("[PDCCH] subframe %d n_pdcch_symbols from PCFICH =%d\n",subframe,n_pdcch_symbols);

  msg("demapping: subframe %d, mi %d, tdd_config %d\n",subframe,get_mi(frame_parms,subframe),frame_parms->tdd_config);
#endif

  pdcch_demapping(lte_ue_pdcch_vars[eNB_id]->llr,
		  lte_ue_pdcch_vars[eNB_id]->wbar,
		  frame_parms,
		  n_pdcch_symbols,
		  get_mi(frame_parms,subframe));

  pdcch_deinterleaving(frame_parms,
1879
		       (uint16_t*)lte_ue_pdcch_vars[eNB_id]->e_rx,
1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895
		       lte_ue_pdcch_vars[eNB_id]->wbar,
		       n_pdcch_symbols,
		       mi);
  
  pdcch_unscrambling(frame_parms,
		     subframe,
		     lte_ue_pdcch_vars[eNB_id]->e_rx,
		     get_nCCE(n_pdcch_symbols,frame_parms,mi)*72);
  
  lte_ue_pdcch_vars[eNB_id]->num_pdcch_symbols = n_pdcch_symbols;

  return(0);
}


void pdcch_scrambling(LTE_DL_FRAME_PARMS *frame_parms,
1896 1897 1898
		      uint8_t subframe,
		      uint8_t *e,
		      uint32_t length) {
1899
  int i;
1900 1901
  uint8_t reset;
  uint32_t x1, x2, s=0;
1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920

  reset = 1;
  // x1 is set in lte_gold_generic

  x2 = (subframe<<9) + frame_parms->Nid_cell; //this is c_init in 36.211 Sec 6.8.2

  for (i=0; i<length; i++) {
    if ((i&0x1f)==0) {
      s = lte_gold_generic(&x1, &x2, reset);
      //printf("lte_gold[%d]=%x\n",i,s);
      reset = 0;
    }
    //    printf("scrambling %d : e %d, c %d\n",i,e[i],((s>>(i&0x1f))&1));
    if (e[i] != 2) // <NIL> element is 2
      e[i] = (e[i]&1) ^ ((s>>(i&0x1f))&1);
  }
}

void pdcch_unscrambling(LTE_DL_FRAME_PARMS *frame_parms,
1921 1922 1923
			uint8_t subframe,
			int8_t* llr,
			uint32_t length) {
1924 1925

  int i;
1926 1927
  uint8_t reset;
  uint32_t x1, x2, s=0;
1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948

  reset = 1;
  // x1 is set in first call to lte_gold_generic

  x2 = (subframe<<9) + frame_parms->Nid_cell; //this is c_init in 36.211 Sec 6.8.2

  for (i=0; i<length; i++) {
    if (i%32==0) {
      s = lte_gold_generic(&x1, &x2, reset);
      //printf("lte_gold[%d]=%x\n",i,s);
      reset = 0;
    }
    // take the quarter of the PBCH that corresponds to this frame
    //    printf("unscrambling %d : e %d, c %d\n",i,llr[i],((s>>(i&0x1f))&1));
    if (((s>>(i%32))&1)==0)
      llr[i] = -llr[i];

  }
}
	     

1949
uint8_t get_num_pdcch_symbols(uint8_t num_dci,
1950 1951
			 DCI_ALLOC_t *dci_alloc,
			 LTE_DL_FRAME_PARMS *frame_parms,
1952
			 uint8_t subframe) {
1953

1954 1955 1956
  uint16_t numCCE = 0;
  uint8_t i;
  uint8_t nCCEmin = 0;
1957 1958 1959

  // check pdcch duration imposed by PHICH duration (Section 6.9 of 36-211)
  if (frame_parms->Ncp==1) { // extended prefix
knopp's avatar
knopp committed
1960
    if ((frame_parms->frame_type == TDD) && 
1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
	((frame_parms->tdd_config<3)||(frame_parms->tdd_config==6)) &&
	((subframe==1) || (subframe==6))) // subframes 1 and 6 (S-subframes) for 5ms switching periodicity are 2 symbols
      nCCEmin = 2;
    else {   // 10ms switching periodicity is always 3 symbols, any DL-only subframe is 3 symbols
      nCCEmin = 3;
    }
  }

  // compute numCCE
  for (i=0;i<num_dci;i++) {
1971
    //     printf("dci %d => %d\n",i,dci_alloc[i].L);
1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003
    numCCE += (1<<(dci_alloc[i].L));
  }

  //if ((9*numCCE) <= (frame_parms->N_RB_DL*2))
  if (numCCE <= get_nCCE(1, frame_parms, get_mi(frame_parms, subframe)))
    return(cmax(1,nCCEmin));
  //else if ((9*numCCE) <= (frame_parms->N_RB_DL*((frame_parms->nb_antennas_tx_eNB==4) ? 4 : 5)))
  else if (numCCE < get_nCCE(2, frame_parms, get_mi(frame_parms, subframe)))
    return(cmax(2,nCCEmin));
  //else if ((9*numCCE) <= (frame_parms->N_RB_DL*((frame_parms->nb_antennas_tx_eNB==4) ? 7 : 8)))
  else if (numCCE < get_nCCE(3, frame_parms, get_mi(frame_parms, subframe)))
    return(cmax(3,nCCEmin));
  else if (frame_parms->N_RB_DL<=10) { 
    if (frame_parms->Ncp == 0) { // normal CP
      if ((9*numCCE) <= (frame_parms->N_RB_DL*((frame_parms->nb_antennas_tx_eNB==4) ? 10 : 11)))
	return(4);
    }
    else { // extended CP
      if ((9*numCCE) <= (frame_parms->N_RB_DL*((frame_parms->nb_antennas_tx_eNB==4) ? 9 : 10)))
	return(4);
    }
  }

  
  msg("[PHY] dci.c: get_num_pdcch_symbols subframe %d FATAL, illegal numCCE %d (num_dci %d)\n",subframe,numCCE,num_dci);
  //for (i=0;i<num_dci;i++) {
  //  printf("dci_alloc[%d].L = %d\n",i,dci_alloc[i].L);
  //}  
  //exit(-1);
  return(0);
}

2004 2005
uint8_t generate_dci_top(uint8_t num_ue_spec_dci,
		    uint8_t num_common_dci,
2006
		    DCI_ALLOC_t *dci_alloc, 
2007 2008
		    uint32_t n_rnti,
		    int16_t amp,
2009 2010
		    LTE_DL_FRAME_PARMS *frame_parms,
		    mod_sym_t **txdataF,
2011 2012 2013 2014 2015 2016 2017 2018 2019
		    uint32_t subframe) {

  uint8_t *e_ptr,num_pdcch_symbols;
  int8_t L;
  uint32_t i, lprime;
  uint32_t gain_lin_QPSK,kprime,kprime_mod12,mprime,nsymb,symbol_offset,tti_offset;
  int16_t re_offset;
  uint8_t mi = get_mi(frame_parms,subframe);
  static uint8_t e[DCI_BITS_MAX];	
2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068
  static mod_sym_t yseq0[Msymb],yseq1[Msymb],wbar0[Msymb],wbar1[Msymb];
     
  mod_sym_t *y[2];
  mod_sym_t *wbar[2];
  
  int nushiftmod3 = frame_parms->nushift%3;

  int Msymb2;
  int split_flag=0;

  switch (frame_parms->N_RB_DL) {
  case 100:
    Msymb2 = Msymb;
    break;
  case 75:
    Msymb2 = 3*Msymb/4;
    break;
  case 50:
    Msymb2 = Msymb>>1;
    break;
  case 25:
    Msymb2 = Msymb>>2;
    break;
  case 15:
    Msymb2 = Msymb*15/100;
    break;
  case 6:
    Msymb2 = Msymb*6/100;
    break;
  default:
    Msymb2 = Msymb>>2;
    break;
  }

  num_pdcch_symbols = get_num_pdcch_symbols(num_ue_spec_dci+num_common_dci,dci_alloc,frame_parms,subframe);
  //   printf("subframe %d in generate_dci_top num_pdcch_symbols = %d, num_dci %d\n",
  //    	 subframe,num_pdcch_symbols,num_ue_spec_dci+num_common_dci);
  generate_pcfich(num_pdcch_symbols,
		  amp,
		  frame_parms,
		  txdataF,
		  subframe);

  wbar[0] = &wbar0[0];
  wbar[1] = &wbar1[0];
  y[0] = &yseq0[0];
  y[1] = &yseq1[0];

  // reset all bits to <NIL>, here we set <NIL> elements as 2
2069
  //memset(e, 2, DCI_BITS_MAX);
2070
  // here we interpred NIL as a random QPSK sequence. That makes power estimation easier.
2071 2072
  for (i=0;i<DCI_BITS_MAX;i++) 
    e[i]=taus()&1;
2073 2074 2075 2076 2077 2078 2079
  e_ptr = e;

  // generate DCIs in order of decreasing aggregation level, then common/ue spec
  // MAC is assumed to have ordered the UE spec DCI according to the RNTI-based randomization
  for (L=3;L>=0;L--) {
    for (i=0;i<num_common_dci;i++) {

2080
      if (dci_alloc[i].L == (uint8_t)L) {
2081 2082
	
#ifdef DEBUG_DCI_ENCODING
2083
	LOG_I(PHY,"Generating common DCI %d/%d (nCCE %d) of length %d, aggregation %d (%x)\n",i,num_common_dci,dci_alloc[i].nCCE,dci_alloc[i].dci_length,1<<dci_alloc[i].L,*(unsigned int*)dci_alloc[i].dci_pdu);
2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095
	dump_dci(frame_parms,&dci_alloc[i]);
#endif

	e_ptr = generate_dci0(dci_alloc[i].dci_pdu,
			      e+(72*dci_alloc[i].nCCE),
			      dci_alloc[i].dci_length,
			      dci_alloc[i].L,
			      dci_alloc[i].rnti);    
      }
    }
    for (;i<num_ue_spec_dci + num_common_dci;i++) {

2096
      if (dci_alloc[i].L == (uint8_t)L) {
2097 2098
      
#ifdef DEBUG_DCI_ENCODING
2099
	LOG_I(PHY," Generating UE (rnti %x) specific DCI %d of length %d, aggregation %d, format %d (%x)\n",dci_alloc[i].rnti,i,dci_alloc[i].dci_length,1<<dci_alloc[i].L,dci_alloc[i].format,dci_alloc[i].dci_pdu);
2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121
	dump_dci(frame_parms,&dci_alloc[i]);
#endif

	e_ptr = generate_dci0(dci_alloc[i].dci_pdu,
			      e+(72*dci_alloc[i].nCCE),
			      dci_alloc[i].dci_length,
			      dci_alloc[i].L,
			      dci_alloc[i].rnti);        
      }
    }
  }

  // Scrambling
  //  printf("pdcch scrambling\n");
  pdcch_scrambling(frame_parms,
		   subframe,
		   e,
                   8*get_nquad(num_pdcch_symbols, frame_parms, mi));
		   //72*get_nCCE(num_pdcch_symbols,frame_parms,mi));
  

#ifdef DEBUG_DCI_ENCODING
2122
  LOG_I(PHY," PDCCH Modulation, Msymb %d\n",Msymb);
2123 2124 2125
#endif
  // Now do modulation
  if (frame_parms->mode1_flag==1) 
2126
    gain_lin_QPSK = (int16_t)((amp*ONE_OVER_SQRT2_Q15)>>15);  
2127 2128 2129 2130 2131 2132
  else
    gain_lin_QPSK = amp/2;  

  e_ptr = e;
  if (frame_parms->mode1_flag) { //SISO

2133

2134
    for (i=0;i<Msymb2;i++) {
2135 2136 2137 2138
      //((int16_t*)(&(y[0][i])))[0] = (*e_ptr == 1) ? -gain_lin_QPSK : gain_lin_QPSK;
      //((int16_t*)(&(y[1][i])))[0] = (*e_ptr == 1) ? -gain_lin_QPSK : gain_lin_QPSK;
      ((int16_t*)(&(y[0][i])))[0] = (*e_ptr == 2) ? 0 : (*e_ptr == 1) ? -gain_lin_QPSK : gain_lin_QPSK;
      ((int16_t*)(&(y[1][i])))[0] = (*e_ptr == 2) ? 0 : (*e_ptr == 1) ? -gain_lin_QPSK : gain_lin_QPSK;
2139 2140

      e_ptr++;
2141 2142 2143 2144
      //((int16_t*)(&(y[0][i])))[1] = (*e_ptr == 1) ? -gain_lin_QPSK : gain_lin_QPSK;
      //((int16_t*)(&(y[1][i])))[1] = (*e_ptr == 1) ? -gain_lin_QPSK : gain_lin_QPSK;
      ((int16_t*)(&(y[0][i])))[1] = (*e_ptr == 2) ? 0 : (*e_ptr == 1) ? -gain_lin_QPSK : gain_lin_QPSK;
      ((int16_t*)(&(y[1][i])))[1] = (*e_ptr == 2) ? 0 : (*e_ptr == 1) ? -gain_lin_QPSK : gain_lin_QPSK;
2145 2146 2147 2148 2149 2150

      e_ptr++;
    }
  }
  else { //ALAMOUTI    

2151

2152 2153 2154
      for (i=0;i<Msymb2;i+=2) {

#ifdef DEBUG_DCI_ENCODING
2155
	LOG_I(PHY," PDCCH Modulation (TX diversity): REG %d\n",i>>2);
2156 2157
#endif
	// first antenna position n -> x0
2158
	((int16_t*)&y[0][i])[0] = (*e_ptr == 1) ? -gain_lin_QPSK : gain_lin_QPSK;
2159
	e_ptr++;
2160
	((int16_t*)&y[0][i])[1] = (*e_ptr == 1) ? -gain_lin_QPSK : gain_lin_QPSK;
2161 2162 2163
	e_ptr++;

	// second antenna position n -> -x1*
2164
	((int16_t*)&y[1][i])[0] = (*e_ptr == 1) ? gain_lin_QPSK : -gain_lin_QPSK;
2165
	e_ptr++;
2166
	((int16_t*)&y[1][i])[1] = (*e_ptr == 1) ? -gain_lin_QPSK : gain_lin_QPSK;
2167 2168 2169
	e_ptr++;

	// fill in the rest of the ALAMOUTI precoding
2170 2171 2172 2173
	((int16_t*)&y[0][i+1])[0] = -((int16_t*)&y[1][i])[0];
	((int16_t*)&y[0][i+1])[1] = ((int16_t*)&y[1][i])[1];
	((int16_t*)&y[1][i+1])[0] = ((int16_t*)&y[0][i])[0];
	((int16_t*)&y[1][i+1])[1] = -((int16_t*)&y[0][i])[1];