1 /* -*- c++ -*- */
2 /*
3  * Copyright 2015 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include <gnuradio/fec/polar_encoder_systematic.h>
28 #include <gnuradio/io_signature.h>
29 #include <volk/volk.h>
30 
31 namespace gr {
32 namespace fec {
33 namespace code {
34 
make(int block_size,int num_info_bits,std::vector<int> frozen_bit_positions)35 generic_encoder::sptr polar_encoder_systematic::make(
36     int block_size, int num_info_bits, std::vector<int> frozen_bit_positions)
37 {
38     return generic_encoder::sptr(
39         new polar_encoder_systematic(block_size, num_info_bits, frozen_bit_positions));
40 }
41 
polar_encoder_systematic(int block_size,int num_info_bits,std::vector<int> frozen_bit_positions)42 polar_encoder_systematic::polar_encoder_systematic(int block_size,
43                                                    int num_info_bits,
44                                                    std::vector<int> frozen_bit_positions)
45     : polar_common(block_size, num_info_bits, frozen_bit_positions, std::vector<char>())
46 {
47     d_volk_syst_intermediate = (unsigned char*)volk_malloc(
48         sizeof(unsigned char) * block_size, volk_get_alignment());
49 }
50 
~polar_encoder_systematic()51 polar_encoder_systematic::~polar_encoder_systematic()
52 {
53     volk_free(d_volk_syst_intermediate);
54 }
55 
generic_work(void * in_buffer,void * out_buffer)56 void polar_encoder_systematic::generic_work(void* in_buffer, void* out_buffer)
57 {
58     const unsigned char* in = (const unsigned char*)in_buffer;
59     unsigned char* out = (unsigned char*)out_buffer;
60 
61     volk_encode(out, in);
62     bit_reverse_and_reset_frozen_bits(d_volk_syst_intermediate, out);
63     volk_encode_block(out, d_volk_syst_intermediate);
64 }
65 
bit_reverse_and_reset_frozen_bits(unsigned char * outbuf,const unsigned char * inbuf)66 void polar_encoder_systematic::bit_reverse_and_reset_frozen_bits(
67     unsigned char* outbuf, const unsigned char* inbuf)
68 {
69     memset(outbuf, 0, sizeof(unsigned char) * block_size());
70     for (int i = 0; i < num_info_bits(); i++) {
71         outbuf[d_info_bit_positions[i]] = inbuf[d_info_bit_positions_reversed[i]];
72     }
73 }
74 
75 } /* namespace code */
76 } /* namespace fec */
77 } /* namespace gr */
78