nr_polar_decoder.c 41.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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.
 *-------------------------------------------------------------------------------
18
 * For more information about the OpenAirInterface (OAI) Software Alliance
19 20
 *      contact@openairinterface.org
 */
Guy De Souza's avatar
Guy De Souza committed
21

22 23 24 25 26 27 28 29 30 31 32
/*!\file PHY/CODING/nrPolar_tools/nr_polar_decoder.c
 * \brief
 * \author Turker Yilmaz
 * \date 2018
 * \version 0.1
 * \company EURECOM
 * \email turker.yilmaz@eurecom.fr
 * \note
 * \warning
*/

Guy De Souza's avatar
Guy De Souza committed
33 34 35 36 37 38
/*
 * Return values:
 *  0 --> Success
 * -1 --> All list entries have failed the CRC checks
 */

39
#include "PHY/CODING/nrPolar_tools/nr_polar_defs.h"
40
#include "assertions.h"
41

42 43
int8_t polar_decoder(
		double *input,
hongzhi wang's avatar
hongzhi wang committed
44
		uint8_t *out,
yilmazt's avatar
yilmazt committed
45
		t_nrPolar_paramsPtr polarParams,
46 47 48
		uint8_t listSize,
		uint8_t pathMetricAppr)
{
49 50
	//Assumes no a priori knowledge.

yilmazt's avatar
yilmazt committed
51 52 53
	uint8_t ***bit = nr_alloc_uint8_3D_array(polarParams->N, (polarParams->n+1), 2*listSize);
	uint8_t **bitUpdated = nr_alloc_uint8_2D_array(polarParams->N, (polarParams->n+1)); //0=False, 1=True
	uint8_t **llrUpdated = nr_alloc_uint8_2D_array(polarParams->N, (polarParams->n+1)); //0=False, 1=True
54
	double ***llr = nr_alloc_double_3D_array(polarParams->N, (polarParams->n+1), 2*listSize);
yilmazt's avatar
yilmazt committed
55
	uint8_t **crcChecksum = nr_alloc_uint8_2D_array(polarParams->crcParityBits, 2*listSize);
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 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 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
	double *pathMetric = malloc(sizeof(double)*(2*listSize));
	uint8_t *crcState = malloc(sizeof(uint8_t)*(2*listSize)); //0=False, 1=True

	for (int i=0; i<(2*listSize); i++) {
		pathMetric[i] = 0;
		crcState[i]=1;
	}
	for (int i=0; i<polarParams->N; i++) {
		llrUpdated[i][polarParams->n]=1;
		bitUpdated[i][0]=((polarParams->information_bit_pattern[i]+1) % 2);
	}

	uint8_t **extended_crc_generator_matrix = malloc(polarParams->K * sizeof(uint8_t *)); //G_P3
	uint8_t **tempECGM = malloc(polarParams->K * sizeof(uint8_t *)); //G_P2
	for (int i = 0; i < polarParams->K; i++){
		extended_crc_generator_matrix[i] = malloc(polarParams->crcParityBits * sizeof(uint8_t));
		tempECGM[i] = malloc(polarParams->crcParityBits * sizeof(uint8_t));
	}

	for (int i=0; i<polarParams->payloadBits; i++) {
		for (int j=0; j<polarParams->crcParityBits; j++) {
			tempECGM[i][j]=polarParams->crc_generator_matrix[i][j];
		}
	}
	for (int i=polarParams->payloadBits; i<polarParams->K; i++) {
			for (int j=0; j<polarParams->crcParityBits; j++) {
				if( (i-polarParams->payloadBits) == j ){
					tempECGM[i][j]=1;
				} else {
					tempECGM[i][j]=0;
				}
			}
	}

	for (int i=0; i<polarParams->K; i++) {
		for (int j=0; j<polarParams->crcParityBits; j++) {
			extended_crc_generator_matrix[i][j]=tempECGM[polarParams->interleaving_pattern[i]][j];
		}
	}

	//The index of the last 1-valued bit that appears in each column.
	uint16_t last1ind[polarParams->crcParityBits];
	for (int j=0; j<polarParams->crcParityBits; j++) {
			for (int i=0; i<polarParams->K; i++) {
				if (extended_crc_generator_matrix[i][j]==1) last1ind[j]=i;
			}
	}

	double *d_tilde = malloc(sizeof(double) * polarParams->N);
	nr_polar_rate_matching(input, d_tilde, polarParams->rate_matching_pattern, polarParams->K, polarParams->N, polarParams->encoderLength);
	for (int j = 0; j < polarParams->N; j++) llr[j][polarParams->n][0]=d_tilde[j];


	/*
	 * SCL polar decoder.
	 */

	uint32_t nonFrozenBit=0;
	uint8_t currentListSize=1;
	uint8_t decoderIterationCheck=0;
	int16_t checkCrcBits=-1;
	uint8_t listIndex[2*listSize], copyIndex;

	for (uint16_t currentBit=0; currentBit<polarParams->N; currentBit++){
		updateLLR(llr, llrUpdated, bit, bitUpdated, currentListSize, currentBit, 0, polarParams->N, (polarParams->n+1), pathMetricAppr);
		if (polarParams->information_bit_pattern[currentBit]==0) { //Frozen bit.
			updatePathMetric(pathMetric, llr, currentListSize, 0, currentBit, pathMetricAppr); //approximation=0 --> 11b, approximation=1 --> 12
		} else { //Information or CRC bit.
			updatePathMetric2(pathMetric, llr, currentListSize, currentBit, pathMetricAppr);

			for (int i = 0; i < currentListSize; i++) {
				for (int j = 0; j < polarParams->N; j++) {
					for (int k = 0; k < (polarParams->n+1); k++) {
						bit[j][k][i+currentListSize]=bit[j][k][i];
						llr[j][k][i+currentListSize]=llr[j][k][i];}}}
			for (int i = 0; i < currentListSize; i++) {
				bit[currentBit][0][i]=0;
				crcState[i+currentListSize]=crcState[i];
			}
			for (int i = currentListSize; i < 2*currentListSize; i++) bit[currentBit][0][i]=1;
			bitUpdated[currentBit][0]=1;
			updateCrcChecksum2(crcChecksum, extended_crc_generator_matrix, currentListSize, nonFrozenBit, polarParams->crcParityBits);
			currentListSize*=2;

			//Keep only the best "listSize" number of entries.
			if (currentListSize > listSize) {
				for (uint8_t i = 0; i < 2*listSize; i++) listIndex[i]=i;
				nr_sort_asc_double_1D_array_ind(pathMetric, listIndex, currentListSize);

				//sort listIndex[listSize, ..., 2*listSize-1] in descending order.
				uint8_t swaps, tempInd;
				for (uint8_t i = 0; i < listSize; i++) {
					swaps = 0;
					for (uint8_t j = listSize; j < (2*listSize - i) - 1; j++) {
						if (listIndex[j+1] > listIndex[j]) {
							tempInd = listIndex[j];
							listIndex[j] = listIndex[j + 1];
							listIndex[j + 1] = tempInd;
							swaps++;
						}
					}
					if (swaps == 0)
						break;
				}

				//First, backup the best "listSize" number of entries.
				for (int k=(listSize-1); k>0; k--) {
					for (int i=0; i<polarParams->N; i++) {
						for (int j=0; j<(polarParams->n+1); j++) {
							bit[i][j][listIndex[(2*listSize-1)-k]]=bit[i][j][listIndex[k]];
							llr[i][j][listIndex[(2*listSize-1)-k]]=llr[i][j][listIndex[k]];
						}
					}
				}
				for (int k=(listSize-1); k>0; k--) {
					for (int i = 0; i < polarParams->crcParityBits; i++) {
						crcChecksum[i][listIndex[(2*listSize-1)-k]] = crcChecksum[i][listIndex[k]];
					}
				}
				for (int k=(listSize-1); k>0; k--) crcState[listIndex[(2*listSize-1)-k]]=crcState[listIndex[k]];

				//Copy the best "listSize" number of entries to the first indices.
				for (int k = 0; k < listSize; k++) {
					if (k > listIndex[k]) {
						copyIndex = listIndex[(2*listSize-1)-k];
					} else { //Use the backup.
						copyIndex = listIndex[k];
					}
					for (int i = 0; i < polarParams->N; i++) {
						for (int j = 0; j < (polarParams->n + 1); j++) {
							bit[i][j][k] = bit[i][j][copyIndex];
							llr[i][j][k] = llr[i][j][copyIndex];
						}
					}
				}
				for (int k = 0; k < listSize; k++) {
					if (k > listIndex[k]) {
						copyIndex = listIndex[(2*listSize-1)-k];
					} else { //Use the backup.
						copyIndex = listIndex[k];
					}
					for (int i = 0; i < polarParams->crcParityBits; i++) {
						crcChecksum[i][k]=crcChecksum[i][copyIndex];
					}
				}
				for (int k = 0; k < listSize; k++) {
					if (k > listIndex[k]) {
						copyIndex = listIndex[(2*listSize-1)-k];
					} else { //Use the backup.
						copyIndex = listIndex[k];
					}
					crcState[k]=crcState[copyIndex];
				}
				currentListSize = listSize;
				}


			for (int i=0; i<polarParams->crcParityBits; i++) {
				if (last1ind[i]==nonFrozenBit) {
					checkCrcBits=i;
					break;
				}
			}

			if ( checkCrcBits > (-1) ) {
				for (uint8_t i = 0; i < currentListSize; i++) {
					if (crcChecksum[checkCrcBits][i]==1) {
						crcState[i]=0; //0=False, 1=True
					}
				}
			}

			for (uint8_t i = 0; i < currentListSize; i++) decoderIterationCheck+=crcState[i];
			if (decoderIterationCheck==0) {
				//perror("[SCL polar decoder] All list entries have failed the CRC checks.");
				free(d_tilde);
				free(pathMetric);
				free(crcState);
yilmazt's avatar
yilmazt committed
234
				nr_free_uint8_3D_array(bit, polarParams->N, (polarParams->n+1));
235
				nr_free_double_3D_array(llr, polarParams->N, (polarParams->n+1));
yilmazt's avatar
yilmazt committed
236
				nr_free_uint8_2D_array(crcChecksum, polarParams->crcParityBits);
237 238 239 240 241 242 243 244 245 246 247 248 249 250
				return(-1);
			}

			nonFrozenBit++;
			decoderIterationCheck=0;
			checkCrcBits=-1;
		}
	}

	for (uint8_t i = 0; i < 2*listSize; i++) listIndex[i]=i;
	nr_sort_asc_double_1D_array_ind(pathMetric, listIndex, currentListSize);

	for (uint8_t i = 0; i < fmin(listSize, (pow(2,polarParams->crcCorrectionBits)) ); i++) {
		if ( crcState[listIndex[i]] == 1 ) {
yilmazt's avatar
yilmazt committed
251
			for (int j = 0; j < polarParams->N; j++) polarParams->nr_polar_U[j]=bit[j][0][listIndex[i]];
252 253

			//Extract the information bits (û to ĉ)
yilmazt's avatar
yilmazt committed
254
			nr_polar_info_bit_extraction(polarParams->nr_polar_U, polarParams->nr_polar_CPrime, polarParams->information_bit_pattern, polarParams->N);
255 256

			//Deinterleaving (ĉ to b)
yilmazt's avatar
yilmazt committed
257
			nr_polar_deinterleaver(polarParams->nr_polar_CPrime, polarParams->nr_polar_B, polarParams->interleaving_pattern, polarParams->K);
258

259
			
260
			//Remove the CRC (â)
yilmazt's avatar
yilmazt committed
261
			for (int j = 0; j < polarParams->payloadBits; j++) polarParams->nr_polar_A[j]=polarParams->nr_polar_B[j];
262 263 264 265 266 267 268 269

			break;
		}
	}

	free(d_tilde);
	free(pathMetric);
	free(crcState);
yilmazt's avatar
yilmazt committed
270
	nr_free_uint8_3D_array(bit, polarParams->N, (polarParams->n+1));
271
	nr_free_double_3D_array(llr, polarParams->N, (polarParams->n+1));
yilmazt's avatar
yilmazt committed
272 273 274
	nr_free_uint8_2D_array(crcChecksum, polarParams->crcParityBits);
	nr_free_uint8_2D_array(extended_crc_generator_matrix, polarParams->K);
	nr_free_uint8_2D_array(tempECGM, polarParams->K);
275 276 277 278

	/*
	 * Return bits.
	 */
hongzhi wang's avatar
hongzhi wang committed
279
	//nr_byte2bit_uint8_32_t(polarParams->nr_polar_A, polarParams->payloadBits, out);
280 281 282
	return(0);
}

