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

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


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

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

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

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

ghaddab's avatar
ghaddab committed
28
 *******************************************************************************/
29 30 31 32 33 34 35 36 37 38 39
/* file: 3gpplte_turbo_decoder.c
   purpose: Fixed-point routines for implementing max-logmap decoding of Turbo-coded (DLSCH) transport channels from 36-212, V8.6 2009-03
   Modified from TI C6x reference design.
   currently maintained in openairinterface.org by R. Knopp (knopp@eurecom.fr)
   Note: This is a reference design for the sse version (3gpplte_turbo_decoder_sse.c)

*/

#include "PHY/defs.h"
#include "PHY/CODING/defs.h"
#include "PHY/CODING/lte_interleaver_inline.h"
40
#include "PHY/sse_intrin.h"
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

/*
// 3gpp2 polynomials
#define M0T -m11
#define M0B m11
#define M1T m11
#define M1B -m11
#define M2T -m10
#define M2B m10
#define M3T m10
#define M3B -m10
#define M4T m10
#define M4B -m10
#define M5T -m10
#define M5B m10
#define M6T m11
#define M6B -m11
#define M7T -m11
#define M7B m11
*/

// 3gpplte polynomials
#define m00 (-m11)
#define m01 (-m10)

#define M0T m00
#define M0B m11
#define M1T m11
#define M1B m00
#define M2T m10
#define M2B m01
#define M3T m01
#define M3B m10
#define M4T m01
#define M4B m10
#define M5T m10
#define M5B m01
#define M6T m11
#define M6B m00
#define M7T m00
#define M7B m11

#define M0T_TERM m00
#define M1T_TERM m11
#define M2T_TERM m10
#define M3T_TERM m01
#define M4T_TERM m01
#define M5T_TERM m10
#define M6T_TERM m11
#define M7T_TERM m00
91 92
/*
// 3GPP2 AlphaBeta
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
#define ALPHA_BETA_1m00 alpha[(k-1)*STATES+0] + beta[k*STATES+0]
#define ALPHA_BETA_1m11 alpha[(k-1)*STATES+0] + beta[k*STATES+4]
#define ALPHA_BETA_2m11 alpha[(k-1)*STATES+1] + beta[k*STATES+0]
#define ALPHA_BETA_2m00 alpha[(k-1)*STATES+1] + beta[k*STATES+4]
#define ALPHA_BETA_1m01 alpha[(k-1)*STATES+2] + beta[k*STATES+1]
#define ALPHA_BETA_1m10 alpha[(k-1)*STATES+2] + beta[k*STATES+5]
#define ALPHA_BETA_2m10 alpha[(k-1)*STATES+3] + beta[k*STATES+1]
#define ALPHA_BETA_2m01 alpha[(k-1)*STATES+3] + beta[k*STATES+5]
#define ALPHA_BETA_3m10 alpha[(k-1)*STATES+4] + beta[k*STATES+2]
#define ALPHA_BETA_3m01 alpha[(k-1)*STATES+4] + beta[k*STATES+6]
#define ALPHA_BETA_4m01 alpha[(k-1)*STATES+5] + beta[k*STATES+2]
#define ALPHA_BETA_4m10 alpha[(k-1)*STATES+5] + beta[k*STATES+6]
#define ALPHA_BETA_3m11 alpha[(k-1)*STATES+6] + beta[k*STATES+3]
#define ALPHA_BETA_3m00 alpha[(k-1)*STATES+6] + beta[k*STATES+7]
#define ALPHA_BETA_4m00 alpha[(k-1)*STATES+7] + beta[k*STATES+3]
#define ALPHA_BETA_4m11 alpha[(k-1)*STATES+7] + beta[k*STATES+7]
*/

#define ALPHA_BETA_1m00 alpha[(k-1)*STATES+0] + beta[k*STATES+0]
#define ALPHA_BETA_1m11 alpha[(k-1)*STATES+0] + beta[k*STATES+4]
#define ALPHA_BETA_2m11 alpha[(k-1)*STATES+1] + beta[k*STATES+0]
#define ALPHA_BETA_2m00 alpha[(k-1)*STATES+1] + beta[k*STATES+4]
#define ALPHA_BETA_1m10 alpha[(k-1)*STATES+2] + beta[k*STATES+1]
#define ALPHA_BETA_1m01 alpha[(k-1)*STATES+2] + beta[k*STATES+5]
#define ALPHA_BETA_2m10 alpha[(k-1)*STATES+3] + beta[k*STATES+5]
#define ALPHA_BETA_2m01 alpha[(k-1)*STATES+3] + beta[k*STATES+1]
#define ALPHA_BETA_3m10 alpha[(k-1)*STATES+4] + beta[k*STATES+6]
#define ALPHA_BETA_3m01 alpha[(k-1)*STATES+4] + beta[k*STATES+2]
#define ALPHA_BETA_4m01 alpha[(k-1)*STATES+5] + beta[k*STATES+6]
#define ALPHA_BETA_4m10 alpha[(k-1)*STATES+5] + beta[k*STATES+2]
#define ALPHA_BETA_3m11 alpha[(k-1)*STATES+6] + beta[k*STATES+3]
#define ALPHA_BETA_3m00 alpha[(k-1)*STATES+6] + beta[k*STATES+7]
#define ALPHA_BETA_4m00 alpha[(k-1)*STATES+7] + beta[k*STATES+3]
#define ALPHA_BETA_4m11 alpha[(k-1)*STATES+7] + beta[k*STATES+7]


typedef char Binary;
typedef short llr_t; // internal decoder data is 16-bit fixed
typedef short channel_t;

