Statistics
| Branch: | Tag: | Revision:

root / host / lib / convert / gen_convert_general.py @ 5a77062d

History | View | Annotate | Download (6.58 KB)

1
#!/usr/bin/env python
2
#
3
# Copyright 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_HEADER = """
20
#import time
21
/***********************************************************************
22
 * This file was generated by $file on $time.strftime("%c")
23
 **********************************************************************/
24

25
\#include "convert_common.hpp"
26
\#include <uhd/utils/byteswap.hpp>
27

28
using namespace uhd::convert;
29
"""
30

    
31
TMPL_CONV_GEN2_ITEM32 = """
32
DECLARE_CONVERTER(item32, 1, sc16_item32_$(end), 1, PRIORITY_GENERAL){
33
    const item32_t *input = reinterpret_cast<const item32_t *>(inputs[0]);
34
    item32_t *output = reinterpret_cast<item32_t *>(outputs[0]);
35

36
    if (scale_factor == 0){} //avoids unused warning
37

38
    for (size_t i = 0; i < nsamps; i++){
39
        output[i] = $(to_wire)(input[i]);
40
    }
41
}
42

43
DECLARE_CONVERTER(sc16_item32_$(end), 1, item32, 1, PRIORITY_GENERAL){
44
    const item32_t *input = reinterpret_cast<const item32_t *>(inputs[0]);
45
    item32_t *output = reinterpret_cast<item32_t *>(outputs[0]);
46

47
    if (scale_factor == 0){} //avoids unused warning
48

49
    for (size_t i = 0; i < nsamps; i++){
50
        output[i] = $(to_host)(input[i]);
51
    }
52
}
53
"""
54

    
55
TMPL_CONV_GEN2_COMPLEX = """
56
DECLARE_CONVERTER($(cpu_type), 1, sc16_item32_$(end), 1, PRIORITY_GENERAL){
57
    const $(cpu_type)_t *input = reinterpret_cast<const $(cpu_type)_t *>(inputs[0]);
58
    item32_t *output = reinterpret_cast<item32_t *>(outputs[0]);
59

60
    for (size_t i = 0; i < nsamps; i++){
61
        output[i] = $(to_wire)($(cpu_type)_to_item32_sc16(input[i], scale_factor));
62
    }
63
}
64

65
DECLARE_CONVERTER(sc16_item32_$(end), 1, $(cpu_type), 1, PRIORITY_GENERAL){
66
    const item32_t *input = reinterpret_cast<const item32_t *>(inputs[0]);
67
    $(cpu_type)_t *output = reinterpret_cast<$(cpu_type)_t *>(outputs[0]);
68

69
    for (size_t i = 0; i < nsamps; i++){
70
        output[i] = item32_sc16_to_$(cpu_type)($(to_host)(input[i]), scale_factor);
71
    }
72
}
73

74
DECLARE_CONVERTER(sc8_item32_$(end), 1, $(cpu_type), 1, PRIORITY_GENERAL){
75
    const item32_t *input = reinterpret_cast<const item32_t *>(size_t(inputs[0]) & ~0x3);
76
    $(cpu_type)_t *output = reinterpret_cast<$(cpu_type)_t *>(outputs[0]);
77
    $(cpu_type)_t dummy;
78
    size_t num_samps = nsamps;
79

80
    if ((size_t(inputs[0]) & 0x3) != 0){
81
        const item32_t item0 = $(to_host)(*input++);
82
        item32_sc8_to_$(cpu_type)(item0, dummy, *output++, scale_factor);
83
        num_samps--;
84
    }
85

86
    const size_t num_pairs = num_samps/2;
87
    for (size_t i = 0, j = 0; i < num_pairs; i++, j+=2){
88
        const item32_t item_i = $(to_host)(input[i]);
89
        item32_sc8_to_$(cpu_type)(item_i, output[j], output[j+1], scale_factor);
90
    }
91

92
    if (num_samps != num_pairs*2){
93
        const item32_t item_n = $(to_host)(input[num_pairs]);
94
        item32_sc8_to_$(cpu_type)(item_n, output[num_samps-1], dummy, scale_factor);
95
    }
96
}
97
"""
98

    
99
TMPL_CONV_USRP1_COMPLEX = """
100
DECLARE_CONVERTER($(cpu_type), $(width), sc16_item16_usrp1, 1, PRIORITY_GENERAL){
101
    #for $w in range($width)
102
    const $(cpu_type)_t *input$(w) = reinterpret_cast<const $(cpu_type)_t *>(inputs[$(w)]);
103
    #end for
104
    boost::uint16_t *output = reinterpret_cast<boost::uint16_t *>(outputs[0]);
105

106
    if (scale_factor == 0){} //avoids unused warning
107

108
    for (size_t i = 0, j = 0; i < nsamps; i++){
109
        #for $w in range($width)
110
        output[j++] = $(to_wire)(boost::int16_t(input$(w)[i].real()$(do_scale)));
111
        output[j++] = $(to_wire)(boost::int16_t(input$(w)[i].imag()$(do_scale)));
112
        #end for
113
    }
114
}
115