yilmazt's avatar
yilmazt committed
283
int8_t polar_decoder_aPriori(double *input,
284 285 286 287 288
			     uint32_t *out,
			     t_nrPolar_paramsPtr polarParams,
			     uint8_t listSize,
			     uint8_t pathMetricAppr,
			     double *aPrioriPayload)
yilmazt's avatar
yilmazt committed
289
{
yilmazt's avatar
yilmazt committed
290 291 292
  uint8_t ***bit = nr_alloc_uint8_3D_array(polarParams->N, (polarParams->n+1), 2*listSize);
	uint8_t **bitUpdated = nr_alloc_uint8_2D_array(polarParams->N, (polarParams->n+1)); //0=False, 1=True
	uint8_t **llrUpdated = nr_alloc_uint8_2D_array(polarParams->N, (polarParams->n+1)); //0=False, 1=True
yilmazt's avatar
yilmazt committed
293
	double ***llr = nr_alloc_double_3D_array(polarParams->N, (polarParams->n+1), 2*listSize);
yilmazt's avatar
yilmazt committed
294
	uint8_t **crcChecksum = nr_alloc_uint8_2D_array(polarParams->crcParityBits, 2*listSize);
yilmazt's avatar
yilmazt committed
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
	double *pathMetric = malloc(sizeof(double)*(2*listSize));
	uint8_t *crcState = malloc(sizeof(uint8_t)*(2*listSize)); //0=False, 1=True

	for (int i=0; i<(2*listSize); i++) {
		pathMetric[i] = 0;
		crcState[i]=1;
	}
	for (int i=0; i<polarParams->N; i++) {
		llrUpdated[i][polarParams->n]=1;
		bitUpdated[i][0]=((polarParams->information_bit_pattern[i]+1) % 2);
	}

	uint8_t **extended_crc_generator_matrix = malloc(polarParams->K * sizeof(uint8_t *)); //G_P3
	uint8_t **tempECGM = malloc(polarParams->K * sizeof(uint8_t *)); //G_P2
	for (int i = 0; i < polarParams->K; i++){
		extended_crc_generator_matrix[i] = malloc(polarParams->crcParityBits * sizeof(uint8_t));
		tempECGM[i] = malloc(polarParams->crcParityBits * sizeof(uint8_t));
	}

	for (int i=0; i<polarParams->payloadBits; i++) {
		for (int j=0; j<polarParams->crcParityBits; j++) {
			tempECGM[i][j]=polarParams->crc_generator_matrix[i][j];
		}
	}
	for (int i=polarParams->payloadBits; i<polarParams->K; i++) {
			for (int j=0; j<polarParams->crcParityBits; j++) {
				if( (i-polarParams->payloadBits) == j ){
					tempECGM[i][j]=1;
				} else {
					tempECGM[i][j]=0;
				}
			}
	}

	for (int i=0; i<polarParams->K; i++) {
		for (int j=0; j<polarParams->crcParityBits; j++) {
			extended_crc_generator_matrix[i][j]=tempECGM[polarParams->interleaving_pattern[i]][j];
		}
	}

	//The index of the last 1-valued bit that appears in each column.
	uint16_t last1ind[polarParams->crcParityBits];
	for (int j=0; j<polarParams->crcParityBits; j++) {
			for (int i=0; i<polarParams->K; i++) {
				if (extended_crc_generator_matrix[i][j]==1) last1ind[j]=i;
			}
	}

	double *d_tilde = malloc(sizeof(double) * polarParams->N);
	nr_polar_rate_matching(input, d_tilde, polarParams->rate_matching_pattern, polarParams->K, polarParams->N, polarParams->encoderLength);
	for (int j = 0; j < polarParams->N; j++) llr[j][polarParams->n][0]=d_tilde[j];


	/*
	 * SCL polar decoder.
	 */

	uint32_t nonFrozenBit=0;
	uint8_t currentListSize=1;
	uint8_t decoderIterationCheck=0;
	int16_t checkCrcBits=-1;
	uint8_t listIndex[2*listSize], copyIndex;

	for (uint16_t currentBit=0; currentBit<polarParams->N; currentBit++){
359

yilmazt's avatar
yilmazt committed
360 361 362 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 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 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
		updateLLR(llr, llrUpdated, bit, bitUpdated, currentListSize, currentBit, 0, polarParams->N, (polarParams->n+1), pathMetricAppr);
		if (polarParams->information_bit_pattern[currentBit]==0) { //Frozen bit.
			updatePathMetric(pathMetric, llr, currentListSize, 0, currentBit, pathMetricAppr); //approximation=0 --> 11b, approximation=1 --> 12
		} else { //Information or CRC bit.
			if ( (polarParams->interleaving_pattern[nonFrozenBit] <= polarParams->payloadBits) &&
					(aPrioriPayload[polarParams->interleaving_pattern[nonFrozenBit]] == 0) ) {
				//Information bit with known value of "0".
				updatePathMetric(pathMetric, llr, currentListSize, 0, currentBit, pathMetricAppr);
				bitUpdated[currentBit][0]=1; //0=False, 1=True
			} else if ( (polarParams->interleaving_pattern[nonFrozenBit] <= polarParams->payloadBits) &&
					(aPrioriPayload[polarParams->interleaving_pattern[nonFrozenBit]] == 1) ) {
				//Information bit with known value of "1".
				updatePathMetric(pathMetric, llr, currentListSize, 1, currentBit, pathMetricAppr);
				for (uint8_t i=0; i<currentListSize; i++) bit[currentBit][0][i]=1;
				bitUpdated[currentBit][0]=1;
				updateCrcChecksum(crcChecksum, extended_crc_generator_matrix, currentListSize, nonFrozenBit, polarParams->crcParityBits);
			} else {
				updatePathMetric2(pathMetric, llr, currentListSize, currentBit, pathMetricAppr);

				for (int i = 0; i < currentListSize; i++) {
					for (int j = 0; j < polarParams->N; j++) {
						for (int k = 0; k < (polarParams->n+1); k++) {
							bit[j][k][i+currentListSize]=bit[j][k][i];
							llr[j][k][i+currentListSize]=llr[j][k][i];}}}
				for (int i = 0; i < currentListSize; i++) {
					bit[currentBit][0][i]=0;
					crcState[i+currentListSize]=crcState[i];
				}
				for (int i = currentListSize; i < 2*currentListSize; i++) bit[currentBit][0][i]=1;
				bitUpdated[currentBit][0]=1;
				updateCrcChecksum2(crcChecksum, extended_crc_generator_matrix, currentListSize, nonFrozenBit, polarParams->crcParityBits);
				currentListSize*=2;

				//Keep only the best "listSize" number of entries.
				if (currentListSize > listSize) {
					for (uint8_t i = 0; i < 2*listSize; i++) listIndex[i]=i;
					nr_sort_asc_double_1D_array_ind(pathMetric, listIndex, currentListSize);

					//sort listIndex[listSize, ..., 2*listSize-1] in descending order.
					uint8_t swaps, tempInd;
					for (uint8_t i = 0; i < listSize; i++) {
						swaps = 0;
						for (uint8_t j = listSize; j < (2*listSize - i) - 1; j++) {
							if (listIndex[j+1] > listIndex[j]) {
								tempInd = listIndex[j];
								listIndex[j] = listIndex[j + 1];
								listIndex[j + 1] = tempInd;
								swaps++;
							}
						}
						if (swaps == 0)
							break;
					}

					//First, backup the best "listSize" number of entries.
					for (int k=(listSize-1); k>0; k--) {
						for (int i=0; i<polarParams->N; i++) {
							for (int j=0; j<(polarParams->n+1); j++) {
								bit[i][j][listIndex[(2*listSize-1)-k]]=bit[i][j][listIndex[k]];
								llr[i][j][listIndex[(2*listSize-1)-k]]=llr[i][j][listIndex[k]];
							}
						}
					}
					for (int k=(listSize-1); k>0; k--) {
						for (int i = 0; i < polarParams->crcParityBits; i++) {
							crcChecksum[i][listIndex[(2*listSize-1)-k]] = crcChecksum[i][listIndex[k]];
						}
					}
					for (int k=(listSize-1); k>0; k--) crcState[listIndex[(2*listSize-1)-k]]=crcState[listIndex[k]];

					//Copy the best "listSize" number of entries to the first indices.
					for (int k = 0; k < listSize; k++) {
						if (k > listIndex[k]) {
							copyIndex = listIndex[(2*listSize-1)-k];
						} else { //Use the backup.
							copyIndex = listIndex[k];
						}
						for (int i = 0; i < polarParams->N; i++) {
							for (int j = 0; j < (polarParams->n + 1); j++) {
								bit[i][j][k] = bit[i][j][copyIndex];
								llr[i][j][k] = llr[i][j][copyIndex];
							}
						}
					}
					for (int k = 0; k < listSize; k++) {
						if (k > listIndex[k]) {
							copyIndex = listIndex[(2*listSize-1)-k];
						} else { //Use the backup.
							copyIndex = listIndex[k];
						}
						for (int i = 0; i < polarParams->crcParityBits; i++) {
							crcChecksum[i][k]=crcChecksum[i][copyIndex];
						}
					}
					for (int k = 0; k < listSize; k++) {
						if (k > listIndex[k]) {
							copyIndex = listIndex[(2*listSize-1)-k];
						} else { //Use the backup.
							copyIndex = listIndex[k];
						}
						crcState[k]=crcState[copyIndex];
					}
					currentListSize = listSize;
				}
			}

			for (int i=0; i<polarParams->crcParityBits; i++) {
				if (last1ind[i]==nonFrozenBit) {
					checkCrcBits=i;
					break;
				}
			}

			if ( checkCrcBits > (-1) ) {
				for (uint8_t i = 0; i < currentListSize; i++) {
					if (crcChecksum[checkCrcBits][i]==1) {
						crcState[i]=0; //0=False, 1=True
					}
				}
			}

			for (uint8_t i = 0; i < currentListSize; i++) decoderIterationCheck+=crcState[i];
			if (decoderIterationCheck==0) {
				//perror("[SCL polar decoder] All list entries have failed the CRC checks.");
				free(d_tilde);
				free(pathMetric);
				free(crcState);
yilmazt's avatar
yilmazt committed
487
				nr_free_uint8_3D_array(bit, polarParams->N, (polarParams->n+1));
yilmazt's avatar
yilmazt committed
488
				nr_free_double_3D_array(llr, polarParams->N, (polarParams->n+1));
yilmazt's avatar
yilmazt committed
489
				nr_free_uint8_2D_array(crcChecksum, polarParams->crcParityBits);
yilmazt's avatar
yilmazt committed
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
				return(-1);
			}

			nonFrozenBit++;
			decoderIterationCheck=0;
			checkCrcBits=-1;
		}
	}

	for (uint8_t i = 0; i < 2*listSize; i++) listIndex[i]=i;
	nr_sort_asc_double_1D_array_ind(pathMetric, listIndex, currentListSize);

	for (uint8_t i = 0; i < fmin(listSize, (pow(2,polarParams->crcCorrectionBits)) ); i++) {
		if ( crcState[listIndex[i]] == 1 ) {
			for (int j = 0; j < polarParams->N; j++) polarParams->nr_polar_U[j]=bit[j][0][listIndex[i]];

			//Extract the information bits (û to ĉ)
			nr_polar_info_bit_extraction(polarParams->nr_polar_U, polarParams->nr_polar_CPrime, polarParams->information_bit_pattern, polarParams->N);

			//Deinterleaving (ĉ to b)
			nr_polar_deinterleaver(polarParams->nr_polar_CPrime, polarParams->nr_polar_B, polarParams->interleaving_pattern, polarParams->K);

			//Remove the CRC (â)
513 514
			for (int j = 0; j < polarParams->payloadBits; j++)
			  polarParams->nr_polar_A[j]=polarParams->nr_polar_B[j];
yilmazt's avatar
yilmazt committed
515 516 517 518 519 520 521
			break;
		}
	}

	free(d_tilde);
	free(pathMetric);
	free(crcState);
yilmazt's avatar
yilmazt committed
522
	nr_free_uint8_3D_array(bit, polarParams->N, (polarParams->n+1));
yilmazt's avatar
yilmazt committed
523
	nr_free_double_3D_array(llr, polarParams->N, (polarParams->n+1));
yilmazt's avatar
yilmazt committed
524 525 526
	nr_free_uint8_2D_array(crcChecksum, polarParams->crcParityBits);
	nr_free_uint8_2D_array(extended_crc_generator_matrix, polarParams->K);
	nr_free_uint8_2D_array(tempECGM, polarParams->K);
yilmazt's avatar
yilmazt committed
527 528 529 530 531

	/*
	 * Return bits.
	 */
	nr_byte2bit_uint8_32_t(polarParams->nr_polar_A, polarParams->payloadBits, out);
532

yilmazt's avatar
yilmazt committed
533
	return(0);
534

535 536 537
}


