Statistics
| Branch: | Tag: | Revision:

root / host / lib / convert / gen_convert_general.py @ 52e20f9e

History | View | Annotate | Download (3.49 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_TO_FROM_ITEM32_1 = """
32
DECLARE_CONVERTER($(cpu_type), 1, sc16_item32_$(end), 1, PRIORITY_GENERAL){
33
    const $(cpu_type)_t *input = reinterpret_cast<const $(cpu_type)_t *>(inputs[0]);
34
    item32_t *output = reinterpret_cast<item32_t *>(outputs[0]);
35

36
    for (size_t i = 0; i < nsamps; i++){
37
        output[i] = $(to_wire)($(cpu_type)_to_item32(input[i], float(scale_factor)));
38
    }
39
}
40

41
DECLARE_CONVERTER(sc16_item32_$(end), 1, $(cpu_type), 1, PRIORITY_GENERAL){
42
    const item32_t *input = reinterpret_cast<const item32_t *>(inputs[0]);
43
    $(cpu_type)_t *output = reinterpret_cast<$(cpu_type)_t *>(outputs[0]);
44

45
    for (size_t i = 0; i < nsamps; i++){
46
        output[i] = item32_to_$(cpu_type)($(to_host)(input[i]), float(scale_factor));
47
    }
48
}
49
"""
50
TMPL_CONV_TO_FROM_ITEM32_X = """
51
DECLARE_CONVERTER($(cpu_type), $(width), sc16_item32_$(end), 1, PRIORITY_GENERAL){
52
    #for $w in range($width)
53
    const $(cpu_type)_t *input$(w) = reinterpret_cast<const $(cpu_type)_t *>(inputs[$(w)]);
54
    #end for
55
    item32_t *output = reinterpret_cast<item32_t *>(outputs[0]);
56

57
    for (size_t i = 0, j = 0; i < nsamps; i++){
58
        #for $w in range($width)
59
        output[j++] = $(to_wire)($(cpu_type)_to_item32(input$(w)[i], float(scale_factor)));
60
        #end for
61
    }
62
}
63

64
DECLARE_CONVERTER(sc16_item32_$(end), 1, $(cpu_type), $(width), PRIORITY_GENERAL){
65
    const item32_t *input = reinterpret_cast<const item32_t *>(inputs[0]);
66
    #for $w in range($width)
67
    $(cpu_type)_t *output$(w) = reinterpret_cast<$(cpu_type)_t *>(outputs[$(w)]);
68
    #end for
69

70
    for (size_t i = 0, j = 0; i < nsamps; i++){
71
        #for $w in range($width)
72
        output$(w)[i] = item32_to_$(cpu_type)($(to_host)(input[j++]), float(scale_factor));
73
        #end for
74
    }
75
}
76
"""
77

    
78
def parse_tmpl(_tmpl_text, **kwargs):
79
    from Cheetah.Template import Template
80
    return str(Template(_tmpl_text, kwargs))
81

    
82
if __name__ == '__main__':
83
    import sys, os
84
    file = os.path.basename(__file__)
85
    output = parse_tmpl(TMPL_HEADER, file=file)
86
    for width in 1, 2, 3, 4:
87
        for end, to_host, to_wire in (
88
            ('be', 'uhd::ntohx', 'uhd::htonx'),
89
            ('le', 'uhd::wtohx', 'uhd::htowx'),
90
        ):
91
            for cpu_type in 'fc64', 'fc32', 'sc16':
92
                output += parse_tmpl(
93
                    TMPL_CONV_TO_FROM_ITEM32_1 if width == 1 else TMPL_CONV_TO_FROM_ITEM32_X,
94
                    width=width, end=end, to_host=to_host, to_wire=to_wire, cpu_type=cpu_type
95
                )
96
    open(sys.argv[1], 'w').write(output)