Statistics
| Branch: | Tag: | Revision:

root / host / lib / convert / convert_common.hpp @ 43b19815

History | View | Annotate | Download (4.35 KB)

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

    
18
#ifndef INCLUDED_LIBUHD_CONVERT_COMMON_HPP
19
#define INCLUDED_LIBUHD_CONVERT_COMMON_HPP
20

    
21
#include <uhd/convert.hpp>
22
#include <uhd/utils/static.hpp>
23
#include <boost/cstdint.hpp>
24
#include <complex>
25

    
26
#define DECLARE_CONVERTER(fcn, prio) \
27
    static void fcn( \
28
        uhd::convert::input_type &inputs, \
29
        uhd::convert::output_type &outputs, \
30
        size_t nsamps \
31
    ); \
32
    UHD_STATIC_BLOCK(register_##fcn##_##prio){ \
33
        uhd::convert::register_converter(#fcn, fcn, prio); \
34
    } \
35
    static void fcn( \
36
        uhd::convert::input_type &inputs, \
37
        uhd::convert::output_type &outputs, \
38
        size_t nsamps \
39
    )
40

    
41
/***********************************************************************
42
 * Typedefs
43
 **********************************************************************/
44
typedef std::complex<double>         fc64_t;
45
typedef std::complex<float>          fc32_t;
46
typedef std::complex<boost::int16_t> sc16_t;
47
typedef std::complex<boost::int8_t>  sc8_t;
48
typedef boost::uint32_t              item32_t;
49

    
50
/***********************************************************************
51
 * Convert complex short buffer to items32
52
 **********************************************************************/
53
static UHD_INLINE item32_t sc16_to_item32(sc16_t num){
54
    boost::uint16_t real = num.real();
55
    boost::uint16_t imag = num.imag();
56
    return (item32_t(real) << 16) | (item32_t(imag) << 0);
57
}
58

    
59
/***********************************************************************
60
 * Convert items32 buffer to complex short
61
 **********************************************************************/
62
static UHD_INLINE sc16_t item32_to_sc16(item32_t item){
63
    return sc16_t(
64
        boost::int16_t(item >> 16),
65
        boost::int16_t(item >> 0)
66
    );
67
}
68

    
69
/***********************************************************************
70
 * Convert complex float buffer to items32 (no swap)
71
 **********************************************************************/
72
static const float shorts_per_float = float(32767);
73

    
74
static UHD_INLINE item32_t fc32_to_item32(fc32_t num){
75
    boost::uint16_t real = boost::int16_t(num.real()*shorts_per_float);
76
    boost::uint16_t imag = boost::int16_t(num.imag()*shorts_per_float);
77
    return (item32_t(real) << 16) | (item32_t(imag) << 0);
78
}
79

    
80
/***********************************************************************
81
 * Convert items32 buffer to complex float
82
 **********************************************************************/
83
static const float floats_per_short = float(1.0/shorts_per_float);
84

    
85
static UHD_INLINE fc32_t item32_to_fc32(item32_t item){
86
    return fc32_t(
87
        float(boost::int16_t(item >> 16)*floats_per_short),
88
        float(boost::int16_t(item >> 0)*floats_per_short)
89
    );
90
}
91

    
92
/***********************************************************************
93
 * Convert complex double buffer to items32 (no swap)
94
 **********************************************************************/
95
static const double shorts_per_double = double(32767);
96

    
97
static UHD_INLINE item32_t fc64_to_item32(fc64_t num){
98
    boost::uint16_t real = boost::int16_t(num.real()*shorts_per_double);
99
    boost::uint16_t imag = boost::int16_t(num.imag()*shorts_per_double);
100
    return (item32_t(real) << 16) | (item32_t(imag) << 0);
101
}
102

    
103
/***********************************************************************
104
 * Convert items32 buffer to complex double
105
 **********************************************************************/
106
static const double doubles_per_short = double(1.0/shorts_per_double);
107

    
108
static UHD_INLINE fc64_t item32_to_fc64(item32_t item){
109
    return fc64_t(
110
        float(boost::int16_t(item >> 16)*doubles_per_short),
111
        float(boost::int16_t(item >> 0)*doubles_per_short)
112
    );
113
}
114

    
115
#endif /* INCLUDED_LIBUHD_CONVERT_COMMON_HPP */