538

yilmazt's avatar
yilmazt committed
539 540 541 542 543 544 545 546
int8_t polar_decoder_aPriori_timing(double *input,
									uint32_t *out,
									t_nrPolar_paramsPtr polarParams,
									uint8_t listSize,
									uint8_t pathMetricAppr,
									double *aPrioriPayload,
									double cpuFreqGHz,
									FILE* logFile)
547
{
Guy De Souza's avatar
Guy De Souza committed
548

yilmazt's avatar
yilmazt committed
549 550 551
	uint8_t ***bit = nr_alloc_uint8_3D_array(polarParams->N, (polarParams->n+1), 2*listSize);
	uint8_t **bitUpdated = nr_alloc_uint8_2D_array(polarParams->N, (polarParams->n+1)); //0=False, 1=True
	uint8_t **llrUpdated = nr_alloc_uint8_2D_array(polarParams->N, (polarParams->n+1)); //0=False, 1=True
Guy De Souza's avatar
Guy De Souza committed
552
	double ***llr = nr_alloc_double_3D_array(polarParams->N, (polarParams->n+1), 2*listSize);
yilmazt's avatar
yilmazt committed
553
	uint8_t **crcChecksum = nr_alloc_uint8_2D_array(polarParams->crcParityBits, 2*listSize);
Guy De Souza's avatar
Guy De Souza committed
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
	double *pathMetric = malloc(sizeof(double)*(2*listSize));
	uint8_t *crcState = malloc(sizeof(uint8_t)*(2*listSize)); //0=False, 1=True

	for (int i=0; i<(2*listSize); i++) {
		pathMetric[i] = 0;
		crcState[i]=1;
	}
	for (int i=0; i<polarParams->N; i++) {
		llrUpdated[i][polarParams->n]=1;
		bitUpdated[i][0]=((polarParams->information_bit_pattern[i]+1) % 2);
	}

	uint8_t **extended_crc_generator_matrix = malloc(polarParams->K * sizeof(uint8_t *)); //G_P3
	uint8_t **tempECGM = malloc(polarParams->K * sizeof(uint8_t *)); //G_P2
	for (int i = 0; i < polarParams->K; i++){
		extended_crc_generator_matrix[i] = malloc(polarParams->crcParityBits * sizeof(uint8_t));
		tempECGM[i] = malloc(polarParams->crcParityBits * sizeof(uint8_t));
	}

	for (int i=0; i<polarParams->payloadBits; i++) {
		for (int j=0; j<polarParams->crcParityBits; j++) {
575
			tempECGM[i][j]=polarParams->crc_generator_matrix[i][j];
Guy De Souza's avatar
Guy De Souza committed
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
		}
	}
	for (int i=polarParams->payloadBits; i<polarParams->K; i++) {
			for (int j=0; j<polarParams->crcParityBits; j++) {
				if( (i-polarParams->payloadBits) == j ){
					tempECGM[i][j]=1;
				} else {
					tempECGM[i][j]=0;
				}
			}
	}

	for (int i=0; i<polarParams->K; i++) {
		for (int j=0; j<polarParams->crcParityBits; j++) {
			extended_crc_generator_matrix[i][j]=tempECGM[polarParams->interleaving_pattern[i]][j];
		}
	}

	//The index of the last 1-valued bit that appears in each column.
	uint16_t last1ind[polarParams->crcParityBits];
	for (int j=0; j<polarParams->crcParityBits; j++) {
			for (int i=0; i<polarParams->K; i++) {
				if (extended_crc_generator_matrix[i][j]==1) last1ind[j]=i;
			}
	}

	double *d_tilde = malloc(sizeof(double) * polarParams->N);
	nr_polar_rate_matching(input, d_tilde, polarParams->rate_matching_pattern, polarParams->K, polarParams->N, polarParams->encoderLength);
	for (int j = 0; j < polarParams->N; j++) llr[j][polarParams->n][0]=d_tilde[j];


	/*
	 * SCL polar decoder.
	 */

	uint32_t nonFrozenBit=0;
	uint8_t currentListSize=1;
	uint8_t decoderIterationCheck=0;
	int16_t checkCrcBits=-1;
	uint8_t listIndex[2*listSize], copyIndex;

	for (uint16_t currentBit=0; currentBit<polarParams->N; currentBit++){
		updateLLR(llr, llrUpdated, bit, bitUpdated, currentListSize, currentBit, 0, polarParams->N, (polarParams->n+1), pathMetricAppr);
		if (polarParams->information_bit_pattern[currentBit]==0) { //Frozen bit.
			updatePathMetric(pathMetric, llr, currentListSize, 0, currentBit, pathMetricAppr); //approximation=0 --> 11b, approximation=1 --> 12
		} else { //Information or CRC bit.
622 623
			if ( (polarParams->interleaving_pattern[nonFrozenBit] <= polarParams->payloadBits) &&
					(aPrioriPayload[polarParams->interleaving_pattern[nonFrozenBit]] == 0) ) {
Guy De Souza's avatar
Guy De Souza committed
624 625 626
				//Information bit with known value of "0".
				updatePathMetric(pathMetric, llr, currentListSize, 0, currentBit, pathMetricAppr);
				bitUpdated[currentBit][0]=1; //0=False, 1=True
627 628
			} else if ( (polarParams->interleaving_pattern[nonFrozenBit] <= polarParams->payloadBits) &&
					(aPrioriPayload[polarParams->interleaving_pattern[nonFrozenBit]] == 1) ) {
Guy De Souza's avatar
Guy De Souza committed
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
				//Information bit with known value of "1".
				updatePathMetric(pathMetric, llr, currentListSize, 1, currentBit, pathMetricAppr);
				for (uint8_t i=0; i<currentListSize; i++) bit[currentBit][0][i]=1;
				bitUpdated[currentBit][0]=1;
				updateCrcChecksum(crcChecksum, extended_crc_generator_matrix, currentListSize, nonFrozenBit, polarParams->crcParityBits);
			} else {
				updatePathMetric2(pathMetric, llr, currentListSize, currentBit, pathMetricAppr);

				for (int i = 0; i < currentListSize; i++) {
					for (int j = 0; j < polarParams->N; j++) {
						for (int k = 0; k < (polarParams->n+1); k++) {
							bit[j][k][i+currentListSize]=bit[j][k][i];
							llr[j][k][i+currentListSize]=llr[j][k][i];}}}
				for (int i = 0; i < currentListSize; i++) {
					bit[currentBit][0][i]=0;
					crcState[i+currentListSize]=crcState[i];
				}
				for (int i = currentListSize; i < 2*currentListSize; i++) bit[currentBit][0][i]=1;
				bitUpdated[currentBit][0]=1;
				updateCrcChecksum2(crcChecksum, extended_crc_generator_matrix, currentListSize, nonFrozenBit, polarParams->crcParityBits);
				currentListSize*=2;
650

Guy De Souza's avatar
Guy De Souza committed
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 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
				//Keep only the best "listSize" number of entries.
				if (currentListSize > listSize) {
					for (uint8_t i = 0; i < 2*listSize; i++) listIndex[i]=i;
					nr_sort_asc_double_1D_array_ind(pathMetric, listIndex, currentListSize);

					//sort listIndex[listSize, ..., 2*listSize-1] in descending order.
					uint8_t swaps, tempInd;
					for (uint8_t i = 0; i < listSize; i++) {
						swaps = 0;
						for (uint8_t j = listSize; j < (2*listSize - i) - 1; j++) {
							if (listIndex[j+1] > listIndex[j]) {
								tempInd = listIndex[j];
								listIndex[j] = listIndex[j + 1];
								listIndex[j + 1] = tempInd;
								swaps++;
							}
						}
						if (swaps == 0)
							break;
					}

					//First, backup the best "listSize" number of entries.
					for (int k=(listSize-1); k>0; k--) {
						for (int i=0; i<polarParams->N; i++) {
							for (int j=0; j<(polarParams->n+1); j++) {
								bit[i][j][listIndex[(2*listSize-1)-k]]=bit[i][j][listIndex[k]];
								llr[i][j][listIndex[(2*listSize-1)-k]]=llr[i][j][listIndex[k]];
							}
						}
					}
					for (int k=(listSize-1); k>0; k--) {
						for (int i = 0; i < polarParams->crcParityBits; i++) {
							crcChecksum[i][listIndex[(2*listSize-1)-k]] = crcChecksum[i][listIndex[k]];
						}
					}
					for (int k=(listSize-1); k>0; k--) crcState[listIndex[(2*listSize-1)-k]]=crcState[listIndex[k]];

					//Copy the best "listSize" number of entries to the first indices.
					for (int k = 0; k < listSize; k++) {
						if (k > listIndex[k]) {
							copyIndex = listIndex[(2*listSize-1)-k];
						} else { //Use the backup.
							copyIndex = listIndex[k];
						}
						for (int i = 0; i < polarParams->N; i++) {
							for (int j = 0; j < (polarParams->n + 1); j++) {
								bit[i][j][k] = bit[i][j][copyIndex];
								llr[i][j][k] = llr[i][j][copyIndex];
							}
						}
					}
					for (int k = 0; k < listSize; k++) {
						if (k > listIndex[k]) {
							copyIndex = listIndex[(2*listSize-1)-k];
						} else { //Use the backup.
							copyIndex = listIndex[k];
						}
						for (int i = 0; i < polarParams->crcParityBits; i++) {
							crcChecksum[i][k]=crcChecksum[i][copyIndex];
						}
					}
					for (int k = 0; k < listSize; k++) {
						if (k > listIndex[k]) {
							copyIndex = listIndex[(2*listSize-1)-k];
						} else { //Use the backup.
							copyIndex = listIndex[k];
						}
						crcState[k]=crcState[copyIndex];
					}
					currentListSize = listSize;
				}
			}

			for (int i=0; i<polarParams->crcParityBits; i++) {
				if (last1ind[i]==nonFrozenBit) {
					checkCrcBits=i;
					break;
				}
			}

			if ( checkCrcBits > (-1) ) {
				for (uint8_t i = 0; i < currentListSize; i++) {
					if (crcChecksum[checkCrcBits][i]==1) {
						crcState[i]=0; //0=False, 1=True
					}
				}
			}

			for (uint8_t i = 0; i < currentListSize; i++) decoderIterationCheck+=crcState[i];
			if (decoderIterationCheck==0) {
				//perror("[SCL polar decoder] All list entries have failed the CRC checks.");
				free(d_tilde);
				free(pathMetric);
744
				free(crcState);
yilmazt's avatar
yilmazt committed
745
				nr_free_uint8_3D_array(bit, polarParams->N, (polarParams->n+1));
Guy De Souza's avatar
Guy De Souza committed
746
				nr_free_double_3D_array(llr, polarParams->N, (polarParams->n+1));
yilmazt's avatar
yilmazt committed
747
				nr_free_uint8_2D_array(crcChecksum, polarParams->crcParityBits);
Guy De Souza's avatar
Guy De Souza committed
748 749 750 751 752 753 754 755 756 757 758 759
				return(-1);
			}

			nonFrozenBit++;
			decoderIterationCheck=0;
			checkCrcBits=-1;
		}
	}

	for (uint8_t i = 0; i < 2*listSize; i++) listIndex[i]=i;
	nr_sort_asc_double_1D_array_ind(pathMetric, listIndex, currentListSize);

760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
	for (uint8_t i = 0; i < fmin(listSize, (pow(2,polarParams->crcCorrectionBits)) ); i++) {
		if ( crcState[listIndex[i]] == 1 ) {
			for (int j = 0; j < polarParams->N; j++) polarParams->nr_polar_U[j]=bit[j][0][listIndex[i]];

			//Extract the information bits (û to ĉ)
			nr_polar_info_bit_extraction(polarParams->nr_polar_U, polarParams->nr_polar_CPrime, polarParams->information_bit_pattern, polarParams->N);

			//Deinterleaving (ĉ to b)
			nr_polar_deinterleaver(polarParams->nr_polar_CPrime, polarParams->nr_polar_B, polarParams->interleaving_pattern, polarParams->K);

			//Remove the CRC (â)
			for (int j = 0; j < polarParams->payloadBits; j++) polarParams->nr_polar_A[j]=polarParams->nr_polar_B[j];

			break;
		}
	}

	free(d_tilde);
	free(pathMetric);
	free(crcState);
yilmazt's avatar
yilmazt committed
780
	nr_free_uint8_3D_array(bit, polarParams->N, (polarParams->n+1));
781
	nr_free_double_3D_array(llr, polarParams->N, (polarParams->n+1));
yilmazt's avatar
yilmazt committed
782 783 784
	nr_free_uint8_2D_array(crcChecksum, polarParams->crcParityBits);
	nr_free_uint8_2D_array(extended_crc_generator_matrix, polarParams->K);
	nr_free_uint8_2D_array(tempECGM, polarParams->K);
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799

	/*
	 * Return bits.
	 */
	nr_byte2bit_uint8_32_t(polarParams->nr_polar_A, polarParams->payloadBits, out);
	return(0);
}


