ulsch_demodulation_NB_IoT.c 76.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.0  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

/*! \file PHY/LTE_TRANSPORT/ulsch_demodulation.c
* \brief Top-level routines for demodulating the PUSCH physical channel from 36.211 V8.6 2009-03
Matthieu Kanj's avatar
Matthieu Kanj committed
24 25
* \authors V. Savaux, M. Kanj
* \date 2018
26
* \version 0.1
Matthieu Kanj's avatar
Matthieu Kanj committed
27 28
* \company b<>com
* \email: vincent.savaux@b-com.com , matthieu.kanj@b-com.com
29 30 31 32 33 34 35 36
* \note
* \warning
*/

#include "PHY/defs_NB_IoT.h"
#include "PHY/extern_NB_IoT.h"
#include "defs_NB_IoT.h"
#include "extern_NB_IoT.h"
Matthieu Kanj's avatar
Matthieu Kanj committed
37 38
//#include "PHY/CODING/lte_interleaver2.h"
#include "PHY/CODING/extern.h"
39 40 41 42 43 44 45 46 47 48 49 50 51 52
//#define DEBUG_ULSCH
//#include "PHY/sse_intrin.h"
#include "PHY/LTE_ESTIMATION/defs_NB_IoT.h"
#include "T.h"

//extern char* namepointer_chMag ;
//eren
//extern int **ulchmag_eren;
//eren

static short jitter[8]  __attribute__ ((aligned(16))) = {1,0,0,1,0,1,1,0};
static short jitterc[8] __attribute__ ((aligned(16))) = {0,1,1,0,1,0,0,1};

#ifndef OFDMA_ULSCH
Matthieu Kanj's avatar
Matthieu Kanj committed
53
void lte_idft_NB_IoT(LTE_DL_FRAME_PARMS *frame_parms,uint32_t *z, uint16_t Msc_PUSCH)
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
{
#if defined(__x86_64__) || defined(__i386__)
  __m128i idft_in128[3][1200],idft_out128[3][1200];
  __m128i norm128;
#elif defined(__arm__)
  int16x8_t idft_in128[3][1200],idft_out128[3][1200];
  int16x8_t norm128;
#endif
  int16_t *idft_in0=(int16_t*)idft_in128[0],*idft_out0=(int16_t*)idft_out128[0];
  int16_t *idft_in1=(int16_t*)idft_in128[1],*idft_out1=(int16_t*)idft_out128[1];
  int16_t *idft_in2=(int16_t*)idft_in128[2],*idft_out2=(int16_t*)idft_out128[2];

  uint32_t *z0,*z1,*z2,*z3,*z4,*z5,*z6,*z7,*z8,*z9,*z10=NULL,*z11=NULL;
  int i,ip;

  //  printf("Doing lte_idft for Msc_PUSCH %d\n",Msc_PUSCH);

71
   // Normal prefix
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    z0 = z;
    z1 = z0+(frame_parms->N_RB_DL*12);
    z2 = z1+(frame_parms->N_RB_DL*12);
    //pilot
    z3 = z2+(2*frame_parms->N_RB_DL*12);
    z4 = z3+(frame_parms->N_RB_DL*12);
    z5 = z4+(frame_parms->N_RB_DL*12);

    z6 = z5+(frame_parms->N_RB_DL*12);
    z7 = z6+(frame_parms->N_RB_DL*12);
    z8 = z7+(frame_parms->N_RB_DL*12);
    //pilot
    z9 = z8+(2*frame_parms->N_RB_DL*12);
    z10 = z9+(frame_parms->N_RB_DL*12);
    // srs
    z11 = z10+(frame_parms->N_RB_DL*12);
88
  
89 90 91 92 93 94 95 96 97 98 99 100 101 102
  // conjugate input
  for (i=0; i<(Msc_PUSCH>>2); i++) {
#if defined(__x86_64__)||defined(__i386__)
    *&(((__m128i*)z0)[i])=_mm_sign_epi16(*&(((__m128i*)z0)[i]),*(__m128i*)&conjugate2[0]);
    *&(((__m128i*)z1)[i])=_mm_sign_epi16(*&(((__m128i*)z1)[i]),*(__m128i*)&conjugate2[0]);
    *&(((__m128i*)z2)[i])=_mm_sign_epi16(*&(((__m128i*)z2)[i]),*(__m128i*)&conjugate2[0]);
    *&(((__m128i*)z3)[i])=_mm_sign_epi16(*&(((__m128i*)z3)[i]),*(__m128i*)&conjugate2[0]);
    *&(((__m128i*)z4)[i])=_mm_sign_epi16(*&(((__m128i*)z4)[i]),*(__m128i*)&conjugate2[0]);
    *&(((__m128i*)z5)[i])=_mm_sign_epi16(*&(((__m128i*)z5)[i]),*(__m128i*)&conjugate2[0]);
    *&(((__m128i*)z6)[i])=_mm_sign_epi16(*&(((__m128i*)z6)[i]),*(__m128i*)&conjugate2[0]);
    *&(((__m128i*)z7)[i])=_mm_sign_epi16(*&(((__m128i*)z7)[i]),*(__m128i*)&conjugate2[0]);
    *&(((__m128i*)z8)[i])=_mm_sign_epi16(*&(((__m128i*)z8)[i]),*(__m128i*)&conjugate2[0]);
    *&(((__m128i*)z9)[i])=_mm_sign_epi16(*&(((__m128i*)z9)[i]),*(__m128i*)&conjugate2[0]);

103
    // if (frame_parms->Ncp==NORMAL_NB_IoT) {
104 105
      *&(((__m128i*)z10)[i])=_mm_sign_epi16(*&(((__m128i*)z10)[i]),*(__m128i*)&conjugate2[0]);
      *&(((__m128i*)z11)[i])=_mm_sign_epi16(*&(((__m128i*)z11)[i]),*(__m128i*)&conjugate2[0]);
106
    // }
107 108 109 110 111 112 113 114 115 116 117 118 119
#elif defined(__arm__)
    *&(((int16x8_t*)z0)[i])=vmulq_s16(*&(((int16x8_t*)z0)[i]),*(int16x8_t*)&conjugate2[0]);
    *&(((int16x8_t*)z1)[i])=vmulq_s16(*&(((int16x8_t*)z1)[i]),*(int16x8_t*)&conjugate2[0]);
    *&(((int16x8_t*)z2)[i])=vmulq_s16(*&(((int16x8_t*)z2)[i]),*(int16x8_t*)&conjugate2[0]);
    *&(((int16x8_t*)z3)[i])=vmulq_s16(*&(((int16x8_t*)z3)[i]),*(int16x8_t*)&conjugate2[0]);
    *&(((int16x8_t*)z4)[i])=vmulq_s16(*&(((int16x8_t*)z4)[i]),*(int16x8_t*)&conjugate2[0]);
    *&(((int16x8_t*)z5)[i])=vmulq_s16(*&(((int16x8_t*)z5)[i]),*(int16x8_t*)&conjugate2[0]);
    *&(((int16x8_t*)z6)[i])=vmulq_s16(*&(((int16x8_t*)z6)[i]),*(int16x8_t*)&conjugate2[0]);
    *&(((int16x8_t*)z7)[i])=vmulq_s16(*&(((int16x8_t*)z7)[i]),*(int16x8_t*)&conjugate2[0]);
    *&(((int16x8_t*)z8)[i])=vmulq_s16(*&(((int16x8_t*)z8)[i]),*(int16x8_t*)&conjugate2[0]);
    *&(((int16x8_t*)z9)[i])=vmulq_s16(*&(((int16x8_t*)z9)[i]),*(int16x8_t*)&conjugate2[0]);


120
    // if (frame_parms->Ncp==NORMAL_NB_IoT) {
121 122
      *&(((int16x8_t*)z10)[i])=vmulq_s16(*&(((int16x8_t*)z10)[i]),*(int16x8_t*)&conjugate2[0]);
      *&(((int16x8_t*)z11)[i])=vmulq_s16(*&(((int16x8_t*)z11)[i]),*(int16x8_t*)&conjugate2[0]);
123
    // }
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

#endif
  }

  for (i=0,ip=0; i<Msc_PUSCH; i++,ip+=4) {
    ((uint32_t*)idft_in0)[ip+0] =  z0[i];
    ((uint32_t*)idft_in0)[ip+1] =  z1[i];
    ((uint32_t*)idft_in0)[ip+2] =  z2[i];
    ((uint32_t*)idft_in0)[ip+3] =  z3[i];
    ((uint32_t*)idft_in1)[ip+0] =  z4[i];
    ((uint32_t*)idft_in1)[ip+1] =  z5[i];
    ((uint32_t*)idft_in1)[ip+2] =  z6[i];
    ((uint32_t*)idft_in1)[ip+3] =  z7[i];
    ((uint32_t*)idft_in2)[ip+0] =  z8[i];
    ((uint32_t*)idft_in2)[ip+1] =  z9[i];

140
    // if (frame_parms->Ncp==0) {
141 142
      ((uint32_t*)idft_in2)[ip+2] =  z10[i];
      ((uint32_t*)idft_in2)[ip+3] =  z11[i];
143
    // }
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
  }


  switch (Msc_PUSCH) {
  case 12:
    dft12((int16_t *)idft_in0,(int16_t *)idft_out0);
    dft12((int16_t *)idft_in1,(int16_t *)idft_out1);
    dft12((int16_t *)idft_in2,(int16_t *)idft_out2);

#if defined(__x86_64__)||defined(__i386__)
    norm128 = _mm_set1_epi16(9459);
#elif defined(__arm__)
    norm128 = vdupq_n_s16(9459);
#endif
    for (i=0; i<12; i++) {
#if defined(__x86_64__)||defined(__i386__)
      ((__m128i*)idft_out0)[i] = _mm_slli_epi16(_mm_mulhi_epi16(((__m128i*)idft_out0)[i],norm128),1);
      ((__m128i*)idft_out1)[i] = _mm_slli_epi16(_mm_mulhi_epi16(((__m128i*)idft_out1)[i],norm128),1);
      ((__m128i*)idft_out2)[i] = _mm_slli_epi16(_mm_mulhi_epi16(((__m128i*)idft_out2)[i],norm128),1);
#elif defined(__arm__)
      ((int16x8_t*)idft_out0)[i] = vqdmulhq_s16(((int16x8_t*)idft_out0)[i],norm128);
      ((int16x8_t*)idft_out1)[i] = vqdmulhq_s16(((int16x8_t*)idft_out1)[i],norm128);
      ((int16x8_t*)idft_out2)[i] = vqdmulhq_s16(((int16x8_t*)idft_out2)[i],norm128);
#endif
    }

    break;

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
  // case 24:
  //   dft24(idft_in0,idft_out0,1);
  //   dft24(idft_in1,idft_out1,1);
  //   dft24(idft_in2,idft_out2,1);
  //   break;

  // case 36:
  //   dft36(idft_in0,idft_out0,1);
  //   dft36(idft_in1,idft_out1,1);
  //   dft36(idft_in2,idft_out2,1);
  //   break;

  // case 48:
  //   dft48(idft_in0,idft_out0,1);
  //   dft48(idft_in1,idft_out1,1);
  //   dft48(idft_in2,idft_out2,1);
  //   break;

  // case 60:
  //   dft60(idft_in0,idft_out0,1);
  //   dft60(idft_in1,idft_out1,1);
  //   dft60(idft_in2,idft_out2,1);
  //   break;

  // case 72:
  //   dft72(idft_in0,idft_out0,1);
  //   dft72(idft_in1,idft_out1,1);
  //   dft72(idft_in2,idft_out2,1);
  //   break;

  // case 96:
  //   dft96(idft_in0,idft_out0,1);
  //   dft96(idft_in1,idft_out1,1);
  //   dft96(idft_in2,idft_out2,1);
  //   break;

  // case 108:
  //   dft108(idft_in0,idft_out0,1);
  //   dft108(idft_in1,idft_out1,1);
  //   dft108(idft_in2,idft_out2,1);
  //   break;

  // case 120:
  //   dft120(idft_in0,idft_out0,1);
  //   dft120(idft_in1,idft_out1,1);
  //   dft120(idft_in2,idft_out2,1);
  //   break;

  // case 144:
  //   dft144(idft_in0,idft_out0,1);
  //   dft144(idft_in1,idft_out1,1);
  //   dft144(idft_in2,idft_out2,1);
  //   break;

  // case 180:
  //   dft180(idft_in0,idft_out0,1);
  //   dft180(idft_in1,idft_out1,1);
  //   dft180(idft_in2,idft_out2,1);
  //   break;

  // case 192:
  //   dft192(idft_in0,idft_out0,1);
  //   dft192(idft_in1,idft_out1,1);
  //   dft192(idft_in2,idft_out2,1);
  //   break;

  // case 216:
  //   dft216(idft_in0,idft_out0,1);
  //   dft216(idft_in1,idft_out1,1);
  //   dft216(idft_in2,idft_out2,1);
  //   break;

  // case 240:
  //   dft240(idft_in0,idft_out0,1);
  //   dft240(idft_in1,idft_out1,1);
  //   dft240(idft_in2,idft_out2,1);
  //   break;

  // case 288:
  //   dft288(idft_in0,idft_out0,1);
  //   dft288(idft_in1,idft_out1,1);
  //   dft288(idft_in2,idft_out2,1);
  //   break;

  // case 300:
  //   dft300(idft_in0,idft_out0,1);
  //   dft300(idft_in1,idft_out1,1);
  //   dft300(idft_in2,idft_out2,1);
  //   break;

  // case 324:
  //   dft324((int16_t*)idft_in0,(int16_t*)idft_out0,1);
  //   dft324((int16_t*)idft_in1,(int16_t*)idft_out1,1);
  //   dft324((int16_t*)idft_in2,(int16_t*)idft_out2,1);
  //   break;

  // case 360:
  //   dft360((int16_t*)idft_in0,(int16_t*)idft_out0,1);
  //   dft360((int16_t*)idft_in1,(int16_t*)idft_out1,1);
  //   dft360((int16_t*)idft_in2,(int16_t*)idft_out2,1);
  //   break;

  // case 384:
  //   dft384((int16_t*)idft_in0,(int16_t*)idft_out0,1);
  //   dft384((int16_t*)idft_in1,(int16_t*)idft_out1,1);
  //   dft384((int16_t*)idft_in2,(int16_t*)idft_out2,1);
  //   break;

  // case 432:
  //   dft432((int16_t*)idft_in0,(int16_t*)idft_out0,1);
  //   dft432((int16_t*)idft_in1,(int16_t*)idft_out1,1);
  //   dft432((int16_t*)idft_in2,(int16_t*)idft_out2,1);
  //   break;

  // case 480:
  //   dft480((int16_t*)idft_in0,(int16_t*)idft_out0,1);
  //   dft480((int16_t*)idft_in1,(int16_t*)idft_out1,1);
  //   dft480((int16_t*)idft_in2,(int16_t*)idft_out2,1);
  //   break;

  // case 540:
  //   dft540((int16_t*)idft_in0,(int16_t*)idft_out0,1);
  //   dft540((int16_t*)idft_in1,(int16_t*)idft_out1,1);
  //   dft540((int16_t*)idft_in2,(int16_t*)idft_out2,1);
  //   break;

  // case 576:
  //   dft576((int16_t*)idft_in0,(int16_t*)idft_out0,1);
  //   dft576((int16_t*)idft_in1,(int16_t*)idft_out1,1);
  //   dft576((int16_t*)idft_in2,(int16_t*)idft_out2,1);
  //   break;

  // case 600:
  //   dft600((int16_t*)idft_in0,(int16_t*)idft_out0,1);
  //   dft600((int16_t*)idft_in1,(int16_t*)idft_out1,1);
  //   dft600((int16_t*)idft_in2,(int16_t*)idft_out2,1);
  //   break;

  // case 648:
  //   dft648((int16_t*)idft_in0,(int16_t*)idft_out0,1);
  //   dft648((int16_t*)idft_in1,(int16_t*)idft_out1,1);
  //   dft648((int16_t*)idft_in2,(int16_t*)idft_out2,1);
  //   break;

  // case 720:
  //   dft720((int16_t*)idft_in0,(int16_t*)idft_out0,1);
  //   dft720((int16_t*)idft_in1,(int16_t*)idft_out1,1);
  //   dft720((int16_t*)idft_in2,(int16_t*)idft_out2,1);
  //   break;

  // case 864:
  //   dft864((int16_t*)idft_in0,(int16_t*)idft_out0,1);
  //   dft864((int16_t*)idft_in1,(int16_t*)idft_out1,1);
  //   dft864((int16_t*)idft_in2,(int16_t*)idft_out2,1);
  //   break;

  // case 900:
  //   dft900((int16_t*)idft_in0,(int16_t*)idft_out0,1);
  //   dft900((int16_t*)idft_in1,(int16_t*)idft_out1,1);
  //   dft900((int16_t*)idft_in2,(int16_t*)idft_out2,1);
  //   break;

  // case 960:
  //   dft960((int16_t*)idft_in0,(int16_t*)idft_out0,1);
  //   dft960((int16_t*)idft_in1,(int16_t*)idft_out1,1);
  //   dft960((int16_t*)idft_in2,(int16_t*)idft_out2,1);
  //   break;

  // case 972:
  //   dft972((int16_t*)idft_in0,(int16_t*)idft_out0,1);
  //   dft972((int16_t*)idft_in1,(int16_t*)idft_out1,1);
  //   dft972((int16_t*)idft_in2,(int16_t*)idft_out2,1);
  //   break;

  // case 1080:
  //   dft1080((int16_t*)idft_in0,(int16_t*)idft_out0,1);
  //   dft1080((int16_t*)idft_in1,(int16_t*)idft_out1,1);
  //   dft1080((int16_t*)idft_in2,(int16_t*)idft_out2,1);
  //   break;

  // case 1152:
  //   dft1152((int16_t*)idft_in0,(int16_t*)idft_out0,1);
  //   dft1152((int16_t*)idft_in1,(int16_t*)idft_out1,1);
  //   dft1152((int16_t*)idft_in2,(int16_t*)idft_out2,1);
  //   break;

  // case 1200:
  //   dft1200(idft_in0,idft_out0,1);
  //   dft1200(idft_in1,idft_out1,1);
  //   dft1200(idft_in2,idft_out2,1);
  //   break;
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

  default:
    // should not be reached
    LOG_E( PHY, "Unsupported Msc_PUSCH value of %"PRIu16"\n", Msc_PUSCH );
    return;
  }



  for (i=0,ip=0; i<Msc_PUSCH; i++,ip+=4) {
    z0[i]     = ((uint32_t*)idft_out0)[ip];
    /*
      printf("out0 (%d,%d),(%d,%d),(%d,%d),(%d,%d)\n",
      ((int16_t*)&idft_out0[ip])[0],((int16_t*)&idft_out0[ip])[1],
      ((int16_t*)&idft_out0[ip+1])[0],((int16_t*)&idft_out0[ip+1])[1],
      ((int16_t*)&idft_out0[ip+2])[0],((int16_t*)&idft_out0[ip+2])[1],
      ((int16_t*)&idft_out0[ip+3])[0],((int16_t*)&idft_out0[ip+3])[1]);
    */
    z1[i]     = ((uint32_t*)idft_out0)[ip+1];
    z2[i]     = ((uint32_t*)idft_out0)[ip+2];
    z3[i]     = ((uint32_t*)idft_out0)[ip+3];
    z4[i]     = ((uint32_t*)idft_out1)[ip+0];
    z5[i]     = ((uint32_t*)idft_out1)[ip+1];
    z6[i]     = ((uint32_t*)idft_out1)[ip+2];
    z7[i]     = ((uint32_t*)idft_out1)[ip+3];
    z8[i]     = ((uint32_t*)idft_out2)[ip];
    z9[i]     = ((uint32_t*)idft_out2)[ip+1];

391
    // if (frame_parms->Ncp==0) {
392 393
      z10[i]    = ((uint32_t*)idft_out2)[ip+2];
      z11[i]    = ((uint32_t*)idft_out2)[ip+3];
394
    // }
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
  }

  // conjugate output
  for (i=0; i<(Msc_PUSCH>>2); i++) {
#if defined(__x86_64__) || defined(__i386__)
    ((__m128i*)z0)[i]=_mm_sign_epi16(((__m128i*)z0)[i],*(__m128i*)&conjugate2[0]);
    ((__m128i*)z1)[i]=_mm_sign_epi16(((__m128i*)z1)[i],*(__m128i*)&conjugate2[0]);
    ((__m128i*)z2)[i]=_mm_sign_epi16(((__m128i*)z2)[i],*(__m128i*)&conjugate2[0]);
    ((__m128i*)z3)[i]=_mm_sign_epi16(((__m128i*)z3)[i],*(__m128i*)&conjugate2[0]);
    ((__m128i*)z4)[i]=_mm_sign_epi16(((__m128i*)z4)[i],*(__m128i*)&conjugate2[0]);
    ((__m128i*)z5)[i]=_mm_sign_epi16(((__m128i*)z5)[i],*(__m128i*)&conjugate2[0]);
    ((__m128i*)z6)[i]=_mm_sign_epi16(((__m128i*)z6)[i],*(__m128i*)&conjugate2[0]);
    ((__m128i*)z7)[i]=_mm_sign_epi16(((__m128i*)z7)[i],*(__m128i*)&conjugate2[0]);
    ((__m128i*)z8)[i]=_mm_sign_epi16(((__m128i*)z8)[i],*(__m128i*)&conjugate2[0]);
    ((__m128i*)z9)[i]=_mm_sign_epi16(((__m128i*)z9)[i],*(__m128i*)&conjugate2[0]);

411
    // if (frame_parms->Ncp==NORMAL_NB_IoT) {
412 413
      ((__m128i*)z10)[i]=_mm_sign_epi16(((__m128i*)z10)[i],*(__m128i*)&conjugate2[0]);
      ((__m128i*)z11)[i]=_mm_sign_epi16(((__m128i*)z11)[i],*(__m128i*)&conjugate2[0]);
414
    // }
415 416 417 418 419 420 421 422 423 424 425 426 427
#elif defined(__arm__)
    *&(((int16x8_t*)z0)[i])=vmulq_s16(*&(((int16x8_t*)z0)[i]),*(int16x8_t*)&conjugate2[0]);
    *&(((int16x8_t*)z1)[i])=vmulq_s16(*&(((int16x8_t*)z1)[i]),*(int16x8_t*)&conjugate2[0]);
    *&(((int16x8_t*)z2)[i])=vmulq_s16(*&(((int16x8_t*)z2)[i]),*(int16x8_t*)&conjugate2[0]);
    *&(((int16x8_t*)z3)[i])=vmulq_s16(*&(((int16x8_t*)z3)[i]),*(int16x8_t*)&conjugate2[0]);
    *&(((int16x8_t*)z4)[i])=vmulq_s16(*&(((int16x8_t*)z4)[i]),*(int16x8_t*)&conjugate2[0]);
    *&(((int16x8_t*)z5)[i])=vmulq_s16(*&(((int16x8_t*)z5)[i]),*(int16x8_t*)&conjugate2[0]);
    *&(((int16x8_t*)z6)[i])=vmulq_s16(*&(((int16x8_t*)z6)[i]),*(int16x8_t*)&conjugate2[0]);
    *&(((int16x8_t*)z7)[i])=vmulq_s16(*&(((int16x8_t*)z7)[i]),*(int16x8_t*)&conjugate2[0]);
    *&(((int16x8_t*)z8)[i])=vmulq_s16(*&(((int16x8_t*)z8)[i]),*(int16x8_t*)&conjugate2[0]);
    *&(((int16x8_t*)z9)[i])=vmulq_s16(*&(((int16x8_t*)z9)[i]),*(int16x8_t*)&conjugate2[0]);


428
    // if (frame_parms->Ncp==NORMAL_NB_IoT) {
429 430
      *&(((int16x8_t*)z10)[i])=vmulq_s16(*&(((int16x8_t*)z10)[i]),*(int16x8_t*)&conjugate2[0]);
      *&(((int16x8_t*)z11)[i])=vmulq_s16(*&(((int16x8_t*)z11)[i]),*(int16x8_t*)&conjugate2[0]);
431
    // }
432 433 434 435 436 437 438 439 440 441 442 443 444

#endif
  }

#if defined(__x86_64__) || defined(__i386__)
  _mm_empty();
  _m_empty();
#endif

}
#endif