#define LLR_MAX 32767
#define LLR_MIN -32768
#define LLRTOT 16
#define MAX 32767//16383
#define FRAME_LENGTH_MAX 6144
#define STATES 8

void log_map_s (llr_t* systematic,channel_t* y_parity, llr_t* ext,unsigned short frame_length,unsigned char term_flag,unsigned char F);
void compute_gamma_s(llr_t* m11,llr_t* m10,llr_t* systematic, channel_t* y_parity, unsigned short frame_length,unsigned char term_flag);
void compute_alpha_s(llr_t*alpha,llr_t* m11,llr_t* m10, unsigned short frame_length,unsigned char F);
void compute_beta_s(llr_t* beta,llr_t* m11,llr_t* m10,llr_t* alpha, unsigned short frame_length,unsigned char F);
void compute_ext_s(llr_t* alpha,llr_t* beta,llr_t* m11,llr_t* m10,llr_t* extrinsic, llr_t* ap, unsigned short frame_length);

// global variables
//
llr_t alpha[(FRAME_LENGTH_MAX+3+1)*8];
llr_t beta[(FRAME_LENGTH_MAX+3+1)*8];
llr_t m11[(FRAME_LENGTH_MAX+3)];
llr_t m10[(FRAME_LENGTH_MAX+3)];


154 155
void log_map_s(llr_t* systematic,channel_t* y_parity, llr_t* ext,unsigned short frame_length,unsigned char term_flag,unsigned char F)
{
156 157 158 159 160 161 162 163 164 165 166 167 168
#ifdef DEBUG_LOGMAP
  unsigned short i;


  unsigned short argmax;
  llr_t max;

  printf("Gamma ...\n");
#endif //DEBUG_LOGMAP

  compute_gamma_s(m11,m10,systematic,y_parity,frame_length,term_flag);

#ifdef DEBUG_LOGMAP
169 170

  for (i=0; i<frame_length+3; i++)
171
    printf("Position %d : (%d,%d,%d,%d)\n",i,m11[i],m10[i],-m11[i],-m10[i]);
172

173 174 175 176 177 178 179
  printf("\n");
  printf("Alpha ...\n");
#endif //DEBUG_LOGMAP

  compute_alpha_s(alpha,m11,m10,frame_length,F);

#ifdef DEBUG_LOGMAP
180 181

  for (i=0; i<frame_length+4; i++) {
182
    printf("Position %d : (%d,%d,%d,%d) (%d,%d,%d,%d,%d,%d,%d,%d) -> ",i,
183 184 185 186 187 188 189 190
           -m11[i],m10[i],m11[i],-m10[i],alpha[(i<<3) + 0],
           alpha[(i<<3) + 1],
           alpha[(i<<3) + 2],
           alpha[(i<<3) + 3],
           alpha[(i<<3) + 4],
           alpha[(i<<3) + 5],
           alpha[(i<<3) + 6],
           alpha[(i<<3) + 7]);
191 192
    argmax = 0;
    max = alpha[(i<<3)];
193

194 195 196 197
    if (max<alpha[(i<<3) + 1]) {
      argmax=1;
      max = alpha[(i<<3) + 1];
    }
198

199 200 201 202
    if (max<alpha[(i<<3) + 2]) {
      argmax=2;
      max = alpha[(i<<3) + 2];
    }
203

204 205 206 207
    if (max<alpha[(i<<3) + 3]) {
      argmax=3;
      max = alpha[(i<<3) + 3];
    }
208

209 210 211 212
    if (max<alpha[(i<<3) + 4]) {
      argmax=4;
      max = alpha[(i<<3) + 4];
    }
213

214 215 216 217
    if (max<alpha[(i<<3) + 5]) {
      argmax=5;
      max = alpha[(i<<3) + 5];
    }
218

219 220 221 222
    if (max<alpha[(i<<3) + 6]) {
      argmax=6;
      max = alpha[(i<<3) + 6];
    }
223

224 225 226 227 228 229 230
    if (max<alpha[(i<<3) + 7]) {
      argmax=7;
      max = alpha[(i<<3) + 7];
    }

    printf("argmax = %d\n",argmax);
  }
231

232 233 234 235 236 237 238
  printf("Beta ...\n");

#endif //DEBUG_LOGMAP

  compute_beta_s(beta,m11,m10,alpha,frame_length,F);

#ifdef DEBUG_LOGMAP
239 240

  for (i=0; i<=frame_length+3; i++) {
241
    printf("Position %d : (%d,%d,%d,%d,%d,%d,%d,%d)->",i,
242 243 244 245 246 247 248 249
           beta[(i<<3) + 0],
           beta[(i<<3) + 1],
           beta[(i<<3) + 2],
           beta[(i<<3) + 3],
           beta[(i<<3) + 4],
           beta[(i<<3) + 5],
           beta[(i<<3) + 6],
           beta[(i<<3) + 7]);
250 251 252

    argmax = 0;
    max = beta[(i<<3)];
253

254 255 256 257
    if (max<beta[(i<<3) + 1]) {
      argmax=1;
      max = beta[(i<<3) + 1];
    }
258

259 260 261 262
    if (max<beta[(i<<3) + 2]) {
      argmax=2;
      max = beta[(i<<3) + 2];
    }
263

264 265 266 267
    if (max<beta[(i<<3) + 3]) {
      argmax=3;
      max = beta[(i<<3) + 3];
    }
268

269 270 271 272
    if (max<beta[(i<<3) + 4]) {
      argmax=4;
      max = beta[(i<<3) + 4];
    }
273

274 275 276 277
    if (max<beta[(i<<3) + 5]) {
      argmax=5;
      max = beta[(i<<3) + 5];
    }
278

279 280 281 282
    if (max<beta[(i<<3) + 6]) {
      argmax=6;
      max = beta[(i<<3) + 6];
    }
283

284 285 286 287 288 289 290
    if (max<beta[(i<<3) + 7]) {
      argmax=7;
      max = beta[(i<<3) + 7];
    }

    printf("argmax = %d\n",argmax);
  }
291

292 293 294 295 296 297
  printf("Extrinsic ...\n");
#endif //DEBUG_LOGMAP

  compute_ext_s(alpha,beta,m11,m10,ext,systematic,frame_length);

#ifdef DEBUG_LOGMAP
298 299

  for (i=0; i<frame_length+3; i++)
300
    printf("Position %d : ext %d, ext+sys %d\n",i,
301 302
           ext[i],ext[i]+systematic[i]);

303 304 305 306 307
  printf("\n");
#endif //DEBUG_LOGMAP

}

