From 6b5cb5b5f75aa1f001bbfdd34d453e40594c3e14 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Wed, 20 Feb 2019 18:41:33 -0500 Subject: [PATCH] Clean up dsp headers and documentation. --- include/dsp/simd.hpp | 29 ++++++++++++++++------------- include/dsp/window.hpp | 10 +++++----- include/engine/Port.hpp | 2 +- include/rack.hpp | 1 + 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/include/dsp/simd.hpp b/include/dsp/simd.hpp index 0a28536a..5661475a 100644 --- a/include/dsp/simd.hpp +++ b/include/dsp/simd.hpp @@ -1,27 +1,27 @@ #include "sse_mathfun.h" #include #include -#include +#include namespace rack { namespace dsp { -/** Casts an int to float, bitwise without conversion. */ -inline float cast_i32_f32(int i) { - static_assert(sizeof(int) == sizeof(float), "int and float must be the same size"); - // Should be optimized to two `mov` instructions - float f; - std::memcpy(&f, &i, sizeof(f)); - return f; -} +/** Casts the literal bits of FROM to TO without type conversion. +API copied from C++20. +Usage example: -inline int cast_f32_i32(float f) { - float i; - std::memcpy(&i, &f, sizeof(i)); - return i; + printf("%08x\n", bit_cast(1.f)); // Prints 3f800000 +*/ +template +TO bit_cast(const FROM &x) { + static_assert(sizeof(FROM) == sizeof(TO), "types must have equal size"); + // Should be optimized to two `mov` instructions + TO y; + std::memcpy(&y, &x, sizeof(x)); + return y; } @@ -30,6 +30,7 @@ inline int cast_f32_i32(float f) { This class is designed to be used just like `float` scalars, with extra features for handling bitwise logic, conditions, loading, and storing. Usage example: + float a[4], b[4]; f32_4 a = f32_4::load(in); f32_4 b = 2.f * a / (1 - a); @@ -117,9 +118,11 @@ DECLARE_F32_4_OPERATOR_INFIX(operator/, _mm_div_ps) /** Use these to apply logic, bit masks, and conditions to elements. + Examples: Subtract 1 from value if greater than or equal to 1. + x -= (x >= 1.f) & 1.f; */ DECLARE_F32_4_OPERATOR_INFIX(operator^, _mm_xor_ps) diff --git a/include/dsp/window.hpp b/include/dsp/window.hpp index f53dfd20..e28ccc30 100644 --- a/include/dsp/window.hpp +++ b/include/dsp/window.hpp @@ -6,7 +6,7 @@ namespace rack { namespace dsp { -/** Hann window function +/** Hann window function. p: proportion from [0, 1], usually `i / (len - 1)` https://en.wikipedia.org/wiki/Window_function#Hann_and_Hamming_windows */ @@ -14,14 +14,14 @@ inline float hann(float p) { return 0.5f * (1.f - std::cos(2*M_PI * p)); } -/** Applies the Hann window to a signal `x` */ +/** Applies the Hann window to a signal `x`. */ inline void hannWindow(float *x, int len) { for (int i = 0; i < len; i++) { x[i] *= hann((float) i / (len - 1)); } } -/** Blackman window function +/** Blackman window function. https://en.wikipedia.org/wiki/Window_function#Blackman_window A typical alpha value is 0.16. */ @@ -39,7 +39,7 @@ inline void blackmanWindow(float alpha, float *x, int len) { } -/** Blackman-Nuttall window function +/** Blackman-Nuttall window function. https://en.wikipedia.org/wiki/Window_function#Blackman%E2%80%93Nuttall_window */ inline float blackmanNuttall(float p) { @@ -56,7 +56,7 @@ inline void blackmanNuttallWindow(float *x, int len) { } } -/** Blackman-Harris window function +/** Blackman-Harris window function. https://en.wikipedia.org/wiki/Window_function#Blackman%E2%80%93Harris_window */ inline float blackmanHarris(float p) { diff --git a/include/engine/Port.hpp b/include/engine/Port.hpp index 04936302..72a73bbb 100644 --- a/include/engine/Port.hpp +++ b/include/engine/Port.hpp @@ -43,7 +43,7 @@ struct alignas(32) Port { /** Returns the given channel's voltage if the port is polyphonic, otherwise returns the first voltage (channel 0). */ float getPolyVoltage(int channel) { - return (channels > 1) ? getVoltage(channel) : getVoltage(0); + return (channels == 1) ? getVoltage(0) : getVoltage(channel); } /** Returns the voltage if a cable is connected, otherwise returns the given normal voltage. */ diff --git a/include/rack.hpp b/include/rack.hpp index 8d134873..f6a5f8ac 100644 --- a/include/rack.hpp +++ b/include/rack.hpp @@ -80,6 +80,7 @@ #include "plugin/callbacks.hpp" #include "dsp/common.hpp" +#include "dsp/simd.hpp" #include "dsp/digital.hpp" #include "dsp/fft.hpp" #include "dsp/filter.hpp"