116
DECLARE_CONVERTER(sc16_item16_usrp1, 1, $(cpu_type), $(width), PRIORITY_GENERAL){
117
    const boost::uint16_t *input = reinterpret_cast<const boost::uint16_t *>(inputs[0]);
118
    #for $w in range($width)
119
    $(cpu_type)_t *output$(w) = reinterpret_cast<$(cpu_type)_t *>(outputs[$(w)]);
120
    #end for
121

122
    if (scale_factor == 0){} //avoids unused warning
123

124
    for (size_t i = 0, j = 0; i < nsamps; i++){
125
        #for $w in range($width)
126
        output$(w)[i] = $(cpu_type)_t(
127
            boost::int16_t($(to_host)(input[j+0]))$(do_scale),
128
            boost::int16_t($(to_host)(input[j+1]))$(do_scale)
129
        );
130
        j += 2;
131
        #end for
132
    }
133
}
134

135
DECLARE_CONVERTER(sc8_item16_usrp1, 1, $(cpu_type), $(width), PRIORITY_GENERAL){
136
    const boost::uint16_t *input = reinterpret_cast<const boost::uint16_t *>(inputs[0]);
137
    #for $w in range($width)
138
    $(cpu_type)_t *output$(w) = reinterpret_cast<$(cpu_type)_t *>(outputs[$(w)]);
139
    #end for
140

141
    if (scale_factor == 0){} //avoids unused warning
142

143
    for (size_t i = 0, j = 0; i < nsamps; i++){
144
        #for $w in range($width)
145
        {
146
        const boost::uint16_t num = $(to_host)(input[j++]);
147
        output$(w)[i] = $(cpu_type)_t(
148
            boost::int8_t(num)$(do_scale),
149
            boost::int8_t(num >> 8)$(do_scale)
150
        );
151
        }
152
        #end for
153
    }
154
}
155
"""
156

    
157
def parse_tmpl(_tmpl_text, **kwargs):
158
    from Cheetah.Template import Template
159
    return str(Template(_tmpl_text, kwargs))
160

    
161
if __name__ == '__main__':
162
    import sys, os
163
    file = os.path.basename(__file__)
164
    output = parse_tmpl(TMPL_HEADER, file=file)
165

    
166
    #generate complex converters for all gen2 platforms
167
    for end, to_host, to_wire in (
168
        ('be', 'uhd::ntohx', 'uhd::htonx'),
169
        ('le', 'uhd::wtohx', 'uhd::htowx'),
170
    ):
171
        for cpu_type in 'fc64', 'fc32', 'sc16':
172
            output += parse_tmpl(
173
                TMPL_CONV_GEN2_COMPLEX,
174
                end=end, to_host=to_host, to_wire=to_wire, cpu_type=cpu_type
175
            )
176
        output += parse_tmpl(
177
                TMPL_CONV_GEN2_ITEM32,
178
                end=end, to_host=to_host, to_wire=to_wire
179
            )
180

    
181
    #generate complex converters for usrp1 format
182
    for width in 1, 2, 4:
183
        for cpu_type, do_scale in (
184
            ('fc64', '*scale_factor'),
185
            ('fc32', '*float(scale_factor)'),
186
            ('sc16', ''),
187
        ):
188
            output += parse_tmpl(
189
                TMPL_CONV_USRP1_COMPLEX,
190
                width=width, to_host='uhd::wtohx', to_wire='uhd::htowx',
191
                cpu_type=cpu_type, do_scale=do_scale
192
            )
193
    open(sys.argv[1], 'w').write(output)