lte_rate_matching.c 19.5 KB
Newer Older
1
2
3
/* file: lte_rate_matching.c
   purpose: Procedures for rate matching/interleaving for LTE (turbo-coded transport channels) (TX/RX)
   author: raymond.knopp@eurecom.fr
4
   date: 21.10.2009
5
6
7
8
9
*/
#ifdef MAIN
#include <stdio.h>
#include <stdlib.h>
#endif
10
#include "PHY/defs.h"
11
#include "assertions.h"
12
13
14

//#define cmin(a,b) ((a)<(b) ? (a) : (b))

gauthier's avatar
gauthier committed
15
16
static uint32_t bitrev[32]    = {0,16,8,24,4,20,12,28,2,18,10,26,6,22,14,30,1,17,9,25,5,21,13,29,3,19,11,27,7,23,15,31};
static uint32_t bitrev_x3[32] = {0,48,24,72,12,60,36,84,6,54,30,78,18,66,42,90,3,51,27,75,15,63,39,87,9,57,33,81,21,69,45,93};
17
18
19
20
21
22
static uint32_t bitrev_cc[32] = {1,17,9,25,5,21,13,29,3,19,11,27,7,23,15,31,0,16,8,24,4,20,12,28,2,18,10,26,6,22,14,30};

//#define RM_DEBUG_TX 1
//#define RM_DEBUG 1
//#define RM_DEBUG2 1
//#define RM_DEBUG_CC 1
23
24
25

uint32_t sub_block_interleaving_turbo(uint32_t D, uint8_t *d,uint8_t *w)
{
26
27

  uint32_t RTC = (D>>5), ND, ND3;
knopp's avatar
knopp committed
28
  uint32_t row,col,Kpi;
29
  uint32_t index3,k,k2;
30
#ifdef RM_DEBUG
31
  uint32_t nulled=0;
32
#endif
33
34
35
36
  uint8_t *d1,*d2,*d3;

  if ((D&0x1f) > 0)
    RTC++;
37

38
39
40
  Kpi = (RTC<<5);
  //  Kpi3 = Kpi*3;
  ND = Kpi - D;
41
#ifdef RM_DEBUG
42
43
  printf("sub_block_interleaving_turbo : D = %d (%d)\n",D,D*3);
  printf("RTC = %d, Kpi=%d, ND=%d\n",RTC,Kpi,ND);
44
#endif
45
46
47
48
  ND3 = ND*3;

  // copy d02 to dD2 (for mod Kpi operation from clause (4), p.16 of 36.212
  d[(3*D)+2] = d[2];
49
50
  k=0;
  k2=0;
51
52
53
54
  d1 = d-ND3;
  d2 = d1+1;
  d3 = d1+5;

55
  for (col=0; col<32; col++) {
56
57
58
59
#ifdef RM_DEBUG
    printf("Col %d\n",col);
#endif
    index3 = bitrev_x3[col];//3*index;
60
61

    for (row=0; row<RTC; row++) {
62
63
64

      w[k]            =  d1[index3];//d[index3-ND3];
      w[Kpi+k2]       =  d2[index3];//d[index3-ND3+1];
65
      w[Kpi+1+k2]     =  d3[index3];//d[index3-ND3+5];
66
67
68
69


#ifdef RM_DEBUG
      printf("row %d, index %d, index-Nd %d index-Nd+1 %d (k,Kpi+2k,Kpi+2k+1) (%d,%d,%d) w(%d,%d,%d)\n",row,index,index-ND,((index+1)%Kpi)-ND,k,Kpi+(k<<1),Kpi+(k<<1)+1,w[k],w[Kpi+(k<<1)],w[Kpi+1+(k<<1)]);
70

71
      if (w[k]== LTE_NULL)
72
73
        nulled++;

74
      if (w[Kpi+(k<<1)] ==LTE_NULL)
75
76
        nulled++;

77
      if (w[Kpi+1+(k<<1)] ==LTE_NULL)
78
79
        nulled++;

80
81
#endif
      index3+=96;
knopp's avatar
knopp committed
82
      k++;k2+=2;
83
    }
84
85
86
87
  }

  if (ND>0)
    w[(3*Kpi) - 1] = LTE_NULL;
88

89
#ifdef RM_DEBUG
90

91
92
93
94
  if (ND>0) {
    printf("RM_TX: Nulled last component in pos %d\n",Kpi-1+k2);
    nulled++;
  }
95

96
97
98
99
100
101
  printf("RM_TX: Nulled %d\n",nulled);
#endif
  return(RTC);
}


