Statistics
| Branch: | Tag: | Revision:

root / host / lib / transport / gen_convert_types.py @ 79ea83d6

History | View | Annotate | Download (4.8 kB)

1
#!/usr/bin/env python
2
#
3
# Copyright 2010 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

25
\#include <uhd/config.hpp>
26
\#include <uhd/transport/convert_types.hpp>
27
\#include <boost/cstdint.hpp>
28
\#include <boost/detail/endian.hpp>
29
\#include <stdexcept>
30
\#include "convert_types_impl.hpp"
31

32
using namespace uhd;
33

34
/***********************************************************************
35
 * Generate predicate for jump table
36
 **********************************************************************/
37
UHD_INLINE boost::uint8_t get_pred(
38
    const io_type_t &io_type,
39
    const otw_type_t &otw_type
40
){
41
    boost::uint8_t pred = 0;
42

43
    switch(otw_type.byteorder){
44
    \#ifdef BOOST_BIG_ENDIAN
45
    case otw_type_t::BO_BIG_ENDIAN:    pred |= $ph.nswap_p; break;
46
    case otw_type_t::BO_LITTLE_ENDIAN: pred |= $ph.bswap_p; break;
47
    \#else
48
    case otw_type_t::BO_BIG_ENDIAN:    pred |= $ph.bswap_p; break;
49
    case otw_type_t::BO_LITTLE_ENDIAN: pred |= $ph.nswap_p; break;
50
    \#endif
51
    case otw_type_t::BO_NATIVE:        pred |= $ph.nswap_p; break;
52
    default: throw std::runtime_error("unhandled otw byteorder type");
53
    }
54

55
    switch(otw_type.get_sample_size()){
56
    case sizeof(boost::uint32_t): pred |= $ph.item32_p; break;
57
    default: throw std::runtime_error("unhandled otw sample size");
58
    }
59

60
    switch(io_type.tid){
61
    case io_type_t::COMPLEX_FLOAT32: pred |= $ph.fc32_p; break;
62
    case io_type_t::COMPLEX_INT16:   pred |= $ph.sc16_p; break;
63
    default: throw std::runtime_error("unhandled io type id");
64
    }
65

66
    return pred;
67
}
68

69
/***********************************************************************
70
 * Convert host type to device type
71
 **********************************************************************/
72
void transport::convert_io_type_to_otw_type(
73
    const void *io_buff, const io_type_t &io_type,
74
    void *otw_buff, const otw_type_t &otw_type,
75
    size_t num_samps
76
){
77
    switch(get_pred(io_type, otw_type)){
78
    #for $pred in range(2**$ph.nbits)
79
    case $pred:
80
        #set $out_type = $ph.get_dev_type($pred)
81
        #set $in_type = $ph.get_host_type($pred)
82
        #set $converter = '_'.join([$in_type, 'to', $out_type, $ph.get_swap_type($pred)])
83
        $(converter)((const $(in_type)_t *)io_buff, ($(out_type)_t *)otw_buff, num_samps);
84
        break;
85
    #end for
86
    }
87
}
88

89
/***********************************************************************
90
 * Convert device type to host type
91
 **********************************************************************/
92
void transport::convert_otw_type_to_io_type(
93
    const void *otw_buff, const otw_type_t &otw_type,
94
    void *io_buff, const io_type_t &io_type,
95
    size_t num_samps
96
){
97
    switch(get_pred(io_type, otw_type)){
98
    #for $pred in range(2**$ph.nbits)
99
    case $pred:
100
        #set $out_type = $ph.get_host_type($pred)
101
        #set $in_type = $ph.get_dev_type($pred)
102
        #set $converter = '_'.join([$in_type, 'to', $out_type, $ph.get_swap_type($pred)])
103
        $(converter)((const $(in_type)_t *)otw_buff, ($(out_type)_t *)io_buff, num_samps);
104
        break;
105
    #end for
106
    }
107
}
108

109
"""
110

    
111
def parse_tmpl(_tmpl_text, **kwargs):
112
    from Cheetah.Template import Template
113
    return str(Template(_tmpl_text, kwargs))
114

    
115
class ph:
116
    bswap_p  = 0b00001
117
    nswap_p  = 0b00000
118
    item32_p = 0b00000
119
    sc16_p   = 0b00010
120
    fc32_p   = 0b00000
121

    
122
    nbits = 2 #see above
123

    
124
    @staticmethod
125
    def has(pred, flag): return (pred & flag) == flag
126

    
127
    @staticmethod
128
    def get_swap_type(pred):
129
        if ph.has(pred, ph.bswap_p): return 'bswap'
130
        if ph.has(pred, ph.nswap_p): return 'nswap'
131
        raise NotImplementedError
132

    
133
    @staticmethod
134
    def get_dev_type(pred):
135
        if ph.has(pred, ph.item32_p): return 'item32'
136
        raise NotImplementedError
137

    
138
    @staticmethod
139
    def get_host_type(pred):
140
        if ph.has(pred, ph.sc16_p): return 'sc16'
141
        if ph.has(pred, ph.fc32_p): return 'fc32'
142
        raise NotImplementedError
143

    
144
if __name__ == '__main__':
145
    import sys
146
    open(sys.argv[1], 'w').write(parse_tmpl(TMPL_TEXT, file=__file__, ph=ph))