edfa4ec37b0c71f1458bf1cd23c8bb93e57357b9
[debian/gnuradio] / vrt / include / vrt / types.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2009 Free Software Foundation, Inc.
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 #ifndef INCLUDED_VRT_TYPES_H
19 #define INCLUDED_VRT_TYPES_H
20
21 #include <stdint.h>
22
23 /* macros for dealing with fixed point numbers */
24 #define _FXPT_C(_type, _x, _rp) ((_type)((_x)*(1ll << _rp)))
25 #define _FXPT_TO_INT(_x, _one) (((_x) + ((_one)/2))/(_one))
26 #define _FXPT_TO_DOUBLE(_x, _one) ((double)(_x) * (1.0/(_one)))
27
28 /***********************************************************************
29  * The VRT Altitude Type (meters)
30  **********************************************************************/
31 typedef int32_t vrt_altitude_t;
32 #define VRT_ALTITUDE_RP 5
33 #define VRT_ALTITUDE_C(_x) _FXPT_C(vrt_altitude_t, _x, VRT_ALTITUDE_RP)
34
35 static inline vrt_altitude_t
36 double_to_vrt_altitude(double num){
37   return VRT_ALTITUDE_C(num);
38 }
39
40 static inline int32_t
41 vrt_altitude_round_to_int(vrt_altitude_t fx){
42   return _FXPT_TO_INT(fx, VRT_ALTITUDE_C(1));
43 }
44
45 static inline double
46 vrt_altitude_to_double(vrt_altitude_t fx){
47   return _FXPT_TO_DOUBLE(fx, VRT_ALTITUDE_C(1));
48 }
49
50 /***********************************************************************
51  * The VRT Geolocation Angle Type (degrees)
52  **********************************************************************/
53 typedef int32_t vrt_geo_angle_t;
54 #define VRT_GEO_ANGLE_RP 22
55 #define VRT_GEO_ANGLE_C(_x) _FXPT_C(vrt_geo_angle_t, _x, VRT_GEO_ANGLE_RP)
56
57 static inline vrt_geo_angle_t
58 double_to_vrt_geo_angle(double num){
59   return VRT_GEO_ANGLE_C(num);
60 }
61
62 static inline int16_t
63 vrt_geo_angle_round_to_int(vrt_geo_angle_t fx){
64   return _FXPT_TO_INT(fx, VRT_GEO_ANGLE_C(1));
65 }
66
67 static inline double
68 vrt_geo_angle_to_double(vrt_geo_angle_t fx){
69   return _FXPT_TO_DOUBLE(fx, VRT_GEO_ANGLE_C(1));
70 }
71
72 /***********************************************************************
73  * The VRT Frequency Type (Hz)
74  **********************************************************************/
75 typedef int64_t vrt_freq_t;
76 #define VRT_FREQ_RP 20
77 #define VRT_FREQ_C(_x) _FXPT_C(vrt_freq_t, _x, VRT_FREQ_RP)
78
79 static inline vrt_freq_t
80 double_to_vrt_freq(double num){
81   return VRT_FREQ_C(num);
82 }
83
84 static inline int64_t
85 vrt_freq_round_to_int(vrt_freq_t fx){
86   return _FXPT_TO_INT(fx, VRT_FREQ_C(1));
87 }
88
89 static inline double
90 vrt_freq_to_double(vrt_freq_t fx){
91   return _FXPT_TO_DOUBLE(fx, VRT_FREQ_C(1));
92 }
93
94 /***********************************************************************
95  * The VRT Gain Type (dB)
96  **********************************************************************/
97 typedef int16_t vrt_gain_t;
98 #define VRT_GAIN_RP 7
99 #define VRT_GAIN_C(_x) _FXPT_C(vrt_gain_t, _x, VRT_GAIN_RP)
100
101 static inline vrt_gain_t
102 double_to_vrt_gain(double num){
103   return VRT_GAIN_C(num);
104 }
105
106 static inline int16_t
107 vrt_gain_round_to_int(vrt_gain_t fx){
108   return _FXPT_TO_INT(fx, VRT_GAIN_C(1));
109 }
110
111 static inline double
112 vrt_gain_to_double(vrt_gain_t fx){
113   return _FXPT_TO_DOUBLE(fx, VRT_GAIN_C(1));
114 }
115
116 /***********************************************************************
117  * The VRT Temperature Type (Celcius)
118  **********************************************************************/
119 typedef int16_t vrt_temp_t;
120 #define VRT_TEMP_RP 6
121 #define VRT_TEMP_C(_x) _FXPT_C(vrt_temp_t, _x, VRT_TEMP_RP)
122
123 static inline vrt_temp_t
124 double_to_vrt_temp(double num){
125   return VRT_TEMP_C(num);
126 }
127
128 static inline int16_t
129 vrt_temp_round_to_int(vrt_temp_t fx){
130   return _FXPT_TO_INT(fx, VRT_TEMP_C(1));
131 }
132
133 static inline double
134 vrt_temp_to_double(vrt_temp_t fx){
135   return _FXPT_TO_DOUBLE(fx, VRT_TEMP_C(1));
136 }
137
138 #endif /* INCLUDED_VRT_TYPES_H */