308 309
inline int SAT_ADD(int a,int b,int m)
{
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328

  if (a+b > m) {
#ifdef DEBUG_LOGMAP
    printf("***\n");
#endif
    return(m);
  }

  if (a+b < -m) {
#ifdef DEBUG_LOGMAP
    printf("***\n");
#endif
    return(-m);
  }

  return(a+b);
}

void compute_gamma_s(llr_t* m11,llr_t* m10,llr_t* systematic,channel_t* y_parity,
329
                     unsigned short frame_length,unsigned char term_flag)
330
{
331
  int k;
332

333 334 335 336
  for (k=0; k<frame_length; k++) {
    m11[k] = (systematic[k]+y_parity[k])/2;
    m10[k] = (systematic[k]-y_parity[k])/2;
    //      printf("gamma %d : (%d,%d) -> (%d,%d)\n",k,systematic[k],y_parity[k],m11[k],m10[k]);
337 338 339 340
  }

  // Compute metrics for trellis termination
  if (term_flag == 0) { // This is for the termination of the first code
341
    for (k=frame_length; k<frame_length+3; k++) {
342 343 344 345
      m11[k] = (systematic[k]+y_parity[k])/2;
      m10[k] = (systematic[k]-y_parity[k])/2;
      //      printf("gamma %d : (%d,%d) -> (%d,%d)\n",k,systematic[k],y_parity[k],m11[k],m10[k]);
    }
346 347
  } else {
    for (k=frame_length; k<frame_length+3; k++) {
348 349 350 351 352 353 354 355 356
      m11[k] = (systematic[k+3]+y_parity[k])/2;
      m10[k] = (systematic[k+3]-y_parity[k])/2;
      //      printf("gamma %d : (%d,%d) -> (%d,%d)\n",k,systematic[k],y_parity[k],m11[k],m10[k]);
    }
  }
}

short systematic0[6144],systematic1[6144],systematic2[6144],yparity1[6144],yparity2[6144];