Matthieu Kanj's avatar
Matthieu Kanj committed
445 446
int32_t ulsch_bpsk_llr_NB_IoT(PHY_VARS_eNB *eNB, 
                              LTE_DL_FRAME_PARMS *frame_parms,
447 448
                              int32_t **rxdataF_comp,
                              int16_t *ulsch_llr, 
449 450
                              uint8_t symbol,
                              uint16_t ul_sc_start, 
451 452 453 454 455
                              uint8_t UE_id, 
                              int16_t **llrp)
{

  int16_t *rxF; 
456 457
 // uint32_t I_sc = 11;//eNB->ulsch_NB_IoT[UE_id]->harq_process->I_sc;  // NB_IoT: subcarrier indication field: must be defined in higher layer
 // uint16_t ul_sc_start; // subcarrier start index into UL RB 
458 459
  // int i; 

460
  //ul_sc_start = get_UL_sc_start_NB_IoT(I_sc); // NB-IoT: get the used subcarrier in RB
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 499 500 501 502 503
  rxF = (int16_t *)&rxdataF_comp[0][(symbol*frame_parms->N_RB_DL*12) + ul_sc_start]; 

  //  printf("qpsk llr for symbol %d (pos %d), llr offset %d\n",symbol,(symbol*frame_parms->N_RB_DL*12),llr128U-(__m128i*)ulsch_llr);

    //printf("%d,%d,%d,%d,%d,%d,%d,%d\n",((int16_t *)rxF)[0],((int16_t *)rxF)[1],((int16_t *)rxF)[2],((int16_t *)rxF)[3],((int16_t *)rxF)[4],((int16_t *)rxF)[5],((int16_t *)rxF)[6],((int16_t *)rxF)[7]);
    *(*llrp) = *rxF;
    //rxF++;
    (*llrp)++;

  return(0);

}

// int32_t ulsch_qpsk_llr_NB_IoT(NB_IoT_DL_FRAME_PARMS *frame_parms,
//                               int32_t **rxdataF_comp,
//                               int16_t *ulsch_llr,
//                               uint8_t symbol,
//                               uint16_t nb_rb,
//                               int16_t **llrp)
// {
// #if defined(__x86_64__) || defined(__i386__)
//   __m128i *rxF=(__m128i*)&rxdataF_comp[0][(symbol*frame_parms->N_RB_DL*12)];
//   __m128i **llrp128 = (__m128i **)llrp;
// #elif defined(__arm__)
//   int16x8_t *rxF= (int16x8_t*)&rxdataF_comp[0][(symbol*frame_parms->N_RB_DL*12)];
//   int16x8_t **llrp128 = (int16x8_t **)llrp;
// #endif

//   int i;

//   //  printf("qpsk llr for symbol %d (pos %d), llr offset %d\n",symbol,(symbol*frame_parms->N_RB_DL*12),llr128U-(__m128i*)ulsch_llr);

//   for (i=0; i<(nb_rb*3); i++) {
//     //printf("%d,%d,%d,%d,%d,%d,%d,%d\n",((int16_t *)rxF)[0],((int16_t *)rxF)[1],((int16_t *)rxF)[2],((int16_t *)rxF)[3],((int16_t *)rxF)[4],((int16_t *)rxF)[5],((int16_t *)rxF)[6],((int16_t *)rxF)[7]);
//     *(*llrp128) = *rxF;
//     rxF++;
//     (*llrp128)++;
//   }

// #if defined(__x86_64__) || defined(__i386__)
//   _mm_empty();
//   _m_empty();
// #endif
504

505
//   return(0);
506

507
// }
508

