1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
/* -*- c++ -*- */
/*
* Copyright 2003,2005,2008 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* GNU Radio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* GNU Radio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
/*
* mathematical odds and ends.
*/
#ifndef _GR_MATH_H_
#define _GR_MATH_H_
#include <gr_core_api.h>
#include <gr_complex.h>
static inline bool
gr_is_power_of_2(long x)
{
return x != 0 && (x & (x-1)) == 0;
}
/*!
* \brief Fast arc tangent using table lookup and linear interpolation
* \ingroup misc
*
* \param y component of input vector
* \param x component of input vector
* \returns float angle angle of vector (x, y) in radians
*
* This function calculates the angle of the vector (x,y) based on a
* table lookup and linear interpolation. The table uses a 256 point
* table covering -45 to +45 degrees and uses symetry to determine the
* final angle value in the range of -180 to 180 degrees. Note that
* this function uses the small angle approximation for values close
* to zero. This routine calculates the arc tangent with an average
* error of +/- 0.045 degrees.
*/
GR_CORE_API float gr_fast_atan2f(float y, float x);
static inline float gr_fast_atan2f(gr_complex z)
{
return gr_fast_atan2f(z.imag(), z.real());
}
/* This bounds x by +/- clip without a branch */
static inline float gr_branchless_clip(float x, float clip)
{
float x1 = fabsf(x+clip);
float x2 = fabsf(x-clip);
x1 -= x2;
return 0.5*x1;
}
static inline float gr_clip(float x, float clip)
{
float y = x;
if(x > clip)
y = clip;
else if(x < -clip)
y = -clip;
return y;
}
// Slicer Functions
static inline unsigned int gr_binary_slicer(float x)
{
if(x >= 0)
return 1;
else
return 0;
}
static inline unsigned int gr_quad_45deg_slicer(float r, float i)
{
unsigned int ret = 0;
if((r >= 0) && (i >= 0))
ret = 0;
else if((r < 0) && (i >= 0))
ret = 1;
else if((r < 0) && (i < 0))
ret = 2;
else
ret = 3;
return ret;
}
static inline unsigned int gr_quad_0deg_slicer(float r, float i)
{
unsigned int ret = 0;
if(fabsf(r) > fabsf(i)) {
if(r > 0)
ret = 0;
else
ret = 2;
}
else {
if(i > 0)
ret = 1;
else
ret = 3;
}
return ret;
}
static inline unsigned int gr_quad_45deg_slicer(gr_complex x)
{
return gr_quad_45deg_slicer(x.real(), x.imag());
}
static inline unsigned int gr_quad_0deg_slicer(gr_complex x)
{
return gr_quad_0deg_slicer(x.real(), x.imag());
}
// Branchless Slicer Functions
static inline unsigned int gr_branchless_binary_slicer(float x)
{
return (x >= 0);
}
static inline unsigned int gr_branchless_quad_0deg_slicer(float r, float i)
{
unsigned int ret = 0;
ret = (fabsf(r) > fabsf(i)) * (((r < 0) << 0x1)); // either 0 (00) or 2 (10)
ret |= (fabsf(i) > fabsf(r)) * (((i < 0) << 0x1) | 0x1); // either 1 (01) or 3 (11)
return ret;
}
static inline unsigned int gr_branchless_quad_0deg_slicer(gr_complex x)
{
return gr_branchless_quad_0deg_slicer(x.real(), x.imag());
}
static inline unsigned int gr_branchless_quad_45deg_slicer(float r, float i)
{
char ret = (r <= 0);
ret |= ((i <= 0) << 1);
return (ret ^ ((ret & 0x2) >> 0x1));
}
static inline unsigned int gr_branchless_quad_45deg_slicer(gr_complex x)
{
return gr_branchless_quad_45deg_slicer(x.real(), x.imag());
}
/*!
* \param x any value
* \param pow2 must be a power of 2
* \returns \p x rounded down to a multiple of \p pow2.
*/
static inline size_t
gr_p2_round_down(size_t x, size_t pow2)
{
return x & -pow2;
}
/*!
* \param x any value
* \param pow2 must be a power of 2
* \returns \p x rounded up to a multiple of \p pow2.
*/
static inline size_t
gr_p2_round_up(size_t x, size_t pow2)
{
return gr_p2_round_down(x + pow2 - 1, pow2);
}
/*!
* \param x any value
* \param pow2 must be a power of 2
* \returns \p x modulo \p pow2.
*/
static inline size_t
gr_p2_modulo(size_t x, size_t pow2)
{
return x & (pow2 - 1);
}
/*!
* \param x any value
* \param pow2 must be a power of 2
* \returns \p pow2 - (\p x modulo \p pow2).
*/
static inline size_t
gr_p2_modulo_neg(size_t x, size_t pow2)
{
return pow2 - gr_p2_modulo(x, pow2);
}
#endif /* _GR_MATH_H_ */
|