summaryrefslogtreecommitdiff
path: root/gr-fec/lib/polar_encoder.cc
blob: eeb283e4b4e4101a4d987d51fb5e6932a450d74b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
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
/* -*- c++ -*- */
/* 
 * Copyright 2015 Free Software Foundation, Inc.
 * 
 * This file is part of GNU Radio
 * 
 * GNU Radio 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, or (at your option)
 * any later version.
 * 
 * GNU Radio 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.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gnuradio/io_signature.h>
#include <gnuradio/fec/polar_encoder.h>
#include <cmath>
#include <stdexcept>
#include <volk/volk.h>

#include <gnuradio/blocks/pack_k_bits.h>
#include <gnuradio/blocks/unpack_k_bits.h>

namespace gr
{
  namespace fec
  {

    generic_encoder::sptr
    polar_encoder::make(int block_size, int num_info_bits, std::vector<int> frozen_bit_positions,
                        std::vector<char> frozen_bit_values, bool is_packed)
    {
      return generic_encoder::sptr(
          new polar_encoder(block_size, num_info_bits, frozen_bit_positions, frozen_bit_values,
                            is_packed));
    }

    polar_encoder::polar_encoder(int block_size, int num_info_bits,
                                 std::vector<int>& frozen_bit_positions,
                                 std::vector<char>& frozen_bit_values, bool is_packed) :
        polar_common(block_size, num_info_bits, frozen_bit_positions, frozen_bit_values),
        d_is_packed(is_packed)
    {
      setup_frozen_bit_inserter();
      setup_volk_vectors();
    }

    void
    polar_encoder::setup_frozen_bit_inserter()
    {
      d_frozen_bit_prototype = (unsigned char*) volk_malloc(block_size() >> 3,
                                                            volk_get_alignment());
      memset(d_frozen_bit_prototype, 0, block_size() >> 3);

      for(unsigned int i = 0; i < d_frozen_bit_positions.size(); i++) {
        int rev_pos = (int) bit_reverse((long) d_frozen_bit_positions.at(i), block_power());
        unsigned char frozen_bit = (unsigned char) d_frozen_bit_values.at(i);
        insert_unpacked_bit_into_packed_array_at_position(d_frozen_bit_prototype, frozen_bit,
                                                          rev_pos);
      }

      for(unsigned int i = 0; i < d_info_bit_positions.size(); i++){
        d_info_bit_reversed_positions.push_back((int) bit_reverse((long) d_info_bit_positions.at(i), block_power()));
      }

      if((int) d_info_bit_reversed_positions.size() != num_info_bits()) {
        throw std::runtime_error("polar_encoder: number of info bit positions MUST equal num_info_bits (K)!");
      }
    }

    void
    polar_encoder::setup_volk_vectors()
    {
      int nfrozen = block_size() - num_info_bits();
      d_temp = (unsigned char*) volk_malloc(sizeof(unsigned char) * block_size(), volk_get_alignment());
      d_frozen_bit_mask = (unsigned char*) volk_malloc(sizeof(unsigned char) * block_size(), volk_get_alignment());
      d_frozen_bits = (unsigned char*) volk_malloc(sizeof(unsigned char) * nfrozen, volk_get_alignment());
      for(int i = 0; i < nfrozen; i++){
        d_frozen_bits[i] = d_frozen_bit_values[i];
      }

      int nfbit = 0;
      for(int i = 0; i < block_size(); i++){
        unsigned char m = 0x00;
        if(d_frozen_bit_positions[nfbit] == i){
          m = 0xFF;
          nfbit++;
        }
        d_frozen_bit_mask[i] = m;
      }
    }

    polar_encoder::~polar_encoder()
    {
      volk_free(d_frozen_bit_prototype);

      volk_free(d_temp);
      volk_free(d_frozen_bit_mask);
      volk_free(d_frozen_bits);
    }

    void
    polar_encoder::generic_work(void* in_buffer, void* out_buffer)
    {
      const unsigned char *in = (const unsigned char*) in_buffer;
      unsigned char *out = (unsigned char*) out_buffer;

      if(d_is_packed){
        insert_packed_frozen_bits_and_reverse(out, in);
        encode_vector_packed(out);
      }
      else{
        volk_encode(out, in);
      }
    }

    void
    polar_encoder::volk_encode(unsigned char* out_buf, const unsigned char* in_buf)
    {
      volk_8u_x3_encodepolar_8u_x2(out_buf, d_temp, d_frozen_bit_mask, d_frozen_bits, in_buf, block_size());
    }

    void
    polar_encoder::encode_vector_packed(unsigned char* target) const
    {
      encode_vector_packed_subbyte(target);
      encode_vector_packed_interbyte(target);
    }

    void
    polar_encoder::encode_vector_packed_subbyte(unsigned char* target) const
    {
      int num_bytes_per_block = block_size() >> 3;
      while(num_bytes_per_block) {
        encode_packed_byte(target);
        ++target;
        --num_bytes_per_block;
      }
    }

    void
    polar_encoder::encode_packed_byte(unsigned char* target) const
    {
      // this method only produces correct results if block_size > 4.
      // this is assumed to be the case.
      *target ^= 0xaa & (*target << 1);
      *target ^= 0xcc & (*target << 2);
      *target ^= *target << 4;
    }

    void
    polar_encoder::encode_vector_packed_interbyte(unsigned char* target) const
    {
      int branch_byte_size = 1;
      unsigned char* pos;
      int n_branches = block_size() >> 4;
      int byte = 0;
      for(int stage = 3; stage < block_power(); ++stage) {
        pos = target;

        for(int branch = 0; branch < n_branches; ++branch) {

          byte = 0;
          while(byte < branch_byte_size) {
            *pos ^= *(pos + branch_byte_size);
            ++pos;
            ++byte;
          }

          pos += branch_byte_size;
        }

        n_branches >>= 1;
        branch_byte_size <<= 1;
      }
    }

    void
    polar_encoder::insert_packed_frozen_bits_and_reverse(unsigned char* target,
                                                         const unsigned char* input) const
    {
      memcpy(target, d_frozen_bit_prototype, block_size() >> 3);
      const int* info_bit_reversed_positions_ptr = &d_info_bit_reversed_positions[0];
      int bit_num = 0;
      unsigned char byte = *input;
      int bit_pos;
      while(bit_num < num_info_bits()) {
        bit_pos = *info_bit_reversed_positions_ptr++;
        insert_packet_bit_into_packed_array_at_position(target, byte, bit_pos, bit_num % 8);
        ++bit_num;
        if(bit_num % 8 == 0) {
          ++input;
          byte = *input;
        }
      }
    }

    void
    polar_encoder::insert_unpacked_bit_into_packed_array_at_position(unsigned char* target,
                                                                     const unsigned char bit,
                                                                     const int pos) const
    {
      int byte_pos = pos >> 3;
      int bit_pos = pos & 0x7;
      *(target + byte_pos) ^= bit << (7 - bit_pos);
    }

    void
    polar_encoder::insert_packet_bit_into_packed_array_at_position(unsigned char* target,
                                                                   const unsigned char bit,
                                                                   const int target_pos,
                                                                   const int bit_pos) const
    {
      insert_unpacked_bit_into_packed_array_at_position(target, (bit >> (7 - bit_pos)) & 0x01,
                                                        target_pos);
    }
  } /* namespace fec */
} /* namespace gr */