summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Braun <martin.braun@ettus.com>2014-09-04 11:32:16 +0200
committerMartin Braun <martin.braun@ettus.com>2014-09-04 11:32:16 +0200
commit0cbebe1b8f1fb23fd14630deddc9b0e08ec493b3 (patch)
tree644c5ad09125bf4a0789c0f855cac12f590d10c2
parent01deede32858ef9e2fe4cc937f3245b5b0e6d7c9 (diff)
pmt: Added float conversion
Added to_float() and from_float(). These are basically aliases for *_double() with a type cast for when strict typing is necessary (e.g. SWIG won't accept an f32 value when using from_double()).
-rw-r--r--gnuradio-runtime/include/pmt/pmt.h10
-rw-r--r--gnuradio-runtime/lib/pmt/pmt.cc12
-rw-r--r--gnuradio-runtime/lib/pmt/qa_pmt_prims.cc9
-rwxr-xr-xgnuradio-runtime/python/pmt/qa_pmt.py2
-rw-r--r--gnuradio-runtime/swig/pmt_swig.i2
5 files changed, 35 insertions, 0 deletions
diff --git a/gnuradio-runtime/include/pmt/pmt.h b/gnuradio-runtime/include/pmt/pmt.h
index 3e17571b23..cb6fdf44d5 100644
--- a/gnuradio-runtime/include/pmt/pmt.h
+++ b/gnuradio-runtime/include/pmt/pmt.h
@@ -201,6 +201,7 @@ PMT_API bool is_real(pmt_t obj);
//! Return the pmt value that represents double \p x.
PMT_API pmt_t from_double(double x);
+PMT_API pmt_t from_float(float x);
/*!
* \brief Convert pmt to double if possible.
@@ -211,6 +212,15 @@ PMT_API pmt_t from_double(double x);
*/
PMT_API double to_double(pmt_t x);
+/*!
+ * \brief Convert pmt to float if possible.
+ *
+ * This basically is to_double() with a type-cast; the PMT stores
+ * the value as a double in any case. Use this when strict typing
+ * is required.
+ */
+PMT_API float to_float(pmt_t x);
+
/*
* ------------------------------------------------------------------------
* Complex
diff --git a/gnuradio-runtime/lib/pmt/pmt.cc b/gnuradio-runtime/lib/pmt/pmt.cc
index 082b98a80d..da830e1aed 100644
--- a/gnuradio-runtime/lib/pmt/pmt.cc
+++ b/gnuradio-runtime/lib/pmt/pmt.cc
@@ -368,6 +368,12 @@ from_double(double x)
return pmt_t(new pmt_real(x));
}
+pmt_t
+from_float(float x)
+{
+ return pmt_t(new pmt_real(x));
+}
+
double
to_double(pmt_t x)
{
@@ -379,6 +385,12 @@ to_double(pmt_t x)
throw wrong_type("pmt_to_double", x);
}
+float
+to_float(pmt_t x)
+{
+ return float(to_double(x));
+}
+
////////////////////////////////////////////////////////////////////////////
// Complex
////////////////////////////////////////////////////////////////////////////
diff --git a/gnuradio-runtime/lib/pmt/qa_pmt_prims.cc b/gnuradio-runtime/lib/pmt/qa_pmt_prims.cc
index 2f46b014db..2b3ca32293 100644
--- a/gnuradio-runtime/lib/pmt/qa_pmt_prims.cc
+++ b/gnuradio-runtime/lib/pmt/qa_pmt_prims.cc
@@ -125,6 +125,15 @@ qa_pmt_prims::test_reals()
CPPUNIT_ASSERT_EQUAL(-1.0, pmt::to_double(m1));
CPPUNIT_ASSERT_EQUAL(1.0, pmt::to_double(p1));
CPPUNIT_ASSERT_EQUAL(1.0, pmt::to_double(pmt::from_long(1)));
+
+ pmt::pmt_t p2 = pmt::from_float(1);
+ pmt::pmt_t m2 = pmt::from_float(-1);
+ CPPUNIT_ASSERT(pmt::is_real(p2));
+ CPPUNIT_ASSERT(pmt::is_real(m2));
+ CPPUNIT_ASSERT_THROW(pmt::to_float(pmt::PMT_T), pmt::wrong_type);
+ CPPUNIT_ASSERT_EQUAL(float(-1.0), pmt::to_float(m2));
+ CPPUNIT_ASSERT_EQUAL(float(1.0), pmt::to_float(p2));
+ CPPUNIT_ASSERT_EQUAL(float(1.0), pmt::to_float(pmt::from_long(1)));
}
void
diff --git a/gnuradio-runtime/python/pmt/qa_pmt.py b/gnuradio-runtime/python/pmt/qa_pmt.py
index 5c1af2c00e..32cff62f44 100755
--- a/gnuradio-runtime/python/pmt/qa_pmt.py
+++ b/gnuradio-runtime/python/pmt/qa_pmt.py
@@ -36,7 +36,9 @@ class test_pmt(unittest.TestCase):
const = 123765
x_pmt = pmt.from_double(const)
x_int = pmt.to_double(x_pmt)
+ x_float = pmt.to_float(x_pmt)
self.assertEqual(x_int, const)
+ self.assertEqual(x_float, const)
def test03(self):
v = pmt.init_f32vector(3, [11, -22, 33])
diff --git a/gnuradio-runtime/swig/pmt_swig.i b/gnuradio-runtime/swig/pmt_swig.i
index e54b544977..c4b678222d 100644
--- a/gnuradio-runtime/swig/pmt_swig.i
+++ b/gnuradio-runtime/swig/pmt_swig.i
@@ -111,6 +111,8 @@ namespace pmt{
bool is_real(pmt_t obj);
pmt_t from_double(double x);
double to_double(pmt_t x);
+ pmt_t from_float(double x);
+ double to_float(pmt_t x);
bool is_complex(pmt_t obj);
pmt_t make_rectangular(double re, double im);