diff --git a/app/audio/audiovisualwaveform.cpp b/app/audio/audiovisualwaveform.cpp index 5d1e86dfd..1018c3e76 100644 --- a/app/audio/audiovisualwaveform.cpp +++ b/app/audio/audiovisualwaveform.cpp @@ -21,6 +21,11 @@ #include "audiovisualwaveform.h" #include +#include + +#ifdef Q_PROCESSOR_X86 +#include +#endif #include "config/config.h" #include "common/functiontimer.h" @@ -286,17 +291,62 @@ AudioVisualWaveform::Sample AudioVisualWaveform::SumSamples(const float *samples return summed_samples; } +#ifdef Q_PROCESSOR_X86 +void ExpandMinMaxSSE(float *a, int start, int end, float &min_val, float &max_val) +{ + // load the first 4 elements of 'a' into min and max (they are 4 * 32 = 128 bits) + __m128 max = _mm_loadu_ps(a + start); + __m128 min = _mm_loadu_ps(a + start); + + // loop over 'a' and compare current elements with min and max 4 by 4. + // we need to make sure we don't read out of boundaries should 'a' lenght be not mod. 4 + for(int i = 4; i < end-4; i+=4) { + __m128 cur = _mm_loadu_ps(a + start + i); + max = _mm_max_ps(max, cur); + min = _mm_min_ps(min, cur); + } + // so we read the last 4 (or less) elements in a safe manner. + __m128 cur = _mm_loadu_ps(a + end - 4); + max = _mm_max_ps(max, cur); + min = _mm_min_ps(min, cur); + // this potentially overlaps up to the last 3 elements but it's not an issue. + + // min and max will contain 4 min and max. To get the absolute min and max + // we need to compare the 4 values over themselves by shuffling each time. + for (int i = 0; i < 3; i++) { + max = _mm_max_ps(max, _mm_shuffle_ps(max, max, 0x93)); + min = _mm_min_ps(min, _mm_shuffle_ps(min, min, 0x93)); + } + // now min and max contain 4 identical items each representing min and max value respectively. + + // and we store the first one into a float variable. + _mm_store_ss(&max_val, max); + _mm_store_ss(&min_val, min); + // I bet you don't find annotated low level code very often. +} +#endif + AudioVisualWaveform::Sample AudioVisualWaveform::SumSamples(SampleBufferPtr samples, int start_index, int length) { - AudioVisualWaveform::Sample summed_samples(samples->audio_params().channel_count()); + int channels = samples->audio_params().channel_count(); + AudioVisualWaveform::Sample summed_samples(channels); - int end_index = start_index + length; - - for (int i=start_index; iaudio_params().channel_count(); channel++) { - ExpandMinMax(summed_samples[channel], samples->data(channel)[i]); + ExpandMinMaxSSE(samples->data(channel), start_index, length, summed_samples[channel].min, summed_samples[channel].max); } - } + #else + int end_index = start_index + length; + for (int channel=0; channelaudio_params().channel_count(); channel++) { + for (int i=start_index; idata(channel)[i]); + } + } + // for reference: this approximation is n x faster (and less accurate) for a n-tracks clip + // for (int i=start_index; idata(i%channels)[i]); + // } + #endif return summed_samples; } @@ -444,6 +494,10 @@ void AudioVisualWaveform::ExpandMinMax(AudioVisualWaveform::SamplePerChannel &su if (value > sum.max) { sum.max = value; } + + // to avoid branching + // sum.min = std::min(value, sum.min); + // sum.max = std::max(value, sum.max); } } diff --git a/app/node/math/math/mathbase.cpp b/app/node/math/math/mathbase.cpp index f19ede011..f488c4bb1 100644 --- a/app/node/math/math/mathbase.cpp +++ b/app/node/math/math/mathbase.cpp @@ -23,6 +23,10 @@ #include #include +#ifdef Q_PROCESSOR_X86 +#include +#endif + #include "common/tohex.h" #include "node/distort/transform/transformdistortnode.h" #include "render/color.h" @@ -163,6 +167,55 @@ void MathNodeBase::PushVector(NodeValueTable *output, olive::NodeValue::Type typ } } +void MathNodeBase::PerformAllOnFloatBuffer(Operation operation, float *a, float b, int start, int end) +{ + for (int j=start;jaudio_params().channel_count();i++) { - for (int j=0;jsample_count();j++) { - job.samples()->data(i)[j] = PerformAll(operation, job.samples()->data(i)[j], number); - } +#ifdef Q_PROCESSOR_X86 + // Use SSE instructions for optimization + PerformAllOnFloatBufferSSE(operation, job.samples()->data(i), number, 0, job.samples()->sample_count()); +#else + PerformAllOnFloatBuffer(operation, job.samples()->data(i), number, 0, job.samples()->sample_count()); +#endif } } diff --git a/app/node/math/math/mathbase.h b/app/node/math/math/mathbase.h index add040fe0..940bbee21 100644 --- a/app/node/math/math/mathbase.h +++ b/app/node/math/math/mathbase.h @@ -101,6 +101,11 @@ protected: template static T PerformAddSubMultDiv(Operation operation, T a, U b); +#ifdef Q_PROCESSOR_X86 + static void PerformAllOnFloatBuffer(Operation operation, float *a, float b, int start, int end); + static void PerformAllOnFloatBufferSSE(Operation operation, float *a, float b, int start, int end); +#endif + static QString GetShaderUniformType(const NodeValue::Type& type); static QString GetShaderVariableCall(const QString& input_id, const NodeValue::Type& type, const QString &coord_op = QString());