Skip to content

Commit 1d55401

Browse files
committed
Introduce single precision float compilation option
This patch aims to add possibility for library to be compiled using single precision floating point numbers. This allows to use this library on embedded systems, where FPU may not support double precision floating point math, thus, is resource intensive and to slow for audio processing.
1 parent 20819b6 commit 1d55401

27 files changed

+163
-152
lines changed

cmake/ClipMode.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ macro(CLIP_MODE)
2424
"
2525
#include <math.h>
2626
int main (void)
27-
{ double fval ;
27+
{ fp_t fval ;
2828
int k, ival ;
2929
3030
fval = 1.0 * 0x7FFFFFFF ;
@@ -45,7 +45,7 @@ macro(CLIP_MODE)
4545
"
4646
#include <math.h>
4747
int main (void)
48-
{ double fval ;
48+
{ fp_t fval ;
4949
int k, ival ;
5050
5151
fval = -8.0 * 0x10000000 ;

examples/timewarp-file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
typedef struct
3434
{ sf_count_t index ;
35-
double ratio ;
35+
fp_t ratio ;
3636
} TIMEWARP_FACTOR ;
3737

3838
static void usage_exit (const char *progname) ;

examples/varispeed-play.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ varispeed_get_data (SRC_CB_DATA *data, float *samples, int out_frames)
206206
} ;
207207

208208
for (out_frame_count = 0 ; out_frame_count < out_frames ; out_frame_count += VARISPEED_BLOCK_LEN)
209-
{ double src_ratio = 1.0 - 0.5 * sin (data->freq_point * 2 * M_PI / 20000) ;
209+
{ fp_t src_ratio = 1.0 - 0.5 * sin (data->freq_point * 2 * M_PI / 20000) ;
210210

211211
data->freq_point ++ ;
212212

include/samplerate.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
extern "C" {
1919
#endif /* __cplusplus */
2020

21+
#ifdef LIBSAMPLERATE_SINGLE_PRECISION
22+
typedef float fp_t;
23+
#else
24+
typedef double fp_t;
25+
#endif
2126

2227
/* Opaque data type SRC_STATE. */
2328
typedef struct SRC_STATE_tag SRC_STATE ;
@@ -32,7 +37,7 @@ typedef struct
3237

3338
int end_of_input ;
3439

35-
double src_ratio ;
40+
fp_t src_ratio ;
3641
} SRC_DATA ;
3742

3843
/*
@@ -89,7 +94,7 @@ int src_process (SRC_STATE *state, SRC_DATA *data) ;
8994
** Callback based processing function. Read up to frames worth of data from
9095
** the converter int *data and return frames read or -1 on error.
9196
*/
92-
long src_callback_read (SRC_STATE *state, double src_ratio, long frames, float *data) ;
97+
long src_callback_read (SRC_STATE *state, fp_t src_ratio, long frames, float *data) ;
9398

9499
/*
95100
** Simple interface for performing a single conversion from input buffer to
@@ -119,7 +124,7 @@ const char *src_get_version (void) ;
119124
** Returns non zero on error.
120125
*/
121126

122-
int src_set_ratio (SRC_STATE *state, double new_ratio) ;
127+
int src_set_ratio (SRC_STATE *state, fp_t new_ratio) ;
123128

124129
/*
125130
** Get the current channel count.
@@ -142,7 +147,7 @@ int src_reset (SRC_STATE *state) ;
142147
** otherwise.
143148
*/
144149

145-
int src_is_valid_ratio (double ratio) ;
150+
int src_is_valid_ratio (fp_t ratio) ;
146151

147152
/*
148153
** Return an error number.

m4/clip_mode.m4

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ if test $ac_cv_c_clip_positive = unknown ; then
3838
#define __USE_ISOC9X 1
3939
#include <math.h>
4040
int main (void)
41-
{ double fval ;
41+
{ fp_t fval ;
4242
int k, ival ;
4343
4444
fval = 1.0 * 0x7FFFFFFF ;
@@ -66,7 +66,7 @@ if test $ac_cv_c_clip_positive = unknown ; then
6666
#define __USE_ISOC9X 1
6767
#include <math.h>
6868
int main (void)
69-
{ double fval ;
69+
{ fp_t fval ;
7070
int k, ival ;
7171
7272
fval = -8.0 * 0x10000000 ;

src/common.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ struct SRC_STATE_tag
156156
{
157157
SRC_STATE_VT *vt ;
158158

159-
double last_ratio, last_position ;
159+
fp_t last_ratio, last_position ;
160160

161161
SRC_ERROR error ;
162162
int channels ;
@@ -209,7 +209,7 @@ static inline int
209209
#ifdef USE_TARGET_ATTRIBUTE
210210
__attribute__((target("sse2")))
211211
#endif
212-
psf_lrint (double x)
212+
psf_lrint (fp_t x)
213213
{
214214
return _mm_cvtsd_si32 (_mm_load_sd (&x)) ;
215215
}
@@ -221,7 +221,7 @@ static inline int psf_lrintf (float x)
221221
return lrintf (x) ;
222222
} /* psf_lrintf */
223223

224-
static inline int psf_lrint (double x)
224+
static inline int psf_lrint (fp_t x)
225225
{
226226
return lrint (x) ;
227227
} /* psf_lrint */
@@ -231,9 +231,9 @@ static inline int psf_lrint (double x)
231231
** Common static inline functions.
232232
*/
233233

234-
static inline double
235-
fmod_one (double x)
236-
{ double res ;
234+
static inline fp_t
235+
fmod_one (fp_t x)
236+
{ fp_t res ;
237237

238238
res = x - psf_lrint (x) ;
239239
if (res < 0.0)
@@ -243,7 +243,7 @@ fmod_one (double x)
243243
} /* fmod_one */
244244

245245
static inline int
246-
is_bad_src_ratio (double ratio)
246+
is_bad_src_ratio (fp_t ratio)
247247
{ return (ratio < (1.0 / SRC_MAX_RATIO) || ratio > (1.0 * SRC_MAX_RATIO)) ;
248248
} /* is_bad_src_ratio */
249249

src/samplerate.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ src_process (SRC_STATE *state, SRC_DATA *data)
143143
} /* src_process */
144144

145145
long
146-
src_callback_read (SRC_STATE *state, double src_ratio, long frames, float *data)
146+
src_callback_read (SRC_STATE *state, fp_t src_ratio, long frames, float *data)
147147
{
148148
SRC_DATA src_data ;
149149

@@ -238,7 +238,7 @@ src_callback_read (SRC_STATE *state, double src_ratio, long frames, float *data)
238238
*/
239239

240240
int
241-
src_set_ratio (SRC_STATE *state, double new_ratio)
241+
src_set_ratio (SRC_STATE *state, fp_t new_ratio)
242242
{
243243
if (state == NULL)
244244
return SRC_ERR_BAD_STATE ;
@@ -321,7 +321,7 @@ src_get_version (void)
321321
} /* src_get_version */
322322

323323
int
324-
src_is_valid_ratio (double ratio)
324+
src_is_valid_ratio (fp_t ratio)
325325
{
326326
if (is_bad_src_ratio (ratio))
327327
return SRC_FALSE ;
@@ -461,7 +461,7 @@ src_int_to_float_array (const int *in, float *out, int len)
461461

462462
void
463463
src_float_to_int_array (const float *in, int *out, int len)
464-
{ double scaled_value ;
464+
{ fp_t scaled_value ;
465465

466466
for (int i = 0 ; i < len ; i++)
467467
{ scaled_value = in [i] * (8.0 * 0x10000000) ;

src/src_linear.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static SRC_STATE_VT linear_state_vt =
5353
static SRC_ERROR
5454
linear_vari_process (SRC_STATE *state, SRC_DATA *data)
5555
{ LINEAR_DATA *priv ;
56-
double src_ratio, input_index, rem ;
56+
fp_t src_ratio, input_index, rem ;
5757
int ch ;
5858

5959
if (data->input_frames <= 0)
@@ -93,7 +93,7 @@ linear_vari_process (SRC_STATE *state, SRC_DATA *data)
9393

9494
for (ch = 0 ; ch < state->channels ; ch++)
9595
{ data->data_out [priv->out_gen] = (float) (priv->last_value [ch] + input_index *
96-
((double) data->data_in [ch] - priv->last_value [ch])) ;
96+
((fp_t) data->data_in [ch] - priv->last_value [ch])) ;
9797
priv->out_gen ++ ;
9898
} ;
9999

@@ -120,7 +120,7 @@ linear_vari_process (SRC_STATE *state, SRC_DATA *data)
120120

121121
for (ch = 0 ; ch < state->channels ; ch++)
122122
{ data->data_out [priv->out_gen] = (float) (data->data_in [priv->in_used - state->channels + ch] + input_index *
123-
((double) data->data_in [priv->in_used + ch] - data->data_in [priv->in_used - state->channels + ch])) ;
123+
((fp_t) data->data_in [priv->in_used + ch] - data->data_in [priv->in_used - state->channels + ch])) ;
124124
priv->out_gen ++ ;
125125
} ;
126126

src/src_sinc.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#define MAKE_INCREMENT_T(x) ((increment_t) (x))
2727

2828
#define SHIFT_BITS 12
29-
#define FP_ONE ((double) (((increment_t) 1) << SHIFT_BITS))
29+
#define FP_ONE ((fp_t) (((increment_t) 1) << SHIFT_BITS))
3030
#define INV_FP_ONE (1.0 / FP_ONE)
3131

3232
/* Customixe max channls from Kconfig. */
@@ -61,14 +61,14 @@ typedef struct
6161

6262
int coeff_half_len, index_inc ;
6363

64-
double src_ratio, input_index ;
64+
fp_t src_ratio, input_index ;
6565

6666
coeff_t const *coeffs ;
6767

6868
int b_current, b_end, b_real_end, b_len ;
6969

7070
/* Sure hope noone does more than 128 channels at once. */
71-
double left_calc [MAX_CHANNELS], right_calc [MAX_CHANNELS] ;
71+
fp_t left_calc [MAX_CHANNELS], right_calc [MAX_CHANNELS] ;
7272

7373
float *buffer ;
7474
} SINC_FILTER ;
@@ -131,7 +131,7 @@ static SRC_STATE_VT sinc_mono_state_vt =
131131
} ;
132132

133133
static inline increment_t
134-
double_to_fp (double x)
134+
double_to_fp (fp_t x)
135135
{ return (increment_t) (psf_lrint ((x) * FP_ONE)) ;
136136
} /* double_to_fp */
137137

@@ -150,7 +150,7 @@ fp_fraction_part (increment_t x)
150150
{ return ((x) & ((((increment_t) 1) << SHIFT_BITS) - 1)) ;
151151
} /* fp_fraction_part */
152152

153-
static inline double
153+
static inline fp_t
154154
fp_to_double (increment_t x)
155155
{ return fp_fraction_part (x) * INV_FP_ONE ;
156156
} /* fp_to_double */
@@ -367,9 +367,9 @@ sinc_copy (SRC_STATE *state)
367367
** Beware all ye who dare pass this point. There be dragons here.
368368
*/
369369

370-
static inline double
370+
static inline fp_t
371371
calc_output_single (SINC_FILTER *filter, increment_t increment, increment_t start_filter_index)
372-
{ double fraction, left, right, icoeff ;
372+
{ fp_t fraction, left, right, icoeff ;
373373
increment_t filter_index, max_filter_index ;
374374
int data_index, coeff_count, indx ;
375375

@@ -430,7 +430,7 @@ calc_output_single (SINC_FILTER *filter, increment_t increment, increment_t star
430430
static SRC_ERROR
431431
sinc_mono_vari_process (SRC_STATE *state, SRC_DATA *data)
432432
{ SINC_FILTER *filter ;
433-
double input_index, src_ratio, count, float_increment, terminate, rem ;
433+
fp_t input_index, src_ratio, count, float_increment, terminate, rem ;
434434
increment_t increment, start_filter_index ;
435435
int half_filter_chan_len, samples_in_hand ;
436436

@@ -521,8 +521,8 @@ sinc_mono_vari_process (SRC_STATE *state, SRC_DATA *data)
521521
} /* sinc_mono_vari_process */
522522

523523
static inline void
524-
calc_output_stereo (SINC_FILTER *filter, int channels, increment_t increment, increment_t start_filter_index, double scale, float * output)
525-
{ double fraction, left [2], right [2], icoeff ;
524+
calc_output_stereo (SINC_FILTER *filter, int channels, increment_t increment, increment_t start_filter_index, fp_t scale, float * output)
525+
{ fp_t fraction, left [2], right [2], icoeff ;
526526
increment_t filter_index, max_filter_index ;
527527
int data_index, coeff_count, indx ;
528528

@@ -586,7 +586,7 @@ calc_output_stereo (SINC_FILTER *filter, int channels, increment_t increment, in
586586
SRC_ERROR
587587
sinc_stereo_vari_process (SRC_STATE *state, SRC_DATA *data)
588588
{ SINC_FILTER *filter ;
589-
double input_index, src_ratio, count, float_increment, terminate, rem ;
589+
fp_t input_index, src_ratio, count, float_increment, terminate, rem ;
590590
increment_t increment, start_filter_index ;
591591
int half_filter_chan_len, samples_in_hand ;
592592

@@ -676,8 +676,8 @@ sinc_stereo_vari_process (SRC_STATE *state, SRC_DATA *data)
676676
} /* sinc_stereo_vari_process */
677677

678678
static inline void
679-
calc_output_quad (SINC_FILTER *filter, int channels, increment_t increment, increment_t start_filter_index, double scale, float * output)
680-
{ double fraction, left [4], right [4], icoeff ;
679+
calc_output_quad (SINC_FILTER *filter, int channels, increment_t increment, increment_t start_filter_index, fp_t scale, float * output)
680+
{ fp_t fraction, left [4], right [4], icoeff ;
681681
increment_t filter_index, max_filter_index ;
682682
int data_index, coeff_count, indx ;
683683

@@ -742,7 +742,7 @@ calc_output_quad (SINC_FILTER *filter, int channels, increment_t increment, incr
742742
SRC_ERROR
743743
sinc_quad_vari_process (SRC_STATE *state, SRC_DATA *data)
744744
{ SINC_FILTER *filter ;
745-
double input_index, src_ratio, count, float_increment, terminate, rem ;
745+
fp_t input_index, src_ratio, count, float_increment, terminate, rem ;
746746
increment_t increment, start_filter_index ;
747747
int half_filter_chan_len, samples_in_hand ;
748748

@@ -832,8 +832,8 @@ sinc_quad_vari_process (SRC_STATE *state, SRC_DATA *data)
832832
} /* sinc_quad_vari_process */
833833

834834
static inline void
835-
calc_output_hex (SINC_FILTER *filter, int channels, increment_t increment, increment_t start_filter_index, double scale, float * output)
836-
{ double fraction, left [6], right [6], icoeff ;
835+
calc_output_hex (SINC_FILTER *filter, int channels, increment_t increment, increment_t start_filter_index, fp_t scale, float * output)
836+
{ fp_t fraction, left [6], right [6], icoeff ;
837837
increment_t filter_index, max_filter_index ;
838838
int data_index, coeff_count, indx ;
839839

@@ -897,7 +897,7 @@ calc_output_hex (SINC_FILTER *filter, int channels, increment_t increment, incre
897897
SRC_ERROR
898898
sinc_hex_vari_process (SRC_STATE *state, SRC_DATA *data)
899899
{ SINC_FILTER *filter ;
900-
double input_index, src_ratio, count, float_increment, terminate, rem ;
900+
fp_t input_index, src_ratio, count, float_increment, terminate, rem ;
901901
increment_t increment, start_filter_index ;
902902
int half_filter_chan_len, samples_in_hand ;
903903

@@ -987,10 +987,10 @@ sinc_hex_vari_process (SRC_STATE *state, SRC_DATA *data)
987987
} /* sinc_hex_vari_process */
988988

989989
static inline void
990-
calc_output_multi (SINC_FILTER *filter, increment_t increment, increment_t start_filter_index, int channels, double scale, float * output)
991-
{ double fraction, icoeff ;
990+
calc_output_multi (SINC_FILTER *filter, increment_t increment, increment_t start_filter_index, int channels, fp_t scale, float * output)
991+
{ fp_t fraction, icoeff ;
992992
/* The following line is 1999 ISO Standard C. If your compiler complains, get a better compiler. */
993-
double *left, *right ;
993+
fp_t *left, *right ;
994994
increment_t filter_index, max_filter_index ;
995995
int data_index, coeff_count, indx ;
996996

@@ -1062,7 +1062,7 @@ calc_output_multi (SINC_FILTER *filter, increment_t increment, increment_t start
10621062
static SRC_ERROR
10631063
sinc_multichan_vari_process (SRC_STATE *state, SRC_DATA *data)
10641064
{ SINC_FILTER *filter ;
1065-
double input_index, src_ratio, count, float_increment, terminate, rem ;
1065+
fp_t input_index, src_ratio, count, float_increment, terminate, rem ;
10661066
increment_t increment, start_filter_index ;
10671067
int half_filter_chan_len, samples_in_hand ;
10681068

src/src_zoh.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static SRC_STATE_VT zoh_state_vt =
5151
static SRC_ERROR
5252
zoh_vari_process (SRC_STATE *state, SRC_DATA *data)
5353
{ ZOH_DATA *priv ;
54-
double src_ratio, input_index, rem ;
54+
fp_t src_ratio, input_index, rem ;
5555
int ch ;
5656

5757
if (data->input_frames <= 0)

0 commit comments

Comments
 (0)