Matthieu Kanj's avatar
Matthieu Kanj committed
509 510
int32_t ulsch_qpsk_llr_NB_IoT(PHY_VARS_eNB *eNB, 
                              LTE_DL_FRAME_PARMS *frame_parms,
511
                              int32_t **rxdataF_comp,
512 513
                              int16_t *ulsch_llr, 
                              uint8_t symbol, 
Matthieu Kanj's avatar
Matthieu Kanj committed
514
                              uint8_t UE_id,
515
                              uint16_t ul_sc_start,
Matthieu Kanj's avatar
Matthieu Kanj committed
516
                              uint8_t Nsc_RU,
Matthieu Kanj's avatar
Matthieu Kanj committed
517
                              int16_t *llrp)
518 519
{

520
  int32_t *rxF; 
Matthieu Kanj's avatar
Matthieu Kanj committed
521
  int32_t *llrp32; // = (int32_t *)llrp; 
Matthieu Kanj's avatar
Matthieu Kanj committed
522
  //uint32_t I_sc = 11;//eNB->ulsch_NB_IoT[UE_id]->harq_process->I_sc;  // NB_IoT: subcarrier indication field: must be defined in higher layer
523
  //uint16_t ul_sc_start; // subcarrier start index into UL RB 
Matthieu Kanj's avatar
Matthieu Kanj committed
524
  //uint8_t Nsc_RU = 1;//eNB->ulsch_NB_IoT[UE_id]->harq_process->N_sc_RU; // Vincent: number of sc 1,3,6,12 
525
  int i; 
Matthieu Kanj's avatar
Matthieu Kanj committed
526 527
  
  llrp32 = (int32_t *)&llrp[0];
528
  //ul_sc_start = get_UL_sc_start_NB_IoT(I_sc); // NB-IoT: get the used subcarrier in RB
529
  rxF = (int32_t *)&rxdataF_comp[0][(symbol*frame_parms->N_RB_DL*12) + ul_sc_start]; 
530 531 532

  //  printf("qpsk llr for symbol %d (pos %d), llr offset %d\n",symbol,(symbol*frame_parms->N_RB_DL*12),llr128U-(__m128i*)ulsch_llr);

533
  for (i=0; i<Nsc_RU; i++) {
534
    //printf("%d,%d,%d,%d,%d,%d,%d,%d\n",((int16_t *)rxF)[0],((int16_t *)rxF)[1],((int16_t *)rxF)[2],((int16_t *)rxF)[3],((int16_t *)rxF)[4],((int16_t *)rxF)[5],((int16_t *)rxF)[6],((int16_t *)rxF)[7]);
Matthieu Kanj's avatar
Matthieu Kanj committed
535
    /**(*llrp32) = *rxF;
536
    rxF++;
Matthieu Kanj's avatar
Matthieu Kanj committed
537 538 539 540
    (*llrp32)++;*/
    llrp32[i] = rxF[i]; 
  /*printf("\nin llr_%d === %d",ul_sc_start,(int32_t)llrp[i]); 
  printf("\n  in llr_%d === %d",ul_sc_start,llrp32[i]);*/
541 542 543 544 545 546 547
  }

  return(0);

}


Matthieu Kanj's avatar
Matthieu Kanj committed
548
void ulsch_detection_mrc_NB_IoT(LTE_DL_FRAME_PARMS *frame_parms,
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
                         int32_t **rxdataF_comp,
                         int32_t **ul_ch_mag,
                         int32_t **ul_ch_magb,
                         uint8_t symbol,
                         uint16_t nb_rb)
{

#if defined(__x86_64__) || defined(__i386__)

  __m128i *rxdataF_comp128_0,*ul_ch_mag128_0,*ul_ch_mag128_0b;
  __m128i *rxdataF_comp128_1,*ul_ch_mag128_1,*ul_ch_mag128_1b;
#elif defined(__arm__)

  int16x8_t *rxdataF_comp128_0,*ul_ch_mag128_0,*ul_ch_mag128_0b;
  int16x8_t *rxdataF_comp128_1,*ul_ch_mag128_1,*ul_ch_mag128_1b;

#endif
  int32_t i;

  if (frame_parms->nb_antennas_rx>1) {
#if defined(__x86_64__) || defined(__i386__)
    rxdataF_comp128_0   = (__m128i *)&rxdataF_comp[0][symbol*frame_parms->N_RB_DL*12];
    rxdataF_comp128_1   = (__m128i *)&rxdataF_comp[1][symbol*frame_parms->N_RB_DL*12];
    ul_ch_mag128_0      = (__m128i *)&ul_ch_mag[0][symbol*frame_parms->N_RB_DL*12];
    ul_ch_mag128_1      = (__m128i *)&ul_ch_mag[1][symbol*frame_parms->N_RB_DL*12];
    ul_ch_mag128_0b     = (__m128i *)&ul_ch_magb[0][symbol*frame_parms->N_RB_DL*12];
    ul_ch_mag128_1b     = (__m128i *)&ul_ch_magb[1][symbol*frame_parms->N_RB_DL*12];

    // MRC on each re of rb, both on MF output and magnitude (for 16QAM/64QAM llr computation)
    for (i=0; i<nb_rb*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));
      ul_ch_mag128_0[i]    = _mm_adds_epi16(_mm_srai_epi16(ul_ch_mag128_0[i],1),_mm_srai_epi16(ul_ch_mag128_1[i],1));
      ul_ch_mag128_0b[i]   = _mm_adds_epi16(_mm_srai_epi16(ul_ch_mag128_0b[i],1),_mm_srai_epi16(ul_ch_mag128_1b[i],1));
      rxdataF_comp128_0[i] = _mm_add_epi16(rxdataF_comp128_0[i],(*(__m128i*)&jitterc[0]));
583
    }
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598

#elif defined(__arm__)
    rxdataF_comp128_0   = (int16x8_t *)&rxdataF_comp[0][symbol*frame_parms->N_RB_DL*12];
    rxdataF_comp128_1   = (int16x8_t *)&rxdataF_comp[1][symbol*frame_parms->N_RB_DL*12];
    ul_ch_mag128_0      = (int16x8_t *)&ul_ch_mag[0][symbol*frame_parms->N_RB_DL*12];
    ul_ch_mag128_1      = (int16x8_t *)&ul_ch_mag[1][symbol*frame_parms->N_RB_DL*12];
    ul_ch_mag128_0b     = (int16x8_t *)&ul_ch_magb[0][symbol*frame_parms->N_RB_DL*12];
    ul_ch_mag128_1b     = (int16x8_t *)&ul_ch_magb[1][symbol*frame_parms->N_RB_DL*12];

    // MRC on each re of rb, both on MF output and magnitude (for 16QAM/64QAM llr computation)
    for (i=0; i<nb_rb*3; i++) {
      rxdataF_comp128_0[i] = vhaddq_s16(rxdataF_comp128_0[i],rxdataF_comp128_1[i]);
      ul_ch_mag128_0[i]    = vhaddq_s16(ul_ch_mag128_0[i],ul_ch_mag128_1[i]);
      ul_ch_mag128_0b[i]   = vhaddq_s16(ul_ch_mag128_0b[i],ul_ch_mag128_1b[i]);
      rxdataF_comp128_0[i] = vqaddq_s16(rxdataF_comp128_0[i],(*(int16x8_t*)&jitterc[0]));
599
    }
600 601 602


#endif
603
    
604 605 606 607 608 609 610 611 612
  }

#if defined(__x86_64__) || defined(__i386__)
  _mm_empty();
  _m_empty();
#endif
}

void ulsch_extract_rbs_single_NB_IoT(int32_t **rxdataF,
Matthieu Kanj's avatar
Matthieu Kanj committed
613
                                     int32_t **rxdataF_ext, 
Matthieu Kanj's avatar
Matthieu Kanj committed
614
                                     uint16_t UL_RB_ID_NB_IoT, // index of UL NB_IoT resource block !!! may be defined twice : in frame_parms and in NB_IoT_UL_eNB_HARQ_t
615
                                     uint8_t N_sc_RU, // number of subcarriers in UL 
616 617
                                     uint8_t l,
                                     uint8_t Ns,
Matthieu Kanj's avatar
Matthieu Kanj committed
618
                                     LTE_DL_FRAME_PARMS *frame_parms)
619
{
620 621 622 623
  uint16_t  nb_rb1; 
  // uint16_t  nb_rb2; 
  uint8_t   aarx,n;
  // int32_t   *rxF,*rxF_ext;
624
  //uint8_t symbol = l+Ns*frame_parms->symbols_per_tti/2;
625 626
  uint8_t   symbol = l+(7*(Ns&1)); ///symbol within sub-frame 
  // uint16_t ul_sc_start; // subcarrier start index into UL RB 
Matthieu Kanj's avatar
Matthieu Kanj committed
627
  //unsigned short UL_RB_ID_NB_IoT; 
628

629
  // ul_sc_start = get_UL_sc_start_NB_IoT(I_sc); 
Matthieu Kanj's avatar
Matthieu Kanj committed
630
  //UL_RB_ID_NB_IoT = frame_parms->NB_IoT_RB_ID; 
Matthieu Kanj's avatar
Matthieu Kanj committed
631
  ////////////////////////////////////////////////////// if NB_IoT_start is used , no need for nb_rb1 and nb_rb2 
632 633 634

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

635 636
    nb_rb1 = cmin(cmax((int)(frame_parms->N_RB_UL) - (int)(2*UL_RB_ID_NB_IoT),(int)0),(int)(2));    // 2 times no. RBs before the DC
                                 // 2 times no. RBs after the DC
637

638
    // rxF_ext   = &rxdataF_ext[aarx][(symbol*frame_parms->N_RB_UL*12)];
Matthieu Kanj's avatar
Matthieu Kanj committed
639 640 641 642 643
  /*  for (n=0;n<12;n++){

            rxdataF_ext[aarx][symbol*frame_parms->N_RB_UL*12 + n] = rxdataF[aarx][NB_IoT_start_UL + (symbol)*frame_parms->ofdm_symbol_size + n]; 
        // since NB_IoT_start -1 is to apply for UPLINK, in this case the computation of start ul and dl should be done during the initializaiton
    }*/
644

645
    if (nb_rb1) { // RB NB-IoT is in the first half
646

647
      for (n=0;n<12;n++){ // extract whole RB of 12 subcarriers
648
        // Note that FFT splits the RBs 
649
        // !!! Note that frame_parms->N_RB_UL is the number of RB in LTE
650
        // rxdataF_ext[aarx][symbol*frame_parms->N_RB_UL*12 + n] = rxdataF[aarx][UL_RB_ID_NB_IoT*12 + ul_sc_start + frame_parms->first_carrier_offset + symbol*frame_parms->ofdm_symbol_size + n];
Matthieu Kanj's avatar
Matthieu Kanj committed
651 652
        rxdataF_ext[aarx][symbol*frame_parms->N_RB_UL*12 + n] = rxdataF[aarx][UL_RB_ID_NB_IoT*12 + frame_parms->first_carrier_offset + (symbol)*frame_parms->ofdm_symbol_size + n]; 
        //rxdataF_ext[aarx][symbol*12 + n] = rxdataF[aarx][UL_RB_ID_NB_IoT*12 + frame_parms->first_carrier_offset + symbol*frame_parms->ofdm_symbol_size + n];
Matthieu Kanj's avatar
Matthieu Kanj committed
653
        //// RB 22
654
      }
655

656 657 658 659 660 661 662 663 664 665 666 667 668 669
      // rxF = &rxdataF[aarx][(first_rb*12 + frame_parms->first_carrier_offset + symbol*frame_parms->ofdm_symbol_size)];
      // memcpy(rxF_ext, rxF, nb_rb1*6*sizeof(int));
      // rxF_ext += nb_rb1*6;

      // if (nb_rb2)  {
      //   //#ifdef OFDMA_ULSCH
      //   //  rxF = &rxdataF[aarx][(1 + symbol*frame_parms->ofdm_symbol_size)*2];
      //   //#else
      //   rxF = &rxdataF[aarx][(symbol*frame_parms->ofdm_symbol_size)];
      //   //#endif
      //   memcpy(rxF_ext, rxF, nb_rb2*6*sizeof(int));
      //   rxF_ext += nb_rb2*6;
      // }
    } else { // RB NB-IoT is in the second half 
Matthieu Kanj's avatar
Matthieu Kanj committed
670
              // RB 2
Matthieu Kanj's avatar
Matthieu Kanj committed
671
     
672
      for (n=0;n<12;n++){ // extract whole RB of 12 subcarriers
673
        // Note that FFT splits the RBs 
674
        // rxdataF_ext[aarx][symbol*frame_parms->N_RB_UL*12 + n] = rxdataF[aarx][6*(2*UL_RB_ID_NB_IoT - frame_parms->N_RB_UL) +  ul_sc_start + symbol*frame_parms->ofdm_symbol_size + n]; 
Matthieu Kanj's avatar
Matthieu Kanj committed
675 676 677 678
        rxdataF_ext[aarx][symbol*frame_parms->N_RB_UL*12 + n] = rxdataF[aarx][6*(2*UL_RB_ID_NB_IoT - frame_parms->N_RB_UL) + (symbol)*frame_parms->ofdm_symbol_size + n]; 
        //printf("   rx_22_%d = %d   ",n,rxdataF[aarx][6*(2*UL_RB_ID_NB_IoT - frame_parms->N_RB_UL) + (subframe*14+symbol)*frame_parms->ofdm_symbol_size + n]); 
        //printf("   rx_20_%d = %d   ",n,rxdataF[aarx][6*(2*(UL_RB_ID_NB_IoT-7) - frame_parms->N_RB_UL) + (subframe*14+symbol)*frame_parms->ofdm_symbol_size + n]);
        //rxdataF_ext[aarx][symbol*12 + n] = rxdataF[aarx][6*(2*UL_RB_ID_NB_IoT - frame_parms->N_RB_UL) + symbol*frame_parms->ofdm_symbol_size + n];
679 680 681 682
      }
      //#ifdef OFDMA_ULSCH
      //      rxF = &rxdataF[aarx][(1 + 6*(2*first_rb - frame_parms->N_RB_UL) + symbol*frame_parms->ofdm_symbol_size)*2];
      //#else
683 684 685 686
      // rxF = &rxdataF[aarx][(6*(2*first_rb - frame_parms->N_RB_UL) + symbol*frame_parms->ofdm_symbol_size)]; 
      // //#endif
      // memcpy(rxF_ext, rxF, nb_rb2*6*sizeof(int));
      // rxF_ext += nb_rb2*6;
687 688 689 690 691 692 693 694 695 696 697
    }
  }

}


