Skip to content

Commit cf587b0

Browse files
author
-
committed
rounding through intrinsics
1 parent 744d638 commit cf587b0

3 files changed

Lines changed: 9 additions & 5 deletions

File tree

src/cubeb_mixer.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,22 +377,24 @@ MixerContext::init()
377377

378378
for (uint32_t j = 0; j < _in_ch_count; j++) {
379379
double target = _matrix[i][j] * 32768 + rem;
380-
int value = lrintf(target);
380+
int value = _mm_cvtsd_si32(_mm_set_sd(target)); // round double to int
381381
rem += target - value;
382382
sum += std::abs(value);
383383
}
384384
maxsum = std::max(maxsum, sum);
385385
}
386-
if (maxsum > 32768) {
386+
if (maxsum > 32768) { //> or >=?
387387
_clipping = true;
388388
}
389389
}
390390

391391
// FIXME quantize for integers
392+
// June 23, 2022: I made a change but have no idea what was wanted
392393
for (uint32_t i = 0; i < CHANNELS_MAX; i++) {
393394
int ch_in = 0;
394395
for (uint32_t j = 0; j < CHANNELS_MAX; j++) {
395-
_matrix32[i][j] = lrintf(_matrix[i][j] * 32768);
396+
// round double to int
397+
_matrix32[i][j] = _mm_cvtsd_si32(_mm_set_sd(_matrix[i][j] * 32768));
396398
if (_matrix[i][j]) {
397399
_matrix_ch[i][++ch_in] = j;
398400
}

src/cubeb_sndio.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ float_to_s16(void * ptr, long nsamp, float volume)
106106
int s;
107107

108108
while (nsamp-- > 0) {
109-
s = lrintf(*(src++) * mult);
109+
// round float to int
110+
s = _mm_cvt_ss2si(_mm_set_ss(*(src++) * mult));
110111
if (s < -32768)
111112
s = -32768;
112113
else if (s > 32767)

src/cubeb_wasapi.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,8 @@ refill(cubeb_stream * stm, void * input_buffer, long input_frames_count,
938938
case CUBEB_SAMPLE_S16NE: {
939939
short * buf = static_cast<short *>(dest);
940940
for (long i = 0; i < out_samples; ++i) {
941-
buf[i] = static_cast<short>(static_cast<float>(buf[i]) * volume);
941+
// round float to int
942+
buf[i] = _mm_cvt_ss2si(_mm_set_ss(buf[i] * volume));
942943
}
943944
break;
944945
}

0 commit comments

Comments
 (0)