357 358
void compute_alpha_s(llr_t* alpha,llr_t* m_11,llr_t* m_10,unsigned short frame_length,unsigned char F)
{
359 360 361 362 363 364 365 366 367 368 369

  int k,i;
  llr_t m11,m10;
  llr_t old0,old1,old2, old3, old4, old5, old6, old7;
  llr_t new0,new1,new2, new3, new4, new5, new6, new7;
  llr_t m_b0,m_b1,m_b2, m_b3, m_b4, m_b5, m_b6, m_b7;
  // initialize log_alpha[0][m]
  llr_t alpha_max;

  old0 = 0;
  old1 = -MAX/2;
370 371 372 373 374 375
  old2 = -MAX/2;
  old3 = -MAX/2;
  old4 = -MAX/2;
  old5 = -MAX/2;
  old6 = -MAX/2;
  old7 = -MAX/2;
376 377 378 379 380 381 382 383 384 385 386 387
  alpha[0*(frame_length)+0] = old0;
  alpha[0*(frame_length)+1] = old1;
  alpha[0*(frame_length)+2] = old2;
  alpha[0*(frame_length)+3] = old3;
  alpha[0*(frame_length)+4] = old4;
  alpha[0*(frame_length)+5] = old5;
  alpha[0*(frame_length)+6] = old6;
  alpha[0*(frame_length)+7] = old7;

  //
  // compute log_alpha[k][m]
  // Steady-state portion
388 389
  for (k=1; k<=F; k++)
    for (i=0; i<8; i++)
390 391
      alpha[i+(k*8)] = alpha[i];

392 393 394
  for (k=1; k<=frame_length; k++) {
    m11=m_11[k-1];
    m10=m_10[k-1];
395

396 397 398 399 400 401 402 403
    m_b0 = SAT_ADD(old1,M1T,MAX);
    m_b4 = SAT_ADD(old1,M1B,MAX);
    m_b1 = SAT_ADD(old3,M3T,MAX);
    m_b5 = SAT_ADD(old3,M3B,MAX);
    m_b2 = SAT_ADD(old5,M5T,MAX);
    m_b6 = SAT_ADD(old5,M5B,MAX);
    m_b3 = SAT_ADD(old7,M7T,MAX);
    m_b7 = SAT_ADD(old7,M7B,MAX);
404

405 406 407 408 409 410 411 412 413
    new0 = SAT_ADD(old0,M0T,MAX);
    new4 = SAT_ADD(old0,M0B,MAX);
    new1 = SAT_ADD(old2,M2T,MAX);
    new5 = SAT_ADD(old2,M2B,MAX);
    new2 = SAT_ADD(old4,M4T,MAX);
    new6 = SAT_ADD(old4,M4B,MAX);
    new3 = SAT_ADD(old6,M6T,MAX);
    new7 = SAT_ADD(old6,M6B,MAX);
    //      printf("Output %d (%d,%d)\n",k-1,systematic0[k-1],yparity1[k-1]);
414

415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 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 504 505 506 507 508 509
    //      printf("(m_b0 %d,new0 %d),(m_b1 %d,new1 %d),(m_b2 %d,new2 %d),(m_b3 %d,new3 %d),(m_b4 %d,new4 %d),(m_b5 %d,new5 %d),(m_b6 %d,new6 %d),(m_b7 %d,new7 %d)\n",m_b0,new0,m_b1,new1,m_b2,new2,m_b3,new3,m_b4,new4,m_b5,new5,m_b6,new6,m_b7,new7);

    if (m_b0 > new0) new0=m_b0;

    alpha[k*STATES + 0] = new0;
    old0=new0;

    if (m_b4 > new4) new4=m_b4;

    alpha[k*STATES + 4] = new4;
    old4=new4;

    if (m_b1 > new1) new1=m_b1;

    alpha[k*STATES + 1] = new1;
    old1=new1;

    if (m_b5 > new5) new5=m_b5;

    alpha[k*STATES + 5] = new5;
    old5=new5;

    if (m_b2 > new2) new2=m_b2;

    alpha[k*STATES + 2] = new2;
    old2=new2;

    if (m_b6 > new6) new6=m_b6;

    alpha[k*STATES + 6] = new6;
    old6=new6;

    if (m_b3 > new3) new3=m_b3;

    alpha[k*STATES + 3] = new3;
    old3=new3;

    if (m_b7 > new7) new7=m_b7;

    alpha[k*STATES + 7] = new7;
    old7=new7;

    alpha_max = alpha[(STATES*k) + 0];

    if(alpha[(STATES*k) + 1]>alpha_max)
      alpha_max = alpha[(STATES*k) + 1];

    if(alpha[(STATES*k) + 2]>alpha_max)
      alpha_max = alpha[(STATES*k) + 2];

    if(alpha[(STATES*k) + 3]>alpha_max)
      alpha_max = alpha[(STATES*k) + 3];

    if(alpha[(STATES*k) + 4]>alpha_max)
      alpha_max = alpha[(STATES*k) + 4];

    if(alpha[(STATES*k) + 5]>alpha_max)
      alpha_max = alpha[(STATES*k) + 5];

    if(alpha[(STATES*k) + 6]>alpha_max)
      alpha_max = alpha[(STATES*k) + 6];

    if(alpha[(STATES*k) + 7]>alpha_max)
      alpha_max = alpha[(STATES*k) + 7];

    alpha[(STATES*k)+0]-=alpha_max;
    alpha[(STATES*k)+1]-=alpha_max;
    alpha[(STATES*k)+2]-=alpha_max;
    alpha[(STATES*k)+3]-=alpha_max;
    alpha[(STATES*k)+4]-=alpha_max;
    alpha[(STATES*k)+5]-=alpha_max;
    alpha[(STATES*k)+6]-=alpha_max;
    alpha[(STATES*k)+7]-=alpha_max;
    new0=alpha[(STATES*k)+0];
    new1=alpha[(STATES*k)+1];
    new2=alpha[(STATES*k)+2];
    new3=alpha[(STATES*k)+3];
    new4=alpha[(STATES*k)+4];
    new5=alpha[(STATES*k)+5];
    new6=alpha[(STATES*k)+6];
    new7=alpha[(STATES*k)+7];
  }

  for (k=frame_length+1; k<=frame_length+3; k++) {
    m11=m_11[k-1];
    m10=m_10[k-1];
    m_b0 = SAT_ADD(old1,M1T_TERM,MAX);
    m_b1 = SAT_ADD(old3,M3T_TERM,MAX);
    m_b2 = SAT_ADD(old5,M5T_TERM,MAX);
    m_b3 = SAT_ADD(old7,M7T_TERM,MAX);

    new0 = SAT_ADD(old0,M0T_TERM,MAX);
    new1 = SAT_ADD(old2,M2T_TERM,MAX);
    new2 = SAT_ADD(old4,M4T_TERM,MAX);
    new3 = SAT_ADD(old6,M6T_TERM,MAX);
510
#ifdef DEBUG_LOGMAP
511 512
    printf("alpha term: m00 %d, m10 %d : (%d,%d,%d,%d) (%d,%d,%d,%d) (%d,%d,%d,%d) (%d,%d,%d,%d)\n",
           m00,m10,old0,old1,m_b0,new0,old2,old3,m_b1,new1,old3,old4,m_b2,new2,old6,old7,m_b3,new3);
513 514
#endif //DEBUG_LOGMAP

515
    if (m_b0 > new0) new0=m_b0;
516

517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
    alpha[k*STATES + 0] = new0;
    old0=new0;

    if (m_b2 > new2) new2=m_b2;

    alpha[k*STATES + 2] = new2;
    old2=new2;

    if (m_b1 > new1) new1=m_b1;

    alpha[k*STATES + 1] = new1;
    old1=new1;

    if (m_b3 > new3) new3=m_b3;

    alpha[k*STATES + 3] = new3;
    old3=new3;

    alpha_max = alpha[(STATES*k) + 0];

    if(alpha[(STATES*k) + 1]>alpha_max)
      alpha_max = alpha[(STATES*k) + 1];

    if(alpha[(STATES*k) + 2]>alpha_max)
      alpha_max = alpha[(STATES*k) + 2];

    if(alpha[(STATES*k) + 3]>alpha_max)
      alpha_max = alpha[(STATES*k) + 3];

    alpha[(STATES*k)+0]-=alpha_max;
    alpha[(STATES*k)+1]-=alpha_max;
    alpha[(STATES*k)+2]-=alpha_max;
    alpha[(STATES*k)+3]-=alpha_max;

    new0=alpha[(STATES*k)+0];
    new1=alpha[(STATES*k)+1];
    new2=alpha[(STATES*k)+2];
    new3=alpha[(STATES*k)+3];
  }
556 557
}

