gnss-sdr/src/algorithms/libs/item_type_helpers.cc

376 lines
14 KiB
C++
Raw Normal View History

2019-11-04 17:14:50 +00:00
/*!
2019-11-16 19:09:14 +00:00
* \file item_type_helpers.cc
2019-11-04 17:14:50 +00:00
* \brief Utility functions for converting between item types
* \authors <ul>
2019-11-16 19:09:14 +00:00
* <li> Cillian O'Driscoll, 2019. cillian.odriscoll(at)gmail.com
2019-11-04 17:14:50 +00:00
* </ul>
*
*
2020-07-28 14:57:15 +00:00
* -----------------------------------------------------------------------------
*
* GNSS-SDR is a Global Navigation Satellite System software-defined receiver.
2019-11-04 17:14:50 +00:00
* This file is part of GNSS-SDR.
*
* Copyright (C) 2010-2020 (see AUTHORS file for a list of contributors)
* SPDX-License-Identifier: GPL-3.0-or-later
2019-11-04 17:14:50 +00:00
*
2020-07-28 14:57:15 +00:00
* -----------------------------------------------------------------------------
2019-11-04 17:14:50 +00:00
*/
#include "item_type_helpers.h"
#include <volk/volk.h>
#include <volk_gnsssdr/volk_gnsssdr.h>
2019-11-08 13:47:08 +00:00
#include <cstring> // memcpy
2019-11-04 17:14:50 +00:00
2019-11-08 13:47:08 +00:00
bool item_type_valid(const std::string &item_type)
2019-11-04 17:14:50 +00:00
{
if (item_type != "byte" and item_type != "cbyte" and item_type != "ibyte" and
item_type != "short" and item_type != "cshort" and item_type != "ishort" and
item_type != "float" and item_type != "gr_complex")
2019-11-08 13:47:08 +00:00
{
return false;
}
2019-11-04 17:14:50 +00:00
return true;
}
2019-11-08 13:47:08 +00:00
size_t item_type_size(const std::string &item_type)
2019-11-04 17:14:50 +00:00
{
if (item_type == "byte" or item_type == "ibyte")
2019-11-08 13:47:08 +00:00
{
return sizeof(int8_t);
}
else if (item_type == "cbyte")
2019-11-08 13:47:08 +00:00
{
return 2 * sizeof(int8_t);
}
else if (item_type == "short" or item_type == "ishort")
2019-11-08 13:47:08 +00:00
{
return sizeof(int16_t);
}
else if (item_type == "cshort")
2019-11-08 13:47:08 +00:00
{
return 2 * sizeof(int16_t);
}
else if (item_type == "float")
2019-11-08 13:47:08 +00:00
{
return sizeof(float);
}
else if (item_type == "gr_complex")
2019-11-08 13:47:08 +00:00
{
return 2 * sizeof(float);
}
2019-11-04 17:14:50 +00:00
else
2019-11-08 13:47:08 +00:00
{
return 0;
}
2019-11-04 17:14:50 +00:00
}
bool item_type_is_complex(const std::string &item_type)
2019-11-04 17:14:50 +00:00
{
return (item_type == "ibyte") or (item_type == "cbyte") or (item_type == "ishort") or (item_type == "cshort") or (item_type == "gr_complex");
2019-11-04 17:14:50 +00:00
}
2020-02-20 10:25:29 +00:00
void copy_converter(void *dest, const void *src, uint32_t num_items, size_t item_size)
2019-11-04 17:14:50 +00:00
{
2019-11-08 13:47:08 +00:00
std::memcpy(dest, src, num_items * item_size);
2019-11-04 17:14:50 +00:00
}
2020-02-20 10:25:29 +00:00
void convert_8i_16i(void *dest, const void *src, uint32_t num_items)
2019-11-04 17:14:50 +00:00
{
2019-11-08 13:47:08 +00:00
volk_8i_convert_16i(reinterpret_cast<int16_t *>(dest),
reinterpret_cast<const int8_t *>(src), num_items);
2019-11-04 17:14:50 +00:00
}
2020-02-20 10:25:29 +00:00
void convert_8i_32f(void *dest, const void *src, uint32_t num_items)
2019-11-04 17:14:50 +00:00
{
2019-11-08 13:47:08 +00:00
volk_8i_s32f_convert_32f(reinterpret_cast<float *>(dest),
reinterpret_cast<const int8_t *>(src), 1.0F, num_items);
2019-11-04 17:14:50 +00:00
}
2020-02-20 10:25:29 +00:00
void convert_8ic_16ic(void *dest, const void *src, uint32_t num_items)
2019-11-04 17:14:50 +00:00
{
2019-11-08 13:47:08 +00:00
volk_8i_convert_16i(reinterpret_cast<int16_t *>(dest),
reinterpret_cast<const int8_t *>(src), 2 * num_items);
2019-11-04 17:14:50 +00:00
}
2020-02-20 10:25:29 +00:00
void convert_8ic_32fc(void *dest, const void *src, uint32_t num_items)
2019-11-04 17:14:50 +00:00
{
2019-11-08 13:47:08 +00:00
volk_8i_s32f_convert_32f(reinterpret_cast<float *>(dest),
reinterpret_cast<const int8_t *>(src), 1.0F, 2 * num_items);
2019-11-04 17:14:50 +00:00
}
2020-02-20 10:25:29 +00:00
void convert_16i_8i(void *dest, const void *src, uint32_t num_items)
2019-11-04 17:14:50 +00:00
{
2019-11-08 13:47:08 +00:00
volk_16i_convert_8i(reinterpret_cast<int8_t *>(dest),
reinterpret_cast<const int16_t *>(src), num_items);
2019-11-04 17:14:50 +00:00
}
2020-02-20 10:25:29 +00:00
void convert_16i_32f(void *dest, const void *src, uint32_t num_items)
2019-11-04 17:14:50 +00:00
{
2019-11-08 13:47:08 +00:00
volk_16i_s32f_convert_32f(reinterpret_cast<float *>(dest),
reinterpret_cast<const int16_t *>(src), 1.0F, num_items);
2019-11-04 17:14:50 +00:00
}
2020-02-20 10:25:29 +00:00
void convert_16ic_8ic(void *dest, const void *src, uint32_t num_items)
2019-11-04 17:14:50 +00:00
{
2019-11-08 13:47:08 +00:00
volk_16i_convert_8i(reinterpret_cast<int8_t *>(dest),
reinterpret_cast<const int16_t *>(src), 2 * num_items);
2019-11-04 17:14:50 +00:00
}
2020-02-20 10:25:29 +00:00
void convert_16ic_32fc(void *dest, const void *src, uint32_t num_items)
2019-11-04 17:14:50 +00:00
{
2019-11-08 13:47:08 +00:00
volk_16i_s32f_convert_32f(reinterpret_cast<float *>(dest),
reinterpret_cast<const int16_t *>(src), 1.0F, 2 * num_items);
2019-11-04 17:14:50 +00:00
}
2020-02-20 10:25:29 +00:00
void convert_32f_8i(void *dest, const void *src, uint32_t num_items)
2019-11-04 17:14:50 +00:00
{
2019-11-08 13:47:08 +00:00
volk_32f_s32f_convert_8i(reinterpret_cast<int8_t *>(dest),
reinterpret_cast<const float *>(src), 1.0F, num_items);
2019-11-04 17:14:50 +00:00
}
2020-02-20 10:25:29 +00:00
void convert_32f_16i(void *dest, const void *src, uint32_t num_items)
2019-11-04 17:14:50 +00:00
{
2019-11-08 13:47:08 +00:00
volk_32f_s32f_convert_16i(reinterpret_cast<int16_t *>(dest),
reinterpret_cast<const float *>(src), 1.0F, num_items);
2019-11-04 17:14:50 +00:00
}
2020-02-20 10:25:29 +00:00
void convert_32fc_8ic(void *dest, const void *src, uint32_t num_items)
2019-11-04 17:14:50 +00:00
{
2019-11-08 13:47:08 +00:00
volk_32f_s32f_convert_8i(reinterpret_cast<int8_t *>(dest),
reinterpret_cast<const float *>(src), 1.0F, 2 * num_items);
2019-11-04 17:14:50 +00:00
}
2020-02-20 10:25:29 +00:00
void convert_32fc_16ic(void *dest, const void *src, uint32_t num_items)
2019-11-04 17:14:50 +00:00
{
2019-11-08 13:47:08 +00:00
volk_32f_s32f_convert_16i(reinterpret_cast<int16_t *>(dest),
reinterpret_cast<const float *>(src), 1.0F, 2 * num_items);
2019-11-04 17:14:50 +00:00
}
item_type_converter_t make_vector_converter(const std::string &input_type,
const std::string &output_type)
2019-11-04 17:14:50 +00:00
{
2019-11-08 13:47:08 +00:00
if (not item_type_valid(input_type) or not item_type_valid(output_type))
2019-11-04 17:14:50 +00:00
{
2019-11-08 13:47:08 +00:00
throw std::runtime_error("make_vector_converter: invalid item types : " + input_type + " " + output_type);
2019-11-04 17:14:50 +00:00
}
2019-11-08 13:47:08 +00:00
if (input_type == output_type)
2019-11-04 17:14:50 +00:00
{
2019-11-08 13:47:08 +00:00
size_t input_size = item_type_size(input_type);
2020-06-13 12:32:08 +00:00
#ifdef DO_NOT_USE_LAMBDAS
2020-06-13 19:13:46 +00:00
return std::bind(copy_converter, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, input_size); // NOLINT(modernize-avoid-bind)
2019-11-16 18:21:02 +00:00
#else
return [=](auto &&arg1, auto &&arg2, auto &&arg3) { return copy_converter(arg1, arg2, arg3, input_size); };
2019-11-16 18:21:02 +00:00
#endif
2019-11-04 17:14:50 +00:00
}
2019-11-08 13:47:08 +00:00
if (input_type == "byte")
2019-11-04 17:14:50 +00:00
{
if (output_type == "short")
2019-11-08 13:47:08 +00:00
{
2020-06-13 12:32:08 +00:00
#ifdef DO_NOT_USE_LAMBDAS
2020-06-13 19:13:46 +00:00
return std::bind(convert_8i_16i, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); // NOLINT(modernize-avoid-bind)
2019-11-16 18:21:02 +00:00
#else
return [=](auto &&arg1, auto &&arg2, auto &&arg3) { return convert_8i_16i(arg1, arg2, arg3); };
2019-11-16 18:21:02 +00:00
#endif
2019-11-08 13:47:08 +00:00
}
else if (output_type == "float")
2019-11-08 13:47:08 +00:00
{
2020-06-13 12:32:08 +00:00
#ifdef DO_NOT_USE_LAMBDAS
2020-06-13 19:13:46 +00:00
return std::bind(convert_8i_32f, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); // NOLINT(modernize-avoid-bind)
2019-11-16 18:21:02 +00:00
#else
return [=](auto &&arg1, auto &&arg2, auto &&arg3) { return convert_8i_32f(arg1, arg2, arg3); };
2019-11-16 18:21:02 +00:00
#endif
2019-11-08 13:47:08 +00:00
}
2019-11-04 17:14:50 +00:00
}
else if (input_type == "cbyte")
2019-11-04 17:14:50 +00:00
{
if (output_type == "ibyte")
{
size_t input_size = item_type_size(input_type);
2020-06-13 12:32:08 +00:00
#ifdef DO_NOT_USE_LAMBDAS
2020-06-13 19:13:46 +00:00
return std::bind(copy_converter, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, input_size); // NOLINT(modernize-avoid-bind)
2019-11-16 18:21:02 +00:00
#else
return [=](auto &&arg1, auto &&arg2, auto &&arg3) { return copy_converter(arg1, arg2, arg3, input_size); };
2019-11-16 18:21:02 +00:00
#endif
}
if (output_type == "cshort" or output_type == "ishort")
2019-11-08 13:47:08 +00:00
{
2020-06-13 12:32:08 +00:00
#ifdef DO_NOT_USE_LAMBDAS
2020-06-13 19:13:46 +00:00
return std::bind(convert_8ic_16ic, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); // NOLINT(modernize-avoid-bind)
2019-11-16 18:21:02 +00:00
#else
return [=](auto &&arg1, auto &&arg2, auto &&arg3) { return convert_8ic_16ic(arg1, arg2, arg3); };
2019-11-16 18:21:02 +00:00
#endif
2019-11-08 13:47:08 +00:00
}
else if (output_type == "gr_complex")
2019-11-08 13:47:08 +00:00
{
2020-06-13 12:32:08 +00:00
#ifdef DO_NOT_USE_LAMBDAS
2020-06-13 19:13:46 +00:00
return std::bind(convert_8ic_32fc, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); // NOLINT(modernize-avoid-bind)
2019-11-16 18:21:02 +00:00
#else
return [=](auto &&arg1, auto &&arg2, auto &&arg3) { return convert_8ic_32fc(arg1, arg2, arg3); };
2019-11-16 18:21:02 +00:00
#endif
2019-11-08 13:47:08 +00:00
}
2019-11-04 17:14:50 +00:00
}
else if (input_type == "ibyte")
2019-11-04 17:14:50 +00:00
{
if (output_type == "cbyte")
2019-11-08 13:47:08 +00:00
{
size_t input_size = item_type_size(input_type);
2020-06-13 12:32:08 +00:00
#ifdef DO_NOT_USE_LAMBDAS
2020-06-13 19:13:46 +00:00
return std::bind(copy_converter, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, input_size); // NOLINT(modernize-avoid-bind)
2019-11-16 18:21:02 +00:00
#else
return [=](auto &&arg1, auto &&arg2, auto &&arg3) { return copy_converter(arg1, arg2, arg3, input_size); };
2019-11-16 18:21:02 +00:00
#endif
}
else if (output_type == "cshort" or output_type == "ishort")
{
2020-06-13 12:32:08 +00:00
#ifdef DO_NOT_USE_LAMBDAS
2020-06-13 19:13:46 +00:00
return std::bind(convert_8i_16i, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); // NOLINT(modernize-avoid-bind)
2019-11-16 18:21:02 +00:00
#else
return [=](auto &&arg1, auto &&arg2, auto &&arg3) { return convert_8i_16i(arg1, arg2, arg3); };
2019-11-16 18:21:02 +00:00
#endif
2019-11-08 13:47:08 +00:00
}
else if (output_type == "gr_complex")
2019-11-08 13:47:08 +00:00
{
2020-06-13 12:32:08 +00:00
#ifdef DO_NOT_USE_LAMBDAS
2020-06-13 19:13:46 +00:00
return std::bind(convert_8i_32f, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); // NOLINT(modernize-avoid-bind)
2019-11-16 18:21:02 +00:00
#else
return [=](auto &&arg1, auto &&arg2, auto &&arg3) { return convert_8i_32f(arg1, arg2, arg3); };
2019-11-16 18:21:02 +00:00
#endif
2019-11-08 13:47:08 +00:00
}
2019-11-04 17:14:50 +00:00
}
else if (input_type == "short")
2019-11-04 17:14:50 +00:00
{
if (output_type == "byte")
2019-11-08 13:47:08 +00:00
{
2020-06-13 12:32:08 +00:00
#ifdef DO_NOT_USE_LAMBDAS
2020-06-13 19:13:46 +00:00
return std::bind(convert_16i_8i, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); // NOLINT(modernize-avoid-bind)
2019-11-16 18:21:02 +00:00
#else
return [=](auto &&arg1, auto &&arg2, auto &&arg3) { return convert_16i_8i(arg1, arg2, arg3); };
2019-11-16 18:21:02 +00:00
#endif
2019-11-08 13:47:08 +00:00
}
else if (output_type == "float")
2019-11-08 13:47:08 +00:00
{
2020-06-13 12:32:08 +00:00
#ifdef DO_NOT_USE_LAMBDAS
2020-06-13 19:13:46 +00:00
return std::bind(convert_16i_32f, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); // NOLINT(modernize-avoid-bind)
2019-11-16 18:21:02 +00:00
#else
return [=](auto &&arg1, auto &&arg2, auto &&arg3) { return convert_16i_32f(arg1, arg2, arg3); };
2019-11-16 18:21:02 +00:00
#endif
2019-11-08 13:47:08 +00:00
}
2019-11-04 17:14:50 +00:00
}
else if (input_type == "cshort")
2019-11-04 17:14:50 +00:00
{
if (output_type == "cbyte" or output_type == "ibyte")
2019-11-08 13:47:08 +00:00
{
2020-06-13 12:32:08 +00:00
#ifdef DO_NOT_USE_LAMBDAS
2020-06-13 19:13:46 +00:00
return std::bind(convert_16ic_8ic, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); // NOLINT(modernize-avoid-bind)
2019-11-16 18:21:02 +00:00
#else
return [=](auto &&arg1, auto &&arg2, auto &&arg3) { return convert_16ic_8ic(arg1, arg2, arg3); };
2019-11-16 18:21:02 +00:00
#endif
2019-11-08 13:47:08 +00:00
}
if (output_type == "ishort")
2019-11-08 13:47:08 +00:00
{
size_t input_size = item_type_size(input_type);
2020-06-13 12:32:08 +00:00
#ifdef DO_NOT_USE_LAMBDAS
2020-06-13 19:13:46 +00:00
return std::bind(copy_converter, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, input_size); // NOLINT(modernize-avoid-bind)
2019-11-16 18:21:02 +00:00
#else
return [=](auto &&arg1, auto &&arg2, auto &&arg3) { return copy_converter(arg1, arg2, arg3, input_size); };
2019-11-16 18:21:02 +00:00
#endif
2019-11-08 13:47:08 +00:00
}
else if (output_type == "gr_complex")
2019-11-08 13:47:08 +00:00
{
2020-06-13 12:32:08 +00:00
#ifdef DO_NOT_USE_LAMBDAS
2020-06-13 19:13:46 +00:00
return std::bind(convert_16ic_32fc, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); // NOLINT(modernize-avoid-bind)
2019-11-16 18:21:02 +00:00
#else
return [=](auto &&arg1, auto &&arg2, auto &&arg3) { return convert_16ic_32fc(arg1, arg2, arg3); };
2019-11-16 18:21:02 +00:00
#endif
2019-11-08 13:47:08 +00:00
}
2019-11-04 17:14:50 +00:00
}
else if (input_type == "ishort")
2019-11-04 17:14:50 +00:00
{
if (output_type == "cbyte" or output_type == "ibyte")
2019-11-08 13:47:08 +00:00
{
2020-06-13 12:32:08 +00:00
#ifdef DO_NOT_USE_LAMBDAS
2020-06-13 19:13:46 +00:00
return std::bind(convert_16i_8i, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); // NOLINT(modernize-avoid-bind)
2019-11-16 18:21:02 +00:00
#else
return [=](auto &&arg1, auto &&arg2, auto &&arg3) { return convert_16i_8i(arg1, arg2, arg3); };
2019-11-16 18:21:02 +00:00
#endif
2019-11-08 13:47:08 +00:00
}
if (output_type == "cshort")
2019-11-08 13:47:08 +00:00
{
size_t input_size = item_type_size(input_type);
2020-06-13 12:32:08 +00:00
#ifdef DO_NOT_USE_LAMBDAS
2020-06-13 19:13:46 +00:00
return std::bind(copy_converter, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, input_size); // NOLINT(modernize-avoid-bind)
2019-11-16 18:21:02 +00:00
#else
return [=](auto &&arg1, auto &&arg2, auto &&arg3) { return copy_converter(arg1, arg2, arg3, input_size); };
2019-11-16 18:21:02 +00:00
#endif
2019-11-08 13:47:08 +00:00
}
else if (output_type == "gr_complex")
2019-11-08 13:47:08 +00:00
{
2020-06-13 12:32:08 +00:00
#ifdef DO_NOT_USE_LAMBDAS
2020-06-13 19:13:46 +00:00
return std::bind(convert_16i_32f, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); // NOLINT(modernize-avoid-bind)
2019-11-16 18:21:02 +00:00
#else
return [=](auto &&arg1, auto &&arg2, auto &&arg3) { return convert_16i_32f(arg1, arg2, arg3); };
2019-11-16 18:21:02 +00:00
#endif
2019-11-08 13:47:08 +00:00
}
2019-11-04 17:14:50 +00:00
}
else if (input_type == "float")
2019-11-04 17:14:50 +00:00
{
if (output_type == "byte")
2019-11-08 13:47:08 +00:00
{
2020-06-13 12:32:08 +00:00
#ifdef DO_NOT_USE_LAMBDAS
2020-06-13 19:13:46 +00:00
return std::bind(convert_32f_8i, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); // NOLINT(modernize-avoid-bind)
2019-11-16 18:21:02 +00:00
#else
return [=](auto &&arg1, auto &&arg2, auto &&arg3) { return convert_32f_8i(arg1, arg2, arg3); };
2019-11-16 18:21:02 +00:00
#endif
2019-11-08 13:47:08 +00:00
}
else if (output_type == "short")
2019-11-08 13:47:08 +00:00
{
2020-06-13 12:32:08 +00:00
#ifdef DO_NOT_USE_LAMBDAS
2020-06-13 19:13:46 +00:00
return std::bind(convert_32f_16i, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); // NOLINT(modernize-avoid-bind)
2019-11-16 18:21:02 +00:00
#else
return [=](auto &&arg1, auto &&arg2, auto &&arg3) { return convert_32f_16i(arg1, arg2, arg3); };
2019-11-16 18:21:02 +00:00
#endif
2019-11-08 13:47:08 +00:00
}
2019-11-04 17:14:50 +00:00
}
else if (input_type == "gr_complex")
2019-11-04 17:14:50 +00:00
{
if (output_type == "cbyte" or output_type == "ibyte")
2019-11-08 13:47:08 +00:00
{
2020-06-13 12:32:08 +00:00
#ifdef DO_NOT_USE_LAMBDAS
2020-06-13 19:13:46 +00:00
return std::bind(convert_32fc_8ic, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); // NOLINT(modernize-avoid-bind)
2019-11-16 18:21:02 +00:00
#else
return [=](auto &&arg1, auto &&arg2, auto &&arg3) { return convert_32fc_8ic(arg1, arg2, arg3); };
2019-11-16 18:21:02 +00:00
#endif
2019-11-08 13:47:08 +00:00
}
else if (output_type == "cshort" or output_type == "ishort")
2019-11-08 13:47:08 +00:00
{
2020-06-13 12:32:08 +00:00
#ifdef DO_NOT_USE_LAMBDAS
2020-06-13 19:13:46 +00:00
return std::bind(convert_32fc_16ic, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); // NOLINT(modernize-avoid-bind)
2019-11-16 18:21:02 +00:00
#else
return [=](auto &&arg1, auto &&arg2, auto &&arg3) { return convert_32fc_16ic(arg1, arg2, arg3); };
2019-11-16 18:21:02 +00:00
#endif
2019-11-08 13:47:08 +00:00
}
2019-11-04 17:14:50 +00:00
}
2019-11-08 13:47:08 +00:00
throw std::runtime_error("make_vector_converter: invalid conversion : " + input_type + " to " + output_type);
2019-11-04 17:14:50 +00:00
}