void ulsch_channel_compensation_NB_IoT(int32_t **rxdataF_ext,
                                int32_t **ul_ch_estimates_ext,
                                int32_t **ul_ch_mag,
                                int32_t **ul_ch_magb,
                                int32_t **rxdataF_comp,
Matthieu Kanj's avatar
Matthieu Kanj committed
698
                                LTE_DL_FRAME_PARMS *frame_parms,
699 700 701 702 703 704
                                uint8_t symbol,
                                uint8_t Qm,
                                uint16_t nb_rb,
                                uint8_t output_shift)
{

705
  // uint16_t rb;
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

#if defined(__x86_64__) || defined(__i386__)

  __m128i *ul_ch128,*ul_ch_mag128,*ul_ch_mag128b,*rxdataF128,*rxdataF_comp128;
  uint8_t aarx;//,symbol_mod;
  __m128i mmtmpU0,mmtmpU1,mmtmpU2,mmtmpU3;
#ifdef OFDMA_ULSCH
  __m128i QAM_amp128U,QAM_amp128bU;
#endif

#elif defined(__arm__)

  int16x4_t *ul_ch128,*rxdataF128;
  int16x8_t *ul_ch_mag128,*ul_ch_mag128b,*rxdataF_comp128;

  uint8_t aarx;//,symbol_mod;
  int32x4_t mmtmpU0,mmtmpU1,mmtmpU0b,mmtmpU1b;
#ifdef OFDMA_ULSCH
  int16x8_t mmtmpU2,mmtmpU3;
  int16x8_t QAM_amp128U,QAM_amp128bU;
#endif
  int16_t conj[4]__attribute__((aligned(16))) = {1,-1,1,-1};
  int32x4_t output_shift128 = vmovq_n_s32(-(int32_t)output_shift);



#endif



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

#if defined(__x86_64__) || defined(__i386__)

    ul_ch128          = (__m128i *)&ul_ch_estimates_ext[aarx][symbol*frame_parms->N_RB_DL*12];
    ul_ch_mag128      = (__m128i *)&ul_ch_mag[aarx][symbol*frame_parms->N_RB_DL*12];
    ul_ch_mag128b     = (__m128i *)&ul_ch_magb[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[aarx][symbol*frame_parms->N_RB_DL*12];

#elif defined(__arm__)


    ul_ch128          = (int16x4_t *)&ul_ch_estimates_ext[aarx][symbol*frame_parms->N_RB_DL*12];
    ul_ch_mag128      = (int16x8_t *)&ul_ch_mag[aarx][symbol*frame_parms->N_RB_DL*12];
    ul_ch_mag128b     = (int16x8_t *)&ul_ch_magb[aarx][symbol*frame_parms->N_RB_DL*12];
    rxdataF128        = (int16x4_t *)&rxdataF_ext[aarx][symbol*frame_parms->N_RB_DL*12];
    rxdataF_comp128   = (int16x8_t *)&rxdataF_comp[aarx][symbol*frame_parms->N_RB_DL*12];

#endif
756 757


758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
#if defined(__x86_64__) || defined(__i386__)
      mmtmpU0 = _mm_madd_epi16(ul_ch128[0],ul_ch128[0]);

      mmtmpU0 = _mm_srai_epi32(mmtmpU0,output_shift);
      mmtmpU1 = _mm_madd_epi16(ul_ch128[1],ul_ch128[1]);

      mmtmpU1 = _mm_srai_epi32(mmtmpU1,output_shift);

      mmtmpU0 = _mm_packs_epi32(mmtmpU0,mmtmpU1);

      ul_ch_mag128[0] = _mm_unpacklo_epi16(mmtmpU0,mmtmpU0);
      ul_ch_mag128[1] = _mm_unpackhi_epi16(mmtmpU0,mmtmpU0);

      mmtmpU0 = _mm_madd_epi16(ul_ch128[2],ul_ch128[2]);

      mmtmpU0 = _mm_srai_epi32(mmtmpU0,output_shift);
      mmtmpU1 = _mm_packs_epi32(mmtmpU0,mmtmpU0);
      ul_ch_mag128[2] = _mm_unpacklo_epi16(mmtmpU1,mmtmpU1);

      // printf("comp: symbol %d rb %d => %d,%d,%d (output_shift %d)\n",symbol,rb,*((int16_t*)&ul_ch_mag128[0]),*((int16_t*)&ul_ch_mag128[1]),*((int16_t*)&ul_ch_mag128[2]),output_shift);


#elif defined(__arm__)
          mmtmpU0 = vmull_s16(ul_ch128[0], ul_ch128[0]);
          mmtmpU0 = vqshlq_s32(vqaddq_s32(mmtmpU0,vrev64q_s32(mmtmpU0)),-output_shift128);
          mmtmpU1 = vmull_s16(ul_ch128[1], ul_ch128[1]);
          mmtmpU1 = vqshlq_s32(vqaddq_s32(mmtmpU1,vrev64q_s32(mmtmpU1)),-output_shift128);
          ul_ch_mag128[0] = vcombine_s16(vmovn_s32(mmtmpU0),vmovn_s32(mmtmpU1));
          mmtmpU0 = vmull_s16(ul_ch128[2], ul_ch128[2]);
          mmtmpU0 = vqshlq_s32(vqaddq_s32(mmtmpU0,vrev64q_s32(mmtmpU0)),-output_shift128);
          mmtmpU1 = vmull_s16(ul_ch128[3], ul_ch128[3]);
          mmtmpU1 = vqshlq_s32(vqaddq_s32(mmtmpU1,vrev64q_s32(mmtmpU1)),-output_shift128);
          ul_ch_mag128[1] = vcombine_s16(vmovn_s32(mmtmpU0),vmovn_s32(mmtmpU1));
          mmtmpU0 = vmull_s16(ul_ch128[4], ul_ch128[4]);
          mmtmpU0 = vqshlq_s32(vqaddq_s32(mmtmpU0,vrev64q_s32(mmtmpU0)),-output_shift128);
          mmtmpU1 = vmull_s16(ul_ch128[5], ul_ch128[5]);
          mmtmpU1 = vqshlq_s32(vqaddq_s32(mmtmpU1,vrev64q_s32(mmtmpU1)),-output_shift128);
          ul_ch_mag128[2] = vcombine_s16(vmovn_s32(mmtmpU0),vmovn_s32(mmtmpU1));

#endif
798
// #endif
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931

#if defined(__x86_64__) || defined(__i386__)
      // multiply by conjugated channel
      mmtmpU0 = _mm_madd_epi16(ul_ch128[0],rxdataF128[0]);
      //        print_ints("re",&mmtmpU0);

      // mmtmpU0 contains real part of 4 consecutive outputs (32-bit)
      mmtmpU1 = _mm_shufflelo_epi16(ul_ch128[0],_MM_SHUFFLE(2,3,0,1));
      mmtmpU1 = _mm_shufflehi_epi16(mmtmpU1,_MM_SHUFFLE(2,3,0,1));
      mmtmpU1 = _mm_sign_epi16(mmtmpU1,*(__m128i*)&conjugate[0]);

      mmtmpU1 = _mm_madd_epi16(mmtmpU1,rxdataF128[0]);
      //      print_ints("im",&mmtmpU1);
      // mmtmpU1 contains imag part of 4 consecutive outputs (32-bit)
      mmtmpU0 = _mm_srai_epi32(mmtmpU0,output_shift);
      //  print_ints("re(shift)",&mmtmpU0);
      mmtmpU1 = _mm_srai_epi32(mmtmpU1,output_shift);
      //  print_ints("im(shift)",&mmtmpU1);
      mmtmpU2 = _mm_unpacklo_epi32(mmtmpU0,mmtmpU1);
      mmtmpU3 = _mm_unpackhi_epi32(mmtmpU0,mmtmpU1);
      //        print_ints("c0",&mmtmpU2);
      //  print_ints("c1",&mmtmpU3);
      rxdataF_comp128[0] = _mm_packs_epi32(mmtmpU2,mmtmpU3);
      /*
              print_shorts("rx:",&rxdataF128[0]);
              print_shorts("ch:",&ul_ch128[0]);
              print_shorts("pack:",&rxdataF_comp128[0]);
      */
      // multiply by conjugated channel
      mmtmpU0 = _mm_madd_epi16(ul_ch128[1],rxdataF128[1]);
      // mmtmpU0 contains real part of 4 consecutive outputs (32-bit)
      mmtmpU1 = _mm_shufflelo_epi16(ul_ch128[1],_MM_SHUFFLE(2,3,0,1));
      mmtmpU1 = _mm_shufflehi_epi16(mmtmpU1,_MM_SHUFFLE(2,3,0,1));
      mmtmpU1 = _mm_sign_epi16(mmtmpU1,*(__m128i*)conjugate);
      mmtmpU1 = _mm_madd_epi16(mmtmpU1,rxdataF128[1]);
      // mmtmpU1 contains imag part of 4 consecutive outputs (32-bit)
      mmtmpU0 = _mm_srai_epi32(mmtmpU0,output_shift);
      mmtmpU1 = _mm_srai_epi32(mmtmpU1,output_shift);
      mmtmpU2 = _mm_unpacklo_epi32(mmtmpU0,mmtmpU1);
      mmtmpU3 = _mm_unpackhi_epi32(mmtmpU0,mmtmpU1);

      rxdataF_comp128[1] = _mm_packs_epi32(mmtmpU2,mmtmpU3);
      //        print_shorts("rx:",rxdataF128[1]);
      //        print_shorts("ch:",ul_ch128[1]);
      //        print_shorts("pack:",rxdataF_comp128[1]);
      //       multiply by conjugated channel
      mmtmpU0 = _mm_madd_epi16(ul_ch128[2],rxdataF128[2]);
      // mmtmpU0 contains real part of 4 consecutive outputs (32-bit)
      mmtmpU1 = _mm_shufflelo_epi16(ul_ch128[2],_MM_SHUFFLE(2,3,0,1));
      mmtmpU1 = _mm_shufflehi_epi16(mmtmpU1,_MM_SHUFFLE(2,3,0,1));
      mmtmpU1 = _mm_sign_epi16(mmtmpU1,*(__m128i*)conjugate);
      mmtmpU1 = _mm_madd_epi16(mmtmpU1,rxdataF128[2]);
      // mmtmpU1 contains imag part of 4 consecutive outputs (32-bit)
      mmtmpU0 = _mm_srai_epi32(mmtmpU0,output_shift);
      mmtmpU1 = _mm_srai_epi32(mmtmpU1,output_shift);
      mmtmpU2 = _mm_unpacklo_epi32(mmtmpU0,mmtmpU1);
      mmtmpU3 = _mm_unpackhi_epi32(mmtmpU0,mmtmpU1);

      rxdataF_comp128[2] = _mm_packs_epi32(mmtmpU2,mmtmpU3);
      //        print_shorts("rx:",rxdataF128[2]);
      //        print_shorts("ch:",ul_ch128[2]);
      //        print_shorts("pack:",rxdataF_comp128[2]);

      // Add a jitter to compensate for the saturation in "packs" resulting in a bias on the DC after IDFT
      rxdataF_comp128[0] = _mm_add_epi16(rxdataF_comp128[0],(*(__m128i*)&jitter[0]));
      rxdataF_comp128[1] = _mm_add_epi16(rxdataF_comp128[1],(*(__m128i*)&jitter[0]));
      rxdataF_comp128[2] = _mm_add_epi16(rxdataF_comp128[2],(*(__m128i*)&jitter[0]));

      ul_ch128+=3;
      ul_ch_mag128+=3;
      ul_ch_mag128b+=3;
      rxdataF128+=3;
      rxdataF_comp128+=3;
#elif defined(__arm__)
        mmtmpU0 = vmull_s16(ul_ch128[0], rxdataF128[0]);
        //mmtmpU0 = [Re(ch[0])Re(rx[0]) Im(ch[0])Im(ch[0]) Re(ch[1])Re(rx[1]) Im(ch[1])Im(ch[1])] 
        mmtmpU1 = vmull_s16(ul_ch128[1], rxdataF128[1]);
        //mmtmpU1 = [Re(ch[2])Re(rx[2]) Im(ch[2])Im(ch[2]) Re(ch[3])Re(rx[3]) Im(ch[3])Im(ch[3])] 
        mmtmpU0 = vcombine_s32(vpadd_s32(vget_low_s32(mmtmpU0),vget_high_s32(mmtmpU0)),
                               vpadd_s32(vget_low_s32(mmtmpU1),vget_high_s32(mmtmpU1)));
        //mmtmpU0 = [Re(ch[0])Re(rx[0])+Im(ch[0])Im(ch[0]) Re(ch[1])Re(rx[1])+Im(ch[1])Im(ch[1]) Re(ch[2])Re(rx[2])+Im(ch[2])Im(ch[2]) Re(ch[3])Re(rx[3])+Im(ch[3])Im(ch[3])] 

        mmtmpU0b = vmull_s16(vrev32_s16(vmul_s16(ul_ch128[0],*(int16x4_t*)conj)), rxdataF128[0]);
        //mmtmpU0 = [-Im(ch[0])Re(rx[0]) Re(ch[0])Im(rx[0]) -Im(ch[1])Re(rx[1]) Re(ch[1])Im(rx[1])]
        mmtmpU1b = vmull_s16(vrev32_s16(vmul_s16(ul_ch128[1],*(int16x4_t*)conj)), rxdataF128[1]);
        //mmtmpU0 = [-Im(ch[2])Re(rx[2]) Re(ch[2])Im(rx[2]) -Im(ch[3])Re(rx[3]) Re(ch[3])Im(rx[3])]
        mmtmpU1 = vcombine_s32(vpadd_s32(vget_low_s32(mmtmpU0b),vget_high_s32(mmtmpU0b)),
                               vpadd_s32(vget_low_s32(mmtmpU1b),vget_high_s32(mmtmpU1b)));
        //mmtmpU1 = [-Im(ch[0])Re(rx[0])+Re(ch[0])Im(rx[0]) -Im(ch[1])Re(rx[1])+Re(ch[1])Im(rx[1]) -Im(ch[2])Re(rx[2])+Re(ch[2])Im(rx[2]) -Im(ch[3])Re(rx[3])+Re(ch[3])Im(rx[3])]

        mmtmpU0 = vqshlq_s32(mmtmpU0,-output_shift128);
        mmtmpU1 = vqshlq_s32(mmtmpU1,-output_shift128);
        rxdataF_comp128[0] = vcombine_s16(vmovn_s32(mmtmpU0),vmovn_s32(mmtmpU1));
        mmtmpU0 = vmull_s16(ul_ch128[2], rxdataF128[2]);
        mmtmpU1 = vmull_s16(ul_ch128[3], rxdataF128[3]);
        mmtmpU0 = vcombine_s32(vpadd_s32(vget_low_s32(mmtmpU0),vget_high_s32(mmtmpU0)),
                               vpadd_s32(vget_low_s32(mmtmpU1),vget_high_s32(mmtmpU1)));
        mmtmpU0b = vmull_s16(vrev32_s16(vmul_s16(ul_ch128[2],*(int16x4_t*)conj)), rxdataF128[2]);
        mmtmpU1b = vmull_s16(vrev32_s16(vmul_s16(ul_ch128[3],*(int16x4_t*)conj)), rxdataF128[3]);
        mmtmpU1 = vcombine_s32(vpadd_s32(vget_low_s32(mmtmpU0b),vget_high_s32(mmtmpU0b)),
                               vpadd_s32(vget_low_s32(mmtmpU1b),vget_high_s32(mmtmpU1b)));
        mmtmpU0 = vqshlq_s32(mmtmpU0,-output_shift128);
        mmtmpU1 = vqshlq_s32(mmtmpU1,-output_shift128);
        rxdataF_comp128[1] = vcombine_s16(vmovn_s32(mmtmpU0),vmovn_s32(mmtmpU1));

        mmtmpU0 = vmull_s16(ul_ch128[4], rxdataF128[4]);
        mmtmpU1 = vmull_s16(ul_ch128[5], rxdataF128[5]);
        mmtmpU0 = vcombine_s32(vpadd_s32(vget_low_s32(mmtmpU0),vget_high_s32(mmtmpU0)),
                               vpadd_s32(vget_low_s32(mmtmpU1),vget_high_s32(mmtmpU1)));

        mmtmpU0b = vmull_s16(vrev32_s16(vmul_s16(ul_ch128[4],*(int16x4_t*)conj)), rxdataF128[4]);
        mmtmpU1b = vmull_s16(vrev32_s16(vmul_s16(ul_ch128[5],*(int16x4_t*)conj)), rxdataF128[5]);
        mmtmpU1 = vcombine_s32(vpadd_s32(vget_low_s32(mmtmpU0b),vget_high_s32(mmtmpU0b)),
                               vpadd_s32(vget_low_s32(mmtmpU1b),vget_high_s32(mmtmpU1b)));

              
        mmtmpU0 = vqshlq_s32(mmtmpU0,-output_shift128);
        mmtmpU1 = vqshlq_s32(mmtmpU1,-output_shift128);
        rxdataF_comp128[2] = vcombine_s16(vmovn_s32(mmtmpU0),vmovn_s32(mmtmpU1));
              
              // Add a jitter to compensate for the saturation in "packs" resulting in a bias on the DC after IDFT
        rxdataF_comp128[0] = vqaddq_s16(rxdataF_comp128[0],(*(int16x8_t*)&jitter[0]));
        rxdataF_comp128[1] = vqaddq_s16(rxdataF_comp128[1],(*(int16x8_t*)&jitter[0]));
        rxdataF_comp128[2] = vqaddq_s16(rxdataF_comp128[2],(*(int16x8_t*)&jitter[0]));

      
        ul_ch128+=6;
        ul_ch_mag128+=3;
        ul_ch_mag128b+=3;
        rxdataF128+=6;
        rxdataF_comp128+=3;
              
#endif
932
    // }
933 934 935 936 937 938 939 940
  }

#if defined(__x86_64__) || defined(__i386__)
  _mm_empty();
  _m_empty();
#endif
}

