Browse Source

Fix `float approxExp2Floor()` to work with negative arguments and arguments above 30.

tags/v2.0.0
Andrew Belt 3 years ago
parent
commit
5faeb67a11
2 changed files with 16 additions and 2 deletions
  1. +13
    -0
      include/common.hpp
  2. +3
    -2
      include/dsp/approx.hpp

+ 13
- 0
include/common.hpp View File

@@ -164,6 +164,17 @@ namespace std {
namespace rack {


/** Casts a primitive, preserving its bits instead of converting. */
template <typename To, typename From>
To bitCast(From from) {
static_assert(sizeof(From) == sizeof(To), "Types must be the same size");
To to;
// This is optimized out
std::memcpy(&to, &from, sizeof(From));
return to;
}


/** C#-style property constructor
Example:

@@ -181,6 +192,7 @@ T* construct(F f, V v, Args... args) {
return o;
}


/** Defers code until the scope is destructed
From http://www.gingerbill.org/article/defer-in-cpp.html
Example:
@@ -244,6 +256,7 @@ typename C::mapped_type get(const C& m, const typename C::key_type& key, const t
return it->second;
}


// config

extern const std::string APP_NAME;


+ 3
- 2
include/dsp/approx.hpp View File

@@ -39,10 +39,11 @@ inline simd::float_4 approxExp2Floor(simd::float_4 x, simd::float_4* xf) {

template <>
inline float approxExp2Floor(float x, float* xf) {
int xi = x;
int32_t xi = x;
if (xf)
*xf = x - xi;
return 1 << xi;
int32_t y = (xi + 127) << 23;
return bitCast<float>(y);
}




Loading…
Cancel
Save