102
103
uint32_t sub_block_interleaving_cc(uint32_t D, uint8_t *d,uint8_t *w)
{
104
105
106
107
108
109
110
111
112
113

  uint32_t RCC = (D>>5), ND, ND3;
  uint32_t row,col,Kpi,index;
  uint32_t index3,k;
#ifdef RM_DEBUG_CC
  uint32_t nulled=0;
#endif

  if ((D&0x1f) > 0)
    RCC++;
114

115
116
117
118
119
120
121
122
123
124
  Kpi = (RCC<<5);
  //  Kpi3 = Kpi*3;
  ND = Kpi - D;
#ifdef RM_DEBUG_CC
  printf("sub_block_interleaving_cc : D = %d (%d), d %p, w %p\n",D,D*3,d,w);
  printf("RCC = %d, Kpi=%d, ND=%d\n",RCC,Kpi,ND);
#endif
  ND3 = ND*3;

  k=0;
125
126

  for (col=0; col<32; col++) {
127
128
129
130
131
#ifdef RM_DEBUG_CC
    printf("Col %d\n",col);
#endif
    index = bitrev_cc[col];
    index3 = 3*index;
132
133

    for (row=0; row<RCC; row++) {
134
135
      w[k]          =  d[(int32_t)index3-(int32_t)ND3];
      w[Kpi+k]     =   d[(int32_t)index3-(int32_t)ND3+1];
136
      w[(Kpi<<1)+k] =  d[(int32_t)index3-(int32_t)ND3+2];
137
138
#ifdef RM_DEBUG_CC
      printf("row %d, index %d k %d w(%d,%d,%d)\n",row,index,k,w[k],w[Kpi+k],w[(Kpi<<1)+k]);
139

140
      if (w[k]== LTE_NULL)
141
142
        nulled++;

143
      if (w[Kpi+k] ==LTE_NULL)
144
145
        nulled++;

146
      if (w[(Kpi<<1)+k] ==LTE_NULL)
147
148
        nulled++;

149
150
151
152
#endif
      index3+=96;
      index+=32;
      k++;
153
    }
154
  }
155

156
157
158
159
160
161
#ifdef RM_DEBUG_CC
  printf("RM_TX: Nulled %d\n",nulled);
#endif
  return(RCC);
}

162
163
void sub_block_deinterleaving_turbo(uint32_t D,int16_t *d,int16_t *w)
{
164
165
166
167
168
169
170
171

  uint32_t RTC = (D>>5), ND, ND3;
  uint32_t row,col,Kpi,index;
  uint32_t index3,k,k2;
  int16_t *d1,*d2,*d3;

  if ((D&0x1f) > 0)
    RTC++;
172

173
174
175
176
177
178
179
180
181
182
  Kpi = (RTC<<5);
  //  Kpi3 = Kpi*3;
  ND = Kpi - D;
#ifdef RM_DEBUG2
  printf("sub_block_interleaving_turbo : D = %d (%d)\n",D,D*3);
  printf("RTC = %d, Kpi=%d, ND=%d\n",RTC,Kpi,ND);
#endif
  ND3 = ND*3;

  // copy d02 to dD2 (for mod Kpi operation from clause (4), p.16 of 36.212
183
184
  k=0;
  k2=0;
185
186
187
188
  d1 = d-ND3;
  d2 = d1+1;
  d3 = d1+5;

189
  for (col=0; col<32; col++) {
190
191
192
193
194
#ifdef RM_DEBUG2
    printf("Col %d\n",col);
#endif
    index = bitrev[col];
    index3 = bitrev_x3[col];//3*index;
195
196

    for (row=0; row<RTC; row++) {
197
198

      d1[index3]   = w[k];
199
200
      d2[index3]   = w[Kpi+k2];
      d3[index3]   = w[Kpi+1+k2];
201
202
      index3+=96;
      index+=32;
203
204
205
206
      k++;
      k2++;
      k2++;
    }
207
  }
208
209
210

  //  if (ND>0)
  //    d[2] = LTE_NULL;//d[(3*D)+2];
211
212
213

}

