Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove shift of signed int in src_float_to_short_array #86

Merged
merged 2 commits into from
Aug 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 11 additions & 17 deletions src/samplerate.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,24 +495,18 @@ src_short_to_float_array (const short *in, float *out, int len)

void
src_float_to_short_array (const float *in, short *out, int len)
{ double scaled_value ;

{
while (len)
{ len -- ;

scaled_value = in [len] * (8.0 * 0x10000000) ;
if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
{ out [len] = 32767 ;
continue ;
} ;
if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
{ out [len] = -32768 ;
continue ;
} ;

out [len] = (short) (lrint (scaled_value) >> 16) ;
} ;

{ float scaled_value ;
len -- ;
scaled_value = in [len] * 32768.f ;
if (scaled_value >= 32767.f)
out [len] = 32767 ;
else if (scaled_value <= -32768.f)
out [len] = -32768 ;
else
out [len] = (short) (lrintf (scaled_value)) ;
}
} /* src_float_to_short_array */

void
Expand Down
8 changes: 6 additions & 2 deletions tests/float_short_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ static void
float_to_short_test (void)
{
static float fpos [] =
{ 0.95, 0.99, 1.0, 1.01, 1.1, 2.0, 11.1, 111.1, 2222.2, 33333.3
{ 0.95, 0.99, 1.0, 1.01, 1.1, 2.0, 11.1, 111.1, 2222.2, 33333.3,
// Some "almost 1" as corner cases
32767./32768., (32767. + 0.4)/32768., (32767. + 0.5)/32768., (32767. + 0.6)/32768., (32767. + 0.9)/32768.,
} ;
static float fneg [] =
{ -0.95, -0.99, -1.0, -1.01, -1.1, -2.0, -11.1, -111.1, -2222.2, -33333.3
{ -0.95, -0.99, -1.0, -1.01, -1.1, -2.0, -11.1, -111.1, -2222.2, -33333.3,
// Some "almost 1" as corner cases
-32767./32768., -(32767. + 0.4)/32768., -(32767. + 0.5)/32768., -(32767. + 0.6)/32768., -(32767. + 0.9)/32768.,
} ;

static short out [MAX (ARRAY_LEN (fpos), ARRAY_LEN (fneg))] ;
Expand Down