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
210
211
212
|
/* -*- c++ -*- */
/*
* Copyright 2003,2005,2008,2013,2018 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*/
/*
* mathematical odds and ends.
*/
#ifndef _GR_MATH_H_
#define _GR_MATH_H_
#include <gnuradio/api.h>
#include <gnuradio/gr_complex.h>
#include <cmath>
/*
* \brief Define commonly used mathematical constants
* \ingroup misc
*
* Mathematical constants are neither defined in the C standard
* nor the C++ standard. For -std=c{++}11 M_LOG2E and M_SQRT2 won't
* compile. GR_M_PI actually works with C++ but is defined here for the sake
* of consistency.
*/
#define GR_M_LOG2E 1.4426950408889634074 /* log_2 e */
#define GR_M_PI 3.14159265358979323846 /* pi */
#define GR_M_PI_4 0.78539816339744830961566084582 /* pi/4 */
#define GR_M_TWOPI 6.28318530717958647692 /* 2*pi */
#define GR_M_SQRT2 1.41421356237309504880 /* sqrt(2) */
#define GR_M_ONE_OVER_2PI 0.15915494309189533577 /* 1 / (2*pi) */
#define GR_M_MINUS_TWO_PI -6.28318530717958647692 /* - 2*pi */
namespace gr {
static inline void
fast_cc_multiply(gr_complex& out, const gr_complex cc1, const gr_complex cc2)
{
// The built-in complex.h multiply has significant NaN/INF checking that
// considerably slows down performance. While on some compilers the
// -fcx-limit-range flag can be used, this fast function makes the math consistent
// in terms of performance for the Costas loop.
float o_r, o_i;
o_r = (cc1.real() * cc2.real()) - (cc1.imag() * cc2.imag());
o_i = (cc1.real() * cc2.imag()) + (cc1.imag() * cc2.real());
out.real(o_r);
out.imag(o_i);
}
static inline bool 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 symmetry 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_RUNTIME_API float fast_atan2f(float y, float x);
static inline float fast_atan2f(gr_complex z) { return fast_atan2f(z.imag(), z.real()); }
/* This bounds x by +/- clip without a branch */
static inline float branchless_clip(float x, float clip)
{
return 0.5 * (std::abs(x + clip) - std::abs(x - clip));
}
static inline float 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 binary_slicer(float x)
{
if (x >= 0)
return 1;
else
return 0;
}
static inline unsigned int 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 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 quad_45deg_slicer(gr_complex x)
{
return quad_45deg_slicer(x.real(), x.imag());
}
static inline unsigned int quad_0deg_slicer(gr_complex x)
{
return quad_0deg_slicer(x.real(), x.imag());
}
// Branchless Slicer Functions
static inline unsigned int branchless_binary_slicer(float x) { return (x >= 0); }
static inline unsigned int 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 branchless_quad_0deg_slicer(gr_complex x)
{
return branchless_quad_0deg_slicer(x.real(), x.imag());
}
static inline unsigned int 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 branchless_quad_45deg_slicer(gr_complex x)
{
return 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 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 p2_round_up(size_t x, size_t pow2)
{
return 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 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 p2_modulo_neg(size_t x, size_t pow2)
{
return pow2 - p2_modulo(x, pow2);
}
} /* namespace gr */
#endif /* _GR_MATH_H_ */
|