214
215
void sub_block_deinterleaving_cc(uint32_t D,int8_t *d,int8_t *w)
{
216
217
218
219
220
221
222
223
224
225
226

  //WANG_Hao uint32_t RCC = (D>>5), ND, ND3;
  uint32_t RCC = (D>>5);
  ptrdiff_t   ND, ND3;
  uint32_t row,col,Kpi,index;
  //WANG_Hao uint32_t index3,k;
  ptrdiff_t index3;
  uint32_t k;

  if ((D&0x1f) > 0)
    RCC++;
227

228
229
230
231
232
233
234
235
236
237
  Kpi = (RCC<<5);
  //  Kpi3 = Kpi*3;
  ND = Kpi - D;
#ifdef RM_DEBUG2
  printf("sub_block_interleaving_cc : D = %d (%d), d %p, w %p\n",D,D*3,d,w);
  printf("RCC = %d, Kpi=%d, ND=%d\n",RCC,Kpi,ND);
#endif
  ND3 = ND*3;

  k=0;
238
239

  for (col=0; col<32; col++) {
240
241
242
243
244
#ifdef RM_DEBUG2
    printf("Col %d\n",col);
#endif
    index = bitrev_cc[col];
    index3 = 3*index;
245
246

    for (row=0; row<RCC; row++) {
247
248

      d[index3-ND3]   = w[k];
249
250
      d[index3-ND3+1] = w[Kpi+k];
      d[index3-ND3+2] = w[(Kpi<<1)+k];
251
252
253
254
255
256
#ifdef RM_DEBUG2
      printf("row %d, index %d k %d index3-ND3 %d w(%d,%d,%d)\n",row,index,k,index3-ND3,w[k],w[Kpi+k],w[(Kpi<<1)+k]);
#endif
      index3+=96;
      index+=32;
      k++;
257
    }
258
259
260
261
  }

}

262
263
uint32_t generate_dummy_w(uint32_t D, uint8_t *w,uint8_t F)
{
264
265
266
267
268
269
270
271
272
273
274

  uint32_t RTC = (D>>5), ND;
  uint32_t col,Kpi,index;
  int32_t k,k2;
#ifdef RM_DEBUG
  uint32_t nulled=0;
#endif
  uint8_t *wKpi,*wKpi1,*wKpi2,*wKpi4;

  if ((D&0x1f) > 0)
    RTC++;
275

276
277
278
279
280
281
282
  Kpi = (RTC<<5);
  //  Kpi3 = Kpi*3;
  ND = Kpi - D;
#ifdef RM_DEBUG
  printf("dummy sub_block_interleaving_turbo : D = %d (%d)\n",D,D*3);
  printf("RTC = %d, Kpi=%d, ND=%d, F=%d (Nulled %d)\n",RTC,Kpi,ND,F,(2*F + 3*ND));
#endif
283
284


285
286
287
288
289
290
  k=0;
  k2=0;
  wKpi = &w[Kpi];
  wKpi1 = &w[Kpi+1];
  wKpi2 = &w[Kpi+2];
  wKpi4 = &w[Kpi+4];
291
292

  for (col=0; col<32; col++) {
293
294
295
296
#ifdef RM_DEBUG
    printf("Col %d\n",col);
#endif
    index = bitrev[col];
297

298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
    if (index<(ND+F)) {
      w[k]   =  LTE_NULL;
      wKpi[k2] = LTE_NULL;
#ifdef RM_DEBUG
      nulled+=2;
#endif
    }

    //bits beyond 32 due to "filler" bits
    if ((index+32)<(ND+F)) {
      w[k+1]   =  LTE_NULL;
      wKpi2[k2] = LTE_NULL;
#ifdef RM_DEBUG
      nulled+=2;
#endif
    }
314

315
316
317
318
319
320
321
322
323
324
325
326
327
328
    if ((index+64)<(ND+F)) {
      w[k+2]   =  LTE_NULL;
      wKpi4[k2] = LTE_NULL;
#ifdef RM_DEBUG
      nulled+=2;
#endif
    }

    if ((index+1)<ND) {
      wKpi1[k2] =  LTE_NULL;
#ifdef RM_DEBUG
      nulled+=1;
#endif
    }
329

330
331
332
333
334
335
336
#ifdef RM_DEBUG
    printf("k %d w (%d,%d,%d) w+1 (%d,%d,%d), index %d index-ND-F %d index+32-ND-F %d\n",k,w[k],w[Kpi+(k<<1)],w[Kpi+1+(k<<1)],w[k+1],w[2+Kpi+(k<<1)],w[2+Kpi+1+(k<<1)],index,index-ND-F,index+32-ND-F);
#endif
    k+=RTC;
    k2=k<<1;
  }

337
  // copy d02 to dD2 (for mod Kpi operation from clause (4), p.16 of 36.212
338
339
  if (ND>0)
    w[(3*Kpi)-1] = LTE_NULL;
340

341
#ifdef RM_DEBUG
342

343
344
345
346
  if (ND>0) {
    nulled++;
    printf("dummy_w: Nulled final position %d\n",(3*Kpi)-1);
  }
347

348
349
350
351
352
  printf("Nulled = %d\n",nulled);
#endif
  return(RTC);
}

