Skip to content

Commit

Permalink
rounding through intrinsics
Browse files Browse the repository at this point in the history
  • Loading branch information
- committed Jun 23, 2022
1 parent 744d638 commit cf587b0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/cubeb_mixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,22 +377,24 @@ MixerContext::init()

for (uint32_t j = 0; j < _in_ch_count; j++) {
double target = _matrix[i][j] * 32768 + rem;
int value = lrintf(target);
int value = _mm_cvtsd_si32(_mm_set_sd(target)); // round double to int
rem += target - value;
sum += std::abs(value);
}
maxsum = std::max(maxsum, sum);
}
if (maxsum > 32768) {
if (maxsum > 32768) { //> or >=?
_clipping = true;
}
}

// FIXME quantize for integers
// June 23, 2022: I made a change but have no idea what was wanted
for (uint32_t i = 0; i < CHANNELS_MAX; i++) {
int ch_in = 0;
for (uint32_t j = 0; j < CHANNELS_MAX; j++) {
_matrix32[i][j] = lrintf(_matrix[i][j] * 32768);
// round double to int
_matrix32[i][j] = _mm_cvtsd_si32(_mm_set_sd(_matrix[i][j] * 32768));
if (_matrix[i][j]) {
_matrix_ch[i][++ch_in] = j;
}
Expand Down
3 changes: 2 additions & 1 deletion src/cubeb_sndio.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ float_to_s16(void * ptr, long nsamp, float volume)
int s;

while (nsamp-- > 0) {
s = lrintf(*(src++) * mult);
// round float to int
s = _mm_cvt_ss2si(_mm_set_ss(*(src++) * mult));
if (s < -32768)
s = -32768;
else if (s > 32767)
Expand Down
3 changes: 2 additions & 1 deletion src/cubeb_wasapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,8 @@ refill(cubeb_stream * stm, void * input_buffer, long input_frames_count,
case CUBEB_SAMPLE_S16NE: {
short * buf = static_cast<short *>(dest);
for (long i = 0; i < out_samples; ++i) {
buf[i] = static_cast<short>(static_cast<float>(buf[i]) * volume);
// round float to int
buf[i] = _mm_cvt_ss2si(_mm_set_ss(buf[i] * volume));
}
break;
}
Expand Down

0 comments on commit cf587b0

Please sign in to comment.