bcf549680653fc65a6fa5b367382bba61446fb77
[debian/gnuradio] / gnuradio-core / src / lib / gengen / gr_unpacked_to_packed_bb.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2006 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 // WARNING: this file is machine generated.  Edits will be over written
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <gr_unpacked_to_packed_bb.h>
30 #include <gr_io_signature.h>
31 #include <assert.h>
32
33 static const unsigned int BITS_PER_TYPE = sizeof(unsigned char) * 8;
34
35
36 gr_unpacked_to_packed_bb_sptr 
37 gr_make_unpacked_to_packed_bb (unsigned int bits_per_chunk, gr_endianness_t endianness)
38 {
39   return gr_unpacked_to_packed_bb_sptr 
40     (new gr_unpacked_to_packed_bb (bits_per_chunk,endianness));
41 }
42
43 gr_unpacked_to_packed_bb::gr_unpacked_to_packed_bb (unsigned int bits_per_chunk, 
44                                                     gr_endianness_t endianness)
45   : gr_block ("unpacked_to_packed_bb",
46               gr_make_io_signature (1, -1, sizeof (unsigned char)),
47               gr_make_io_signature (1, -1, sizeof (unsigned char))),
48     d_bits_per_chunk(bits_per_chunk),d_endianness(endianness),d_index(0)
49 {
50   assert (bits_per_chunk <= BITS_PER_TYPE);
51   assert (bits_per_chunk > 0);
52
53   set_relative_rate (bits_per_chunk/(1.0 * BITS_PER_TYPE));
54 }
55
56 void
57 gr_unpacked_to_packed_bb::forecast(int noutput_items, gr_vector_int &ninput_items_required)
58 {
59   int input_required = (int) ceil( (d_index+noutput_items * 1.0 * BITS_PER_TYPE)/d_bits_per_chunk);
60   unsigned ninputs = ninput_items_required.size();
61   for (unsigned int i = 0; i < ninputs; i++) {
62     ninput_items_required[i] = input_required;
63   }
64 }
65
66 unsigned int
67 get_bit_be1 (const unsigned char *in_vector,unsigned int bit_addr, unsigned int bits_per_chunk) {
68   unsigned int byte_addr = (int)bit_addr/bits_per_chunk;
69   unsigned char x = in_vector[byte_addr];
70   unsigned int residue = bit_addr - byte_addr * bits_per_chunk;
71   //printf("Bit addr %d  byte addr %d  residue %d  val  %d\n",bit_addr,byte_addr,residue,(x>>(bits_per_chunk-1-residue))&1);
72   return (x >> (bits_per_chunk-1-residue))&1;
73 }
74
75 int
76 gr_unpacked_to_packed_bb::general_work (int noutput_items,
77                                         gr_vector_int &ninput_items,
78                                         gr_vector_const_void_star &input_items,
79                                         gr_vector_void_star &output_items)
80 {
81   unsigned int index_tmp = d_index;
82
83   assert (input_items.size() == output_items.size());
84   int nstreams = input_items.size();
85
86   for (int m=0; m< nstreams; m++) {
87     const unsigned char *in = (unsigned char *) input_items[m];
88     unsigned char *out = (unsigned char *) output_items[m];
89     index_tmp=d_index;
90
91     // per stream processing
92
93     //assert((ninput_items[m]-d_index)*d_bits_per_chunk >= noutput_items*BITS_PER_TYPE);
94   
95     switch(d_endianness){
96
97     case GR_MSB_FIRST:
98       for(int i=0;i<noutput_items;i++) {
99         unsigned char tmp=0;
100         for(unsigned int j=0; j<BITS_PER_TYPE; j++) {
101           tmp = (tmp<<1) | get_bit_be1(in,index_tmp,d_bits_per_chunk);
102           index_tmp++;
103         }
104         out[i] = tmp;
105       }
106       break;
107
108     case GR_LSB_FIRST:
109       for(int i=0;i<noutput_items;i++) {
110         unsigned long tmp=0;
111         for(unsigned int j=0; j<BITS_PER_TYPE; j++) {
112           tmp = (tmp>>1)| (get_bit_be1(in,index_tmp,d_bits_per_chunk)<<(BITS_PER_TYPE-1));
113           index_tmp++;
114         }
115         out[i] = tmp;
116       }
117       break;
118       
119     default:
120       assert(0);
121     }
122   }
123
124   d_index = index_tmp;
125   consume_each ((int)(d_index/d_bits_per_chunk));
126   d_index = d_index%d_bits_per_chunk;
127
128   return noutput_items;
129 }