Statistics
| Branch: | Tag: | Revision:

root / host / lib / convert / gen_convert_pred.py @ 4357f5d3

History | View | Annotate | Download (6.03 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 throw pred_error("unhandled otw type " + otw_type);
82

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

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

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

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

103
    return pred;
104
}
105

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

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

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

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

140
UHD_INLINE pred_type make_pred(
141
    const io_type_t &io_type,
142
    const otw_type_t &otw_type,
143
    size_t num_inputs,
144
    size_t num_outputs
145
){
146
    pred_type pred = $ph.item32_p; //only item32 supported as of now
147

148
    static const pred_vector_type pred_byte_order_table(get_pred_byte_order_table());
149
    pred |= pred_byte_order_table[pred_table_index(otw_type.byteorder)];
150

151
    static const pred_vector_type pred_io_type_table(get_pred_io_type_table());
152
    pred |= pred_io_type_table[pred_table_index(io_type.tid)];
153

154
    static const pred_vector_type pred_num_io_table(get_pred_num_io_table());
155
    pred |= pred_num_io_table[pred_table_index(num_inputs*num_outputs)];
156

157
    if (pred == pred_table_wildcard) throw pred_error(
158
        "unhanded input combination for make_pred()"
159
    );
160

161
    return pred;
162
}
163
"""
164

    
165
def parse_tmpl(_tmpl_text, **kwargs):
166
    from Cheetah.Template import Template
167
    return str(Template(_tmpl_text, kwargs))
168

    
169
class ph:
170
    bswap_p  = 0b00001
171
    nswap_p  = 0b00000
172
    item32_p = 0b00000
173
    sc8_p    = 0b00000
174
    sc16_p   = 0b00010
175
    fc32_p   = 0b00100
176
    fc64_p   = 0b00110
177
    chan1_p  = 0b00000
178
    chan2_p  = 0b01000
179
    chan3_p  = 0b10000
180
    chan4_p  = 0b11000
181

    
182
if __name__ == '__main__':
183
    import sys, os
184
    file = os.path.basename(__file__)
185
    open(sys.argv[1], 'w').write(parse_tmpl(TMPL_TEXT, file=file, ph=ph))