Browse Source

Use alloca() instead of VLAs for dynamic stack allocation.

tags/v1.0.0
Andrew Belt 5 years ago
parent
commit
bb34db46c9
1 changed files with 10 additions and 9 deletions
  1. +10
    -9
      include/dsp/ode.hpp

+ 10
- 9
include/dsp/ode.hpp View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include <dsp/common.hpp> #include <dsp/common.hpp>
#include <alloca.h>




namespace rack { namespace rack {
@@ -28,7 +29,7 @@ For example, the following solves the system x''(t) = -x(t) using a fixed timest
/** Solves an ODE system using the 1st order Euler method */ /** Solves an ODE system using the 1st order Euler method */
template <typename T, typename F> template <typename T, typename F>
void stepEuler(T t, T dt, T x[], int len, F f) { void stepEuler(T t, T dt, T x[], int len, F f) {
T k[len];
T *k = (T*) alloca(len);


f(t, x, k); f(t, x, k);
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
@@ -39,9 +40,9 @@ void stepEuler(T t, T dt, T x[], int len, F f) {
/** Solves an ODE system using the 2nd order Runge-Kutta method */ /** Solves an ODE system using the 2nd order Runge-Kutta method */
template <typename T, typename F> template <typename T, typename F>
void stepRK2(T t, T dt, T x[], int len, F f) { void stepRK2(T t, T dt, T x[], int len, F f) {
T k1[len];
T k2[len];
T yi[len];
T *k1 = (T*) alloca(len);
T *k2 = (T*) alloca(len);
T *yi = (T*) alloca(len);


f(t, x, k1); f(t, x, k1);


@@ -58,11 +59,11 @@ void stepRK2(T t, T dt, T x[], int len, F f) {
/** Solves an ODE system using the 4th order Runge-Kutta method */ /** Solves an ODE system using the 4th order Runge-Kutta method */
template <typename T, typename F> template <typename T, typename F>
void stepRK4(T t, T dt, T x[], int len, F f) { void stepRK4(T t, T dt, T x[], int len, F f) {
T k1[len];
T k2[len];
T k3[len];
T k4[len];
T yi[len];
T *k1 = (T*) alloca(len);
T *k2 = (T*) alloca(len);
T *k3 = (T*) alloca(len);
T *k4 = (T*) alloca(len);
T *yi = (T*) alloca(len);


f(t, x, k1); f(t, x, k1);




Loading…
Cancel
Save