353
354
uint32_t generate_dummy_w_cc(uint32_t D, uint8_t *w)
{
355
356
357

  uint32_t RCC = (D>>5), ND;
  uint32_t col,Kpi,index;
gauthier's avatar
gauthier committed
358
  int32_t k;
359
360
361
362
363
364
#ifdef RM_DEBUG_CC
  uint32_t nulled=0;
#endif

  if ((D&0x1f) > 0)
    RCC++;
365

366
367
368
369
370
371
372
373
374
375
376
377
  Kpi = (RCC<<5);
  //  Kpi3 = Kpi*3;
  ND = Kpi - D;
#ifdef RM_DEBUG_CC
  printf("dummy sub_block_interleaving_cc : D = %d (%d)\n",D,D*3);
  printf("RCC = %d, Kpi=%d, ND=%d, (Nulled %d)\n",RCC,Kpi,ND,3*ND);
#endif
  //  ND3 = ND*3;

  // copy d02 to dD2 (for mod Kpi operation from clause (4), p.16 of 36.212
  k=0;

378
  for (col=0; col<32; col++) {
379
380
381
382
#ifdef RM_DEBUG_CC
    printf("Col %d\n",col);
#endif
    index = bitrev_cc[col];
383

384
385
386
387
388
389
390
391
    if (index<ND) {
      w[k]          = LTE_NULL;
      w[Kpi+k]      = LTE_NULL;
      w[(Kpi<<1)+k] = LTE_NULL;
#ifdef RM_DEBUG_CC
      nulled+=3;
#endif
    }
392

393
394
395
396
397
398
    /*
    //bits beyond 32 due to "filler" bits
    if (index+32<ND) {
      w[k+1]          = LTE_NULL;
      w[Kpi+1+k]      = LTE_NULL;
      w[(Kpi<<1)+1+k] = LTE_NULL;
399
    #ifdef RM_DEBUG
400
      nulled+=3;
401
    #endif
402
403
404
405
406
    }
    if (index+64<ND) {
      w[k+2]          = LTE_NULL;
      w[Kpi+2+k]      = LTE_NULL;
      w[(Kpi<<1)+2+k] = LTE_NULL;
407
    #ifdef RM_DEBUG
408
      nulled+=3;
409
    #endif
410
411
412
413
414
    }


    if ((index+1)<ND) {
      w[Kpi+1+(k<<1)] =  LTE_NULL;
415
    #ifdef RM_DEBUG
416
      nulled+=1;
417
    #endif
418
    }
419
    */
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#ifdef RM_DEBUG_CC
    printf("k %d w (%d,%d,%d), index-ND %d index+32-ND %d\n",k,w[k],w[Kpi+k],w[(Kpi<<1)+k],index-ND,index+32-ND);
#endif
    k+=RCC;
  }

#ifdef RM_DEBUG_CC
  printf("Nulled = %d\n",nulled);
#endif
  return(RCC);
}