Matthieu Kanj's avatar
Matthieu Kanj committed
941 942
void fill_rbs_zeros_NB_IoT(PHY_VARS_eNB *eNB, 
                            LTE_DL_FRAME_PARMS *frame_parms,
943 944
                            int32_t **rxdataF_comp,
                            uint16_t ul_sc_start,
945 946 947 948
                            uint8_t UE_id,
                            uint8_t symbol)
{

949
  //uint32_t I_sc = 11;//eNB->ulsch[UE_id]->harq_process->I_sc;  // NB_IoT: subcarrier indication field: must be defined in higher layer
Matthieu Kanj's avatar
Matthieu Kanj committed
950
  uint8_t Nsc_RU = 1;//eNB->ulsch[UE_id]->harq_process->N_sc_RU; // Vincent: number of sc 1,3,6,12 
951
  //uint16_t ul_sc_start; // subcarrier start index into UL RB 
952 953 954
  int32_t *rxdataF_comp32;   
  uint8_t m; // index of subcarrier

955
 // ul_sc_start = get_UL_sc_start_NB_IoT(I_sc); // NB-IoT: get the used subcarrier in RB 
956 957
  rxdataF_comp32   = (int32_t *)&rxdataF_comp[0][symbol*frame_parms->N_RB_DL*12]; 
  if (Nsc_RU != 12){
Matthieu Kanj's avatar
Matthieu Kanj committed
958 959 960 961 962 963 964 965 966 967 968
    for (m=0;m<12;m++)
    { // 12 is the number of subcarriers per RB
        if (m == ul_sc_start)
        {
            m = m + Nsc_RU; // skip non-zeros subcarriers
        }

        if(m<12)
        {
            rxdataF_comp32[m] = 0; 
        }   
969 970 971 972 973
    }  
  }


}
Matthieu Kanj's avatar
Matthieu Kanj committed
974 975


Matthieu Kanj's avatar
Matthieu Kanj committed
976 977 978
void rotate_single_carrier_NB_IoT(PHY_VARS_eNB          *eNB, 
                                  LTE_DL_FRAME_PARMS    *frame_parms,
                                  int32_t               **rxdataF_comp, 
Matthieu Kanj's avatar
Matthieu Kanj committed
979 980 981
                                  uint8_t               eNB_id,                // to be removed ??? since not used
                                  uint8_t               l,                     //symbol within subframe
                                  uint8_t               counter_msg3,          ///  to be replaced by the number of received part
Matthieu Kanj's avatar
Matthieu Kanj committed
982
                                  uint16_t              ul_sc_start,
Matthieu Kanj's avatar
Matthieu Kanj committed
983 984
                                  uint8_t               Qm,
                                  uint8_t               option)  // 0 for data and 1 for ACK
985 986
{

Matthieu Kanj's avatar
Matthieu Kanj committed
987
  //uint32_t I_sc = 11;//eNB->ulsch_NB_IoT[UE_id]->harq_process->I_sc;  // NB_IoT: subcarrier indication field: must be defined in higher layer
Matthieu Kanj's avatar
Matthieu Kanj committed
988
  //uint16_t ul_sc_start; // subcarrier start index into UL RB 
989
  int16_t pi_2_re[2] = {32767 , 0}; 
Matthieu Kanj's avatar
Matthieu Kanj committed
990
  int16_t pi_2_im[2] = {0 , 32767}; 
Matthieu Kanj's avatar
Matthieu Kanj committed
991 992 993 994 995 996
  //int16_t pi_4_re[2] = {32767 , 25735}; 
  //int16_t pi_4_im[2] = {0 , 25736}; 
  int16_t pi_4_re[2] = {32767 , 23170}; 
  int16_t pi_4_im[2] = {0 , 23170}; 
  int16_t e_phi_re[120] = {32767, 24811, 4807, -17531, -31357, -29956, -14010, 0, 21402, 32412, 27683, 9511, -13279, -29622, -32767, -24812, -4808, 17530, 31356, 29955, 14009, 0, -21403, -32413, -27684, -9512, 13278, 29621, 32767, 24811, 4807, -17531, -31357, -29956, -14010, 0, 21402, 32412, 27683, 9511, -13279, -29622, -32767, -24812, -4808, 17530, 31356, 29955, 14009, -1, -21403, -32413, -27684, -9512, 13278, 29621, 32767, 24811, 4807, -17531, -31357, -29956, -14010, 0, 21402, 32412, 27683, 9511, -13279, -29622, -32767, -24812, -4808, 17530, 31356, 29955, 14009, 0, -21403, -32413, -27684, -9512, 13278, 29621, 32767, 24811, 4807, -17531, -31357, -29956, -14010, -1, 21402, 32412, 27683, 9511, -13279, -29622, -32767, -24812, -4808, 17530, 31356, 29955, 14009, 0, -21403, -32413, -27684, -9512, 13278, 29621}; 
  int16_t e_phi_im[120] = {0, -21403, -32413, -27684, -9512, 13278, 29621, 32767, 24811, 4807, -17531, -31357, -29956, -14010, -1, 21402, 32412, 27683, 9511, -13279, -29622, -32767, -24812, -4808, 17530, 31356, 29955, 14009, 0, -21403, -32413, -27684, -9512, 13278, 29621, 32767, 24811, 4807, -17531, -31357, -29956, -14010, 0, 21402, 32412, 27683, 9511, -13279, -29622, -32767, -24812, -4808, 17530, 31356, 29955, 14009, -1, -21403, -32413, -27684, -9512, 13278, 29621, 32767, 24811, 4807, -17531, -31357, -29956, -14010, 0, 21402, 32412, 27683, 9511, -13279, -29622, -32767, -24812, -4808, 17530, 31356, 29955, 14009, 0, -21403, -32413, -27684, -9512, 13278, 29621, 32767, 24811, 4807, -17531, -31357, -29956, -14010, -1, 21402, 32412, 27683, 9511, -13279, -29622, -32767, -24812, -4808, 17530, 31356, 29955, 14009}; 
Matthieu Kanj's avatar
Matthieu Kanj committed
997 998
  int16_t e_phi_re_m6[120] = {32767, 24811, 4807, -17531, -31357, -29956, -14010, 0, 21402, 32412, 27683, 9511, -13279, -29622, -32767, -24812, -4808, 17530, 31356, 29955, 14009, 0, -21403, -32413, -27684, -9512, 13278, 29621, 32767, 24811, 4807, -17531, -31357, -29956, -14010, 0, 21402, 32412, 27683, 9511, -13279, -29622, -32767, -24812, -4808, 17530, 31356, 29955, 14009, -1, -21403, -32413, -27684, -9512, 13278, 29621, 32767, 24811, 4807, -17531, -31357, -29956, -14010, 0, 21402, 32412, 27683, 9511, -13279, -29622, -32767, -24812, -4808, 17530, 31356, 29955, 14009, 0, -21403, -32413, -27684, -9512, 13278, 29621, 32767, 24811, 4807, -17531, -31357, -29956, -14010, -1, 21402, 32412, 27683, 9511, -13279, -29622, -32767, -24812, -4808, 17530, 31356, 29955, 14009, 0, -21403, -32413, -27684, -9512, 13278, 29621};
  int16_t e_phi_im_m6[120] = {0, 21402, 32412, 27683, 9511, -13279, -29622, -32767, -24812, -4808, 17530, 31356, 29955, 14009, 0, -21403, -32413, -27684, -9512, 13278, 29621, 32767, 24811, 4807, -17531, -31357, -29956, -14010, -1, 21402, 32412, 27683, 9511, -13279, -29622, -32767, -24812, -4808, 17530, 31356, 29955, 14009, -1, -21403, -32413, -27684, -9512, 13278, 29621, 32767, 24811, 4807, -17531, -31357, -29956, -14010, 0, 21402, 32412, 27683, 9511, -13279, -29622, -32767, -24812, -4808, 17530, 31356, 29955, 14009, -1, -21403, -32413, -27684, -9512, 13278, 29621, 32767, 24811, 4807, -17531, -31357, -29956, -14010, -1, 21402, 32412, 27683, 9511, -13279, -29622, -32767, -24812, -4808, 17530, 31356, 29955, 14009, 0, -21403, -32413, -27684, -9512, 13278, 29621, 32767, 24811, 4807, -17531, -31357, -29956, -14010};
999
  int16_t *rxdataF_comp16; 
Matthieu Kanj's avatar
Matthieu Kanj committed
1000
  int16_t rxdataF_comp16_re, rxdataF_comp16_im,rxdataF_comp16_re_2,rxdataF_comp16_im_2;    
1001

Matthieu Kanj's avatar
Matthieu Kanj committed
1002
  //ul_sc_start = get_UL_sc_start_NB_IoT(I_sc); // NB-IoT: get the used subcarrier in RB
Matthieu Kanj's avatar
Matthieu Kanj committed
1003
  rxdataF_comp16   = (int16_t *)&rxdataF_comp[eNB_id][l*frame_parms->N_RB_DL*12 + ul_sc_start]; 
1004 1005
  rxdataF_comp16_re = rxdataF_comp16[0]; 
  rxdataF_comp16_im = rxdataF_comp16[1]; 
Matthieu Kanj's avatar
Matthieu Kanj committed
1006 1007
  rxdataF_comp16_re_2 = rxdataF_comp16_re; 
  rxdataF_comp16_im_2 = rxdataF_comp16_re;
1008 1009

  if (Qm == 1){
Matthieu Kanj's avatar
Matthieu Kanj committed
1010 1011 1012 1013
    rxdataF_comp16_re_2 = (int16_t)(((int32_t)pi_2_re[l%2] * (int32_t)rxdataF_comp16_re + 
                        (int32_t)pi_2_im[l%2] * (int32_t)rxdataF_comp16_im)>>15); 
    rxdataF_comp16_im_2 = (int16_t)(((int32_t)pi_2_re[l%2] * (int32_t)rxdataF_comp16_im - 
                        (int32_t)pi_2_im[l%2] * (int32_t)rxdataF_comp16_re)>>15); 
1014 1015
  }
  if(Qm == 2){
Matthieu Kanj's avatar
Matthieu Kanj committed
1016 1017 1018 1019
    rxdataF_comp16_re_2 = (int16_t)(((int32_t)pi_4_re[l%2] * (int32_t)rxdataF_comp16_re + 
                        (int32_t)pi_4_im[l%2] * (int32_t)rxdataF_comp16_im)>>15); 
    rxdataF_comp16_im_2 = (int16_t)(((int32_t)pi_4_re[l%2] * (int32_t)rxdataF_comp16_im - 
                        (int32_t)pi_4_im[l%2] * (int32_t)rxdataF_comp16_re)>>15); 
1020
  }
1021

Matthieu Kanj's avatar
Matthieu Kanj committed
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
  if (option ==0)
  {
        rxdataF_comp16[0] = (int16_t)(((int32_t)e_phi_re[14*(8-counter_msg3) + l] * (int32_t)rxdataF_comp16_re_2 + 
                              (int32_t)e_phi_im[14*(8-counter_msg3) + l] * (int32_t)rxdataF_comp16_im_2)>>15); 
        rxdataF_comp16[1] = (int16_t)(((int32_t)e_phi_re[14*(8-counter_msg3) + l] * (int32_t)rxdataF_comp16_im_2 - 
                              (int32_t)e_phi_im[14*(8-counter_msg3) + l] * (int32_t)rxdataF_comp16_re_2)>>15); 
  } else {
        /*rxdataF_comp16[0] = (int16_t)(((int32_t)e_phi_re[0] * (int32_t)rxdataF_comp16_re_2 + 
                              (int32_t)e_phi_im[0] * (int32_t)rxdataF_comp16_im_2)>>15); 
        rxdataF_comp16[1] = (int16_t)(((int32_t)e_phi_re[0] * (int32_t)rxdataF_comp16_im_2 - 
                              (int32_t)e_phi_im[0] * (int32_t)rxdataF_comp16_re_2)>>15); */
          rxdataF_comp16[0] = (int16_t)(((int32_t)e_phi_re_m6[14*(2-counter_msg3) + l] * (int32_t)rxdataF_comp16_re_2 + 
                              (int32_t)e_phi_im_m6[14*(2-counter_msg3) + l] * (int32_t)rxdataF_comp16_im_2)>>15); 
          rxdataF_comp16[1] = (int16_t)(((int32_t)e_phi_re_m6[14*(2-counter_msg3) + l] * (int32_t)rxdataF_comp16_im_2 - 
                              (int32_t)e_phi_im_m6[14*(2-counter_msg3) + l] * (int32_t)rxdataF_comp16_re_2)>>15); 
  }
Matthieu Kanj's avatar
Matthieu Kanj committed
1038 1039 1040
  /*printf("\n");
  printf("  re_eq_data = %d  im_eq_data = %d   ",rxdataF_comp16[0],rxdataF_comp16[1]);
  printf("\n");*/
1041

1042 1043
}

