BogaudioModules

BogaudioModules for VCV Rack
Log | Files | Refs | README | LICENSE

commit 95b491117c96576a24150ef5463486f2368dd784
parent 25a9e6ae09fdcda00d66438eefa28aa6c93b6cad
Author: Matt Demanett <matt@demanett.net>
Date:   Sat, 21 Apr 2018 20:20:07 -0400

Rudimentary fixed-width math.

Diffstat:
Asrc/dsp/fixed.hpp | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtest/testmain.cpp | 59+++++++++++++++++++++++++++++++++++++++++++++++------------
2 files changed, 100 insertions(+), 12 deletions(-)

diff --git a/src/dsp/fixed.hpp b/src/dsp/fixed.hpp @@ -0,0 +1,53 @@ +#pragma once + +#include <stdint.h> +#include <math.h> + +namespace bogaudio { +namespace dsp { + +template<typename I, const int SCALE> +struct Fixed { + static constexpr I scale = ((I)1) << SCALE; + static constexpr double inverseScale = 1.0 / (double)scale; + typedef union { I v; } value_t; + value_t _v; + + Fixed() { _v.v = 0; } + Fixed(const value_t& o) { _v.v = o.v; } + Fixed(const int32_t& o) { _v.v = o * scale; } + Fixed(const int64_t& o) { _v.v = o * scale; } + Fixed(const double& o) { _v.v = round(o * scale); } + Fixed(const float& o) { _v.v = roundf(o * scale); } + + inline operator int32_t() const { return _v.v / scale; } + inline operator int64_t() const { return _v.v / scale; } + inline operator double() const { return inverseScale * (double)_v.v; } + inline operator float() const { return inverseScale * (float)_v.v; } + + inline Fixed& operator+=(const Fixed& o) { _v.v += o._v.v; return *this; } + inline Fixed operator+(const Fixed& o) { return Fixed(*this) += o; } + inline Fixed& operator-=(const Fixed& o) { _v.v -= o._v.v; return *this; } + inline Fixed operator-(const Fixed& o) { return Fixed(*this) -= o; } + + inline Fixed& operator+=(double o) { return *this += Fixed(o); } + inline Fixed operator+(double o) { return Fixed(*this) += o; } + inline Fixed& operator-=(double o) { return *this -= Fixed(o); } + inline Fixed operator-(double o) { return Fixed(*this) -= o; } + + inline Fixed& operator+=(float o) { return *this += Fixed(o); } + inline Fixed operator+(float o) { return Fixed(*this) += o; } + inline Fixed& operator-=(float o) { return *this -= Fixed(o); } + inline Fixed operator-(float o) { return Fixed(*this) -= o; } + + inline Fixed& operator+=(int o) { return *this += Fixed(o); } + inline Fixed operator+(int o) { return Fixed(*this) += o; } + inline Fixed& operator-=(int o) { return *this -= Fixed(o); } + inline Fixed operator-(int o) { return Fixed(*this) -= o; } +}; + +typedef Fixed<int32_t, 16> fixed_16_16; +typedef Fixed<int64_t, 32> fixed_32_32; + +} // namespace dsp +} // namespace bogaudio diff --git a/test/testmain.cpp b/test/testmain.cpp @@ -8,17 +8,52 @@ #include "util/math.hpp" // Rack +#include "dsp/fixed.hpp" + +using namespace bogaudio::dsp; + int main() { - // rando math or whatever, test it here! - - printf("%f\n", rack::eucmod(M_PI, 2.0f)); - printf("%f\n", rack::eucmod(-M_PI, 2.0f)); - printf("%f\n", rack::eucmod(M_PI, -2.0f)); - printf("%f\n", rack::eucmod(-M_PI, -2.0f)); - printf("%f\n", fmodf(M_PI, 2.0f)); - printf("%f\n", fmodf(-M_PI, 2.0f)); - printf("%f\n", fmodf(M_PI, -2.0f)); - printf("%f\n", fmodf(-M_PI, -2.0f)); - - return 0; + { + fixed_16_16 x = 5; + fixed_16_16 y = 1; + x = 3; + printf("X=%d\n", (int)(x + y)); + y = 2; + printf("X=%d\n", (int)(x - y)); + x = y + 5; + printf("X=%d\n", (int)x); + x = y - 3; + printf("X=%d\n", (int)x); + + x += 2.5; + printf("X=%d\n", (int)x); + printf("X=%f\n", (float)x); + + x = y - 0.3; + printf("X=%d\n", (int)x); + printf("X=%f\n", (float)x); + } + + { + fixed_32_32 x = 5; + fixed_32_32 y = 1; + x = 3; + printf("X=%d\n", (int)(x + y)); + y = 2; + printf("X=%d\n", (int)(x - y)); + x = y + 5; + printf("X=%d\n", (int)x); + x = y - 3; + printf("X=%d\n", (int)x); + + x += 2.5; + printf("X=%d\n", (int)x); + printf("X=%f\n", (float)x); + + x = y - 0.3; + printf("X=%d\n", (int)x); + printf("X=%f\n", (float)x); + } + + return 0; }