558 559
void compute_beta_s(llr_t* beta,llr_t *m_11,llr_t* m_10,llr_t* alpha,unsigned short frame_length,unsigned char F)
{
560 561 562 563
  int k,i;
  llr_t old0, old1, old2, old3, old4, old5, old6, old7;
  llr_t new0, new1, new2, new3, new4, new5, new6, new7;
  llr_t m_b0, m_b1, m_b2, m_b3, m_b4,m_b5, m_b6, m_b7;
564
  llr_t m11,m10;
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
  llr_t beta_max;

  //  int m_max;

  //  llr_t max1, temp1, max2, temp2;

  //  int m_max1, m_max2;

  //
  // initialize the first stage;
  // for state for which alpha iz maximum, set log_beta to 0,
  // set the other ones to MAX.

  /*
  m_max1 = 0;
  max1 = alpha[((frame_length+3)*STATES)+0];
  temp1 = alpha[((frame_length+3)*STATES)+1];
  if (temp1 > max1) { m_max1 = 1; max1 = temp1;};
  temp1 = alpha[((frame_length+3)*STATES)+2];
  if (temp1 > max1) { m_max1 = 2; max1 = temp1;};
  temp1 = alpha[((frame_length+3)*STATES)+3];
  if (temp1 > max1) { m_max1 = 3; max1 = temp1;};
  m_max2 = 7; max2 = alpha[((frame_length+3)*STATES)+7];
  temp2 = alpha[((frame_length+3)*STATES)+6];
  if (temp2 > max2) { m_max2 = 6; max2 = temp2;};
  temp2 = alpha[((frame_length+3)*STATES)+5];
  if (temp2 > max2) { m_max2 = 5; max2 = temp2;};
  temp2 = alpha[((frame_length+3)*STATES)+4];
  if (temp2 > max2) { m_max2 = 4; max2 = temp2;};
  if (max2 > max1) m_max1 = m_max2;
595

596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687

  if (m_max1==0) old0=0; else old0 = -MAX/2;
  if (m_max1==1) old1=0; else old1 = -MAX/2;
  if (m_max1==2) old2=0; else old2 = -MAX/2;
  if (m_max1==3) old3=0; else old3 = -MAX/2;
  if (m_max1==4) old4=0; else old4 = -MAX/2;
  if (m_max1==5) old5=0; else old5 = -MAX/2;
  if (m_max1==6) old6=0; else old6 = -MAX/2;
  if (m_max1==7) old7=0; else old7 = -MAX/2;
  */

  /*
  // Initialise zero state because of termination
  beta[(STATES*(frame_length+3)) + 0] = 0;
  beta[(STATES*(frame_length+3)) + 1] = -MAX/2;
  beta[(STATES*(frame_length+3)) + 2] = -MAX/2;
  beta[(STATES*(frame_length+3)) + 3] = -MAX/2;
  beta[(STATES*(frame_length+3)) + 4] = -MAX/2;
  beta[(STATES*(frame_length+3)) + 5] = -MAX/2;
  beta[(STATES*(frame_length+3)) + 6] = -MAX/2;
  beta[(STATES*(frame_length+3)) + 7] = -MAX/2;

  for (k=0;k<F;k++)
    for (i=0;i<8;i++)
      beta[i+(k*8)] = beta[i];

  old0 = 0;
  old1 = -MAX/2;
  old2 = -MAX/2;
  old3 = -MAX/2;
  old4 = -MAX/2;
  old5 = -MAX/2;
  old6 = -MAX/2;
  old7 = -MAX/2;

  //
  // compute beta[k][m]
   //
  for (k=(frame_length+2);k>=frame_length;k--)
    {
      m11=m_11[k];
      m10=m_10[k];
      new0 = SAT_ADD(old0,M0T_TERM,MAX);
      new1 = SAT_ADD(old0,M1T_TERM,MAX);
      new2 = SAT_ADD(old1,M2T_TERM,MAX);
      new3 = SAT_ADD(old1,M3T_TERM,MAX);
      new4 = SAT_ADD(old2,M4T_TERM,MAX);
      new5 = SAT_ADD(old2,M5T_TERM,MAX);
      new6 = SAT_ADD(old3,M6T_TERM,MAX);
      new7 = SAT_ADD(old3,M7T_TERM,MAX);

      beta[k*STATES + 0] = new0;
      old0=new0;

      beta[k*STATES + 1] = new1;
      old1=new1;

      beta[k*STATES + 2] = new2;
      old2=new2;

      beta[k*STATES + 3] = new3;
      old3=new3;

      beta[k*STATES + 4] = new4;
      old4=new4;

      beta[k*STATES + 5] = new5;
      old5=new5;

      beta[k*STATES + 6] = new6;
      old6=new6;

      beta[k*STATES + 7] = new7;
      old7=new7;
    }
  */


  beta[(STATES*(frame_length)) + 0] = alpha[(STATES*frame_length) + 0];
  beta[(STATES*(frame_length)) + 1] = alpha[(STATES*frame_length) + 1];
  beta[(STATES*(frame_length)) + 2] = alpha[(STATES*frame_length) + 2];
  beta[(STATES*(frame_length)) + 3] = alpha[(STATES*frame_length) + 3];
  beta[(STATES*(frame_length)) + 4] = alpha[(STATES*frame_length) + 4];
  beta[(STATES*(frame_length)) + 5] = alpha[(STATES*frame_length) + 5];
  beta[(STATES*(frame_length)) + 6] = alpha[(STATES*frame_length) + 6];
  beta[(STATES*(frame_length)) + 7] = alpha[(STATES*frame_length) + 7];


  old0 = beta[(STATES*frame_length)+0];
  old1 = beta[(STATES*frame_length)+1];
  old2 = beta[(STATES*frame_length)+2];
  old3 = beta[(STATES*frame_length)+3];
688
  old4 = beta[(STATES*frame_length)+4];
689 690 691 692
  old5 = beta[(STATES*frame_length)+5];
  old6 = beta[(STATES*frame_length)+6];
  old7 = beta[(STATES*frame_length)+7];

693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
  for (k=(frame_length-1); k>=0; k--) {
    m11=m_11[k];
    m10=m_10[k];
    m_b0 = SAT_ADD(old4,M0B,MAX);
    m_b1 = SAT_ADD(old4,M1B,MAX);
    m_b2 = SAT_ADD(old5,M2B,MAX);
    m_b3 = SAT_ADD(old5,M3B,MAX);
    m_b4 = SAT_ADD(old6,M4B,MAX);
    m_b5 = SAT_ADD(old6,M5B,MAX);
    m_b6 = SAT_ADD(old7,M6B,MAX);
    m_b7 = SAT_ADD(old7,M7B,MAX);
    new0 = SAT_ADD(old0,M0T,MAX);
    new1 = SAT_ADD(old0,M1T,MAX);
    new2 = SAT_ADD(old1,M2T,MAX);
    new3 = SAT_ADD(old1,M3T,MAX);
    new4 = SAT_ADD(old2,M4T,MAX);
    new5 = SAT_ADD(old2,M5T,MAX);
    new6 = SAT_ADD(old3,M6T,MAX);
    new7 = SAT_ADD(old3,M7T,MAX);



    if (m_b0 > new0) new0=m_b0;

    beta[k*STATES + 0] = new0;
    old0=new0;

    if (m_b1 > new1) new1=m_b1;

    beta[k*STATES + 1] = new1;
    old1=new1;

    if (m_b2 > new2) new2=m_b2;

    beta[k*STATES + 2] = new2;
    old2=new2;

    if (m_b3 > new3) new3=m_b3;

    beta[k*STATES + 3] = new3;
    old3=new3;

    if (m_b4 > new4) new4=m_b4;

    beta[k*STATES + 4] = new4;
    old4=new4;

    if (m_b5 > new5) new5=m_b5;

    beta[k*STATES + 5] = new5;
    old5=new5;

    if (m_b6 > new6) new6=m_b6;

    beta[k*STATES + 6] = new6;
    old6=new6;

    if (m_b7 > new7) new7=m_b7;

    beta[k*STATES + 7] = new7;
    old7=new7;

    beta_max = beta[(STATES*k) + 0];

    if(beta[(STATES*k) + 1]>beta_max)
      beta_max = beta[(STATES*k) + 1];

    if(beta[(STATES*k) + 2]>beta_max)
      beta_max = beta[(STATES*k) + 2];

    if(beta[(STATES*k) + 3]>beta_max)
      beta_max = beta[(STATES*k) + 3];
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 798 799 800 801 802 803 804 805
    if(beta[(STATES*k) + 4]>beta_max)
      beta_max = beta[(STATES*k) + 4];

    if(beta[(STATES*k) + 5]>beta_max)
      beta_max = beta[(STATES*k) + 5];

    if(beta[(STATES*k) + 6]>beta_max)
      beta_max = beta[(STATES*k) + 6];

    if(beta[(STATES*k) + 7]>beta_max)
      beta_max = beta[(STATES*k) + 7];

    beta[(STATES*k)+0]-=beta_max;
    beta[(STATES*k)+1]-=beta_max;
    beta[(STATES*k)+2]-=beta_max;
    beta[(STATES*k)+3]-=beta_max;
    beta[(STATES*k)+4]-=beta_max;
    beta[(STATES*k)+5]-=beta_max;
    beta[(STATES*k)+6]-=beta_max;
    beta[(STATES*k)+7]-=beta_max;

    new0=beta[(STATES*k)+0];
    new1=beta[(STATES*k)+1];
    new2=beta[(STATES*k)+2];
    new3=beta[(STATES*k)+3];
    new4=beta[(STATES*k)+4];
    new5=beta[(STATES*k)+5];
    new6=beta[(STATES*k)+6];
    new7=beta[(STATES*k)+7];
    /*
    if (((k%(frame_length>>3))==0)&&(k>0)) {

    alpha[((k)*STATES)+0]=beta[((k)*STATES)+0]
    alpha[((k)*STATES)+1]=beta[((k)*STATES)+1];
    alpha[((k)*STATES)+2]=beta[((k)*STATES)+2];
    alpha[((k)*STATES)+3]=beta[((k)*STATES)+3];
    alpha[((k)*STATES)+4]=beta[((k)*STATES)+4];
    alpha[((k)*STATES)+5]=beta[((k)*STATES)+5];
    alpha[((k)*STATES)+6]=beta[((k)*STATES)+6];
    alpha[((k)*STATES)+7]=beta[((k)*STATES)+7];
806
    }
807 808
    */
  }
809 810 811 812 813 814 815
}
void compute_ext_s(llr_t* alpha,llr_t* beta,llr_t* m_11,llr_t* m_10,llr_t* ext, llr_t* systematic,unsigned short frame_length)
{
  int k;
  llr_t m11,m10;
  llr_t m00_1,m11_1,m00_2,m11_2,m00_3,m11_3,m00_4,m11_4;
  llr_t m01_1,m10_1,m01_2,m10_2,m01_3,m10_3,m01_4,m10_4;
816

817 818 819
  //
  // LLR computation
  //
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
  for (k=1; k<=(frame_length+3); k++) {
    m00_4 = ALPHA_BETA_4m00;
    m11_4 = ALPHA_BETA_4m11;
    m00_3 = ALPHA_BETA_3m00;
    m11_3 = ALPHA_BETA_3m11;
    m00_2 = ALPHA_BETA_2m00;
    m11_2 = ALPHA_BETA_2m11;
    m11_1 = ALPHA_BETA_1m11;
    m00_1 = ALPHA_BETA_1m00;
    m01_4 = ALPHA_BETA_4m01;
    m10_4 = ALPHA_BETA_4m10;
    m01_3 = ALPHA_BETA_3m01;
    m10_3 = ALPHA_BETA_3m10;
    m01_2 = ALPHA_BETA_2m01;
    m10_2 = ALPHA_BETA_2m10;
    m10_1 = ALPHA_BETA_1m10;
    m01_1 = ALPHA_BETA_1m01;

    if (m01_2 > m01_1) m01_1 = m01_2;

    if (m01_3 > m01_1) m01_1 = m01_3;

    if (m01_4 > m01_1) m01_1 = m01_4;

    if (m00_2 > m00_1) m00_1 = m00_2;

    if (m00_3 > m00_1) m00_1 = m00_3;

    if (m00_4 > m00_1) m00_1 = m00_4;

    if (m10_2 > m10_1) m10_1 = m10_2;

    if (m10_3 > m10_1) m10_1 = m10_3;

    if (m10_4 > m10_1) m10_1 = m10_4;

    if (m11_2 > m11_1) m11_1 = m11_2;

    if (m11_3 > m11_1) m11_1 = m11_3;

    if (m11_4 > m11_1) m11_1 = m11_4;

    m11=m_11[k-1];
    m10=m_10[k-1];
    m01_1 = SAT_ADD(m01_1,m01,MAX);
    m00_1 = SAT_ADD(m00_1,m00,MAX);
    m10_1 = SAT_ADD(m10_1,m10,MAX);
    m11_1 = SAT_ADD(m11_1,m11,MAX);

    if (m00_1 > m01_1) m01_1 = m00_1;

    if (m11_1 > m10_1) m10_1 = m11_1;

    ext[k-1] = SAT_ADD(m10_1,-SAT_ADD(m01_1,systematic[k-1],MAX),MAX);
874
#ifdef DEBUG_LOGMAP
875
    printf("Ext %d: m0 %d, m1 %d, syst %d ext %d\n",k-1,m01_1,m10_1,systematic[k-1],ext[k-1]);
876 877
#endif //DEBUG_LOGMAP

878
  };
879 880 881 882 883
}