int8_t polar_decoder_dci(double *input,
						 uint32_t *out,
						 t_nrPolar_paramsPtr polarParams,
						 uint8_t listSize,
						 uint8_t pathMetricAppr,
						 uint16_t n_RNTI)
800 801
{

yilmazt's avatar
yilmazt committed
802 803 804
	uint8_t ***bit = nr_alloc_uint8_3D_array(polarParams->N, (polarParams->n+1), 2*listSize);
	uint8_t **bitUpdated = nr_alloc_uint8_2D_array(polarParams->N, (polarParams->n+1)); //0=False, 1=True
	uint8_t **llrUpdated = nr_alloc_uint8_2D_array(polarParams->N, (polarParams->n+1)); //0=False, 1=True
805
	double ***llr = nr_alloc_double_3D_array(polarParams->N, (polarParams->n+1), 2*listSize);
yilmazt's avatar
yilmazt committed
806
	uint8_t **crcChecksum = nr_alloc_uint8_2D_array(polarParams->crcParityBits, 2*listSize);
807 808 809 810 811 812 813 814 815 816 817 818
	double *pathMetric = malloc(sizeof(double)*(2*listSize));
	uint8_t *crcState = malloc(sizeof(uint8_t)*(2*listSize)); //0=False, 1=True
	uint8_t extended_crc_scrambling_pattern[polarParams->crcParityBits];

	for (int i=0; i<(2*listSize); i++) {
		pathMetric[i] = 0;
		crcState[i]=1;
	}
	for (int i=0; i<polarParams->N; i++) {
		llrUpdated[i][polarParams->n]=1;
		bitUpdated[i][0]=((polarParams->information_bit_pattern[i]+1) % 2);
	}
819

820 821 822 823 824 825
	uint8_t **extended_crc_generator_matrix = malloc(polarParams->K * sizeof(uint8_t *)); //G_P3: K-by-P
	uint8_t **tempECGM = malloc(polarParams->K * sizeof(uint8_t *)); //G_P2: K-by-P
	for (int i = 0; i < polarParams->K; i++){
		extended_crc_generator_matrix[i] = malloc(polarParams->crcParityBits * sizeof(uint8_t));
		tempECGM[i] = malloc(polarParams->crcParityBits * sizeof(uint8_t));
	}
826

827 828 829 830 831 832 833 834 835 836 837 838 839 840
	for (int i=0; i<polarParams->payloadBits; i++) {
		for (int j=0; j<polarParams->crcParityBits; j++) {
			tempECGM[i][j]=polarParams->crc_generator_matrix[i+polarParams->crcParityBits][j];
		}
	}
	for (int i=polarParams->payloadBits; i<polarParams->K; i++) {
			for (int j=0; j<polarParams->crcParityBits; j++) {
				if( (i-polarParams->payloadBits) == j ){
					tempECGM[i][j]=1;
				} else {
					tempECGM[i][j]=0;
				}
			}
	}
841

842 843 844 845 846
	for (int i=0; i<polarParams->K; i++) {
		for (int j=0; j<polarParams->crcParityBits; j++) {
			extended_crc_generator_matrix[i][j]=tempECGM[polarParams->interleaving_pattern[i]][j];
		}
	}
847

848 849 850 851 852 853 854
	//The index of the last 1-valued bit that appears in each column.
	uint16_t last1ind[polarParams->crcParityBits];
	for (int j=0; j<polarParams->crcParityBits; j++) {
			for (int i=0; i<polarParams->K; i++) {
				if (extended_crc_generator_matrix[i][j]==1) last1ind[j]=i;
			}
	}
855

856 857 858 859
	for (int i=0;i<8;i++) extended_crc_scrambling_pattern[i]=0;
	for (int i=8; i<polarParams->crcParityBits; i++) {
		extended_crc_scrambling_pattern[i]=(n_RNTI>>(23-i))&1;
	}
860

861 862 863
	double *d_tilde = malloc(sizeof(double) * polarParams->N);
	nr_polar_rate_matching(input, d_tilde, polarParams->rate_matching_pattern, polarParams->K, polarParams->N, polarParams->encoderLength);
	for (int j = 0; j < polarParams->N; j++) llr[j][polarParams->n][0]=d_tilde[j];
864

865 866 867
	/*
	 * SCL polar decoder.
	 */
868

869 870 871 872
	for (int i=0; i<polarParams->crcParityBits; i++) {
		for (int j=0; j<polarParams->crcParityBits; j++) crcChecksum[i][0]=crcChecksum[i][0]+polarParams->crc_generator_matrix[j][i];
		crcChecksum[i][0]=(crcChecksum[i][0]%2);
	}
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 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
	uint32_t nonFrozenBit=0;
	uint8_t currentListSize=1;
	uint8_t decoderIterationCheck=0;
	int16_t checkCrcBits=-1;
	uint8_t listIndex[2*listSize], copyIndex;

	for (uint16_t currentBit=0; currentBit<polarParams->N; currentBit++){
		updateLLR(llr, llrUpdated, bit, bitUpdated, currentListSize, currentBit, 0, polarParams->N, (polarParams->n+1), pathMetricAppr);
		if (polarParams->information_bit_pattern[currentBit]==0) { //Frozen bit.
			updatePathMetric(pathMetric, llr, currentListSize, 0, currentBit, pathMetricAppr); //approximation=0 --> 11b, approximation=1 --> 12
		} else { //Information or CRC bit.
				updatePathMetric2(pathMetric, llr, currentListSize, currentBit, pathMetricAppr);

				for (int i = 0; i < currentListSize; i++) {
					for (int j = 0; j < polarParams->N; j++) {
						for (int k = 0; k < (polarParams->n+1); k++) {
							bit[j][k][i+currentListSize]=bit[j][k][i];
							llr[j][k][i+currentListSize]=llr[j][k][i];}}}
				for (int i = 0; i < currentListSize; i++) {
					bit[currentBit][0][i]=0;
					crcState[i+currentListSize]=crcState[i];
				}
				for (int i = currentListSize; i < 2*currentListSize; i++) bit[currentBit][0][i]=1;
				bitUpdated[currentBit][0]=1;
				updateCrcChecksum2(crcChecksum, extended_crc_generator_matrix, currentListSize, nonFrozenBit, polarParams->crcParityBits);
				currentListSize*=2;

				//Keep only the best "listSize" number of entries.
				if (currentListSize > listSize) {
					for (uint8_t i = 0; i < 2*listSize; i++) listIndex[i]=i;
					nr_sort_asc_double_1D_array_ind(pathMetric, listIndex, currentListSize);

					//sort listIndex[listSize, ..., 2*listSize-1] in descending order.
					uint8_t swaps, tempInd;
					for (uint8_t i = 0; i < listSize; i++) {
						swaps = 0;
						for (uint8_t j = listSize; j < (2*listSize - i) - 1; j++) {
							if (listIndex[j+1] > listIndex[j]) {
								tempInd = listIndex[j];
								listIndex[j] = listIndex[j + 1];
								listIndex[j + 1] = tempInd;
								swaps++;
							}
						}
						if (swaps == 0)
							break;
					}

					//First, backup the best "listSize" number of entries.
					for (int k=(listSize-1); k>0; k--) {
						for (int i=0; i<polarParams->N; i++) {
							for (int j=0; j<(polarParams->n+1); j++) {
								bit[i][j][listIndex[(2*listSize-1)-k]]=bit[i][j][listIndex[k]];
								llr[i][j][listIndex[(2*listSize-1)-k]]=llr[i][j][listIndex[k]];
							}
						}
					}
					for (int k=(listSize-1); k>0; k--) {
						for (int i = 0; i < polarParams->crcParityBits; i++) {
							crcChecksum[i][listIndex[(2*listSize-1)-k]] = crcChecksum[i][listIndex[k]];
						}
					}
					for (int k=(listSize-1); k>0; k--) crcState[listIndex[(2*listSize-1)-k]]=crcState[listIndex[k]];

					//Copy the best "listSize" number of entries to the first indices.
					for (int k = 0; k < listSize; k++) {
						if (k > listIndex[k]) {
							copyIndex = listIndex[(2*listSize-1)-k];
						} else { //Use the backup.
							copyIndex = listIndex[k];
						}
						for (int i = 0; i < polarParams->N; i++) {
							for (int j = 0; j < (polarParams->n + 1); j++) {
								bit[i][j][k] = bit[i][j][copyIndex];
								llr[i][j][k] = llr[i][j][copyIndex];
							}
						}
					}
					for (int k = 0; k < listSize; k++) {
						if (k > listIndex[k]) {
							copyIndex = listIndex[(2*listSize-1)-k];
						} else { //Use the backup.
							copyIndex = listIndex[k];
						}
						for (int i = 0; i < polarParams->crcParityBits; i++) {
							crcChecksum[i][k]=crcChecksum[i][copyIndex];
						}
					}
					for (int k = 0; k < listSize; k++) {
						if (k > listIndex[k]) {
							copyIndex = listIndex[(2*listSize-1)-k];
						} else { //Use the backup.
							copyIndex = listIndex[k];
						}
						crcState[k]=crcState[copyIndex];
					}
					currentListSize = listSize;
				}

			for (int i=0; i<polarParams->crcParityBits; i++) {
				if (last1ind[i]==nonFrozenBit) {
					checkCrcBits=i;
					break;
				}
			}

			if ( checkCrcBits > (-1) ) {
				for (uint8_t i = 0; i < currentListSize; i++) {
					if (crcChecksum[checkCrcBits][i]!=extended_crc_scrambling_pattern[checkCrcBits]) {
						crcState[i]=0; //0=False, 1=True
					}
				}
			}

			for (uint8_t i = 0; i < currentListSize; i++) decoderIterationCheck+=crcState[i];
			if (decoderIterationCheck==0) {
				//perror("[SCL polar decoder] All list entries have failed the CRC checks.");
				free(d_tilde);
				free(pathMetric);
				free(crcState);
yilmazt's avatar
yilmazt committed
994
				nr_free_uint8_3D_array(bit, polarParams->N, (polarParams->n+1));
995
				nr_free_double_3D_array(llr, polarParams->N, (polarParams->n+1));
yilmazt's avatar
yilmazt committed
996
				nr_free_uint8_2D_array(crcChecksum, polarParams->crcParityBits);
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
				return(-1);
			}

			nonFrozenBit++;
			decoderIterationCheck=0;
			checkCrcBits=-1;
		}
	}

	for (uint8_t i = 0; i < 2*listSize; i++) listIndex[i]=i;
	nr_sort_asc_double_1D_array_ind(pathMetric, listIndex, currentListSize);

Guy De Souza's avatar
Guy De Souza committed
1009 1010
	for (uint8_t i = 0; i < fmin(listSize, (pow(2,polarParams->crcCorrectionBits)) ); i++) {
		if ( crcState[listIndex[i]] == 1 ) {
yilmazt's avatar
yilmazt committed
1011
			for (int j = 0; j < polarParams->N; j++) polarParams->nr_polar_U[j]=bit[j][0][listIndex[i]];
Guy De Souza's avatar
Guy De Souza committed
1012 1013

			//Extract the information bits (û to ĉ)
yilmazt's avatar
yilmazt committed
1014
			nr_polar_info_bit_extraction(polarParams->nr_polar_U, polarParams->nr_polar_CPrime, polarParams->information_bit_pattern, polarParams->N);
Guy De Souza's avatar
Guy De Souza committed
1015 1016

			//Deinterleaving (ĉ to b)
yilmazt's avatar
yilmazt committed
1017
			nr_polar_deinterleaver(polarParams->nr_polar_CPrime, polarParams->nr_polar_B, polarParams->interleaving_pattern, polarParams->K);
Guy De Souza's avatar
Guy De Souza committed
1018 1019

			//Remove the CRC (â)
yilmazt's avatar
yilmazt committed
1020
			for (int j = 0; j < polarParams->payloadBits; j++) polarParams->nr_polar_A[j]=polarParams->nr_polar_B[j];
Guy De Souza's avatar
Guy De Souza committed
1021 1022 1023 1024 1025 1026 1027

			break;
		}
	}

	free(d_tilde);
	free(pathMetric);
1028
	free(crcState);
yilmazt's avatar
yilmazt committed
1029
	nr_free_uint8_3D_array(bit, polarParams->N, (polarParams->n+1));
Guy De Souza's avatar
Guy De Souza committed
1030
	nr_free_double_3D_array(llr, polarParams->N, (polarParams->n+1));
yilmazt's avatar
yilmazt committed
1031 1032 1033
	nr_free_uint8_2D_array(crcChecksum, polarParams->crcParityBits);
	nr_free_uint8_2D_array(extended_crc_generator_matrix, polarParams->K);
	nr_free_uint8_2D_array(tempECGM, polarParams->K);
1034 1035 1036 1037

	/*
	 * Return bits.
	 */
yilmazt's avatar
yilmazt committed
1038
	nr_byte2bit_uint8_32_t(polarParams->nr_polar_A, polarParams->payloadBits, out);
Guy De Souza's avatar
Guy De Souza committed
1039
	return(0);
1040 1041
}

