1 /* -*- c++ -*- */
2 /*
3 * Copyright 2012 Free Software Foundation, Inc.
4 *
5 * This file is part of GNU Radio
6 *
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
10 * any later version.
11 *
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "float_array_to_int.h"
28 #include "float_to_int_impl.h"
29 #include <gnuradio/io_signature.h>
30 #include <volk/volk.h>
31
32 namespace gr {
33 namespace blocks {
34
make(size_t vlen,float scale)35 float_to_int::sptr float_to_int::make(size_t vlen, float scale)
36 {
37 return gnuradio::get_initial_sptr(new float_to_int_impl(vlen, scale));
38 }
39
float_to_int_impl(size_t vlen,float scale)40 float_to_int_impl::float_to_int_impl(size_t vlen, float scale)
41 : sync_block("float_to_int",
42 io_signature::make(1, 1, sizeof(float) * vlen),
43 io_signature::make(1, 1, sizeof(int) * vlen)),
44 d_vlen(vlen),
45 d_scale(scale)
46 {
47 const int alignment_multiple = volk_get_alignment() / sizeof(int);
48 set_alignment(std::max(1, alignment_multiple));
49 }
50
work(int noutput_items,gr_vector_const_void_star & input_items,gr_vector_void_star & output_items)51 int float_to_int_impl::work(int noutput_items,
52 gr_vector_const_void_star& input_items,
53 gr_vector_void_star& output_items)
54 {
55 // Disable the Volk for now. There is a problem for large 32-bit ints that
56 // are not properly represented by the precisions of a single float, which
57 // can cause wrapping from large, positive numbers to negative.
58 // In gri_float_to_int, the value is first promoted to a 64-bit
59 // value, clipped, then converted to a float.
60 #if 0
61 const float *in = (const float *) input_items[0];
62 int32_t *out = (int32_t *) output_items[0];
63
64 volk_32f_s32f_convert_32i(out, in, d_scale, d_vlen*noutput_items);
65 #else
66 const float* in = (const float*)input_items[0];
67 int* out = (int*)output_items[0];
68
69 float_array_to_int(in, out, d_scale, d_vlen * noutput_items);
70
71 #endif
72
73 return noutput_items;
74 }
75
76 } /* namespace blocks */
77 } /* namespace gr */
78