Skip to content

Commit

Permalink
Add loop vectorisation comments and reword misleading sentences
Browse files Browse the repository at this point in the history
  • Loading branch information
acolombier committed Jun 23, 2024
1 parent 2d831ae commit 4621cdc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 45 deletions.
4 changes: 2 additions & 2 deletions src/engine/bufferscalers/rubberbandwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace {
/// most optimum way.
///
/// The following table provide the expected number of channel per task with
/// stereo processing (the default behaviour)
/// stereo processing for a given number of CPU core (the default behaviour)
///
/// | NbOfCore | Stereo | Stem |
/// |----------|--------|------|
Expand All @@ -28,7 +28,7 @@ namespace {
///
/// The following table provide the expected number of channel per task when the
/// user has explicitly requested stereo channel to be processed as mono
/// channels.
/// channels for a given number of CPU core.
///
/// | NbOfCore | Stereo | Stem |
/// |----------|--------|------|
Expand Down
28 changes: 14 additions & 14 deletions src/preferences/dialog/dlgprefsound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ const QString kKeylockMultiThreadedAvailable = QStringLiteral("<p>") +
QStringLiteral("</p>");
const QString kKeylockMultiThreadedUnavailableMono = QStringLiteral("<i>") +
QObject::tr(
"Multi threading mode is incompatible with mono main mix.") +
"Dual threading mode is incompatible with mono main mix.") +
QStringLiteral("</i>");
const QString kKeylockMultiThreadedUnavailableRubberband =
QStringLiteral("<i>") +
QObject::tr("Multi threading mode is only available with RubberBand.") +
QObject::tr("Dual threading mode is only available with RubberBand.") +
QStringLiteral("</i>");
#endif
} // namespace
Expand Down Expand Up @@ -213,12 +213,12 @@ DlgPrefSound::DlgPrefSound(QWidget* pParent,
this,
&DlgPrefSound::settingChanged);
#ifdef __RUBBERBAND__
connect(keylockMultithreadedCheckBox,
connect(keylockDualthreadedCheckBox,
&QCheckBox::clicked,
this,
&DlgPrefSound::updateKeylockMultithreading);
#else
keylockMultithreadedCheckBox->hide();
keylockDualthreadedCheckBox->hide();
#endif

connect(queryButton, &QAbstractButton::clicked, this, &DlgPrefSound::queryClicked);
Expand Down Expand Up @@ -341,11 +341,11 @@ void DlgPrefSound::slotApply() {
bool keylockMultithreading = m_pSettings->getValue(
ConfigKey(kAppGroup, "keylock_multithreading"), false);
m_pSettings->setValue(ConfigKey(kAppGroup, "keylock_multithreading"),
keylockMultithreadedCheckBox->isChecked() &&
keylockMultithreadedCheckBox->isEnabled());
keylockDualthreadedCheckBox->isChecked() &&
keylockDualthreadedCheckBox->isEnabled());
if (keylockMultithreading !=
(keylockMultithreadedCheckBox->isChecked() &&
keylockMultithreadedCheckBox->isEnabled())) {
(keylockDualthreadedCheckBox->isChecked() &&
keylockDualthreadedCheckBox->isEnabled())) {
QMessageBox::information(this,
tr("Information"),
tr("Mixxx must be restarted before the multi-threaded "
Expand Down Expand Up @@ -544,7 +544,7 @@ void DlgPrefSound::loadSettings(const SoundManagerConfig& config) {

#ifdef __RUBBERBAND__
// Default is no multi threading on keylock
keylockMultithreadedCheckBox->setChecked(m_pSettings->getValue(
keylockDualthreadedCheckBox->setChecked(m_pSettings->getValue(
ConfigKey(kAppGroup, QStringLiteral("keylock_multithreading")),
false));
#endif
Expand Down Expand Up @@ -749,8 +749,8 @@ void DlgPrefSound::settingChanged() {
.value<EngineBuffer::KeylockEngine>() !=
EngineBuffer::KeylockEngine::SoundTouch;
bool monoMix = mainOutputModeComboBox->currentIndex() == 1;
keylockMultithreadedCheckBox->setEnabled(!monoMix && supportedScaler);
keylockMultithreadedCheckBox->setToolTip(monoMix
keylockDualthreadedCheckBox->setEnabled(!monoMix && supportedScaler);
keylockDualthreadedCheckBox->setToolTip(monoMix
? kKeylockMultiThreadedUnavailableMono
: (supportedScaler
? kKeylockMultiThreadedAvailable
Expand Down Expand Up @@ -778,7 +778,7 @@ void DlgPrefSound::updateKeylockMultithreading(bool enabled) {
tr("Yes, I know what I am doing"), QMessageBox::RejectRole);
msg.setDefaultButton(pNoBtn);
msg.exec();
keylockMultithreadedCheckBox->setChecked(msg.clickedButton() == pYesBtn);
keylockDualthreadedCheckBox->setChecked(msg.clickedButton() == pYesBtn);
#endif
}

Expand Down Expand Up @@ -932,8 +932,8 @@ void DlgPrefSound::mainOutputModeComboBoxChanged(int value) {
bool supportedScaler = keylockComboBox->currentData()
.value<EngineBuffer::KeylockEngine>() !=
EngineBuffer::KeylockEngine::SoundTouch;
keylockMultithreadedCheckBox->setEnabled(!value && supportedScaler);
keylockMultithreadedCheckBox->setToolTip(
keylockDualthreadedCheckBox->setEnabled(!value && supportedScaler);
keylockDualthreadedCheckBox->setToolTip(
value ? kKeylockMultiThreadedUnavailableMono
: (supportedScaler
? kKeylockMultiThreadedAvailable
Expand Down
4 changes: 2 additions & 2 deletions src/preferences/dialog/dlgprefsounddlg.ui
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,15 @@
<widget class="QComboBox" name="keylockComboBox"/>
</item>
<item>
<widget class="QCheckBox" name="keylockMultithreadedCheckBox">
<widget class="QCheckBox" name="keylockDualthreadedCheckBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Multi-threaded</string>
<string>Dual-threaded Stereo</string>
</property>
</widget>
</item>
Expand Down
47 changes: 20 additions & 27 deletions src/util/sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ void SampleUtil::applyAlternatingGain(CSAMPLE* pBuffer, CSAMPLE gain1,
return;
}

// note: LOOP VECTORIZED.
for (SINT i = 0; i < numSamples / 2; ++i) {
pBuffer[i * 2] *= gain1;
pBuffer[i * 2 + 1] *= gain2;
Expand Down Expand Up @@ -217,6 +218,7 @@ void SampleUtil::applyRampingAlternatingGain(CSAMPLE* pBuffer,
/ CSAMPLE_GAIN(numSamples / 2);
if (gain2Delta != 0) {
const CSAMPLE_GAIN start_gain = gain2Old + gain2Delta;
// note: LOOP VECTORIZED. (gcc + clang >= 14)
for (int i = 0; i < numSamples / 2; ++i) {
const CSAMPLE_GAIN gain = start_gain + gain2Delta * i;
pBuffer[i * 2 + 1] *= gain;
Expand All @@ -233,6 +235,7 @@ void SampleUtil::applyRampingAlternatingGain(CSAMPLE* pBuffer,
void SampleUtil::add(CSAMPLE* M_RESTRICT pDest,
const CSAMPLE* M_RESTRICT pSrc,
SINT numSamples) {
// note: LOOP VECTORIZED. (gcc + clang >= 14)
for (SINT i = 0; i < numSamples; ++i) {
pDest[i] += pSrc[i];
}
Expand Down Expand Up @@ -314,6 +317,7 @@ void SampleUtil::add3WithGain(CSAMPLE* pDest,
return;
}

// note: LOOP VECTORIZED.
for (SINT i = 0; i < numSamples; ++i) {
pDest[i] += pSrc1[i] * gain1 + pSrc2[i] * gain2 + pSrc3[i] * gain3;
}
Expand All @@ -332,6 +336,7 @@ void SampleUtil::copyWithGain(CSAMPLE* M_RESTRICT pDest,
return;
}

// note: LOOP VECTORIZED.
for (SINT i = 0; i < numSamples; ++i) {
pDest[i] = pSrc[i] * gain;
}
Expand Down Expand Up @@ -470,6 +475,7 @@ CSAMPLE SampleUtil::maxAbsAmplitude(const CSAMPLE* pBuffer, SINT numSamples) {
// static
void SampleUtil::copyClampBuffer(CSAMPLE* M_RESTRICT pDest,
const CSAMPLE* M_RESTRICT pSrc, SINT iNumSamples) {
// note: LOOP VECTORIZED.
for (SINT i = 0; i < iNumSamples; ++i) {
pDest[i] = clampSample(pSrc[i]);
}
Expand Down Expand Up @@ -555,11 +561,13 @@ void SampleUtil::linearCrossfadeStereoBuffersOut(
// M_RESTRICT unoptimizes the function for some reason.
const CSAMPLE_GAIN cross_inc = CSAMPLE_GAIN_ONE
/ CSAMPLE_GAIN(numSamples / 2);
// note: LOOP VECTORIZED only with "int i" (not SINT i)
for (int i = 0; i < numSamples / 2; ++i) {
const CSAMPLE_GAIN cross_mix = cross_inc * i;
pDestSrcFadeOut[i * 2] *= (CSAMPLE_GAIN_ONE - cross_mix);
pDestSrcFadeOut[i * 2] += pSrcFadeIn[i * 2] * cross_mix;
}
// note: LOOP VECTORIZED only with "int i" (not SINT i)
for (int i = 0; i < numSamples / 2; ++i) {
const CSAMPLE_GAIN cross_mix = cross_inc * i;
pDestSrcFadeOut[i * 2 + 1] *= (CSAMPLE_GAIN_ONE - cross_mix);
Expand Down Expand Up @@ -640,9 +648,10 @@ void SampleUtil::linearCrossfadeBuffersOut(
int numFrame = numSamples / channelCount;
const CSAMPLE_GAIN cross_inc =
CSAMPLE_GAIN_ONE / CSAMPLE_GAIN(numSamples / channelCount);
for (int chIdx = 0; chIdx < channelCount; chIdx++) {
for (int i = 0; i < numFrame; ++i) {
const CSAMPLE_GAIN cross_mix = cross_inc * i;
for (int i = 0; i < numFrame; ++i) {
const CSAMPLE_GAIN cross_mix = cross_inc * i;
// note: LOOP VECTORIZED.
for (int chIdx = 0; chIdx < channelCount; chIdx++) {
pDestSrcFadeOut[i * channelCount + chIdx] *= (CSAMPLE_GAIN_ONE - cross_mix);
pDestSrcFadeOut[i * channelCount + chIdx] +=
pSrcFadeIn[i * channelCount + chIdx] * cross_mix;
Expand All @@ -660,11 +669,13 @@ void SampleUtil::linearCrossfadeStereoBuffersIn(
SINT numSamples) {
// M_RESTRICT unoptimizes the function for some reason.
const CSAMPLE_GAIN cross_inc = CSAMPLE_GAIN_ONE / CSAMPLE_GAIN(numSamples / 2);
/// note: LOOP VECTORIZED only with "int i" (not SINT i)
for (int i = 0; i < numSamples / 2; ++i) {
const CSAMPLE_GAIN cross_mix = cross_inc * i;
pDestSrcFadeIn[i * 2] *= cross_mix;
pDestSrcFadeIn[i * 2] += pSrcFadeOut[i * 2] * (CSAMPLE_GAIN_ONE - cross_mix);
}
// note: LOOP VECTORIZED only with "int i" (not SINT i)
for (int i = 0; i < numSamples / 2; ++i) {
const CSAMPLE_GAIN cross_mix = cross_inc * i;
pDestSrcFadeIn[i * 2 + 1] *= cross_mix;
Expand All @@ -683,39 +694,18 @@ void SampleUtil::linearCrossfadeStemBuffersIn(
const CSAMPLE_GAIN cross_mix = cross_inc * i;
pDestSrcFadeIn[i * 8] *= cross_mix;
pDestSrcFadeIn[i * 8] += pSrcFadeOut[i * 8] * (CSAMPLE_GAIN_ONE - cross_mix);
}
for (int i = 0; i < numSamples / 8; ++i) {
const CSAMPLE_GAIN cross_mix = cross_inc * i;
pDestSrcFadeIn[i * 8 + 1] *= cross_mix;
pDestSrcFadeIn[i * 8 + 1] += pSrcFadeOut[i * 8 + 1] * (CSAMPLE_GAIN_ONE - cross_mix);
}
for (int i = 0; i < numSamples / 8; ++i) {
const CSAMPLE_GAIN cross_mix = cross_inc * i;
pDestSrcFadeIn[i * 8 + 2] *= cross_mix;
pDestSrcFadeIn[i * 8 + 2] += pSrcFadeOut[i * 8 + 2] * (CSAMPLE_GAIN_ONE - cross_mix);
}
for (int i = 0; i < numSamples / 8; ++i) {
const CSAMPLE_GAIN cross_mix = cross_inc * i;
pDestSrcFadeIn[i * 8 + 3] *= cross_mix;
pDestSrcFadeIn[i * 8 + 3] += pSrcFadeOut[i * 8 + 3] * (CSAMPLE_GAIN_ONE - cross_mix);
}
for (int i = 0; i < numSamples / 8; ++i) {
const CSAMPLE_GAIN cross_mix = cross_inc * i;
pDestSrcFadeIn[i * 8 + 4] *= cross_mix;
pDestSrcFadeIn[i * 8 + 4] += pSrcFadeOut[i * 8 + 4] * (CSAMPLE_GAIN_ONE - cross_mix);
}
for (int i = 0; i < numSamples / 8; ++i) {
const CSAMPLE_GAIN cross_mix = cross_inc * i;
pDestSrcFadeIn[i * 8 + 5] *= cross_mix;
pDestSrcFadeIn[i * 8 + 5] += pSrcFadeOut[i * 8 + 5] * (CSAMPLE_GAIN_ONE - cross_mix);
}
for (int i = 0; i < numSamples / 8; ++i) {
const CSAMPLE_GAIN cross_mix = cross_inc * i;
pDestSrcFadeIn[i * 8 + 6] *= cross_mix;
pDestSrcFadeIn[i * 8 + 6] += pSrcFadeOut[i * 8 + 6] * (CSAMPLE_GAIN_ONE - cross_mix);
}
for (int i = 0; i < numSamples / 8; ++i) {
const CSAMPLE_GAIN cross_mix = cross_inc * i;
pDestSrcFadeIn[i * 8 + 7] *= cross_mix;
pDestSrcFadeIn[i * 8 + 7] += pSrcFadeOut[i * 8 + 7] * (CSAMPLE_GAIN_ONE - cross_mix);
}
Expand Down Expand Up @@ -744,9 +734,10 @@ void SampleUtil::linearCrossfadeBuffersIn(
int numFrame = numSamples / channelCount;
const CSAMPLE_GAIN cross_inc =
CSAMPLE_GAIN_ONE / CSAMPLE_GAIN(numSamples / channelCount);
for (int chIdx = 0; chIdx < channelCount; chIdx++) {
for (int i = 0; i < numFrame; ++i) {
const CSAMPLE_GAIN cross_mix = cross_inc * i;
for (int i = 0; i < numFrame; ++i) {
const CSAMPLE_GAIN cross_mix = cross_inc * i;
// note: LOOP VECTORIZED.
for (int chIdx = 0; chIdx < channelCount; chIdx++) {
pDestSrcFadeIn[i * channelCount + chIdx] *= cross_mix;
pDestSrcFadeIn[i * channelCount + chIdx] +=
pSrcFadeOut[i * channelCount + chIdx] *
Expand All @@ -764,6 +755,7 @@ void SampleUtil::mixStereoToMono(CSAMPLE* M_RESTRICT pDest,
SINT numSamples) {
const CSAMPLE_GAIN mixScale = CSAMPLE_GAIN_ONE
/ (CSAMPLE_GAIN_ONE + CSAMPLE_GAIN_ONE);
// note: LOOP VECTORIZED
for (SINT i = 0; i < numSamples / 2; ++i) {
pDest[i * 2] = (pSrc[i * 2] + pSrc[i * 2 + 1]) * mixScale;
pDest[i * 2 + 1] = pDest[i * 2];
Expand All @@ -773,6 +765,7 @@ void SampleUtil::mixStereoToMono(CSAMPLE* M_RESTRICT pDest,
// static
void SampleUtil::mixStereoToMono(CSAMPLE* pBuffer, SINT numSamples) {
const CSAMPLE_GAIN mixScale = CSAMPLE_GAIN_ONE / (CSAMPLE_GAIN_ONE + CSAMPLE_GAIN_ONE);
// note: LOOP VECTORIZED
for (SINT i = 0; i < numSamples / 2; ++i) {
pBuffer[i * 2] = (pBuffer[i * 2] + pBuffer[i * 2 + 1]) * mixScale;
pBuffer[i * 2 + 1] = pBuffer[i * 2];
Expand Down

0 comments on commit 4621cdc

Please sign in to comment.