HackRF: convert _lut to float[] to reduce size by a factor of 256

Similar to commit 33a8d1c for RTL-SDR, this uses two lookups in a
256-element LUT instead of one lookup in a 65536-element LUT, which
saves a bit of CPU cache. It also eliminates a dependency on the
host's byte ordering.

Signed-off-by: Eric Wild <ewild@sysmocom.de>
This commit is contained in:
Wim Lewis 2020-07-15 01:31:43 -07:00 committed by Eric Wild
parent 800d2eaeaf
commit 2e7d343fed
2 changed files with 11 additions and 18 deletions

View File

@ -31,8 +31,6 @@
#include <stdexcept>
#include <iostream>
#include <boost/detail/endian.hpp>
#include <gnuradio/io_signature.h>
#include "hackrf_source_c.h"
@ -89,14 +87,8 @@ hackrf_source_c::hackrf_source_c (const std::string &args)
_samp_avail = _buf_len / BYTES_PER_SAMPLE;
// create a lookup table for gr_complex values
for (unsigned int i = 0; i <= 0xffff; i++) {
#ifdef BOOST_LITTLE_ENDIAN
_lut.push_back( gr_complex( (float(int8_t(i & 0xff))) * (1.0f/128.0f),
(float(int8_t(i >> 8))) * (1.0f/128.0f) ) );
#else // BOOST_BIG_ENDIAN
_lut.push_back( gr_complex( (float(int8_t(i >> 8))) * (1.0f/128.0f),
(float(int8_t(i & 0xff))) * (1.0f/128.0f) ) );
#endif
for (unsigned int i = 0; i <= 0xff; i++) {
_lut.push_back( float(i) * (1.0f/128.0f) );
}
if ( BUF_NUM != _buf_num || BUF_LEN != _buf_len ) {
@ -119,11 +111,11 @@ hackrf_source_c::hackrf_source_c (const std::string &args)
hackrf_common::set_bias(dict["bias"] == "1");
}
_buf = (unsigned short **) malloc(_buf_num * sizeof(unsigned short *));
_buf = (unsigned char **) malloc(_buf_num * sizeof(unsigned char *));
if (_buf) {
for(unsigned int i = 0; i < _buf_num; ++i)
_buf[i] = (unsigned short *) malloc(_buf_len);
_buf[i] = (unsigned char *) malloc(_buf_len);
}
}
@ -218,17 +210,18 @@ int hackrf_source_c::work( int noutput_items,
if ( ! running )
return WORK_DONE;
unsigned short *buf = _buf[_buf_head] + _buf_offset;
const uint8_t *buf = _buf[_buf_head] + _buf_offset * BYTES_PER_SAMPLE;
#define TO_COMPLEX(p) gr_complex( _lut[(p)[0]], _lut[(p)[1]] )
if (noutput_items <= _samp_avail) {
for (int i = 0; i < noutput_items; ++i)
*out++ = _lut[ *(buf + i) ];
*out++ = TO_COMPLEX( buf + i*BYTES_PER_SAMPLE );
_buf_offset += noutput_items;
_samp_avail -= noutput_items;
} else {
for (int i = 0; i < _samp_avail; ++i)
*out++ = _lut[ *(buf + i) ];
*out++ = TO_COMPLEX( buf + i*BYTES_PER_SAMPLE );
{
std::lock_guard<std::mutex> lock(_buf_mutex);
@ -242,7 +235,7 @@ int hackrf_source_c::work( int noutput_items,
int remaining = noutput_items - _samp_avail;
for (int i = 0; i < remaining; ++i)
*out++ = _lut[ *(buf + i) ];
*out++ = TO_COMPLEX( buf + i*BYTES_PER_SAMPLE );
_buf_offset = remaining;
_samp_avail = (_buf_len / BYTES_PER_SAMPLE) - remaining;

View File

@ -123,9 +123,9 @@ private:
static int _hackrf_rx_callback(hackrf_transfer* transfer);
int hackrf_rx_callback(unsigned char *buf, uint32_t len);
std::vector<gr_complex> _lut;
std::vector<float> _lut;
unsigned short **_buf;
unsigned char **_buf;
unsigned int _buf_num;
unsigned int _buf_len;
unsigned int _buf_head;