uint32_t lte_rate_matching_turbo(uint32_t RTC,
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
                                 uint32_t G,
                                 uint8_t *w,
                                 uint8_t *e,
                                 uint8_t C,
                                 uint32_t Nsoft,
                                 uint8_t Mdlharq,
                                 uint8_t Kmimo,
                                 uint8_t rvidx,
                                 uint8_t Qm,
                                 uint8_t Nl,
                                 uint8_t r,
                                 uint8_t nb_rb,
                                 uint8_t m)
{


450
451
452
453
  uint32_t Nir,Ncb,Gp,GpmodC,E,Ncbmod,ind,k;
  //  int cnt=0;
  uint8_t *e2;
#ifdef RM_DEBUG_TX
454
  int code_block,round;
455
456
457
458
459
460
461
462
463
464
  int cnt;
  int zeroed=0;
  int oned=0;
  int twoed=0;
  int threed =0;
  uint32_t nulled=0;
  static unsigned char *counter_buffer[MAX_NUM_DLSCH_SEGMENTS][4];
  FILE *counter_fd;
  char fname[512];
#endif
465

466
467
468
469
470
471
472
473
  if (Mdlharq>0) {  // Downlink
    Nir = Nsoft/Kmimo/cmin(8,Mdlharq);
    Ncb = cmin(Nir/C,3*(RTC<<5));
  }
  else {  // Uplink
    Nir=0;
    Ncb = 3*(RTC<<5); // Kw
  }
474
#ifdef RM_DEBUG_TX
475
476
477
478
479
480

  if (rvidx==0 && r==0) {
    for(round=0; round<4; round++)
      for (code_block=0; code_block<MAX_NUM_DLSCH_SEGMENTS; code_block++) {
        counter_buffer[code_block][round] = (unsigned char *)malloc(Ncb*sizeof(char));
        memset(counter_buffer[code_block][round],0,Ncb*sizeof(char));
481
      }
482
483
484
485
  } else if(rvidx==3) {
    sprintf(fname, "mcs%d_rate_matching_RB_%d.txt", m, nb_rb);
    // sprintf(fname,"mcs0_rate_matching_RB_6.txt");
    counter_fd = fopen(fname,"w");
486
  }
487

488
#endif
489

490
491
492
  // if (rvidx==3)
  //  for (cnt=0;cnt<Ncb;cnt++)
  //    counter_buffer[rvidx][cnt]=0;
knopp's avatar
knopp committed
493
494
  if (Ncb>(3*(RTC<<5)))
    AssertFatal(1==0,"Exiting, RM condition (Ncb %d, RTC %d, Nir/C %d, Nsoft %d, Kw %d)\n",Ncb,RTC,Nir/C,Nsoft,3*(RTC<<5));
495
  
496

497
498
499
500
  Gp = G/Nl/Qm;
  GpmodC = Gp%C;

#ifdef RM_DEBUG
501
  printf("lte_rate_matching_turbo: Ncb %d, Kw %d, Nir/C %d, rvidx %d, G %d, Qm %d, Nl%d, r %d\n",Ncb,3*(RTC<<5),Nir/C,rvidx, G, Qm,Nl,r);
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#endif

  if (r < (C-(GpmodC)))
    E = Nl*Qm * (Gp/C);
  else
    E = Nl*Qm * ((GpmodC==0?0:1) + (Gp/C));

  Ncbmod = Ncb%(RTC<<3);

  ind = RTC * (2+(rvidx*(((Ncbmod==0)?0:1) + (Ncb/(RTC<<3)))*2));

#ifdef RM_DEBUG_TX
  printf("lte_rate_matching_turbo: E %d, k0 %d, Ncbmod %d, Ncb/(RTC<<3) %d\n",E,ind,Ncbmod,Ncb/(RTC<<3));
#endif

517
518
  //e2=e+(r*E);
  e2 = e;
519

520
521
  k=0;

522
  for (; (ind<Ncb)&&(k<E); ind++) {
523
    //    e2[k]=w[ind];
524
525
526
#ifdef RM_DEBUG_TX
    printf("RM_TX k%d Ind: %d (%d)\n",k,ind,w[ind]);
#endif
527

528
    if (w[ind] != LTE_NULL) e2[k++]=w[ind];
529
  }
530

531
  while(k<E) {
532
    for (ind=0; (ind<Ncb)&&(k<E); ind++) {
533
      //      e2[k] = w[ind];
534
#ifdef RM_DEBUG_TX
535
      printf("RM_TX k%d Ind: %d (%d)\n",k,ind,w[ind]);
536
#endif
537

538
      if (w[ind] != LTE_NULL) e2[k++]=w[ind];
539
540
    }
  }
541
542

  /*
543
  for (k=0;k<E;k++) {
544

545
546

    while(w[ind] == LTE_NULL) {
547
  #ifdef RM_DEBUG_TX
548
549
      printf("RM_tx : ind %d, NULL\n",ind);
      nulled++;
550
  #endif
551
552
      ind++;
      if (ind==Ncb)
553
  ind=0;
554
555
556
557
558
    }

    e2[k] = w[ind];
    //    printf("RM_TX k%d Ind: %d (%d)\n",k,ind,w[ind]);
    //    cnt = cnt+1;
559
  #ifdef RM_DEBUG_TX
560
561
562
563
    counter_buffer[r][rvidx][ind]++;
    // printf("Bit_Counter[%d][%d][%d]=%d\n",r,rvidx,ind,counter_buffer[r][rvidx][ind]);
    //    printf("k %d ind %d, w %c(%d)\n",k,ind,w[ind],w[ind]);
    // printf("RM_TX %d (%d) Ind: %d (%d)\n",k,k+r*E,ind,e2[k]);
564
  #endif
565
566
567
568
    ind++;
    if (ind==Ncb)
      ind=0;
  }
569
570

  #ifdef RM_DEBUG_TX
571
572
573
  if (rvidx==3){
    for(cnt=0;cnt<Ncb;cnt++)
      {
574
575
576
577
578
579
  fprintf(counter_fd,"%d %x %x %x %x %x %x %x %x %x %x %x %x\n",cnt,
    counter_buffer[0][0][cnt],counter_buffer[1][0][cnt],counter_buffer[2][0][cnt],
    counter_buffer[0][1][cnt],counter_buffer[1][1][cnt],counter_buffer[2][1][cnt],
    counter_buffer[0][2][cnt],counter_buffer[1][2][cnt],counter_buffer[2][2][cnt],
    counter_buffer[0][3][cnt],counter_buffer[1][3][cnt],counter_buffer[2][3][cnt]
    );
580
581
    }
    fclose(counter_fd);
582

583
  }
584
585


586
587
588
589
590
591
592
593
594
595
596
597
   for(cnt=0;cnt<Ncb;cnt++){
    printf("Bit_Counter[%d][%d]=%d\n",rvidx,cnt,counter_buffer[r][rvidx][cnt]);
    if(counter_buffer[r][rvidx][cnt]==0)
      zeroed++;
    else if(counter_buffer[r][rvidx][cnt]==1)
      oned++;
    else if(counter_buffer[r][rvidx][cnt]==2)
      twoed++;
    else if(counter_buffer[r][rvidx][cnt]==3)
      threed++;
   }

598
599
600
601
602
  printf("zeroed %d\n",zeroed);
  printf("oned %d\n",oned);
  printf("twoed %d\n",twoed);
  printf("threed %d\n",threed);

603
  printf("nulled %d\n",nulled);
604
605
606

  #endif
  */
607
608
  return(E);
}
609