Matthieu Kanj's avatar
Matthieu Kanj committed
1044

Matthieu Kanj's avatar
Matthieu Kanj committed
1045 1046
void rotate_bpsk_NB_IoT(PHY_VARS_eNB *eNB, 
                        LTE_DL_FRAME_PARMS *frame_parms,
1047
                        int32_t **rxdataF_comp, 
1048
                        uint16_t ul_sc_start,
1049 1050 1051 1052
                        uint8_t UE_id,
                        uint8_t symbol)
{

1053 1054
  //uint32_t I_sc = eNB->ulsch_NB_IoT[UE_id]->harq_process->I_sc;  // NB_IoT: subcarrier indication field: must be defined in higher layer
  //uint16_t ul_sc_start; // subcarrier start index into UL RB 
1055 1056 1057 1058 1059
  int16_t m_pi_4_re = 25735; // cos(pi/4) 
  int16_t m_pi_4_im = 25736; // sin(pi/4) 
  int16_t *rxdataF_comp16; 
  int16_t rxdataF_comp16_re, rxdataF_comp16_im; 

1060
  //ul_sc_start = get_UL_sc_start_NB_IoT(I_sc); // NB-IoT: get the used subcarrier in RB
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
  rxdataF_comp16   = (int16_t *)&rxdataF_comp[0][symbol*frame_parms->N_RB_DL*12 + ul_sc_start]; 
  rxdataF_comp16_re = rxdataF_comp16[0]; 
  rxdataF_comp16_im = rxdataF_comp16[1]; 

  rxdataF_comp16[0] = (int16_t)(((int32_t)m_pi_4_re * (int32_t)rxdataF_comp16_re + 
                        (int32_t)m_pi_4_im * (int32_t)rxdataF_comp16_im)>>15); 
  rxdataF_comp16[1] = (int16_t)(((int32_t)m_pi_4_re * (int32_t)rxdataF_comp16_im -  
                        (int32_t)m_pi_4_im * (int32_t)rxdataF_comp16_re)>>15); 

} 

1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082




#if defined(__x86_64__) || defined(__i386__)
__m128i avg128U;
#elif defined(__arm__)
int32x4_t avg128U;
#endif

void ulsch_channel_level_NB_IoT(int32_t **drs_ch_estimates_ext,
Matthieu Kanj's avatar
Matthieu Kanj committed
1083
                                LTE_DL_FRAME_PARMS *frame_parms,
1084 1085 1086 1087
                                int32_t *avg,
                                uint16_t nb_rb)
{

1088
  // int16_t rb;
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
  uint8_t aarx;
#if defined(__x86_64__) || defined(__i386__)
  __m128i *ul_ch128;
#elif defined(__arm__)
  int16x4_t *ul_ch128;
#endif
  for (aarx=0; aarx<frame_parms->nb_antennas_rx; aarx++) {
    //clear average level
#if defined(__x86_64__) || defined(__i386__)
    avg128U = _mm_setzero_si128();
    ul_ch128=(__m128i *)drs_ch_estimates_ext[aarx];

1101
    // for (rb=0; rb<nb_rb; rb++) {
1102 1103 1104 1105 1106 1107 1108 1109

      avg128U = _mm_add_epi32(avg128U,_mm_madd_epi16(ul_ch128[0],ul_ch128[0]));
      avg128U = _mm_add_epi32(avg128U,_mm_madd_epi16(ul_ch128[1],ul_ch128[1]));
      avg128U = _mm_add_epi32(avg128U,_mm_madd_epi16(ul_ch128[2],ul_ch128[2]));

      ul_ch128+=3;


1110
    // }
1111 1112 1113 1114 1115

#elif defined(__arm__)
    avg128U = vdupq_n_s32(0);
    ul_ch128=(int16x4_t *)drs_ch_estimates_ext[aarx];

1116
    // for (rb=0; rb<nb_rb; rb++) {
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126

       avg128U = vqaddq_s32(avg128U,vmull_s16(ul_ch128[0],ul_ch128[0]));
       avg128U = vqaddq_s32(avg128U,vmull_s16(ul_ch128[1],ul_ch128[1]));
       avg128U = vqaddq_s32(avg128U,vmull_s16(ul_ch128[2],ul_ch128[2]));
       avg128U = vqaddq_s32(avg128U,vmull_s16(ul_ch128[3],ul_ch128[3]));
       avg128U = vqaddq_s32(avg128U,vmull_s16(ul_ch128[4],ul_ch128[4]));
       avg128U = vqaddq_s32(avg128U,vmull_s16(ul_ch128[5],ul_ch128[5]));
       ul_ch128+=6;


1127
    // }
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145

#endif

    DevAssert( nb_rb );
    avg[aarx] = (((int*)&avg128U)[0] +
                 ((int*)&avg128U)[1] +
                 ((int*)&avg128U)[2] +
                 ((int*)&avg128U)[3])/(nb_rb*12);

  }

#if defined(__x86_64__) || defined(__i386__)
  _mm_empty();
  _m_empty();
#endif
}


Matthieu Kanj's avatar
Matthieu Kanj committed
1146 1147 1148
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////// generalization of RX procedures //////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Matthieu Kanj's avatar
Matthieu Kanj committed
1149 1150
int32_t llr_msg5[16]; 
int32_t y_msg5[16];
Matthieu Kanj's avatar
Matthieu Kanj committed
1151

1152
uint8_t rx_ulsch_Gen_NB_IoT(PHY_VARS_eNB            *eNB,
1153 1154 1155 1156 1157 1158
                            eNB_rxtx_proc_t         *proc,
                            uint8_t                 eNB_id,                    // this is the effective sector id
                            uint8_t                 UE_id,
                            uint16_t                UL_RB_ID_NB_IoT,           // 22 , to be included in // to be replaced by NB_IoT_start ??     
                            uint8_t                 rx_subframe,               //  received subframe 
                            uint32_t                rx_frame)                  //  received frame 