1042 1043 1044
void init_polar_deinterleaver_table(t_nrPolar_params *polarParams) {

  AssertFatal(polarParams->K > 32, "K = %d < 33, is not supported yet\n",polarParams->K);
1045
  AssertFatal(polarParams->K < 129, "K = %d > 128, is not supported yet\n",polarParams->K);
1046
  
1047
  int bit_i,ip,ipmod64;
1048 1049 1050 1051 1052 1053 1054 1055 1056

  int numbytes = polarParams->K>>3;
  int residue = polarParams->K&7;
  int numbits;
  if (residue>0) numbytes++;
  for (int byte=0;byte<numbytes;byte++) {
    if (byte<(polarParams->K>>3)) numbits=8;
    else numbits=residue;
    for (int i=0;i<numbits;i++) {
1057 1058
      // flip bit endian for B
      ip=polarParams->K - 1 - polarParams->interleaving_pattern[(8*byte)+i];
knopp's avatar
knopp committed
1059
#if 0
1060 1061
      printf("byte %d, i %d => ip %d\n",byte,i,ip);
#endif
1062 1063
      ipmod64 = ip&63;
      AssertFatal(ip<128,"ip = %d\n",ip);
1064 1065
      for (int val=0;val<256;val++) {
	bit_i=(val>>i)&1;
1066 1067
	if (ip<64) polarParams->B_tab0[byte][val] |= (((uint64_t)bit_i)<<ipmod64);
	else       polarParams->B_tab1[byte][val] |= (((uint64_t)bit_i)<<ipmod64);
1068 1069 1070
      }
    }
  }
1071
 
1072
}
1073