unsigned char phy_threegpplte_turbo_decoder_scalar(llr_t *y,
884 885 886 887 888 889 890 891 892 893
    unsigned char *decoded_bytes,
    unsigned short n,
    unsigned short f1,
    unsigned short f2,
    unsigned char max_iterations,
    unsigned char crc_type,
    unsigned char F,
    unsigned char inst)
{

894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
  /*  y is a pointer to the input
    decoded_bytes is a pointer to the decoded output
    n is the size in bits of the coded block, with the tail */
  short ext[n],ext2[n];

  //  short systematic0[n],systematic1[n],systematic2[n],yparity1[n],yparity2[n];
  llr_t *yp = y;
  unsigned short i,pi;
  unsigned char iteration_cnt=0;
  unsigned int crc,oldcrc;
  unsigned char crc_len,temp;

  if (crc_type > 3) {
    msg("Illegal crc length!\n");
    return 255;
  }

  switch (crc_type) {
  case CRC24_A:
  case CRC24_B:
    crc_len=3;
    break;
916

917 918 919
  case CRC16:
    crc_len=2;
    break;
920

921 922 923
  case CRC8:
    crc_len=1;
    break;
924

925 926 927 928
  default:
    crc_len=3;
  }

929 930 931 932 933 934 935
  for (i=0; i<n; i++) {
    systematic0[i] = *yp;
    yp++;
    yparity1[i] = *yp;
    yp++;
    yparity2[i] = *yp;
    yp++;
936 937 938 939 940
#ifdef DEBUG_LOGMAP
    printf("Position %d: (%d,%d,%d)\n",i,systematic0[i],yparity1[i],yparity2[i]);
#endif //DEBUG_LOGMAP

  }
941 942 943 944 945 946

  for (i=n; i<n+3; i++) {
    systematic0[i]= *yp ;
    yp++;
    yparity1[i] = *yp;
    yp++;
947 948 949 950
#ifdef DEBUG_LOGMAP
    printf("Term 1 (%d): %d %d\n",i,systematic0[i],yparity1[i]);
#endif //DEBUG_LOGMAP
  }
951 952 953 954 955 956

  for (i=n+3; i<n+6; i++) {
    systematic0[i]= *yp ;
    yp++;
    yparity2[i-3] = *yp;
    yp++;
957 958 959 960
#ifdef DEBUG_LOGMAP
    printf("Term 2 (%d): %d %d\n",i-3,systematic0[i],yparity2[i-3]);
#endif //DEBUG_LOGMAP
  }
961

962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
#ifdef DEBUG_LOGMAP
  printf("\n");
#endif //DEBUG_LOGMAP



  // do log_map from first parity bit
  log_map_s(systematic0,yparity1,ext,n,0,F);


  while (iteration_cnt++ < max_iterations) {

#ifdef DEBUG_LOGMAP
    printf("\n*******************ITERATION %d\n\n",iteration_cnt);
#endif //DEBUG_LOGMAP

    threegpplte_interleaver_reset();
    pi=0;

    // compute input to second encoder by interleaving extrinsic info
982
    for (i=0; i<n; i++) { // steady-state portion
983 984 985
      systematic2[i] = (ext[pi] + systematic0[pi]);
      pi              = threegpplte_interleaver(f1,f2,n);
    }
986 987

    for (i=n; i<n+3; i++) { // termination
988 989
      systematic2[i] = (systematic0[i+8]);
    }
990 991

    // do log_map from second parity bit
992 993 994 995 996
    log_map_s(systematic2,yparity2,ext2,n,1,0);


    threegpplte_interleaver_reset();
    pi=0;
997 998

    for (i=0; i<n>>3; i++)
999
      decoded_bytes[i]=0;
1000

1001
    // compute input to first encoder and output
1002
    for (i=0; i<n; i++) {
1003 1004 1005
      systematic1[pi] = (ext2[i] + systematic0[pi]);
#ifdef DEBUG_LOGMAP
      printf("Second half %d: ext2[i] %d, systematic0[i] %d (e+s %d)\n",i,ext2[i],systematic0[pi],
1006
             ext2[i]+systematic2[i]);
1007 1008 1009
#endif //DEBUG_LOGMAP

      if ((systematic2[i] + ext2[i]) > 0)
1010
        decoded_bytes[pi>>3] += (1 << (7-(pi&7)));
1011 1012 1013

      pi              = threegpplte_interleaver(f1,f2,n);
    }
1014 1015

    for (i=n; i<n+3; i++) {
1016 1017 1018
      systematic1[i] = (systematic0[i]);
#ifdef DEBUG_LOGMAP
      printf("Second half %d: ext2[i] %d, systematic0[i] %d (e+s %d)\n",i,ext2[i],systematic0[i],
1019
             ext2[i]+systematic2[i]);
1020 1021
#endif //DEBUG_LOGMAP
    }
1022

1023 1024 1025 1026 1027 1028 1029

    // check status on output

    oldcrc= *((unsigned int *)(&decoded_bytes[(n>>3)-crc_len]));

    switch (crc_type) {

1030
    case CRC24_A:
1031 1032
      oldcrc&=0x00ffffff;
      crc = crc24a(&decoded_bytes[F>>3],
1033
                   n-24-F)>>8;
1034 1035 1036
      temp=((uint8_t *)&crc)[2];
      ((uint8_t *)&crc)[2] = ((uint8_t *)&crc)[0];
      ((uint8_t *)&crc)[0] = temp;
1037 1038 1039 1040

      //           msg("CRC24_A = %x, oldcrc = %x (F %d)\n",crc,oldcrc,F);

      break;
1041

1042 1043 1044
    case CRC24_B:
      oldcrc&=0x00ffffff;
      crc = crc24b(decoded_bytes,
1045
                   n-24)>>8;
1046 1047 1048
      temp=((uint8_t *)&crc)[2];
      ((uint8_t *)&crc)[2] = ((uint8_t *)&crc)[0];
      ((uint8_t *)&crc)[0] = temp;
1049 1050 1051 1052

      //      msg("CRC24_B = %x, oldcrc = %x\n",crc,oldcrc);

      break;
1053

1054 1055 1056
    case CRC16:
      oldcrc&=0x0000ffff;
      crc = crc16(decoded_bytes,
1057
                  n-16)>>16;
1058 1059

      break;
1060

1061 1062 1063
    case CRC8:
      oldcrc&=0x000000ff;
      crc = crc8(decoded_bytes,
1064
                 n-8)>>24;
1065 1066 1067 1068 1069 1070
      break;
    }

    if ((crc == oldcrc) && (crc!=0)) {
      return(iteration_cnt);
    }
1071

1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
    // do log_map from first parity bit
    if (iteration_cnt < max_iterations)
      log_map_s(systematic1,yparity1,ext,n,0,F);
  }

  return(iteration_cnt);
}

