Houston, we have a trunk.
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_packed_to_unpacked_XX.cc.t
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004 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 2, 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., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 // @WARNING@
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <@NAME@.h>
30 #include <gr_io_signature.h>
31 #include <assert.h>
32 #include <gr_log2_const.h>
33
34 static const unsigned int BITS_PER_TYPE = sizeof(@I_TYPE@) * 8;
35 static const unsigned int LOG2_L_TYPE = gr_log2_const<sizeof(@I_TYPE@) * 8>();
36
37
38 @SPTR_NAME@ 
39 gr_make_@BASE_NAME@ (unsigned int bits_per_chunk, gr_endianness_t endianness)
40 {
41   return @SPTR_NAME@ 
42     (new @NAME@ (bits_per_chunk,endianness));
43 }
44
45 @NAME@::@NAME@ (unsigned int bits_per_chunk, 
46                                                     gr_endianness_t endianness)
47   : gr_block ("@BASE_NAME@",
48               gr_make_io_signature (1, -1, sizeof (@I_TYPE@)),
49               gr_make_io_signature (1, -1, sizeof (@O_TYPE@))),
50     d_bits_per_chunk(bits_per_chunk),d_endianness(endianness),d_index(0)
51 {
52   assert (bits_per_chunk <= BITS_PER_TYPE);
53   assert (bits_per_chunk > 0);
54
55   set_relative_rate ((1.0 * BITS_PER_TYPE) / bits_per_chunk);
56 }
57
58 void
59 @NAME@::forecast(int noutput_items, gr_vector_int &ninput_items_required)
60 {
61
62   int input_required = (int) ceil((d_index + noutput_items * d_bits_per_chunk) / (1.0 * BITS_PER_TYPE));
63   unsigned ninputs = ninput_items_required.size();
64   for (unsigned int i = 0; i < ninputs; i++) {
65     ninput_items_required[i] = input_required;
66     //printf("Forecast wants %d needs %d\n",noutput_items,ninput_items_required[i]);
67   }
68 }
69
70 unsigned int
71 get_bit_le (const @I_TYPE@ *in_vector,unsigned int bit_addr)
72 {
73   @I_TYPE@ x = in_vector[bit_addr>>LOG2_L_TYPE];
74   return (x>>(bit_addr&(BITS_PER_TYPE-1)))&1;
75 }
76
77 unsigned int
78 get_bit_be (const @I_TYPE@ *in_vector,unsigned int bit_addr)
79 {
80   @I_TYPE@ x = in_vector[bit_addr>>LOG2_L_TYPE];
81   return (x>>((BITS_PER_TYPE-1)-(bit_addr&(BITS_PER_TYPE-1))))&1;
82 }
83
84 int
85 @NAME@::general_work (int noutput_items,
86                                         gr_vector_int &ninput_items,
87                                         gr_vector_const_void_star &input_items,
88                                         gr_vector_void_star &output_items)
89 {
90   assert (input_items.size() == output_items.size());
91   int nstreams = input_items.size();
92
93   for (int m=0; m < nstreams; m++){
94     const @I_TYPE@ *in = (@I_TYPE@ *) input_items[m];
95     @O_TYPE@ *out = (@O_TYPE@ *) output_items[m];
96
97     // per stream processing
98
99     switch (d_endianness){
100
101     case GR_MSB_FIRST:
102       for (int i = 0; i < noutput_items; i++){
103         //printf("here msb %d\n",i);
104         @O_TYPE@ x = 0;
105         for(unsigned int j=0; j<d_bits_per_chunk; j++, d_index++)
106           x = (x<<1) | get_bit_be(in, d_index);
107         out[i] = x;
108       }
109       break;
110
111     case GR_LSB_FIRST:
112       for (int i = 0; i < noutput_items; i++){
113         //printf("here lsb %d\n",i);
114         @O_TYPE@ x = 0;
115         for(unsigned int j=0; j<d_bits_per_chunk; j++, d_index++)
116           x = (x<<1) | get_bit_le(in, d_index);
117         out[i] = x;
118       }
119       break;
120
121     default:
122       assert(0);
123     }
124
125     //printf("almost got to end\n");
126     assert(ninput_items[m] >= (int) ((d_index+(BITS_PER_TYPE-1))>>LOG2_L_TYPE));
127   }
128
129   consume_each (d_index >> LOG2_L_TYPE);
130   d_index = d_index & (BITS_PER_TYPE-1);
131   //printf("got to end\n");
132   return noutput_items;
133 }