1074 1075 1076
uint32_t polar_decoder_int16(int16_t *input,
			     uint64_t *out,
			     t_nrPolar_params *polarParams)
1077 1078 1079 1080 1081
{
  

  
  int16_t d_tilde[polarParams->N];// = malloc(sizeof(double) * polarParams->N);
knopp's avatar
knopp committed
1082

1083
  nr_polar_rate_matching_int16(input, d_tilde, polarParams->rate_matching_pattern, polarParams->K, polarParams->N, polarParams->encoderLength);
1084 1085 1086 1087
  for (int i=0;i<polarParams->N;i++) {
    if (d_tilde[i]<-128) d_tilde[i]=-128;
    else if (d_tilde[i]>127) d_tilde[i]=128;
  }
knopp's avatar
knopp committed
1088

1089 1090 1091 1092
  memcpy((void*)&polarParams->tree.root->alpha[0],(void*)&d_tilde[0],sizeof(int16_t)*polarParams->N);
  
  generic_polar_decoder(polarParams,polarParams->tree.root);

1093

1094
  //Extract the information bits (û to ĉ)
1095 1096 1097
  uint64_t Cprime[4]={0,0,0,0};
  uint64_t B[4]={0,0,0,0};
  for (int i=0;i<polarParams->K;i++) Cprime[i>>6] = Cprime[i>>6] | ((uint64_t)polarParams->nr_polar_U[polarParams->Q_I_N[i]])<<(i&63);
1098

1099
  //Deinterleaving (ĉ to b)
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
  uint8_t *Cprimebyte = (uint8_t*)Cprime;
  if (polarParams->K<65) {
    B[0] = polarParams->B_tab0[0][Cprimebyte[0]] |
      polarParams->B_tab0[1][Cprimebyte[1]] |
      polarParams->B_tab0[2][Cprimebyte[2]] |
      polarParams->B_tab0[3][Cprimebyte[3]] |
      polarParams->B_tab0[4][Cprimebyte[4]] |
      polarParams->B_tab0[5][Cprimebyte[5]] |
      polarParams->B_tab0[6][Cprimebyte[6]] |
      polarParams->B_tab0[7][Cprimebyte[7]];
  }
1111 1112 1113 1114
  else if (polarParams->K<129) {
    int len = polarParams->K/8;
    if ((polarParams->K&7) > 0) len++;
    for (int k=0;k<len;k++) {
1115 1116 1117 1118
      B[0] |= polarParams->B_tab0[k][Cprimebyte[k]];
      B[1] |= polarParams->B_tab1[k][Cprimebyte[k]];
    }
  }
1119

1120

1121 1122 1123
  int len=polarParams->payloadBits;
  int len_mod64=len&63;
  int crclen = polarParams->crcParityBits;
1124
  uint64_t rxcrc=B[0]&((1<<crclen)-1);
1125
  uint32_t crc;
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
  uint64_t Ar;

  AssertFatal(len<65,"A must be less than 65 bits\n");
  if (len<=32) {
    Ar = (uint32_t)(B[0]>>crclen);
    uint8_t A32_flip[4];
    uint32_t Aprime= (uint32_t)(Ar<<(32-len));
    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];
    crc = (uint64_t)(crc24c(A32_flip,len)>>8);
  }
  else if (len<=64) {
    Ar = (B[0]>>crclen) | (B[1]<<(64-crclen));;
    uint8_t A64_flip[4];
    uint64_t Aprime= (uint32_t)(Ar<<(64-len));
    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];
    crc = (uint64_t)(crc24c(A64_flip,len)>>8);
1152
  }
1153

knopp's avatar
knopp committed
1154
#if 0
1155
  printf("A %llx B %llx|%llx Cprime %llx|%llx  (crc %x,rxcrc %llx %d)\n",
1156
	 Ar,
1157 1158
	 B[1],B[0],Cprime[1],Cprime[0],crc,
   	 rxcrc,polarParams->payloadBits);
1159
#endif
1160 1161 1162

  out[0]=Ar;

1163 1164 1165

  return(crc^rxcrc);
  
1166

1167

Guy De Souza's avatar
Guy De Souza committed
1168
}