From fcef50a290e97e815b4bf3ff9d90fdb6a98f5349 Mon Sep 17 00:00:00 2001 From: Johannes Demel Date: Sat, 28 Aug 2021 17:03:21 +0200 Subject: [PATCH] complex: Add example interface for C++ --- compile.sh | 6 ++-- include/volk/volk_complex.h | 12 ++++++-- main.c | 42 +++++++++++++++++++++++++- main.cc | 60 +++++++++++++++++++++++++++++++++---- 4 files changed, 107 insertions(+), 13 deletions(-) diff --git a/compile.sh b/compile.sh index f9f19bca8..0af7ef7c1 100755 --- a/compile.sh +++ b/compile.sh @@ -1,3 +1,3 @@ -gcc -std=c17 -I/home/johannes/src/volk/include -x c main.c -o mainvolkgnuc -lm -clang -std=c17 -I/home/johannes/src/volk/include -x c main.c -o mainvolkclangc -lm -g++ -std=c++17 -I/home/johannes/src/volk/include -x c++ main.cc -o mainvolkcpp -lm -lfmt \ No newline at end of file +gcc -std=c17 -I/home/johannes/src/volk/include -I/home/johannes/src/volk/build/include -L/home/johannes/src/volk/build/lib -x c main.c -o mainvolkgnuc -lm -lvolk +clang -std=c17 -I/home/johannes/src/volk/include -I/home/johannes/src/volk/build/include -L/home/johannes/src/volk/build/lib -x c main.c -o mainvolkclangc -lm -lvolk +g++ -std=c++17 -I/home/johannes/src/volk/include -I/home/johannes/src/volk/build/include -L/home/johannes/src/volk/build/lib -x c++ main.cc -o mainvolkcpp -lm -lfmt -lvolk \ No newline at end of file diff --git a/include/volk/volk_complex.h b/include/volk/volk_complex.h index 69234f9fc..6ba6ff2fb 100644 --- a/include/volk/volk_complex.h +++ b/include/volk/volk_complex.h @@ -17,9 +17,11 @@ * - lv_conj - take the conjugate of the complex number */ -#include +// #include -__VOLK_DECL_BEGIN +#if defined(__cplusplus) +extern "C" { +#endif #include @@ -68,6 +70,10 @@ typedef double _Complex lv_64fc_t; #endif /* __GNUC__ */ -__VOLK_DECL_END +// __VOLK_DECL_END + +#if defined(__cplusplus) +} +#endif #endif /* INCLUDE_VOLK_COMPLEX_H */ diff --git a/main.c b/main.c index dd7e9cdf1..0d93eb85b 100644 --- a/main.c +++ b/main.c @@ -1,9 +1,49 @@ #include -#include +#include +#include + +void function_test(int num_points) +{ + unsigned int alignment = volk_get_alignment(); + lv_32fc_t* in0 = (lv_32fc_t*)volk_malloc(sizeof(lv_32fc_t) * num_points, alignment); + lv_32fc_t* in1 = (lv_32fc_t*)volk_malloc(sizeof(lv_32fc_t) * num_points, alignment); + lv_32fc_t* out = (lv_32fc_t*)volk_malloc(sizeof(lv_32fc_t) * num_points, alignment); + + for (unsigned int ii = 0; ii < num_points; ++ii) { + // Generate two tones + float real_1 = cosf(0.3f * (float)ii); + float imag_1 = sinf(0.3f * (float)ii); + in0[ii] = lv_cmake(real_1, imag_1); + float real_2 = cosf(0.1f * (float)ii); + float imag_2 = sinf(0.1f * (float)ii); + in1[ii] = lv_cmake(real_2, imag_2); + } + + volk_32fc_x2_multiply_32fc(out, in0, in1, num_points); + + for (unsigned int ii = 0; ii < num_points; ++ii) { + lv_32fc_t v0 = in0[ii]; + lv_32fc_t v1 = in1[ii]; + lv_32fc_t o = out[ii]; + printf("in0=(%+.1f%+.1fj), in1=(%+.1f%+.1fj), out=(%+.1f%+.1fj)\n", + creal(v0), + cimag(v0), + creal(v1), + cimag(v1), + creal(o), + cimag(o)); + } + + volk_free(in0); + volk_free(in1); + volk_free(out); +} int main(int argc, char* argv[]) { + function_test(32); + lv_32fc_t fc_cpl[4]; printf("float=%lu, complex float=%lu, complex float array[4]=%lu\n", sizeof(float), diff --git a/main.cc b/main.cc index a72489861..83117827b 100644 --- a/main.cc +++ b/main.cc @@ -1,15 +1,64 @@ #include +#include +#include #include #include #include -#include + +typedef std::complex cmplxf; + +#include +#include -#include +void cppmultiply(volk::vector& result, + volk::vector& input0, + volk::vector& input1) +{ + volk_32fc_x2_multiply_32fc(reinterpret_cast(result.data()), + reinterpret_cast(input0.data()), + reinterpret_cast(input1.data()), + input0.size()); +} + +void function_test(int num_points) +{ + volk::vector in0(num_points); + volk::vector in1(num_points); + volk::vector out(num_points); + + for (unsigned int ii = 0; ii < num_points; ++ii) { + // Generate two tones + float real_1 = std::cos(0.3f * (float)ii); + float imag_1 = std::sin(0.3f * (float)ii); + in0[ii] = cmplxf(real_1, imag_1); + float real_2 = std::cos(0.1f * (float)ii); + float imag_2 = std::sin(0.1f * (float)ii); + in1[ii] = cmplxf(real_2, imag_2); + } + + cppmultiply(out, in0, in1); + + for (int ii = 0; ii < num_points; ++ii) { + cmplxf v0 = in0[ii]; + cmplxf v1 = in1[ii]; + cmplxf o = out[ii]; + + fmt::print( + "in0=({:+.1f}{:+.1f}j), in1=({:+.1f}{:+.1f}j), out=({:+.1f}{:+.1f}j)\n", + std::real(v0), + std::imag(v0), + std::real(v1), + std::imag(v1), + std::real(o), + std::imag(o)); + } +} int main(int argc, char* argv[]) { + function_test(32); lv_32fc_t fc_cpl[4]; fmt::print("float={}, complex float={}, complex float array[4]={}\n", sizeof(float), @@ -19,14 +68,13 @@ int main(int argc, char* argv[]) std::vector vec(4); for (int i = 0; i < 4; i++) { - auto foo = std::complex( (i + 3), (i + 8) ); + auto foo = std::complex((i + 3), (i + 8)); fmt::print("std::complex: ({:+.1f}{:+.1f}j)\n", std::real(foo), std::imag(foo)); - lv_32fc_t bar = lv_32fc_t{5, 6}; + lv_32fc_t bar = lv_32fc_t{ 5, 6 }; vec.at(i) = bar; - } - for(auto &val : vec){ + for (auto& val : vec) { float r = __real__ val; float i = __imag__ val; fmt::print("sizeof(val)={}, {:+.1f}{:+.1f}j\n", sizeof(val), r, i);