1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2010,2012,2018 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 "encoder_impl.h"
28 #include <gnuradio/io_signature.h>
29 #include <iostream>
30 
31 namespace gr {
32 namespace trellis {
33 
34 template <class IN_T, class OUT_T>
make(const fsm & FSM,int ST)35 typename encoder<IN_T, OUT_T>::sptr encoder<IN_T, OUT_T>::make(const fsm& FSM, int ST)
36 {
37     return gnuradio::get_initial_sptr(new encoder_impl<IN_T, OUT_T>(FSM, ST, 0, false));
38 }
39 
40 template <class IN_T, class OUT_T>
41 typename encoder<IN_T, OUT_T>::sptr
make(const fsm & FSM,int ST,int K)42 encoder<IN_T, OUT_T>::make(const fsm& FSM, int ST, int K)
43 {
44     return gnuradio::get_initial_sptr(new encoder_impl<IN_T, OUT_T>(FSM, ST, K, true));
45 }
46 
47 template <class IN_T, class OUT_T>
encoder_impl(const fsm & FSM,int ST,int K,bool B)48 encoder_impl<IN_T, OUT_T>::encoder_impl(const fsm& FSM, int ST, int K, bool B)
49     : sync_block("encoder<IN_T,OUT_T>",
50                  io_signature::make(1, 1, sizeof(IN_T)),
51                  io_signature::make(1, 1, sizeof(OUT_T))),
52       d_FSM(FSM),
53       d_ST(ST),
54       d_K(K),
55       d_B(B)
56 {
57 }
58 
59 template <class IN_T, class OUT_T>
set_FSM(const fsm & FSM)60 void encoder_impl<IN_T, OUT_T>::set_FSM(const fsm& FSM)
61 {
62     gr::thread::scoped_lock guard(this->d_setlock);
63     d_FSM = FSM;
64 }
65 
66 template <class IN_T, class OUT_T>
set_ST(int ST)67 void encoder_impl<IN_T, OUT_T>::set_ST(int ST)
68 {
69     gr::thread::scoped_lock guard(this->d_setlock);
70     d_ST = ST;
71 }
72 
73 template <class IN_T, class OUT_T>
set_K(int K)74 void encoder_impl<IN_T, OUT_T>::set_K(int K)
75 {
76     gr::thread::scoped_lock guard(this->d_setlock);
77     d_K = K;
78 }
79 
80 template <class IN_T, class OUT_T>
~encoder_impl()81 encoder_impl<IN_T, OUT_T>::~encoder_impl()
82 {
83 }
84 
85 template <class IN_T, class OUT_T>
work(int noutput_items,gr_vector_const_void_star & input_items,gr_vector_void_star & output_items)86 int encoder_impl<IN_T, OUT_T>::work(int noutput_items,
87                                     gr_vector_const_void_star& input_items,
88                                     gr_vector_void_star& output_items)
89 {
90     gr::thread::scoped_lock guard(this->d_setlock);
91     int ST_tmp = 0;
92 
93     if (d_B) { // blockwise operation
94         int nblocks = noutput_items / d_K;
95         const IN_T* in = (const IN_T*)input_items[0];
96         OUT_T* out = (OUT_T*)output_items[0];
97         for (int n = 0; n < nblocks; n++) {
98             ST_tmp = d_ST;
99             for (int i = 0; i < d_K; i++) {
100                 out[n * d_K + i] =
101                     (OUT_T)d_FSM.OS()[ST_tmp * d_FSM.I() + in[n * d_K + i]];
102                 ST_tmp = (int)d_FSM.NS()[ST_tmp * d_FSM.I() + in[n * d_K + i]];
103             }
104         }
105         return nblocks * d_K;
106     }      // end blockwise operation
107     else { // streaming operation
108         const IN_T* in = (const IN_T*)input_items[0];
109         OUT_T* out = (OUT_T*)output_items[0];
110         ST_tmp = d_ST;
111         for (int i = 0; i < noutput_items; i++) {
112             out[i] = (OUT_T)d_FSM.OS()[ST_tmp * d_FSM.I() + in[i]];
113             ST_tmp = (int)d_FSM.NS()[ST_tmp * d_FSM.I() + in[i]];
114         }
115         d_ST = ST_tmp;
116         return noutput_items;
117     } // end streaming operation
118 }
119 template class encoder<std::uint8_t, std::uint8_t>;
120 template class encoder<std::uint8_t, std::int16_t>;
121 template class encoder<std::uint8_t, std::int32_t>;
122 template class encoder<std::int16_t, std::int16_t>;
123 template class encoder<std::int16_t, std::int32_t>;
124 template class encoder<std::int32_t, std::int32_t>;
125 
126 
127 } /* namespace trellis */
128 } /* namespace gr */
129