610
611

uint32_t lte_rate_matching_cc(uint32_t RCC,
612
613
614
615
616
                              uint16_t E,
                              uint8_t *w,
                              uint8_t *e)
{

617
618
619

  uint32_t ind=0,k;

gauthier's avatar
gauthier committed
620
  uint16_t Kw = 3*(RCC<<5);
621
622
623
624
625
626
627

#ifdef RM_DEBUG_CC
  uint32_t nulled=0;

  printf("lte_rate_matching_cc: Kw %d, E %d\n",Kw, E);
#endif

628
  for (k=0; k<E; k++) {
629
630
631
632
633
634
635
636
637


    while(w[ind] == LTE_NULL) {

#ifdef RM_DEBUG_CC
      nulled++;
      printf("RM_TX_CC : ind %d, NULL\n",ind);
#endif
      ind++;
638

639
      if (ind==Kw)
640
        ind=0;
641
642
643
644
645
    }


    e[k] = w[ind];
#ifdef RM_DEBUG_CC
646
    //    printf("k %d ind %d, w %c(%d)\n",k,ind,w[ind],w[ind]);
647
648
649
    printf("RM_TX_CC %d Ind: %d (%d)\n",k,ind,e[k]);
#endif
    ind++;
650

651
652
653
    if (ind==Kw)
      ind=0;
  }
654

655
656
657
658
659
660
661
662
#ifdef RM_DEBUG_CC
  printf("nulled %d\n",nulled);
#endif
  return(E);
}


