nr_polar_encoder.c 24.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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.1  (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
 */

22 23
/*!\file PHY/CODING/nrPolar_tools/nr_polar_encoder.c
 * \brief
yilmazt's avatar
yilmazt committed
24
 * \author Raymond Knopp, Turker Yilmaz
25 26 27
 * \date 2018
 * \version 0.1
 * \company EURECOM
yilmazt's avatar
yilmazt committed
28
 * \email raymond.knopp@eurecom.fr, turker.yilmaz@eurecom.fr
29 30
 * \note
 * \warning
31
 */
32

Raymond Knopp's avatar
Raymond Knopp committed
33
//#define DEBUG_POLAR_ENCODER
Guy De Souza's avatar
Guy De Souza committed
34
//#define DEBUG_POLAR_ENCODER_DCI
35
//#define DEBUG_POLAR_MATLAB
yilmazt's avatar
yilmazt committed
36

Guy De Souza's avatar
Guy De Souza committed
37
#include "PHY/CODING/nrPolar_tools/nr_polar_defs.h"
38
#include "assertions.h"
yilmazt's avatar
yilmazt committed
39
#include <stdint.h>
Guy De Souza's avatar
Guy De Souza committed
40

yilmazt's avatar
yilmazt committed
41 42 43
//input  [a_31 a_30 ... a_0]
//output [f_31 f_30 ... f_0] [f_63 f_62 ... f_32] ...

44
void polar_encoder(uint32_t *in,
45 46
                   uint32_t *out,
                   t_nrPolar_params *polarParams) {
47
  if (1) {//polarParams->idx == 0 || polarParams->idx == 1) { //PBCH or PDCCH
48
    /*
49
    uint64_t B = (((uint64_t)*in)&((((uint64_t)1)<<32)-1)) | (((uint64_t)crc24c((uint8_t*)in,polarParams->payloadBits)>>8)<<polarParams->payloadBits);
50
    #ifdef DEBUG_POLAR_ENCODER
51
    printf("polar_B %llx (crc %x)\n",B,crc24c((uint8_t*)in,polarParams->payloadBits)>>8);
52 53
    #endif
    nr_bit2byte_uint32_8_t((uint32_t*)&B, polarParams->K, polarParams->nr_polar_B);*/
54
    nr_bit2byte_uint32_8(in, polarParams->payloadBits, polarParams->nr_polar_A);
55 56 57 58
    /*
     * Bytewise operations
     */
    //Calculate CRC.
59
    nr_matrix_multiplication_uint8_1D_uint8_2D(polarParams->nr_polar_A,
60 61 62 63
    										   polarParams->crc_generator_matrix,
											   polarParams->nr_polar_crc,
											   polarParams->payloadBits,
											   polarParams->crcParityBits);
64 65 66 67 68 69 70 71 72 73 74

    for (uint8_t i = 0; i < polarParams->crcParityBits; i++)
      polarParams->nr_polar_crc[i] = (polarParams->nr_polar_crc[i] % 2);

    //Attach CRC to the Transport Block. (a to b)
    for (uint16_t i = 0; i < polarParams->payloadBits; i++)
      polarParams->nr_polar_B[i] = polarParams->nr_polar_A[i];

    for (uint16_t i = polarParams->payloadBits; i < polarParams->K; i++)
      polarParams->nr_polar_B[i]= polarParams->nr_polar_crc[i-(polarParams->payloadBits)];

75
#ifdef DEBUG_POLAR_ENCODER
76
    uint64_t B2=0;
77 78 79

    for (int i = 0; i<polarParams->K; i++) B2 = B2 | ((uint64_t)polarParams->nr_polar_B[i] << i);

80
    printf("polar_B %llx\n",B2);
81 82
    for (int i=0; i< polarParams->payloadBits; i++) printf("a[%d]=%d\n", i, polarParams->nr_polar_A[i]);
    for (int i=0; i< polarParams->K; i++) printf("b[%d]=%d\n", i, polarParams->nr_polar_B[i]);
83
#endif
84
    /*    for (int j=0;j<polarParams->crcParityBits;j++) {
85 86
      for (int i=0;i<polarParams->payloadBits;i++)
    printf("%1d.%1d+",polarParams->crc_generator_matrix[i][j],polarParams->nr_polar_A[i]);
87 88
      printf(" => %d\n",polarParams->nr_polar_crc[j]);
      }*/
89 90 91 92 93
  } else { //UCI
  }

  //Interleaving (c to c')
  nr_polar_interleaver(polarParams->nr_polar_B,
94 95 96 97
                       polarParams->nr_polar_CPrime,
                       polarParams->interleaving_pattern,
                       polarParams->K);
#ifdef DEBUG_POLAR_ENCODER
98
  uint64_t Cprime=0;
99 100

  for (int i = 0; i<polarParams->K; i++) {
101
    Cprime = Cprime | ((uint64_t)polarParams->nr_polar_CPrime[i] << i);
102

103 104
    if (polarParams->nr_polar_CPrime[i] == 1) printf("pos %d : %llx\n",i,Cprime);
  }
105

106
  printf("polar_Cprime %llx\n",Cprime);
107
#endif
108 109
  //Bit insertion (c' to u)
  nr_polar_bit_insertion(polarParams->nr_polar_CPrime,
110 111 112 113 114 115
                         polarParams->nr_polar_U,
                         polarParams->N,
                         polarParams->K,
                         polarParams->Q_I_N,
                         polarParams->Q_PC_N,
                         polarParams->n_pc);
116
  //Encoding (u to d)
117 118 119
  /*  memset(polarParams->nr_polar_U,0,polarParams->N);
  polarParams->nr_polar_U[247]=1;
  polarParams->nr_polar_U[253]=1;*/
yilmazt's avatar
yilmazt committed
120
  nr_matrix_multiplication_uint8_1D_uint8_2D(polarParams->nr_polar_U,
121 122 123 124
		  	  	  	  	  	  	  	  	     polarParams->G_N,
											 polarParams->nr_polar_D,
											 polarParams->N,
											 polarParams->N);
125

126 127 128
  for (uint16_t i = 0; i < polarParams->N; i++)
    polarParams->nr_polar_D[i] = (polarParams->nr_polar_D[i] % 2);

129
  uint64_t D[8];
130
  memset((void *)D,0,8*sizeof(int64_t));
131
#ifdef DEBUG_POLAR_ENCODER
132 133 134

  for (int i=0; i<polarParams->N; i++)  D[i/64] |= ((uint64_t)polarParams->nr_polar_D[i])<<(i&63);

135
  printf("D %llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx\n",
136
         D[0],D[1],D[2],D[3],D[4],D[5],D[6],D[7]);
137
#endif
138 139 140
  //Rate matching
  //Sub-block interleaving (d to y) and Bit selection (y to e)
  nr_polar_interleaver(polarParams->nr_polar_D,
141 142 143
                       polarParams->nr_polar_E,
                       polarParams->rate_matching_pattern,
                       polarParams->encoderLength);
144 145 146
  /*
   * Return bits.
   */
yilmazt's avatar
yilmazt committed
147 148
#ifdef DEBUG_POLAR_ENCODER

149 150 151
  for (int i=0; i< polarParams->encoderLength; i++) printf("f[%d]=%d\n", i, polarParams->nr_polar_E[i]);

#endif
152
  nr_byte2bit_uint8_32(polarParams->nr_polar_E, polarParams->encoderLength, out);
yilmazt's avatar
yilmazt committed
153 154 155
}

void polar_encoder_dci(uint32_t *in,
156 157 158
                       uint32_t *out,
                       t_nrPolar_params *polarParams,
                       uint16_t n_RNTI) {
yilmazt's avatar
yilmazt committed
159
#ifdef DEBUG_POLAR_ENCODER_DCI
160
  printf("[polar_encoder_dci] in: [0]->0x%08x \t [1]->0x%08x \t [2]->0x%08x \t [3]->0x%08x\n", in[0], in[1], in[2], in[3]);
yilmazt's avatar
yilmazt committed
161
#endif
162 163 164 165
  /*
   * Bytewise operations
   */
  //(a to a')
166
  nr_bit2byte_uint32_8(in, polarParams->payloadBits, polarParams->nr_polar_A);
167

168
  for (int i=0; i<polarParams->crcParityBits; i++) polarParams->nr_polar_APrime[i]=1;
169

170
  for (int i=0; i<polarParams->payloadBits; i++) polarParams->nr_polar_APrime[i+(polarParams->crcParityBits)]=polarParams->nr_polar_A[i];
171

172
#ifdef DEBUG_POLAR_ENCODER_DCI
173 174 175
  printf("[polar_encoder_dci] A: ");
  for (int i=0; i<polarParams->payloadBits; i++) printf("%d-", polarParams->nr_polar_A[i]);
  printf("\n");
176

177
  printf("[polar_encoder_dci] APrime: ");
178 179
  for (int i=0; i<polarParams->K; i++) printf("%d-", polarParams->nr_polar_APrime[i]);
  printf("\n");
180

181
  printf("[polar_encoder_dci] GP: ");
182 183
  for (int i=0; i<polarParams->crcParityBits; i++) printf("%d-", polarParams->crc_generator_matrix[0][i]);
  printf("\n");
184
#endif
185
  //Calculate CRC.
yilmazt's avatar
yilmazt committed
186
  nr_matrix_multiplication_uint8_1D_uint8_2D(polarParams->nr_polar_APrime,
187 188 189 190
		  	  	  	  	  	  	  	  	  	 polarParams->crc_generator_matrix,
											 polarParams->nr_polar_crc,
											 polarParams->K,
											 polarParams->crcParityBits);
191

192
  for (uint8_t i = 0; i < polarParams->crcParityBits; i++) polarParams->nr_polar_crc[i] = (polarParams->nr_polar_crc[i] % 2);
193

194
#ifdef DEBUG_POLAR_ENCODER_DCI
195 196 197
  printf("[polar_encoder_dci] CRC: ");
  for (int i=0; i<polarParams->crcParityBits; i++) printf("%d-", polarParams->nr_polar_crc[i]);
  printf("\n");
198 199
#endif

200 201 202
  //Attach CRC to the Transport Block. (a to b)
  for (uint16_t i = 0; i < polarParams->payloadBits; i++)
    polarParams->nr_polar_B[i] = polarParams->nr_polar_A[i];
203

204 205
  for (uint16_t i = polarParams->payloadBits; i < polarParams->K; i++)
    polarParams->nr_polar_B[i]= polarParams->nr_polar_crc[i-(polarParams->payloadBits)];
206

207
  //Scrambling (b to c)
208 209
  for (int i=0; i<16; i++)
	polarParams->nr_polar_B[polarParams->payloadBits+8+i]=( polarParams->nr_polar_B[polarParams->payloadBits+8+i] + ((n_RNTI>>(15-i))&1) ) % 2;
210

yilmazt's avatar
yilmazt committed
211
#ifdef DEBUG_POLAR_ENCODER_DCI
212 213 214
  printf("[polar_encoder_dci] B: ");
  for (int i = 0; i < polarParams->K; i++) printf("%d-", polarParams->nr_polar_B[i]);
  printf("\n");
yilmazt's avatar
yilmazt committed
215
#endif
216
  //Interleaving (c to c')
217
  nr_polar_interleaver(polarParams->nr_polar_B, polarParams->nr_polar_CPrime, polarParams->interleaving_pattern, polarParams->K);
218 219
  //Bit insertion (c' to u)
  nr_polar_bit_insertion(polarParams->nr_polar_CPrime,
220 221 222 223 224 225
                         polarParams->nr_polar_U,
                         polarParams->N,
                         polarParams->K,
                         polarParams->Q_I_N,
                         polarParams->Q_PC_N,
                         polarParams->n_pc);
226
  //Encoding (u to d)
yilmazt's avatar
yilmazt committed
227
  nr_matrix_multiplication_uint8_1D_uint8_2D(polarParams->nr_polar_U,
228 229 230 231 232
		  	  	  	  	  	  	  	  	  	 polarParams->G_N,
											 polarParams->nr_polar_D,
											 polarParams->N,
											 polarParams->N);
  for (uint16_t i = 0; i < polarParams->N; i++) polarParams->nr_polar_D[i] = (polarParams->nr_polar_D[i] % 2);
233 234 235 236

  //Rate matching
  //Sub-block interleaving (d to y) and Bit selection (y to e)
  nr_polar_interleaver(polarParams->nr_polar_D,
237 238 239
                       polarParams->nr_polar_E,
                       polarParams->rate_matching_pattern,
                       polarParams->encoderLength);
240 241 242
  /*
   * Return bits.
   */
243
  nr_byte2bit_uint8_32(polarParams->nr_polar_E, polarParams->encoderLength, out);
yilmazt's avatar
yilmazt committed
244
#ifdef DEBUG_POLAR_ENCODER_DCI
245 246
  printf("[polar_encoder_dci] E: ");
  for (int i = 0; i < polarParams->encoderLength; i++) printf("%d-", polarParams->nr_polar_E[i]);
247

248 249
  uint8_t outputInd = ceil(polarParams->encoderLength / 32.0);
  printf("\n[polar_encoder_dci] out: ");
250
  for (int i = 0; i < outputInd; i++) printf("[%d]->0x%08x\t", i, out[i]);
yilmazt's avatar
yilmazt committed
251
#endif
Guy De Souza's avatar
Guy De Souza committed
252
}
yilmazt's avatar
yilmazt committed
253

254
static inline void polar_rate_matching(t_nrPolar_params *polarParams,void *in,void *out) __attribute__((always_inline));
255

256
static inline void polar_rate_matching(t_nrPolar_params *polarParams,void *in,void *out) {
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
  // handle rate matching with a single 128 bit word using bit shuffling
  // can be done with SIMD intrisics if needed
  if (polarParams->groupsize < 8)  {
    AssertFatal(polarParams->encoderLength<=128,"Need to handle groupsize<8 and N>128\n");
    uint128_t *out128=(uint128_t*)out;
    uint128_t *in128=(uint128_t*)in;
    *out128=0;
    uint128_t tmp0;
#ifdef DEBUG_POLAR_ENCODER
    uint128_t tmp1;
#endif
    for (int i=0; i<polarParams->encoderLength; i++) {
#ifdef DEBUG_POLAR_ENCODER
      printf("%u<-%u : %llx.%llx =>",i,polarParams->rate_matching_pattern[i],((uint64_t *)out)[1],((uint64_t *)out)[0]);
#endif
      tmp0 = (*in128&(((uint128_t)1)<<polarParams->rate_matching_pattern[i]));
      if (tmp0!=0) {
	*out128 = *out128 | ((uint128_t)1)<<i;
#ifdef DEBUG_POLAR_ENCODER
	tmp1 = ((uint128_t)1)<<i;
	printf("%llx.%llx<->%llx.%llx => %llx.%llx\n",
	       ((uint64_t *)&tmp0)[1],((uint64_t *)&tmp0)[0],
	       ((uint64_t *)&tmp1)[1],((uint64_t *)&tmp1)[0],
	       ((uint64_t *)out)[1],((uint64_t *)out)[0]);
#endif
      }
    }

  }					     
  // These are based on LUTs for byte and short word groups
  else if (polarParams->groupsize == 8)
289
    for (int i=0; i<polarParams->encoderLength>>3; i++) ((uint8_t *)out)[i] = ((uint8_t *)in)[polarParams->rm_tab[i]];
290
  else // groupsize==16
291 292
    for (int i=0; i<polarParams->encoderLength>>4; i++) {
      ((uint16_t *)out)[i] = ((uint16_t *)in)[polarParams->rm_tab[i]];
293
    }
294
}
295

296
void build_polar_tables(t_nrPolar_params *polarParams) {
297 298
  // build table b -> c'
  AssertFatal(polarParams->K > 32, "K = %d < 33, is not supported yet\n",polarParams->K);
299
  AssertFatal(polarParams->K < 129, "K = %d > 64, is not supported yet\n",polarParams->K);
300 301 302 303
  int bit_i,ip;
  int numbytes = polarParams->K>>3;
  int residue = polarParams->K&7;
  int numbits;
304

305
  if (residue>0) numbytes++;
306 307

  for (int byte=0; byte<numbytes; byte++) {
308 309
    if (byte<(polarParams->K>>3)) numbits=8;
    else numbits=residue;
310 311

    for (int val=0; val<256; val++) {
312 313
      polarParams->cprime_tab0[byte][val] = 0;
      polarParams->cprime_tab1[byte][val] = 0;
314 315 316 317 318 319 320 321 322

      for (int i=0; i<numbits; i++) {
        // flip bit endian of B bitstring
        ip=polarParams->deinterleaving_pattern[polarParams->K-1-((8*byte)+i)];
        AssertFatal(ip<128,"ip = %d\n",ip);
        bit_i=(val>>i)&1;

        if (ip<64) polarParams->cprime_tab0[byte][val] |= (((uint64_t)bit_i)<<ip);
        else       polarParams->cprime_tab1[byte][val] |= (((uint64_t)bit_i)<<(ip&63));
323 324 325
      }
    }
  }
326

327
  AssertFatal(polarParams->N==512 || polarParams->N==256 || polarParams->N==128,"N = %d, not done yet\n",polarParams->N);
328 329
  // build G bit vectors for information bit positions and convert the bit as bytes tables in nr_polar_kronecker_power_matrices.c to 64 bit packed vectors.
  // keep only rows of G which correspond to information/crc bits
330
  polarParams->G_N_tab = (uint64_t **)malloc(polarParams->K * sizeof(int64_t *));
331
  int k=0;
332 333

  for (int i=0; i<polarParams->N; i++) {
334
    if (polarParams->information_bit_pattern[i] > 0) {
335 336 337 338 339 340
      polarParams->G_N_tab[k] = (uint64_t *)memalign(32,(polarParams->N/64)*sizeof(uint64_t));
      memset((void *)polarParams->G_N_tab[k],0,(polarParams->N/64)*sizeof(uint64_t));

      for (int j=0; j<polarParams->N; j++)
        polarParams->G_N_tab[k][j/64] |= ((uint64_t)polarParams->G_N[i][j])<<(j&63);

341 342
#ifdef DEBUG_POLAR_ENCODER
      printf("Bit %d Selecting row %d of G : ",k,i);
343 344 345

      for (int j=0; j<polarParams->N; j+=4) printf("%1x",polarParams->G_N[i][j]+(polarParams->G_N[i][j+1]*2)+(polarParams->G_N[i][j+2]*4)+(polarParams->G_N[i][j+3]*8));

346 347 348 349 350 351 352 353 354 355
      printf("\n");
#endif
      k++;
    }
  }

  // rate matching table
  int iplast=polarParams->rate_matching_pattern[0];
  int ccnt=0;
  int groupcnt=0;
356
#ifdef DEBUG_POLAR_ENCODER
357 358
  int firstingroup_out=0;
  int firstingroup_in=iplast;
359
#endif
360
  int mingroupsize = 1024;
361

362 363 364
  // compute minimum group size of rate-matching pattern
  for (int outpos=1; outpos<polarParams->encoderLength; outpos++) {
    ip=polarParams->rate_matching_pattern[outpos];
365 366 367
#ifdef DEBUG_POLAR_ENCODER
    printf("rm: outpos %d, inpos %d\n",outpos,ip);
#endif
368 369 370
    if ((ip - iplast) == 1) ccnt++;
    else {
      groupcnt++;
Raymond Knopp's avatar
Raymond Knopp committed
371
#ifdef DEBUG_POLAR_ENCODER
372
      printf("group %d (size %d): (%d:%d) => (%d:%d)\n",groupcnt,ccnt+1,
373 374
             firstingroup_in,firstingroup_in+ccnt,
             firstingroup_out,firstingroup_out+ccnt);
Raymond Knopp's avatar
Raymond Knopp committed
375
#endif
376

377
      if ((ccnt+1)<mingroupsize) mingroupsize=ccnt+1;
378

379
      ccnt=0;
380
#ifdef DEBUG_POLAR_ENCODER
381 382
      firstingroup_out=outpos;
      firstingroup_in=ip;
383
#endif
384
    }
385

386
    iplast=ip;
387
  }
388 389 390 391 392 393 394 395
  groupcnt++;
  if ((ccnt+1)<mingroupsize) mingroupsize=ccnt+1;
#ifdef DEBUG_POLAR_ENCODER
  printf("group %d (size %d): (%d:%d) => (%d:%d)\n",groupcnt,ccnt+1,
             firstingroup_in,firstingroup_in+ccnt,
             firstingroup_out,firstingroup_out+ccnt);
#endif
  AssertFatal(mingroupsize==4 || mingroupsize==8 || mingroupsize==16,"mingroupsize %d, needs to be handled\n",mingroupsize);
396 397
  polarParams->groupsize=mingroupsize;
  int shift=3;
398

399
  if (mingroupsize == 16) shift=4;
400
  else if (mingroupsize == 4) shift=2;
401 402

  polarParams->rm_tab=(int *)malloc(sizeof(int)*polarParams->encoderLength/mingroupsize);
403
  // rerun again to create groups
yilmazt's avatar
yilmazt committed
404
  int tcnt=0;
405 406

  for (int outpos=0; outpos<polarParams->encoderLength; outpos+=mingroupsize,tcnt++)
407
    polarParams->rm_tab[tcnt] = polarParams->rate_matching_pattern[outpos]>>shift;
408
}
yilmazt's avatar
yilmazt committed
409

yilmazt's avatar
yilmazt committed
410
void polar_encoder_fast(uint64_t *A,
411 412 413
                        uint32_t *out,
                        int32_t crcmask,
                        t_nrPolar_params *polarParams) {
414
  AssertFatal(polarParams->K > 32, "K = %d < 33, is not supported yet\n",polarParams->K);
415 416
  AssertFatal(polarParams->K < 129, "K = %d > 128, is not supported yet\n",polarParams->K);
  AssertFatal(polarParams->payloadBits < 65, "payload bits = %d > 64, is not supported yet\n",polarParams->payloadBits);
417
  uint64_t B[4]= {0,0,0,0},Cprime[4]= {0,0,0,0};
418
  int bitlen = polarParams->payloadBits;
419 420
  // append crc
  AssertFatal(bitlen<129,"support for payloads <= 128 bits\n");
421
  AssertFatal(polarParams->crcParityBits == 24,"support for 24-bit crc only for now\n");
422
  //int bitlen0=bitlen;
423 424
  uint64_t tcrc=0;

425 426
  // A bit string should be stored as 0, 0, ..., 0, a'_0, a'_1, ..., a'_A-1,
  //???a'_{N-1} a'_{N-2} ... a'_{N-A} 0 .... 0, where N=64,128,192,..., N is smallest multiple of 64 greater than or equal to A
427 428

  // First flip A bitstring byte endian for CRC routines (optimized for DLSCH/ULSCH, not PBCH/PDCCH)
429
  // CRC reads in each byte in bit positions 7 down to 0, for PBCH/PDCCH we need to read in a_{A-1} down to a_{0}, A = length of bit string (e.g. 32 for PBCH)
430 431 432
  if (bitlen<=32) {
    uint8_t A32_flip[4];
    uint32_t Aprime= (uint32_t)(((uint32_t)*A)<<(32-bitlen));
433 434 435 436
    A32_flip[0]=((uint8_t *)&Aprime)[3];
    A32_flip[1]=((uint8_t *)&Aprime)[2];
    A32_flip[2]=((uint8_t *)&Aprime)[1];
    A32_flip[3]=((uint8_t *)&Aprime)[0];
437
    tcrc = (uint64_t)((crcmask^(crc24c(A32_flip,bitlen)>>8)));
438
  } else if (bitlen<=64) {
439
    uint8_t A64_flip[8];
440
    uint64_t Aprime= (uint32_t)(((uint64_t)*A)<<(64-bitlen));
441 442 443 444 445 446 447 448
    A64_flip[0]=((uint8_t *)&Aprime)[7];
    A64_flip[1]=((uint8_t *)&Aprime)[6];
    A64_flip[2]=((uint8_t *)&Aprime)[5];
    A64_flip[3]=((uint8_t *)&Aprime)[4];
    A64_flip[4]=((uint8_t *)&Aprime)[3];
    A64_flip[5]=((uint8_t *)&Aprime)[2];
    A64_flip[6]=((uint8_t *)&Aprime)[1];
    A64_flip[7]=((uint8_t *)&Aprime)[0];
449 450
    tcrc = (uint64_t)((crcmask^(crc24c(A64_flip,bitlen)>>8)));
  }
451
  else if (bitlen<=128) {
452 453 454 455 456 457 458 459 460 461 462
    uint8_t A128_flip[16];
    uint128_t Aprime= (uint32_t)(((uint128_t)*A)<<(128-bitlen));
    A128_flip[0]=((uint8_t*)&Aprime)[15];	  A128_flip[1]=((uint8_t*)&Aprime)[14];
    A128_flip[2]=((uint8_t*)&Aprime)[13];	  A128_flip[3]=((uint8_t*)&Aprime)[12];
    A128_flip[4]=((uint8_t*)&Aprime)[11];	  A128_flip[5]=((uint8_t*)&Aprime)[10];
    A128_flip[6] =((uint8_t*)&Aprime)[9];	  A128_flip[7] =((uint8_t*)&Aprime)[8];
    A128_flip[8] =((uint8_t*)&Aprime)[7];	  A128_flip[9] =((uint8_t*)&Aprime)[6];
    A128_flip[10]=((uint8_t*)&Aprime)[5];	  A128_flip[11]=((uint8_t*)&Aprime)[4];
    A128_flip[12]=((uint8_t*)&Aprime)[3];	  A128_flip[13]=((uint8_t*)&Aprime)[2];
    A128_flip[14]=((uint8_t*)&Aprime)[1];	  A128_flip[15]=((uint8_t*)&Aprime)[0];
    tcrc = (uint64_t)((crcmask^(crc24c(A128_flip,bitlen)>>8)));
463
  }
464

465
  int n;
466 467
  // this is number of quadwords in the bit string
  int quadwlen = (polarParams->K>>6);
468

469 470
  if ((polarParams->K&63) > 0) quadwlen++;

471 472 473 474 475
  // Create the B bit string as
  // 0, 0, ..., 0, a'_0, a'_1, ..., a'_A-1, p_0, p_1, ..., p_{N_parity-1}

  //??? b_{N'-1} b_{N'-2} ... b_{N'-A} b_{N'-A-1} ... b_{N'-A-Nparity} = a_{N-1} a_{N-2} ... a_{N-A} p_{N_parity-1} ... p_0

476

477
  for (n=0; n<quadwlen; n++) if (n==0) B[n] = (A[n] << polarParams->crcParityBits) | tcrc;
478
                             else      B[n] = (A[n] << polarParams->crcParityBits) | (A[n-1]>>(64-polarParams->crcParityBits));
479 480

  uint8_t *Bbyte = (uint8_t *)B;
481

482
  // for each byte of B, lookup in corresponding table for 64-bit word corresponding to that byte and its position
483
  if (polarParams->K<65)
484 485 486 487 488 489 490 491
    Cprime[0] = polarParams->cprime_tab0[0][Bbyte[0]] |
                polarParams->cprime_tab0[1][Bbyte[1]] |
                polarParams->cprime_tab0[2][Bbyte[2]] |
                polarParams->cprime_tab0[3][Bbyte[3]] |
                polarParams->cprime_tab0[4][Bbyte[4]] |
                polarParams->cprime_tab0[5][Bbyte[5]] |
                polarParams->cprime_tab0[6][Bbyte[6]] |
                polarParams->cprime_tab0[7][Bbyte[7]];
492
  else if (polarParams->K < 129) {
493
    for (int i=0; i<1+(polarParams->K/8); i++) {
494 495 496
      Cprime[0] |= polarParams->cprime_tab0[i][Bbyte[i]];
      Cprime[1] |= polarParams->cprime_tab1[i][Bbyte[i]];
    }
497
  }
498

499 500 501 502 503
#ifdef DEBUG_POLAR_MATLAB
  // Cprime = pbchCprime
  for (int i = 0; i < quadwlen; i++) printf("[polar_encoder_fast]C'[%d]= 0x%llx\n", i, (unsigned long long)(Cprime[i]));
#endif

504
#ifdef DEBUG_POLAR_ENCODER
505 506

  if (polarParams->K<65)
507
    printf("A %llx B %llx Cprime %llx (payload bits %d,crc %x)\n",
508 509 510 511 512
           (unsigned long long)(A[0]&(((uint64_t)1<<bitlen)-1)),
           (unsigned long long)(B[0]),
           (unsigned long long)(Cprime[0]),
           polarParams->payloadBits,
           tcrc);
513 514 515
  else if (polarParams->K<129) {
    if (bitlen<64)
      printf("A %llx B %llx|%llx Cprime %llx|%llx (payload bits %d,crc %x)\n",
516 517 518 519 520 521
             (unsigned long long)(A[0]&(((uint64_t)1<<bitlen)-1)),
             (unsigned long long)(B[1]),(unsigned long long)(B[0]),
             (unsigned long long)(Cprime[1]),(unsigned long long)(Cprime[0]),
             polarParams->payloadBits,
             tcrc);
    else
522
      printf("A %llx|%llx B %llx|%llx Cprime %llx|%llx (payload bits %d,crc %x)\n",
523 524 525 526 527
             (unsigned long long)(A[1]&(((uint64_t)1<<(bitlen-64))-1)),(unsigned long long)(A[0]),
             (unsigned long long)(B[1]),(unsigned long long)(B[0]),
             (unsigned long long)(Cprime[1]),(unsigned long long)(Cprime[0]),
             polarParams->payloadBits,
             crc24c((uint8_t *)A,bitlen)>>8);
528
  }
529

530 531
#endif
  /*  printf("Bbytes : %x.%x.%x.%x.%x.%x.%x.%x\n",Bbyte[0],Bbyte[1],Bbyte[2],Bbyte[3],Bbyte[4],Bbyte[5],Bbyte[6],Bbyte[7]);
532 533 534 535 536 537 538 539
  printf("%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx\n",polarParams->cprime_tab[0][Bbyte[0]] ,
      polarParams->cprime_tab[1][Bbyte[1]] ,
      polarParams->cprime_tab[2][Bbyte[2]] ,
      polarParams->cprime_tab[3][Bbyte[3]] ,
      polarParams->cprime_tab[4][Bbyte[4]] ,
      polarParams->cprime_tab[5][Bbyte[5]] ,
      polarParams->cprime_tab[6][Bbyte[6]] ,
      polarParams->cprime_tab[7][Bbyte[7]]);*/
540 541
  // now do Gu product (here using 64-bit XORs, we can also do with SIMD after)
  // here we're reading out the bits LSB -> MSB, is this correct w.r.t. 3GPP ?
542
  uint64_t Cprime_i;
543
  /*  printf("%llx Cprime_0 (%llx) G %llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx\n",Cprime_i,Cprime &1,
544 545 546 547 548 549 550 551 552
   polarParams->G_N_tab[0][0],
   polarParams->G_N_tab[0][1],
   polarParams->G_N_tab[0][2],
   polarParams->G_N_tab[0][3],
   polarParams->G_N_tab[0][4],
   polarParams->G_N_tab[0][5],
   polarParams->G_N_tab[0][6],
   polarParams->G_N_tab[0][7]);*/
  uint64_t D[8]= {0,0,0,0,0,0,0,0};
553 554
  int off=0;
  int len=polarParams->K;
555

556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
  if (polarParams->N==512) {
    for (int j=0; j<(1+(polarParams->K>>6)); j++,off+=64,len-=64) {
      for (int i=0; i<((len>63) ? 64 : len); i++) {
	Cprime_i = -((Cprime[j]>>i)&1); // this converts bit 0 as, 0 => 0000x00, 1 => 1111x11
	/*
	  #ifdef DEBUG_POLAR_ENCODER
	  printf("%llx Cprime_%d (%llx) G %llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx\n",
	  Cprime_i,off+i,(Cprime[j]>>i) &1,
	  polarParams->G_N_tab[off+i][0],
	  polarParams->G_N_tab[off+i][1],
	  polarParams->G_N_tab[off+i][2],
	  polarParams->G_N_tab[off+i][3],
	  polarParams->G_N_tab[off+i][4],
	  polarParams->G_N_tab[off+i][5],
	  polarParams->G_N_tab[off+i][6],
	  polarParams->G_N_tab[off+i][7]);
	  #endif
	*/
	uint64_t *Gi=polarParams->G_N_tab[off+i];
	D[0] ^= (Cprime_i & Gi[0]);
	D[1] ^= (Cprime_i & Gi[1]);
	D[2] ^= (Cprime_i & Gi[2]);
	D[3] ^= (Cprime_i & Gi[3]);
	D[4] ^= (Cprime_i & Gi[4]);
	D[5] ^= (Cprime_i & Gi[5]);
	D[6] ^= (Cprime_i & Gi[6]);
	D[7] ^= (Cprime_i & Gi[7]);
      
#ifdef DEBUG_POLAR_ENCODER
	printf("D %llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx\n",
	       D[0],
	       D[1],
	       D[2],
	       D[3],
	       D[4],
	       D[5],
	       D[6],
	       D[7]);
#endif
      }
596
    }
597
  }
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
  else if (polarParams->N==256) {
    for (int j=0; j<(1+(polarParams->K>>6)); j++,off+=64,len-=64) {
      for (int i=0; i<((len>63) ? 64 : len); i++) {
	Cprime_i = -((Cprime[j]>>i)&1); // this converts bit 0 as, 0 => 0000x00, 1 => 1111x11
	/*
	  #ifdef DEBUG_POLAR_ENCODER
	  printf("%llx Cprime_%d (%llx) G %llx,%llx,%llx,%llx\n",
	  Cprime_i,off+i,(Cprime[j]>>i) &1,
	  polarParams->G_N_tab[off+i][0],
	  polarParams->G_N_tab[off+i][1],
	  polarParams->G_N_tab[off+i][2],
	  polarParams->G_N_tab[off+i][3],
	  #endif
	*/
	uint64_t *Gi=polarParams->G_N_tab[off+i];
	D[0] ^= (Cprime_i & Gi[0]);
	D[1] ^= (Cprime_i & Gi[1]);
	D[2] ^= (Cprime_i & Gi[2]);
	D[3] ^= (Cprime_i & Gi[3]);
      
	
619
#ifdef DEBUG_POLAR_ENCODER
620 621 622 623 624
	printf("D %llx,%llx,%llx,%llx\n",
	  D[0],
	  D[1],
	  D[2],
	  D[3]);
625
#endif
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
      }
    }
  }
  else if (polarParams->N==128) {
    for (int j=0; j<(1+(polarParams->K>>6)); j++,off+=64,len-=64) {
      for (int i=0; i<((len>63) ? 64 : len); i++) {
	Cprime_i = -((Cprime[j]>>i)&1); // this converts bit 0 as, 0 => 0000x00, 1 => 1111x11
	
	  #ifdef DEBUG_POLAR_ENCODER
	  printf("%llx Cprime_%d (%d+%d) (%llx) G %llx,%llx\n",
	  Cprime_i,off+i,off,i,(Cprime[j]>>i) &1,
	  polarParams->G_N_tab[off+i][0],
	  polarParams->G_N_tab[off+i][1]);
	  #endif
	
	uint64_t *Gi=polarParams->G_N_tab[off+i];
	D[0] ^= (Cprime_i & Gi[0]);
	D[1] ^= (Cprime_i & Gi[1]);
     
      
#ifdef DEBUG_POLAR_ENCODER
	printf("D %llx,%llx\n",
	  D[0],
	  D[1]);
650
#endif
651 652 653 654
      }
    }
  }
  memset((void*)out,0,polarParams->encoderLength>>3);
655
  polar_rate_matching(polarParams,(void *)D,(void *)out);
yilmazt's avatar
yilmazt committed
656
}