Statistics
| Branch: | Tag: | Revision:

root / host / lib / convert / gen_convert_pred.py @ 16b54d61

History | View | Annotate | Download (6.44 KB)

1
#!/usr/bin/env python
2
#
3
# Copyright 2011-2011 Ettus Research LLC
4
#
5
# This program is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation, either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
#
18

    
19
TMPL_TEXT = """
20
#import time
21
/***********************************************************************
22
 * This file was generated by $file on $time.strftime("%c")
23
 **********************************************************************/
24
\#include <uhd/exception.hpp>
25
\#include <boost/tokenizer.hpp>
26
\#include <boost/lexical_cast.hpp>
27
\#include <boost/detail/endian.hpp>
28
\#include <boost/cstdint.hpp>
29
\#include <string>
30
\#include <vector>
31

32
typedef size_t pred_type;
33
typedef std::vector<pred_type> pred_vector_type;
34

35
enum dir_type{
36
    DIR_OTW_TO_CPU = 0,
37
    DIR_CPU_TO_OTW = 1
38
};
39

40
struct pred_error : uhd::value_error{
41
    pred_error(const std::string &what):
42
        uhd::value_error("convert::make_pred: " + what)
43
    {
44
        /* NOP */
45
    }
46
};
47

48
pred_type make_pred(const std::string &markup, dir_type &dir){
49
    pred_type pred = 0;
50

51
    try{
52
        boost::tokenizer<boost::char_separator<char> > tokenizer(markup, boost::char_separator<char>("_"));
53
        std::vector<std::string> tokens(tokenizer.begin(), tokenizer.end());
54
        //token 0 is <convert>
55
        std::string inp_type = tokens.at(1);
56
        std::string num_inps = tokens.at(2);
57
        //token 3 is <to>
58
        std::string out_type = tokens.at(4);
59
        std::string num_outs = tokens.at(5);
60
        std::string swap_type = tokens.at(6);
61

62
        std::string cpu_type, otw_type;
63
        if (inp_type.find("item") == std::string::npos){
64
            cpu_type = inp_type;
65
            otw_type = out_type;
66
            dir = DIR_CPU_TO_OTW;
67
        }
68
        else{
69
            cpu_type = out_type;
70
            otw_type = inp_type;
71
            dir = DIR_OTW_TO_CPU;
72
        }
73

74
        if      (cpu_type == "fc64") pred |= $ph.fc64_p;
75
        else if (cpu_type == "fc32") pred |= $ph.fc32_p;
76
        else if (cpu_type == "sc16") pred |= $ph.sc16_p;
77
        else if (cpu_type == "sc8")  pred |= $ph.sc8_p;
78
        else throw pred_error("unhandled io type " + cpu_type);
79

80
        if      (otw_type == "item32") pred |= $ph.item32_p;
81
        else if (otw_type == "item16") pred |= $ph.item16_p;
82
        else throw pred_error("unhandled otw type " + otw_type);
83

84
        int num_inputs = boost::lexical_cast<int>(num_inps);
85
        int num_outputs = boost::lexical_cast<int>(num_outs);
86

87
        switch(num_inputs*num_outputs){ //FIXME treated as one value
88
        case 1: pred |= $ph.chan1_p; break;
89
        case 2: pred |= $ph.chan2_p; break;
90
        case 3: pred |= $ph.chan3_p; break;
91
        case 4: pred |= $ph.chan4_p; break;
92
        default: throw pred_error("unhandled number of channels");
93
        }
94

95
        if      (swap_type == "bswap") pred |= $ph.bswap_p;
96
        else if (swap_type == "nswap") pred |= $ph.nswap_p;
97
        else throw pred_error("unhandled swap type");
98

99
    }
100
    catch(...){
101
        throw pred_error("could not parse markup: " + markup);
102
    }
103

104
    return pred;
105
}
106

107
#define pred_table_wildcard pred_type(~0)
108
#define pred_table_max_size size_t(128)
109
#define pred_table_index(e) (pred_type(e) & 0x7f)
110

111
static pred_vector_type get_pred_byte_order_table(void){
112
    pred_vector_type table(pred_table_max_size, pred_table_wildcard);
113
    \#ifdef BOOST_BIG_ENDIAN
114
    table[pred_table_index(otw_type_t::BO_BIG_ENDIAN)]    = $ph.nswap_p;
115
    table[pred_table_index(otw_type_t::BO_LITTLE_ENDIAN)] = $ph.bswap_p;
116
    \#else
117
    table[pred_table_index(otw_type_t::BO_BIG_ENDIAN)]    = $ph.bswap_p;
118
    table[pred_table_index(otw_type_t::BO_LITTLE_ENDIAN)] = $ph.nswap_p;
119
    \#endif
120
    table[pred_table_index(otw_type_t::BO_NATIVE)]        = $ph.nswap_p;
121
    return table;
122
}
123

124
static pred_vector_type get_pred_io_type_table(void){
125
    pred_vector_type table(pred_table_max_size, pred_table_wildcard);
126
    table[pred_table_index(io_type_t::COMPLEX_FLOAT64)]    = $ph.fc64_p;
127
    table[pred_table_index(io_type_t::COMPLEX_FLOAT32)]    = $ph.fc32_p;
128
    table[pred_table_index(io_type_t::COMPLEX_INT16)]      = $ph.sc16_p;
129
    return table;
130
}
131

132
static pred_vector_type get_pred_num_io_table(void){
133
    pred_vector_type table(pred_table_max_size, pred_table_wildcard);
134
    table[1] = $ph.chan1_p;
135
    table[2] = $ph.chan2_p;
136
    table[3] = $ph.chan3_p;
137
    table[4] = $ph.chan4_p;
138
    return table;
139
}
140

141
static pred_vector_type get_pred_num_otw_table(void){
142
    pred_vector_type table(pred_table_max_size, pred_table_wildcard);
143
    table[4] = $ph.item32_p;
144
    table[2] = $ph.item16_p;
145
    return table;
146
}
147

148
UHD_INLINE pred_type make_pred(
149
    const io_type_t &io_type,
150
    const otw_type_t &otw_type,
151
    size_t num_inputs,
152
    size_t num_outputs
153
){
154
    pred_type pred = 0;
155

156
    static const pred_vector_type pred_otw_type_table(get_pred_num_otw_table());
157
    pred |= pred_otw_type_table[pred_table_index(otw_type.get_sample_size())];
158

159
    static const pred_vector_type pred_byte_order_table(get_pred_byte_order_table());
160
    pred |= pred_byte_order_table[pred_table_index(otw_type.byteorder)];
161

162
    static const pred_vector_type pred_io_type_table(get_pred_io_type_table());
163
    pred |= pred_io_type_table[pred_table_index(io_type.tid)];
164

165
    static const pred_vector_type pred_num_io_table(get_pred_num_io_table());
166
    pred |= pred_num_io_table[pred_table_index(num_inputs*num_outputs)];
167

168
    if (pred == pred_table_wildcard) throw pred_error(
169
        "unhanded input combination for make_pred()"
170
    );
171

172
    return pred;
173
}
174
"""
175

    
176
def parse_tmpl(_tmpl_text, **kwargs):
177
    from Cheetah.Template import Template
178
    return str(Template(_tmpl_text, kwargs))
179

    
180
class ph:
181
    bswap_p  = 0b000001
182
    nswap_p  = 0b000000
183
    sc8_p    = 0b000000
184
    sc16_p   = 0b000010
185
    fc32_p   = 0b000100
186
    fc64_p   = 0b000110
187
    chan1_p  = 0b000000
188
    chan2_p  = 0b001000
189
    chan3_p  = 0b010000
190
    chan4_p  = 0b011000
191
    item32_p = 0b000000
192
    item16_p = 0b100000
193

    
194
if __name__ == '__main__':
195
    import sys, os
196
    file = os.path.basename(__file__)
197
    open(sys.argv[1], 'w').write(parse_tmpl(TMPL_TEXT, file=file, ph=ph))