int lte_rate_matching_turbo_rx(uint32_t RTC,
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
                               uint32_t G,
                               int16_t *w,
                               uint8_t *dummy_w,
                               int16_t *soft_input,
                               uint8_t C,
                               uint32_t Nsoft,
                               uint8_t Mdlharq,
                               uint8_t Kmimo,
                               uint8_t rvidx,
                               uint8_t clear,
                               uint8_t Qm,
                               uint8_t Nl,
                               uint8_t r,
                               uint32_t *E_out)
{


680
681
  uint32_t Nir,Ncb,Gp,GpmodC,E,Ncbmod,ind,k;
  int16_t *soft_input2;
682
  //   int32_t w_tmp;
683
684
685
#ifdef RM_DEBUG
  int nulled=0;
#endif
686

687
  if (Kmimo==0 || C==0 || Qm==0 || Nl==0) {
688
    printf("lte_rate_matching.c: invalid parameters (Kmimo %d, Mdlharq %d, C %d, Qm %d, Nl %d\n",
689
        Kmimo,Mdlharq,C,Qm,Nl);
690
691
692
    return(-1);
  }

693
694
695
696
697
698
699
700
  if (Mdlharq>0) { // Downlink
    Nir = Nsoft/Kmimo/cmin(8,Mdlharq);
    Ncb = cmin(Nir/C,3*(RTC<<5));
  }
  else {  // Uplink
    Nir=0;
    Ncb = 3*(RTC<<5);
  }
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721

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



  if (r < (C-(GpmodC)))
    E = Nl*Qm * (Gp/C);
  else
    E = Nl*Qm * ((GpmodC==0?0:1) + (Gp/C));

  Ncbmod = Ncb%(RTC<<3);

  ind = RTC * (2+(rvidx*(((Ncbmod==0)?0:1) + (Ncb/(RTC<<3)))*2));

#ifdef RM_DEBUG
  printf("lte_rate_matching_turbo_rx: Clear %d, E %d, Ncb %d, Kw %d, rvidx %d, G %d, Qm %d, Nl%d, r %d\n",clear,E,Ncb,3*(RTC<<5),rvidx, G, Qm,Nl,r);
#endif

  if (clear==1)
    memset(w,0,Ncb*sizeof(int16_t));
722

knopp's avatar
   
knopp committed
723
  soft_input2 = soft_input;// + (r*E);
724
725
  k=0;

726
727
728
  for (; (ind<Ncb)&&(k<E); ind++) {
    if (dummy_w[ind] != LTE_NULL) {
      w[ind] += soft_input2[k++];
729
730
731
732
#ifdef RM_DEBUG
      printf("RM_RX k%d Ind: %d (%d)\n",k-1,ind,w[ind]);
#endif
    }
733

734
735
736
737
738
#ifdef RM_DEBUG
    else {
      printf("RM_RX Ind: %d NULL %d\n",ind,nulled);
      nulled++;
    }
739

740
741
#endif
  }
742

743
  while(k<E) {
744
745
746
    for (ind=0; (ind<Ncb)&&(k<E); ind++) {
      if (dummy_w[ind] != LTE_NULL) {
        w[ind] += soft_input2[k++];
747
#ifdef RM_DEBUG
748
        printf("RM_RX k%d Ind: %d (%d)(soft in %d)\n",k-1,ind,w[ind],soft_input2[k-1]);
749
750
#endif
      }
751

752
753
#ifdef RM_DEBUG
      else {
754
755
756
757
        printf("RM_RX Ind: %d NULL %d\n",ind,nulled);
        nulled++;
      }

758
759
760
761
762
763
764
765
766
#endif
    }
  }

  /*
  for (k=0;k<E;k++) {


    while(dummy_w[ind] == LTE_NULL) {
767
  #ifdef RM_DEBUG
768
      printf("RM_rx : ind %d, NULL\n",ind);
769
  #endif
770
771
      ind++;
      if (ind==Ncb)
772
  ind=0;
773
774
    }
  */
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
  /*
  if (w[ind] != 0)
    printf("repetition %d (%d,%d,%d)\n",ind,rvidx,E,Ncb);
  */
  // Maximum-ratio combining of repeated bits and retransmissions
  /*
  w_tmp = (int) w[ind] + (int) soft_input2[k];
  if (w_tmp > 32767) {
    //#ifdef DEBUG_RM
    printf("OVERFLOW!!!!!, w_tmp = %d\n",w_tmp);
    //#endif
    w[ind] = 32767;
  }
  else if (w_tmp < -32768) {
    //#ifdef DEBUG_RM
    printf("UNDERFLOW!!!!!, w_tmp = %d\n",w_tmp);
    //#endif
    w[ind] = -32768;
  }
  else
  */
796
797
  /*
    w[ind] += soft_input2[k];
798
  #ifdef RM_DEBUG
799
      printf("RM_RX k%d Ind: %d (%d)\n",k,ind,w[ind]);
800
  #endif
801
802
803
804
805
806
807
808
809
810
811
812
813
    ind++;
    if (ind==Ncb)
      ind=0;
  }
  */

  *E_out = E;
  return(0);

}