#ifdef TEST_DEBUG

int test_logmap8()
{
  unsigned char test[8];
  //_declspec(align(16))  char channel_output[512];
  //_declspec(align(16))  unsigned char output[512],decoded_output[16], *inPtr, *outPtr;

  short channel_output[512];
  unsigned char output[512],decoded_output[16];
  unsigned int i;

1092

1093 1094 1095 1096 1097 1098 1099 1100 1101
  test[0] = 7;
  test[1] = 0xa5;
  test[2] = 0;
  test[3] = 0xfe;
  test[4] = 0x1a;
  test[5] = 0x0;
  //  test[5] = 0x33;
  //  test[6] = 0x99;
  //  test[7] = 0;
1102

1103 1104

  threegpplte_turbo_encoder(test,
1105 1106 1107 1108
                            5,
                            output,
                            3,
                            10);
1109

1110
  for (i = 0; i < 132; i++) {
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
    channel_output[i] = 15*(2*output[i] - 1);
    //    printf("Position %d : %d\n",i,channel_output[i]);
  }

  memset(decoded_output,0,16);
  phy_threegpplte_turbo_decoder(channel_output,decoded_output,40,3,10,6,3);




}




1126 1127
void main()
{
1128 1129 1130 1131 1132 1133 1134 1135 1136


  test_logmap8();

}

#endif // TEST_DEBUG