Matthieu Kanj's avatar
Matthieu Kanj committed
1159
{
Matthieu Kanj's avatar
Matthieu Kanj committed
1160
      
Matthieu Kanj's avatar
Matthieu Kanj committed
1161 1162 1163 1164
      LTE_eNB_PUSCH       *pusch_vars   =  eNB->pusch_vars[UE_id];
      LTE_eNB_COMMON      *common_vars  =  &eNB->common_vars;
      //NB_IoT_DL_FRAME_PARMS  *frame_parms  =  &eNB->frame_parms;
      LTE_DL_FRAME_PARMS     *fp  =  &eNB->frame_parms; 
1165
      // NB_IoT_eNB_NULSCH_t    **ulsch_NB_IoT   =  &eNB->ulsch_NB_IoT[0];//[0][0]; 
Matthieu Kanj's avatar
Matthieu Kanj committed
1166 1167
      NB_IoT_eNB_NULSCH_t     *ulsch_NB_IoT     = eNB->ulsch_NB_IoT[0];
      NB_IoT_UL_eNB_HARQ_t    *ulsch_harq       = ulsch_NB_IoT->harq_process;
1168 1169 1170

  if (ulsch_NB_IoT->Msg3_active  == 1)    
  {    
Matthieu Kanj's avatar
Matthieu Kanj committed
1171 1172 1173 1174
      uint8_t      npusch_format         = ulsch_NB_IoT->npusch_format;           /// 0 or 1 -> format 1 or format 2         
      uint8_t      subcarrier_spacing    = ulsch_harq->subcarrier_spacing;        // can be set as fix value //values are OK // 0 (3.75 KHz) or 1 (15 KHz)
      uint16_t     I_sc                  = ulsch_harq->subcarrier_indication;     // Isc =0->18 , or 0->47 // format 2, 0->3 or 0->7
      uint16_t     I_mcs                 = ulsch_harq->mcs;                       // values 0->10
1175
      uint16_t     Nsc_RU                = get_UL_N_ru_NB_IoT(I_mcs,ulsch_harq->resource_assignment,ulsch_NB_IoT->Msg3_flag);
Matthieu Kanj's avatar
Matthieu Kanj committed
1176
      uint16_t     N_UL_slots            = get_UL_slots_per_RU_NB_IoT(subcarrier_spacing,I_sc,npusch_format)*Nsc_RU;           // N_UL_slots per word
1177
      uint16_t     N_SF_per_word         = N_UL_slots/2;
Matthieu Kanj's avatar
Matthieu Kanj committed
1178

1179 1180 1181 1182
      if(ulsch_NB_IoT->flag_vars == 1)
      {
        ulsch_NB_IoT->counter_sf          = N_SF_per_word;
        ulsch_NB_IoT->counter_repetitions = get_UL_N_rep_NB_IoT(ulsch_harq->repetition_number);
Matthieu Kanj's avatar
Matthieu Kanj committed
1183

1184 1185 1186 1187 1188 1189 1190 1191 1192
        ulsch_NB_IoT->flag_vars = 0;
      }
      
      if(ulsch_NB_IoT->counter_sf == N_SF_per_word)                // initialization for scrambling
      {
          ulsch_NB_IoT->Msg3_subframe   =   rx_subframe;      // first received subframe 
          ulsch_NB_IoT->Msg3_frame      =   rx_frame;         // first received frame
      }

Matthieu Kanj's avatar
Matthieu Kanj committed
1193
       uint8_t     pilot_pos1, pilot_pos2;                                      // holds for npusch format 1, and 15 kHz subcarrier bandwidth
1194 1195
      int16_t      *llrp, *llrp2;
      uint32_t     l,ii=0;
Matthieu Kanj's avatar
Matthieu Kanj committed
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
      uint32_t     rnti_tmp                   = ulsch_NB_IoT->rnti;                   
      uint16_t     ul_sc_start                = get_UL_sc_index_start_NB_IoT(subcarrier_spacing,I_sc,npusch_format);
      uint8_t      Qm                         = get_Qm_UL_NB_IoT(I_mcs,Nsc_RU,I_sc,ulsch_NB_IoT->Msg3_flag);
      uint8_t      pilot_pos1_format1_15k     = 3, pilot_pos2_format1_15k = 10;      // holds for npusch format 1, and 15 kHz subcarrier bandwidth
      uint8_t      pilot_pos1_format2_15k     = 2, pilot_pos2_format2_15k = 9;       // holds for npusch format 2, and 15 kHz subcarrier bandwidth 
      uint8_t      pilot_pos1_format1_3_75k   = 4, pilot_pos2_format1_3_75k = 11;    // holds for npusch format 1, and 3.75 kHz subcarrier bandwidth
      uint8_t      pilot_pos1_format2_3_75k   = 0,pilot_pos2_format2_3_75k = 7;      // holds for npusch format 2, and 3.75 kHz subcarrier bandwidth 
      uint8_t      pilots_slot                =0;        

      //void get_pilots_position(uint8_t npusch_format,uint8_t  subcarrier_spacing,pilot_pos1,pilot_pos2)
Matthieu Kanj's avatar
Matthieu Kanj committed
1206 1207
      switch(npusch_format + subcarrier_spacing*2)
      {
Matthieu Kanj's avatar
Matthieu Kanj committed
1208
        case 0:    // data
Matthieu Kanj's avatar
Matthieu Kanj committed
1209 1210 1211 1212 1213
                pilot_pos1 = pilot_pos1_format1_3_75k; 
                pilot_pos2 = pilot_pos2_format1_3_75k;
                pilots_slot=1;
        break;

Matthieu Kanj's avatar
Matthieu Kanj committed
1214
        case 1:    // ACK
Matthieu Kanj's avatar
Matthieu Kanj committed
1215 1216 1217 1218 1219
                pilot_pos1 = pilot_pos1_format2_3_75k; 
                pilot_pos2 = pilot_pos2_format2_3_75k;
                pilots_slot=3;
        break;

Matthieu Kanj's avatar
Matthieu Kanj committed
1220
        case 2:   // data 
Matthieu Kanj's avatar
Matthieu Kanj committed
1221 1222 1223 1224 1225
                pilot_pos1 = pilot_pos1_format1_15k; 
                pilot_pos2 = pilot_pos2_format1_15k;
                pilots_slot=1;
        break;

Matthieu Kanj's avatar
Matthieu Kanj committed
1226
        case 3:  // ACK
Matthieu Kanj's avatar
Matthieu Kanj committed
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
                pilot_pos1 = pilot_pos1_format2_15k; 
                pilot_pos2 = pilot_pos2_format2_15k;
                pilots_slot=3;
        break;

        default:
         printf("Error in rx_ulsch_NB_IoT");
        break;
      }

Matthieu Kanj's avatar
Matthieu Kanj committed
1237 1238
      for (l=0; l<fp->symbols_per_tti; l++)
      { 
Matthieu Kanj's avatar
Matthieu Kanj committed
1239 1240 1241 1242 1243 1244 1245 1246
            
             ulsch_extract_rbs_single_NB_IoT(common_vars->rxdataF[eNB_id],
                                             pusch_vars->rxdataF_ext[eNB_id],
                                             UL_RB_ID_NB_IoT,      //ulsch[UE_id]->harq_process->UL_RB_ID_NB_IoT, // index of UL NB_IoT resource block 
                                             Nsc_RU, //1, //ulsch_NB_IoT[0]->harq_process->N_sc_RU, // number of subcarriers in UL  //////////////// high level parameter
                                             l%(fp->symbols_per_tti/2),           // (0..13)
                                             l/(fp->symbols_per_tti/2),           // (0,1)
                                             fp);
Matthieu Kanj's avatar
Matthieu Kanj committed
1247
            if(npusch_format == 0)
Matthieu Kanj's avatar
Matthieu Kanj committed
1248 1249 1250 1251 1252
            {
                      ul_chest_tmp_NB_IoT(pusch_vars->rxdataF_ext[eNB_id],
                                          pusch_vars->drs_ch_estimates[eNB_id],
                                          l%(fp->symbols_per_tti/2),          //symbol within slot 
                                          l/(fp->symbols_per_tti/2),
1253
                                          ulsch_NB_IoT->counter_sf,  // counter_msg
Matthieu Kanj's avatar
Matthieu Kanj committed
1254 1255
                                          pilot_pos1,
                                          pilot_pos2,
Matthieu Kanj's avatar
Matthieu Kanj committed
1256
                                          ul_sc_start,
Matthieu Kanj's avatar
Matthieu Kanj committed
1257 1258 1259 1260 1261 1262
                                          Qm,
                                          fp); 
            } else {
                    /// Channel Estimation (NPUSCH format 2)
                      ul_chest_tmp_f2_NB_IoT(pusch_vars->rxdataF_ext[0],
                                            pusch_vars->drs_ch_estimates[0],
1263
                                            l%(fp->symbols_per_tti/2),        //symbol within slot 
Matthieu Kanj's avatar
Matthieu Kanj committed
1264
                                            l/(fp->symbols_per_tti/2), 
1265
                                            ulsch_NB_IoT->counter_sf,         //counter_msg, 
Matthieu Kanj's avatar
Matthieu Kanj committed
1266
                                            npusch_format,                    // proc->flag_msg5, 
1267 1268 1269
                                            rx_subframe,
                                            Qm,                               // =1
                                            ul_sc_start,                       // = 0   
Matthieu Kanj's avatar
Matthieu Kanj committed
1270 1271 1272
                                            fp); 
            }
      }
Matthieu Kanj's avatar
Matthieu Kanj committed
1273
      /////////////////////////////////////// Equalization ///////////////////////////////////
Matthieu Kanj's avatar
Matthieu Kanj committed
1274 1275
      for (l=0; l<fp->symbols_per_tti; l++)
      { 
Matthieu Kanj's avatar
Matthieu Kanj committed
1276 1277 1278
              ul_chequal_tmp_NB_IoT(pusch_vars->rxdataF_ext[eNB_id],
                                    pusch_vars->rxdataF_comp[eNB_id],
                                    pusch_vars->drs_ch_estimates[eNB_id],
Matthieu Kanj's avatar
Matthieu Kanj committed
1279
                                    l%(fp->symbols_per_tti/2),               //symbol within slot 
Matthieu Kanj's avatar
Matthieu Kanj committed
1280 1281
                                    l/(fp->symbols_per_tti/2),
                                    fp);
Matthieu Kanj's avatar
Matthieu Kanj committed
1282 1283
      }
      ////////////////////////////////////// End Equalization /////////////////////////////////
Matthieu Kanj's avatar
Matthieu Kanj committed
1284

Matthieu Kanj's avatar
Matthieu Kanj committed
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
      ///////////////////////////////////////// Rotation       ////////////////////////////////
      for (l=0; l<fp->symbols_per_tti; l++)
      { 
           /// In case of 1 subcarrier: BPSK and QPSK should be rotated by pi/2 and pi/4, respectively 
            rotate_single_carrier_NB_IoT(eNB, 
                                         fp, 
                                         pusch_vars->rxdataF_comp[eNB_id], 
                                         UE_id, // UE ID
                                         l, 
                                         ulsch_NB_IoT->counter_sf,   //counter_msg,
                                         ul_sc_start,
                                         Qm,
                                         npusch_format); // or data
      }
      //////////////////////////////////////////End rotation ///////////////////////////////////
Matthieu Kanj's avatar
Matthieu Kanj committed
1300

Matthieu Kanj's avatar
Matthieu Kanj committed
1301 1302 1303
      if(npusch_format == 0)
      { 
             llrp = (int16_t*)&pusch_vars->llr[0+ (N_SF_per_word-ulsch_NB_IoT->counter_sf)*24];  /// 24= 12 symbols/SF * 2 // since Real and im
Matthieu Kanj's avatar
Matthieu Kanj committed
1304
      } else {
Matthieu Kanj's avatar
Matthieu Kanj committed
1305
              llrp = (int16_t*)&pusch_vars->llr[0+ (2-ulsch_NB_IoT->counter_sf)*16]; // 16 = 8 symbols/SF * 2 // since real and im
Matthieu Kanj's avatar
Matthieu Kanj committed
1306 1307 1308 1309
      }

      for (l=0; l<fp->symbols_per_tti; l++)
      {
Matthieu Kanj's avatar
Matthieu Kanj committed
1310 1311
          if (l==pilot_pos1 || l==pilot_pos2) // skip pilots  // option 0 pilots = x,y,  for option 1 pilots = 2,9 (subcarrier_spacing=1, npush_format=1)
          { 
Matthieu Kanj's avatar
Matthieu Kanj committed
1312
              if(npusch_format == 0)
Matthieu Kanj's avatar
Matthieu Kanj committed
1313 1314 1315 1316 1317
              {
                  l++;
              } else {
                  l=l+3;
              }
Matthieu Kanj's avatar
Matthieu Kanj committed
1318 1319 1320 1321 1322 1323 1324 1325
          }
 
          ulsch_qpsk_llr_NB_IoT(eNB, 
                                fp,
                                pusch_vars->rxdataF_comp[eNB_id],
                                pusch_vars->llr,
                                l, 
                                UE_id, // UE ID
1326
                                ul_sc_start,
Matthieu Kanj's avatar
Matthieu Kanj committed
1327
                                Nsc_RU,
Matthieu Kanj's avatar
Matthieu Kanj committed
1328
                                &llrp[ii*2]); 
Matthieu Kanj's avatar
Matthieu Kanj committed
1329 1330
          ii++;
      }
Matthieu Kanj's avatar
Matthieu Kanj committed
1331
      
Matthieu Kanj's avatar
Matthieu Kanj committed
1332
      /////////////////////////////////////////////////  NPUSH DECOD //////////////////////////////////////
1333
      if(ulsch_NB_IoT->counter_sf == 1)
Matthieu Kanj's avatar
Matthieu Kanj committed
1334
      {   
1335
          int16_t         *ulsch_llr    = eNB->pusch_vars[eNB_id]->llr;             //UE_id=0
Matthieu Kanj's avatar
Matthieu Kanj committed
1336
          
1337 1338
          unsigned int    A      = ulsch_harq->TBS;
          uint8_t         rvdx   = ulsch_harq->rvidx;
Matthieu Kanj's avatar
Matthieu Kanj committed
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
          unsigned int    j,j2; //i2,
          int             iprime;
          int             r,Kr;
          unsigned int    sumKr=0;
          unsigned int    G,H,Hprime,Hpp,Cmux,Rmux_prime;     // Q_CQI,Q_RI=0
          uint32_t        x1, x2, s=0;
          int16_t         y[6*14*1200] __attribute__((aligned(32)));
          uint8_t         ytag[14*1200];
          //int16_t         cseq[6*14*1200];
          uint8_t      reset;
Matthieu Kanj's avatar
Matthieu Kanj committed
1349 1350 1351
          uint8_t      counter_ack;       // ack counter for decision ack/nack
          int32_t      counter_ack_soft;
          
Matthieu Kanj's avatar
Matthieu Kanj committed
1352
          if (npusch_format == 0)
Matthieu Kanj's avatar
Matthieu Kanj committed
1353
          {
Matthieu Kanj's avatar
Matthieu Kanj committed
1354 1355 1356 1357 1358 1359
              // NB-IoT ///////////////////////////////////////////////
              // x1 is set in lte_gold_generic
              // x2 should not reinitialized each subframe
              // x2 should be reinitialized according to 36.211 Sections 10.1.3.1 and 10.1.3.6
              // A     =  ulsch_harq->TBS; //88; //  // only for msg3 , should be replace by generic one
              // Qm   =  get_Qm_ul_NB_IoT(I_MCS,Nsc_RU);   // (2,1)      ///// ulsch_harq->mcs,ulsch_harq->N_sc_RU   // G_UL ??
1360
              G     =  (7-pilots_slot) * Qm * N_UL_slots; //(1 * Q_m) * 6 * 16; // Vincent : see 36.212, Section 5.1.4.1.2  // 16 slot(total number of slots) * 6 symboles (7-pilots_slot) * Qm*1 
Matthieu Kanj's avatar
Matthieu Kanj committed
1361 1362
              //G = ulsch_harq->N_sc_RU * Q_m) * ulsch_harq->Nsymb_UL * ulsch_harq->Nslot_UL;   (= number of RE * 2 - pilots)
              if (ulsch_harq->round == 0)
Matthieu Kanj's avatar
Matthieu Kanj committed
1363
              {
Matthieu Kanj's avatar
Matthieu Kanj committed
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
                  // This is a new packet, so compute quantities regarding segmentation
                  ulsch_harq->B = A+24;
                  lte_segmentation_NB_IoT(NULL,
                                          NULL,
                                          ulsch_harq->B,
                                          &ulsch_harq->C,
                                          &ulsch_harq->Cplus,
                                          &ulsch_harq->Cminus,
                                          &ulsch_harq->Kplus,
                                          &ulsch_harq->Kminus,
                                          &ulsch_harq->F);
Matthieu Kanj's avatar
Matthieu Kanj committed
1375 1376
              }

Matthieu Kanj's avatar
Matthieu Kanj committed
1377 1378 1379 1380 1381
              sumKr = 0;

              for (r=0; r<ulsch_harq->C; r++)
              {
                  if (r<ulsch_harq->Cminus)
Matthieu Kanj's avatar
Matthieu Kanj committed
1382
                  {
Matthieu Kanj's avatar
Matthieu Kanj committed
1383 1384
                      Kr = ulsch_harq->Kminus;
                  }else{
Matthieu Kanj's avatar
Matthieu Kanj committed
1385

Matthieu Kanj's avatar
Matthieu Kanj committed
1386
                      Kr = ulsch_harq->Kplus;
Matthieu Kanj's avatar
Matthieu Kanj committed
1387
                  }
Matthieu Kanj's avatar
Matthieu Kanj committed
1388 1389
                  sumKr += Kr;
              }
Matthieu Kanj's avatar
Matthieu Kanj committed
1390

Matthieu Kanj's avatar
Matthieu Kanj committed
1391 1392 1393 1394 1395 1396 1397
              ulsch_harq->G = G;
              H      = G ;
              Hprime = H/Qm;
              // Demultiplexing/Deinterleaving of PUSCH/ACK/RI/CQI
              Hpp        = Hprime;
              // Cmux       = (ulsch_harq->Nsymb_UL-1)*ulsch_harq->Nslot_UL; // see definition in 36.212, Section 6.3.2, but not consistent with definition
              // of RU in 36.211, Section 10.1.2.3. Maybe prefer the following: 
1398
              Cmux       =  (7-pilots_slot) * N_UL_slots; // 6*16; //////////////(ulsch_harq->Nsymb_UL)*ulsch_harq->Nslot_UL;  // * N_RU **********
Matthieu Kanj's avatar
Matthieu Kanj committed
1399 1400 1401 1402 1403
              Rmux_prime = Hpp/Cmux;
              // Clear "tag" interleaving matrix to allow for CQI/DATA identification
              memset(ytag,0,Cmux*Rmux_prime);
              memset(y,LTE_NULL_NB_IoT,Qm*Hpp);

1404
              x2 =  (rnti_tmp<<14) + (ulsch_NB_IoT->Msg3_subframe<<9) + ((ulsch_NB_IoT->Msg3_frame%2)<<13) + fp->Nid_cell; //this is c_init in 36.211 Sec 10.1.3.1
Matthieu Kanj's avatar
Matthieu Kanj committed
1405 1406 1407

              reset = 1; 
              switch (Qm)
Matthieu Kanj's avatar
Matthieu Kanj committed
1408
              {
Matthieu Kanj's avatar
Matthieu Kanj committed
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
                  case 1: 
                      for (j=0; j<Cmux; j++)
                      { 
                          //y[j] = cseq[j]*ulsch_llr[j]; /// To be defined for bpsk
                      }
                  break; 
                  case 2:
                      for (j=0; j<Cmux*2; j++)
                      {

                            if (j%32==0) 
                            {
                                s = lte_gold_generic(&x1, &x2, reset);
                                //      printf("lte_gold[%d]=%x\n",i,s);
                                reset = 0;
                            }
                            if (((s>>(j%32))&1)==0)
                            {
                                y[j] = -ulsch_llr[j];
                            } else {

                                y[j] = ulsch_llr[j];
                            }
                      }
                  break;
Matthieu Kanj's avatar
Matthieu Kanj committed
1434
              }
1435 1436
                    ///////////////////////////////// desin  multi-tone
                              //if multi-RU
Matthieu Kanj's avatar
Matthieu Kanj committed
1437
/// deinterleaving
Matthieu Kanj's avatar
Matthieu Kanj committed
1438

Matthieu Kanj's avatar
Matthieu Kanj committed
1439 1440 1441 1442
              j  = 0;
              j2 = 0;
              int16_t *yp,*ep;
              for (iprime=0,yp=&y[j2],ep=&ulsch_harq->e[0]; iprime<G; iprime+=8,j2+=8,ep+=8,yp+=8)
Matthieu Kanj's avatar
Matthieu Kanj committed
1443
              {
Matthieu Kanj's avatar
Matthieu Kanj committed
1444 1445 1446 1447 1448 1449 1450 1451 1452
                   ep[0] = yp[0];
                   ep[1] = yp[1];
                   ep[2] = yp[2];
                   ep[3] = yp[3];
                   ep[4] = yp[4];
                   ep[5] = yp[5];
                   ep[6] = yp[6];
                   ep[7] = yp[7];
              }
Matthieu Kanj's avatar
Matthieu Kanj committed
1453 1454
/////
              /// decoding
Matthieu Kanj's avatar
Matthieu Kanj committed
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
              r=0;
              Kr=0;
              unsigned int r_offset=0,Kr_bytes,iind;
              uint8_t crc_type;
              int offset = 0;
              int16_t dummy_w[MAX_NUM_ULSCH_SEGMENTS_NB_IoT][3*(6144+64)];
              //NB_IoT_UL_eNB_HARQ_t *ulsch_harq = ulsch_NB_IoT[0]->harq_process;  ///
              int ret = 1;
              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 *);

              tc = phy_threegpplte_turbo_decoder16;

              for (r=0; r<ulsch_harq->C; r++)
              {
                  // Get Turbo interleaver parameters
                  if (r<ulsch_harq->Cminus)
                  {
                      Kr = ulsch_harq->Kminus;
                  } else{
                      Kr = ulsch_harq->Kplus;
                  }
Matthieu Kanj's avatar
Matthieu Kanj committed
1492

Matthieu Kanj's avatar
Matthieu Kanj committed
1493
                  Kr_bytes = Kr>>3;
Matthieu Kanj's avatar
Matthieu Kanj committed
1494

Matthieu Kanj's avatar
Matthieu Kanj committed
1495 1496 1497
                  if (Kr_bytes<=64)
                  {
                      iind = (Kr_bytes-5);
Matthieu Kanj's avatar
Matthieu Kanj committed
1498

Matthieu Kanj's avatar
Matthieu Kanj committed
1499 1500 1501
                  } else if (Kr_bytes <=128) { 
                      
                      iind = 59 + ((Kr_bytes-64)>>1);
Matthieu Kanj's avatar
Matthieu Kanj committed
1502

Matthieu Kanj's avatar
Matthieu Kanj committed
1503 1504 1505
                  } else if (Kr_bytes <= 256) {
                      
                      iind = 91 + ((Kr_bytes-128)>>2);
Matthieu Kanj's avatar
Matthieu Kanj committed
1506

Matthieu Kanj's avatar
Matthieu Kanj committed
1507 1508 1509
                  } else if (Kr_bytes <= 768) { 
                      
                      iind = 123 + ((Kr_bytes-256)>>3);
Matthieu Kanj's avatar
Matthieu Kanj committed
1510 1511

                  } else {
Matthieu Kanj's avatar
Matthieu Kanj committed
1512 1513
                      LOG_E(PHY,"ulsch_decoding: Illegal codeword size %d!!!\n",Kr_bytes);
                      //return(-1);
Matthieu Kanj's avatar
Matthieu Kanj committed
1514
                  }
Matthieu Kanj's avatar
Matthieu Kanj committed
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529

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

                  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,
                                                 1, //////////////////////////////// not used
                                                 0,   //Uplink
                                                 1,
1530
                                                 rvdx,//ulsch_harq->rvidx,
Matthieu Kanj's avatar
Matthieu Kanj committed
1531 1532 1533 1534 1535
                                                 (ulsch_harq->round==0)?1:0,  // clear
                                                 Qm, //2 //get_Qm_ul(ulsch_harq->mcs),
                                                 1,
                                                 r,
                                                 &E)==-1) 
Matthieu Kanj's avatar
Matthieu Kanj committed
1536
                  {
Matthieu Kanj's avatar
Matthieu Kanj committed
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557
                      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;
                  }
                  // turbo decoding and CRC 
                  ret = tc(&ulsch_harq->d[r][96],
                           ulsch_harq->c[r],
                           Kr,
                           f1f2mat_old[iind*2],
                           f1f2mat_old[(iind*2)+1],
Matthieu Kanj's avatar
Matthieu Kanj committed
1558
                           ulsch_NB_IoT->max_turbo_iterations, //MAX_TURBO_ITERATIONS,
Matthieu Kanj's avatar
Matthieu Kanj committed
1559 1560 1561 1562 1563 1564 1565 1566 1567
                           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); 
Matthieu Kanj's avatar
Matthieu Kanj committed
1568
              ///////////////////end decoding //////////////////////////////////////////////
Matthieu Kanj's avatar
Matthieu Kanj committed
1569
                  if (ret != (1+ulsch_NB_IoT->max_turbo_iterations)) 
Matthieu Kanj's avatar
Matthieu Kanj committed
1570
                  {   
Matthieu Kanj's avatar
Matthieu Kanj committed
1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
                      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));
1589 1590
                      }
                      