void lte_rate_matching_cc_rx(uint32_t RCC,
814
815
816
817
818
819
820
                             uint16_t E,
                             int8_t *w,
                             uint8_t *dummy_w,
                             int8_t *soft_input)
{


821
822

  uint32_t ind=0,k;
gauthier's avatar
gauthier committed
823
  uint16_t Kw = 3*(RCC<<5);
824
825
826
827
828
829
830
831
832
833
834
835
  uint32_t acc=1;
  int16_t w16[Kw];
#ifdef RM_DEBUG_CC
  uint32_t nulled=0;

  printf("lte_rate_matching_cc_rx: Kw %d, E %d, w %p, soft_input %p\n",3*(RCC<<5),E,w,soft_input);
#endif


  memset(w,0,Kw);
  memset(w16,0,Kw*sizeof(int16_t));

836
  for (k=0; k<E; k++) {
837
838
839
840
841
842
843
844


    while(dummy_w[ind] == LTE_NULL) {
#ifdef RM_DEBUG_CC
      nulled++;
      printf("RM_RX : ind %d, NULL\n",ind);
#endif
      ind++;
845

846
      if (ind==Kw)
847
        ind=0;
848
    }
849

850
851
852
853
854
855
    /*
    if (w[ind] != 0)
      printf("repetition %d (%d,%d,%d)\n",ind,rvidx,E,Ncb);
    */
    // Maximum-ratio combining of repeated bits and retransmissions
#ifdef RM_DEBUG_CC
856
    printf("RM_RX_CC k %d (%d) ind: %d (%d)\n",k,soft_input[k],ind,w16[ind]);
857
858
859
#endif


860
    w16[ind] += soft_input[k];
861

862
863
864
865
866
867
    ind++;

    if (ind==Kw) {
      ind=0;
      acc++;
    }
868
  }
869

870
  // rescale
871
  for (ind=0; ind<Kw; ind++) {
872
873
874
875
876
877
878
879
    //    w16[ind]=(w16[ind]/acc);
    if (w16[ind]>7)
      w[ind]=7;
    else if (w16[ind]<-8)
      w[ind]=-8;
    else
      w[ind]=(int8_t)w16[ind];
  }
880

881
882
883
884
885
886
887
888
889
#ifdef RM_DEBUG_CC

  printf("Nulled %d\n",nulled);
#endif
}


#ifdef MAIN

890
891
void main()
{
892
893
894
895
896
897
898
899
900
  uint8_t d[96+3+(3*6144)];
  uint8_t w[3*6144],e[12*6144];
  uint32_t RTC,G,rvidx;
  uint32_t nb_rb=6;
  uint32_t mod_order = 4;
  uint32_t first_dlsch_symbol = 2;
  uint32_t i;

  G = ( nb_rb * (12 * mod_order) * (12-first_dlsch_symbol-3)) ;//( nb_rb * (12 * mod_order) * (14-first_dlsch_symbol-3)) :
901

902
  // initialize 96 first positions to "LTE_NULL"
903
  for (i=0; i<96; i++)
904
905
906
    d[i]=LTE_NULL;

  RTC = sub_block_interleaving_turbo(4+(192*8), &d[96], w);
907
908

  for (rvidx=0; rvidx<4; rvidx++) {
909
    lte_rate_matching_turbo(RTC,
910
911
912
913
914
915
916
917
918
919
920
921
922
                            G,
                            w,
                            e,
                            1,           //C
                            1827072,     //Nsoft,
                            8,           //Mdlharq,
                            1,           //Kmimo,
                            rvidx,       //rvidx,
                            mod_order,   //Qm,
                            1,           //Nl,
                            0            //r
                           );
  }
923
924
925
926
}

#endif