Skip to content

Commit

Permalink
complex: Add example interface for C++
Browse files Browse the repository at this point in the history
  • Loading branch information
jdemel committed Aug 28, 2021
1 parent 11ec56f commit 1b90608
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 13 deletions.
6 changes: 3 additions & 3 deletions compile.sh
Original file line number Diff line number Diff line change
@@ -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
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
12 changes: 9 additions & 3 deletions include/volk/volk_complex.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
* - lv_conj - take the conjugate of the complex number
*/

#include <volk/volk_common.h>
// #include <volk/volk_common.h>

__VOLK_DECL_BEGIN
#if defined(__cplusplus)
extern "C" {
#endif

#include <complex.h>

Expand Down Expand Up @@ -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 */
42 changes: 41 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,49 @@

#include <stdio.h>
#include <volk/volk_complex.h>
#include <math.h>
#include <volk/volk.h>

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),
Expand Down
60 changes: 54 additions & 6 deletions main.cc
Original file line number Diff line number Diff line change
@@ -1,15 +1,64 @@
#include <fmt/core.h>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <complex>

typedef std::complex<float> cmplxf;

#include <volk/volk.h>
#include <volk/volk_alloc.hh>


#include <volk/volk_complex.h>
void cppmultiply(volk::vector<cmplxf>& result,
volk::vector<cmplxf>& input0,
volk::vector<cmplxf>& input1)
{
volk_32fc_x2_multiply_32fc(reinterpret_cast<lv_32fc_t*>(result.data()),
reinterpret_cast<lv_32fc_t*>(input0.data()),
reinterpret_cast<lv_32fc_t*>(input1.data()),
input0.size());
}

void function_test(int num_points)
{
volk::vector<cmplxf> in0(num_points);
volk::vector<cmplxf> in1(num_points);
volk::vector<cmplxf> 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),
Expand All @@ -19,14 +68,13 @@ int main(int argc, char* argv[])

std::vector<lv_32fc_t> vec(4);
for (int i = 0; i < 4; i++) {
auto foo = std::complex<float>( (i + 3), (i + 8) );
auto foo = std::complex<float>((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);
Expand Down

0 comments on commit 1b90608

Please sign in to comment.