1591
                      fill_crc_indication_NB_IoT(eNB,0,rx_frame,rx_subframe,0); // indicate ACK to MAC
1592
                      //fill_rx_indication_NB_IoT(eNB,i,frame,subframe);  // indicate SDU to MAC
Matthieu Kanj's avatar
Matthieu Kanj committed
1593
                      fill_rx_indication_NB_IoT(eNB,proc,npusch_format,ulsch_NB_IoT->Msg3_flag);
Matthieu Kanj's avatar
Matthieu Kanj committed
1594

Matthieu Kanj's avatar
Matthieu Kanj committed
1595
                  } else {
1596
                      
1597
                      fill_crc_indication_NB_IoT(eNB,0,rx_frame,rx_subframe,1);   // indicate NAK to MAC
1598
                      //fill_rx_indication_NB_IoT(eNB,i,frame,subframe);      // indicate SDU to MAC 
Matthieu Kanj's avatar
Matthieu Kanj committed
1599
                      fill_rx_indication_NB_IoT(eNB,proc,npusch_format,ulsch_NB_IoT->Msg3_flag);
1600
                      break;
Matthieu Kanj's avatar
Matthieu Kanj committed
1601
                  }
Matthieu Kanj's avatar
Matthieu Kanj committed
1602
              }////////////  r loop end  ////////////
Matthieu Kanj's avatar
Matthieu Kanj committed
1603

Matthieu Kanj's avatar
Matthieu Kanj committed
1604
         //     uint8_t *msg3 = &eNB->msg3_pdu[0];
Matthieu Kanj's avatar
Matthieu Kanj committed
1605 1606
             // printf("pdu[0] = %d \n",ulsch_harq->b[0]);
              
Matthieu Kanj's avatar
Matthieu Kanj committed
1607

Matthieu Kanj's avatar
Matthieu Kanj committed
1608
           /*   int m =0;
Matthieu Kanj's avatar
Matthieu Kanj committed
1609 1610
              for(m=0; m<6;m++)
              { 
Matthieu Kanj's avatar
Matthieu Kanj committed
1611

Matthieu Kanj's avatar
Matthieu Kanj committed
1612
                    msg3[m]=ulsch_harq->b[2+m];
Matthieu Kanj's avatar
Matthieu Kanj committed
1613
              }*/
Matthieu Kanj's avatar
Matthieu Kanj committed
1614

Matthieu Kanj's avatar
Matthieu Kanj committed
1615
          } else {   //////////////////////////////////// ACK //////////////////////////////
Matthieu Kanj's avatar
Matthieu Kanj committed
1616

Matthieu Kanj's avatar
Matthieu Kanj committed
1617 1618 1619 1620 1621 1622
                llrp2 = (int16_t*)&pusch_vars->llr[0]; 
                for (l=0;l<16;l++) /// Add real and imaginary parts of BPSK constellation
                {
                    llr_msg5[l] = llrp2[l<<1] + llrp2[(l<<1)+1];
                }
 
1623
                x2     =  (rnti_tmp<<14) + (ulsch_NB_IoT->Msg3_subframe<<9) + ((ulsch_NB_IoT->Msg3_frame%2)<<13) + fp->Nid_cell; 
Matthieu Kanj's avatar
Matthieu Kanj committed
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
                reset = 1;  
                s = lte_gold_generic(&x1, &x2, reset);
                reset = 0;
                counter_ack = 0; 
                for (l=0;l<16;l++)
                {
                    //printf("\n s = %d \n",(s_msg5>>(l%32))&1);
                    if (((s>>(l%32))&1)==1) //xor
                    {
                            y_msg5[l] = -llr_msg5[l];
                    } else {
                            y_msg5[l] = llr_msg5[l];
                    }
                    counter_ack += (y_msg5[l]>>31)&1; 
                }
                    /// Decision ACK/NACK
                printf("\n\n\n");
                if (counter_ack>8) //hard decision
                {
                      printf("  decoded msg5: ACK  ");
1644
                      fill_crc_indication_NB_IoT(eNB,0,rx_frame,rx_subframe,0); // indicate ACK to MAC
1645
                      //fill_rx_indication_NB_IoT(eNB,i,frame,subframe);  // indicate SDU to MAC
Matthieu Kanj's avatar
Matthieu Kanj committed
1646
                      fill_rx_indication_NB_IoT(eNB,proc,npusch_format,ulsch_NB_IoT->Msg3_flag);
Matthieu Kanj's avatar
Matthieu Kanj committed
1647 1648 1649 1650

                } else if (counter_ack<8) { //hard decision

                      printf("  decoded msg5: NACK  "); 
1651
                      fill_crc_indication_NB_IoT(eNB,0,rx_frame,rx_subframe,1);   // indicate NAK to MAC
1652
                      //fill_rx_indication_NB_IoT(eNB,i,frame,subframe);      // indicate SDU to MAC 
Matthieu Kanj's avatar
Matthieu Kanj committed
1653
                      fill_rx_indication_NB_IoT(eNB,proc,npusch_format,ulsch_NB_IoT->Msg3_flag);
Matthieu Kanj's avatar
Matthieu Kanj committed
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666

                } else  {  //when equality (8 bits 0 vs 8 bits 1), soft decision
               
                      counter_ack_soft=0;

                      for (l=0;l<16;l++)
                      {
                           counter_ack_soft += y_msg5[l];   
                      }
                      if (counter_ack_soft>=0) 
                      {

                        printf("  decoded msg5 (soft): ACK  ");
1667
                        fill_crc_indication_NB_IoT(eNB,0,rx_frame,rx_subframe,0); // indicate ACK to MAC
1668
                        //fill_rx_indication_NB_IoT(eNB,i,frame,subframe);  // indicate SDU to MAC
Matthieu Kanj's avatar
Matthieu Kanj committed
1669
                        fill_rx_indication_NB_IoT(eNB,proc,npusch_format,ulsch_NB_IoT->Msg3_flag);
Matthieu Kanj's avatar
Matthieu Kanj committed
1670 1671 1672

                      } else {

1673
                        printf("  decoded msg5 (soft): NACK "); 
1674
                        fill_crc_indication_NB_IoT(eNB,0,rx_frame,rx_subframe,1);   // indicate NAK to MAC
1675
                        //fill_rx_indication_NB_IoT(eNB,i,frame,subframe);      // indicate SDU to MAC 
Matthieu Kanj's avatar
Matthieu Kanj committed
1676
                        fill_rx_indication_NB_IoT(eNB,proc,npusch_format,ulsch_NB_IoT->Msg3_flag); 
Matthieu Kanj's avatar
Matthieu Kanj committed
1677 1678 1679 1680
                      }
                }
                printf("\n\n\n");
          } 
Matthieu Kanj's avatar
Matthieu Kanj committed
1681

1682 1683 1684 1685 1686
          /////  if last sf of the word
          ulsch_NB_IoT->counter_repetitions--;

          if(ulsch_NB_IoT->Msg3_flag == 1)
          {
Matthieu Kanj's avatar
Matthieu Kanj committed
1687 1688
              ulsch_harq->rvidx =  (ulsch_NB_IoT->counter_repetitions % 2)*2;        // rvidx toogle for new code word
          } // else {}       for other npusch cases ??
1689 1690 1691 1692 1693 1694 1695

          if( (ulsch_NB_IoT->counter_sf == 1) && (ulsch_NB_IoT->counter_repetitions == 0) )
          {
            ulsch_NB_IoT->Msg3_active  = 0;
            ulsch_NB_IoT->Msg3_flag    = 0;
          }  
          
Matthieu Kanj's avatar
Matthieu Kanj committed
1696 1697
      } // NPUSH decode end

1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
      /// update conter sf after every call
      ulsch_NB_IoT->counter_sf--;

      if( (ulsch_NB_IoT->counter_sf == 0) && (ulsch_NB_IoT->counter_repetitions > 0) )
      {
          ulsch_NB_IoT->counter_sf          = N_SF_per_word;
      }
      /////////////////////////////////////////END/////////////////////////////////////////////////////////////////////////////////////
  } else {
    return 0;     // create void function for NPUSCH ?
  }
        
Matthieu Kanj's avatar
Matthieu Kanj committed
1710 1711
        
}