add oversampler
This commit is contained in:
257
oversampling/HIIR/FPUDownsampler2x.h
Normal file
257
oversampling/HIIR/FPUDownsampler2x.h
Normal file
@@ -0,0 +1,257 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
FPUDownsampler2x.h
|
||||||
|
Copyright(c) 2005 Laurent de Soras
|
||||||
|
|
||||||
|
Downsamples the input signal by a factor 2, using FPU.
|
||||||
|
|
||||||
|
Template parameters:
|
||||||
|
- NC: number of coefficients, > 0
|
||||||
|
|
||||||
|
--- Legal stuff ---
|
||||||
|
|
||||||
|
This program is free software. It comes without any warranty, to
|
||||||
|
the extent permitted by applicable law. You can redistribute it
|
||||||
|
and/or modify it under the terms of the Do What The Fuck You Want
|
||||||
|
To Public License, Version 2, as published by Sam Hocevar. See
|
||||||
|
http://sam.zoy.org/wtfpl/COPYING for more details.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include "FPUStageProc.h"
|
||||||
|
|
||||||
|
namespace hiir
|
||||||
|
{
|
||||||
|
template <int NC, typename T>
|
||||||
|
class Downsampler2xFPU
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
enum { NBR_COEFS = NC };
|
||||||
|
Downsampler2xFPU();
|
||||||
|
|
||||||
|
/*
|
||||||
|
Name: set_coefs
|
||||||
|
Description:
|
||||||
|
Sets filter coefficients. Generate them with the PolyphaseIir2Designer
|
||||||
|
class.
|
||||||
|
Call this function before doing any processing.
|
||||||
|
Input parameters:
|
||||||
|
- coef_arr: Array of coefficients. There should be as many coefficients as
|
||||||
|
mentioned in the class template parameter.
|
||||||
|
*/
|
||||||
|
void set_coefs(const double coef_arr[]);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Name: process_sample
|
||||||
|
Description:
|
||||||
|
Downsamples (x2) one pair of samples, to generate one output sample.
|
||||||
|
Input parameters:
|
||||||
|
- in_ptr: pointer on the two samples to decimate
|
||||||
|
Returns: Samplerate-reduced sample.
|
||||||
|
*/
|
||||||
|
inline T process_sample(const T in_ptr [2]);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Name: process_block
|
||||||
|
Description:
|
||||||
|
Downsamples (x2) a block of samples.
|
||||||
|
Input and output blocks may overlap, see assert() for details.
|
||||||
|
Input parameters:
|
||||||
|
- in_ptr: Input array, containing nbr_spl * 2 samples.
|
||||||
|
- nbr_spl: Number of samples to output, > 0
|
||||||
|
Output parameters:
|
||||||
|
- out_ptr: Array for the output samples, capacity: nbr_spl samples.
|
||||||
|
*/
|
||||||
|
void process_block(T out_ptr[], const T in_ptr[], long nbr_spl);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Name: process_sample_split
|
||||||
|
Description:
|
||||||
|
Split (spectrum-wise) in half a pair of samples. The lower part of the
|
||||||
|
spectrum is a classic downsampling, equivalent to the output of
|
||||||
|
process_sample().
|
||||||
|
The higher part is the complementary signal: original filter response
|
||||||
|
is flipped from left to right, becoming a high-pass filter with the same
|
||||||
|
cutoff frequency. This signal is then critically sampled (decimation by 2),
|
||||||
|
flipping the spectrum: Fs/4...Fs/2 becomes Fs/4...0.
|
||||||
|
Input parameters:
|
||||||
|
- in_ptr: pointer on the pair of input samples
|
||||||
|
Output parameters:
|
||||||
|
- low: output sample, lower part of the spectrum (downsampling)
|
||||||
|
- high: output sample, higher part of the spectrum.
|
||||||
|
*/
|
||||||
|
inline void process_sample_split(T &low, T &high, const T in_ptr[2]);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Name: process_block_split
|
||||||
|
Description:
|
||||||
|
Split (spectrum-wise) in half a block of samples. The lower part of the
|
||||||
|
spectrum is a classic downsampling, equivalent to the output of
|
||||||
|
process_block().
|
||||||
|
The higher part is the complementary signal: original filter response
|
||||||
|
is flipped from left to right, becoming a high-pass filter with the same
|
||||||
|
cutoff frequency. This signal is then critically sampled (decimation by 2),
|
||||||
|
flipping the spectrum: Fs/4...Fs/2 becomes Fs/4...0.
|
||||||
|
Input and output blocks may overlap, see assert() for details.
|
||||||
|
Input parameters:
|
||||||
|
- in_ptr: Input array, containing nbr_spl * 2 samples.
|
||||||
|
- nbr_spl: Number of samples for each output, > 0
|
||||||
|
Output parameters:
|
||||||
|
- out_l_ptr: Array for the output samples, lower part of the spectrum
|
||||||
|
(downsampling). Capacity: nbr_spl samples.
|
||||||
|
- out_h_ptr: Array for the output samples, higher part of the spectrum.
|
||||||
|
Capacity: nbr_spl samples.
|
||||||
|
*/
|
||||||
|
void process_block_split(T out_l_ptr[], T out_h_ptr[], const T in_ptr[], long nbr_spl);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Name: clear_buffers
|
||||||
|
Description:
|
||||||
|
Clears filter memory, as if it processed silence since an infinite amount
|
||||||
|
of time.
|
||||||
|
*/
|
||||||
|
void clear_buffers();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::array<T, NBR_COEFS> _coef;
|
||||||
|
std::array<T, NBR_COEFS> _x;
|
||||||
|
std::array<T, NBR_COEFS> _y;
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool operator == (const Downsampler2xFPU &other);
|
||||||
|
bool operator != (const Downsampler2xFPU &other);
|
||||||
|
|
||||||
|
}; // class Downsampler2xFPU
|
||||||
|
|
||||||
|
|
||||||
|
template <int NC, typename T>
|
||||||
|
Downsampler2xFPU <NC, T>::Downsampler2xFPU ()
|
||||||
|
: _coef ()
|
||||||
|
, _x ()
|
||||||
|
, _y ()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < NBR_COEFS; ++i)
|
||||||
|
{
|
||||||
|
_coef [i] = 0;
|
||||||
|
}
|
||||||
|
clear_buffers ();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int NC, typename T>
|
||||||
|
void Downsampler2xFPU <NC, T>::set_coefs (const double coef_arr[])
|
||||||
|
{
|
||||||
|
assert (coef_arr != 0);
|
||||||
|
|
||||||
|
for (int i = 0; i < NBR_COEFS; ++i)
|
||||||
|
{
|
||||||
|
_coef [i] = static_cast <T> (coef_arr [i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int NC, typename T>
|
||||||
|
T Downsampler2xFPU <NC, T>::process_sample (const T in_ptr [2])
|
||||||
|
{
|
||||||
|
assert (in_ptr != 0);
|
||||||
|
|
||||||
|
T spl_0 (in_ptr [1]);
|
||||||
|
T spl_1 (in_ptr [0]);
|
||||||
|
|
||||||
|
#if defined (_MSC_VER)
|
||||||
|
#pragma inline_depth (255)
|
||||||
|
#endif // _MSC_VER
|
||||||
|
|
||||||
|
StageProcFPU <NBR_COEFS, T>::process_sample_pos (
|
||||||
|
NBR_COEFS,
|
||||||
|
spl_0,
|
||||||
|
spl_1,
|
||||||
|
&_coef [0],
|
||||||
|
&_x [0],
|
||||||
|
&_y [0]
|
||||||
|
);
|
||||||
|
|
||||||
|
return (0.5f * (spl_0 + spl_1));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template <int NC, typename T>
|
||||||
|
void Downsampler2xFPU <NC, T>::process_block (T out_ptr[], const T in_ptr[], long nbr_spl)
|
||||||
|
{
|
||||||
|
assert (in_ptr != 0);
|
||||||
|
assert (out_ptr != 0);
|
||||||
|
assert (out_ptr <= in_ptr || out_ptr >= in_ptr + nbr_spl * 2);
|
||||||
|
assert (nbr_spl > 0);
|
||||||
|
|
||||||
|
long pos = 0;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
out_ptr [pos] = process_sample (&in_ptr [pos * 2]);
|
||||||
|
++pos;
|
||||||
|
}
|
||||||
|
while (pos < nbr_spl);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int NC, typename T>
|
||||||
|
void Downsampler2xFPU <NC, T>::process_sample_split (T &low, T &high, const T in_ptr [2])
|
||||||
|
{
|
||||||
|
assert (&low != 0);
|
||||||
|
assert (&high != 0);
|
||||||
|
assert (in_ptr != 0);
|
||||||
|
|
||||||
|
T spl_0 = in_ptr [1];
|
||||||
|
T spl_1 = in_ptr [0];
|
||||||
|
|
||||||
|
#if defined (_MSC_VER)
|
||||||
|
#pragma inline_depth (255)
|
||||||
|
#endif // _MSC_VER
|
||||||
|
|
||||||
|
StageProcFPU <NBR_COEFS, T>::process_sample_pos (
|
||||||
|
NBR_COEFS,
|
||||||
|
spl_0,
|
||||||
|
spl_1,
|
||||||
|
&_coef [0],
|
||||||
|
&_x [0],
|
||||||
|
&_y [0]
|
||||||
|
);
|
||||||
|
|
||||||
|
low = (spl_0 + spl_1) * 0.5f;
|
||||||
|
high = spl_0 - low; // (spl_0 - spl_1) * 0.5f;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int NC, typename T>
|
||||||
|
void Downsampler2xFPU <NC, T>::process_block_split (T out_l_ptr[], T out_h_ptr[], const T in_ptr[], long nbr_spl)
|
||||||
|
{
|
||||||
|
assert (in_ptr != 0);
|
||||||
|
assert (out_l_ptr != 0);
|
||||||
|
assert (out_l_ptr <= in_ptr || out_l_ptr >= in_ptr + nbr_spl * 2);
|
||||||
|
assert (out_h_ptr != 0);
|
||||||
|
assert (out_h_ptr <= in_ptr || out_h_ptr >= in_ptr + nbr_spl * 2);
|
||||||
|
assert (out_h_ptr != out_l_ptr);
|
||||||
|
assert (nbr_spl > 0);
|
||||||
|
|
||||||
|
long pos = 0;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
process_sample_split
|
||||||
|
(
|
||||||
|
out_l_ptr [pos],
|
||||||
|
out_h_ptr [pos],
|
||||||
|
&in_ptr [pos * 2]
|
||||||
|
);
|
||||||
|
++pos;
|
||||||
|
}
|
||||||
|
while (pos < nbr_spl);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int NC, typename T>
|
||||||
|
void Downsampler2xFPU <NC, T>::clear_buffers ()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < NBR_COEFS; ++i)
|
||||||
|
{
|
||||||
|
_x [i] = 0;
|
||||||
|
_y [i] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace hiir
|
||||||
|
|
||||||
162
oversampling/HIIR/FPUStageProc.h
Normal file
162
oversampling/HIIR/FPUStageProc.h
Normal file
@@ -0,0 +1,162 @@
|
|||||||
|
/*
|
||||||
|
StageProcFPU.h
|
||||||
|
Copyright (c) 2005 Laurent de Soras
|
||||||
|
|
||||||
|
Template parameters:
|
||||||
|
- REMAINING: Number of remaining coefficients to process, >= 0
|
||||||
|
|
||||||
|
--- Legal stuff ---
|
||||||
|
|
||||||
|
This program is free software. It comes without any warranty, to
|
||||||
|
the extent permitted by applicable law. You can redistribute it
|
||||||
|
and/or modify it under the terms of the Do What The Fuck You Want
|
||||||
|
To Public License, Version 2, as published by Sam Hocevar. See
|
||||||
|
http://sam.zoy.org/wtfpl/COPYING for more details.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace hiir
|
||||||
|
{
|
||||||
|
|
||||||
|
template <int REMAINING, typename T>
|
||||||
|
class StageProcFPU
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static inline void process_sample_pos (const int nbr_coefs, T &spl_0, T &spl_1, const T coef [], T x [], T y []);
|
||||||
|
static inline void process_sample_neg (const int nbr_coefs, T &spl_0, T &spl_1, const T coef [], T x [], T y []);
|
||||||
|
|
||||||
|
private:
|
||||||
|
StageProcFPU();
|
||||||
|
StageProcFPU(const StageProcFPU &other);
|
||||||
|
StageProcFPU& operator = (const StageProcFPU &other);
|
||||||
|
bool operator == (const StageProcFPU &other);
|
||||||
|
bool operator != (const StageProcFPU &other);
|
||||||
|
|
||||||
|
}; // class StageProcFPU
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline void StageProcFPU <1, double>::process_sample_pos (const int nbr_coefs, double &spl_0, double &/*spl_1*/, const double coef [], double x [], double y [])
|
||||||
|
{
|
||||||
|
const int last = nbr_coefs - 1;
|
||||||
|
const double temp = (spl_0 - y [last]) * coef [last] + x [last];
|
||||||
|
x [last] = spl_0;
|
||||||
|
y [last] = temp;
|
||||||
|
spl_0 = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline void StageProcFPU <0, double>::process_sample_pos (const int /*nbr_coefs*/, double &/*spl_0*/, double &/*spl_1*/, const double /*coef*/ [], double /*x*/ [], double /*y*/ [])
|
||||||
|
{
|
||||||
|
// Nothing (stops recursion)
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline void StageProcFPU <1, float>::process_sample_pos (const int nbr_coefs, float &spl_0, float &/*spl_1*/, const float coef [], float x [], float y [])
|
||||||
|
{
|
||||||
|
const int last = nbr_coefs - 1;
|
||||||
|
const float temp = (spl_0 - y [last]) * coef [last] + x [last];
|
||||||
|
x [last] = spl_0;
|
||||||
|
y [last] = temp;
|
||||||
|
spl_0 = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline void StageProcFPU <0, float>::process_sample_pos (const int /*nbr_coefs*/, float &/*spl_0*/, float &/*spl_1*/, const float /*coef*/ [], float /*x*/ [], float /*y*/ [])
|
||||||
|
{
|
||||||
|
// Nothing (stops recursion)
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int REMAINING, typename T>
|
||||||
|
void StageProcFPU <REMAINING, T>::process_sample_pos (const int nbr_coefs, T &spl_0, T &spl_1, const T coef [], T x [], T y [])
|
||||||
|
{
|
||||||
|
const int cnt = nbr_coefs - REMAINING;
|
||||||
|
|
||||||
|
const T temp_0 =
|
||||||
|
(spl_0 - y [cnt + 0]) * coef [cnt + 0] + x [cnt + 0];
|
||||||
|
const T temp_1 =
|
||||||
|
(spl_1 - y [cnt + 1]) * coef [cnt + 1] + x [cnt + 1];
|
||||||
|
|
||||||
|
x [cnt + 0] = spl_0;
|
||||||
|
x [cnt + 1] = spl_1;
|
||||||
|
|
||||||
|
y [cnt + 0] = temp_0;
|
||||||
|
y [cnt + 1] = temp_1;
|
||||||
|
|
||||||
|
spl_0 = temp_0;
|
||||||
|
spl_1 = temp_1;
|
||||||
|
|
||||||
|
StageProcFPU <REMAINING - 2, T>::process_sample_pos (
|
||||||
|
nbr_coefs,
|
||||||
|
spl_0,
|
||||||
|
spl_1,
|
||||||
|
&coef [0],
|
||||||
|
&x [0],
|
||||||
|
&y [0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline void StageProcFPU <1, double>::process_sample_neg (const int nbr_coefs, double &spl_0, double &/*spl_1*/, const double coef [], double x [], double y [])
|
||||||
|
{
|
||||||
|
const int last = nbr_coefs - 1;
|
||||||
|
const double temp = (spl_0 + y [last]) * coef [last] - x [last];
|
||||||
|
x [last] = spl_0;
|
||||||
|
y [last] = temp;
|
||||||
|
spl_0 = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline void StageProcFPU <0, double>::process_sample_neg (const int /*nbr_coefs*/, double &/*spl_0*/, double &/*spl_1*/, const double /*coef*/ [], double /*x*/ [], double /*y*/ [])
|
||||||
|
{
|
||||||
|
// Nothing (stops recursion)
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline void StageProcFPU <1, float>::process_sample_neg (const int nbr_coefs, float &spl_0, float &/*spl_1*/, const float coef [], float x [], float y [])
|
||||||
|
{
|
||||||
|
const int last = nbr_coefs - 1;
|
||||||
|
const float temp = (spl_0 + y [last]) * coef [last] - x [last];
|
||||||
|
x [last] = spl_0;
|
||||||
|
y [last] = temp;
|
||||||
|
spl_0 = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline void StageProcFPU <0, float>::process_sample_neg (const int /*nbr_coefs*/, float &/*spl_0*/, float &/*spl_1*/, const float /*coef*/ [], float /*x*/ [], float /*y*/ [])
|
||||||
|
{
|
||||||
|
// Nothing (stops recursion)
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int REMAINING, typename T>
|
||||||
|
void StageProcFPU <REMAINING, T>::process_sample_neg (const int nbr_coefs, T &spl_0, T &spl_1, const T coef [], T x [], T y [])
|
||||||
|
{
|
||||||
|
const int cnt = nbr_coefs - REMAINING;
|
||||||
|
|
||||||
|
const T temp_0 =
|
||||||
|
(spl_0 + y [cnt + 0]) * coef [cnt + 0] - x [cnt + 0];
|
||||||
|
const T temp_1 =
|
||||||
|
(spl_1 + y [cnt + 1]) * coef [cnt + 1] - x [cnt + 1];
|
||||||
|
|
||||||
|
x [cnt + 0] = spl_0;
|
||||||
|
x [cnt + 1] = spl_1;
|
||||||
|
|
||||||
|
y [cnt + 0] = temp_0;
|
||||||
|
y [cnt + 1] = temp_1;
|
||||||
|
|
||||||
|
spl_0 = temp_0;
|
||||||
|
spl_1 = temp_1;
|
||||||
|
|
||||||
|
StageProcFPU <REMAINING - 2, T>::process_sample_neg (
|
||||||
|
nbr_coefs,
|
||||||
|
spl_0,
|
||||||
|
spl_1,
|
||||||
|
&coef [0],
|
||||||
|
&x [0],
|
||||||
|
&y [0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace hiir
|
||||||
168
oversampling/HIIR/FPUUpsampler2x.h
Executable file
168
oversampling/HIIR/FPUUpsampler2x.h
Executable file
@@ -0,0 +1,168 @@
|
|||||||
|
/*
|
||||||
|
FPUUpsampler2x.h
|
||||||
|
Copyright (c) 2005 Laurent de Soras
|
||||||
|
|
||||||
|
Upsamples by a factor 2 the input signal, using FPU.
|
||||||
|
|
||||||
|
Template parameters:
|
||||||
|
- NC: number of coefficients, > 0
|
||||||
|
|
||||||
|
--- Legal stuff ---
|
||||||
|
|
||||||
|
This program is free software. It comes without any warranty, to
|
||||||
|
the extent permitted by applicable law. You can redistribute it
|
||||||
|
and/or modify it under the terms of the Do What The Fuck You Want
|
||||||
|
To Public License, Version 2, as published by Sam Hocevar. See
|
||||||
|
http://sam.zoy.org/wtfpl/COPYING for more details.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include "FPUStageProc.h"
|
||||||
|
|
||||||
|
namespace hiir
|
||||||
|
{
|
||||||
|
|
||||||
|
template <int NC, typename T>
|
||||||
|
class Upsampler2xFPU
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
enum { NBR_COEFS = NC };
|
||||||
|
|
||||||
|
Upsampler2xFPU ();
|
||||||
|
|
||||||
|
/*
|
||||||
|
Name: set_coefs
|
||||||
|
Description:
|
||||||
|
Sets filter coefficients. Generate them with the PolyphaseIir2Designer
|
||||||
|
class.
|
||||||
|
Call this function before doing any processing.
|
||||||
|
Input parameters:
|
||||||
|
- coef_arr: Array of coefficients. There should be as many coefficients as
|
||||||
|
mentioned in the class template parameter.
|
||||||
|
*/
|
||||||
|
void set_coefs (const double coef_arr [NBR_COEFS]);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Name: process_sample
|
||||||
|
Description:
|
||||||
|
Upsamples (x2) the input sample, generating two output samples.
|
||||||
|
Input parameters:
|
||||||
|
- input: The input sample.
|
||||||
|
Output parameters:
|
||||||
|
- out_0: First output sample.
|
||||||
|
- out_1: Second output sample.
|
||||||
|
*/
|
||||||
|
inline void process_sample (T &out_0, T &out_1, T input);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Name: process_block
|
||||||
|
Description:
|
||||||
|
Upsamples (x2) the input sample block.
|
||||||
|
Input and output blocks may overlap, see assert() for details.
|
||||||
|
Input parameters:
|
||||||
|
- in_ptr: Input array, containing nbr_spl samples.
|
||||||
|
- nbr_spl: Number of input samples to process, > 0
|
||||||
|
Output parameters:
|
||||||
|
- out_0_ptr: Output sample array, capacity: nbr_spl * 2 samples.
|
||||||
|
*/
|
||||||
|
void process_block (T out_ptr [], const T in_ptr [], long nbr_spl);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Name: clear_buffers
|
||||||
|
Description:
|
||||||
|
Clears filter memory, as if it processed silence since an infinite amount
|
||||||
|
of time.
|
||||||
|
*/
|
||||||
|
void clear_buffers ();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::array<T, NBR_COEFS> _coef;
|
||||||
|
std::array<T, NBR_COEFS> _x;
|
||||||
|
std::array<T, NBR_COEFS> _y;
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool operator == (const Upsampler2xFPU &other);
|
||||||
|
bool operator != (const Upsampler2xFPU &other);
|
||||||
|
|
||||||
|
}; // class Upsampler2xFPU
|
||||||
|
|
||||||
|
template <int NC, typename T>
|
||||||
|
Upsampler2xFPU <NC, T>::Upsampler2xFPU ()
|
||||||
|
: _coef ()
|
||||||
|
, _x ()
|
||||||
|
, _y ()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < NBR_COEFS; ++i)
|
||||||
|
{
|
||||||
|
_coef [i] = 0;
|
||||||
|
}
|
||||||
|
clear_buffers ();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int NC, typename T>
|
||||||
|
void Upsampler2xFPU <NC, T>::set_coefs (const double coef_arr [NBR_COEFS])
|
||||||
|
{
|
||||||
|
assert (coef_arr != 0);
|
||||||
|
|
||||||
|
for (int i = 0; i < NBR_COEFS; ++i)
|
||||||
|
{
|
||||||
|
_coef [i] = static_cast <T> (coef_arr [i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int NC, typename T>
|
||||||
|
void Upsampler2xFPU <NC, T>::process_sample (T &out_0, T &out_1, T input)
|
||||||
|
{
|
||||||
|
// assert (&out_0 != 0);
|
||||||
|
// assert (&out_1 != 0);
|
||||||
|
|
||||||
|
T even = input;
|
||||||
|
T odd = input;
|
||||||
|
StageProcFPU <NBR_COEFS, T>::process_sample_pos (
|
||||||
|
NBR_COEFS,
|
||||||
|
even,
|
||||||
|
odd,
|
||||||
|
&_coef [0],
|
||||||
|
&_x [0],
|
||||||
|
&_y [0]
|
||||||
|
);
|
||||||
|
out_0 = even;
|
||||||
|
out_1 = odd;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int NC, typename T>
|
||||||
|
void Upsampler2xFPU <NC, T>::process_block (T out_ptr [], const T in_ptr [], long nbr_spl)
|
||||||
|
{
|
||||||
|
// assert (out_ptr != 0);
|
||||||
|
// assert (in_ptr != 0);
|
||||||
|
// assert (out_ptr >= in_ptr + nbr_spl || in_ptr >= out_ptr + nbr_spl);
|
||||||
|
// assert (nbr_spl > 0);
|
||||||
|
|
||||||
|
long pos = 0;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
process_sample (
|
||||||
|
out_ptr [pos * 2],
|
||||||
|
out_ptr [pos * 2 + 1],
|
||||||
|
in_ptr [pos]
|
||||||
|
);
|
||||||
|
++ pos;
|
||||||
|
}
|
||||||
|
while (pos < nbr_spl);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int NC, typename T>
|
||||||
|
void Upsampler2xFPU <NC, T>::clear_buffers ()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < NBR_COEFS; ++i)
|
||||||
|
{
|
||||||
|
_x [i] = 0;
|
||||||
|
_y [i] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace hiir
|
||||||
267
oversampling/HIIR/PolyphaseIIR2Designer.cpp
Normal file
267
oversampling/HIIR/PolyphaseIIR2Designer.cpp
Normal file
@@ -0,0 +1,267 @@
|
|||||||
|
/*
|
||||||
|
PolyphaseIIR2Designer.cpp
|
||||||
|
Copyright (c) 2005 Laurent de Soras
|
||||||
|
|
||||||
|
--- Legal stuff ---
|
||||||
|
|
||||||
|
This program is free software. It comes without any warranty, to
|
||||||
|
the extent permitted by applicable law. You can redistribute it
|
||||||
|
and/or modify it under the terms of the Do What The Fuck You Want
|
||||||
|
To Public License, Version 2, as published by Sam Hocevar. See
|
||||||
|
http://sam.zoy.org/wtfpl/COPYING for more details.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#if defined (_MSC_VER)
|
||||||
|
#pragma warning (1 : 4130) // "'operator' : logical operation on address of string constant"
|
||||||
|
#pragma warning (1 : 4223) // "nonstandard extension used : non-lvalue array converted to pointer"
|
||||||
|
#pragma warning (1 : 4705) // "statement has no effect"
|
||||||
|
#pragma warning (1 : 4706) // "assignment within conditional expression"
|
||||||
|
#pragma warning (4 : 4786) // "identifier was truncated to '255' characters in the debug information"
|
||||||
|
#pragma warning (4 : 4800) // "forcing value to bool 'true' or 'false' (performance warning)"
|
||||||
|
#pragma warning (4 : 4355) // "'this' : used in base member initializer list"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "PolyphaseIIR2Designer.h"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
namespace hiir
|
||||||
|
{
|
||||||
|
static const double PI = 3.141592653589793238;
|
||||||
|
|
||||||
|
int round_int (double x)
|
||||||
|
{
|
||||||
|
return (static_cast <int> (std::floor (x + 0.5)));
|
||||||
|
}
|
||||||
|
|
||||||
|
int ceil_int (double x)
|
||||||
|
{
|
||||||
|
return (static_cast <int> (std::ceil (x)));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
T ipowp (T x, long n)
|
||||||
|
{
|
||||||
|
assert (n >= 0);
|
||||||
|
|
||||||
|
T z (1);
|
||||||
|
while (n != 0)
|
||||||
|
{
|
||||||
|
if ((n & 1) != 0)
|
||||||
|
{
|
||||||
|
z *= x;
|
||||||
|
}
|
||||||
|
n >>= 1;
|
||||||
|
x *= x;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (z);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int PolyphaseIIR2Designer::compute_nbr_coefs_from_proto (double attenuation, double transition)
|
||||||
|
{
|
||||||
|
assert (attenuation > 0);
|
||||||
|
assert (transition > 0);
|
||||||
|
assert (transition < 0.5);
|
||||||
|
|
||||||
|
double k;
|
||||||
|
double q;
|
||||||
|
compute_transition_param (k, q, transition);
|
||||||
|
const int order = compute_order (attenuation, q);
|
||||||
|
const int nbr_coefs = (order - 1) / 2;
|
||||||
|
|
||||||
|
return (nbr_coefs);
|
||||||
|
}
|
||||||
|
|
||||||
|
double PolyphaseIIR2Designer::compute_atten_from_order_tbw (int nbr_coefs, double transition)
|
||||||
|
{
|
||||||
|
assert (nbr_coefs > 0);
|
||||||
|
assert (transition > 0);
|
||||||
|
assert (transition < 0.5);
|
||||||
|
|
||||||
|
double k;
|
||||||
|
double q;
|
||||||
|
compute_transition_param (k, q, transition);
|
||||||
|
const int order = nbr_coefs * 2 + 1;
|
||||||
|
const double attenuation = compute_atten (q, order);
|
||||||
|
|
||||||
|
return (attenuation);
|
||||||
|
}
|
||||||
|
|
||||||
|
int PolyphaseIIR2Designer::compute_coefs (double coef_arr[], double attenuation, double transition)
|
||||||
|
{
|
||||||
|
assert (&coef_arr != 0);
|
||||||
|
assert (attenuation > 0);
|
||||||
|
assert (transition > 0);
|
||||||
|
assert (transition < 0.5);
|
||||||
|
|
||||||
|
double k;
|
||||||
|
double q;
|
||||||
|
compute_transition_param (k, q, transition);
|
||||||
|
|
||||||
|
// Computes number of required coefficients
|
||||||
|
const int order = compute_order (attenuation, q);
|
||||||
|
const int nbr_coefs = (order - 1) / 2;
|
||||||
|
|
||||||
|
// Coefficient calculation
|
||||||
|
for (int index = 0; index < nbr_coefs; ++index)
|
||||||
|
{
|
||||||
|
coef_arr [index] = compute_coef (index, k, q, order);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (nbr_coefs);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PolyphaseIIR2Designer::compute_coefs_spec_order_tbw (double coef_arr[], int nbr_coefs, double transition)
|
||||||
|
{
|
||||||
|
assert (&coef_arr != 0);
|
||||||
|
assert (nbr_coefs > 0);
|
||||||
|
assert (transition > 0);
|
||||||
|
assert (transition < 0.5);
|
||||||
|
|
||||||
|
double k;
|
||||||
|
double q;
|
||||||
|
compute_transition_param (k, q, transition);
|
||||||
|
const int order = nbr_coefs * 2 + 1;
|
||||||
|
|
||||||
|
// Coefficient calculation
|
||||||
|
for (int index = 0; index < nbr_coefs; ++index)
|
||||||
|
{
|
||||||
|
coef_arr [index] = compute_coef (index, k, q, order);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PolyphaseIIR2Designer::compute_transition_param (double &k, double &q, double transition)
|
||||||
|
{
|
||||||
|
assert (&k != 0);
|
||||||
|
assert (&q != 0);
|
||||||
|
assert (transition > 0);
|
||||||
|
assert (transition < 0.5);
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
k = tan ((1 - transition * 2) * hiir::PI / 4);
|
||||||
|
k *= k;
|
||||||
|
assert (k < 1);
|
||||||
|
assert (k > 0);
|
||||||
|
double kksqrt = pow (1 - k * k, 0.25);
|
||||||
|
const double e = 0.5 * (1 - kksqrt) / (1 + kksqrt);
|
||||||
|
const double e2 = e * e;
|
||||||
|
const double e4 = e2 * e2;
|
||||||
|
q = e * (1 + e4 * (2 + e4 * (15 + 150 * e4)));
|
||||||
|
assert (q > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int PolyphaseIIR2Designer::compute_order (double attenuation, double q)
|
||||||
|
{
|
||||||
|
assert (attenuation > 0);
|
||||||
|
assert (q > 0);
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
const double attn_p2 = pow (10.0, -attenuation / 10);
|
||||||
|
const double a = attn_p2 / (1 - attn_p2);
|
||||||
|
int order = hiir::ceil_int (log (a * a / 16) / log (q));
|
||||||
|
if ((order & 1) == 0)
|
||||||
|
{
|
||||||
|
++ order;
|
||||||
|
}
|
||||||
|
if (order == 1)
|
||||||
|
{
|
||||||
|
order = 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (order);
|
||||||
|
}
|
||||||
|
|
||||||
|
double PolyphaseIIR2Designer::compute_atten (double q, int order)
|
||||||
|
{
|
||||||
|
assert (q > 0);
|
||||||
|
assert (order > 0);
|
||||||
|
assert ((order & 1) == 1);
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
const double a = 4 * exp (order * 0.5 * log (q));
|
||||||
|
assert (a != -1.0);
|
||||||
|
const double attn_p2 = a / (1 + a);
|
||||||
|
const double attenuation = -10 * log10 (attn_p2);
|
||||||
|
assert (attenuation > 0);
|
||||||
|
|
||||||
|
return (attenuation);
|
||||||
|
}
|
||||||
|
|
||||||
|
double PolyphaseIIR2Designer::compute_coef (int index, double k, double q, int order)
|
||||||
|
{
|
||||||
|
assert (index >= 0);
|
||||||
|
assert (index * 2 < order);
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
const int c = index + 1;
|
||||||
|
const double num = compute_acc_num (q, order, c) * pow (q, 0.25);
|
||||||
|
const double den = compute_acc_den (q, order, c) + 0.5;
|
||||||
|
const double ww = num / den;
|
||||||
|
const double wwsq = ww * ww;
|
||||||
|
|
||||||
|
const double x = sqrt ((1 - wwsq * k) * (1 - wwsq / k)) / (1 + wwsq);
|
||||||
|
const double coef = (1 - x) / (1 + x);
|
||||||
|
|
||||||
|
return (coef);
|
||||||
|
}
|
||||||
|
|
||||||
|
double PolyphaseIIR2Designer::compute_acc_num (double q, int order, int c)
|
||||||
|
{
|
||||||
|
assert (c >= 1);
|
||||||
|
assert (c < order * 2);
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
int j = 1;
|
||||||
|
double acc = 0;
|
||||||
|
double q_ii1;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
q_ii1 = hiir::ipowp (q, i * (i + 1));
|
||||||
|
q_ii1 *= sin ((i * 2 + 1) * c * hiir::PI / order) * j;
|
||||||
|
acc += q_ii1;
|
||||||
|
|
||||||
|
j = -j;
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
while (fabs (q_ii1) > 1e-100);
|
||||||
|
|
||||||
|
return (acc);
|
||||||
|
}
|
||||||
|
|
||||||
|
double PolyphaseIIR2Designer::compute_acc_den (double q, int order, int c)
|
||||||
|
{
|
||||||
|
assert (c >= 1);
|
||||||
|
assert (c < order * 2);
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int i = 1;
|
||||||
|
int j = -1;
|
||||||
|
double acc = 0;
|
||||||
|
double q_i2;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
q_i2 = hiir::ipowp (q, i * i);
|
||||||
|
q_i2 *= cos (i * 2 * c * hiir::PI / order) * j;
|
||||||
|
acc += q_i2;
|
||||||
|
|
||||||
|
j = -j;
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
while (fabs (q_i2) > 1e-100);
|
||||||
|
|
||||||
|
return (acc);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace hiir
|
||||||
|
|
||||||
141
oversampling/HIIR/PolyphaseIIR2Designer.h
Normal file
141
oversampling/HIIR/PolyphaseIIR2Designer.h
Normal file
@@ -0,0 +1,141 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
PolyphaseIIR2Designer.h
|
||||||
|
Copyright (c) 2005 Laurent de Soras
|
||||||
|
|
||||||
|
Compute coefficients for 2-path polyphase IIR filter, half-band filter or
|
||||||
|
Pi/2 phaser.
|
||||||
|
|
||||||
|
-2
|
||||||
|
a + z
|
||||||
|
N/2-1 2k+1
|
||||||
|
A0 (z) = Prod ------------
|
||||||
|
k = 0 -2
|
||||||
|
1 + a z
|
||||||
|
2k+1
|
||||||
|
|
||||||
|
-2
|
||||||
|
a + z
|
||||||
|
-1 (N-1)/2 2k
|
||||||
|
A1 (z) = z . Prod ----------
|
||||||
|
k = 0 -2
|
||||||
|
1 + a z
|
||||||
|
2k
|
||||||
|
|
||||||
|
1
|
||||||
|
H (z) = - (A0 (z) + A1 (z))
|
||||||
|
2
|
||||||
|
|
||||||
|
Sum of A0 and A1 gives a low-pass filter.
|
||||||
|
Difference of A0 and A1 gives the complementary high-pass filter.
|
||||||
|
|
||||||
|
For the Pi/2 phaser, product form is (a - z^-2) / (1 - az^-2)
|
||||||
|
Sum and difference of A0 and A1 have a Pi/2 phase difference.
|
||||||
|
|
||||||
|
References:
|
||||||
|
|
||||||
|
* Polyphase Two-Path Filter Designer in Java
|
||||||
|
Artur Krukowski
|
||||||
|
http://www.cmsa.wmin.ac.uk/~artur/Poly.html
|
||||||
|
|
||||||
|
* Digital Signal Processing Schemes for Efficient Interpolation and Decimation
|
||||||
|
Valenzuela and Constantinides
|
||||||
|
IEE Proceedings, Dec 1983
|
||||||
|
|
||||||
|
* A Hilbert-Transformer Frequency Shifter for Audio
|
||||||
|
Scott Wardle
|
||||||
|
http://www.iua.upf.es/dafx98/papers/WAR19.PS
|
||||||
|
|
||||||
|
--- Legal stuff ---
|
||||||
|
|
||||||
|
This program is free software. It comes without any warranty, to
|
||||||
|
the extent permitted by applicable law. You can redistribute it
|
||||||
|
and/or modify it under the terms of the Do What The Fuck You Want
|
||||||
|
To Public License, Version 2, as published by Sam Hocevar. See
|
||||||
|
http://sam.zoy.org/wtfpl/COPYING for more details.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace hiir
|
||||||
|
{
|
||||||
|
|
||||||
|
class PolyphaseIIR2Designer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
/*
|
||||||
|
Name: compute_nbr_coefs_from_proto
|
||||||
|
Description:
|
||||||
|
Finds the minimum number of coefficients for a given filter specification
|
||||||
|
Input parameters:
|
||||||
|
- attenuation: stop-band attenuation, dB. > 0.
|
||||||
|
- transition: normalized transition bandwidth. Range ]0 ; 1/2[
|
||||||
|
Returns: Number of coefficients, > 0
|
||||||
|
*/
|
||||||
|
static int compute_nbr_coefs_from_proto (double attenuation, double transition);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Name: compute_atten_from_order_tbw
|
||||||
|
Description:
|
||||||
|
Compute the attenuation corresponding to a given number of coefficients
|
||||||
|
and the transition bandwidth.
|
||||||
|
Input parameters:
|
||||||
|
- nbr_coefs: Number of desired coefficients. > 0.
|
||||||
|
- transition: normalized transition bandwidth. Range ]0 ; 1/2[
|
||||||
|
Returns: stop-band attenuation, dB. > 0.
|
||||||
|
*/
|
||||||
|
static double compute_atten_from_order_tbw (int nbr_coefs, double transition);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Name: compute_coefs
|
||||||
|
Description:
|
||||||
|
Computes coefficients for a half-band polyphase IIR filter, function of a
|
||||||
|
given stop-band gain / transition bandwidth specification.
|
||||||
|
Order is automatically calculated.
|
||||||
|
Input parameters:
|
||||||
|
- attenuation: stop-band attenuation, dB. > 0.
|
||||||
|
- transition: normalized transition bandwidth. Range ]0 ; 1/2[
|
||||||
|
Output parameters:
|
||||||
|
- coef_arr: Coefficient list, must be large enough to store all the
|
||||||
|
coefficients. Filter order = nbr_coefs * 2 + 1
|
||||||
|
Returns: number of coefficients
|
||||||
|
*/
|
||||||
|
static int compute_coefs (double coef_arr[], double attenuation, double transition);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Name: compute_coefs_spec_order_tbw
|
||||||
|
Description:
|
||||||
|
Computes coefficients for a half-band polyphase IIR filter, function of a
|
||||||
|
given transition bandwidth and desired filter order. Bandstop attenuation
|
||||||
|
is set to the maximum value for these constraints.
|
||||||
|
Input parameters:
|
||||||
|
- nbr_coefs: Number of desired coefficients. > 0.
|
||||||
|
- transition: normalized transition bandwidth. Range ]0 ; 1/2[
|
||||||
|
Output parameters:
|
||||||
|
- coef_arr: Coefficient list, must be large enough to store all the
|
||||||
|
coefficients.
|
||||||
|
*/
|
||||||
|
static void compute_coefs_spec_order_tbw (double coef_arr[], int nbr_coefs, double transition);
|
||||||
|
|
||||||
|
private:
|
||||||
|
static void compute_transition_param (double &k, double &q, double transition);
|
||||||
|
static int compute_order (double attenuation, double q);
|
||||||
|
static double compute_atten (double q, int order);
|
||||||
|
static double compute_coef (int index, double k, double q, int order);
|
||||||
|
static double compute_acc_num (double q, int order, int c);
|
||||||
|
static double compute_acc_den (double q, int order, int c);
|
||||||
|
|
||||||
|
private:
|
||||||
|
PolyphaseIIR2Designer();
|
||||||
|
~PolyphaseIIR2Designer();
|
||||||
|
PolyphaseIIR2Designer(const PolyphaseIIR2Designer &other);
|
||||||
|
PolyphaseIIR2Designer&
|
||||||
|
operator = (const PolyphaseIIR2Designer &other);
|
||||||
|
bool operator == (const PolyphaseIIR2Designer &other);
|
||||||
|
bool operator != (const PolyphaseIIR2Designer &other);
|
||||||
|
|
||||||
|
}; // class PolyphaseIIR2Designer
|
||||||
|
|
||||||
|
} // namespace hiir
|
||||||
13
oversampling/HIIR/license.txt
Executable file
13
oversampling/HIIR/license.txt
Executable file
@@ -0,0 +1,13 @@
|
|||||||
|
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
||||||
|
Version 2, December 2004
|
||||||
|
|
||||||
|
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
|
||||||
|
|
||||||
|
Everyone is permitted to copy and distribute verbatim or modified
|
||||||
|
copies of this license document, and changing it is allowed as long
|
||||||
|
as the name is changed.
|
||||||
|
|
||||||
|
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
||||||
|
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||||
|
|
||||||
|
0. You just DO WHAT THE FUCK YOU WANT TO.
|
||||||
248
oversampling/HIIR/readme.txt
Executable file
248
oversampling/HIIR/readme.txt
Executable file
@@ -0,0 +1,248 @@
|
|||||||
|
==============================================================================
|
||||||
|
|
||||||
|
hiir
|
||||||
|
Version 1.11
|
||||||
|
|
||||||
|
An oversampling and Hilbert transform library in C++
|
||||||
|
|
||||||
|
By Laurent de Soras, 2005-2013
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Contents:
|
||||||
|
|
||||||
|
1. Legal
|
||||||
|
2. What is hiir ?
|
||||||
|
3. Using hiir
|
||||||
|
4. Compilation
|
||||||
|
5. Oversampling to higher ratios
|
||||||
|
6. History
|
||||||
|
7. Contact
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
1. Legal
|
||||||
|
--------
|
||||||
|
|
||||||
|
Check the file license.txt to get full information about the license.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
2. What is hiir ?
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
hiir is a DSP (digital signal processing) library in C++, with two purposes:
|
||||||
|
|
||||||
|
- Changing the sampling rate of a signal by a factor two, in both
|
||||||
|
directions (upsampling and downsampling).
|
||||||
|
- Obtaining two signals with a pi/2 phase difference (Hilbert transform)
|
||||||
|
|
||||||
|
These distinct operations are actually sharing the same filter design method
|
||||||
|
and processing kernel, that is why they are included in the same package. The
|
||||||
|
filter (a two-path polyphase IIR) is very efficient and can be used to achieve
|
||||||
|
high-quality oversampling or phase shift at a low CPU cost. It is made of a
|
||||||
|
symetric half-band elliptic low-pass filter, hence having an extremely flat
|
||||||
|
frequency response in the passband.
|
||||||
|
|
||||||
|
Various implementations are supplied with the library, using special CPU
|
||||||
|
instructions to optimise the calculation speed. Currently SSE and 3DNow!
|
||||||
|
instruction sets are supported, as well as the classic and portable FPU
|
||||||
|
implementation.
|
||||||
|
|
||||||
|
Source code may be downloaded from this webpage:
|
||||||
|
http://ldesoras.free.fr/prod.html
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
3. Using hiir
|
||||||
|
-------------
|
||||||
|
|
||||||
|
To avoid any collision, names have been encapsulated in the namespace "hiir".
|
||||||
|
So you have to prefix every name of this library with hiir:: or put a line
|
||||||
|
"using namespace hiir;" into your code.
|
||||||
|
|
||||||
|
The filter design class is PolyphaseIir2Designer. It generates coefficients
|
||||||
|
for the filters from a specification: stopband attenuation, transition
|
||||||
|
bandwidth and/or number of coefficients.
|
||||||
|
|
||||||
|
The main processing classes are Downsampler2x*, Upsampler2x* and PhaseHalfPi*.
|
||||||
|
The suffix indicates the implementation. Choose "Fpu" if you are not sure
|
||||||
|
about the right one to use. All implementations of a class category have the
|
||||||
|
same function syntax, so you can use them with C++ templates.
|
||||||
|
|
||||||
|
The implementations should have a consistent behaviour, based on the FPU one.
|
||||||
|
Some of them have specific requirement, like object alignment in memory or
|
||||||
|
delay in processing. See the header file (.h) of the class for details about
|
||||||
|
the constraints and inconsistencies, and the code file (.cpp/.hpp) for details
|
||||||
|
about function calls.
|
||||||
|
|
||||||
|
As you can see, almost all classes are templates based on number of
|
||||||
|
coefficients. This means it is not possible to change this number at run-time.
|
||||||
|
This is the most important constraint of this library. However the reward is
|
||||||
|
the high speed of the execution. Anyway, you could build a wrapper to support
|
||||||
|
variable number of coefficients, althought it means that you will have
|
||||||
|
probably to compile a large number of variations on the same code.
|
||||||
|
|
||||||
|
The library processes only 32-bit floating point data.
|
||||||
|
|
||||||
|
hiir is intended to be portable, but has little architecture-dependant pieces
|
||||||
|
of code. So far, it has been built and tested on:
|
||||||
|
|
||||||
|
- MS Windows / MS Visual C++ 6.0 (FPU/SSE/3DNow)
|
||||||
|
- MS Windows / MS Visual C++ 2005 (FPU/SSE/3DNow)
|
||||||
|
- MS Windows / MS Visual C++ 2010 (FPU/SSE/3DNow)
|
||||||
|
- MS Windows / GCC 4.5.3 (FPU/SSE only)
|
||||||
|
- MS Windows / Clang 3.1 (FPU/SSE only)
|
||||||
|
- MacOS 10.5 / GCC 4 (FPU/SSE only)
|
||||||
|
|
||||||
|
If you happen to have another system and tweak it to make it run successfully,
|
||||||
|
pleeeeease send me your modification so I can include it to the main
|
||||||
|
distribution. Run main.cpp in Debug mode before, then in Release mode, in
|
||||||
|
order to be sure that everything is fine. I would also be glad to include
|
||||||
|
implementations for other processors/compilers.
|
||||||
|
|
||||||
|
References for filter use and design:
|
||||||
|
|
||||||
|
- Scott Wardle, "A Hilbert-Transformer Frequency Shifter for Audio"
|
||||||
|
http://www.iua.upf.es/dafx98/papers/
|
||||||
|
|
||||||
|
- Valenzuela and Constantinides, "Digital Signal Processing Schemes for
|
||||||
|
Efficient Interpolation and Decimation", IEEE Proceedings, Dec 1983
|
||||||
|
|
||||||
|
- Artur Krukowski, Izzet Kale, "The design of arbitrary-band multi-path
|
||||||
|
polyphase IIR filters", ISCAS 2, page 741-744. IEEE, 2001
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
4. Compilation and testing
|
||||||
|
--------------------------
|
||||||
|
|
||||||
|
Drop the following files into your project or makefile :
|
||||||
|
|
||||||
|
hiir/Array.*
|
||||||
|
hiir/def.h
|
||||||
|
hiir/Downsampler2x*.*
|
||||||
|
hiir/fnc.*
|
||||||
|
hiir/PhaseHalfPi*.*
|
||||||
|
hiir/PolyphaseIir2Designer.*
|
||||||
|
hiir/Stage*.*
|
||||||
|
hiir/Upsampler2x*.*
|
||||||
|
|
||||||
|
Other files (in the hiir/test directory) are for testing purpose only, do not
|
||||||
|
include them if you just need to use the library; they are not needed to use
|
||||||
|
hiir in your own programs.
|
||||||
|
|
||||||
|
hiir may be compiled in two versions: release and debug. Debug version
|
||||||
|
has checks that could slow down the code. Define NDEBUG to set the Release
|
||||||
|
mode. For example, the command line to compile the test bench on GCC or
|
||||||
|
Clang would look like:
|
||||||
|
|
||||||
|
Debug mode:
|
||||||
|
g++ -msse -I. -o ./hiir_debug.exe hiir/*.cpp hiir/test/*.cpp
|
||||||
|
clang++ -D_X86_ -msse -I. -o ./hiir_debug.exe hiir/*.cpp hiir/test/*.cpp
|
||||||
|
|
||||||
|
Release mode:
|
||||||
|
g++ -msse -I. -o ./hiir_release.exe -DNDEBUG -O3 hiir/*.cpp hiir/test/*.cpp
|
||||||
|
clang++ -D_X86_ -msse -I. -o ./hiir_release.exe -DNDEBUG -O3 hiir/*.cpp hiir/test/*.cpp
|
||||||
|
|
||||||
|
The "-msse" option enables the compilation of the SSE intrinsics.
|
||||||
|
|
||||||
|
Notes for MS VC++ 6.0 users:
|
||||||
|
|
||||||
|
- You'll need the Processor Pack in order to be able to compile 3DNow! and
|
||||||
|
SSE code.
|
||||||
|
|
||||||
|
- The intensive use of recursive templates may slow down a bit the compilation,
|
||||||
|
especially if you use many different filter sizes (number of coefficients).
|
||||||
|
On MS Visual C++, you will probably have to use the /Zm option to increase
|
||||||
|
the memory reserved to the compiler. /Zm500 should be enough to compile the
|
||||||
|
test bench.
|
||||||
|
|
||||||
|
- Also, MS Visual C++ issues a lot of warning related to the use of the EBX
|
||||||
|
register or lack of FEMMS instruction at the end of a function. This is normal
|
||||||
|
and you can safely disable these warning while using hiir classes.
|
||||||
|
|
||||||
|
The included test bench checks roughly the accuracy of the filters. It also
|
||||||
|
tests the speed of every available function. Therefore, implementing new
|
||||||
|
instruction set should be facilitated.
|
||||||
|
|
||||||
|
If you want to compile and run the test bench, please first edit the
|
||||||
|
test/conf.h file, in order to select the instruction sets available for your
|
||||||
|
CPU (there is currently no automatic detection). If you are not sure, disable
|
||||||
|
all of them.
|
||||||
|
|
||||||
|
In the same file, you have also testing options. You can save on the disk all
|
||||||
|
the samples generated during tests in order to check them in a sample editor.
|
||||||
|
However the files may take a lot of space on the disk, so it is recommended to
|
||||||
|
disable this option if it is not required. The "long tests" options are
|
||||||
|
intended to provide extensive checks on various filter sizes (it takes longer
|
||||||
|
to compile, but is safer if you want to change anything in the lib).
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
5. Oversampling to higher ratios
|
||||||
|
--------------------------------
|
||||||
|
|
||||||
|
It is possible to oversample a signal at a higher ratio than 2. You just have
|
||||||
|
to cascade up/downsamplers to achieve a power-of-2 ratio. Depending on your
|
||||||
|
requirements, you can reduce the filter order as the sampling rate is getting
|
||||||
|
bigger by reducing the transition bandwidth (TBW).
|
||||||
|
|
||||||
|
For example, let's suppose one wants 16x downsampling, with 96 dB of stopband
|
||||||
|
attenuation and a 0.49*Fs passband. You'll need the following specifications
|
||||||
|
for each stage:
|
||||||
|
|
||||||
|
2x -> 1x: TBW = 0.01
|
||||||
|
4x -> 2x: TBW = 0.01/2 + 1/4 = 0.255
|
||||||
|
8x -> 4x: TBW = 0.01/4 + 1/8 + 1/4 = 0.3775
|
||||||
|
16x -> 8x: TBW = 0.01/8 + 1/16 + 1/8 + 1/4 = 0.43865
|
||||||
|
|
||||||
|
The reason is that you do not need to preserve spectrum parts that will be
|
||||||
|
wiped out by subsequent stages. Only the spectrum part present after the
|
||||||
|
final stage has to be perserved.
|
||||||
|
|
||||||
|
More generally:
|
||||||
|
|
||||||
|
TBW[stage] = (TBW[stage-1] + 0.5) / 2
|
||||||
|
or
|
||||||
|
TBW[stage] = TBW[0] * (0.5^stage) + 0.5 * (1 - 0.5^stage)
|
||||||
|
|
||||||
|
So transition bandwidth requirement is significatively low until the last
|
||||||
|
stage (0). Thus, the optimal performance would be reached by using hiir
|
||||||
|
downsampler for the last stage because the requirement on the transition
|
||||||
|
bandwidth is important, and by using a classic FIR filtering for other
|
||||||
|
stages. Of course, it's possible to use hiir at every stage, but a well-
|
||||||
|
optimised polyphase FIR routine is probably more efficient than a 1- or 2-
|
||||||
|
coefficent IIR downsampler. Indeed, these IIR SIMD implementations have
|
||||||
|
little or no benefit for low-order filters, whereas small FIR filters can
|
||||||
|
benefit from SIMD. Check the speed test results to make your mind.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
6. History
|
||||||
|
----------
|
||||||
|
|
||||||
|
v1.11 (2012.06.26)
|
||||||
|
- Changed the license to the WTFPL
|
||||||
|
- Fixed some compilation warnings
|
||||||
|
|
||||||
|
v1.10 (2008.05.28)
|
||||||
|
- Changed directory structure
|
||||||
|
- Test code is now in its own namespace (hiir::test)
|
||||||
|
- Uses intrinsics for SSE code, making the code compilable on GCC.
|
||||||
|
|
||||||
|
v1.00 (2005.03.29)
|
||||||
|
- Initial release
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
7. Contact
|
||||||
|
----------
|
||||||
|
|
||||||
|
Please address any comment, bug report or flame to:
|
||||||
|
|
||||||
|
Laurent de Soras
|
||||||
|
http://ldesoras.free.fr
|
||||||
|
|
||||||
24
oversampling/LICENSE.txt
Normal file
24
oversampling/LICENSE.txt
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
iPlug 2 C++ Plug-in Framework.
|
||||||
|
|
||||||
|
Copyright (C) the iPlug 2 Developers. Portions copyright other contributors, see each source file for more information.
|
||||||
|
|
||||||
|
Based on WDL-OL/iPlug by Oli Larkin (2011-2018), and the original iPlug v1 (2008) by John Schwartz / Cockos
|
||||||
|
|
||||||
|
LICENSE:
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
|
||||||
|
1. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
||||||
|
1. This notice may not be removed or altered from any source distribution.
|
||||||
|
|
||||||
|
iPlug 2 includes the following 3rd party libraries (see each license info):
|
||||||
|
|
||||||
|
* Cockos WDL https://www.cockos.com/wdl
|
||||||
|
* NanoVG https://github.com/memononen/nanovg
|
||||||
|
* NanoSVG https://github.com/memononen/nanosvg
|
||||||
|
* MetalNanoVG https://github.com/ollix/MetalNanoVG
|
||||||
|
* RTAudio https://www.music.mcgill.ca/~gary/rtaudio
|
||||||
|
* RTMidi https://www.music.mcgill.ca/~gary/rtmidi
|
||||||
458
oversampling/Oversampler.h
Normal file
458
oversampling/Oversampler.h
Normal file
@@ -0,0 +1,458 @@
|
|||||||
|
/*
|
||||||
|
==============================================================================
|
||||||
|
|
||||||
|
This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
|
||||||
|
|
||||||
|
Modified by Christopher Herb for standalone use.
|
||||||
|
|
||||||
|
See LICENSE.txt for more info.
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#define OVERSAMPLING_FACTORS_VA_LIST "None", "2x", "4x", "8x", "16x"
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
#include "HIIR/FPUDownsampler2x.h"
|
||||||
|
#include "HIIR/FPUUpsampler2x.h"
|
||||||
|
|
||||||
|
#include "WDL/heapbuf.h"
|
||||||
|
#include "WDL/ptrlist.h"
|
||||||
|
|
||||||
|
// #include "IPlugPlatform.h"
|
||||||
|
|
||||||
|
using namespace hiir;
|
||||||
|
|
||||||
|
enum EFactor {
|
||||||
|
kNone = 0,
|
||||||
|
k2x,
|
||||||
|
k4x,
|
||||||
|
k8x,
|
||||||
|
k16x,
|
||||||
|
kNumFactors
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T = double>
|
||||||
|
class OverSampler {
|
||||||
|
public:
|
||||||
|
using BlockProcessFunc = std::function<void(T**, T**, int)>;
|
||||||
|
|
||||||
|
OverSampler(EFactor factor = kNone, bool blockProcessing = true, int nInChannels = 1, int nOutChannels = 1)
|
||||||
|
: mBlockProcessing(blockProcessing)
|
||||||
|
, mNInChannels(nInChannels)
|
||||||
|
, mNOutChannels(nOutChannels)
|
||||||
|
{
|
||||||
|
|
||||||
|
static constexpr double coeffs2x[12] = {0.036681502163648017, 0.13654762463195794, 0.27463175937945444,
|
||||||
|
0.42313861743656711, 0.56109869787919531, 0.67754004997416184,
|
||||||
|
0.76974183386322703, 0.83988962484963892, 0.89226081800387902,
|
||||||
|
0.9315419599631839, 0.96209454837808417, 0.98781637073289585};
|
||||||
|
static constexpr double coeffs4x[4] = {0.041893991997656171, 0.16890348243995201, 0.39056077292116603,
|
||||||
|
0.74389574826847926};
|
||||||
|
static constexpr double coeffs8x[3] = {0.055748680811302048, 0.24305119574153072, 0.64669913119268196};
|
||||||
|
static constexpr double coeffs16x[2] = {0.10717745346023573, 0.53091435354504557};
|
||||||
|
|
||||||
|
for (auto c = 0; c < mNInChannels; c++) {
|
||||||
|
mUpsampler2x.Add(new Upsampler2xFPU<12, T>());
|
||||||
|
mUpsampler4x.Add(new Upsampler2xFPU<4, T>());
|
||||||
|
mUpsampler8x.Add(new Upsampler2xFPU<3, T>());
|
||||||
|
mUpsampler16x.Add(new Upsampler2xFPU<2, T>());
|
||||||
|
|
||||||
|
mUpsampler2x.Get(c)->set_coefs(coeffs2x);
|
||||||
|
mUpsampler4x.Get(c)->set_coefs(coeffs4x);
|
||||||
|
mUpsampler8x.Get(c)->set_coefs(coeffs8x);
|
||||||
|
mUpsampler16x.Get(c)->set_coefs(coeffs16x);
|
||||||
|
|
||||||
|
// ptr location doesn't matter at this stage
|
||||||
|
mNextInputPtrs.Add(mUp2x.Get());
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto c = 0; c < mNOutChannels; c++) {
|
||||||
|
mDownsampler2x.Add(new Downsampler2xFPU<12, T>());
|
||||||
|
mDownsampler4x.Add(new Downsampler2xFPU<4, T>());
|
||||||
|
mDownsampler8x.Add(new Downsampler2xFPU<3, T>());
|
||||||
|
mDownsampler16x.Add(new Downsampler2xFPU<2, T>());
|
||||||
|
|
||||||
|
mDownsampler2x.Get(c)->set_coefs(coeffs2x);
|
||||||
|
mDownsampler4x.Get(c)->set_coefs(coeffs4x);
|
||||||
|
mDownsampler8x.Get(c)->set_coefs(coeffs8x);
|
||||||
|
mDownsampler16x.Get(c)->set_coefs(coeffs16x);
|
||||||
|
|
||||||
|
// ptr location doesn't matter at this stage
|
||||||
|
mNextOutputPtrs.Add(mDown2x.Get());
|
||||||
|
}
|
||||||
|
|
||||||
|
SetOverSampling(factor);
|
||||||
|
|
||||||
|
Reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
~OverSampler()
|
||||||
|
{
|
||||||
|
mUpsampler2x.Empty(true);
|
||||||
|
mDownsampler2x.Empty(true);
|
||||||
|
mUpsampler4x.Empty(true);
|
||||||
|
mDownsampler4x.Empty(true);
|
||||||
|
mUpsampler8x.Empty(true);
|
||||||
|
mDownsampler8x.Empty(true);
|
||||||
|
mUpsampler16x.Empty(true);
|
||||||
|
mDownsampler16x.Empty(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
OverSampler(const OverSampler&) = delete;
|
||||||
|
OverSampler& operator=(const OverSampler&) = delete;
|
||||||
|
|
||||||
|
void Reset(int blockSize = DEFAULT_BLOCK_SIZE)
|
||||||
|
{
|
||||||
|
int numBufSamples = 1;
|
||||||
|
|
||||||
|
if (mBlockProcessing) numBufSamples = blockSize;
|
||||||
|
else {
|
||||||
|
numBufSamples = 1;
|
||||||
|
blockSize = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
numBufSamples *= mNInChannels;
|
||||||
|
|
||||||
|
mUp2x.Resize(2 * numBufSamples);
|
||||||
|
mUp4x.Resize(4 * numBufSamples);
|
||||||
|
mUp8x.Resize(8 * numBufSamples);
|
||||||
|
mUp16x.Resize(16 * numBufSamples);
|
||||||
|
|
||||||
|
mDown2x.Resize(2 * numBufSamples);
|
||||||
|
mDown4x.Resize(4 * numBufSamples);
|
||||||
|
mDown8x.Resize(8 * numBufSamples);
|
||||||
|
mDown16x.Resize(16 * numBufSamples);
|
||||||
|
|
||||||
|
mUp16BufferPtrs.Empty();
|
||||||
|
mUp8BufferPtrs.Empty();
|
||||||
|
mUp4BufferPtrs.Empty();
|
||||||
|
mUp2BufferPtrs.Empty();
|
||||||
|
|
||||||
|
mDown16BufferPtrs.Empty();
|
||||||
|
mDown8BufferPtrs.Empty();
|
||||||
|
mDown4BufferPtrs.Empty();
|
||||||
|
mDown2BufferPtrs.Empty();
|
||||||
|
|
||||||
|
for (auto c = 0; c < mNInChannels; c++) {
|
||||||
|
mUpsampler2x.Get(c)->clear_buffers();
|
||||||
|
mUpsampler4x.Get(c)->clear_buffers();
|
||||||
|
mUpsampler8x.Get(c)->clear_buffers();
|
||||||
|
mUpsampler16x.Get(c)->clear_buffers();
|
||||||
|
|
||||||
|
mUp2BufferPtrs.Add(mUp2x.Get() + c * 2 * blockSize);
|
||||||
|
mUp4BufferPtrs.Add(mUp4x.Get() + (c * 4 * blockSize));
|
||||||
|
mUp8BufferPtrs.Add(mUp8x.Get() + (c * 8 * blockSize));
|
||||||
|
mUp16BufferPtrs.Add(mUp16x.Get() + (c * 16 * blockSize));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto c = 0; c < mNOutChannels; c++) {
|
||||||
|
mDownsampler2x.Get(c)->clear_buffers();
|
||||||
|
mDownsampler4x.Get(c)->clear_buffers();
|
||||||
|
mDownsampler8x.Get(c)->clear_buffers();
|
||||||
|
mDownsampler16x.Get(c)->clear_buffers();
|
||||||
|
|
||||||
|
mDown2BufferPtrs.Add(mDown2x.Get() + c * 2 * blockSize);
|
||||||
|
mDown4BufferPtrs.Add(mDown4x.Get() + (c * 4 * blockSize));
|
||||||
|
mDown8BufferPtrs.Add(mDown8x.Get() + (c * 8 * blockSize));
|
||||||
|
mDown16BufferPtrs.Add(mDown16x.Get() + (c * 16 * blockSize));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Over sample an input block with a per-block function (up sample input -> process with function -> down sample)
|
||||||
|
* @param inputs Two-dimensional array containing the non-interleaved input buffers of audio samples for all
|
||||||
|
* channels
|
||||||
|
* @param outputs Two-dimensional array for audio output (non-interleaved).
|
||||||
|
* @param nFrames The block size for this block: number of samples per channel.
|
||||||
|
* @param nInChans The number of input channels to process. Must be less or equal to the number of channels passed
|
||||||
|
* to the constructor
|
||||||
|
* @param nOutChans The number of output channels to process. Must be less or equal to the number of channels passed
|
||||||
|
* to the constructor
|
||||||
|
* @param func The function that processes the audio sample at the higher sampling rate. NOTE: std::function can
|
||||||
|
* call malloc if you pass in captures */
|
||||||
|
void ProcessBlock(T** inputs, T** outputs, int nFrames, int nInChans, int nOutChans, BlockProcessFunc func)
|
||||||
|
{
|
||||||
|
assert(nInChans <= mNInChannels);
|
||||||
|
assert(nOutChans <= mNOutChannels);
|
||||||
|
|
||||||
|
if (mRate != mPrevRate) {
|
||||||
|
switch (mRate) {
|
||||||
|
case 2:
|
||||||
|
mInPtrLoopSrc = &mUp2BufferPtrs;
|
||||||
|
mOutPtrLoopSrc = &mDown2BufferPtrs;
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
mInPtrLoopSrc = &mUp4BufferPtrs;
|
||||||
|
mOutPtrLoopSrc = &mDown4BufferPtrs;
|
||||||
|
break;
|
||||||
|
case 8:
|
||||||
|
mInPtrLoopSrc = &mUp8BufferPtrs;
|
||||||
|
mOutPtrLoopSrc = &mDown8BufferPtrs;
|
||||||
|
break;
|
||||||
|
case 16:
|
||||||
|
mInPtrLoopSrc = &mUp16BufferPtrs;
|
||||||
|
mOutPtrLoopSrc = &mDown16BufferPtrs;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
mPrevRate = mRate;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto c = 0; c < nInChans; c++) {
|
||||||
|
if (mRate >= 2) { mUpsampler2x.Get(c)->process_block(mUp2BufferPtrs.Get(c), inputs[c], nFrames); }
|
||||||
|
if (mRate >= 4) {
|
||||||
|
mUpsampler4x.Get(c)->process_block(mUp4BufferPtrs.Get(c), mUp2BufferPtrs.Get(c), nFrames * 2);
|
||||||
|
}
|
||||||
|
if (mRate >= 8) {
|
||||||
|
mUpsampler8x.Get(c)->process_block(mUp8BufferPtrs.Get(c), mUp4BufferPtrs.Get(c), nFrames * 4);
|
||||||
|
}
|
||||||
|
if (mRate == 16) {
|
||||||
|
mUpsampler16x.Get(c)->process_block(mUp16BufferPtrs.Get(c), mUp8BufferPtrs.Get(c), nFrames * 8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mRate == 1) {
|
||||||
|
func(inputs, outputs, nFrames);
|
||||||
|
} else {
|
||||||
|
for (auto i = 0; i < mRate; i++) {
|
||||||
|
for (auto c = 0; c < nInChans; c++) {
|
||||||
|
mNextInputPtrs.Set(c, mInPtrLoopSrc->Get(c) + (i * nFrames));
|
||||||
|
mNextOutputPtrs.Set(c, mOutPtrLoopSrc->Get(c) + (i * nFrames));
|
||||||
|
}
|
||||||
|
func(mNextInputPtrs.GetList(), mNextOutputPtrs.GetList(), nFrames);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto c = 0; c < nOutChans; c++) {
|
||||||
|
if (mRate == 16) {
|
||||||
|
mDownsampler16x.Get(c)->process_block(mDown8BufferPtrs.Get(c), mDown16BufferPtrs.Get(c), nFrames * 8);
|
||||||
|
}
|
||||||
|
if (mRate >= 8) {
|
||||||
|
mDownsampler8x.Get(c)->process_block(mDown4BufferPtrs.Get(c), mDown8BufferPtrs.Get(c), nFrames * 4);
|
||||||
|
}
|
||||||
|
if (mRate >= 4) {
|
||||||
|
mDownsampler4x.Get(c)->process_block(mDown2BufferPtrs.Get(c), mDown4BufferPtrs.Get(c), nFrames * 2);
|
||||||
|
}
|
||||||
|
if (mRate >= 2) { mDownsampler2x.Get(c)->process_block(outputs[c], mDown2BufferPtrs.Get(c), nFrames); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Over sample an input sample with a per-sample function (up-sample input -> process with function -> down-sample)
|
||||||
|
* @param input The audio sample to input
|
||||||
|
* @param std::function<double(double)> The function that processes the audio sample at the higher sampling rate.
|
||||||
|
* NOTE: std::function can call malloc if you pass in captures
|
||||||
|
* @return The audio sample output */
|
||||||
|
T Process(T input, std::function<T(T)> func)
|
||||||
|
{
|
||||||
|
T output;
|
||||||
|
|
||||||
|
if (mRate == 16) {
|
||||||
|
mUpsampler2x.Get(0)->process_sample(mUp2x.Get()[0], mUp2x.Get()[1], input);
|
||||||
|
mUpsampler4x.Get(0)->process_block(mUp4x.Get(), mUp2x.Get(), 2);
|
||||||
|
mUpsampler8x.Get(0)->process_block(mUp8x.Get(), mUp4x.Get(), 4);
|
||||||
|
mUpsampler16x.Get(0)->process_block(mUp16x.Get(), mUp8x.Get(), 8);
|
||||||
|
|
||||||
|
for (auto i = 0; i < 16; i++) { mDown16x.Get()[i] = func(mUp16x.Get()[i]); }
|
||||||
|
|
||||||
|
mDownsampler16x.Get(0)->process_block(mDown8x.Get(), mDown16x.Get(), 8);
|
||||||
|
mDownsampler8x.Get(0)->process_block(mDown4x.Get(), mDown8x.Get(), 4);
|
||||||
|
mDownsampler4x.Get(0)->process_block(mDown2x.Get(), mDown4x.Get(), 2);
|
||||||
|
output = mDownsampler2x.Get(0)->process_sample(mDown2x.Get());
|
||||||
|
} else if (mRate == 8) {
|
||||||
|
mUpsampler2x.Get(0)->process_sample(mUp2x.Get()[0], mUp2x.Get()[1], input);
|
||||||
|
mUpsampler4x.Get(0)->process_block(mUp4x.Get(), mUp2x.Get(), 2);
|
||||||
|
mUpsampler8x.Get(0)->process_block(mUp8x.Get(), mUp4x.Get(), 4);
|
||||||
|
|
||||||
|
for (auto i = 0; i < 8; i++) { mDown8x.Get()[i] = func(mUp8x.Get()[i]); }
|
||||||
|
|
||||||
|
mDownsampler8x.Get(0)->process_block(mDown4x.Get(), mDown8x.Get(), 4);
|
||||||
|
mDownsampler4x.Get(0)->process_block(mDown2x.Get(), mDown4x.Get(), 2);
|
||||||
|
output = mDownsampler2x.Get(0)->process_sample(mDown2x.Get());
|
||||||
|
} else if (mRate == 4) {
|
||||||
|
mUpsampler2x.Get(0)->process_sample(mUp2x.Get()[0], mUp2x.Get()[1], input);
|
||||||
|
mUpsampler4x.Get(0)->process_block(mUp4x.Get(), mUp2x.Get(), 2);
|
||||||
|
|
||||||
|
for (auto i = 0; i < 4; i++) { mDown4x.Get()[i] = func(mUp4x.Get()[i]); }
|
||||||
|
|
||||||
|
mDownsampler4x.Get(0)->process_block(mDown2x.Get(), mDown4x.Get(), 2);
|
||||||
|
output = mDownsampler2x.Get(0)->process_sample(mDown2x.Get());
|
||||||
|
} else if (mRate == 2) {
|
||||||
|
mUpsampler2x.Get(0)->process_sample(mUp2x.Get()[0], mUp2x.Get()[1], input);
|
||||||
|
|
||||||
|
mDown2x.Get()[0] = func(mUp2x.Get()[0]);
|
||||||
|
mDown2x.Get()[1] = func(mUp2x.Get()[1]);
|
||||||
|
output = mDownsampler2x.Get(0)->process_sample(mDown2x.Get());
|
||||||
|
} else {
|
||||||
|
output = func(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Over-sample an per-sample synthesis function
|
||||||
|
* @param genFunc The function that generates the audio sample
|
||||||
|
* @return The audio sample output */
|
||||||
|
T ProcessGen(std::function<T()> genFunc)
|
||||||
|
{
|
||||||
|
auto ProcessDown16x = [&](T input) {
|
||||||
|
mDown16x.Get()[mWritePos] = (T)input;
|
||||||
|
|
||||||
|
mWritePos++;
|
||||||
|
mWritePos &= 15;
|
||||||
|
|
||||||
|
if (mWritePos == 0) {
|
||||||
|
mDownsampler16x.Get(0)->process_block(mDown8x.Get(), mDown16x.Get(), 8);
|
||||||
|
mDownsampler8x.Get(0)->process_block(mDown4x.Get(), mDown8x.Get(), 4);
|
||||||
|
mDownsampler4x.Get(0)->process_block(mDown2x.Get(), mDown4x.Get(), 2);
|
||||||
|
mDownSamplerOutput = mDownsampler2x.Get(0)->process_sample(mDown2x.Get());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
auto ProcessDown8x = [&](T input) {
|
||||||
|
mDown8x.Get()[mWritePos] = (T)input;
|
||||||
|
|
||||||
|
mWritePos++;
|
||||||
|
mWritePos &= 7;
|
||||||
|
|
||||||
|
if (mWritePos == 0) {
|
||||||
|
mDownsampler8x.Get(0)->process_block(mDown4x.Get(), mDown8x.Get(), 4);
|
||||||
|
mDownsampler4x.Get(0)->process_block(mDown2x.Get(), mDown4x.Get(), 2);
|
||||||
|
mDownSamplerOutput = mDownsampler2x.Get(0)->process_sample(mDown2x.Get());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
auto ProcessDown4x = [&](T input) {
|
||||||
|
mDown4x.Get()[mWritePos] = (T)input;
|
||||||
|
|
||||||
|
mWritePos++;
|
||||||
|
mWritePos &= 3;
|
||||||
|
|
||||||
|
if (mWritePos == 0) {
|
||||||
|
mDownsampler4x.Get(0)->process_block(mDown2x.Get(), mDown4x.Get(), 2);
|
||||||
|
mDownSamplerOutput = mDownsampler2x.Get(0)->process_sample(mDown2x.Get());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
auto ProcessDown2x = [&](T input) {
|
||||||
|
mDown2x.Get()[mWritePos] = (T)input;
|
||||||
|
|
||||||
|
mWritePos = !mWritePos;
|
||||||
|
|
||||||
|
if (mWritePos == 0) { mDownSamplerOutput = mDownsampler2x.Get(0)->process_sample(mDown2x.Get()); }
|
||||||
|
};
|
||||||
|
|
||||||
|
T output;
|
||||||
|
|
||||||
|
for (int j = 0; j < mRate; j++) {
|
||||||
|
output = genFunc();
|
||||||
|
|
||||||
|
switch (mRate) {
|
||||||
|
case 2:
|
||||||
|
ProcessDown2x(output);
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
ProcessDown4x(output);
|
||||||
|
break;
|
||||||
|
case 8:
|
||||||
|
ProcessDown8x(output);
|
||||||
|
break;
|
||||||
|
case 16:
|
||||||
|
ProcessDown16x(output);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mRate > 1) output = mDownSamplerOutput;
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetOverSampling(EFactor factor)
|
||||||
|
{
|
||||||
|
if (factor != mFactor) {
|
||||||
|
mFactor = factor;
|
||||||
|
mRate = std::pow(2, (int)factor);
|
||||||
|
|
||||||
|
Reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static EFactor RateToFactor(int rate)
|
||||||
|
{
|
||||||
|
switch (rate) {
|
||||||
|
case 1:
|
||||||
|
return EFactor::kNone;
|
||||||
|
case 2:
|
||||||
|
return EFactor::k2x;
|
||||||
|
case 4:
|
||||||
|
return EFactor::k4x;
|
||||||
|
case 8:
|
||||||
|
return EFactor::k8x;
|
||||||
|
case 16:
|
||||||
|
return EFactor::k16x;
|
||||||
|
default:
|
||||||
|
assert(0);
|
||||||
|
return EFactor::kNone;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int GetRate() const { return mRate; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
EFactor mFactor = kNone;
|
||||||
|
int mPrevRate = 0;
|
||||||
|
int mRate = 1;
|
||||||
|
int mWritePos = 0;
|
||||||
|
T mDownSamplerOutput = 0.;
|
||||||
|
bool mBlockProcessing; // false
|
||||||
|
int mNInChannels; // 1
|
||||||
|
int mNOutChannels;
|
||||||
|
|
||||||
|
// the actual data
|
||||||
|
WDL_TypedBuf<T> mUp16x;
|
||||||
|
WDL_TypedBuf<T> mUp8x;
|
||||||
|
WDL_TypedBuf<T> mUp4x;
|
||||||
|
WDL_TypedBuf<T> mUp2x;
|
||||||
|
|
||||||
|
WDL_TypedBuf<T> mDown16x;
|
||||||
|
WDL_TypedBuf<T> mDown8x;
|
||||||
|
WDL_TypedBuf<T> mDown4x;
|
||||||
|
WDL_TypedBuf<T> mDown2x;
|
||||||
|
|
||||||
|
// Ptrs into buffer data
|
||||||
|
WDL_PtrList<T> mUp16BufferPtrs;
|
||||||
|
WDL_PtrList<T> mUp8BufferPtrs;
|
||||||
|
WDL_PtrList<T> mUp4BufferPtrs;
|
||||||
|
WDL_PtrList<T> mUp2BufferPtrs;
|
||||||
|
|
||||||
|
WDL_PtrList<T> mDown16BufferPtrs;
|
||||||
|
WDL_PtrList<T> mDown8BufferPtrs;
|
||||||
|
WDL_PtrList<T> mDown4BufferPtrs;
|
||||||
|
WDL_PtrList<T> mDown2BufferPtrs;
|
||||||
|
|
||||||
|
WDL_PtrList<T> mNextInputPtrs;
|
||||||
|
WDL_PtrList<T> mNextOutputPtrs;
|
||||||
|
|
||||||
|
// Ptrs to the buffer data ptrs, changed depending on rate (block processing only)
|
||||||
|
WDL_PtrList<T>* mInPtrLoopSrc = nullptr;
|
||||||
|
WDL_PtrList<T>* mOutPtrLoopSrc = nullptr;
|
||||||
|
|
||||||
|
// Ptrs to oversamplers for each channel
|
||||||
|
WDL_PtrList<Upsampler2xFPU<12, T>> mUpsampler2x; // for 1x to 2x SR
|
||||||
|
WDL_PtrList<Upsampler2xFPU<4, T>> mUpsampler4x; // for 2x to 4x SR
|
||||||
|
WDL_PtrList<Upsampler2xFPU<3, T>> mUpsampler8x; // for 4x to 8x SR
|
||||||
|
WDL_PtrList<Upsampler2xFPU<2, T>> mUpsampler16x; // for 8x to 16x SR
|
||||||
|
|
||||||
|
WDL_PtrList<Downsampler2xFPU<12, T>> mDownsampler2x; // decimator for 2x to 1x SR
|
||||||
|
WDL_PtrList<Downsampler2xFPU<4, T>> mDownsampler4x; // decimator for 4x to 2x SR
|
||||||
|
WDL_PtrList<Downsampler2xFPU<3, T>> mDownsampler8x; // decimator for 8x to 4x SR
|
||||||
|
WDL_PtrList<Downsampler2xFPU<2, T>> mDownsampler16x; // decimator for 16x to 8x SR
|
||||||
|
};
|
||||||
26
oversampling/WDL/.gitattributes
vendored
Normal file
26
oversampling/WDL/.gitattributes
vendored
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
* -text
|
||||||
|
*.c text=auto
|
||||||
|
*.cpp text=auto
|
||||||
|
*.cc text=auto
|
||||||
|
*.h text=auto
|
||||||
|
*.hpp text=auto
|
||||||
|
*.m text=auto
|
||||||
|
*.mm text=auto
|
||||||
|
|
||||||
|
*.eel text=auto
|
||||||
|
*.php text=auto
|
||||||
|
*.txt text=auto
|
||||||
|
|
||||||
|
*.bat text eol=crlf
|
||||||
|
*.cmd text eol=crlf
|
||||||
|
|
||||||
|
*.rc text eol=crlf
|
||||||
|
*.dsp text eol=crlf
|
||||||
|
*.dsw text eol=crlf
|
||||||
|
*.sln text eol=crlf
|
||||||
|
*.vcxproj text eol=crlf
|
||||||
|
*.vcxproj.filters text eol=crlf
|
||||||
|
|
||||||
|
*.sh text eol=lf
|
||||||
|
*.pbxproj text eol=lf
|
||||||
|
Makefile text eol=lf
|
||||||
413
oversampling/WDL/MersenneTwister.h
Normal file
413
oversampling/WDL/MersenneTwister.h
Normal file
@@ -0,0 +1,413 @@
|
|||||||
|
// Taken from http://www-personal.engin.umich.edu/~wagnerr/MersenneTwister.html
|
||||||
|
|
||||||
|
|
||||||
|
// MersenneTwister.h
|
||||||
|
// Mersenne Twister random number generator -- a C++ class MTRand
|
||||||
|
// Based on code by Makoto Matsumoto, Takuji Nishimura, and Shawn Cokus
|
||||||
|
// Richard J. Wagner v1.0 15 May 2003 rjwagner@writeme.com
|
||||||
|
|
||||||
|
// The Mersenne Twister is an algorithm for generating random numbers. It
|
||||||
|
// was designed with consideration of the flaws in various other generators.
|
||||||
|
// The period, 2^19937-1, and the order of equidistribution, 623 dimensions,
|
||||||
|
// are far greater. The generator is also fast; it avoids multiplication and
|
||||||
|
// division, and it benefits from caches and pipelines. For more information
|
||||||
|
// see the inventors' web page at http://www.math.keio.ac.jp/~matumoto/emt.html
|
||||||
|
|
||||||
|
// Reference
|
||||||
|
// M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally
|
||||||
|
// Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions on
|
||||||
|
// Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.
|
||||||
|
|
||||||
|
// Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
|
||||||
|
// Copyright (C) 2000 - 2003, Richard J. Wagner
|
||||||
|
// All rights reserved.
|
||||||
|
//
|
||||||
|
// Redistribution and use in source and binary forms, with or without
|
||||||
|
// modification, are permitted provided that the following conditions
|
||||||
|
// are met:
|
||||||
|
//
|
||||||
|
// 1. Redistributions of source code must retain the above copyright
|
||||||
|
// notice, this list of conditions and the following disclaimer.
|
||||||
|
//
|
||||||
|
// 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
// notice, this list of conditions and the following disclaimer in the
|
||||||
|
// documentation and/or other materials provided with the distribution.
|
||||||
|
//
|
||||||
|
// 3. The names of its contributors may not be used to endorse or promote
|
||||||
|
// products derived from this software without specific prior written
|
||||||
|
// permission.
|
||||||
|
//
|
||||||
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||||
|
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
// The original code included the following notice:
|
||||||
|
//
|
||||||
|
// When you use this, send an email to: matumoto@math.keio.ac.jp
|
||||||
|
// with an appropriate reference to your work.
|
||||||
|
//
|
||||||
|
// It would be nice to CC: rjwagner@writeme.com and Cokus@math.washington.edu
|
||||||
|
// when you write.
|
||||||
|
|
||||||
|
#ifndef _MERSENNETWISTER_H_
|
||||||
|
#define _MERSENNETWISTER_H_
|
||||||
|
|
||||||
|
// Not thread safe (unless auto-initialization is avoided and each thread has
|
||||||
|
// its own MTRand object)
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
class MTRand {
|
||||||
|
// Data
|
||||||
|
public:
|
||||||
|
typedef unsigned int uint32; // unsigned integer type, at least 32 bits
|
||||||
|
|
||||||
|
enum { N = 624 }; // length of state vector
|
||||||
|
enum { SAVE = N + 1 }; // length of array for save()
|
||||||
|
|
||||||
|
protected:
|
||||||
|
enum { M = 397 }; // period parameter
|
||||||
|
|
||||||
|
uint32 state[N]; // internal state
|
||||||
|
uint32 *pNext; // next value to get from state
|
||||||
|
int left; // number of values left before reload needed
|
||||||
|
|
||||||
|
|
||||||
|
//Methods
|
||||||
|
public:
|
||||||
|
MTRand( const uint32& oneSeed ); // initialize with a simple uint32
|
||||||
|
MTRand( uint32 *const bigSeed, uint32 const seedLength = N ); // or an array
|
||||||
|
MTRand(); // auto-initialize with /dev/urandom or time() and clock()
|
||||||
|
|
||||||
|
// Do NOT use for CRYPTOGRAPHY without securely hashing several returned
|
||||||
|
// values together, otherwise the generator state can be learned after
|
||||||
|
// reading 624 consecutive values.
|
||||||
|
|
||||||
|
// Access to 32-bit random numbers
|
||||||
|
double rand(); // real number in [0,1]
|
||||||
|
double rand( const double& n ); // real number in [0,n]
|
||||||
|
double randExc(); // real number in [0,1)
|
||||||
|
double randExc( const double& n ); // real number in [0,n)
|
||||||
|
double randDblExc(); // real number in (0,1)
|
||||||
|
double randDblExc( const double& n ); // real number in (0,n)
|
||||||
|
uint32 randInt(); // integer in [0,2^32-1]
|
||||||
|
uint32 randInt( const uint32& n ); // integer in [0,n] for n < 2^32
|
||||||
|
double operator()() { return rand(); } // same as rand()
|
||||||
|
|
||||||
|
// Access to 53-bit random numbers (capacity of IEEE double precision)
|
||||||
|
double rand53(); // real number in [0,1)
|
||||||
|
|
||||||
|
// Access to nonuniform random number distributions
|
||||||
|
double randNorm( const double& mean = 0.0, const double& variance = 0.0 );
|
||||||
|
|
||||||
|
// Re-seeding functions with same behavior as initializers
|
||||||
|
void seed( const uint32 oneSeed );
|
||||||
|
void seed( uint32 *const bigSeed, const uint32 seedLength = N );
|
||||||
|
void seed();
|
||||||
|
|
||||||
|
// Saving and loading generator state
|
||||||
|
void save( uint32* saveArray ) const; // to array of size SAVE
|
||||||
|
void load( uint32 *const loadArray ); // from such array
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void initialize( const uint32 oneSeed );
|
||||||
|
void reload();
|
||||||
|
uint32 hiBit( const uint32& u ) const { return u & 0x80000000UL; }
|
||||||
|
uint32 loBit( const uint32& u ) const { return u & 0x00000001UL; }
|
||||||
|
uint32 loBits( const uint32& u ) const { return u & 0x7fffffffUL; }
|
||||||
|
uint32 mixBits( const uint32& u, const uint32& v ) const
|
||||||
|
{ return hiBit(u) | loBits(v); }
|
||||||
|
uint32 twist( const uint32& m, const uint32& s0, const uint32& s1 ) const
|
||||||
|
{ return m ^ (mixBits(s0,s1)>>1) ^ (-((int)loBit(s1)) & 0x9908b0dfUL); }
|
||||||
|
|
||||||
|
public:
|
||||||
|
// This was protected, but we need it exposed so FIRan1.h can use it for seeding
|
||||||
|
static uint32 hash( time_t t, clock_t c );
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
inline MTRand::MTRand( const uint32& oneSeed )
|
||||||
|
{ seed(oneSeed); }
|
||||||
|
|
||||||
|
inline MTRand::MTRand( uint32 *const bigSeed, const uint32 seedLength )
|
||||||
|
{ seed(bigSeed,seedLength); }
|
||||||
|
|
||||||
|
inline MTRand::MTRand()
|
||||||
|
{ seed(); }
|
||||||
|
|
||||||
|
inline double MTRand::rand()
|
||||||
|
{ return double(randInt()) * (1.0/4294967295.0); }
|
||||||
|
|
||||||
|
inline double MTRand::rand( const double& n )
|
||||||
|
{ return rand() * n; }
|
||||||
|
|
||||||
|
inline double MTRand::randExc()
|
||||||
|
{ return double(randInt()) * (1.0/4294967296.0); }
|
||||||
|
|
||||||
|
inline double MTRand::randExc( const double& n )
|
||||||
|
{ return randExc() * n; }
|
||||||
|
|
||||||
|
inline double MTRand::randDblExc()
|
||||||
|
{ return ( double(randInt()) + 0.5 ) * (1.0/4294967296.0); }
|
||||||
|
|
||||||
|
inline double MTRand::randDblExc( const double& n )
|
||||||
|
{ return randDblExc() * n; }
|
||||||
|
|
||||||
|
inline double MTRand::rand53()
|
||||||
|
{
|
||||||
|
uint32 a = randInt() >> 5, b = randInt() >> 6;
|
||||||
|
return ( a * 67108864.0 + b ) * (1.0/9007199254740992.0); // by Isaku Wada
|
||||||
|
}
|
||||||
|
|
||||||
|
inline double MTRand::randNorm( const double& mean, const double& variance )
|
||||||
|
{
|
||||||
|
// Return a real number from a normal (Gaussian) distribution with given
|
||||||
|
// mean and variance by Box-Muller method
|
||||||
|
double r = sqrt( -2.0 * log( 1.0-randDblExc()) ) * variance;
|
||||||
|
double phi = 2.0 * 3.14159265358979323846264338328 * randExc();
|
||||||
|
return mean + r * cos(phi);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline MTRand::uint32 MTRand::randInt()
|
||||||
|
{
|
||||||
|
// Pull a 32-bit integer from the generator state
|
||||||
|
// Every other access function simply transforms the numbers extracted here
|
||||||
|
|
||||||
|
if( left == 0 ) reload();
|
||||||
|
--left;
|
||||||
|
|
||||||
|
uint32 s1;
|
||||||
|
s1 = *pNext++;
|
||||||
|
s1 ^= (s1 >> 11);
|
||||||
|
s1 ^= (s1 << 7) & 0x9d2c5680UL;
|
||||||
|
s1 ^= (s1 << 15) & 0xefc60000UL;
|
||||||
|
return ( s1 ^ (s1 >> 18) );
|
||||||
|
}
|
||||||
|
|
||||||
|
inline MTRand::uint32 MTRand::randInt( const uint32& n )
|
||||||
|
{
|
||||||
|
// Find which bits are used in n
|
||||||
|
// Optimized by Magnus Jonsson (magnus@smartelectronix.com)
|
||||||
|
uint32 used = n;
|
||||||
|
used |= used >> 1;
|
||||||
|
used |= used >> 2;
|
||||||
|
used |= used >> 4;
|
||||||
|
used |= used >> 8;
|
||||||
|
used |= used >> 16;
|
||||||
|
|
||||||
|
// Draw numbers until one is found in [0,n]
|
||||||
|
uint32 i;
|
||||||
|
do
|
||||||
|
i = randInt() & used; // toss unused bits to shorten search
|
||||||
|
while( i > n );
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline void MTRand::seed( const uint32 oneSeed )
|
||||||
|
{
|
||||||
|
// Seed the generator with a simple uint32
|
||||||
|
initialize(oneSeed);
|
||||||
|
reload();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline void MTRand::seed( uint32 *const bigSeed, const uint32 seedLength )
|
||||||
|
{
|
||||||
|
// Seed the generator with an array of uint32's
|
||||||
|
// There are 2^19937-1 possible initial states. This function allows
|
||||||
|
// all of those to be accessed by providing at least 19937 bits (with a
|
||||||
|
// default seed length of N = 624 uint32's). Any bits above the lower 32
|
||||||
|
// in each element are discarded.
|
||||||
|
// Just call seed() if you want to get array from /dev/urandom
|
||||||
|
initialize(19650218UL);
|
||||||
|
int i = 1;
|
||||||
|
uint32 j = 0;
|
||||||
|
int k = ( N > seedLength ? N : seedLength );
|
||||||
|
for( ; k; --k )
|
||||||
|
{
|
||||||
|
state[i] =
|
||||||
|
state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1664525UL );
|
||||||
|
state[i] += ( bigSeed[j] & 0xffffffffUL ) + j;
|
||||||
|
state[i] &= 0xffffffffUL;
|
||||||
|
++i; ++j;
|
||||||
|
if( i >= N ) { state[0] = state[N-1]; i = 1; }
|
||||||
|
if( j >= seedLength ) j = 0;
|
||||||
|
}
|
||||||
|
for( k = N - 1; k; --k )
|
||||||
|
{
|
||||||
|
state[i] =
|
||||||
|
state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1566083941UL );
|
||||||
|
state[i] -= i;
|
||||||
|
state[i] &= 0xffffffffUL;
|
||||||
|
++i;
|
||||||
|
if( i >= N ) { state[0] = state[N-1]; i = 1; }
|
||||||
|
}
|
||||||
|
state[0] = 0x80000000UL; // MSB is 1, assuring non-zero initial array
|
||||||
|
reload();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline void MTRand::seed()
|
||||||
|
{
|
||||||
|
// Seed the generator with an array from /dev/urandom if available
|
||||||
|
// Otherwise use a hash of time() and clock() values
|
||||||
|
|
||||||
|
// No point in trying this on Windows machines - won't work, so it just slows things down
|
||||||
|
#ifndef WIN32
|
||||||
|
#ifndef WDL_MTRAND_FASTSEED
|
||||||
|
// First try getting an array from /dev/urandom
|
||||||
|
FILE* urandom = fopen( "/dev/urandom", "rb" );
|
||||||
|
if( urandom )
|
||||||
|
{
|
||||||
|
uint32 bigSeed[N];
|
||||||
|
uint32 *s = bigSeed;
|
||||||
|
int i = N;
|
||||||
|
bool success = true;
|
||||||
|
while( success && i-- )
|
||||||
|
success = fread( s++, sizeof(uint32), 1, urandom );
|
||||||
|
fclose(urandom);
|
||||||
|
if( success ) { seed( bigSeed, N ); return; }
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Was not successful, so use time() and clock() instead
|
||||||
|
seed( hash( time(NULL), clock() ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline void MTRand::initialize( const uint32 seedv )
|
||||||
|
{
|
||||||
|
// Initialize generator state with seed
|
||||||
|
// See Knuth TAOCP Vol 2, 3rd Ed, p.106 for multiplier.
|
||||||
|
// In previous versions, most significant bits (MSBs) of the seed affect
|
||||||
|
// only MSBs of the state array. Modified 9 Jan 2002 by Makoto Matsumoto.
|
||||||
|
uint32 *s = state;
|
||||||
|
uint32 *r = state;
|
||||||
|
int i = 1;
|
||||||
|
*s++ = seedv & 0xffffffffUL;
|
||||||
|
for( ; i < N; ++i )
|
||||||
|
{
|
||||||
|
*s++ = ( 1812433253UL * ( *r ^ (*r >> 30) ) + i ) & 0xffffffffUL;
|
||||||
|
r++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline void MTRand::reload()
|
||||||
|
{
|
||||||
|
// Generate N new values in state
|
||||||
|
// Made clearer and faster by Matthew Bellew (matthew.bellew@home.com)
|
||||||
|
uint32 *p = state;
|
||||||
|
int i;
|
||||||
|
for( i = int(N) - int(M); i--; ++p )
|
||||||
|
*p = twist( p[M], p[0], p[1] );
|
||||||
|
for( i = M; --i; ++p )
|
||||||
|
*p = twist( p[int(M)-int(N)], p[0], p[1] );
|
||||||
|
*p = twist( p[int(M)-int(N)], p[0], state[0] );
|
||||||
|
|
||||||
|
left = N, pNext = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline MTRand::uint32 MTRand::hash( time_t t, clock_t c )
|
||||||
|
{
|
||||||
|
// Get a uint32 from t and c
|
||||||
|
// Better than uint32(x) in case x is floating point in [0,1]
|
||||||
|
// Based on code by Lawrence Kirby (fred@genesis.demon.co.uk)
|
||||||
|
|
||||||
|
static uint32 differ = 0; // guarantee time-based seeds will change
|
||||||
|
|
||||||
|
uint32 h1 = 0;
|
||||||
|
unsigned char *p = (unsigned char *) &t;
|
||||||
|
for( size_t i = 0; i < sizeof(t); ++i )
|
||||||
|
{
|
||||||
|
h1 *= UCHAR_MAX + 2U;
|
||||||
|
h1 += p[i];
|
||||||
|
}
|
||||||
|
uint32 h2 = 0;
|
||||||
|
p = (unsigned char *) &c;
|
||||||
|
for( size_t j = 0; j < sizeof(c); ++j )
|
||||||
|
{
|
||||||
|
h2 *= UCHAR_MAX + 2U;
|
||||||
|
h2 += p[j];
|
||||||
|
}
|
||||||
|
return ( h1 + differ++ ) ^ h2;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline void MTRand::save( uint32* saveArray ) const
|
||||||
|
{
|
||||||
|
uint32 *sa = saveArray;
|
||||||
|
const uint32 *s = state;
|
||||||
|
int i = N;
|
||||||
|
for( ; i--; *sa++ = *s++ ) {}
|
||||||
|
*sa = left;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline void MTRand::load( uint32 *const loadArray )
|
||||||
|
{
|
||||||
|
uint32 *s = state;
|
||||||
|
uint32 *la = loadArray;
|
||||||
|
int i = N;
|
||||||
|
for( ; i--; *s++ = *la++ ) {}
|
||||||
|
left = *la;
|
||||||
|
pNext = &state[N-left];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif // MERSENNETWISTER_H
|
||||||
|
|
||||||
|
// Change log:
|
||||||
|
//
|
||||||
|
// v0.1 - First release on 15 May 2000
|
||||||
|
// - Based on code by Makoto Matsumoto, Takuji Nishimura, and Shawn Cokus
|
||||||
|
// - Translated from C to C++
|
||||||
|
// - Made completely ANSI compliant
|
||||||
|
// - Designed convenient interface for initialization, seeding, and
|
||||||
|
// obtaining numbers in default or user-defined ranges
|
||||||
|
// - Added automatic seeding from /dev/urandom or time() and clock()
|
||||||
|
// - Provided functions for saving and loading generator state
|
||||||
|
//
|
||||||
|
// v0.2 - Fixed bug which reloaded generator one step too late
|
||||||
|
//
|
||||||
|
// v0.3 - Switched to clearer, faster reload() code from Matthew Bellew
|
||||||
|
//
|
||||||
|
// v0.4 - Removed trailing newline in saved generator format to be consistent
|
||||||
|
// with output format of built-in types
|
||||||
|
//
|
||||||
|
// v0.5 - Improved portability by replacing static const int's with enum's and
|
||||||
|
// clarifying return values in seed(); suggested by Eric Heimburg
|
||||||
|
// - Removed MAXINT constant; use 0xffffffffUL instead
|
||||||
|
//
|
||||||
|
// v0.6 - Eliminated seed overflow when uint32 is larger than 32 bits
|
||||||
|
// - Changed integer [0,n] generator to give better uniformity
|
||||||
|
//
|
||||||
|
// v0.7 - Fixed operator precedence ambiguity in reload()
|
||||||
|
// - Added access for real numbers in (0,1) and (0,n)
|
||||||
|
//
|
||||||
|
// v0.8 - Included time.h header to properly support time_t and clock_t
|
||||||
|
//
|
||||||
|
// v1.0 - Revised seeding to match 26 Jan 2002 update of Nishimura and Matsumoto
|
||||||
|
// - Allowed for seeding with arrays of any length
|
||||||
|
// - Added access for real numbers in [0,1) with 53-bit resolution
|
||||||
|
// - Added access for real numbers from normal (Gaussian) distributions
|
||||||
|
// - Increased overall speed by optimizing twist()
|
||||||
|
// - Doubled speed of integer [0,n] generation
|
||||||
|
// - Fixed out-of-range number generation on 64-bit machines
|
||||||
|
// - Improved portability by substituting literal constants for long enum's
|
||||||
|
// - Changed license from GNU LGPL to BSD
|
||||||
319
oversampling/WDL/adpcm_decode.h
Normal file
319
oversampling/WDL/adpcm_decode.h
Normal file
@@ -0,0 +1,319 @@
|
|||||||
|
#ifndef _WDL_ADPCM_DECODE_H_
|
||||||
|
#define _WDL_ADPCM_DECODE_H_
|
||||||
|
|
||||||
|
#include "queue.h"
|
||||||
|
|
||||||
|
#define MSADPCM_TYPE 2
|
||||||
|
#define IMAADPCM_TYPE 0x11
|
||||||
|
#define CADPCM2_TYPE 0xac0c
|
||||||
|
|
||||||
|
class WDL_adpcm_decoder
|
||||||
|
{
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int cf1,cf2,deltas,spl1,spl2;
|
||||||
|
} WDL_adpcm_decode_chanctx;
|
||||||
|
|
||||||
|
public:
|
||||||
|
enum { MSADPCM_PREAMBLELEN=7, IMA_PREAMBLELEN=4 };
|
||||||
|
static INT64 sampleLengthFromBytes(INT64 nbytes, int blockalign, int nch, int type, int bps)
|
||||||
|
{
|
||||||
|
if (!bps||type!=CADPCM2_TYPE) bps=4;
|
||||||
|
// remove overhead of headers
|
||||||
|
INT64 nblocks=((nbytes+blockalign-1)/blockalign);
|
||||||
|
|
||||||
|
// remove preambles
|
||||||
|
if (type==IMAADPCM_TYPE||type==CADPCM2_TYPE) nbytes -= nblocks*IMA_PREAMBLELEN*nch;
|
||||||
|
else nbytes -= nblocks*MSADPCM_PREAMBLELEN*nch;
|
||||||
|
|
||||||
|
// scale from bytes to samples
|
||||||
|
nbytes = (nbytes*8)/(nch*bps);
|
||||||
|
|
||||||
|
if (type==IMAADPCM_TYPE||type==CADPCM2_TYPE) nbytes++; // IMA has just one initial sample
|
||||||
|
else nbytes+=2; // msadpcm has 2 initial sample values
|
||||||
|
|
||||||
|
return nbytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
WDL_adpcm_decoder(int blockalign,int nch, int type, int bps)
|
||||||
|
{
|
||||||
|
m_bps=0;
|
||||||
|
m_type=0;
|
||||||
|
m_nch=0;
|
||||||
|
m_blockalign=0;
|
||||||
|
m_srcbuf=0;
|
||||||
|
m_chans=0;
|
||||||
|
setParameters(blockalign,nch,type,bps);
|
||||||
|
}
|
||||||
|
~WDL_adpcm_decoder()
|
||||||
|
{
|
||||||
|
free(m_srcbuf);
|
||||||
|
free(m_chans);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void resetState()
|
||||||
|
{
|
||||||
|
if (m_chans) memset(m_chans,0,m_nch*sizeof(WDL_adpcm_decode_chanctx));
|
||||||
|
m_srcbuf_valid=0;
|
||||||
|
samplesOut.Clear();
|
||||||
|
samplesOut.Compact();
|
||||||
|
}
|
||||||
|
|
||||||
|
void setParameters(int ba, int nch, int type, int bps)
|
||||||
|
{
|
||||||
|
if (m_blockalign != ba||nch != m_nch||type!=m_type||bps != m_bps)
|
||||||
|
{
|
||||||
|
free(m_srcbuf);
|
||||||
|
free(m_chans);
|
||||||
|
m_bps=bps;
|
||||||
|
m_blockalign=ba;
|
||||||
|
m_nch=nch;
|
||||||
|
m_srcbuf_valid=0;
|
||||||
|
m_srcbuf=(unsigned char*)malloc(ba);
|
||||||
|
m_chans=(WDL_adpcm_decode_chanctx*)malloc(sizeof(WDL_adpcm_decode_chanctx)*nch);
|
||||||
|
m_type=type;
|
||||||
|
resetState();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int blockAlign() { return m_blockalign; }
|
||||||
|
|
||||||
|
int samplesPerBlock()
|
||||||
|
{
|
||||||
|
if (m_type==IMAADPCM_TYPE||m_type==CADPCM2_TYPE)
|
||||||
|
{
|
||||||
|
if (m_bps == 2) return (m_blockalign/m_nch - IMA_PREAMBLELEN)*4 + 1;
|
||||||
|
return (m_blockalign/m_nch - IMA_PREAMBLELEN)*2 + 1; //4 bit
|
||||||
|
}
|
||||||
|
return (m_blockalign/m_nch - MSADPCM_PREAMBLELEN)*2 + 2; // 4 bit
|
||||||
|
}
|
||||||
|
|
||||||
|
INT64 samplesToSourceBytes(INT64 outlen_samples) // length in samplepairs
|
||||||
|
{
|
||||||
|
outlen_samples -= samplesOut.Available()/m_nch;
|
||||||
|
if (outlen_samples<1) return 0; // no data required
|
||||||
|
|
||||||
|
int spls_block = samplesPerBlock();
|
||||||
|
if (spls_block<1) return 0;
|
||||||
|
INT64 nblocks = (outlen_samples+spls_block-1)/spls_block;
|
||||||
|
INT64 v=nblocks * m_blockalign;
|
||||||
|
|
||||||
|
v -= m_srcbuf_valid;
|
||||||
|
|
||||||
|
return wdl_max(v,0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AddInput(void *buf, int len, short *parm_cotab=NULL)
|
||||||
|
{
|
||||||
|
unsigned char *rdbuf = (unsigned char *)buf;
|
||||||
|
if (m_srcbuf_valid)
|
||||||
|
{
|
||||||
|
int v=m_blockalign-m_srcbuf_valid;
|
||||||
|
if (v>len) v=len;
|
||||||
|
|
||||||
|
memcpy(m_srcbuf+m_srcbuf_valid,rdbuf,v);
|
||||||
|
len-=v;
|
||||||
|
rdbuf+=v;
|
||||||
|
if ((m_srcbuf_valid+=v)>=m_blockalign)
|
||||||
|
{
|
||||||
|
DecodeBlock(m_srcbuf,parm_cotab);
|
||||||
|
m_srcbuf_valid=0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (len >= m_blockalign)
|
||||||
|
{
|
||||||
|
DecodeBlock(rdbuf,parm_cotab);
|
||||||
|
rdbuf+=m_blockalign;
|
||||||
|
len-=m_blockalign;
|
||||||
|
}
|
||||||
|
if (len>0) memcpy(m_srcbuf,rdbuf,m_srcbuf_valid=len);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int sourceBytesQueued() { return m_srcbuf_valid; }
|
||||||
|
WDL_TypedQueue<short> samplesOut;
|
||||||
|
|
||||||
|
private:
|
||||||
|
static int getwordsigned(unsigned char **rdptr)
|
||||||
|
{
|
||||||
|
int s = (*rdptr)[0] + ((*rdptr)[1]<<8);
|
||||||
|
(*rdptr)+=2;
|
||||||
|
if (s & 0x8000) s -= 0x10000;
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DecodeBlockIMA(unsigned char *buf)
|
||||||
|
{
|
||||||
|
int samples_block = samplesPerBlock();
|
||||||
|
|
||||||
|
int nch=m_nch;
|
||||||
|
int ch;
|
||||||
|
short *outptr = samplesOut.Add(NULL,samples_block * nch);
|
||||||
|
|
||||||
|
for (ch=0;ch<nch;ch++)
|
||||||
|
{
|
||||||
|
m_chans[ch].spl1 = getwordsigned(&buf);
|
||||||
|
m_chans[ch].cf1 = buf[0] | (buf[1]<<8);
|
||||||
|
buf+=2;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (ch=0;ch<nch;ch++) *outptr++ = m_chans[ch].spl1;
|
||||||
|
|
||||||
|
char bstate=0;
|
||||||
|
unsigned char lastbyte=0;
|
||||||
|
int x;
|
||||||
|
|
||||||
|
static signed char index_table[8] = { -1, -1, -1, -1, 2, 4, 6, 8 };
|
||||||
|
static short step_table[89] = {
|
||||||
|
7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
|
||||||
|
19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
|
||||||
|
50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
|
||||||
|
130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
|
||||||
|
337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
|
||||||
|
876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
|
||||||
|
2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
|
||||||
|
5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
|
||||||
|
15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
int splcnt = (samples_block-1)*nch;
|
||||||
|
int cnt=0;
|
||||||
|
ch=0;
|
||||||
|
int wrpos = 0;
|
||||||
|
|
||||||
|
int bps=m_bps;
|
||||||
|
const int chunksize = bps == 2 ? 16 : 8;
|
||||||
|
|
||||||
|
for (x=0; x < splcnt; x++)
|
||||||
|
{
|
||||||
|
int nib;
|
||||||
|
|
||||||
|
if (bps==2)
|
||||||
|
{
|
||||||
|
switch (bstate++)
|
||||||
|
{
|
||||||
|
case 0: nib=(lastbyte=*buf++)<<2; break;
|
||||||
|
case 1: nib=lastbyte; break;
|
||||||
|
case 2: nib=lastbyte>>2; break;
|
||||||
|
default: nib=lastbyte>>4; bstate=0; break;
|
||||||
|
}
|
||||||
|
nib &= 8|4;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if ((bstate^=1)) nib=(lastbyte=*buf++)&0xf;
|
||||||
|
else nib=lastbyte>>4;
|
||||||
|
}
|
||||||
|
|
||||||
|
int step_index=m_chans[ch].cf1;
|
||||||
|
if (step_index<0)step_index=0;
|
||||||
|
else if (step_index>88)step_index=88;
|
||||||
|
|
||||||
|
int step=step_table[step_index];
|
||||||
|
|
||||||
|
int diff = ((nib&7)*step)/4 + step/8;
|
||||||
|
|
||||||
|
int v=m_chans[ch].spl1 + ((nib&8) ? -diff : diff);
|
||||||
|
|
||||||
|
if (v<-32768)v=-32768;
|
||||||
|
else if (v>32767)v=32767;
|
||||||
|
|
||||||
|
outptr[wrpos]=(short)v;
|
||||||
|
wrpos+=nch;
|
||||||
|
|
||||||
|
m_chans[ch].spl1=v;
|
||||||
|
|
||||||
|
m_chans[ch].cf1=step_index + index_table[nib&7];
|
||||||
|
|
||||||
|
|
||||||
|
// advance channelcounts
|
||||||
|
if (++cnt==chunksize)
|
||||||
|
{
|
||||||
|
if (++ch>=nch)
|
||||||
|
{
|
||||||
|
ch=0;
|
||||||
|
outptr += chunksize*nch;
|
||||||
|
}
|
||||||
|
wrpos = ch;
|
||||||
|
cnt=0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DecodeBlock(unsigned char *buf, short *parm_cotab=NULL)
|
||||||
|
{
|
||||||
|
if (m_type==IMAADPCM_TYPE||m_type==CADPCM2_TYPE) return DecodeBlockIMA(buf);
|
||||||
|
static short cotab[14] = { 256,0, 512,-256, 0,0, 192,64, 240, 0,460, -208, 392, -232 };
|
||||||
|
static short adtab[16] = { 230, 230, 230, 230, 307, 409, 512, 614, 768, 614, 512, 409, 307, 230, 230, 230 };
|
||||||
|
|
||||||
|
short *use_cotab = parm_cotab ? parm_cotab : cotab;
|
||||||
|
int nch = m_nch;
|
||||||
|
int ch;
|
||||||
|
for(ch=0;ch<nch;ch++)
|
||||||
|
{
|
||||||
|
unsigned char c=*buf++;
|
||||||
|
if (c > 6) return false;
|
||||||
|
c*=2;
|
||||||
|
m_chans[ch].cf1 = use_cotab[c];
|
||||||
|
m_chans[ch].cf2 = use_cotab[c+1];
|
||||||
|
}
|
||||||
|
for(ch=0;ch<nch;ch++) m_chans[ch].deltas = getwordsigned(&buf);
|
||||||
|
for(ch=0;ch<nch;ch++) m_chans[ch].spl1 = getwordsigned(&buf);
|
||||||
|
for(ch=0;ch<nch;ch++) m_chans[ch].spl2 = getwordsigned(&buf);
|
||||||
|
|
||||||
|
int samples_block = samplesPerBlock();
|
||||||
|
|
||||||
|
short *outptr = samplesOut.Add(NULL,samples_block * nch);
|
||||||
|
|
||||||
|
for(ch=0;ch<nch;ch++) *outptr++ = m_chans[ch].spl2;
|
||||||
|
for(ch=0;ch<nch;ch++) *outptr++ = m_chans[ch].spl1;
|
||||||
|
|
||||||
|
int x;
|
||||||
|
char bstate=0;
|
||||||
|
unsigned char lastbyte;
|
||||||
|
for (x=2; x < samples_block; x++)
|
||||||
|
{
|
||||||
|
for(ch=0;ch<nch;ch++)
|
||||||
|
{
|
||||||
|
int nib;
|
||||||
|
if ((bstate^=1)) nib=(lastbyte=*buf++)>>4;
|
||||||
|
else nib=lastbyte&0xf;
|
||||||
|
|
||||||
|
int sn=nib;
|
||||||
|
if (sn & 8) sn -= 16;
|
||||||
|
|
||||||
|
int pred = ( ((m_chans[ch].spl1 * m_chans[ch].cf1) +
|
||||||
|
(m_chans[ch].spl2 * m_chans[ch].cf2)) / 256) +
|
||||||
|
(sn * m_chans[ch].deltas);
|
||||||
|
|
||||||
|
m_chans[ch].spl2 = m_chans[ch].spl1;
|
||||||
|
|
||||||
|
if (pred < -32768) pred=-32768;
|
||||||
|
else if (pred > 32767) pred=32767;
|
||||||
|
|
||||||
|
*outptr++ = m_chans[ch].spl1 = pred;
|
||||||
|
|
||||||
|
int i= (adtab[nib] * m_chans[ch].deltas) / 256;
|
||||||
|
if (i <= 16) m_chans[ch].deltas=16;
|
||||||
|
else m_chans[ch].deltas = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
WDL_adpcm_decode_chanctx *m_chans;
|
||||||
|
unsigned char *m_srcbuf;
|
||||||
|
int m_srcbuf_valid;
|
||||||
|
|
||||||
|
int m_blockalign,m_nch,m_type,m_bps;
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
137
oversampling/WDL/adpcm_encode.h
Normal file
137
oversampling/WDL/adpcm_encode.h
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
#ifndef _WDL_ADPCM_ENCODE_H_
|
||||||
|
#define _WDL_ADPCM_ENCODE_H_
|
||||||
|
|
||||||
|
|
||||||
|
#include "pcmfmtcvt.h"
|
||||||
|
|
||||||
|
void WDL_adpcm_encode_IMA(PCMFMTCVT_DBL_TYPE *samples, int numsamples, int nch, int bps,
|
||||||
|
unsigned char *bufout, int *bufout_used, short **predState);
|
||||||
|
|
||||||
|
#define WDL_adpcm_encode_IMA_samplesneededbytes(bytes,bps) ((((bytes)-4)*8)/(bps)+1)
|
||||||
|
|
||||||
|
|
||||||
|
// untested. also probably slow.
|
||||||
|
#ifdef WDL_ADPCM_ENCODE_IMPL
|
||||||
|
|
||||||
|
|
||||||
|
static signed char ima_adpcm_index_table[8] = { -1, -1, -1, -1, 2, 4, 6, 8, };
|
||||||
|
static short ima_adpcm_step_table[89] = {
|
||||||
|
7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
|
||||||
|
19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
|
||||||
|
50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
|
||||||
|
130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
|
||||||
|
337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
|
||||||
|
876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
|
||||||
|
2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
|
||||||
|
5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
|
||||||
|
15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
|
||||||
|
};
|
||||||
|
|
||||||
|
static char calcBestNibble(int *initial_step_index, int lastSpl, int thisSpl, short *lastsplout, int bps)
|
||||||
|
{
|
||||||
|
|
||||||
|
int step=ima_adpcm_step_table[*initial_step_index];
|
||||||
|
|
||||||
|
char sign=0;
|
||||||
|
int adiff = thisSpl - lastSpl;
|
||||||
|
if (adiff<0) { adiff=-adiff; sign=8; }
|
||||||
|
|
||||||
|
// adiff == (nib*step)/4 + step/8
|
||||||
|
// adiff - step/8 = nib*step/4
|
||||||
|
// nib = 4*(adiff-step/8)/step = 4*adiff/step - 0.5
|
||||||
|
int nib = step ? ((4 * adiff - step/2) / step) : 0;
|
||||||
|
if (nib<0) nib=0;
|
||||||
|
else if(nib>7) nib=7;
|
||||||
|
|
||||||
|
if (bps==2) nib&=4;
|
||||||
|
|
||||||
|
int diff = (nib*step)/4 + step/8;
|
||||||
|
*lastsplout = lastSpl + (sign?-diff:diff);
|
||||||
|
|
||||||
|
|
||||||
|
*initial_step_index += ima_adpcm_index_table[nib];
|
||||||
|
|
||||||
|
if (*initial_step_index<0)*initial_step_index=0;
|
||||||
|
else if (*initial_step_index>88)*initial_step_index=88;
|
||||||
|
|
||||||
|
return (char)nib|sign;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void WDL_adpcm_encode_IMA(PCMFMTCVT_DBL_TYPE *samples, int numsamples, int nch, int bps,
|
||||||
|
unsigned char *bufout, int *bufout_used, short **predState)
|
||||||
|
{
|
||||||
|
int x;
|
||||||
|
if (!*predState) *predState=(short *)calloc(nch,sizeof(short));
|
||||||
|
|
||||||
|
short *pstate = *predState;
|
||||||
|
for(x=0;x<nch;x++)
|
||||||
|
{
|
||||||
|
int left=numsamples;
|
||||||
|
PCMFMTCVT_DBL_TYPE *spl = samples+x;
|
||||||
|
unsigned char *wrptr = bufout + x*4;
|
||||||
|
|
||||||
|
int step_index=pstate[x];
|
||||||
|
|
||||||
|
short last_out;
|
||||||
|
double_TO_INT16(last_out,*spl);
|
||||||
|
left--;
|
||||||
|
|
||||||
|
*wrptr++ = last_out&0xff; *wrptr++ = last_out>>8;
|
||||||
|
spl+=nch;
|
||||||
|
|
||||||
|
*wrptr++ = step_index&0xff; *wrptr++ = step_index>>8;
|
||||||
|
|
||||||
|
wrptr += (nch-1)*4;
|
||||||
|
|
||||||
|
|
||||||
|
int outpos=0;
|
||||||
|
const int outblocklen = bps == 2 ? 16 : 8;
|
||||||
|
unsigned char buildchar=0;
|
||||||
|
|
||||||
|
while (left-->0)
|
||||||
|
{
|
||||||
|
short this_spl;
|
||||||
|
double_TO_INT16(this_spl,*spl);
|
||||||
|
char nib = calcBestNibble(&step_index,last_out,this_spl,&last_out,bps);
|
||||||
|
|
||||||
|
spl+=nch;
|
||||||
|
|
||||||
|
// update output
|
||||||
|
if (bps == 2)
|
||||||
|
{
|
||||||
|
nib>>=2;
|
||||||
|
switch (outpos&3)
|
||||||
|
{
|
||||||
|
case 0: buildchar = nib; break;
|
||||||
|
case 1: buildchar |= nib<<2; break;
|
||||||
|
case 2: buildchar |= nib<<4; break;
|
||||||
|
case 3: *wrptr++ = buildchar | (nib<<6); break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (!(outpos&1)) buildchar = nib;
|
||||||
|
else *wrptr++ = buildchar | (nib<<4);
|
||||||
|
}
|
||||||
|
|
||||||
|
// skip other channels
|
||||||
|
if (++outpos == outblocklen)
|
||||||
|
{
|
||||||
|
wrptr += ((nch-1)*outblocklen*bps)/8;
|
||||||
|
outpos=0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pstate[x] = step_index;
|
||||||
|
|
||||||
|
}
|
||||||
|
*bufout_used = (((numsamples-1)*bps)/8 + 4)*nch;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#endif//_WDL_ADPCM_ENCODE_H_
|
||||||
491
oversampling/WDL/assocarray.h
Normal file
491
oversampling/WDL/assocarray.h
Normal file
@@ -0,0 +1,491 @@
|
|||||||
|
#ifndef _WDL_ASSOCARRAY_H_
|
||||||
|
#define _WDL_ASSOCARRAY_H_
|
||||||
|
|
||||||
|
#include "heapbuf.h"
|
||||||
|
#include "mergesort.h"
|
||||||
|
#include "wdlcstring.h"
|
||||||
|
|
||||||
|
// on all of these, if valdispose is set, the array will dispose of values as needed.
|
||||||
|
// if keydup/keydispose are set, copies of (any) key data will be made/destroyed as necessary
|
||||||
|
|
||||||
|
|
||||||
|
// WDL_AssocArrayImpl can be used on its own, and can contain structs for keys or values
|
||||||
|
template <class KEY, class VAL> class WDL_AssocArrayImpl
|
||||||
|
{
|
||||||
|
WDL_AssocArrayImpl(const WDL_AssocArrayImpl &cp) { CopyContents(cp); }
|
||||||
|
|
||||||
|
WDL_AssocArrayImpl &operator=(const WDL_AssocArrayImpl &cp) { CopyContents(cp); return *this; }
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
explicit WDL_AssocArrayImpl(int (*keycmp)(KEY *k1, KEY *k2),
|
||||||
|
KEY (*keydup)(KEY)=NULL,
|
||||||
|
void (*keydispose)(KEY)=NULL,
|
||||||
|
void (*valdispose)(VAL)=NULL)
|
||||||
|
{
|
||||||
|
m_keycmp = keycmp;
|
||||||
|
m_keydup = keydup;
|
||||||
|
m_keydispose = keydispose;
|
||||||
|
m_valdispose = valdispose;
|
||||||
|
}
|
||||||
|
|
||||||
|
~WDL_AssocArrayImpl()
|
||||||
|
{
|
||||||
|
DeleteAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
VAL* GetPtr(KEY key, KEY *keyPtrOut=NULL) const
|
||||||
|
{
|
||||||
|
bool ismatch = false;
|
||||||
|
int i = LowerBound(key, &ismatch);
|
||||||
|
if (ismatch)
|
||||||
|
{
|
||||||
|
KeyVal* kv = m_data.Get()+i;
|
||||||
|
if (keyPtrOut) *keyPtrOut = kv->key;
|
||||||
|
return &(kv->val);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Exists(KEY key) const
|
||||||
|
{
|
||||||
|
bool ismatch = false;
|
||||||
|
LowerBound(key, &ismatch);
|
||||||
|
return ismatch;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Insert(KEY key, VAL val)
|
||||||
|
{
|
||||||
|
bool ismatch = false;
|
||||||
|
int i = LowerBound(key, &ismatch);
|
||||||
|
if (ismatch)
|
||||||
|
{
|
||||||
|
KeyVal* kv = m_data.Get()+i;
|
||||||
|
if (m_valdispose) m_valdispose(kv->val);
|
||||||
|
kv->val = val;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
KeyVal* kv = m_data.Resize(m_data.GetSize()+1)+i;
|
||||||
|
memmove(kv+1, kv, (m_data.GetSize()-i-1)*(unsigned int)sizeof(KeyVal));
|
||||||
|
if (m_keydup) key = m_keydup(key);
|
||||||
|
kv->key = key;
|
||||||
|
kv->val = val;
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Delete(KEY key)
|
||||||
|
{
|
||||||
|
bool ismatch = false;
|
||||||
|
int i = LowerBound(key, &ismatch);
|
||||||
|
if (ismatch)
|
||||||
|
{
|
||||||
|
KeyVal* kv = m_data.Get()+i;
|
||||||
|
if (m_keydispose) m_keydispose(kv->key);
|
||||||
|
if (m_valdispose) m_valdispose(kv->val);
|
||||||
|
m_data.Delete(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void DeleteByIndex(int idx)
|
||||||
|
{
|
||||||
|
if (idx >= 0 && idx < m_data.GetSize())
|
||||||
|
{
|
||||||
|
KeyVal* kv = m_data.Get()+idx;
|
||||||
|
if (m_keydispose) m_keydispose(kv->key);
|
||||||
|
if (m_valdispose) m_valdispose(kv->val);
|
||||||
|
m_data.Delete(idx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void DeleteAll(bool resizedown=false)
|
||||||
|
{
|
||||||
|
if (m_keydispose || m_valdispose)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < m_data.GetSize(); ++i)
|
||||||
|
{
|
||||||
|
KeyVal* kv = m_data.Get()+i;
|
||||||
|
if (m_keydispose) m_keydispose(kv->key);
|
||||||
|
if (m_valdispose) m_valdispose(kv->val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_data.Resize(0, resizedown);
|
||||||
|
}
|
||||||
|
|
||||||
|
int GetSize() const
|
||||||
|
{
|
||||||
|
return m_data.GetSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
VAL* EnumeratePtr(int i, KEY* key=NULL) const
|
||||||
|
{
|
||||||
|
if (i >= 0 && i < m_data.GetSize())
|
||||||
|
{
|
||||||
|
KeyVal* kv = m_data.Get()+i;
|
||||||
|
if (key) *key = kv->key;
|
||||||
|
return &(kv->val);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
KEY* ReverseLookupPtr(VAL val) const
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < m_data.GetSize(); ++i)
|
||||||
|
{
|
||||||
|
KeyVal* kv = m_data.Get()+i;
|
||||||
|
if (kv->val == val) return &kv->key;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ChangeKey(KEY oldkey, KEY newkey)
|
||||||
|
{
|
||||||
|
bool ismatch=false;
|
||||||
|
int i=LowerBound(oldkey, &ismatch);
|
||||||
|
if (ismatch) ChangeKeyByIndex(i, newkey, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ChangeKeyByIndex(int idx, KEY newkey, bool needsort)
|
||||||
|
{
|
||||||
|
if (idx >= 0 && idx < m_data.GetSize())
|
||||||
|
{
|
||||||
|
KeyVal* kv=m_data.Get()+idx;
|
||||||
|
if (!needsort)
|
||||||
|
{
|
||||||
|
if (m_keydispose) m_keydispose(kv->key);
|
||||||
|
if (m_keydup) newkey=m_keydup(newkey);
|
||||||
|
kv->key=newkey;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
VAL val=kv->val;
|
||||||
|
m_data.Delete(idx);
|
||||||
|
Insert(newkey, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// fast add-block mode
|
||||||
|
void AddUnsorted(KEY key, VAL val)
|
||||||
|
{
|
||||||
|
int i=m_data.GetSize();
|
||||||
|
KeyVal* kv = m_data.Resize(i+1)+i;
|
||||||
|
if (m_keydup) key = m_keydup(key);
|
||||||
|
kv->key = key;
|
||||||
|
kv->val = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Resort(int (*new_keycmp)(KEY *k1, KEY *k2)=NULL)
|
||||||
|
{
|
||||||
|
if (new_keycmp) m_keycmp = new_keycmp;
|
||||||
|
if (m_data.GetSize() > 1 && m_keycmp)
|
||||||
|
{
|
||||||
|
qsort(m_data.Get(), m_data.GetSize(), sizeof(KeyVal),
|
||||||
|
(int(*)(const void*, const void*))m_keycmp);
|
||||||
|
if (!new_keycmp)
|
||||||
|
RemoveDuplicateKeys();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ResortStable()
|
||||||
|
{
|
||||||
|
if (m_data.GetSize() > 1 && m_keycmp)
|
||||||
|
{
|
||||||
|
char *tmp=(char*)malloc(m_data.GetSize()*sizeof(KeyVal));
|
||||||
|
if (WDL_NORMALLY(tmp))
|
||||||
|
{
|
||||||
|
WDL_mergesort(m_data.Get(), m_data.GetSize(), sizeof(KeyVal),
|
||||||
|
(int(*)(const void*, const void*))m_keycmp, tmp);
|
||||||
|
free(tmp);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
qsort(m_data.Get(), m_data.GetSize(), sizeof(KeyVal),
|
||||||
|
(int(*)(const void*, const void*))m_keycmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
RemoveDuplicateKeys();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int LowerBound(KEY key, bool* ismatch) const
|
||||||
|
{
|
||||||
|
int a = 0;
|
||||||
|
int c = m_data.GetSize();
|
||||||
|
while (a != c)
|
||||||
|
{
|
||||||
|
int b = (a+c)/2;
|
||||||
|
KeyVal* kv=m_data.Get()+b;
|
||||||
|
int cmp = m_keycmp(&key, &kv->key);
|
||||||
|
if (cmp > 0) a = b+1;
|
||||||
|
else if (cmp < 0) c = b;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
*ismatch = true;
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*ismatch = false;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
int GetIdx(KEY key) const
|
||||||
|
{
|
||||||
|
bool ismatch=false;
|
||||||
|
int i = LowerBound(key, &ismatch);
|
||||||
|
if (ismatch) return i;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetGranul(int gran)
|
||||||
|
{
|
||||||
|
m_data.SetGranul(gran);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CopyContents(const WDL_AssocArrayImpl &cp)
|
||||||
|
{
|
||||||
|
m_data=cp.m_data;
|
||||||
|
m_keycmp = cp.m_keycmp;
|
||||||
|
m_keydup = cp.m_keydup;
|
||||||
|
m_keydispose = m_keydup ? cp.m_keydispose : NULL;
|
||||||
|
m_valdispose = NULL; // avoid disposing of values twice, since we don't have a valdup, we can't have a fully valid copy
|
||||||
|
if (m_keydup)
|
||||||
|
{
|
||||||
|
const int n=m_data.GetSize();
|
||||||
|
for (int x=0;x<n;x++)
|
||||||
|
{
|
||||||
|
KeyVal *kv=m_data.Get()+x;
|
||||||
|
kv->key = m_keydup(kv->key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CopyContentsAsReference(const WDL_AssocArrayImpl &cp)
|
||||||
|
{
|
||||||
|
DeleteAll(true);
|
||||||
|
m_keycmp = cp.m_keycmp;
|
||||||
|
m_keydup = NULL; // this no longer can own any data
|
||||||
|
m_keydispose = NULL;
|
||||||
|
m_valdispose = NULL;
|
||||||
|
|
||||||
|
m_data=cp.m_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// private data, but exposed in case the caller wants to manipulate at its own risk
|
||||||
|
struct KeyVal
|
||||||
|
{
|
||||||
|
KEY key;
|
||||||
|
VAL val;
|
||||||
|
};
|
||||||
|
WDL_TypedBuf<KeyVal> m_data;
|
||||||
|
|
||||||
|
static int keycmp_ptr(KEY *a, KEY *b) { return (INT_PTR)*a > (INT_PTR)*b ? 1 : (INT_PTR)*a < (INT_PTR)*b ? -1 : 0; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
int (*m_keycmp)(KEY *k1, KEY *k2);
|
||||||
|
KEY (*m_keydup)(KEY);
|
||||||
|
void (*m_keydispose)(KEY);
|
||||||
|
void (*m_valdispose)(VAL);
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
void RemoveDuplicateKeys() // after resorting
|
||||||
|
{
|
||||||
|
const int sz = m_data.GetSize();
|
||||||
|
|
||||||
|
int cnt = 1;
|
||||||
|
KeyVal *rd = m_data.Get() + 1, *wr = rd;
|
||||||
|
for (int x = 1; x < sz; x ++)
|
||||||
|
{
|
||||||
|
if (m_keycmp(&rd->key, &wr[-1].key))
|
||||||
|
{
|
||||||
|
if (rd != wr) *wr=*rd;
|
||||||
|
wr++;
|
||||||
|
cnt++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (m_keydispose) m_keydispose(rd->key);
|
||||||
|
if (m_valdispose) m_valdispose(rd->val);
|
||||||
|
}
|
||||||
|
rd++;
|
||||||
|
}
|
||||||
|
if (cnt < sz) m_data.Resize(cnt,false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// WDL_AssocArray adds useful functions but cannot contain structs for keys or values
|
||||||
|
template <class KEY, class VAL> class WDL_AssocArray : public WDL_AssocArrayImpl<KEY, VAL>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
explicit WDL_AssocArray(int (*keycmp)(KEY *k1, KEY *k2),
|
||||||
|
KEY (*keydup)(KEY)=NULL,
|
||||||
|
void (*keydispose)(KEY)=NULL, void (*valdispose)(VAL)=NULL)
|
||||||
|
: WDL_AssocArrayImpl<KEY, VAL>(keycmp, keydup, keydispose, valdispose)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
VAL Get(KEY key, VAL notfound=0) const
|
||||||
|
{
|
||||||
|
VAL* p = this->GetPtr(key);
|
||||||
|
if (p) return *p;
|
||||||
|
return notfound;
|
||||||
|
}
|
||||||
|
|
||||||
|
VAL Enumerate(int i, KEY* key=NULL, VAL notfound=0) const
|
||||||
|
{
|
||||||
|
VAL* p = this->EnumeratePtr(i, key);
|
||||||
|
if (p) return *p;
|
||||||
|
return notfound;
|
||||||
|
}
|
||||||
|
|
||||||
|
KEY ReverseLookup(VAL val, KEY notfound=0) const
|
||||||
|
{
|
||||||
|
KEY* p=this->ReverseLookupPtr(val);
|
||||||
|
if (p) return *p;
|
||||||
|
return notfound;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template <class VAL> class WDL_IntKeyedArray : public WDL_AssocArray<int, VAL>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
explicit WDL_IntKeyedArray(void (*valdispose)(VAL)=NULL)
|
||||||
|
: WDL_AssocArray<int, VAL>(cmpint, NULL, NULL, valdispose) {}
|
||||||
|
|
||||||
|
static int cmpint(int *a, int *b) { return *a > *b ? 1 : *a < *b ? -1 : 0; }
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class VAL> class WDL_IntKeyedArray2 : public WDL_AssocArrayImpl<int, VAL>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
explicit WDL_IntKeyedArray2(void (*valdispose)(VAL)=NULL)
|
||||||
|
: WDL_AssocArrayImpl<int, VAL>(cmpint, NULL, NULL, valdispose) {}
|
||||||
|
|
||||||
|
static int cmpint(int *a, int *b) { return *a > *b ? 1 : *a < *b ? -1 : 0; }
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class VAL> class WDL_StringKeyedArray : public WDL_AssocArray<const char *, VAL>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
explicit WDL_StringKeyedArray(bool caseSensitive=true, void (*valdispose)(VAL)=NULL)
|
||||||
|
: WDL_AssocArray<const char*, VAL>(caseSensitive?cmpstr:cmpistr, dupstr, freestr, valdispose) {}
|
||||||
|
|
||||||
|
static const char *dupstr(const char *s) { return strdup(s); } // these might not be necessary but depending on the libc maybe...
|
||||||
|
static int cmpstr(const char **s1, const char **s2) { return strcmp(*s1, *s2); }
|
||||||
|
static int cmpistr(const char **a, const char **b) { return stricmp(*a,*b); }
|
||||||
|
static void freestr(const char* s) { free((void*)s); }
|
||||||
|
static void freecharptr(char *p) { free(p); }
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template <class VAL> class WDL_StringKeyedArray2 : public WDL_AssocArrayImpl<const char *, VAL>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
explicit WDL_StringKeyedArray2(bool caseSensitive=true, void (*valdispose)(VAL)=NULL)
|
||||||
|
: WDL_AssocArrayImpl<const char*, VAL>(caseSensitive?cmpstr:cmpistr, dupstr, freestr, valdispose) {}
|
||||||
|
|
||||||
|
~WDL_StringKeyedArray2() { }
|
||||||
|
|
||||||
|
static const char *dupstr(const char *s) { return strdup(s); } // these might not be necessary but depending on the libc maybe...
|
||||||
|
static int cmpstr(const char **s1, const char **s2) { return strcmp(*s1, *s2); }
|
||||||
|
static int cmpistr(const char **a, const char **b) { return stricmp(*a,*b); }
|
||||||
|
static void freestr(const char* s) { free((void*)s); }
|
||||||
|
static void freecharptr(char *p) { free(p); }
|
||||||
|
};
|
||||||
|
|
||||||
|
// sorts text as text, sorts anything that looks like a number as a number
|
||||||
|
template <class VAL> class WDL_LogicalSortStringKeyedArray : public WDL_StringKeyedArray<VAL>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
explicit WDL_LogicalSortStringKeyedArray(bool caseSensitive=true, void (*valdispose)(VAL)=NULL)
|
||||||
|
: WDL_StringKeyedArray<VAL>(caseSensitive, valdispose)
|
||||||
|
{
|
||||||
|
WDL_StringKeyedArray<VAL>::m_keycmp = caseSensitive?cmpstr:cmpistr; // override
|
||||||
|
}
|
||||||
|
|
||||||
|
~WDL_LogicalSortStringKeyedArray() { }
|
||||||
|
|
||||||
|
static int cmpstr(const char **a, const char **b)
|
||||||
|
{
|
||||||
|
int r=WDL_strcmp_logical_ex(*a, *b, 1, WDL_STRCMP_LOGICAL_EX_FLAG_UTF8CONVERT);
|
||||||
|
return r?r:strcmp(*a,*b);
|
||||||
|
}
|
||||||
|
static int cmpistr(const char **a, const char **b)
|
||||||
|
{
|
||||||
|
int r=WDL_strcmp_logical_ex(*a, *b, 0, WDL_STRCMP_LOGICAL_EX_FLAG_UTF8CONVERT);
|
||||||
|
return r?r:stricmp(*a,*b);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template <class VAL> class WDL_PtrKeyedArray : public WDL_AssocArray<INT_PTR, VAL>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit WDL_PtrKeyedArray(void (*valdispose)(VAL)=NULL)
|
||||||
|
: WDL_AssocArray<INT_PTR, VAL>(WDL_AssocArray<INT_PTR, VAL>::keycmp_ptr, NULL, NULL, valdispose) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class KEY, class VAL> class WDL_PointerKeyedArray : public WDL_AssocArray<KEY, VAL>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit WDL_PointerKeyedArray(void (*valdispose)(VAL)=NULL)
|
||||||
|
: WDL_AssocArray<KEY, VAL>(WDL_AssocArray<KEY, VAL>::keycmp_ptr, NULL, NULL, valdispose) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct WDL_Set_DummyRec { };
|
||||||
|
template <class KEY> class WDL_Set : public WDL_AssocArrayImpl<KEY,WDL_Set_DummyRec>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit WDL_Set(int (*keycmp)(KEY *k1, KEY *k2),
|
||||||
|
KEY (*keydup)(KEY)=NULL,
|
||||||
|
void (*keydispose)(KEY)=NULL
|
||||||
|
)
|
||||||
|
: WDL_AssocArrayImpl<KEY, WDL_Set_DummyRec>(keycmp,keydup,keydispose)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
int Insert(KEY key)
|
||||||
|
{
|
||||||
|
WDL_Set_DummyRec r;
|
||||||
|
return WDL_AssocArrayImpl<KEY, WDL_Set_DummyRec>::Insert(key,r);
|
||||||
|
}
|
||||||
|
void AddUnsorted(KEY key)
|
||||||
|
{
|
||||||
|
WDL_Set_DummyRec r;
|
||||||
|
WDL_AssocArrayImpl<KEY, WDL_Set_DummyRec>::AddUnsorted(key,r);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Get(KEY key) const
|
||||||
|
{
|
||||||
|
return WDL_AssocArrayImpl<KEY, WDL_Set_DummyRec>::Exists(key);
|
||||||
|
}
|
||||||
|
bool Enumerate(int i, KEY *key=NULL)
|
||||||
|
{
|
||||||
|
return WDL_AssocArrayImpl<KEY, WDL_Set_DummyRec>::EnumeratePtr(i,key) != NULL;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class KEY> class WDL_PtrSet : public WDL_Set<KEY>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit WDL_PtrSet() : WDL_Set<KEY>( WDL_AssocArray<KEY, WDL_Set_DummyRec>::keycmp_ptr ) { }
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
243
oversampling/WDL/audiobuffercontainer.cpp
Normal file
243
oversampling/WDL/audiobuffercontainer.cpp
Normal file
@@ -0,0 +1,243 @@
|
|||||||
|
#include "audiobuffercontainer.h"
|
||||||
|
#include "queue.h"
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
void ChannelPinMapper::Reset()
|
||||||
|
{
|
||||||
|
for (int i=0; i < CHANNELPINMAPPER_MAXPINS; ++i)
|
||||||
|
m_mapping[i].set_excl(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ChannelPinMapper::SetNPins(int nPins)
|
||||||
|
{
|
||||||
|
if (nPins<0) nPins=0;
|
||||||
|
else if (nPins>CHANNELPINMAPPER_MAXPINS) nPins=CHANNELPINMAPPER_MAXPINS;
|
||||||
|
int i;
|
||||||
|
for (i = m_nPins; i < nPins; ++i)
|
||||||
|
{
|
||||||
|
ClearPin(i);
|
||||||
|
if (i < m_nCh)
|
||||||
|
{
|
||||||
|
SetPin(i, i, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_nPins = nPins;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ChannelPinMapper::SetNChannels(int nCh, bool auto_passthru)
|
||||||
|
{
|
||||||
|
if (auto_passthru) for (int i = m_nCh; i < nCh && i < m_nPins; ++i) {
|
||||||
|
SetPin(i, i, true);
|
||||||
|
}
|
||||||
|
m_nCh = nCh;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ChannelPinMapper::Init(const PinMapPin * pMapping, int nPins)
|
||||||
|
{
|
||||||
|
if (nPins<0) nPins=0;
|
||||||
|
else if (nPins>CHANNELPINMAPPER_MAXPINS) nPins=CHANNELPINMAPPER_MAXPINS;
|
||||||
|
memcpy(m_mapping, pMapping, nPins*sizeof(PinMapPin));
|
||||||
|
memset(m_mapping+nPins, 0, (CHANNELPINMAPPER_MAXPINS-nPins)*sizeof(PinMapPin));
|
||||||
|
m_nPins = m_nCh = nPins;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define BITMASK64(bitIdx) (((WDL_UINT64)1)<<(bitIdx))
|
||||||
|
|
||||||
|
void ChannelPinMapper::ClearPin(int pinIdx)
|
||||||
|
{
|
||||||
|
if (pinIdx >=0 && pinIdx < CHANNELPINMAPPER_MAXPINS) m_mapping[pinIdx].clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ChannelPinMapper::SetPin(int pinIdx, int chIdx, bool on)
|
||||||
|
{
|
||||||
|
if (pinIdx >=0 && pinIdx < CHANNELPINMAPPER_MAXPINS)
|
||||||
|
{
|
||||||
|
if (on)
|
||||||
|
{
|
||||||
|
m_mapping[pinIdx].set_chan(chIdx);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_mapping[pinIdx].clear_chan(chIdx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ChannelPinMapper::TogglePin(int pinIdx, int chIdx)
|
||||||
|
{
|
||||||
|
bool on = GetPin(pinIdx, chIdx);
|
||||||
|
on = !on;
|
||||||
|
SetPin(pinIdx, chIdx, on);
|
||||||
|
return on;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ChannelPinMapper::GetPin(int pinIdx, int chIdx) const
|
||||||
|
{
|
||||||
|
if (pinIdx >= 0 && pinIdx < CHANNELPINMAPPER_MAXPINS)
|
||||||
|
{
|
||||||
|
return m_mapping[pinIdx].has_chan(chIdx);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ChannelPinMapper::IsStraightPassthrough() const
|
||||||
|
{
|
||||||
|
if (m_nCh != m_nPins) return false;
|
||||||
|
PinMapPin tmp;
|
||||||
|
tmp.clear();
|
||||||
|
for (int i = 0; i < m_nPins; ++i)
|
||||||
|
{
|
||||||
|
tmp.set_chan(i);
|
||||||
|
if (!tmp.equal_to(m_mapping[i])) return false;
|
||||||
|
tmp.clear_chan(i);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define PINMAPPER_MAGIC 1000
|
||||||
|
|
||||||
|
const char *ChannelPinMapper::SaveStateNew(int* pLen)
|
||||||
|
{
|
||||||
|
m_cfgret.Clear();
|
||||||
|
int magic = PINMAPPER_MAGIC;
|
||||||
|
WDL_Queue__AddToLE(&m_cfgret, &magic);
|
||||||
|
WDL_Queue__AddToLE(&m_cfgret, &m_nCh);
|
||||||
|
WDL_Queue__AddToLE(&m_cfgret, &m_nPins);
|
||||||
|
const int num64 = wdl_max(1,(wdl_min(m_nCh,CHANNELPINMAPPER_MAXPINS) + 63)/64);
|
||||||
|
for (int y = 0; y < num64; y ++)
|
||||||
|
{
|
||||||
|
for (int x = 0; x < m_nPins; x ++)
|
||||||
|
{
|
||||||
|
const WDL_UINT64 v = m_mapping[x].get_64(y);
|
||||||
|
WDL_Queue__AddToLE(&m_cfgret, &v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*pLen = m_cfgret.GetSize();
|
||||||
|
return (const char*)m_cfgret.Get();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ChannelPinMapper::LoadState(const char* buf, int len)
|
||||||
|
{
|
||||||
|
WDL_Queue chunk;
|
||||||
|
chunk.Add(buf, len);
|
||||||
|
int* pMagic = WDL_Queue__GetTFromLE(&chunk, (int*)0);
|
||||||
|
if (!pMagic || *pMagic != PINMAPPER_MAGIC) return false;
|
||||||
|
int* pNCh = WDL_Queue__GetTFromLE(&chunk, (int*) 0);
|
||||||
|
int* pNPins = WDL_Queue__GetTFromLE(&chunk, (int*) 0);
|
||||||
|
if (!pNCh || !pNPins) return false;
|
||||||
|
const int src_pins = *pNPins;
|
||||||
|
SetNPins(src_pins);
|
||||||
|
SetNChannels(*pNCh);
|
||||||
|
const int num64 = wdl_max(1,(wdl_min(m_nCh,CHANNELPINMAPPER_MAXPINS)+63)/64);
|
||||||
|
const int maplen = src_pins * sizeof(WDL_UINT64);
|
||||||
|
for (int y = 0; y < num64; y ++)
|
||||||
|
{
|
||||||
|
if (chunk.Available() < maplen) return y>0;
|
||||||
|
const WDL_UINT64 *pMap = (const WDL_UINT64 *)WDL_Queue__GetDataFromLE(&chunk, maplen, sizeof(WDL_UINT64));
|
||||||
|
const int sz = wdl_min(m_nPins,src_pins);
|
||||||
|
for (int x = 0; x < sz; x ++)
|
||||||
|
{
|
||||||
|
m_mapping[x].set_64(pMap[x], y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
AudioBufferContainer::AudioBufferContainer()
|
||||||
|
{
|
||||||
|
m_nCh = 0;
|
||||||
|
m_nFrames = 0;
|
||||||
|
m_fmt = FMT_32FP;
|
||||||
|
m_interleaved = true;
|
||||||
|
m_hasData = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// converts interleaved buffer to interleaved buffer, using min(len_in,len_out) and zeroing any extra samples
|
||||||
|
// isInput means it reads from track channels and writes to plugin pins
|
||||||
|
// wantZeroExcessOutput=false means that untouched channels will be preserved in buf_out
|
||||||
|
void PinMapperConvertBuffers(const double *buf, int len_in, int nch_in,
|
||||||
|
double *buf_out, int len_out, int nch_out,
|
||||||
|
const ChannelPinMapper *pinmap, bool isInput, bool wantZeroExcessOutput)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (pinmap->IsStraightPassthrough() || !pinmap->GetNPins())
|
||||||
|
{
|
||||||
|
int x;
|
||||||
|
char *op = (char *)buf_out;
|
||||||
|
const char *ip = (const char *)buf;
|
||||||
|
|
||||||
|
const int ip_adv = nch_in * sizeof(double);
|
||||||
|
|
||||||
|
const int clen = wdl_min(nch_in, nch_out) * sizeof(double);
|
||||||
|
const int zlen = nch_out > nch_in ? (nch_out - nch_in) * sizeof(double) : 0;
|
||||||
|
|
||||||
|
const int cplen = wdl_min(len_in,len_out);
|
||||||
|
|
||||||
|
for (x=0;x<cplen;x++)
|
||||||
|
{
|
||||||
|
memcpy(op,ip,clen);
|
||||||
|
op += clen;
|
||||||
|
if (zlen)
|
||||||
|
{
|
||||||
|
if (wantZeroExcessOutput) memset(op,0,zlen);
|
||||||
|
op += zlen;
|
||||||
|
}
|
||||||
|
ip += ip_adv;
|
||||||
|
}
|
||||||
|
if (x < len_out && wantZeroExcessOutput) memset(op, 0, (len_out-x)*sizeof(double)*nch_out);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (wantZeroExcessOutput) memset(buf_out,0,len_out*nch_out*sizeof(double));
|
||||||
|
|
||||||
|
const int npins = wdl_min(pinmap->GetNPins(),isInput ? nch_out : nch_in);
|
||||||
|
const int nchan = isInput ? nch_in : nch_out;
|
||||||
|
|
||||||
|
int p;
|
||||||
|
PinMapPin clearmask;
|
||||||
|
clearmask.clear();
|
||||||
|
for (p = 0; p < npins; p ++)
|
||||||
|
{
|
||||||
|
const PinMapPin &map = pinmap->m_mapping[p];
|
||||||
|
for (unsigned int x = 0; map.enum_chans(&x,nchan); x ++)
|
||||||
|
{
|
||||||
|
int i=len_in;
|
||||||
|
const double *ip = buf + (isInput ? x : p);
|
||||||
|
const int out_idx = (isInput ? p : x);
|
||||||
|
|
||||||
|
bool want_zero=false;
|
||||||
|
if (!wantZeroExcessOutput)
|
||||||
|
{
|
||||||
|
if (!clearmask.has_chan(out_idx))
|
||||||
|
{
|
||||||
|
clearmask.set_chan(out_idx);
|
||||||
|
want_zero=true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
double *op = buf_out + out_idx;
|
||||||
|
|
||||||
|
if (want_zero)
|
||||||
|
{
|
||||||
|
while (i-- > 0)
|
||||||
|
{
|
||||||
|
*op = *ip;
|
||||||
|
op += nch_out;
|
||||||
|
ip += nch_in;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
while (i-- > 0)
|
||||||
|
{
|
||||||
|
*op += *ip;
|
||||||
|
op += nch_out;
|
||||||
|
ip += nch_in;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
228
oversampling/WDL/audiobuffercontainer.h
Normal file
228
oversampling/WDL/audiobuffercontainer.h
Normal file
@@ -0,0 +1,228 @@
|
|||||||
|
#ifndef _AUDIOBUFFERCONTAINER_
|
||||||
|
#define _AUDIOBUFFERCONTAINER_
|
||||||
|
|
||||||
|
#include "wdltypes.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "ptrlist.h"
|
||||||
|
#include "queue.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define CHANNELPINMAPPER_MAXPINS 128
|
||||||
|
|
||||||
|
|
||||||
|
struct PinMapPin
|
||||||
|
{
|
||||||
|
enum { PINMAP_PIN_MAX_CHANNELS = CHANNELPINMAPPER_MAXPINS };
|
||||||
|
|
||||||
|
enum { STATE_ENT_BITS=64, STATE_SIZE=(PINMAP_PIN_MAX_CHANNELS + STATE_ENT_BITS - 1) / STATE_ENT_BITS };
|
||||||
|
WDL_UINT64 state[STATE_SIZE];
|
||||||
|
static WDL_UINT64 make_mask(unsigned int idx) { return WDL_UINT64_CONST(1) << (idx & (STATE_ENT_BITS-1)); }
|
||||||
|
static WDL_UINT64 full_mask() { return ~WDL_UINT64_CONST(0); }
|
||||||
|
|
||||||
|
WDL_UINT64 get_64(unsigned int offs=0) const {
|
||||||
|
return WDL_NORMALLY(offs < STATE_SIZE) ? state[offs] : 0;
|
||||||
|
}
|
||||||
|
void set_64(WDL_UINT64 s, unsigned int offs=0) {
|
||||||
|
if (WDL_NORMALLY(offs < STATE_SIZE)) state[offs]=s;
|
||||||
|
}
|
||||||
|
unsigned int get_64_max() const { return STATE_SIZE; }
|
||||||
|
unsigned int get_64_top(unsigned int minv=0) const {
|
||||||
|
unsigned int x = STATE_SIZE;
|
||||||
|
while (x > minv && !state[x-1]) x--;
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear() { memset(state,0,sizeof(state)); }
|
||||||
|
void clear_chan(unsigned int ch) { if (WDL_NORMALLY(ch < PINMAP_PIN_MAX_CHANNELS)) state[ch/STATE_ENT_BITS] &= ~make_mask(ch); }
|
||||||
|
void set_chan(unsigned int ch) { if (WDL_NORMALLY(ch < PINMAP_PIN_MAX_CHANNELS)) state[ch/STATE_ENT_BITS] |= make_mask(ch); }
|
||||||
|
void tog_chan(unsigned int ch) { if (WDL_NORMALLY(ch < PINMAP_PIN_MAX_CHANNELS)) state[ch/STATE_ENT_BITS] ^= make_mask(ch); }
|
||||||
|
void set_chan_lt(unsigned int cnt)
|
||||||
|
{
|
||||||
|
if (WDL_NOT_NORMALLY(cnt > PINMAP_PIN_MAX_CHANNELS)) cnt = PINMAP_PIN_MAX_CHANNELS;
|
||||||
|
for (int x = 0; cnt && x < STATE_SIZE; x ++)
|
||||||
|
{
|
||||||
|
if (cnt < STATE_ENT_BITS) { state[x] |= make_mask(cnt)-1; cnt=0; }
|
||||||
|
else { state[x] = full_mask(); cnt -= STATE_ENT_BITS; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void set_excl(unsigned int ch) { clear(); set_chan(ch); }
|
||||||
|
|
||||||
|
bool has_chan(unsigned int ch) const { return WDL_NORMALLY(ch < PINMAP_PIN_MAX_CHANNELS) && (state[ch/STATE_ENT_BITS] & make_mask(ch)); }
|
||||||
|
bool has_chan_lt(unsigned int cnt) const
|
||||||
|
{
|
||||||
|
if (WDL_NOT_NORMALLY(cnt > PINMAP_PIN_MAX_CHANNELS)) cnt = PINMAP_PIN_MAX_CHANNELS;
|
||||||
|
for (int x = 0; cnt && x < STATE_SIZE; x ++)
|
||||||
|
{
|
||||||
|
if (cnt < STATE_ENT_BITS) return (state[x] & (make_mask(cnt)-1));
|
||||||
|
if (state[x]) return true;
|
||||||
|
cnt -= STATE_ENT_BITS;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// call with 0, then increment after each call (returns false when done)
|
||||||
|
bool enum_chans(unsigned int *ch, unsigned int maxch=PINMAP_PIN_MAX_CHANNELS) const
|
||||||
|
{
|
||||||
|
if (WDL_NOT_NORMALLY(maxch > PINMAP_PIN_MAX_CHANNELS))
|
||||||
|
maxch = PINMAP_PIN_MAX_CHANNELS;
|
||||||
|
|
||||||
|
unsigned int x = *ch;
|
||||||
|
if (x >= maxch) return false;
|
||||||
|
|
||||||
|
WDL_UINT64 s = state[x / STATE_ENT_BITS] >> (x & (STATE_ENT_BITS-1));
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
if (s)
|
||||||
|
{
|
||||||
|
do
|
||||||
|
{
|
||||||
|
if (s&1) { *ch = x; return true; }
|
||||||
|
s>>=1;
|
||||||
|
x++;
|
||||||
|
WDL_ASSERT(x & (STATE_ENT_BITS-1)); // we should never run out of bits!
|
||||||
|
}
|
||||||
|
while (x < maxch);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
x = (x & ~(STATE_ENT_BITS-1)) + STATE_ENT_BITS;
|
||||||
|
if (x >= maxch) break;
|
||||||
|
s = state[x / STATE_ENT_BITS];
|
||||||
|
}
|
||||||
|
|
||||||
|
*ch = x;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
PinMapPin & operator |= (const PinMapPin &v)
|
||||||
|
{
|
||||||
|
for (int x = 0; x < STATE_SIZE; x ++) state[x]|=v.state[x];
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
PinMapPin & operator &= (const PinMapPin &v)
|
||||||
|
{
|
||||||
|
for (int x = 0; x < STATE_SIZE; x ++) state[x]&=v.state[x];
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void invert()
|
||||||
|
{
|
||||||
|
for (int x = 0; x < STATE_SIZE; x ++) state[x]^=full_mask();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool equal_to(const PinMapPin &v, unsigned int nch_top = PINMAP_PIN_MAX_CHANNELS) const
|
||||||
|
{
|
||||||
|
if (WDL_NOT_NORMALLY(nch_top > PINMAP_PIN_MAX_CHANNELS)) nch_top = PINMAP_PIN_MAX_CHANNELS;
|
||||||
|
for (unsigned int x = 0; x < nch_top; x += STATE_ENT_BITS)
|
||||||
|
{
|
||||||
|
if ((v.state[x/STATE_ENT_BITS]^state[x/STATE_ENT_BITS]) &
|
||||||
|
(((nch_top-x) < STATE_ENT_BITS) ? (make_mask(nch_top-x)-1) : full_mask()))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
class ChannelPinMapper
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
ChannelPinMapper() : m_nCh(0), m_nPins(0) { Reset(); }
|
||||||
|
~ChannelPinMapper() {}
|
||||||
|
|
||||||
|
void SetNPins(int nPins);
|
||||||
|
void SetNChannels(int nCh, bool auto_passthru=true);
|
||||||
|
// or ...
|
||||||
|
void Init(const PinMapPin * pMapping, int nPins);
|
||||||
|
// or ...
|
||||||
|
void Reset(); // set to full passthrough
|
||||||
|
|
||||||
|
int GetNPins() const { return m_nPins; }
|
||||||
|
int GetNChannels() const { return m_nCh; }
|
||||||
|
|
||||||
|
void ClearPin(int pinIdx);
|
||||||
|
void SetPin(int pinIdx, int chIdx, bool on);
|
||||||
|
bool TogglePin(int pinIdx, int chIdx);
|
||||||
|
|
||||||
|
// true if this pin is mapped to this channel
|
||||||
|
bool GetPin(int pinIdx, int chIdx) const;
|
||||||
|
|
||||||
|
// true if this mapper is a straight 1:1 passthrough
|
||||||
|
bool IsStraightPassthrough() const;
|
||||||
|
|
||||||
|
const char *SaveStateNew(int* pLen); // owned
|
||||||
|
bool LoadState(const char* buf, int len);
|
||||||
|
|
||||||
|
PinMapPin m_mapping[CHANNELPINMAPPER_MAXPINS];
|
||||||
|
int m_nCh, m_nPins;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
WDL_Queue m_cfgret;
|
||||||
|
};
|
||||||
|
|
||||||
|
// converts interleaved buffer to interleaved buffer, using min(len_in,len_out) and zeroing any extra samples
|
||||||
|
// isInput means it reads from track channels and writes to plugin pins
|
||||||
|
// wantZeroExcessOutput=false means that untouched channels will be preserved in buf_out
|
||||||
|
void PinMapperConvertBuffers(const double *buf, int len_in, int nch_in,
|
||||||
|
double *buf_out, int len_out, int nch_out,
|
||||||
|
const ChannelPinMapper *pinmap, bool isInput, bool wantZeroExcessOutput);
|
||||||
|
|
||||||
|
// use for float and double only ... ints will break it
|
||||||
|
class AudioBufferContainer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
AudioBufferContainer();
|
||||||
|
~AudioBufferContainer() {}
|
||||||
|
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
FMT_32FP=4,
|
||||||
|
FMT_64FP=8
|
||||||
|
};
|
||||||
|
|
||||||
|
static bool BufConvert(void* dest, const void* src, int destFmt, int srcFmt, int nFrames, int destStride, int srcStride);
|
||||||
|
|
||||||
|
int GetNChannels() const { return m_nCh; }
|
||||||
|
int GetNFrames() const { return m_nFrames; }
|
||||||
|
int GetFormat() const { return m_fmt; }
|
||||||
|
|
||||||
|
void Resize(int nCh, int nFrames, bool preserveData);
|
||||||
|
// call Reformat(GetFormat(), false) to discard current data (for efficient repopulating)
|
||||||
|
void Reformat(int fmt, bool preserveData);
|
||||||
|
|
||||||
|
// src=NULL to memset(0)
|
||||||
|
void* SetAllChannels(int fmt, const void* src, int nCh, int nFrames);
|
||||||
|
|
||||||
|
// src=NULL to memset(0)
|
||||||
|
void* SetChannel(int fmt, const void* src, int chIdx, int nFrames);
|
||||||
|
|
||||||
|
void* MixChannel(int fmt, const void* src, int chIdx, int nFrames, bool addToDest, double wt_start, double wt_end);
|
||||||
|
|
||||||
|
void* GetAllChannels(int fmt, bool preserveData);
|
||||||
|
void* GetChannel(int fmt, int chIdx, bool preserveData);
|
||||||
|
|
||||||
|
void CopyFrom(const AudioBufferContainer* rhs);
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
void ReLeave(bool interleave, bool preserveData);
|
||||||
|
|
||||||
|
WDL_HeapBuf m_data;
|
||||||
|
int m_nCh;
|
||||||
|
int m_nFrames;
|
||||||
|
|
||||||
|
int m_fmt;
|
||||||
|
bool m_interleaved;
|
||||||
|
bool m_hasData;
|
||||||
|
} WDL_FIXALIGN;
|
||||||
|
|
||||||
|
|
||||||
|
void SetPinsFromChannels(AudioBufferContainer* dest, AudioBufferContainer* src, const ChannelPinMapper* mapper, int forceMinChanCnt=0);
|
||||||
|
void SetChannelsFromPins(AudioBufferContainer* dest, AudioBufferContainer* src, const ChannelPinMapper* mapper, double wt_start=1.0, double wt_end=1.0);
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
21
oversampling/WDL/besselfilter.cpp
Normal file
21
oversampling/WDL/besselfilter.cpp
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#include "besselfilter.h"
|
||||||
|
|
||||||
|
// table produced by mkfilter's bessel -- N.B. only one member of each C.Conj. pair is listed
|
||||||
|
const WDL_BesselFilterCoeffs::complex WDL_BesselFilterCoeffs::mPoles[10 * 3] =
|
||||||
|
{
|
||||||
|
complex(-9.9999999999999978e-001, 0.0000000000000000e+000), complex(-1.1016013305921748e+000, 6.3600982475704204e-001),
|
||||||
|
complex(-1.3226757999104561e+000, 0.0000000000000000e+000), complex(-1.0474091610089453e+000, 9.9926443628064676e-001),
|
||||||
|
complex(-1.3700678305514509e+000, 4.1024971749375211e-001), complex(-9.9520876435027783e-001, 1.2571057394546732e+000),
|
||||||
|
complex(-1.5023162714474929e+000, 0.0000000000000000e+000), complex(-1.3808773258604621e+000, 7.1790958762678447e-001),
|
||||||
|
complex(-9.5767654856269568e-001, 1.4711243207304141e+000), complex(-1.5714904036160653e+000, 3.2089637422258727e-001),
|
||||||
|
complex(-1.3818580975965893e+000, 9.7147189071159734e-001), complex(-9.3065652294686840e-001, 1.6618632689426145e+000),
|
||||||
|
complex(-1.6843681792730136e+000, 0.0000000000000000e+000), complex(-1.6120387662260254e+000, 5.8924450693124097e-001),
|
||||||
|
complex(-1.3789032167953983e+000, 1.1915667778006367e+000), complex(-9.0986778062340656e-001, 1.8364513530362705e+000),
|
||||||
|
complex(-1.7574084004017807e+000, 2.7286757510254800e-001), complex(-1.6369394181268191e+000, 8.2279562513962257e-001),
|
||||||
|
complex(-1.3738412176373478e+000, 1.3883565758775174e+000), complex(-8.9286971884713073e-001, 1.9983258436412903e+000),
|
||||||
|
complex(-1.8566005012059337e+000, 0.0000000000000000e+000), complex(-1.8071705349890272e+000, 5.1238373057326947e-001),
|
||||||
|
complex(-1.6523964845794235e+000, 1.0313895669915880e+000), complex(-1.3675883097976385e+000, 1.5677337122426156e+000),
|
||||||
|
complex(-8.7839927616393221e-001, 2.1498005243204834e+000), complex(-1.9276196913244821e+000, 2.4162347094517739e-001),
|
||||||
|
complex(-1.8421962445659017e+000, 7.2725759775348364e-001), complex(-1.6618102413500957e+000, 1.2211002185915161e+000),
|
||||||
|
complex(-1.3606922783857325e+000, 1.7335057426584299e+000), complex(-8.6575690170894881e-001, 2.2926048309837426e+000)
|
||||||
|
};
|
||||||
412
oversampling/WDL/besselfilter.h
Normal file
412
oversampling/WDL/besselfilter.h
Normal file
@@ -0,0 +1,412 @@
|
|||||||
|
/*
|
||||||
|
WDL - besselfilter.h
|
||||||
|
(c) Theo Niessink 2011
|
||||||
|
<http://www.taletn.com/>
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied
|
||||||
|
warranty. In no event will the authors be held liable for any damages
|
||||||
|
arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
|
including commercial applications, and to alter it and redistribute it
|
||||||
|
freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not
|
||||||
|
claim that you wrote the original software. If you use this software
|
||||||
|
in a product, an acknowledgment in the product documentation would be
|
||||||
|
appreciated but is not required.
|
||||||
|
2. Altered source versions must be plainly marked as such, and must not be
|
||||||
|
misrepresented as being the original software.
|
||||||
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
|
||||||
|
|
||||||
|
This file provides classes for a low-pass Bessel filter design using the
|
||||||
|
matched Z-transform method.
|
||||||
|
|
||||||
|
This Bessel filter implementation was originally extracted from from the
|
||||||
|
source code of mkfilter, written by A.J. Fisher.
|
||||||
|
<http://www-users.cs.york.ac.uk/~fisher/mkfilter>
|
||||||
|
|
||||||
|
|
||||||
|
Example #1:
|
||||||
|
|
||||||
|
// 8th order anti-alias filter
|
||||||
|
#define WDL_BESSEL_FILTER_ORDER 8
|
||||||
|
#include "besselfilter.h"
|
||||||
|
|
||||||
|
int oversampling = 8;
|
||||||
|
|
||||||
|
WDL_BesselFilterCoeffs bessel;
|
||||||
|
WDL_BesselFilterStage filter;
|
||||||
|
|
||||||
|
bessel.Calc(0.5 / (double)oversampling);
|
||||||
|
filter.Reset();
|
||||||
|
|
||||||
|
for (int i = 0; i < nFrames; ++i)
|
||||||
|
{
|
||||||
|
filter.Process(inputs[0][i], bessel.Coeffs());
|
||||||
|
outputs[0][i] = filter.Output();
|
||||||
|
}
|
||||||
|
|
||||||
|
Example #2:
|
||||||
|
|
||||||
|
#include "besselfilter.h"
|
||||||
|
|
||||||
|
int order = 4;
|
||||||
|
int oversampling = 8;
|
||||||
|
|
||||||
|
// 2 cascaded filters
|
||||||
|
WDL_BesselFilterStage filter[2];
|
||||||
|
filter[0].Reset();
|
||||||
|
filter[1].Reset();
|
||||||
|
|
||||||
|
WDL_BesselFilterCoeffs coeffs;
|
||||||
|
coeffs.Calc(0.5 / (double)oversampling, order);
|
||||||
|
|
||||||
|
for (int i = 0; i < nFrames; ++i)
|
||||||
|
{
|
||||||
|
filter[0].Process(inputs[0][i], &coeffs);
|
||||||
|
filter[1].Process(filter[0].Output(), &coeffs);
|
||||||
|
outputs[0][i] = filter[1].Output();
|
||||||
|
}
|
||||||
|
|
||||||
|
Example #3:
|
||||||
|
|
||||||
|
#define WDL_BESSEL_DENORMAL_AGGRESSIVE
|
||||||
|
#include "besselfilter.h"
|
||||||
|
|
||||||
|
int order = 8;
|
||||||
|
int oversampling = 8;
|
||||||
|
|
||||||
|
WDL_BesselFilter bessel;
|
||||||
|
bessel.Calc(0.5 / (double)oversampling, order);
|
||||||
|
bessel.Reset();
|
||||||
|
|
||||||
|
for (int i = 0; i < nFrames; ++i)
|
||||||
|
{
|
||||||
|
bessel.Process(inputs[0][i]);
|
||||||
|
outputs[0][i] = bessel.Output();
|
||||||
|
}
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef _BESSELFILTER_H_
|
||||||
|
#define _BESSELFILTER_H_
|
||||||
|
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#pragma warning(disable:4996) // hypot
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include "wdltypes.h"
|
||||||
|
|
||||||
|
// By default denormals are zeroed to prevent exessive CPU use. Defining
|
||||||
|
// WDL_BESSEL_DENORMAL_IGNORE will disable denormal filtering. Defining
|
||||||
|
// WDL_BESSEL_DENORMAL_AGGRESSIVE will filter out denormals more
|
||||||
|
// aggressively by zeroing anything below 5.6e-017.
|
||||||
|
#ifndef WDL_BESSEL_DENORMAL_IGNORE
|
||||||
|
#include "denormal.h"
|
||||||
|
#if defined(WDL_BESSEL_DENORMAL_AGGRESSIVE)
|
||||||
|
#define WDL_BESSEL_FIX_DENORMAL(a) (denormal_fix_double_aggressive(a))
|
||||||
|
#else
|
||||||
|
#define WDL_BESSEL_FIX_DENORMAL(a) (denormal_fix_double(a))
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
#define WDL_BESSEL_FIX_DENORMAL(a) ((void)0)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
// Defining WDL_BESSEL_FILTER_ORDER will make the Bessel filter order fixed,
|
||||||
|
// which increases efficiency.
|
||||||
|
#ifdef WDL_BESSEL_FILTER_ORDER
|
||||||
|
#if !(WDL_BESSEL_FILTER_ORDER >= 1 && WDL_BESSEL_FILTER_ORDER <= 10)
|
||||||
|
#error WDL_BESSEL_FILTER_ORDER should be in 1..10 range
|
||||||
|
#endif
|
||||||
|
#define WDL_BESSEL_FILTER_MAX WDL_BESSEL_FILTER_ORDER
|
||||||
|
|
||||||
|
// Defining WDL_BESSEL_FILTER_MAX limits the maximum Bessel filter order,
|
||||||
|
// which reduces buffer sizes.
|
||||||
|
#else
|
||||||
|
#if defined(WDL_BESSEL_FILTER_MAX) && WDL_BESSEL_FILTER_MAX < 1
|
||||||
|
#define WDL_BESSEL_FILTER_MAX 1
|
||||||
|
#elif !defined(WDL_BESSEL_FILTER_MAX) || WDL_BESSEL_FILTER_MAX > 10
|
||||||
|
#define WDL_BESSEL_FILTER_MAX 10
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
class WDL_BesselFilterCoeffs
|
||||||
|
{
|
||||||
|
friend class WDL_BesselFilterStage;
|
||||||
|
|
||||||
|
public:
|
||||||
|
inline WDL_BesselFilterCoeffs() {}
|
||||||
|
|
||||||
|
#ifdef WDL_BESSEL_FILTER_ORDER
|
||||||
|
// alpha = cornerFreq / (oversampling * sampleRate)
|
||||||
|
inline WDL_BesselFilterCoeffs(const double alpha) { Calc(alpha); }
|
||||||
|
|
||||||
|
void Calc(double alpha)
|
||||||
|
{
|
||||||
|
const int order = WDL_BESSEL_FILTER_ORDER;
|
||||||
|
|
||||||
|
#else
|
||||||
|
inline WDL_BesselFilterCoeffs(const double alpha, const int order) { Calc(alpha, order); }
|
||||||
|
|
||||||
|
void Calc(double alpha, const int order)
|
||||||
|
{
|
||||||
|
assert(order >= 1 && order <= WDL_BESSEL_FILTER_MAX);
|
||||||
|
mOrder = order;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
assert(alpha >= 1e-37 && alpha < 0.5);
|
||||||
|
alpha *= 6.283185307179586476; // 2.*M_PI
|
||||||
|
|
||||||
|
// compute S-plane poles for prototype LP filter
|
||||||
|
// transform prototype into appropriate filter type (lp)
|
||||||
|
// given S-plane poles & zeros, compute Z-plane poles & zeros, by matched z-transform
|
||||||
|
complex zplane[WDL_BESSEL_FILTER_MAX];
|
||||||
|
int p = (order*order) / 4;
|
||||||
|
int n = 0;
|
||||||
|
if (order & 1) zplane[n++] = exp(multiply(alpha, mPoles[p++]));
|
||||||
|
for (int i = 0; i < order / 2; ++i)
|
||||||
|
{
|
||||||
|
zplane[n++] = exp(multiply(alpha, mPoles[p]));
|
||||||
|
zplane[n++] = exp(multiply(alpha, conjugate(mPoles[p++])));
|
||||||
|
}
|
||||||
|
|
||||||
|
// compute product of poles or zeros as a polynomial of z
|
||||||
|
complex coeffs[WDL_BESSEL_FILTER_MAX + 1];
|
||||||
|
coeffs[0] = 1.;
|
||||||
|
for (int i = 1; i <= order; ++i) coeffs[i] = 0.;
|
||||||
|
|
||||||
|
for (int i = 0; i < order; ++i)
|
||||||
|
{
|
||||||
|
// multiply factor (z-w) into coeffs
|
||||||
|
complex w = minus(zplane[i]);
|
||||||
|
for (int i = order; i >= 1; --i) coeffs[i] = add(multiply(w, coeffs[i]), coeffs[i - 1]);
|
||||||
|
coeffs[0] = multiply(w, coeffs[0]);
|
||||||
|
}
|
||||||
|
#ifndef NDEBUG
|
||||||
|
// check computed coeffs of z^k are all real
|
||||||
|
for (int n = 0; n <= order; ++n)
|
||||||
|
{
|
||||||
|
// mkfilter: coeff of z^n is not real; poles/zeros are not complex conjugates
|
||||||
|
assert(fabs(coeffs[n].im) <= 1e-10);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// given Z-plane poles [& zeros], compute [top &] bot polynomials in Z, and then recurrence relation
|
||||||
|
complex gain = 0.;
|
||||||
|
for (int i = order; i >= 0; --i) gain = add(gain, coeffs[i]);
|
||||||
|
gain = inverse(gain);
|
||||||
|
mCoeffs[0] = 1./hypot(gain.im, gain.re);
|
||||||
|
for (int i = 1, j = order - 1; i <= order; ++i, --j) mCoeffs[i] = -(coeffs[j].re / coeffs[order].re);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int Order() const
|
||||||
|
{
|
||||||
|
#ifdef WDL_BESSEL_FILTER_ORDER
|
||||||
|
return WDL_BESSEL_FILTER_ORDER;
|
||||||
|
#else
|
||||||
|
return mOrder;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const double* Coeffs() const { return mCoeffs; }
|
||||||
|
inline double Gain() const { return mCoeffs[0]; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
double mCoeffs[WDL_BESSEL_FILTER_MAX + 1];
|
||||||
|
|
||||||
|
#ifndef WDL_BESSEL_FILTER_ORDER
|
||||||
|
int mOrder;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Minimalistic complex number implementation
|
||||||
|
|
||||||
|
struct complex
|
||||||
|
{
|
||||||
|
double re, im;
|
||||||
|
complex() {}
|
||||||
|
complex(const double r, const double j = 0.): re(r), im(j) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
// z = z1 + z2
|
||||||
|
inline complex add(const complex z1, const complex z2) const
|
||||||
|
{
|
||||||
|
return complex(z1.re + z2.re, z1.im + z2.im);
|
||||||
|
}
|
||||||
|
|
||||||
|
// z = r * z
|
||||||
|
inline complex multiply(const double r, const complex z) const
|
||||||
|
{
|
||||||
|
return complex(r * z.re, r * z.im);
|
||||||
|
}
|
||||||
|
|
||||||
|
// z = z1 * z2
|
||||||
|
inline complex multiply(const complex z1, const complex z2) const
|
||||||
|
{
|
||||||
|
return complex(z1.re * z2.re - z1.im * z2.im, z1.re * z2.im + z1.im * z2.re);
|
||||||
|
}
|
||||||
|
|
||||||
|
// z = -z
|
||||||
|
inline complex minus(const complex z) const
|
||||||
|
{
|
||||||
|
return complex(-z.re, -z.im);
|
||||||
|
}
|
||||||
|
|
||||||
|
// z = conjugate(z)
|
||||||
|
inline complex conjugate(const complex z) const
|
||||||
|
{
|
||||||
|
return complex(z.re, -z.im);
|
||||||
|
}
|
||||||
|
|
||||||
|
// z = 1/z
|
||||||
|
inline complex inverse(const complex z) const
|
||||||
|
{
|
||||||
|
const double r = z.re*z.re + z.im*z.im;
|
||||||
|
return complex(z.re / r, -z.im / r);
|
||||||
|
}
|
||||||
|
|
||||||
|
// z = exp(z)
|
||||||
|
inline complex exp(const complex z) const
|
||||||
|
{
|
||||||
|
const double r = ::exp(z.re);
|
||||||
|
return complex(r * cos(z.im), r * sin(z.im));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Precalculated Bessel poles
|
||||||
|
static const complex mPoles[10 * 3];
|
||||||
|
} WDL_FIXALIGN;
|
||||||
|
|
||||||
|
#ifdef WDL_BESSEL_FILTER_ORDER
|
||||||
|
#define WDL_BESSEL_FILTER_OUTPUT(n) (coeffs[WDL_BESSEL_FILTER_ORDER - n + 1] * mOutput[WDL_BESSEL_FILTER_ORDER - n + 1])
|
||||||
|
#endif
|
||||||
|
|
||||||
|
class WDL_BesselFilterStage
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
inline WDL_BesselFilterStage() {}
|
||||||
|
inline WDL_BesselFilterStage(const double value) { Reset(value); }
|
||||||
|
|
||||||
|
inline void Reset() { memset(mOutput, 0, sizeof(mOutput)); }
|
||||||
|
|
||||||
|
inline void Reset(const double value)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < WDL_BESSEL_FILTER_MAX; ++i) mOutput[i] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef WDL_BESSEL_FILTER_ORDER
|
||||||
|
|
||||||
|
inline void Process(const double input, const double* const coeffs)
|
||||||
|
{
|
||||||
|
#if WDL_BESSEL_FILTER_ORDER >= 10
|
||||||
|
mOutput[10] = mOutput[9];
|
||||||
|
#endif
|
||||||
|
#if WDL_BESSEL_FILTER_ORDER >= 9
|
||||||
|
mOutput[9] = mOutput[8];
|
||||||
|
#endif
|
||||||
|
#if WDL_BESSEL_FILTER_ORDER >= 8
|
||||||
|
mOutput[8] = mOutput[7];
|
||||||
|
#endif
|
||||||
|
#if WDL_BESSEL_FILTER_ORDER >= 7
|
||||||
|
mOutput[7] = mOutput[6];
|
||||||
|
#endif
|
||||||
|
#if WDL_BESSEL_FILTER_ORDER >= 6
|
||||||
|
mOutput[6] = mOutput[5];
|
||||||
|
#endif
|
||||||
|
#if WDL_BESSEL_FILTER_ORDER >= 5
|
||||||
|
mOutput[5] = mOutput[4];
|
||||||
|
#endif
|
||||||
|
#if WDL_BESSEL_FILTER_ORDER >= 4
|
||||||
|
mOutput[4] = mOutput[3];
|
||||||
|
#endif
|
||||||
|
#if WDL_BESSEL_FILTER_ORDER >= 3
|
||||||
|
mOutput[3] = mOutput[2];
|
||||||
|
#endif
|
||||||
|
#if WDL_BESSEL_FILTER_ORDER >= 2
|
||||||
|
mOutput[2] = mOutput[1];
|
||||||
|
#endif
|
||||||
|
mOutput[1] = mOutput[0];
|
||||||
|
|
||||||
|
mOutput[0] = coeffs[0] * input
|
||||||
|
#if WDL_BESSEL_FILTER_ORDER >= 10
|
||||||
|
+ WDL_BESSEL_FILTER_OUTPUT(10)
|
||||||
|
#endif
|
||||||
|
#if WDL_BESSEL_FILTER_ORDER >= 9
|
||||||
|
+ WDL_BESSEL_FILTER_OUTPUT(9)
|
||||||
|
#endif
|
||||||
|
#if WDL_BESSEL_FILTER_ORDER >= 8
|
||||||
|
+ WDL_BESSEL_FILTER_OUTPUT(8)
|
||||||
|
#endif
|
||||||
|
#if WDL_BESSEL_FILTER_ORDER >= 7
|
||||||
|
+ WDL_BESSEL_FILTER_OUTPUT(7)
|
||||||
|
#endif
|
||||||
|
#if WDL_BESSEL_FILTER_ORDER >= 6
|
||||||
|
+ WDL_BESSEL_FILTER_OUTPUT(6)
|
||||||
|
#endif
|
||||||
|
#if WDL_BESSEL_FILTER_ORDER >= 5
|
||||||
|
+ WDL_BESSEL_FILTER_OUTPUT(5)
|
||||||
|
#endif
|
||||||
|
#if WDL_BESSEL_FILTER_ORDER >= 4
|
||||||
|
+ WDL_BESSEL_FILTER_OUTPUT(4)
|
||||||
|
#endif
|
||||||
|
#if WDL_BESSEL_FILTER_ORDER >= 3
|
||||||
|
+ WDL_BESSEL_FILTER_OUTPUT(3)
|
||||||
|
#endif
|
||||||
|
#if WDL_BESSEL_FILTER_ORDER >= 2
|
||||||
|
+ WDL_BESSEL_FILTER_OUTPUT(2)
|
||||||
|
#endif
|
||||||
|
+ WDL_BESSEL_FILTER_OUTPUT(1);
|
||||||
|
WDL_BESSEL_FIX_DENORMAL(&mOutput[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void Process(const double input, const WDL_BesselFilterCoeffs* const bessel) { Process(input, bessel->mCoeffs); }
|
||||||
|
|
||||||
|
#else // #elif !defined(WDL_BESSEL_FILTER_ORDER)
|
||||||
|
|
||||||
|
inline void Process(const double input, const double* const coeffs, const int order)
|
||||||
|
{
|
||||||
|
double output = coeffs[0] * input;
|
||||||
|
for (int i = order; i > 0; --i)
|
||||||
|
{
|
||||||
|
mOutput[i] = mOutput[i - 1];
|
||||||
|
output += coeffs[i] * mOutput[i];
|
||||||
|
WDL_BESSEL_FIX_DENORMAL(&output);
|
||||||
|
}
|
||||||
|
mOutput[0] = output;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void Process(const double input, const WDL_BesselFilterCoeffs* const bessel) { Process(input, bessel->mCoeffs, bessel->mOrder); }
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
inline double Output() const { return mOutput[0]; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
double mOutput[WDL_BESSEL_FILTER_MAX + 1];
|
||||||
|
} WDL_FIXALIGN;
|
||||||
|
|
||||||
|
|
||||||
|
class WDL_BesselFilter: public WDL_BesselFilterCoeffs, public WDL_BesselFilterStage
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
inline WDL_BesselFilter() {}
|
||||||
|
|
||||||
|
#ifdef WDL_BESSEL_FILTER_ORDER
|
||||||
|
inline WDL_BesselFilter(const double alpha) { Calc(alpha); }
|
||||||
|
inline void Process(const double input) { WDL_BesselFilterStage::Process(input, mCoeffs); }
|
||||||
|
#else
|
||||||
|
inline WDL_BesselFilter(const double alpha, const int order) { Calc(alpha, order); }
|
||||||
|
inline void Process(const double input) { WDL_BesselFilterStage::Process(input, mCoeffs, mOrder); }
|
||||||
|
#endif
|
||||||
|
} WDL_FIXALIGN;
|
||||||
|
|
||||||
|
|
||||||
|
#endif // _BESSELFILTER_H_
|
||||||
34
oversampling/WDL/bitfield.h
Normal file
34
oversampling/WDL/bitfield.h
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
#ifndef _WDL_BITFIELD_H_
|
||||||
|
#define _WDL_BITFIELD_H_
|
||||||
|
|
||||||
|
#include "heapbuf.h"
|
||||||
|
|
||||||
|
class WDL_BitField // ultra simple bit field
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
bool SetSize(int sz) // clears state
|
||||||
|
{
|
||||||
|
void *b=m_hb.ResizeOK((sz+7)/8);
|
||||||
|
if (b) memset(b,0,m_hb.GetSize());
|
||||||
|
return !!b;
|
||||||
|
}
|
||||||
|
int GetApproxSize() const { return m_hb.GetSize()*8; } // may return slightly greater than the size set
|
||||||
|
|
||||||
|
bool IsSet(unsigned int idx) const
|
||||||
|
{
|
||||||
|
const unsigned char mask = 1<<(idx&7);
|
||||||
|
idx>>=3;
|
||||||
|
return idx < (unsigned int)m_hb.GetSize() && (((unsigned char *)m_hb.Get())[idx]&mask);
|
||||||
|
}
|
||||||
|
void Set(unsigned int idx)
|
||||||
|
{
|
||||||
|
const unsigned char mask = 1<<(idx&7);
|
||||||
|
idx>>=3;
|
||||||
|
if (idx < (unsigned int)m_hb.GetSize()) ((unsigned char *)m_hb.Get())[idx] |= mask;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
WDL_HeapBuf m_hb;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif //_WDL_BITFIELD_H_
|
||||||
103
oversampling/WDL/chunkalloc.h
Normal file
103
oversampling/WDL/chunkalloc.h
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
#ifndef _WDL_CHUNKALLOC_H_
|
||||||
|
#define _WDL_CHUNKALLOC_H_
|
||||||
|
|
||||||
|
#include "wdltypes.h"
|
||||||
|
|
||||||
|
class WDL_ChunkAlloc
|
||||||
|
{
|
||||||
|
struct _hdr
|
||||||
|
{
|
||||||
|
struct _hdr *_next;
|
||||||
|
char data[16];
|
||||||
|
};
|
||||||
|
|
||||||
|
_hdr *m_chunks;
|
||||||
|
int m_chunksize, m_chunkused;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
WDL_ChunkAlloc(int chunksize=65500) { m_chunks=NULL; m_chunkused=0; m_chunksize=chunksize>16?chunksize:16; }
|
||||||
|
~WDL_ChunkAlloc() { Free(); }
|
||||||
|
|
||||||
|
void Free()
|
||||||
|
{
|
||||||
|
_hdr *a = m_chunks;
|
||||||
|
m_chunks=0;
|
||||||
|
m_chunkused=0;
|
||||||
|
while (a) { _hdr *f=a; a=a->_next; free(f); }
|
||||||
|
}
|
||||||
|
|
||||||
|
void *Alloc(int sz, int align=0)
|
||||||
|
{
|
||||||
|
if (sz<1) return NULL;
|
||||||
|
|
||||||
|
if (align < 1 || (align & (align-1))) align=1;
|
||||||
|
|
||||||
|
if (m_chunks)
|
||||||
|
{
|
||||||
|
int use_sz=sz;
|
||||||
|
char *p = m_chunks->data + m_chunkused;
|
||||||
|
int a = ((int) (INT_PTR)p) & (align-1);
|
||||||
|
if (a)
|
||||||
|
{
|
||||||
|
use_sz += align-a;
|
||||||
|
p += align-a;
|
||||||
|
}
|
||||||
|
if (use_sz <= m_chunksize - m_chunkused)
|
||||||
|
{
|
||||||
|
m_chunkused += use_sz;
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// we assume that malloc always gives at least 8 byte alignment, and our _next ptr may offset that by 4,
|
||||||
|
// so no need to allocate extra if less than 4 bytes of alignment requested
|
||||||
|
int use_align = (align>=4 ? align : 0);
|
||||||
|
int alloc_sz=sz+use_align;
|
||||||
|
if (alloc_sz < m_chunksize)
|
||||||
|
{
|
||||||
|
// if existing chunk has less free space in it than we would at chunksize, allocate chunksize
|
||||||
|
if (!m_chunks || m_chunkused > alloc_sz) alloc_sz=m_chunksize;
|
||||||
|
}
|
||||||
|
_hdr *nc = (_hdr *)malloc(sizeof(_hdr) + alloc_sz - 16);
|
||||||
|
if (!nc) return NULL;
|
||||||
|
|
||||||
|
int use_sz=sz;
|
||||||
|
char *ret = nc->data;
|
||||||
|
int a = ((int) (INT_PTR)ret) & (align-1);
|
||||||
|
if (a)
|
||||||
|
{
|
||||||
|
use_sz += align-a;
|
||||||
|
ret += align-a;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_chunks && (m_chunksize-m_chunkused) >= (alloc_sz - use_sz))
|
||||||
|
{
|
||||||
|
// current chunk has as much or more free space than our chunk, put our chunk on the list second
|
||||||
|
nc->_next = m_chunks->_next;
|
||||||
|
m_chunks->_next=nc;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// push our chunk to the top of the list
|
||||||
|
nc->_next = m_chunks;
|
||||||
|
m_chunks=nc;
|
||||||
|
m_chunkused = alloc_sz >= m_chunksize ? use_sz : m_chunksize;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *StrDup(const char *s)
|
||||||
|
{
|
||||||
|
if (!s) return NULL;
|
||||||
|
const int l = (int) strlen(s)+1;
|
||||||
|
char *ret = (char*)Alloc(l);
|
||||||
|
if (!ret) return NULL;
|
||||||
|
memcpy(ret,s,l);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
275
oversampling/WDL/circbuf.h
Normal file
275
oversampling/WDL/circbuf.h
Normal file
@@ -0,0 +1,275 @@
|
|||||||
|
/*
|
||||||
|
WDL - circbuf.h
|
||||||
|
Copyright (C) 2005 Cockos Incorporated
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied
|
||||||
|
warranty. In no event will the authors be held liable for any damages
|
||||||
|
arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
|
including commercial applications, and to alter it and redistribute it
|
||||||
|
freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not
|
||||||
|
claim that you wrote the original software. If you use this software
|
||||||
|
in a product, an acknowledgment in the product documentation would be
|
||||||
|
appreciated but is not required.
|
||||||
|
2. Altered source versions must be plainly marked as such, and must not be
|
||||||
|
misrepresented as being the original software.
|
||||||
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
This file provides a simple class for a circular FIFO queue of bytes.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _WDL_CIRCBUF_H_
|
||||||
|
#define _WDL_CIRCBUF_H_
|
||||||
|
|
||||||
|
#include "heapbuf.h"
|
||||||
|
|
||||||
|
class WDL_CircBuf
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
WDL_CircBuf()
|
||||||
|
{
|
||||||
|
m_inbuf = m_wrptr = 0;
|
||||||
|
m_buf = NULL;
|
||||||
|
m_alloc = 0;
|
||||||
|
}
|
||||||
|
~WDL_CircBuf()
|
||||||
|
{
|
||||||
|
free(m_buf);
|
||||||
|
}
|
||||||
|
void SetSize(int size)
|
||||||
|
{
|
||||||
|
if (size<0) size=0;
|
||||||
|
m_inbuf = m_wrptr = 0;
|
||||||
|
if (size != m_alloc || !m_buf)
|
||||||
|
{
|
||||||
|
m_alloc = size;
|
||||||
|
free(m_buf);
|
||||||
|
m_buf = size ? (char*)malloc(size) : NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetSizePreserveContents(int newsz)
|
||||||
|
{
|
||||||
|
if (newsz < NbInBuf()) newsz = NbInBuf(); // do not allow destructive resize down
|
||||||
|
const int oldsz = m_alloc, dsize = newsz - oldsz;
|
||||||
|
if (!dsize) return;
|
||||||
|
if (!m_inbuf||!m_buf) { SetSize(newsz); return; }
|
||||||
|
|
||||||
|
const int div1 = m_inbuf - m_wrptr; // div1>0 is size of end block, div1<0 is offset of start block
|
||||||
|
char *buf=NULL;
|
||||||
|
if (dsize > 0)
|
||||||
|
{
|
||||||
|
buf = (char *)realloc(m_buf, newsz);
|
||||||
|
if (WDL_NORMALLY(buf) && div1 > 0) // block crossing loop, need to shuffle some data
|
||||||
|
{
|
||||||
|
if (div1 > m_wrptr) // m_wrptr is size of start block, div1 is size of end block
|
||||||
|
{
|
||||||
|
// end block is larger than start, move some of start block to end of end block and shuffle forward start
|
||||||
|
if (dsize >= m_wrptr)
|
||||||
|
{
|
||||||
|
if (m_wrptr>0) memmove(buf+oldsz,buf,m_wrptr);
|
||||||
|
m_wrptr += oldsz;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
memmove(buf + oldsz, buf, dsize);
|
||||||
|
m_wrptr -= dsize;
|
||||||
|
memmove(buf, buf+dsize, m_wrptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else // end block is smaller, move it to the new end of buffer
|
||||||
|
{
|
||||||
|
memmove(buf + newsz - div1, buf + oldsz - div1, div1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (div1 < 0) // shrinking, and not a wrapped buffer
|
||||||
|
{
|
||||||
|
if (m_wrptr > newsz)
|
||||||
|
{
|
||||||
|
memmove(m_buf,m_buf-div1, m_inbuf);
|
||||||
|
m_wrptr = m_inbuf;
|
||||||
|
}
|
||||||
|
buf = (char *)realloc(m_buf, newsz);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!buf) // failed realloc(), or sizing down with block crossing loop boundary
|
||||||
|
{
|
||||||
|
buf = (char *)malloc(newsz);
|
||||||
|
if (WDL_NOT_NORMALLY(!buf)) return;
|
||||||
|
const int peeked = Peek(buf,0,m_inbuf);
|
||||||
|
if (peeked != m_inbuf) { WDL_ASSERT(peeked == m_inbuf); }
|
||||||
|
free(m_buf);
|
||||||
|
m_wrptr = m_inbuf = peeked;
|
||||||
|
}
|
||||||
|
if (m_wrptr > newsz) { WDL_ASSERT(m_wrptr <= newsz); }
|
||||||
|
if (m_wrptr >= newsz) m_wrptr=0;
|
||||||
|
m_alloc = newsz;
|
||||||
|
m_buf = buf;
|
||||||
|
}
|
||||||
|
void Reset() { m_inbuf = m_wrptr = 0; }
|
||||||
|
int Add(const void *buf, int l)
|
||||||
|
{
|
||||||
|
if (!m_buf) return 0;
|
||||||
|
const int bf = m_alloc - m_inbuf;
|
||||||
|
if (l>bf) l = bf;
|
||||||
|
if (l > 0)
|
||||||
|
{
|
||||||
|
m_wrptr = __write_bytes(m_wrptr,l,buf);
|
||||||
|
m_inbuf += l;
|
||||||
|
}
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
void UnAdd(int amt)
|
||||||
|
{
|
||||||
|
if (amt > 0)
|
||||||
|
{
|
||||||
|
if (amt > m_inbuf) amt=m_inbuf;
|
||||||
|
m_wrptr -= amt;
|
||||||
|
if (m_wrptr < 0) m_wrptr += m_alloc;
|
||||||
|
m_inbuf -= amt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Skip(int l) // can be used to rewind read pointer
|
||||||
|
{
|
||||||
|
m_inbuf -= l;
|
||||||
|
if (m_inbuf<0) m_inbuf=0;
|
||||||
|
else if (m_inbuf>m_alloc) m_inbuf=m_alloc;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Peek(void *buf, int offs, int len) const
|
||||||
|
{
|
||||||
|
if (offs<0||!m_buf) return 0;
|
||||||
|
const int ibo = m_inbuf-offs;
|
||||||
|
if (len > ibo) len = ibo;
|
||||||
|
if (len > 0)
|
||||||
|
{
|
||||||
|
int rp = m_wrptr - ibo;
|
||||||
|
if (rp < 0) rp += m_alloc;
|
||||||
|
const int wr1 = m_alloc - rp;
|
||||||
|
char * const rd = m_buf;
|
||||||
|
if (wr1 < len)
|
||||||
|
{
|
||||||
|
memcpy(buf,rd+rp,wr1);
|
||||||
|
memcpy((char*)buf+wr1,rd,len-wr1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
memcpy(buf,rd+rp,len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WriteAtReadPointer(const void *buf, int len, int offs=0)
|
||||||
|
{
|
||||||
|
if (WDL_NOT_NORMALLY(offs<0) || WDL_NOT_NORMALLY(offs>=m_inbuf)) return;
|
||||||
|
if (!m_buf || len<1) return;
|
||||||
|
if (offs+len > m_inbuf) len = m_inbuf-offs;
|
||||||
|
|
||||||
|
int write_offs = m_wrptr - m_inbuf + offs;
|
||||||
|
if (write_offs < 0) write_offs += m_alloc;
|
||||||
|
__write_bytes(write_offs, len, buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
int Get(void *buf, int l)
|
||||||
|
{
|
||||||
|
const int amt = Peek(buf,0,l);
|
||||||
|
m_inbuf -= amt;
|
||||||
|
return amt;
|
||||||
|
}
|
||||||
|
int NbFree() const { return m_alloc - m_inbuf; } // formerly Available()
|
||||||
|
int NbInBuf() const { return m_inbuf; }
|
||||||
|
int GetTotalSize() const { return m_alloc; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
int __write_bytes(int wrptr, int l, const void *buf) // no bounds checking, return end offset
|
||||||
|
{
|
||||||
|
const int wr1 = m_alloc-wrptr;
|
||||||
|
char * const p = m_buf, * const pw = p + wrptr;
|
||||||
|
if (wr1 < l)
|
||||||
|
{
|
||||||
|
if (buf)
|
||||||
|
{
|
||||||
|
memcpy(pw, buf, wr1);
|
||||||
|
memcpy(p, (char*)buf + wr1, l-wr1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
memset(pw, 0, wr1);
|
||||||
|
memset(p, 0, l-wr1);
|
||||||
|
}
|
||||||
|
return l-wr1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (buf) memcpy(pw, buf, l);
|
||||||
|
else memset(pw, 0, l);
|
||||||
|
return wr1 == l ? 0 : wrptr+l;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *m_buf;
|
||||||
|
int m_inbuf, m_wrptr,m_alloc;
|
||||||
|
} WDL_FIXALIGN;
|
||||||
|
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
class WDL_TypedCircBuf
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
WDL_TypedCircBuf() {}
|
||||||
|
~WDL_TypedCircBuf() {}
|
||||||
|
|
||||||
|
void SetSize(int size)
|
||||||
|
{
|
||||||
|
mBuf.SetSize(size * sizeof(T));
|
||||||
|
}
|
||||||
|
void SetSizePreserveContents(int size)
|
||||||
|
{
|
||||||
|
mBuf.SetSizePreserveContents(size*sizeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Reset()
|
||||||
|
{
|
||||||
|
mBuf.Reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
void UnAdd(int l) { mBuf.UnAdd(l*sizeof(T)); }
|
||||||
|
|
||||||
|
int Add(const T* buf, int l)
|
||||||
|
{
|
||||||
|
return mBuf.Add(buf, l * sizeof(T)) / sizeof(T);
|
||||||
|
}
|
||||||
|
|
||||||
|
int Get(T* buf, int l)
|
||||||
|
{
|
||||||
|
return mBuf.Get(buf, l * sizeof(T)) / sizeof(T);
|
||||||
|
}
|
||||||
|
|
||||||
|
int Peek(T* buf, int offs, int l)
|
||||||
|
{
|
||||||
|
return mBuf.Peek(buf, offs*sizeof(T), l * sizeof(T)) / sizeof(T);
|
||||||
|
}
|
||||||
|
void Skip(int l) { mBuf.Skip(l*sizeof(T)); }
|
||||||
|
|
||||||
|
void WriteAtReadPointer(const void *buf, int len, int offs=0) { mBuf.WriteAtReadPointer(buf,len*sizeof(T),offs*sizeof(T)); }
|
||||||
|
|
||||||
|
int NbFree() const { return mBuf.NbFree() / sizeof(T); } // formerly Available()
|
||||||
|
int ItemsInQueue() const { return mBuf.NbInBuf() / sizeof(T); }
|
||||||
|
int NbInBuf() const { return mBuf.NbInBuf() / sizeof(T); }
|
||||||
|
int GetTotalSize() const { return mBuf.GetTotalSize() / sizeof(T); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
WDL_CircBuf mBuf;
|
||||||
|
} WDL_FIXALIGN;
|
||||||
|
|
||||||
|
#endif
|
||||||
75
oversampling/WDL/cmath/bessel_polynomial.h
Normal file
75
oversampling/WDL/cmath/bessel_polynomial.h
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
/*
|
||||||
|
bessel_polynomial.h
|
||||||
|
Copyright (C) 2011 and later Lubomir I. Ivanov (neolit123 [at] gmail)
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied
|
||||||
|
warranty. In no event will the authors be held liable for any damages
|
||||||
|
arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
|
including commercial applications, and to alter it and redistribute it
|
||||||
|
freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not
|
||||||
|
claim that you wrote the original software. If you use this software
|
||||||
|
in a product, an acknowledgment in the product documentation would be
|
||||||
|
appreciated but is not required.
|
||||||
|
2. Altered source versions must be plainly marked as such, and must not be
|
||||||
|
misrepresented as being the original software.
|
||||||
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
algorithm to calculate coefficients for a bessel polynomial from krall & fink
|
||||||
|
series.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _BESSEL_POLYNOMIAL_H_
|
||||||
|
#define _BESSEL_POLYNOMIAL_H_
|
||||||
|
|
||||||
|
#include "custom_math.h"
|
||||||
|
#include "factorial.h"
|
||||||
|
|
||||||
|
#ifdef _BESSEL_USE_INLINE_
|
||||||
|
#define _BESSEL_INLINE _CMATH_INLINE
|
||||||
|
#else
|
||||||
|
#define _BESSEL_INLINE
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef _CMATH_ANSI
|
||||||
|
#define _BESSEL_MAX_ORDER 10
|
||||||
|
#else
|
||||||
|
#define _BESSEL_MAX_ORDER 3
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* return a coefficient */
|
||||||
|
_BESSEL_INLINE cmath_std_int_t
|
||||||
|
bessel_coefficient(const cmath_uint16_t k, const cmath_uint16_t n)
|
||||||
|
{
|
||||||
|
register cmath_std_int_t c;
|
||||||
|
const cmath_uint16_t nmk = (cmath_uint16_t)(n - k);
|
||||||
|
c = factorial(2*n - k);
|
||||||
|
c /= (factorial(nmk)*factorial(k)) * (1 << nmk);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* calculate all coefficients for n-th order polynomial */
|
||||||
|
_BESSEL_INLINE
|
||||||
|
void bessel_polynomial( cmath_std_int_t *coeff,
|
||||||
|
const cmath_uint16_t order,
|
||||||
|
const cmath_uint16_t reverse )
|
||||||
|
{
|
||||||
|
register cmath_uint16_t i = (cmath_uint16_t)(order + 1);
|
||||||
|
if (reverse)
|
||||||
|
{
|
||||||
|
while (i--)
|
||||||
|
coeff[order-i] = bessel_coefficient(i, order);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
while (i--)
|
||||||
|
coeff[i] = bessel_coefficient(i, order);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* _BESSEL_POLYNOMIAL_H */
|
||||||
368
oversampling/WDL/cmath/complex_number.h
Normal file
368
oversampling/WDL/cmath/complex_number.h
Normal file
@@ -0,0 +1,368 @@
|
|||||||
|
/*
|
||||||
|
complex_number.h
|
||||||
|
Copyright (C) 2011 and later Lubomir I. Ivanov (neolit123 [at] gmail)
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied
|
||||||
|
warranty. In no event will the authors be held liable for any damages
|
||||||
|
arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
|
including commercial applications, and to alter it and redistribute it
|
||||||
|
freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not
|
||||||
|
claim that you wrote the original software. If you use this software
|
||||||
|
in a product, an acknowledgment in the product documentation would be
|
||||||
|
appreciated but is not required.
|
||||||
|
2. Altered source versions must be plainly marked as such, and must not be
|
||||||
|
misrepresented as being the original software.
|
||||||
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
portable complex number operations
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _COMPLEX_NUMBER_H_
|
||||||
|
#define _COMPLEX_NUMBER_H_
|
||||||
|
|
||||||
|
#include "custom_math.h"
|
||||||
|
|
||||||
|
/* settings */
|
||||||
|
#ifndef _CNUM_NO_INLINE
|
||||||
|
#define _CNUM_INLINE _CMATH_INLINE
|
||||||
|
#else
|
||||||
|
#define _CNUM_INLINE
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef _CNUM_NO_ALIAS
|
||||||
|
#define _CNUM_ALIAS _CMATH_MAY_ALIAS
|
||||||
|
#else
|
||||||
|
#define _CNUM_ALIAS
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* types & constants */
|
||||||
|
#ifndef cnum_t
|
||||||
|
#define cnum_t cmath_t
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
cnum_t r _CMATH_ALIGN(8);
|
||||||
|
cnum_t i _CMATH_ALIGN(8);
|
||||||
|
} _CNUM_ALIAS cnum_s;
|
||||||
|
|
||||||
|
const cnum_s cnum_zero = {0, 0};
|
||||||
|
const cnum_s cnum_i1 = {0, 1};
|
||||||
|
const cnum_s cnum_r1 = {1, 0};
|
||||||
|
const cnum_s cnum_r2 = {2, 0};
|
||||||
|
|
||||||
|
/* methods */
|
||||||
|
#define _CNUM(r, i) cnum_new(r, i)
|
||||||
|
#define _CNUMD(x, r, i) cnum_s x = {r, i}
|
||||||
|
|
||||||
|
_CNUM_INLINE
|
||||||
|
cnum_s cnum_set(cnum_s *x, const cnum_t r, const cnum_t i)
|
||||||
|
{
|
||||||
|
x->r = r;
|
||||||
|
x->i = i;
|
||||||
|
return *x;
|
||||||
|
}
|
||||||
|
|
||||||
|
_CNUM_INLINE
|
||||||
|
cnum_s cnum_from(cnum_s *x, const cnum_s y)
|
||||||
|
{
|
||||||
|
x->r = y.r;
|
||||||
|
x->i = y.i;
|
||||||
|
return *x;
|
||||||
|
}
|
||||||
|
|
||||||
|
_CNUM_INLINE
|
||||||
|
cnum_s cnum_new(const cnum_t r, const cnum_t i)
|
||||||
|
{
|
||||||
|
cnum_s x;
|
||||||
|
x.r = r;
|
||||||
|
x.i = i;
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
_CNUM_INLINE
|
||||||
|
cnum_s cnum_cartesian(const cnum_s x)
|
||||||
|
{
|
||||||
|
return cnum_new(x.r * cmath_cos(x.i), x.r * cmath_sin(x.i));
|
||||||
|
}
|
||||||
|
|
||||||
|
_CNUM_INLINE
|
||||||
|
cnum_s cnum_polar(const cnum_s x)
|
||||||
|
{
|
||||||
|
return cnum_new(cmath_cabs(x.r, x.i), cmath_carg(x.r, x.i));
|
||||||
|
}
|
||||||
|
|
||||||
|
_CNUM_INLINE
|
||||||
|
cnum_s cnum_conjugate(const cnum_s x)
|
||||||
|
{
|
||||||
|
return cnum_new(x.r, -x.i);
|
||||||
|
}
|
||||||
|
|
||||||
|
_CNUM_INLINE
|
||||||
|
cnum_s cnum_negative(const cnum_s x)
|
||||||
|
{
|
||||||
|
return cnum_new(-x.r, -x.i);
|
||||||
|
}
|
||||||
|
|
||||||
|
_CNUM_INLINE
|
||||||
|
cnum_s cnum_swap(const cnum_s x)
|
||||||
|
{
|
||||||
|
return cnum_new(x.i, x.r);
|
||||||
|
}
|
||||||
|
|
||||||
|
_CNUM_INLINE
|
||||||
|
cnum_s cnum_add(const cnum_s x, const cnum_s y)
|
||||||
|
{
|
||||||
|
return cnum_new(x.r + y.r, x.i + y.i);
|
||||||
|
}
|
||||||
|
|
||||||
|
_CNUM_INLINE
|
||||||
|
cnum_s cnum_add_r(const cnum_s x, const cnum_t y)
|
||||||
|
{
|
||||||
|
return cnum_new(x.r + y, x.i + y);
|
||||||
|
}
|
||||||
|
|
||||||
|
_CNUM_INLINE
|
||||||
|
cnum_s cnum_sub(const cnum_s x, const cnum_s y)
|
||||||
|
{
|
||||||
|
return cnum_new(x.r - y.r, x.i - y.i);
|
||||||
|
}
|
||||||
|
|
||||||
|
_CNUM_INLINE
|
||||||
|
cnum_s cnum_sub_r(register cnum_s x, const cnum_t y)
|
||||||
|
{
|
||||||
|
return cnum_new(x.r - y, x.i - y);
|
||||||
|
}
|
||||||
|
|
||||||
|
_CNUM_INLINE
|
||||||
|
cnum_s cnum_r_sub(const cnum_t x, register cnum_s y)
|
||||||
|
{
|
||||||
|
return cnum_new(x - y.r, x - y.i);
|
||||||
|
}
|
||||||
|
|
||||||
|
_CNUM_INLINE
|
||||||
|
cnum_s cnum_mul(const cnum_s x, const cnum_s y)
|
||||||
|
{
|
||||||
|
return cnum_new(x.r*y.r - x.i*y.i, x.r*y.i + x.i*y.r);
|
||||||
|
}
|
||||||
|
|
||||||
|
_CNUM_INLINE
|
||||||
|
cnum_s cnum_mul_r(const cnum_s x, const cnum_t y)
|
||||||
|
{
|
||||||
|
return cnum_new(x.r*y, x.i*y);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define cnum_sqr(x) \
|
||||||
|
cnum_mul(x, x)
|
||||||
|
|
||||||
|
_CNUM_INLINE
|
||||||
|
cnum_s cnum_div_r(const cnum_s x, const cnum_t y)
|
||||||
|
{
|
||||||
|
return cnum_new(x.r/y, x.i/y);
|
||||||
|
}
|
||||||
|
|
||||||
|
_CNUM_INLINE
|
||||||
|
cnum_s cnum_r_div(const cnum_t x, const cnum_s y)
|
||||||
|
{
|
||||||
|
return cnum_new(x/y.r, x/y.i);
|
||||||
|
}
|
||||||
|
|
||||||
|
_CNUM_INLINE
|
||||||
|
cnum_s cnum_div(const cnum_s x, const cnum_s y)
|
||||||
|
{
|
||||||
|
return cnum_div_r(cnum_mul(x, cnum_conjugate(y)),
|
||||||
|
(y.r*y.r + cmath_abs(y.i*y.i)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#define cnum_inv(x) \
|
||||||
|
cnum_div(cnum_r1, x)
|
||||||
|
|
||||||
|
#define _CNUM_CHECK_EXP_D_ \
|
||||||
|
cmath_abs(deg - cmath_round(deg)) == 0
|
||||||
|
|
||||||
|
_CNUM_INLINE
|
||||||
|
cnum_s cnum_exp(const cnum_s x)
|
||||||
|
{
|
||||||
|
cnum_t sin_i = cmath_sin(x.i);
|
||||||
|
cnum_t cos_i = cmath_cos(x.i);
|
||||||
|
const cnum_t exp_r = cmath_exp(x.r);
|
||||||
|
|
||||||
|
#ifndef _CNUM_NO_CHECK_EXP_
|
||||||
|
register cnum_t deg;
|
||||||
|
|
||||||
|
if (x.r == 0)
|
||||||
|
return cnum_zero;
|
||||||
|
deg = x.i / cmath_pi;
|
||||||
|
if (_CNUM_CHECK_EXP_D_)
|
||||||
|
sin_i = 0;
|
||||||
|
deg += 0.5;
|
||||||
|
if (_CNUM_CHECK_EXP_D_)
|
||||||
|
cos_i = 0;
|
||||||
|
deg = x.i / cmath_pi2;
|
||||||
|
if (_CNUM_CHECK_EXP_D_)
|
||||||
|
cos_i = 1;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return cnum_new(exp_r*cos_i, exp_r*sin_i);
|
||||||
|
}
|
||||||
|
|
||||||
|
_CNUM_INLINE
|
||||||
|
cnum_s cnum_log_k(const cnum_s x, const cmath_int32_t k)
|
||||||
|
{
|
||||||
|
return cnum_new(cmath_log(cmath_cabs(x.r, x.i)),
|
||||||
|
(cmath_carg(x.r, x.i) + (cmath_pi2*k)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#define cnum_log(x) \
|
||||||
|
cnum_log_k(x, 0)
|
||||||
|
|
||||||
|
#define cnum_log_b_k(x, b, k) \
|
||||||
|
cnum_div(cnum_log_k(x, k), cnum_log_k(b, k))
|
||||||
|
|
||||||
|
#define cnum_log_b(b, x) \
|
||||||
|
cnum_div(cnum_log(x), cnum_log(b))
|
||||||
|
|
||||||
|
#define cnum_log2(x) \
|
||||||
|
cnum_log_b(x, 2)
|
||||||
|
|
||||||
|
#define cnum_log2_k(x, k) \
|
||||||
|
cnum_log_b_k(x, 2, k)
|
||||||
|
|
||||||
|
#define cnum_log10(x) \
|
||||||
|
cnum_log_b(x, 2)
|
||||||
|
|
||||||
|
#define cnum_log10_k(x, k) \
|
||||||
|
cnum_log_b_k(x, 10, k)
|
||||||
|
|
||||||
|
#define _CNUM_CHECK_POW_C_ \
|
||||||
|
if (x.r == 0 && x.i == 0) \
|
||||||
|
return cnum_zero; \
|
||||||
|
if (y.r == 0 && y.i == 0) \
|
||||||
|
return cnum_r1 \
|
||||||
|
|
||||||
|
_CNUM_INLINE
|
||||||
|
cnum_s cnum_pow_c_k(const cnum_s x, const cnum_s y, const cmath_int32_t k)
|
||||||
|
{
|
||||||
|
_CNUM_CHECK_POW_C_;
|
||||||
|
return cnum_exp(cnum_mul(cnum_log_k(x, k), y));
|
||||||
|
}
|
||||||
|
|
||||||
|
_CNUM_INLINE
|
||||||
|
cnum_s cnum_pow_c(const cnum_s x, const cnum_s y)
|
||||||
|
{
|
||||||
|
_CNUM_CHECK_POW_C_;
|
||||||
|
return cnum_exp(cnum_mul(cnum_log(x), y));
|
||||||
|
}
|
||||||
|
|
||||||
|
_CNUM_INLINE
|
||||||
|
cnum_s cnum_pow(const cnum_s x, const cnum_t n)
|
||||||
|
{
|
||||||
|
const cnum_t r_pow_n = cmath_pow(cmath_cabs(x.r, x.i), n);
|
||||||
|
const cnum_t theta_n = cmath_carg(x.r, x.i) * n;
|
||||||
|
if (n == 0)
|
||||||
|
return cnum_new(1, 0);
|
||||||
|
if (n == 1)
|
||||||
|
return x;
|
||||||
|
return cnum_new(r_pow_n * cmath_cos(theta_n), r_pow_n * cmath_sin(theta_n));
|
||||||
|
}
|
||||||
|
|
||||||
|
#define cnum_root_c_k(x, y, k) \
|
||||||
|
cnum_exp(cnum_div(cnum_log_k(x, k), y))
|
||||||
|
|
||||||
|
#define cnum_root_c(x, y) \
|
||||||
|
cnum_exp(cnum_div(cnum_log(x), y))
|
||||||
|
|
||||||
|
#define cnum_root(x, n) \
|
||||||
|
cnum_pow(x, 1/n)
|
||||||
|
|
||||||
|
#define cnum_sqrt(x) \
|
||||||
|
cnum_pow(x, 0.5)
|
||||||
|
|
||||||
|
#define cnum_sin(x) \
|
||||||
|
cnum_new(cmath_sin((x).r)*cmath_cosh((x).i), \
|
||||||
|
cmath_cos((x).r)*cmath_sinh((x).i))
|
||||||
|
|
||||||
|
#define cnum_sinh(x) \
|
||||||
|
cnum_new(cmath_sinh(x.r)*cmath_cos(x.i), cmath_cosh(x.r)*sin(x.i))
|
||||||
|
|
||||||
|
#define cnum_cos(x) \
|
||||||
|
cnum_new(cmath_cos(x.r)*cmath_cosh(x.i), -cmath_sin(x.r)*cmath_sinh(x.i))
|
||||||
|
|
||||||
|
#define cnum_cosh(x) \
|
||||||
|
cnum_new(cmath_cosh(x.r)*cmath_cos(x.i), cmath_sinh(x.r)*cmath_sin(x.i))
|
||||||
|
|
||||||
|
#define cnum_tan(x) \
|
||||||
|
cnum_div(cnum_sin(x), cnum_cos(x))
|
||||||
|
|
||||||
|
#define cnum_tanh(x) \
|
||||||
|
cnum_div(cnum_sinh(x), cnum_cosh(x))
|
||||||
|
|
||||||
|
#define cnum_csc(x) \
|
||||||
|
cnum_inv(cnum_sin(x))
|
||||||
|
|
||||||
|
#define cnum_sec(x) \
|
||||||
|
cnum_inv(cnum_cos(x))
|
||||||
|
|
||||||
|
#define cnum_cotan(x) \
|
||||||
|
cnum_inv(cnum_tan(x))
|
||||||
|
|
||||||
|
#define cnum_asin(x) \
|
||||||
|
cnum_negative(cnum_mul(cnum_i1, cnum_log(cnum_add(cnum_mul(cnum_i1, x), \
|
||||||
|
cnum_sqrt(cnum_sub(cnum_r1, cnum_sqr(x)))))))
|
||||||
|
|
||||||
|
#define cnum_acos(x) \
|
||||||
|
cnum_negative(cnum_mul(cnum_i1, cnum_log(cnum_add(x, cnum_mul(cnum_i1, \
|
||||||
|
cnum_sqrt(cnum_sub(cnum_r1, cnum_sqr(x))))))))
|
||||||
|
|
||||||
|
#define cnum_atan(x) \
|
||||||
|
cnum_div(cnum_mul(cnum_i1, cnum_log(cnum_div(cnum_sub(cnum_r1, \
|
||||||
|
cnum_mul(cnum_i1, x)), cnum_add(cnum_r1, cnum_mul(cnum_i1, x))))), cnum_r2)
|
||||||
|
|
||||||
|
#define cnum_acsc(x) \
|
||||||
|
cnum_asin(cnum_inv(x))
|
||||||
|
|
||||||
|
#define cnum_asec(x) \
|
||||||
|
cnum_acos(cnum_inv(x))
|
||||||
|
|
||||||
|
#define cnum_acot(x) \
|
||||||
|
cnum_atan(cnum_inv(x))
|
||||||
|
|
||||||
|
#define cnum_csch(x) \
|
||||||
|
cnum_inv(cnum_sinh(x))
|
||||||
|
|
||||||
|
#define cnum_sech(x) \
|
||||||
|
cnum_inv(cnum_cosh(x))
|
||||||
|
|
||||||
|
#define cnum_coth(x) \
|
||||||
|
cnum_inv(cnum_tanh(x))
|
||||||
|
|
||||||
|
#define cnum_asinh(x) \
|
||||||
|
cnum_log(cnum_add(x, cnum_sqrt(cnum_add(cnum_r1, cnum_sqr(x)))))
|
||||||
|
|
||||||
|
#define cnum_acosh(x) \
|
||||||
|
cnum_log(cnum_add(x, cnum_mul(cnum_sqrt(cnum_add(x, cnum_r1)), \
|
||||||
|
cnum_sqrt(cnum_sub(x, cnum_r1)))))
|
||||||
|
|
||||||
|
#define cnum_atanh(x) \
|
||||||
|
cnum_div(cnum_sub(cnum_log(cnum_add(cnum_r1, x)), \
|
||||||
|
cnum_log(cnum_sub(cnum_r1, x))), cnum_r2)
|
||||||
|
|
||||||
|
#define cnum_acsch(x) \
|
||||||
|
cnum_asinh(cnum_inv(x))
|
||||||
|
|
||||||
|
#define cnum_asech(x) \
|
||||||
|
cnum_acosh(cnum_inv(x))
|
||||||
|
|
||||||
|
#define cnum_asech(x) \
|
||||||
|
cnum_acosh(cnum_inv(x))
|
||||||
|
|
||||||
|
#define cnum_acoth(x) \
|
||||||
|
cnum_atanh(cnum_inv(x))
|
||||||
|
|
||||||
|
#endif /* _COMPLEX_NUMBER_H_ */
|
||||||
209
oversampling/WDL/cmath/custom_math.h
Normal file
209
oversampling/WDL/cmath/custom_math.h
Normal file
@@ -0,0 +1,209 @@
|
|||||||
|
/*
|
||||||
|
custom_math.h
|
||||||
|
Copyright (C) 2011 and later Lubomir I. Ivanov (neolit123 [at] gmail)
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied
|
||||||
|
warranty. In no event will the authors be held liable for any damages
|
||||||
|
arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
|
including commercial applications, and to alter it and redistribute it
|
||||||
|
freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not
|
||||||
|
claim that you wrote the original software. If you use this software
|
||||||
|
in a product, an acknowledgment in the product documentation would be
|
||||||
|
appreciated but is not required.
|
||||||
|
2. Altered source versions must be plainly marked as such, and must not be
|
||||||
|
misrepresented as being the original software.
|
||||||
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
portable definitions for ansi c and cross compiler compatibility.
|
||||||
|
contains: numeric constants, custom math functions, macros & other.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _CUSTOM_MATH_H_
|
||||||
|
#define _CUSTOM_MATH_H_
|
||||||
|
|
||||||
|
#include "math.h"
|
||||||
|
|
||||||
|
/* check for "c89" mode */
|
||||||
|
#if (defined _MSC_VER && defined __STDC__) || \
|
||||||
|
(defined __GNUC__ && defined __STRICT_ANSI__)
|
||||||
|
#define _CMATH_ANSI
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* enable inline */
|
||||||
|
#if defined __cplusplus || (!defined _CMATH_ANSI && defined _CMATH_USE_INLINE)
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#define _CMATH_INLINE __inline
|
||||||
|
#else
|
||||||
|
#define _CMATH_INLINE inline
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
#define _CMATH_INLINE
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* align type to size of type */
|
||||||
|
#if defined __GNUC__ || defined __TINYC__
|
||||||
|
#define _CMATH_ALIGN(x) __attribute__ ((aligned(x)))
|
||||||
|
#define _CMATH_ALIGN_T(x) __attribute__ ((aligned(sizeof(x))))
|
||||||
|
#else
|
||||||
|
#define _CMATH_ALIGN(x)
|
||||||
|
#define _CMATH_ALIGN_T(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* printf max integer */
|
||||||
|
#ifndef _CMATH_ANSI
|
||||||
|
#ifdef _WIN32
|
||||||
|
#define _CMATH_PR_STD_UINT "I64u"
|
||||||
|
#define _CMATH_PR_STD_INT "I64i"
|
||||||
|
#define _CMATH_PR_STD_HEX "I64x"
|
||||||
|
#else
|
||||||
|
#define _CMATH_PR_STD_UINT "llu"
|
||||||
|
#define _CMATH_PR_STD_INT "lli"
|
||||||
|
#define _CMATH_PR_STD_HEX "llx"
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
#define _CMATH_PR_STD_UINT "u"
|
||||||
|
#define _CMATH_PR_STD_INT "d"
|
||||||
|
#define _CMATH_PR_STD_HEX "x"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* msvc specifics */
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#pragma warning(disable : 4514)
|
||||||
|
|
||||||
|
#define MK_L(x) (x)
|
||||||
|
#define MK_UL(x) (x)
|
||||||
|
#define MK_LL(x) (x)
|
||||||
|
#define MK_ULL(x) (x)
|
||||||
|
#else
|
||||||
|
#define MK_L(x) (x##L)
|
||||||
|
#define MK_UL(x) (x##UL)
|
||||||
|
#ifdef _CMATH_ANSI
|
||||||
|
#define MK_LL(x) (x##L)
|
||||||
|
#define MK_ULL(x) (x##UL)
|
||||||
|
#else
|
||||||
|
#define MK_LL(x) (x##LL)
|
||||||
|
#define MK_ULL(x) (x##ULL)
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* definitions depending on c standard */
|
||||||
|
#ifdef _CMATH_ANSI
|
||||||
|
#define cmath_std_signbit MK_UL(0x7fffffff)
|
||||||
|
#define cmath_std_float_t float
|
||||||
|
#define cmath_std_int_t int
|
||||||
|
#else
|
||||||
|
#define cmath_std_signbit MK_ULL(0x7fffffffffffffff)
|
||||||
|
#define cmath_std_float_t double
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#define cmath_std_int_t __int64
|
||||||
|
#else
|
||||||
|
#define cmath_std_int_t long long
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* types and constants */
|
||||||
|
#ifndef cmath_t
|
||||||
|
#define cmath_t double
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define cmath_std_uint_t unsigned cmath_std_int_t
|
||||||
|
|
||||||
|
#define cmath_pi 3.1415926535897932384626433832795
|
||||||
|
#define cmath_pi2 6.2831853071795864769252867665590
|
||||||
|
#define cmath_pi_2 1.5707963267948966192313216916398
|
||||||
|
#define cmath_e 2.7182818284590452353602874713526
|
||||||
|
#define cmath_sqrt2 1.4142135623730950488016887242097
|
||||||
|
#define cmath_pi_180 0.0174532925199432957692369076848
|
||||||
|
#define cmath_180_pi 57.295779513082320876798154814105
|
||||||
|
|
||||||
|
#define cmath_int8_t char
|
||||||
|
#define cmath_uint8_t unsigned char
|
||||||
|
#define cmath_int16_t short
|
||||||
|
#define cmath_uint16_t unsigned short
|
||||||
|
#define cmath_int32_t int
|
||||||
|
#define cmath_uint32_t unsigned int
|
||||||
|
|
||||||
|
/* aliased types */
|
||||||
|
#ifdef __GNUC__
|
||||||
|
#define _CMATH_MAY_ALIAS __attribute__((__may_alias__))
|
||||||
|
#else
|
||||||
|
#define _CMATH_MAY_ALIAS
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef cmath_t _CMATH_MAY_ALIAS cmath_t_a;
|
||||||
|
|
||||||
|
/* possible approximations */
|
||||||
|
#define cmath_sin sin
|
||||||
|
#define cmath_cos cos
|
||||||
|
#define cmath_tan tan
|
||||||
|
#define cmath_asin asin
|
||||||
|
#define cmath_acos acos
|
||||||
|
#define cmath_atan atan
|
||||||
|
#define cmath_atan2 atan2
|
||||||
|
#define cmath_sinh sinh
|
||||||
|
#define cmath_cosh cosh
|
||||||
|
#define cmath_tanh tanh
|
||||||
|
#define cmath_exp exp
|
||||||
|
#define cmath_pow pow
|
||||||
|
#define cmath_sqrt sqrt
|
||||||
|
#define cmath_log log
|
||||||
|
#define cmath_log2 log2
|
||||||
|
#define cmath_log10 log10
|
||||||
|
|
||||||
|
/* methods */
|
||||||
|
#define cmath_array_size(x) \
|
||||||
|
(sizeof(x) / sizeof(*(x)))
|
||||||
|
|
||||||
|
#define poly_order(x) \
|
||||||
|
(sizeof(x) / sizeof(*(x)) - 1)
|
||||||
|
|
||||||
|
#define cmath_cabs(a, b) \
|
||||||
|
cmath_sqrt((a)*(a) + (b)*(b))
|
||||||
|
|
||||||
|
#define cmath_carg(a, b) \
|
||||||
|
cmath_atan2((b), (a))
|
||||||
|
|
||||||
|
#define cmath_radians(x) \
|
||||||
|
((x)*cmath_pi_180)
|
||||||
|
|
||||||
|
#define cmath_degrees(x) \
|
||||||
|
((x)*cmath_180_pi)
|
||||||
|
|
||||||
|
_CMATH_INLINE
|
||||||
|
cmath_t cmath_powi(const cmath_t x, register cmath_uint16_t n)
|
||||||
|
{
|
||||||
|
register cmath_t result = 1;
|
||||||
|
while (n--)
|
||||||
|
result *= x;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
_CMATH_INLINE
|
||||||
|
cmath_t cmath_abs(const cmath_t x)
|
||||||
|
{
|
||||||
|
register union
|
||||||
|
{
|
||||||
|
cmath_std_int_t i;
|
||||||
|
cmath_std_float_t j;
|
||||||
|
} u;
|
||||||
|
u.j = (cmath_std_float_t)x;
|
||||||
|
u.i &= cmath_std_signbit;
|
||||||
|
return u.j;
|
||||||
|
}
|
||||||
|
|
||||||
|
_CMATH_INLINE
|
||||||
|
cmath_t cmath_round(const cmath_t x)
|
||||||
|
{
|
||||||
|
if (x < 0.0)
|
||||||
|
return (cmath_t)(cmath_std_int_t)(x - 0.5);
|
||||||
|
else
|
||||||
|
return (cmath_t)(cmath_std_int_t)(x + 0.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* _CUSTOM_MATH_H_ */
|
||||||
117
oversampling/WDL/cmath/durand_kerner.h
Normal file
117
oversampling/WDL/cmath/durand_kerner.h
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
/*
|
||||||
|
durand_kerner.h
|
||||||
|
Copyright (C) 2011 and later Lubomir I. Ivanov (neolit123 [at] gmail)
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied
|
||||||
|
warranty. In no event will the authors be held liable for any damages
|
||||||
|
arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
|
including commercial applications, and to alter it and redistribute it
|
||||||
|
freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not
|
||||||
|
claim that you wrote the original software. If you use this software
|
||||||
|
in a product, an acknowledgment in the product documentation would be
|
||||||
|
appreciated but is not required.
|
||||||
|
2. Altered source versions must be plainly marked as such, and must not be
|
||||||
|
misrepresented as being the original software.
|
||||||
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
durand-kerner (weierstrass) algorithm for finding complex roots
|
||||||
|
of polynomials.
|
||||||
|
accuracy depends a lot on data type precision.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _DURAND_KERNER_H_
|
||||||
|
#define _DURAND_KERNER_H_
|
||||||
|
|
||||||
|
#include "horner.h"
|
||||||
|
#include "custom_math.h"
|
||||||
|
#include "complex_number.h"
|
||||||
|
|
||||||
|
/* settings */
|
||||||
|
#ifdef _DURAND_KERNER_USE_INLINE_
|
||||||
|
#define _DURAND_KERNER_INLINE _CMATH_INLINE
|
||||||
|
#else
|
||||||
|
#define _DURAND_KERNER_INLINE
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define DK_EPSILON 1E-16
|
||||||
|
#define DK_MAX_ITR 1E+3
|
||||||
|
#define DK_MAX_N 256
|
||||||
|
|
||||||
|
const cnum_s dk_demoivre_c = {0.4, 0.9};
|
||||||
|
|
||||||
|
/* accepts an array of complex numbers */
|
||||||
|
_DURAND_KERNER_INLINE
|
||||||
|
void durand_kerner_c
|
||||||
|
(const cnum_s *coeff, cnum_s *roots, const cmath_uint16_t order)
|
||||||
|
{
|
||||||
|
register cmath_uint16_t i, j;
|
||||||
|
register cmath_uint32_t itr;
|
||||||
|
cnum_s coeff_sc[DK_MAX_N];
|
||||||
|
cnum_s x;
|
||||||
|
cnum_s hor; /* needs an address or breaks g++ 4.x */
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while(i < order)
|
||||||
|
{
|
||||||
|
cnum_from(&roots[i], cnum_pow(dk_demoivre_c, i));
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
cnum_from(&coeff_sc[0], cnum_r1);
|
||||||
|
i = 1;
|
||||||
|
while(i < order+1)
|
||||||
|
{
|
||||||
|
cnum_from(&coeff_sc[i], cnum_div(coeff[i], coeff[0]));
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
itr = 0;
|
||||||
|
while(itr < DK_MAX_ITR)
|
||||||
|
{
|
||||||
|
i = 0;
|
||||||
|
while(i < order)
|
||||||
|
{
|
||||||
|
j = 0;
|
||||||
|
x = cnum_r1;
|
||||||
|
while (j < order)
|
||||||
|
{
|
||||||
|
if (i != j)
|
||||||
|
x = cnum_mul(cnum_sub(roots[i], roots[j]), x);
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
hor = horner_eval_c(coeff_sc, roots[i], order);
|
||||||
|
x = cnum_div(hor, x);
|
||||||
|
x = cnum_sub(roots[i], x);
|
||||||
|
if (cmath_abs(cmath_abs(x.r) - cmath_abs(roots[i].r)) < DK_EPSILON &&
|
||||||
|
cmath_abs(cmath_abs(x.i) - cmath_abs(roots[i].i)) < DK_EPSILON)
|
||||||
|
return;
|
||||||
|
cnum_from(&roots[i], x);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
itr++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* accepts an array of real numbers */
|
||||||
|
_DURAND_KERNER_INLINE
|
||||||
|
void durand_kerner
|
||||||
|
(const cmath_t *coeff, cnum_s *roots, const cmath_uint16_t order)
|
||||||
|
{
|
||||||
|
register cmath_uint16_t i;
|
||||||
|
cnum_s coeff_c[DK_MAX_N];
|
||||||
|
i = 0;
|
||||||
|
while(i < (order+1))
|
||||||
|
{
|
||||||
|
cnum_set(&coeff_c[i], coeff[i], 0);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
durand_kerner_c(coeff_c, roots, order);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* _DURAND_KERNER_H_ */
|
||||||
114
oversampling/WDL/cmath/factorial.h
Normal file
114
oversampling/WDL/cmath/factorial.h
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
/*
|
||||||
|
factorial.h
|
||||||
|
Copyright (C) 2011 and later Lubomir I. Ivanov (neolit123 [at] gmail)
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied
|
||||||
|
warranty. In no event will the authors be held liable for any damages
|
||||||
|
arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
|
including commercial applications, and to alter it and redistribute it
|
||||||
|
freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not
|
||||||
|
claim that you wrote the original software. If you use this software
|
||||||
|
in a product, an acknowledgment in the product documentation would be
|
||||||
|
appreciated but is not required.
|
||||||
|
2. Altered source versions must be plainly marked as such, and must not be
|
||||||
|
misrepresented as being the original software.
|
||||||
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
methods to return low-order factorials depending on allowed data types.
|
||||||
|
|
||||||
|
20! = 2432902008176640000 is the maximum factorial to be held
|
||||||
|
in a unsigned 64bit integer.
|
||||||
|
13! = 479001600 is the maximum factorial to be held in a unsigned
|
||||||
|
32bit integer.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _FACTORIAL_H_
|
||||||
|
#define _FACTORIAL_H_
|
||||||
|
|
||||||
|
#include "custom_math.h"
|
||||||
|
|
||||||
|
#define FACTORIAL_LOWER \
|
||||||
|
MK_ULL(1), \
|
||||||
|
MK_ULL(1), \
|
||||||
|
MK_ULL(2), \
|
||||||
|
MK_ULL(6), \
|
||||||
|
MK_ULL(24), \
|
||||||
|
MK_ULL(120), \
|
||||||
|
MK_ULL(720), \
|
||||||
|
MK_ULL(5040), \
|
||||||
|
MK_ULL(40320), \
|
||||||
|
MK_ULL(362880), \
|
||||||
|
MK_ULL(3628800), \
|
||||||
|
MK_ULL(39916800), \
|
||||||
|
MK_ULL(479001600)
|
||||||
|
|
||||||
|
#define FACTORIAL_HIGHER \
|
||||||
|
MK_ULL(6227020800), \
|
||||||
|
MK_ULL(87178291200), \
|
||||||
|
MK_ULL(1307674368000), \
|
||||||
|
MK_ULL(20922789888000), \
|
||||||
|
MK_ULL(355687428096000), \
|
||||||
|
MK_ULL(6402373705728000), \
|
||||||
|
MK_ULL(121645100408832000), \
|
||||||
|
MK_ULL(2432902008176640000)
|
||||||
|
|
||||||
|
static const cmath_std_uint_t _factorials[] =
|
||||||
|
{
|
||||||
|
#ifdef _CMATH_ANSI
|
||||||
|
FACTORIAL_LOWER
|
||||||
|
#else
|
||||||
|
FACTORIAL_LOWER,
|
||||||
|
FACTORIAL_HIGHER
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
static const cmath_t _inv_factorials[] =
|
||||||
|
{
|
||||||
|
1.00000000000000000000000000000000,
|
||||||
|
1.00000000000000000000000000000000,
|
||||||
|
0.50000000000000000000000000000000,
|
||||||
|
0.16666666666666666666666666666667,
|
||||||
|
0.04166666666666666666666666666666,
|
||||||
|
0.00833333333333333333333333333333,
|
||||||
|
0.00138888888888888888888888888888,
|
||||||
|
0.00019841269841269841269841269841,
|
||||||
|
0.00002480158730158730158730158730,
|
||||||
|
0.00000275573192239858906525573192,
|
||||||
|
0.00000027557319223985890652557319,
|
||||||
|
0.00000002505210838544171877505210,
|
||||||
|
0.00000000208767569878680989792100,
|
||||||
|
0.00000000016059043836821614599390,
|
||||||
|
0.00000000001147074559772972471385,
|
||||||
|
0.00000000000076471637318198164750,
|
||||||
|
0.00000000000004779477332387385297,
|
||||||
|
0.00000000000000281145725434552076,
|
||||||
|
0.00000000000000015619206968586225,
|
||||||
|
0.00000000000000000822063524662433,
|
||||||
|
0.00000000000000000041103176233122
|
||||||
|
};
|
||||||
|
|
||||||
|
_CMATH_INLINE
|
||||||
|
cmath_std_uint_t factorial(const cmath_uint32_t x)
|
||||||
|
{
|
||||||
|
if(x >= cmath_array_size(_factorials))
|
||||||
|
return 0;
|
||||||
|
else
|
||||||
|
return _factorials[x];
|
||||||
|
}
|
||||||
|
|
||||||
|
_CMATH_INLINE
|
||||||
|
cmath_t inv_factorial(const cmath_uint32_t x)
|
||||||
|
{
|
||||||
|
if(x >= cmath_array_size(_inv_factorials))
|
||||||
|
return 0;
|
||||||
|
else
|
||||||
|
return _inv_factorials[x];
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* _FACTORIAL_H_ */
|
||||||
71
oversampling/WDL/cmath/horner.h
Normal file
71
oversampling/WDL/cmath/horner.h
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
horner.h
|
||||||
|
Copyright (C) 2011 and later Lubomir I. Ivanov (neolit123 [at] gmail)
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied
|
||||||
|
warranty. In no event will the authors be held liable for any damages
|
||||||
|
arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
|
including commercial applications, and to alter it and redistribute it
|
||||||
|
freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not
|
||||||
|
claim that you wrote the original software. If you use this software
|
||||||
|
in a product, an acknowledgment in the product documentation would be
|
||||||
|
appreciated but is not required.
|
||||||
|
2. Altered source versions must be plainly marked as such, and must not be
|
||||||
|
misrepresented as being the original software.
|
||||||
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
algorithm to evaluate integer order polynomials using horner's scheme.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _HORNER_H_
|
||||||
|
#define _HORNER_H_
|
||||||
|
|
||||||
|
#include "custom_math.h"
|
||||||
|
#include "complex_number.h"
|
||||||
|
|
||||||
|
/* settings */
|
||||||
|
#ifndef _HORNER_INLINE
|
||||||
|
#define _HORNER_INLINE _CMATH_INLINE
|
||||||
|
#else
|
||||||
|
#define _HORNER_INLINE
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* real */
|
||||||
|
_HORNER_INLINE
|
||||||
|
cmath_t horner_eval
|
||||||
|
(const cmath_t *coeff, const cmath_t x, cmath_uint16_t order)
|
||||||
|
{
|
||||||
|
register cmath_t y = coeff[0];
|
||||||
|
register cmath_uint16_t n = 1;
|
||||||
|
order += 1;
|
||||||
|
while(n < order)
|
||||||
|
{
|
||||||
|
y = y*x + coeff[n];
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* complex */
|
||||||
|
_HORNER_INLINE
|
||||||
|
cnum_s horner_eval_c
|
||||||
|
(const cnum_s *coeff, const cnum_s x, cmath_uint16_t order)
|
||||||
|
{
|
||||||
|
register cmath_uint16_t n = 1;
|
||||||
|
cnum_s y = coeff[0];
|
||||||
|
order += 1;
|
||||||
|
while(n < order)
|
||||||
|
{
|
||||||
|
y = cnum_add(cnum_mul(y, x), coeff[n]);
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* _HORNER_H_ */
|
||||||
119
oversampling/WDL/cmath/test_bessel.c
Normal file
119
oversampling/WDL/cmath/test_bessel.c
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
/*
|
||||||
|
test_bessel.h
|
||||||
|
Copyright (C) 2011 and later Lubomir I. Ivanov (neolit123 [at] gmail)
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied
|
||||||
|
warranty. In no event will the authors be held liable for any damages
|
||||||
|
arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
|
including commercial applications, and to alter it and redistribute it
|
||||||
|
freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not
|
||||||
|
claim that you wrote the original software. If you use this software
|
||||||
|
in a product, an acknowledgment in the product documentation would be
|
||||||
|
appreciated but is not required.
|
||||||
|
2. Altered source versions must be plainly marked as such, and must not be
|
||||||
|
misrepresented as being the original software.
|
||||||
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
test bessel_polynomial.h and other related headers
|
||||||
|
|
||||||
|
gcc -W -Wall -Wextra -ansi pedantic
|
||||||
|
cl /W4 /Za
|
||||||
|
|
||||||
|
reduced precisions for ansi c
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "stdio.h"
|
||||||
|
#include "custom_math.h"
|
||||||
|
#include "bessel_polynomial.h"
|
||||||
|
#include "durand_kerner.h"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
register cmath_uint16_t i = 0;
|
||||||
|
register cmath_int16_t diff = 0;
|
||||||
|
|
||||||
|
cmath_uint32_t in_order = _BESSEL_MAX_ORDER + 1;
|
||||||
|
cmath_uint16_t order;
|
||||||
|
const cmath_uint16_t reverse = 1;
|
||||||
|
|
||||||
|
cmath_std_int_t coeff[_BESSEL_MAX_ORDER + 1];
|
||||||
|
|
||||||
|
cnum_t dk_coeff[_BESSEL_MAX_ORDER + 1];
|
||||||
|
cnum_s dk_roots[_BESSEL_MAX_ORDER];
|
||||||
|
|
||||||
|
/* */
|
||||||
|
#ifdef _CMATH_ANSI
|
||||||
|
puts("\n\nansi c is: on");
|
||||||
|
#else
|
||||||
|
puts("\n\nansi c is: off");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* */
|
||||||
|
while (in_order > _BESSEL_MAX_ORDER)
|
||||||
|
{
|
||||||
|
printf("\nenter order of bessel polynomial (0 - %d): ", _BESSEL_MAX_ORDER);
|
||||||
|
scanf("%u", &in_order);
|
||||||
|
}
|
||||||
|
|
||||||
|
order = (cmath_uint16_t)in_order;
|
||||||
|
bessel_polynomial(coeff, order, reverse);
|
||||||
|
|
||||||
|
printf("\norder [N]: %d", order);
|
||||||
|
printf("\nreversed bessel: %d\n\n", reverse);
|
||||||
|
printf("list of coefficients:\n");
|
||||||
|
while (i <= order)
|
||||||
|
{
|
||||||
|
printf("order[%2d]: ", (order - i));
|
||||||
|
printf("%"_CMATH_PR_STD_INT"\n", coeff[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
puts("\npolynomial:");
|
||||||
|
printf("y(x) = ");
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (i <= order)
|
||||||
|
{
|
||||||
|
diff = (cmath_int16_t)(order - i);
|
||||||
|
if (diff > 0)
|
||||||
|
if (coeff[i] > 1)
|
||||||
|
{
|
||||||
|
printf("%"_CMATH_PR_STD_INT, coeff[i]);
|
||||||
|
if (diff > 1)
|
||||||
|
printf("*x^%d + ", diff);
|
||||||
|
else
|
||||||
|
printf("*x + ");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
printf("x^%d + ", diff);
|
||||||
|
else
|
||||||
|
printf("%"_CMATH_PR_STD_INT"", coeff[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* */
|
||||||
|
puts("\n\nlist roots:");
|
||||||
|
i = 0;
|
||||||
|
while (i < order+1)
|
||||||
|
{
|
||||||
|
dk_coeff[i] = (cnum_t)coeff[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
durand_kerner(dk_coeff, dk_roots, order);
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (i < order)
|
||||||
|
{
|
||||||
|
printf("root[%2d]: %.15f \t % .15f*i\n",
|
||||||
|
i+1, (double)dk_roots[i].r, (double)dk_roots[i].i);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
87
oversampling/WDL/cmath/test_eval.c
Normal file
87
oversampling/WDL/cmath/test_eval.c
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
/*
|
||||||
|
test_eval.h
|
||||||
|
Copyright (C) 2011 and later Lubomir I. Ivanov (neolit123 [at] gmail)
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied
|
||||||
|
warranty. In no event will the authors be held liable for any damages
|
||||||
|
arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
|
including commercial applications, and to alter it and redistribute it
|
||||||
|
freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not
|
||||||
|
claim that you wrote the original software. If you use this software
|
||||||
|
in a product, an acknowledgment in the product documentation would be
|
||||||
|
appreciated but is not required.
|
||||||
|
2. Altered source versions must be plainly marked as such, and must not be
|
||||||
|
misrepresented as being the original software.
|
||||||
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
test horner.h for complex numbers and other related headers
|
||||||
|
|
||||||
|
gcc -W -Wall -Wextra -ansi pedantic
|
||||||
|
cl /W4 /Za
|
||||||
|
|
||||||
|
reduced precisions for ansi c
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "stdio.h"
|
||||||
|
#include "complex_number.h"
|
||||||
|
#include "horner.h"
|
||||||
|
#include "durand_kerner.h"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
cmath_uint16_t i = 0;
|
||||||
|
|
||||||
|
cnum_t y[] = {2, -6, 2, -1};
|
||||||
|
cnum_s cy[] = {{2, 0}, {-6, 0}, {2, 0}, {-1, 0}};
|
||||||
|
|
||||||
|
cnum_t fx = horner_eval(y, 5, poly_order(y));
|
||||||
|
cnum_s fcx = horner_eval_c(cy, _CNUM(5, 0), poly_order(y));
|
||||||
|
|
||||||
|
cnum_t dk_coeff[] = {12, -7, 0.001, 0, 3, -5};
|
||||||
|
cnum_s dk_roots[5];
|
||||||
|
|
||||||
|
cnum_s dk_coeff_c[] = {{12, 0}, {-7, 0}, {0.001, 0}, {0, 0}, {3, 0}, {-5, 0}};
|
||||||
|
cnum_s dk_roots_c[5];
|
||||||
|
|
||||||
|
durand_kerner(dk_coeff, dk_roots, poly_order(dk_coeff));
|
||||||
|
durand_kerner_c(dk_coeff_c, dk_roots_c, poly_order(dk_coeff_c));
|
||||||
|
|
||||||
|
/* */
|
||||||
|
#ifdef _CMATH_ANSI
|
||||||
|
puts("\n\nansi c is: on");
|
||||||
|
#else
|
||||||
|
puts("\n\nansi c is: off");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* */
|
||||||
|
puts("\n\nevaluate polynomials:\n");
|
||||||
|
printf("* y[]: %.15f\n", (double)fx);
|
||||||
|
printf("* cy[]: %.15f \t %.15f*i\n", (double)fcx.r, (double)fcx.i);
|
||||||
|
|
||||||
|
/* */
|
||||||
|
puts("\nfind roots:");
|
||||||
|
puts("\n* dk_coeff[]:");
|
||||||
|
i = 0;
|
||||||
|
while (i < poly_order(dk_coeff))
|
||||||
|
{
|
||||||
|
printf("root[%2d]: %.15f \t % .15f*i\n",
|
||||||
|
i+1, (double)dk_roots[i].r, (double)dk_roots[i].i);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
i = 0;
|
||||||
|
puts("\n* dk_coeff_c[]:");
|
||||||
|
while (i < poly_order(dk_coeff_c))
|
||||||
|
{
|
||||||
|
printf("root[%2d]: %.15f \t % .15f*i\n",
|
||||||
|
i+1, (double)dk_roots_c[i].r, (double)dk_roots_c[i].i);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
1065
oversampling/WDL/convoengine.cpp
Normal file
1065
oversampling/WDL/convoengine.cpp
Normal file
File diff suppressed because it is too large
Load Diff
179
oversampling/WDL/convoengine.h
Normal file
179
oversampling/WDL/convoengine.h
Normal file
@@ -0,0 +1,179 @@
|
|||||||
|
/*
|
||||||
|
WDL - convoengine.h
|
||||||
|
Copyright (C) 2006 and later Cockos Incorporated
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied
|
||||||
|
warranty. In no event will the authors be held liable for any damages
|
||||||
|
arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
|
including commercial applications, and to alter it and redistribute it
|
||||||
|
freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not
|
||||||
|
claim that you wrote the original software. If you use this software
|
||||||
|
in a product, an acknowledgment in the product documentation would be
|
||||||
|
appreciated but is not required.
|
||||||
|
2. Altered source versions must be plainly marked as such, and must not be
|
||||||
|
misrepresented as being the original software.
|
||||||
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
|
||||||
|
|
||||||
|
This file provides an interface to the WDL fast convolution engine. This engine can convolve audio streams using
|
||||||
|
either brute force (for small impulses), or a partitioned FFT scheme (for larger impulses).
|
||||||
|
|
||||||
|
Note that this library needs to have lookahead ability in order to process samples. Calling Add(somevalue) may produce Avail() < somevalue.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef _WDL_CONVOENGINE_H_
|
||||||
|
#define _WDL_CONVOENGINE_H_
|
||||||
|
|
||||||
|
#include "queue.h"
|
||||||
|
#include "fastqueue.h"
|
||||||
|
#include "fft.h"
|
||||||
|
|
||||||
|
//#define WDL_CONVO_WANT_FULLPRECISION_IMPULSE_STORAGE // define this for slowerness with -138dB error difference in resulting output (+-1 LSB at 24 bit)
|
||||||
|
|
||||||
|
#ifdef WDL_CONVO_WANT_FULLPRECISION_IMPULSE_STORAGE
|
||||||
|
|
||||||
|
typedef WDL_FFT_REAL WDL_CONVO_IMPULSEBUFf;
|
||||||
|
typedef WDL_FFT_COMPLEX WDL_CONVO_IMPULSEBUFCPLXf;
|
||||||
|
|
||||||
|
#else
|
||||||
|
typedef float WDL_CONVO_IMPULSEBUFf;
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
WDL_CONVO_IMPULSEBUFf re, im;
|
||||||
|
}
|
||||||
|
WDL_CONVO_IMPULSEBUFCPLXf;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
class WDL_ImpulseBuffer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
WDL_ImpulseBuffer()
|
||||||
|
{
|
||||||
|
samplerate=44100.0;
|
||||||
|
impulses.list.Add(new WDL_TypedBuf<WDL_FFT_REAL>);
|
||||||
|
}
|
||||||
|
~WDL_ImpulseBuffer()
|
||||||
|
{
|
||||||
|
impulses.list.Empty(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
int GetLength() { return impulses.list.GetSize() ? impulses[0].GetSize() : 0; }
|
||||||
|
int SetLength(int samples); // resizes/clears all channels accordingly, returns actual size set (can be 0 if error)
|
||||||
|
void SetNumChannels(int usench, bool duplicateExisting=true); // handles allocating/converting/etc
|
||||||
|
int GetNumChannels() { return impulses.list.GetSize(); }
|
||||||
|
|
||||||
|
double samplerate;
|
||||||
|
struct {
|
||||||
|
WDL_PtrList<WDL_TypedBuf<WDL_FFT_REAL> > list;
|
||||||
|
|
||||||
|
WDL_TypedBuf<WDL_FFT_REAL> &operator[](size_t i) const
|
||||||
|
{
|
||||||
|
WDL_TypedBuf<WDL_FFT_REAL> *p = list.Get(i);
|
||||||
|
if (WDL_NORMALLY(p != NULL)) return *p;
|
||||||
|
return *list.Get(0); // if for some reason an out of range index was passed, return first item rather than crash
|
||||||
|
}
|
||||||
|
} impulses;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
class WDL_ConvolutionEngine
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
WDL_ConvolutionEngine();
|
||||||
|
~WDL_ConvolutionEngine();
|
||||||
|
|
||||||
|
int SetImpulse(WDL_ImpulseBuffer *impulse, int fft_size=-1, int impulse_sample_offset=0, int max_imp_size=0, bool forceBrute=false);
|
||||||
|
|
||||||
|
int GetFFTSize() { return m_fft_size; }
|
||||||
|
int GetLatency() { return m_fft_size/2; }
|
||||||
|
|
||||||
|
void Reset(); // clears out any latent samples
|
||||||
|
|
||||||
|
void Add(WDL_FFT_REAL **bufs, int len, int nch);
|
||||||
|
|
||||||
|
int Avail(int wantSamples);
|
||||||
|
WDL_FFT_REAL **Get(); // returns length valid
|
||||||
|
void Advance(int len);
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
struct ImpChannelInfo {
|
||||||
|
WDL_TypedBuf<WDL_CONVO_IMPULSEBUFf> imp;
|
||||||
|
WDL_TypedBuf<char> zflag;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ProcChannelInfo {
|
||||||
|
WDL_Queue samplesout;
|
||||||
|
WDL_Queue samplesin2;
|
||||||
|
WDL_FastQueue samplesin;
|
||||||
|
|
||||||
|
WDL_TypedBuf<WDL_FFT_REAL> samplehist; // FFT'd sample blocks per channel
|
||||||
|
WDL_TypedBuf<char> samplehist_zflag;
|
||||||
|
WDL_TypedBuf<WDL_FFT_REAL> overlaphist;
|
||||||
|
|
||||||
|
int hist_pos;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
WDL_PtrList<ImpChannelInfo> m_impdata;
|
||||||
|
|
||||||
|
int m_impulse_len;
|
||||||
|
int m_fft_size;
|
||||||
|
|
||||||
|
int m_proc_nch;
|
||||||
|
WDL_PtrList<ProcChannelInfo> m_proc;
|
||||||
|
|
||||||
|
|
||||||
|
WDL_TypedBuf<WDL_FFT_REAL> m_combinebuf;
|
||||||
|
WDL_TypedBuf<WDL_FFT_REAL *> m_get_tmpptrs;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
// _div stuff
|
||||||
|
int m_zl_delaypos;
|
||||||
|
int m_zl_dumpage;
|
||||||
|
|
||||||
|
//#define WDLCONVO_ZL_ACCOUNTING
|
||||||
|
#ifdef WDLCONVO_ZL_ACCOUNTING
|
||||||
|
int m_zl_fftcnt;//removeme (testing of benchmarks)
|
||||||
|
#endif
|
||||||
|
void AddSilenceToOutput(int len);
|
||||||
|
|
||||||
|
} WDL_FIXALIGN;
|
||||||
|
|
||||||
|
// low latency version
|
||||||
|
class WDL_ConvolutionEngine_Div
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
WDL_ConvolutionEngine_Div();
|
||||||
|
~WDL_ConvolutionEngine_Div();
|
||||||
|
|
||||||
|
int SetImpulse(WDL_ImpulseBuffer *impulse, int maxfft_size=0, int known_blocksize=0, int max_imp_size=0, int impulse_offset=0, int latency_allowed=0);
|
||||||
|
|
||||||
|
int GetLatency();
|
||||||
|
void Reset();
|
||||||
|
|
||||||
|
void Add(WDL_FFT_REAL **bufs, int len, int nch);
|
||||||
|
|
||||||
|
int Avail(int wantSamples);
|
||||||
|
WDL_FFT_REAL **Get(); // returns length valid
|
||||||
|
void Advance(int len);
|
||||||
|
|
||||||
|
private:
|
||||||
|
WDL_PtrList<WDL_ConvolutionEngine> m_engines;
|
||||||
|
|
||||||
|
WDL_PtrList<WDL_Queue> m_sout;
|
||||||
|
WDL_TypedBuf<WDL_FFT_REAL *> m_get_tmpptrs;
|
||||||
|
|
||||||
|
bool m_need_feedsilence;
|
||||||
|
|
||||||
|
} WDL_FIXALIGN;
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
384
oversampling/WDL/coreaudio_channel_formats.h
Normal file
384
oversampling/WDL/coreaudio_channel_formats.h
Normal file
@@ -0,0 +1,384 @@
|
|||||||
|
#ifndef _CAFCHANNELFORMATS_H_
|
||||||
|
#define _CAFCHANNELFORMATS_H_
|
||||||
|
|
||||||
|
// from CoreAudioBaseTypes.h in the ios sdk, good luck finding it online
|
||||||
|
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
// these are identical to the WAVEFORMATEXTENSIBLE bitmask
|
||||||
|
kAudioChannelBit_Left = (1U<<0), // WAVEXT: SPEAKER_FRONT_LEFT
|
||||||
|
kAudioChannelBit_Right = (1U<<1), // WAVEXT: SPEAKER_FRONT_RIGHT
|
||||||
|
kAudioChannelBit_Center = (1U<<2), // WAVEXT: SPEAKER_FRONT_CENTER
|
||||||
|
kAudioChannelBit_LFEScreen = (1U<<3), // WAVEXT: SPEAKER_LOW_FREQUENCY
|
||||||
|
kAudioChannelBit_LeftSurround = (1U<<4), // WAVEXT: SPEAKER_BACK_LEFT
|
||||||
|
kAudioChannelBit_RightSurround = (1U<<5), // WAVEXT: SPEAKER_BACK_RIGHT
|
||||||
|
kAudioChannelBit_LeftCenter = (1U<<6), // WAVEXT: SPEAKER_FRONT_LEFT_OF_CENTER
|
||||||
|
kAudioChannelBit_RightCenter = (1U<<7), // WAVEXT: SPEAKER_FROM_RIGHT_OF_CENTER
|
||||||
|
kAudioChannelBit_CenterSurround = (1U<<8), // WAVEXT: SPEAKER_BACK_CENTER
|
||||||
|
kAudioChannelBit_LeftSurroundDirect = (1U<<9), // WAVEXT: SPEAKER_SIDE_LEFT
|
||||||
|
kAudioChannelBit_RightSurroundDirect = (1U<<10), // WAVEXT: SPEAKER_SIDE_RIGHT
|
||||||
|
kAudioChannelBit_TopCenterSurround = (1U<<11), // WAVEXT: SPEAKER_TOP_CENTER
|
||||||
|
kAudioChannelBit_VerticalHeightLeft = (1U<<12), // WAVEXT: SPEAKER_TOP_FRONT_LEFT
|
||||||
|
kAudioChannelBit_VerticalHeightCenter = (1U<<13), // WAVEXT: SPEAKER_TOP_FRONT_CENTER
|
||||||
|
kAudioChannelBit_VerticalHeightRight = (1U<<14), // WAVEXT: SPEAKER_TOP_FRONT_RIGHT
|
||||||
|
kAudioChannelBit_TopBackLeft = (1U<<15), // WAVEXT: SPEAKER_TOP_BACK_LEFT
|
||||||
|
kAudioChannelBit_TopBackCenter = (1U<<16), // WAVEXT: SPEAKER_TOP_BACK_CENTER
|
||||||
|
kAudioChannelBit_TopBackRight = (1U<<17), // WAVEXT: SPEAKER_TOP_BACK_RIGHT
|
||||||
|
kAudioChannelBit_LeftTopFront = (1U<<18),
|
||||||
|
kAudioChannelBit_CenterTopFront = (1U<<19),
|
||||||
|
kAudioChannelBit_RightTopFront = (1U<<20),
|
||||||
|
kAudioChannelBit_LeftTopMiddle = (1U<<21),
|
||||||
|
kAudioChannelBit_CenterTopMiddle = (1U<<22),
|
||||||
|
kAudioChannelBit_RightTopMiddle = (1U<<23),
|
||||||
|
kAudioChannelBit_LeftTopRear = (1U<<24),
|
||||||
|
kAudioChannelBit_CenterTopRear = (1U<<25),
|
||||||
|
kAudioChannelBit_RightTopRear = (1U<<26),
|
||||||
|
};
|
||||||
|
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
// Some channel abbreviations used below:
|
||||||
|
// L - left
|
||||||
|
// R - right
|
||||||
|
// C - center
|
||||||
|
// Ls - left surround
|
||||||
|
// Rs - right surround
|
||||||
|
// Cs - center surround
|
||||||
|
// Rls - rear left surround
|
||||||
|
// Rrs - rear right surround
|
||||||
|
// Lw - left wide
|
||||||
|
// Rw - right wide
|
||||||
|
// Lsd - left surround direct
|
||||||
|
// Rsd - right surround direct
|
||||||
|
// Lc - left center
|
||||||
|
// Rc - right center
|
||||||
|
// Ts - top surround
|
||||||
|
// Vhl - vertical height left
|
||||||
|
// Vhc - vertical height center
|
||||||
|
// Vhr - vertical height right
|
||||||
|
// Ltf - left top front
|
||||||
|
// Ctf - center top front
|
||||||
|
// Rtf - right top front
|
||||||
|
// Ltm - left top middle
|
||||||
|
// Ctm - center top middle
|
||||||
|
// Rtm - right top middle
|
||||||
|
// Ltr - left top rear
|
||||||
|
// Ctr - center top rear
|
||||||
|
// Rtr - right top rear
|
||||||
|
// Lt - left matrix total. for matrix encoded stereo.
|
||||||
|
// Rt - right matrix total. for matrix encoded stereo.
|
||||||
|
|
||||||
|
// General layouts
|
||||||
|
kAudioChannelLayoutTag_UseChannelDescriptions = (0U<<16) | 0, // use the array of AudioChannelDescriptions to define the mapping.
|
||||||
|
kAudioChannelLayoutTag_UseChannelBitmap = (1U<<16) | 0, // use the bitmap to define the mapping.
|
||||||
|
|
||||||
|
kAudioChannelLayoutTag_Mono = (100U<<16) | 1, // a standard mono stream
|
||||||
|
kAudioChannelLayoutTag_Stereo = (101U<<16) | 2, // a standard stereo stream (L R) - implied playback
|
||||||
|
kAudioChannelLayoutTag_StereoHeadphones = (102U<<16) | 2, // a standard stereo stream (L R) - implied headphone playback
|
||||||
|
kAudioChannelLayoutTag_MatrixStereo = (103U<<16) | 2, // a matrix encoded stereo stream (Lt, Rt)
|
||||||
|
kAudioChannelLayoutTag_MidSide = (104U<<16) | 2, // mid/side recording
|
||||||
|
kAudioChannelLayoutTag_XY = (105U<<16) | 2, // coincident mic pair (often 2 figure 8's)
|
||||||
|
kAudioChannelLayoutTag_Binaural = (106U<<16) | 2, // binaural stereo (left, right)
|
||||||
|
kAudioChannelLayoutTag_Ambisonic_B_Format = (107U<<16) | 4, // W, X, Y, Z
|
||||||
|
|
||||||
|
kAudioChannelLayoutTag_Quadraphonic = (108U<<16) | 4, // L R Ls Rs -- 90 degree speaker separation
|
||||||
|
kAudioChannelLayoutTag_Pentagonal = (109U<<16) | 5, // L R Ls Rs C -- 72 degree speaker separation
|
||||||
|
kAudioChannelLayoutTag_Hexagonal = (110U<<16) | 6, // L R Ls Rs C Cs -- 60 degree speaker separation
|
||||||
|
kAudioChannelLayoutTag_Octagonal = (111U<<16) | 8, // L R Ls Rs C Cs Lw Rw -- 45 degree speaker separation
|
||||||
|
kAudioChannelLayoutTag_Cube = (112U<<16) | 8, // left, right, rear left, rear right
|
||||||
|
// top left, top right, top rear left, top rear right
|
||||||
|
|
||||||
|
// MPEG defined layouts
|
||||||
|
kAudioChannelLayoutTag_MPEG_1_0 = kAudioChannelLayoutTag_Mono, // C
|
||||||
|
kAudioChannelLayoutTag_MPEG_2_0 = kAudioChannelLayoutTag_Stereo, // L R
|
||||||
|
kAudioChannelLayoutTag_MPEG_3_0_A = (113U<<16) | 3, // L R C
|
||||||
|
kAudioChannelLayoutTag_MPEG_3_0_B = (114U<<16) | 3, // C L R
|
||||||
|
kAudioChannelLayoutTag_MPEG_4_0_A = (115U<<16) | 4, // L R C Cs
|
||||||
|
kAudioChannelLayoutTag_MPEG_4_0_B = (116U<<16) | 4, // C L R Cs
|
||||||
|
kAudioChannelLayoutTag_MPEG_5_0_A = (117U<<16) | 5, // L R C Ls Rs
|
||||||
|
kAudioChannelLayoutTag_MPEG_5_0_B = (118U<<16) | 5, // L R Ls Rs C
|
||||||
|
kAudioChannelLayoutTag_MPEG_5_0_C = (119U<<16) | 5, // L C R Ls Rs
|
||||||
|
kAudioChannelLayoutTag_MPEG_5_0_D = (120U<<16) | 5, // C L R Ls Rs
|
||||||
|
kAudioChannelLayoutTag_MPEG_5_1_A = (121U<<16) | 6, // L R C LFE Ls Rs
|
||||||
|
kAudioChannelLayoutTag_MPEG_5_1_B = (122U<<16) | 6, // L R Ls Rs C LFE
|
||||||
|
kAudioChannelLayoutTag_MPEG_5_1_C = (123U<<16) | 6, // L C R Ls Rs LFE
|
||||||
|
kAudioChannelLayoutTag_MPEG_5_1_D = (124U<<16) | 6, // C L R Ls Rs LFE
|
||||||
|
kAudioChannelLayoutTag_MPEG_6_1_A = (125U<<16) | 7, // L R C LFE Ls Rs Cs
|
||||||
|
kAudioChannelLayoutTag_MPEG_7_1_A = (126U<<16) | 8, // L R C LFE Ls Rs Lc Rc
|
||||||
|
kAudioChannelLayoutTag_MPEG_7_1_B = (127U<<16) | 8, // C Lc Rc L R Ls Rs LFE (doc: IS-13818-7 MPEG2-AAC Table 3.1)
|
||||||
|
kAudioChannelLayoutTag_MPEG_7_1_C = (128U<<16) | 8, // L R C LFE Ls Rs Rls Rrs
|
||||||
|
kAudioChannelLayoutTag_Emagic_Default_7_1 = (129U<<16) | 8, // L R Ls Rs C LFE Lc Rc
|
||||||
|
kAudioChannelLayoutTag_SMPTE_DTV = (130U<<16) | 8, // L R C LFE Ls Rs Lt Rt
|
||||||
|
// (kAudioChannelLayoutTag_ITU_5_1 plus a matrix encoded stereo mix)
|
||||||
|
// ITU defined layouts
|
||||||
|
kAudioChannelLayoutTag_ITU_1_0 = kAudioChannelLayoutTag_Mono, // C
|
||||||
|
kAudioChannelLayoutTag_ITU_2_0 = kAudioChannelLayoutTag_Stereo, // L R
|
||||||
|
|
||||||
|
kAudioChannelLayoutTag_ITU_2_1 = (131U<<16) | 3, // L R Cs
|
||||||
|
kAudioChannelLayoutTag_ITU_2_2 = (132U<<16) | 4, // L R Ls Rs
|
||||||
|
kAudioChannelLayoutTag_ITU_3_0 = kAudioChannelLayoutTag_MPEG_3_0_A, // L R C
|
||||||
|
kAudioChannelLayoutTag_ITU_3_1 = kAudioChannelLayoutTag_MPEG_4_0_A, // L R C Cs
|
||||||
|
|
||||||
|
kAudioChannelLayoutTag_ITU_3_2 = kAudioChannelLayoutTag_MPEG_5_0_A, // L R C Ls Rs
|
||||||
|
kAudioChannelLayoutTag_ITU_3_2_1 = kAudioChannelLayoutTag_MPEG_5_1_A, // L R C LFE Ls Rs
|
||||||
|
kAudioChannelLayoutTag_ITU_3_4_1 = kAudioChannelLayoutTag_MPEG_7_1_C, // L R C LFE Ls Rs Rls Rrs
|
||||||
|
|
||||||
|
// DVD defined layouts
|
||||||
|
kAudioChannelLayoutTag_DVD_0 = kAudioChannelLayoutTag_Mono, // C (mono)
|
||||||
|
kAudioChannelLayoutTag_DVD_1 = kAudioChannelLayoutTag_Stereo, // L R
|
||||||
|
kAudioChannelLayoutTag_DVD_2 = kAudioChannelLayoutTag_ITU_2_1, // L R Cs
|
||||||
|
kAudioChannelLayoutTag_DVD_3 = kAudioChannelLayoutTag_ITU_2_2, // L R Ls Rs
|
||||||
|
kAudioChannelLayoutTag_DVD_4 = (133U<<16) | 3, // L R LFE
|
||||||
|
kAudioChannelLayoutTag_DVD_5 = (134U<<16) | 4, // L R LFE Cs
|
||||||
|
kAudioChannelLayoutTag_DVD_6 = (135U<<16) | 5, // L R LFE Ls Rs
|
||||||
|
kAudioChannelLayoutTag_DVD_7 = kAudioChannelLayoutTag_MPEG_3_0_A, // L R C
|
||||||
|
kAudioChannelLayoutTag_DVD_8 = kAudioChannelLayoutTag_MPEG_4_0_A, // L R C Cs
|
||||||
|
kAudioChannelLayoutTag_DVD_9 = kAudioChannelLayoutTag_MPEG_5_0_A, // L R C Ls Rs
|
||||||
|
kAudioChannelLayoutTag_DVD_10 = (136U<<16) | 4, // L R C LFE
|
||||||
|
kAudioChannelLayoutTag_DVD_11 = (137U<<16) | 5, // L R C LFE Cs
|
||||||
|
kAudioChannelLayoutTag_DVD_12 = kAudioChannelLayoutTag_MPEG_5_1_A, // L R C LFE Ls Rs
|
||||||
|
// 13 through 17 are duplicates of 8 through 12.
|
||||||
|
kAudioChannelLayoutTag_DVD_13 = kAudioChannelLayoutTag_DVD_8, // L R C Cs
|
||||||
|
kAudioChannelLayoutTag_DVD_14 = kAudioChannelLayoutTag_DVD_9, // L R C Ls Rs
|
||||||
|
kAudioChannelLayoutTag_DVD_15 = kAudioChannelLayoutTag_DVD_10, // L R C LFE
|
||||||
|
kAudioChannelLayoutTag_DVD_16 = kAudioChannelLayoutTag_DVD_11, // L R C LFE Cs
|
||||||
|
kAudioChannelLayoutTag_DVD_17 = kAudioChannelLayoutTag_DVD_12, // L R C LFE Ls Rs
|
||||||
|
kAudioChannelLayoutTag_DVD_18 = (138U<<16) | 5, // L R Ls Rs LFE
|
||||||
|
kAudioChannelLayoutTag_DVD_19 = kAudioChannelLayoutTag_MPEG_5_0_B, // L R Ls Rs C
|
||||||
|
kAudioChannelLayoutTag_DVD_20 = kAudioChannelLayoutTag_MPEG_5_1_B, // L R Ls Rs C LFE
|
||||||
|
|
||||||
|
// These layouts are recommended for AudioUnit usage
|
||||||
|
// These are the symmetrical layouts
|
||||||
|
kAudioChannelLayoutTag_AudioUnit_4 = kAudioChannelLayoutTag_Quadraphonic,
|
||||||
|
kAudioChannelLayoutTag_AudioUnit_5 = kAudioChannelLayoutTag_Pentagonal,
|
||||||
|
kAudioChannelLayoutTag_AudioUnit_6 = kAudioChannelLayoutTag_Hexagonal,
|
||||||
|
kAudioChannelLayoutTag_AudioUnit_8 = kAudioChannelLayoutTag_Octagonal,
|
||||||
|
// These are the surround-based layouts
|
||||||
|
kAudioChannelLayoutTag_AudioUnit_5_0 = kAudioChannelLayoutTag_MPEG_5_0_B, // L R Ls Rs C
|
||||||
|
kAudioChannelLayoutTag_AudioUnit_6_0 = (139U<<16) | 6, // L R Ls Rs C Cs
|
||||||
|
kAudioChannelLayoutTag_AudioUnit_7_0 = (140U<<16) | 7, // L R Ls Rs C Rls Rrs
|
||||||
|
kAudioChannelLayoutTag_AudioUnit_7_0_Front = (148U<<16) | 7, // L R Ls Rs C Lc Rc
|
||||||
|
kAudioChannelLayoutTag_AudioUnit_5_1 = kAudioChannelLayoutTag_MPEG_5_1_A, // L R C LFE Ls Rs
|
||||||
|
kAudioChannelLayoutTag_AudioUnit_6_1 = kAudioChannelLayoutTag_MPEG_6_1_A, // L R C LFE Ls Rs Cs
|
||||||
|
kAudioChannelLayoutTag_AudioUnit_7_1 = kAudioChannelLayoutTag_MPEG_7_1_C, // L R C LFE Ls Rs Rls Rrs
|
||||||
|
kAudioChannelLayoutTag_AudioUnit_7_1_Front = kAudioChannelLayoutTag_MPEG_7_1_A, // L R C LFE Ls Rs Lc Rc
|
||||||
|
|
||||||
|
kAudioChannelLayoutTag_AAC_3_0 = kAudioChannelLayoutTag_MPEG_3_0_B, // C L R
|
||||||
|
kAudioChannelLayoutTag_AAC_Quadraphonic = kAudioChannelLayoutTag_Quadraphonic, // L R Ls Rs
|
||||||
|
kAudioChannelLayoutTag_AAC_4_0 = kAudioChannelLayoutTag_MPEG_4_0_B, // C L R Cs
|
||||||
|
kAudioChannelLayoutTag_AAC_5_0 = kAudioChannelLayoutTag_MPEG_5_0_D, // C L R Ls Rs
|
||||||
|
kAudioChannelLayoutTag_AAC_5_1 = kAudioChannelLayoutTag_MPEG_5_1_D, // C L R Ls Rs Lfe
|
||||||
|
kAudioChannelLayoutTag_AAC_6_0 = (141U<<16) | 6, // C L R Ls Rs Cs
|
||||||
|
kAudioChannelLayoutTag_AAC_6_1 = (142U<<16) | 7, // C L R Ls Rs Cs Lfe
|
||||||
|
kAudioChannelLayoutTag_AAC_7_0 = (143U<<16) | 7, // C L R Ls Rs Rls Rrs
|
||||||
|
kAudioChannelLayoutTag_AAC_7_1 = kAudioChannelLayoutTag_MPEG_7_1_B, // C Lc Rc L R Ls Rs Lfe
|
||||||
|
kAudioChannelLayoutTag_AAC_7_1_B = (183U<<16) | 8, // C L R Ls Rs Rls Rrs LFE
|
||||||
|
kAudioChannelLayoutTag_AAC_7_1_C = (184U<<16) | 8, // C L R Ls Rs LFE Vhl Vhr
|
||||||
|
kAudioChannelLayoutTag_AAC_Octagonal = (144U<<16) | 8, // C L R Ls Rs Rls Rrs Cs
|
||||||
|
|
||||||
|
kAudioChannelLayoutTag_TMH_10_2_std = (145U<<16) | 16, // L R C Vhc Lsd Rsd Ls Rs Vhl Vhr Lw Rw Csd Cs LFE1 LFE2
|
||||||
|
kAudioChannelLayoutTag_TMH_10_2_full = (146U<<16) | 21, // TMH_10_2_std plus: Lc Rc HI VI Haptic
|
||||||
|
|
||||||
|
kAudioChannelLayoutTag_AC3_1_0_1 = (149U<<16) | 2, // C LFE
|
||||||
|
kAudioChannelLayoutTag_AC3_3_0 = (150U<<16) | 3, // L C R
|
||||||
|
kAudioChannelLayoutTag_AC3_3_1 = (151U<<16) | 4, // L C R Cs
|
||||||
|
kAudioChannelLayoutTag_AC3_3_0_1 = (152U<<16) | 4, // L C R LFE
|
||||||
|
kAudioChannelLayoutTag_AC3_2_1_1 = (153U<<16) | 4, // L R Cs LFE
|
||||||
|
kAudioChannelLayoutTag_AC3_3_1_1 = (154U<<16) | 5, // L C R Cs LFE
|
||||||
|
|
||||||
|
kAudioChannelLayoutTag_EAC_6_0_A = (155U<<16) | 6, // L C R Ls Rs Cs
|
||||||
|
kAudioChannelLayoutTag_EAC_7_0_A = (156U<<16) | 7, // L C R Ls Rs Rls Rrs
|
||||||
|
|
||||||
|
kAudioChannelLayoutTag_EAC3_6_1_A = (157U<<16) | 7, // L C R Ls Rs LFE Cs
|
||||||
|
kAudioChannelLayoutTag_EAC3_6_1_B = (158U<<16) | 7, // L C R Ls Rs LFE Ts
|
||||||
|
kAudioChannelLayoutTag_EAC3_6_1_C = (159U<<16) | 7, // L C R Ls Rs LFE Vhc
|
||||||
|
kAudioChannelLayoutTag_EAC3_7_1_A = (160U<<16) | 8, // L C R Ls Rs LFE Rls Rrs
|
||||||
|
kAudioChannelLayoutTag_EAC3_7_1_B = (161U<<16) | 8, // L C R Ls Rs LFE Lc Rc
|
||||||
|
kAudioChannelLayoutTag_EAC3_7_1_C = (162U<<16) | 8, // L C R Ls Rs LFE Lsd Rsd
|
||||||
|
kAudioChannelLayoutTag_EAC3_7_1_D = (163U<<16) | 8, // L C R Ls Rs LFE Lw Rw
|
||||||
|
kAudioChannelLayoutTag_EAC3_7_1_E = (164U<<16) | 8, // L C R Ls Rs LFE Vhl Vhr
|
||||||
|
|
||||||
|
kAudioChannelLayoutTag_EAC3_7_1_F = (165U<<16) | 8, // L C R Ls Rs LFE Cs Ts
|
||||||
|
kAudioChannelLayoutTag_EAC3_7_1_G = (166U<<16) | 8, // L C R Ls Rs LFE Cs Vhc
|
||||||
|
kAudioChannelLayoutTag_EAC3_7_1_H = (167U<<16) | 8, // L C R Ls Rs LFE Ts Vhc
|
||||||
|
|
||||||
|
kAudioChannelLayoutTag_DTS_3_1 = (168U<<16) | 4, // C L R LFE
|
||||||
|
kAudioChannelLayoutTag_DTS_4_1 = (169U<<16) | 5, // C L R Cs LFE
|
||||||
|
kAudioChannelLayoutTag_DTS_6_0_A = (170U<<16) | 6, // Lc Rc L R Ls Rs
|
||||||
|
kAudioChannelLayoutTag_DTS_6_0_B = (171U<<16) | 6, // C L R Rls Rrs Ts
|
||||||
|
kAudioChannelLayoutTag_DTS_6_0_C = (172U<<16) | 6, // C Cs L R Rls Rrs
|
||||||
|
kAudioChannelLayoutTag_DTS_6_1_A = (173U<<16) | 7, // Lc Rc L R Ls Rs LFE
|
||||||
|
kAudioChannelLayoutTag_DTS_6_1_B = (174U<<16) | 7, // C L R Rls Rrs Ts LFE
|
||||||
|
kAudioChannelLayoutTag_DTS_6_1_C = (175U<<16) | 7, // C Cs L R Rls Rrs LFE
|
||||||
|
kAudioChannelLayoutTag_DTS_7_0 = (176U<<16) | 7, // Lc C Rc L R Ls Rs
|
||||||
|
kAudioChannelLayoutTag_DTS_7_1 = (177U<<16) | 8, // Lc C Rc L R Ls Rs LFE
|
||||||
|
kAudioChannelLayoutTag_DTS_8_0_A = (178U<<16) | 8, // Lc Rc L R Ls Rs Rls Rrs
|
||||||
|
kAudioChannelLayoutTag_DTS_8_0_B = (179U<<16) | 8, // Lc C Rc L R Ls Cs Rs
|
||||||
|
kAudioChannelLayoutTag_DTS_8_1_A = (180U<<16) | 9, // Lc Rc L R Ls Rs Rls Rrs LFE
|
||||||
|
kAudioChannelLayoutTag_DTS_8_1_B = (181U<<16) | 9, // Lc C Rc L R Ls Cs Rs LFE
|
||||||
|
kAudioChannelLayoutTag_DTS_6_1_D = (182U<<16) | 7, // C L R Ls Rs LFE Cs
|
||||||
|
|
||||||
|
kAudioChannelLayoutTag_WAVE_2_1 = kAudioChannelLayoutTag_DVD_4, // 3 channels, L R LFE
|
||||||
|
kAudioChannelLayoutTag_WAVE_3_0 = kAudioChannelLayoutTag_MPEG_3_0_A, // 3 channels, L R C
|
||||||
|
kAudioChannelLayoutTag_WAVE_4_0_A = kAudioChannelLayoutTag_ITU_2_2, // 4 channels, L R Ls Rs
|
||||||
|
kAudioChannelLayoutTag_WAVE_4_0_B = (185U<<16) | 4, // 4 channels, L R Rls Rrs
|
||||||
|
kAudioChannelLayoutTag_WAVE_5_0_A = kAudioChannelLayoutTag_MPEG_5_0_A, // 5 channels, L R C Ls Rs
|
||||||
|
kAudioChannelLayoutTag_WAVE_5_0_B = (186U<<16) | 5, // 5 channels, L R C Rls Rrs
|
||||||
|
kAudioChannelLayoutTag_WAVE_5_1_A = kAudioChannelLayoutTag_MPEG_5_1_A, // 6 channels, L R C LFE Ls Rs
|
||||||
|
kAudioChannelLayoutTag_WAVE_5_1_B = (187U<<16) | 6, // 6 channels, L R C LFE Rls Rrs
|
||||||
|
kAudioChannelLayoutTag_WAVE_6_1 = (188U<<16) | 7, // 7 channels, L R C LFE Cs Ls Rs
|
||||||
|
kAudioChannelLayoutTag_WAVE_7_1 = (189U<<16) | 8, // 8 channels, L R C LFE Rls Rrs Ls Rs
|
||||||
|
|
||||||
|
kAudioChannelLayoutTag_HOA_ACN_SN3D = (190U<<16) | 0, // Higher Order Ambisonics, Ambisonics Channel Number, SN3D normalization
|
||||||
|
// needs to be ORed with the actual number of channels (not the HOA order)
|
||||||
|
kAudioChannelLayoutTag_HOA_ACN_N3D = (191U<<16) | 0, // Higher Order Ambisonics, Ambisonics Channel Number, N3D normalization
|
||||||
|
// needs to be ORed with the actual number of channels (not the HOA order)
|
||||||
|
|
||||||
|
kAudioChannelLayoutTag_Atmos_5_1_2 = (194U<<16) | 8, ///< L R C LFE Ls Rs Ltm Rtm
|
||||||
|
kAudioChannelLayoutTag_Atmos_5_1_4 = (195U<<16) | 10, ///< L R C LFE Ls Rs Vhl Vhr Ltr Rtr
|
||||||
|
kAudioChannelLayoutTag_Atmos_7_1_2 = (196U<<16) | 10, ///< L R C LFE Ls Rs Rls Rrs Ltm Rtm
|
||||||
|
kAudioChannelLayoutTag_Atmos_7_1_4 = (192U<<16) | 12, ///< L R C LFE Ls Rs Rls Rrs Vhl Vhr Ltr Rtr
|
||||||
|
kAudioChannelLayoutTag_Atmos_9_1_6 = (193U<<16) | 16, ///< L R C LFE Ls Rs Rls Rrs Lw Rw Vhl Vhr Ltm Rtm Ltr Rtr // L R C LFE Ls Rs Ltm Rtm
|
||||||
|
|
||||||
|
kAudioChannelLayoutTag_DiscreteInOrder = (147U<<16) | 0, ///< needs to be ORed with the actual number of channels // needs to be ORed with the actual number of channels
|
||||||
|
|
||||||
|
kAudioChannelLayoutTag_BeginReserved = 0xF0000000, // Channel layout tag values in this range are reserved for internal use
|
||||||
|
kAudioChannelLayoutTag_EndReserved = 0xFFFEFFFF,
|
||||||
|
|
||||||
|
kAudioChannelLayoutTag_Unknown = 0xFFFF0000 // needs to be ORed with the actual number of channels
|
||||||
|
};
|
||||||
|
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
kAudioChannelLabel_Unknown = 0xFFFFFFFF, // unknown or unspecified other use
|
||||||
|
kAudioChannelLabel_Unused = 0, // channel is present, but has no intended use or destination
|
||||||
|
kAudioChannelLabel_UseCoordinates = 100, // channel is described by the mCoordinates fields.
|
||||||
|
|
||||||
|
kAudioChannelLabel_Left = 1,
|
||||||
|
kAudioChannelLabel_Right = 2,
|
||||||
|
kAudioChannelLabel_Center = 3,
|
||||||
|
kAudioChannelLabel_LFEScreen = 4,
|
||||||
|
kAudioChannelLabel_LeftSurround = 5,
|
||||||
|
kAudioChannelLabel_RightSurround = 6,
|
||||||
|
kAudioChannelLabel_LeftCenter = 7,
|
||||||
|
kAudioChannelLabel_RightCenter = 8,
|
||||||
|
kAudioChannelLabel_CenterSurround = 9, // WAVE: "Back Center" or plain "Rear Surround"
|
||||||
|
kAudioChannelLabel_LeftSurroundDirect = 10,
|
||||||
|
kAudioChannelLabel_RightSurroundDirect = 11,
|
||||||
|
kAudioChannelLabel_TopCenterSurround = 12,
|
||||||
|
kAudioChannelLabel_VerticalHeightLeft = 13, // WAVE: "Top Front Left"
|
||||||
|
kAudioChannelLabel_VerticalHeightCenter = 14, // WAVE: "Top Front Center"
|
||||||
|
kAudioChannelLabel_VerticalHeightRight = 15, // WAVE: "Top Front Right"
|
||||||
|
|
||||||
|
kAudioChannelLabel_TopBackLeft = 16,
|
||||||
|
kAudioChannelLabel_TopBackCenter = 17,
|
||||||
|
kAudioChannelLabel_TopBackRight = 18,
|
||||||
|
|
||||||
|
kAudioChannelLabel_RearSurroundLeft = 33,
|
||||||
|
kAudioChannelLabel_RearSurroundRight = 34,
|
||||||
|
kAudioChannelLabel_LeftWide = 35,
|
||||||
|
kAudioChannelLabel_RightWide = 36,
|
||||||
|
kAudioChannelLabel_LFE2 = 37,
|
||||||
|
kAudioChannelLabel_LeftTotal = 38, // matrix encoded 4 channels
|
||||||
|
kAudioChannelLabel_RightTotal = 39, // matrix encoded 4 channels
|
||||||
|
kAudioChannelLabel_HearingImpaired = 40,
|
||||||
|
kAudioChannelLabel_Narration = 41,
|
||||||
|
kAudioChannelLabel_Mono = 42,
|
||||||
|
kAudioChannelLabel_DialogCentricMix = 43,
|
||||||
|
|
||||||
|
kAudioChannelLabel_CenterSurroundDirect = 44, // back center, non diffuse
|
||||||
|
|
||||||
|
kAudioChannelLabel_Haptic = 45,
|
||||||
|
|
||||||
|
kAudioChannelLabel_LeftTopFront = 46,
|
||||||
|
kAudioChannelLabel_CenterTopFront = 47,
|
||||||
|
kAudioChannelLabel_RightTopFront = 48,
|
||||||
|
kAudioChannelLabel_LeftTopMiddle = 49,
|
||||||
|
kAudioChannelLabel_CenterTopMiddle = 50,
|
||||||
|
kAudioChannelLabel_RightTopMiddle = 51,
|
||||||
|
kAudioChannelLabel_LeftTopRear = 52,
|
||||||
|
kAudioChannelLabel_CenterTopRear = 53,
|
||||||
|
kAudioChannelLabel_RightTopRear = 54,
|
||||||
|
|
||||||
|
// first order ambisonic channels
|
||||||
|
kAudioChannelLabel_Ambisonic_W = 200,
|
||||||
|
kAudioChannelLabel_Ambisonic_X = 201,
|
||||||
|
kAudioChannelLabel_Ambisonic_Y = 202,
|
||||||
|
kAudioChannelLabel_Ambisonic_Z = 203,
|
||||||
|
|
||||||
|
// Mid/Side Recording
|
||||||
|
kAudioChannelLabel_MS_Mid = 204,
|
||||||
|
kAudioChannelLabel_MS_Side = 205,
|
||||||
|
|
||||||
|
// X-Y Recording
|
||||||
|
kAudioChannelLabel_XY_X = 206,
|
||||||
|
kAudioChannelLabel_XY_Y = 207,
|
||||||
|
|
||||||
|
// Binaural Recording
|
||||||
|
kAudioChannelLabel_BinauralLeft = 208,
|
||||||
|
kAudioChannelLabel_BinauralRight = 209,
|
||||||
|
|
||||||
|
// other
|
||||||
|
kAudioChannelLabel_HeadphonesLeft = 301,
|
||||||
|
kAudioChannelLabel_HeadphonesRight = 302,
|
||||||
|
kAudioChannelLabel_ClickTrack = 304,
|
||||||
|
kAudioChannelLabel_ForeignLanguage = 305,
|
||||||
|
|
||||||
|
// generic discrete channel
|
||||||
|
kAudioChannelLabel_Discrete = 400,
|
||||||
|
|
||||||
|
// numbered discrete channel
|
||||||
|
kAudioChannelLabel_Discrete_0 = (1U<<16) | 0,
|
||||||
|
kAudioChannelLabel_Discrete_1 = (1U<<16) | 1,
|
||||||
|
kAudioChannelLabel_Discrete_2 = (1U<<16) | 2,
|
||||||
|
kAudioChannelLabel_Discrete_3 = (1U<<16) | 3,
|
||||||
|
kAudioChannelLabel_Discrete_4 = (1U<<16) | 4,
|
||||||
|
kAudioChannelLabel_Discrete_5 = (1U<<16) | 5,
|
||||||
|
kAudioChannelLabel_Discrete_6 = (1U<<16) | 6,
|
||||||
|
kAudioChannelLabel_Discrete_7 = (1U<<16) | 7,
|
||||||
|
kAudioChannelLabel_Discrete_8 = (1U<<16) | 8,
|
||||||
|
kAudioChannelLabel_Discrete_9 = (1U<<16) | 9,
|
||||||
|
kAudioChannelLabel_Discrete_10 = (1U<<16) | 10,
|
||||||
|
kAudioChannelLabel_Discrete_11 = (1U<<16) | 11,
|
||||||
|
kAudioChannelLabel_Discrete_12 = (1U<<16) | 12,
|
||||||
|
kAudioChannelLabel_Discrete_13 = (1U<<16) | 13,
|
||||||
|
kAudioChannelLabel_Discrete_14 = (1U<<16) | 14,
|
||||||
|
kAudioChannelLabel_Discrete_15 = (1U<<16) | 15,
|
||||||
|
kAudioChannelLabel_Discrete_65535 = (1U<<16) | 65535,
|
||||||
|
|
||||||
|
// generic HOA ACN channel
|
||||||
|
kAudioChannelLabel_HOA_ACN = 500,
|
||||||
|
|
||||||
|
// numbered HOA ACN channels
|
||||||
|
kAudioChannelLabel_HOA_ACN_0 = (2U << 16) | 0,
|
||||||
|
kAudioChannelLabel_HOA_ACN_1 = (2U << 16) | 1,
|
||||||
|
kAudioChannelLabel_HOA_ACN_2 = (2U << 16) | 2,
|
||||||
|
kAudioChannelLabel_HOA_ACN_3 = (2U << 16) | 3,
|
||||||
|
kAudioChannelLabel_HOA_ACN_4 = (2U << 16) | 4,
|
||||||
|
kAudioChannelLabel_HOA_ACN_5 = (2U << 16) | 5,
|
||||||
|
kAudioChannelLabel_HOA_ACN_6 = (2U << 16) | 6,
|
||||||
|
kAudioChannelLabel_HOA_ACN_7 = (2U << 16) | 7,
|
||||||
|
kAudioChannelLabel_HOA_ACN_8 = (2U << 16) | 8,
|
||||||
|
kAudioChannelLabel_HOA_ACN_9 = (2U << 16) | 9,
|
||||||
|
kAudioChannelLabel_HOA_ACN_10 = (2U << 16) | 10,
|
||||||
|
kAudioChannelLabel_HOA_ACN_11 = (2U << 16) | 11,
|
||||||
|
kAudioChannelLabel_HOA_ACN_12 = (2U << 16) | 12,
|
||||||
|
kAudioChannelLabel_HOA_ACN_13 = (2U << 16) | 13,
|
||||||
|
kAudioChannelLabel_HOA_ACN_14 = (2U << 16) | 14,
|
||||||
|
kAudioChannelLabel_HOA_ACN_15 = (2U << 16) | 15,
|
||||||
|
kAudioChannelLabel_HOA_ACN_65024 = (2U << 16) | 65024, // 254th order uses 65025 channels
|
||||||
|
|
||||||
|
kAudioChannelLabel_BeginReserved = 0xF0000000, // Channel label values in this range are reserved for internal use
|
||||||
|
kAudioChannelLabel_EndReserved = 0xFFFFFFFE
|
||||||
|
};
|
||||||
|
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
kAudioChannelFlags_AllOff = 0,
|
||||||
|
kAudioChannelFlags_RectangularCoordinates = (1U<<0),
|
||||||
|
kAudioChannelFlags_SphericalCoordinates = (1U<<1),
|
||||||
|
kAudioChannelFlags_Meters = (1U<<2)
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif // _CAFCHANNELFORMATS_H_
|
||||||
22
oversampling/WDL/db2val.h
Normal file
22
oversampling/WDL/db2val.h
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#ifndef _WDL_DB2VAL_H_
|
||||||
|
#define _WDL_DB2VAL_H_
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#define TWENTY_OVER_LN10 8.6858896380650365530225783783321
|
||||||
|
#define LN10_OVER_TWENTY 0.11512925464970228420089957273422
|
||||||
|
#define DB2VAL(x) exp((x)*LN10_OVER_TWENTY)
|
||||||
|
|
||||||
|
static inline double VAL2DB(double x)
|
||||||
|
{
|
||||||
|
if (x < 0.0000000298023223876953125) return -150.0;
|
||||||
|
double v=log(x)*TWENTY_OVER_LN10;
|
||||||
|
return v<-150.0?-150.0:v;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline double VAL2DB_EX(double x, double mindb)
|
||||||
|
{
|
||||||
|
return x <= DB2VAL(mindb) ? mindb : (log(x)*TWENTY_OVER_LN10);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
180
oversampling/WDL/delay_line.h
Normal file
180
oversampling/WDL/delay_line.h
Normal file
@@ -0,0 +1,180 @@
|
|||||||
|
#ifndef _WDL_DELAY_LINE_H_
|
||||||
|
#define _WDL_DELAY_LINE_H_
|
||||||
|
|
||||||
|
#include "circbuf.h"
|
||||||
|
|
||||||
|
template<class SampleType> class WDL_DelayLine {
|
||||||
|
public:
|
||||||
|
WDL_DelayLine() : m_nch(1) { }
|
||||||
|
~WDL_DelayLine() { }
|
||||||
|
|
||||||
|
// call before accessing anything.
|
||||||
|
// total_size is maximum delay line length required (preserves contents on resize)
|
||||||
|
// if valid_size >=0, ensures delay line has exactly valid_size pairs set (must be <= total_size!)
|
||||||
|
void set_nch_length(int nch, int total_size, int valid_size=-1)
|
||||||
|
{
|
||||||
|
if (WDL_NOT_NORMALLY(valid_size > total_size)) valid_size=total_size;
|
||||||
|
|
||||||
|
int avail = get_avail_pairs();
|
||||||
|
const int minsz = valid_size >= 0 ? valid_size : total_size;
|
||||||
|
if (avail > minsz)
|
||||||
|
{
|
||||||
|
skip_pairs(avail-minsz);
|
||||||
|
avail = minsz;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_nch != nch && WDL_NORMALLY(nch>0))
|
||||||
|
{
|
||||||
|
if (nch > m_nch) m_q.SetSizePreserveContents(total_size*nch);
|
||||||
|
|
||||||
|
SampleType work[2048];
|
||||||
|
const int chunk = 2048 / wdl_max(nch,m_nch);
|
||||||
|
int nleft = avail;
|
||||||
|
while (nleft > 0)
|
||||||
|
{
|
||||||
|
const int a = wdl_min(chunk,nleft);
|
||||||
|
nleft-=a;
|
||||||
|
|
||||||
|
m_q.Get(work,a*m_nch);
|
||||||
|
VALIDATE_BUFFER(work,a*m_nch);
|
||||||
|
reinterleave_buffer(work,m_nch,nch,a);
|
||||||
|
m_q.Add(work,a*nch);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nch < m_nch) m_q.SetSizePreserveContents(total_size*nch);
|
||||||
|
|
||||||
|
m_nch=nch;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
const int newsz = total_size*nch, cursz = m_q.GetTotalSize();
|
||||||
|
if (newsz > cursz || newsz < cursz/2) m_q.SetSizePreserveContents(newsz);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (valid_size > 0)
|
||||||
|
{
|
||||||
|
const int need_extra = valid_size - get_avail_pairs();
|
||||||
|
if (need_extra > 0)
|
||||||
|
{
|
||||||
|
// insert zero data at read pointer in delay line
|
||||||
|
m_q.Skip(-need_extra*nch);
|
||||||
|
m_q.WriteAtReadPointer(NULL,need_extra*nch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void add_pairs(const SampleType *buf, int pairs) // pushes data off old end of queue
|
||||||
|
{
|
||||||
|
WDL_ASSERT(pairs>=0);
|
||||||
|
if (pairs <= 0) return;
|
||||||
|
|
||||||
|
VALIDATE_BUFFER(buf,pairs*m_nch);
|
||||||
|
|
||||||
|
int add_sz = pairs*m_nch;
|
||||||
|
if (add_sz > m_q.NbFree())
|
||||||
|
{
|
||||||
|
if (add_sz > m_q.GetTotalSize())
|
||||||
|
{
|
||||||
|
if (buf) buf += add_sz - m_q.GetTotalSize();
|
||||||
|
add_sz = m_q.GetTotalSize();
|
||||||
|
}
|
||||||
|
m_q.Skip(add_sz - m_q.NbFree());
|
||||||
|
}
|
||||||
|
|
||||||
|
const int added = m_q.Add(buf,add_sz);
|
||||||
|
if (added != add_sz) { WDL_ASSERT(added == add_sz); }
|
||||||
|
}
|
||||||
|
|
||||||
|
void unadd_pairs(int pairs)
|
||||||
|
{
|
||||||
|
WDL_ASSERT(pairs>=0);
|
||||||
|
if (pairs>0) m_q.UnAdd(pairs*m_nch);
|
||||||
|
}
|
||||||
|
|
||||||
|
int peek_pairs(SampleType *buf, int pairs, int offs=0) // allow to request more than available, returns amt returned
|
||||||
|
{
|
||||||
|
WDL_ASSERT(offs >= 0);
|
||||||
|
const int avail = get_avail_pairs();
|
||||||
|
const int rdsize = wdl_min(avail - offs, pairs);
|
||||||
|
if (rdsize <= 0) return 0;
|
||||||
|
|
||||||
|
int ret = m_q.Peek(buf,offs*m_nch,rdsize*m_nch);
|
||||||
|
WDL_ASSERT(ret == rdsize*m_nch);
|
||||||
|
VALIDATE_BUFFER(buf,rdsize);
|
||||||
|
return ret/m_nch;
|
||||||
|
}
|
||||||
|
|
||||||
|
void get_pairs(SampleType *buf, int pairs) // asserts if pairs > available
|
||||||
|
{
|
||||||
|
int amt;
|
||||||
|
WDL_ASSERT(pairs <= get_avail_pairs());
|
||||||
|
if (buf)
|
||||||
|
{
|
||||||
|
amt = peek_pairs(buf,pairs,0);
|
||||||
|
WDL_ASSERT(amt == pairs);
|
||||||
|
VALIDATE_BUFFER(buf,amt);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
amt = pairs;
|
||||||
|
}
|
||||||
|
skip_pairs(amt);
|
||||||
|
}
|
||||||
|
|
||||||
|
void skip_pairs(int samt) { if (samt > 0) m_q.Skip(samt*m_nch); }
|
||||||
|
int get_avail_pairs() const { return m_q.NbInBuf()/m_nch; }
|
||||||
|
|
||||||
|
void free_memory() { m_q.SetSize(0); } // frees memory
|
||||||
|
void clear() { m_q.Reset(); } // keeps max buffer size intact but clears contents
|
||||||
|
|
||||||
|
static void reinterleave_buffer(SampleType *rdptr, int in_nch, int out_nch, int len)
|
||||||
|
{
|
||||||
|
if (len < 1 || !rdptr) return;
|
||||||
|
|
||||||
|
int x=len-1;
|
||||||
|
SampleType *wrptr = rdptr;
|
||||||
|
if (out_nch < in_nch)
|
||||||
|
{
|
||||||
|
const int sz1=out_nch*sizeof(SampleType);
|
||||||
|
while (x--)
|
||||||
|
{
|
||||||
|
rdptr += in_nch;
|
||||||
|
wrptr += out_nch;
|
||||||
|
memmove(wrptr,rdptr,sz1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (out_nch > in_nch)
|
||||||
|
{
|
||||||
|
const int sz1=in_nch*sizeof(SampleType);
|
||||||
|
const int sz2=(out_nch-in_nch)*sizeof(SampleType);
|
||||||
|
rdptr += in_nch*x;
|
||||||
|
wrptr += out_nch*x;
|
||||||
|
while(x--)
|
||||||
|
{
|
||||||
|
memmove(wrptr,rdptr,sz1);
|
||||||
|
memset(wrptr+in_nch,0,sz2);
|
||||||
|
|
||||||
|
rdptr-=in_nch;
|
||||||
|
wrptr-=out_nch;
|
||||||
|
}
|
||||||
|
memset(wrptr+in_nch,0,sz2); // last iteration doesnt need memcpy (but does need clear)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
WDL_TypedCircBuf<SampleType> m_q;
|
||||||
|
int m_nch;
|
||||||
|
|
||||||
|
static void VALIDATE_BUFFER(const SampleType *buf, int cnt)
|
||||||
|
{
|
||||||
|
#ifdef _DEBUG
|
||||||
|
if (buf) for (int x = 0; x < cnt; x ++)
|
||||||
|
{
|
||||||
|
double v = buf[x];
|
||||||
|
WDL_ASSERT(v >= -40000.0 && v < 40000.0);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
248
oversampling/WDL/denormal.h
Normal file
248
oversampling/WDL/denormal.h
Normal file
@@ -0,0 +1,248 @@
|
|||||||
|
#ifndef _WDL_DENORMAL_H_
|
||||||
|
#define _WDL_DENORMAL_H_
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include "wdltypes.h"
|
||||||
|
// note: the _aggressive versions filter out anything less than around 1.0e-16 or so (approximately) to 0.0, including -0.0 (becomes 0.0)
|
||||||
|
// note: new! the _aggressive versions also filter inf and NaN to 0.0
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
#define WDL_DENORMAL_INLINE inline
|
||||||
|
#elif defined(_MSC_VER)
|
||||||
|
#define WDL_DENORMAL_INLINE __inline
|
||||||
|
#else
|
||||||
|
#ifdef WDL_STATICFUNC_UNUSED
|
||||||
|
#define WDL_DENORMAL_INLINE WDL_STATICFUNC_UNUSED
|
||||||
|
#else
|
||||||
|
#define WDL_DENORMAL_INLINE
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static WDL_DENORMAL_INLINE unsigned int WDL_DENORMAL_FLOAT_W(const float *a) { unsigned int v; memcpy(&v,a,sizeof(v)); return v; }
|
||||||
|
static WDL_DENORMAL_INLINE unsigned int WDL_DENORMAL_DOUBLE_HW(const double *a) { WDL_UINT64 v; memcpy(&v,(char*)a,sizeof(v)); return (unsigned int) (v>>32); }
|
||||||
|
|
||||||
|
#define WDL_DENORMAL_DOUBLE_AGGRESSIVE_CUTOFF 0x3cA00000 // 0x3B8000000 maybe instead? that's 10^-5 smaller or so
|
||||||
|
#define WDL_DENORMAL_FLOAT_AGGRESSIVE_CUTOFF 0x25000000
|
||||||
|
|
||||||
|
|
||||||
|
// define WDL_DENORMAL_WANTS_SCOPED_FTZ, and then use a WDL_denormal_ftz_scope in addition to denormal_*(), then
|
||||||
|
// if FTZ is available it will be used instead...
|
||||||
|
//
|
||||||
|
#ifdef WDL_DENORMAL_WANTS_SCOPED_FTZ
|
||||||
|
|
||||||
|
#if defined(__SSE2__) || _M_IX86_FP >= 2 || defined(_M_X64)
|
||||||
|
#define WDL_DENORMAL_FTZMODE
|
||||||
|
#define WDL_DENORMAL_FTZSTATE_TYPE unsigned int
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#include <intrin.h>
|
||||||
|
#else
|
||||||
|
#include <xmmintrin.h>
|
||||||
|
#endif
|
||||||
|
#define wdl_denorm_mm_getcsr() _mm_getcsr()
|
||||||
|
#define wdl_denorm_mm_setcsr(x) _mm_setcsr(x)
|
||||||
|
#if defined(__SSE3__)
|
||||||
|
#define wdl_denorm_mm_csr_mask ((1<<15)|(1<<11) | (1<<8) | (1<<6)) // FTZ, underflow, denormal mask, DAZ
|
||||||
|
#else
|
||||||
|
#define wdl_denorm_mm_csr_mask ((1<<15)|(1<<11)) // FTZ and underflow only (target SSE2)
|
||||||
|
#endif
|
||||||
|
#elif defined(__arm__) || defined(__aarch64__)
|
||||||
|
#define WDL_DENORMAL_FTZMODE
|
||||||
|
#define WDL_DENORMAL_FTZSTATE_TYPE unsigned long
|
||||||
|
static unsigned long __attribute__((unused)) wdl_denorm_mm_getcsr()
|
||||||
|
{
|
||||||
|
unsigned long rv;
|
||||||
|
#ifdef __aarch64__
|
||||||
|
asm volatile ( "mrs %0, fpcr" : "=r" (rv));
|
||||||
|
#else
|
||||||
|
asm volatile ( "fmrx %0, fpscr" : "=r" (rv));
|
||||||
|
#endif
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
static void __attribute__((unused)) wdl_denorm_mm_setcsr(unsigned long v)
|
||||||
|
{
|
||||||
|
#ifdef __aarch64__
|
||||||
|
asm volatile ( "msr fpcr, %0" :: "r"(v));
|
||||||
|
#else
|
||||||
|
asm volatile ( "fmxr fpscr, %0" :: "r"(v));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
#define wdl_denorm_mm_csr_mask (1<<24)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
class WDL_denormal_ftz_scope
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
WDL_denormal_ftz_scope()
|
||||||
|
{
|
||||||
|
#ifdef WDL_DENORMAL_FTZMODE
|
||||||
|
const WDL_DENORMAL_FTZSTATE_TYPE b = wdl_denorm_mm_csr_mask;
|
||||||
|
old_state = wdl_denorm_mm_getcsr();
|
||||||
|
if ((need_restore = (old_state & b) != b))
|
||||||
|
wdl_denorm_mm_setcsr(old_state|b);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
~WDL_denormal_ftz_scope()
|
||||||
|
{
|
||||||
|
#ifdef WDL_DENORMAL_FTZMODE
|
||||||
|
if (need_restore) wdl_denorm_mm_setcsr(old_state);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef WDL_DENORMAL_FTZMODE
|
||||||
|
WDL_DENORMAL_FTZSTATE_TYPE old_state;
|
||||||
|
bool need_restore;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#if !defined(WDL_DENORMAL_FTZMODE) && !defined(WDL_DENORMAL_DO_NOT_FILTER)
|
||||||
|
|
||||||
|
static double WDL_DENORMAL_INLINE denormal_filter_double(double a)
|
||||||
|
{
|
||||||
|
return (WDL_DENORMAL_DOUBLE_HW(&a)&0x7ff00000) ? a : 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static double WDL_DENORMAL_INLINE denormal_filter_double2(double a)
|
||||||
|
{
|
||||||
|
return ((WDL_DENORMAL_DOUBLE_HW(&a)+0x100000)&0x7ff00000) > 0x100000 ? a : 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static double WDL_DENORMAL_INLINE denormal_filter_double_aggressive(double a)
|
||||||
|
{
|
||||||
|
return ((WDL_DENORMAL_DOUBLE_HW(&a)+0x100000)&0x7ff00000) >= WDL_DENORMAL_DOUBLE_AGGRESSIVE_CUTOFF ? a : 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static float WDL_DENORMAL_INLINE denormal_filter_float(float a)
|
||||||
|
{
|
||||||
|
return (WDL_DENORMAL_FLOAT_W(&a)&0x7f800000) ? a : 0.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
static float WDL_DENORMAL_INLINE denormal_filter_float2(float a)
|
||||||
|
{
|
||||||
|
return ((WDL_DENORMAL_FLOAT_W(&a)+0x800000)&0x7f800000) > 0x800000 ? a : 0.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static float WDL_DENORMAL_INLINE denormal_filter_float_aggressive(float a)
|
||||||
|
{
|
||||||
|
return ((WDL_DENORMAL_FLOAT_W(&a)+0x800000)&0x7f800000) >= WDL_DENORMAL_FLOAT_AGGRESSIVE_CUTOFF ? a : 0.0f;
|
||||||
|
}
|
||||||
|
static void WDL_DENORMAL_INLINE denormal_fix_double(double *a)
|
||||||
|
{
|
||||||
|
if (!(WDL_DENORMAL_DOUBLE_HW(a)&0x7ff00000)) *a=0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void WDL_DENORMAL_INLINE denormal_fix_double_aggressive(double *a)
|
||||||
|
{
|
||||||
|
if (((WDL_DENORMAL_DOUBLE_HW(a)+0x100000)&0x7ff00000) < WDL_DENORMAL_DOUBLE_AGGRESSIVE_CUTOFF) *a=0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void WDL_DENORMAL_INLINE denormal_fix_float(float *a)
|
||||||
|
{
|
||||||
|
if (!(WDL_DENORMAL_FLOAT_W(a)&0x7f800000)) *a=0.0f;
|
||||||
|
}
|
||||||
|
static void WDL_DENORMAL_INLINE denormal_fix_float_aggressive(float *a)
|
||||||
|
{
|
||||||
|
if (((WDL_DENORMAL_FLOAT_W(a)+0x800000)&0x7f800000) < WDL_DENORMAL_FLOAT_AGGRESSIVE_CUTOFF) *a=0.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus // automatic typed versions (though one should probably use the explicit versions...
|
||||||
|
|
||||||
|
|
||||||
|
static double WDL_DENORMAL_INLINE denormal_filter(double a)
|
||||||
|
{
|
||||||
|
return (WDL_DENORMAL_DOUBLE_HW(&a)&0x7ff00000) ? a : 0.0;
|
||||||
|
}
|
||||||
|
static double WDL_DENORMAL_INLINE denormal_filter_aggressive(double a)
|
||||||
|
{
|
||||||
|
return ((WDL_DENORMAL_DOUBLE_HW(&a)+0x100000)&0x7ff00000) >= WDL_DENORMAL_DOUBLE_AGGRESSIVE_CUTOFF ? a : 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static float WDL_DENORMAL_INLINE denormal_filter(float a)
|
||||||
|
{
|
||||||
|
return (WDL_DENORMAL_FLOAT_W(&a)&0x7f800000) ? a : 0.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
static float WDL_DENORMAL_INLINE denormal_filter_aggressive(float a)
|
||||||
|
{
|
||||||
|
return ((WDL_DENORMAL_FLOAT_W(&a)+0x800000)&0x7f800000) >= WDL_DENORMAL_FLOAT_AGGRESSIVE_CUTOFF ? a : 0.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void WDL_DENORMAL_INLINE denormal_fix(double *a)
|
||||||
|
{
|
||||||
|
if (!(WDL_DENORMAL_DOUBLE_HW(a)&0x7ff00000)) *a=0.0;
|
||||||
|
}
|
||||||
|
static void WDL_DENORMAL_INLINE denormal_fix_aggressive(double *a)
|
||||||
|
{
|
||||||
|
if (((WDL_DENORMAL_DOUBLE_HW(a)+0x100000)&0x7ff00000) < WDL_DENORMAL_DOUBLE_AGGRESSIVE_CUTOFF) *a=0.0;
|
||||||
|
}
|
||||||
|
static void WDL_DENORMAL_INLINE denormal_fix(float *a)
|
||||||
|
{
|
||||||
|
if (!(WDL_DENORMAL_FLOAT_W(a)&0x7f800000)) *a=0.0f;
|
||||||
|
}
|
||||||
|
static void WDL_DENORMAL_INLINE denormal_fix_aggressive(float *a)
|
||||||
|
{
|
||||||
|
if (((WDL_DENORMAL_FLOAT_W(a)+0x800000)&0x7f800000) < WDL_DENORMAL_FLOAT_AGGRESSIVE_CUTOFF) *a=0.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif // cplusplus versions
|
||||||
|
|
||||||
|
#else // end of !WDL_DENORMAL_DO_NOT_FILTER (and other platform-specific checks)
|
||||||
|
|
||||||
|
#define denormal_filter(x) (x)
|
||||||
|
#define denormal_filter2(x) (x)
|
||||||
|
#define denormal_filter_double(x) (x)
|
||||||
|
#define denormal_filter_double2(x) (x)
|
||||||
|
#define denormal_filter_double_aggressive(x) (x)
|
||||||
|
#define denormal_filter_float(x) (x)
|
||||||
|
#define denormal_filter_float2(x) (x)
|
||||||
|
#define denormal_filter_float_aggressive(x) (x)
|
||||||
|
#define denormal_filter_aggressive(x) (x)
|
||||||
|
#define denormal_fix(x) do { } while(0)
|
||||||
|
#define denormal_fix_aggressive(x) do { } while(0)
|
||||||
|
#define denormal_fix_double(x) do { } while(0)
|
||||||
|
#define denormal_fix_double_aggressive(x) do { } while(0)
|
||||||
|
#define denormal_fix_float(x) do { } while(0)
|
||||||
|
#define denormal_fix_float_aggressive(x) do { } while(0)
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////
|
||||||
|
// this isnt a denormal function but it is similar, so we'll put it here as a bonus
|
||||||
|
|
||||||
|
static void WDL_DENORMAL_INLINE GetDoubleMaxAbsValue(double *out, const double *in) // note: the value pointed to by "out" must be >=0.0, __NOT__ <= -0.0
|
||||||
|
{
|
||||||
|
WDL_UINT64 i, o;
|
||||||
|
memcpy(&i,in,sizeof(i));
|
||||||
|
memcpy(&o,out,sizeof(o));
|
||||||
|
i &= WDL_UINT64_CONST(0x7fffffffffffffff);
|
||||||
|
if (i > o) memcpy(out,&i,sizeof(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void WDL_DENORMAL_INLINE GetFloatMaxAbsValue(float *out, const float *in) // note: the value pointed to by "out" must be >=0.0, __NOT__ <= -0.0
|
||||||
|
{
|
||||||
|
unsigned int i, o;
|
||||||
|
memcpy(&i, in, sizeof(i));
|
||||||
|
memcpy(&o, out, sizeof(o));
|
||||||
|
i &= 0x7fffffff;
|
||||||
|
if (i > o) memcpy(out, &i, sizeof(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
static void WDL_DENORMAL_INLINE GetFloatMaxAbsValue(double *out, const double *in) // note: the value pointed to by "out" must be >=0.0, __NOT__ <= -0.0
|
||||||
|
{
|
||||||
|
GetDoubleMaxAbsValue(out,in);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
65
oversampling/WDL/destroycheck.h
Normal file
65
oversampling/WDL/destroycheck.h
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
#ifndef _WDL_DESTROYCHECK_H_
|
||||||
|
#define _WDL_DESTROYCHECK_H_
|
||||||
|
|
||||||
|
// this is a useful class for verifying that an object (usually "this") hasn't been destroyed:
|
||||||
|
// to use it you add a WDL_DestroyState as a member of your class, then use the WDL_DestroyCheck
|
||||||
|
// helper class (creating it when the pointer is known valid, and checking it later to see if it
|
||||||
|
// is still valid).
|
||||||
|
//
|
||||||
|
// example:
|
||||||
|
// class myClass {
|
||||||
|
// WDL_DestroyState dest;
|
||||||
|
// ...
|
||||||
|
// };
|
||||||
|
//
|
||||||
|
// calling code (on myClass *classInstnace):
|
||||||
|
// WDL_DestroyCheck chk(&classInstance->dest);
|
||||||
|
// somefunction();
|
||||||
|
// if (!chk.isOK()) printf("classInstance got deleted!\n");
|
||||||
|
//
|
||||||
|
// NOTE: only use this when these objects will be accessed from the same thread -- it will fail miserably
|
||||||
|
// in a multithreaded environment
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class WDL_DestroyCheck
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
class WDL_DestroyStateNextRec { public: WDL_DestroyCheck *next; };
|
||||||
|
|
||||||
|
WDL_DestroyStateNextRec n, *prev;
|
||||||
|
WDL_DestroyCheck(WDL_DestroyStateNextRec *state)
|
||||||
|
{
|
||||||
|
n.next=NULL;
|
||||||
|
if ((prev=state))
|
||||||
|
{
|
||||||
|
if ((n.next=prev->next)) n.next->prev = &n;
|
||||||
|
prev->next=this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
~WDL_DestroyCheck()
|
||||||
|
{
|
||||||
|
if (prev)
|
||||||
|
{
|
||||||
|
prev->next = n.next;
|
||||||
|
if (n.next) n.next->prev = prev;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isOK() { return !!prev; }
|
||||||
|
};
|
||||||
|
|
||||||
|
class WDL_DestroyState : public WDL_DestroyCheck::WDL_DestroyStateNextRec
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
WDL_DestroyState() { next=NULL; }
|
||||||
|
|
||||||
|
~WDL_DestroyState()
|
||||||
|
{
|
||||||
|
WDL_DestroyCheck *p = next;
|
||||||
|
while (p) { WDL_DestroyCheck *np = p->n.next; p->prev=NULL; p->n.next=NULL; p=np; }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
163
oversampling/WDL/diffcalc.h
Normal file
163
oversampling/WDL/diffcalc.h
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
#ifndef _WDL_DIFFCALC_H_
|
||||||
|
#define _WDL_DIFFCALC_H_
|
||||||
|
|
||||||
|
#include "assocarray.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Based on "An O(ND) Difference Algorithm and Its Variations", Myers
|
||||||
|
// http://xmailserver.org/diff2.pdf
|
||||||
|
|
||||||
|
|
||||||
|
template <class T> class WDL_DiffCalc
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
WDL_DiffCalc() {}
|
||||||
|
virtual ~WDL_DiffCalc() {}
|
||||||
|
|
||||||
|
// cmp() returns 0 if the elements are equal.
|
||||||
|
// returns the length of the merged list and populates m_rx, m_ry.
|
||||||
|
int Diff(const T* x, const T* y, int nx, int ny, int (*cmp)(const T*, const T*))
|
||||||
|
{
|
||||||
|
m_rx.Resize(0, false);
|
||||||
|
m_ry.Resize(0, false);
|
||||||
|
ClearV();
|
||||||
|
|
||||||
|
if (!nx && !ny) return 0;
|
||||||
|
if (!nx || !ny)
|
||||||
|
{
|
||||||
|
int i, n=max(nx, ny);
|
||||||
|
for (i=0; i < n; ++i)
|
||||||
|
{
|
||||||
|
m_rx.Add(nx ? i : -1);
|
||||||
|
m_ry.Add(ny ? i : -1);
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!cmp(x, y)) // special case
|
||||||
|
{
|
||||||
|
int i, n;
|
||||||
|
for (n=1; n < min(nx, ny); ++n)
|
||||||
|
{
|
||||||
|
if (cmp(x+n, y+n)) break;
|
||||||
|
}
|
||||||
|
int len=Diff(x+n, y+n, nx-n, ny-n, cmp);
|
||||||
|
int *rx=m_rx.Get(), *ry=m_ry.Get();
|
||||||
|
for (i=0; i < len; ++i)
|
||||||
|
{
|
||||||
|
if (rx[i] >= 0) rx[i] += n;
|
||||||
|
if (ry[i] >= 0) ry[i] += n;
|
||||||
|
}
|
||||||
|
len += n;
|
||||||
|
while (n--)
|
||||||
|
{
|
||||||
|
m_rx.Insert(n, 0);
|
||||||
|
m_ry.Insert(n, 0);
|
||||||
|
}
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
SetV(0, 1, 0);
|
||||||
|
int d, k, xi, yi;
|
||||||
|
for (d=0; d <= nx+ny; ++d)
|
||||||
|
{
|
||||||
|
for (k=-d; k <= d ; k += 2)
|
||||||
|
{
|
||||||
|
if (k == -d || (k != d && GetV(d, k-1) < GetV(d, k+1)))
|
||||||
|
{
|
||||||
|
xi=GetV(d, k+1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
xi=GetV(d, k-1)+1;
|
||||||
|
}
|
||||||
|
yi=xi-k;
|
||||||
|
while (xi < nx && yi < ny && !cmp(x+xi, y+yi))
|
||||||
|
{
|
||||||
|
++xi;
|
||||||
|
++yi;
|
||||||
|
}
|
||||||
|
SetV(d+1, k, xi);
|
||||||
|
if (xi >= nx && yi >= ny) break;
|
||||||
|
}
|
||||||
|
if (xi >= nx && yi >= ny) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
int len=(nx+ny+d)/2;
|
||||||
|
int *rx=m_rx.Resize(len);
|
||||||
|
int *ry=m_ry.Resize(len);
|
||||||
|
int pos=len;
|
||||||
|
|
||||||
|
while (d)
|
||||||
|
{
|
||||||
|
while (xi > 0 && yi > 0 && !cmp(x+xi-1, y+yi-1))
|
||||||
|
{
|
||||||
|
--pos;
|
||||||
|
rx[pos]=--xi;
|
||||||
|
ry[pos]=--yi;
|
||||||
|
}
|
||||||
|
--pos;
|
||||||
|
if (k == -d || (k != d && GetV(d, k-1) < GetV(d, k+1)))
|
||||||
|
{
|
||||||
|
++k;
|
||||||
|
rx[pos]=-1;
|
||||||
|
ry[pos]=--yi;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
--k;
|
||||||
|
rx[pos]=--xi;
|
||||||
|
ry[pos]=-1;
|
||||||
|
}
|
||||||
|
--d;
|
||||||
|
}
|
||||||
|
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
// m_rx, m_ry hold the index of each merged list element in x and y,
|
||||||
|
// or -1 if the merged list element is not an element in that source list.
|
||||||
|
// example: X="ABCABBA", Y="CBABAC"
|
||||||
|
// 0123456 012345
|
||||||
|
// WDL_Merge() returns "ABCBABBAC"
|
||||||
|
// 012 3456
|
||||||
|
// 0123 45
|
||||||
|
// m_rx={ 0, 1, 2, -1, 3, 4, 5, 6, -1}
|
||||||
|
// m_ry={-1, -1, 0, 1, 2, 3, -1, 4, 5}
|
||||||
|
WDL_TypedBuf<int> m_rx, m_ry;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
WDL_IntKeyedArray<int> m_v; // x coord of d-contour on row k
|
||||||
|
void ClearV()
|
||||||
|
{
|
||||||
|
m_v.DeleteAll();
|
||||||
|
}
|
||||||
|
void SetV(int d, int k, int xi)
|
||||||
|
{
|
||||||
|
m_v.Insert(_key(d, k), xi);
|
||||||
|
}
|
||||||
|
int GetV(int d, int k)
|
||||||
|
{
|
||||||
|
return m_v.Get(_key(d, k));
|
||||||
|
}
|
||||||
|
static int _key(int d, int k) { return (d<<16)|(k+(1<<15)); }
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// this is a separate function from WDL_DiffCalc only because it requires T::operator=
|
||||||
|
template <class T> int WDL_Merge(const T* x, const T* y, int nx, int ny,
|
||||||
|
int (*cmp)(const T*, const T*), T* list)
|
||||||
|
{
|
||||||
|
WDL_DiffCalc<T> dc;
|
||||||
|
int i, n=dc.Diff(x, y, nx, ny, cmp);
|
||||||
|
int *rx=dc.m_rx.Get(), *ry=dc.m_ry.Get();
|
||||||
|
for (i=0; i < n; ++i)
|
||||||
|
{
|
||||||
|
if (list) list[i]=(rx[i] >= 0 ? x[rx[i]] : y[ry[i]]);
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
329
oversampling/WDL/dirscan.h
Normal file
329
oversampling/WDL/dirscan.h
Normal file
@@ -0,0 +1,329 @@
|
|||||||
|
/*
|
||||||
|
WDL - dirscan.h
|
||||||
|
Copyright (C) 2005 and later Cockos Incorporated
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied
|
||||||
|
warranty. In no event will the authors be held liable for any damages
|
||||||
|
arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
|
including commercial applications, and to alter it and redistribute it
|
||||||
|
freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not
|
||||||
|
claim that you wrote the original software. If you use this software
|
||||||
|
in a product, an acknowledgment in the product documentation would be
|
||||||
|
appreciated but is not required.
|
||||||
|
2. Altered source versions must be plainly marked as such, and must not be
|
||||||
|
misrepresented as being the original software.
|
||||||
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
This file provides the interface and implementation for WDL_DirScan, a simple
|
||||||
|
(and somewhat portable) directory reading class. On non-Win32 systems it wraps
|
||||||
|
opendir()/readdir()/etc. On Win32, it uses FindFirst*, and supports wildcards as
|
||||||
|
well.
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef _WDL_DIRSCAN_H_
|
||||||
|
#define _WDL_DIRSCAN_H_
|
||||||
|
|
||||||
|
#include "wdlstring.h"
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <dirent.h>
|
||||||
|
extern struct stat wdl_stat_chk;
|
||||||
|
// if this fails on linux, use CFLAGS += -D_FILE_OFFSET_BITS=64
|
||||||
|
typedef char wdl_dirscan_assert_failed_stat_not_64[sizeof(wdl_stat_chk.st_size)!=8 ? -1 : 1];
|
||||||
|
#endif
|
||||||
|
|
||||||
|
class WDL_DirScan
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
WDL_DirScan() :
|
||||||
|
#ifdef _WIN32
|
||||||
|
m_h(INVALID_HANDLE_VALUE)
|
||||||
|
#ifndef WDL_NO_SUPPORT_UTF8
|
||||||
|
, m_wcmode(false)
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
m_h(NULL), m_ent(NULL)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
~WDL_DirScan()
|
||||||
|
{
|
||||||
|
Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
int First(const char *dirname
|
||||||
|
#ifdef _WIN32
|
||||||
|
, int isExactSpec=0
|
||||||
|
#endif
|
||||||
|
) // returns 0 if success
|
||||||
|
{
|
||||||
|
WDL_FastString scanstr(dirname);
|
||||||
|
const int l = scanstr.GetLength();
|
||||||
|
if (l < 1) return -1;
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
if (!isExactSpec)
|
||||||
|
{
|
||||||
|
if (dirname[l-1] == '\\' || dirname[l-1] == '/') scanstr.SetLen(l-1);
|
||||||
|
m_leading_path = scanstr;
|
||||||
|
scanstr.Append("\\*");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_leading_path = scanstr;
|
||||||
|
|
||||||
|
// remove trailing wildcards and directory separator from m_leading_path
|
||||||
|
const char *sp = m_leading_path.Get();
|
||||||
|
int idx = m_leading_path.GetLength() - 1;
|
||||||
|
while (idx > 0 && sp[idx] != '/' && sp[idx] != '\\') idx--;
|
||||||
|
if (idx > 0) m_leading_path.SetLen(idx);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
if (dirname[l-1] == '\\' || dirname[l-1] == '/') scanstr.SetLen(l-1);
|
||||||
|
m_leading_path = scanstr;
|
||||||
|
if (!scanstr.GetLength()) scanstr.Set("/"); // fix for scanning /
|
||||||
|
#endif
|
||||||
|
|
||||||
|
Close();
|
||||||
|
#ifdef _WIN32
|
||||||
|
#ifndef WDL_NO_SUPPORT_UTF8
|
||||||
|
m_h=INVALID_HANDLE_VALUE;
|
||||||
|
#ifdef WDL_SUPPORT_WIN9X
|
||||||
|
m_wcmode = GetVersion()< 0x80000000;
|
||||||
|
#else
|
||||||
|
m_wcmode = true;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (m_wcmode)
|
||||||
|
{
|
||||||
|
int reqbuf = MultiByteToWideChar(CP_UTF8,MB_ERR_INVALID_CHARS,scanstr.Get(),-1,NULL,0);
|
||||||
|
if (reqbuf > 1000)
|
||||||
|
{
|
||||||
|
WDL_TypedBuf<WCHAR> tmp;
|
||||||
|
tmp.Resize(reqbuf+20);
|
||||||
|
if (MultiByteToWideChar(CP_UTF8,MB_ERR_INVALID_CHARS,scanstr.Get(),-1,tmp.Get(),tmp.GetSize()-10))
|
||||||
|
{
|
||||||
|
correctlongpath(tmp.Get());
|
||||||
|
m_h=FindFirstFileW(tmp.Get(),&m_fd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
WCHAR wfilename[1024];
|
||||||
|
if (MultiByteToWideChar(CP_UTF8,MB_ERR_INVALID_CHARS,scanstr.Get(),-1,wfilename,1024-10))
|
||||||
|
{
|
||||||
|
correctlongpath(wfilename);
|
||||||
|
m_h=FindFirstFileW(wfilename,&m_fd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_h==INVALID_HANDLE_VALUE) m_wcmode=false;
|
||||||
|
|
||||||
|
if (m_h==INVALID_HANDLE_VALUE)
|
||||||
|
#endif
|
||||||
|
m_h=FindFirstFileA(scanstr.Get(),(WIN32_FIND_DATAA*)&m_fd);
|
||||||
|
return (m_h == INVALID_HANDLE_VALUE);
|
||||||
|
#else
|
||||||
|
m_ent=0;
|
||||||
|
m_h=opendir(scanstr.Get());
|
||||||
|
return !m_h || Next();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
int Next() // returns 0 on success
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
if (m_h == INVALID_HANDLE_VALUE) return -1;
|
||||||
|
#ifndef WDL_NO_SUPPORT_UTF8
|
||||||
|
if (m_wcmode) return !FindNextFileW(m_h,&m_fd);
|
||||||
|
#endif
|
||||||
|
return !FindNextFileA(m_h,(WIN32_FIND_DATAA*)&m_fd);
|
||||||
|
#else
|
||||||
|
if (!m_h) return -1;
|
||||||
|
return !(m_ent=readdir(m_h));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
void Close()
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
if (m_h != INVALID_HANDLE_VALUE) FindClose(m_h);
|
||||||
|
m_h=INVALID_HANDLE_VALUE;
|
||||||
|
#else
|
||||||
|
if (m_h) closedir(m_h);
|
||||||
|
m_h=0; m_ent=0;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
const char *GetCurrentFN()
|
||||||
|
{
|
||||||
|
#ifndef WDL_NO_SUPPORT_UTF8
|
||||||
|
if (m_wcmode)
|
||||||
|
{
|
||||||
|
if (!WideCharToMultiByte(CP_UTF8,0,m_fd.cFileName,-1,m_tmpbuf,sizeof(m_tmpbuf),NULL,NULL))
|
||||||
|
m_tmpbuf[0]=0;
|
||||||
|
return m_tmpbuf;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return ((WIN32_FIND_DATAA *)&m_fd)->cFileName;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
const char *GetCurrentFN() const { return m_ent?m_ent->d_name : ""; }
|
||||||
|
#endif
|
||||||
|
template<class T> void GetCurrentFullFN(T *str)
|
||||||
|
{
|
||||||
|
str->Set(m_leading_path.Get());
|
||||||
|
#ifdef _WIN32
|
||||||
|
str->Append("\\");
|
||||||
|
#else
|
||||||
|
str->Append("/");
|
||||||
|
#endif
|
||||||
|
str->Append(GetCurrentFN());
|
||||||
|
}
|
||||||
|
int GetCurrentIsDirectory() const // returns 1 if dir, 2 if symlink to dir, 4 if possibly-recursive symlink to dir
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
return !!(m_fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
|
||||||
|
#else
|
||||||
|
char tmp[2048];
|
||||||
|
if (m_ent) switch (m_ent->d_type)
|
||||||
|
{
|
||||||
|
case DT_DIR: return 1;
|
||||||
|
case DT_LNK:
|
||||||
|
{
|
||||||
|
snprintf(tmp,sizeof(tmp),"%s/%s",m_leading_path.Get(),m_ent->d_name);
|
||||||
|
char *rp = realpath(tmp,NULL);
|
||||||
|
if (!rp) return 0;
|
||||||
|
|
||||||
|
struct stat sb;
|
||||||
|
int ret = (!stat(rp,&sb) && (sb.st_mode & S_IFMT) == S_IFDIR) ? 2 : 0;
|
||||||
|
if (ret)
|
||||||
|
{
|
||||||
|
// treat symlinks of /path/to/foo -> /path from being resolved (avoiding obvious feedback loops)
|
||||||
|
const int rpl = (int) strlen(rp);
|
||||||
|
if (
|
||||||
|
#ifdef __APPLE__
|
||||||
|
!strnicmp(rp,m_leading_path.Get(),rpl)
|
||||||
|
#else
|
||||||
|
!strncmp(rp,m_leading_path.Get(),rpl)
|
||||||
|
#endif
|
||||||
|
&& (m_leading_path.Get()[rpl] == '/' || m_leading_path.Get()[rpl] == 0)
|
||||||
|
) ret = 4;
|
||||||
|
}
|
||||||
|
free(rp);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
case DT_UNKNOWN:
|
||||||
|
{
|
||||||
|
snprintf(tmp,sizeof(tmp),"%s/%s",m_leading_path.Get(),m_ent->d_name);
|
||||||
|
DIR *d = opendir(tmp);
|
||||||
|
if (d) { closedir(d); return 1; }
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
// these are somewhat windows specific calls, eh
|
||||||
|
#ifdef _WIN32
|
||||||
|
DWORD GetCurrentFileSize(DWORD *HighWord=NULL) const { if (HighWord) *HighWord = m_fd.nFileSizeHigh; return m_fd.nFileSizeLow; }
|
||||||
|
void GetCurrentLastWriteTime(FILETIME *ft) const { *ft = m_fd.ftLastWriteTime; }
|
||||||
|
void GetCurrentLastAccessTime(FILETIME *ft) const { *ft = m_fd.ftLastAccessTime; }
|
||||||
|
void GetCurrentCreationTime(FILETIME *ft) const { *ft = m_fd.ftCreationTime; }
|
||||||
|
DWORD GetFileAttributes() const { return m_fd.dwFileAttributes; }
|
||||||
|
#elif defined(_WDL_SWELL_H_)
|
||||||
|
|
||||||
|
void GetCurrentCreationTime(FILETIME *ft)
|
||||||
|
{
|
||||||
|
char tmp[2048];
|
||||||
|
snprintf(tmp,sizeof(tmp),"%s/%s",m_leading_path.Get(),GetCurrentFN());
|
||||||
|
struct stat st={0,};
|
||||||
|
stat(tmp,&st);
|
||||||
|
unsigned long long a=(unsigned long long)st.st_ctime; // seconds since january 1st, 1970
|
||||||
|
a+=11644473600ull; // 1601->1970
|
||||||
|
a*=10000000; // seconds to 1/10th microseconds (100 nanoseconds)
|
||||||
|
ft->dwLowDateTime=a & 0xffffffff;
|
||||||
|
ft->dwHighDateTime=a>>32;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GetCurrentLastWriteTime(FILETIME *ft)
|
||||||
|
{
|
||||||
|
char tmp[2048];
|
||||||
|
snprintf(tmp,sizeof(tmp),"%s/%s",m_leading_path.Get(),GetCurrentFN());
|
||||||
|
struct stat st={0,};
|
||||||
|
stat(tmp,&st);
|
||||||
|
unsigned long long a=(unsigned long long)st.st_mtime; // seconds since january 1st, 1970
|
||||||
|
a+=11644473600ull; // 1601->1970
|
||||||
|
a*=10000000; // seconds to 1/10th microseconds (100 nanoseconds)
|
||||||
|
ft->dwLowDateTime=a & 0xffffffff;
|
||||||
|
ft->dwHighDateTime=a>>32;
|
||||||
|
}
|
||||||
|
DWORD GetCurrentFileSize(DWORD *HighWord=NULL)
|
||||||
|
{
|
||||||
|
char tmp[2048];
|
||||||
|
snprintf(tmp,sizeof(tmp),"%s/%s",m_leading_path.Get(),GetCurrentFN());
|
||||||
|
struct stat st={0,};
|
||||||
|
stat(tmp,&st);
|
||||||
|
|
||||||
|
if (HighWord) *HighWord = (DWORD)(st.st_size>>32);
|
||||||
|
return (DWORD)(st.st_size&0xffffffff);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
private:
|
||||||
|
#ifdef _WIN32
|
||||||
|
|
||||||
|
#ifndef WDL_NO_SUPPORT_UTF8
|
||||||
|
bool m_wcmode;
|
||||||
|
WIN32_FIND_DATAW m_fd;
|
||||||
|
char m_tmpbuf[MAX_PATH*5]; // even if each byte gets encoded as 4 utf-8 bytes this should be plenty ;)
|
||||||
|
#else
|
||||||
|
WIN32_FIND_DATAA m_fd;
|
||||||
|
#endif
|
||||||
|
HANDLE m_h;
|
||||||
|
#else
|
||||||
|
DIR *m_h;
|
||||||
|
struct dirent *m_ent;
|
||||||
|
#endif
|
||||||
|
WDL_FastString m_leading_path;
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
static void correctlongpath(WCHAR *buf) // this also exists as wdl_utf8_correctlongpath
|
||||||
|
{
|
||||||
|
const WCHAR *insert;
|
||||||
|
WCHAR *wr;
|
||||||
|
int skip = 0;
|
||||||
|
if (!buf || !buf[0] || wcslen(buf) < 256) return;
|
||||||
|
if (buf[1] == ':') insert=L"\\\\?\\";
|
||||||
|
else if (buf[0] == '\\' && buf[1] == '\\') { insert = L"\\\\?\\UNC\\"; skip=2; }
|
||||||
|
else return;
|
||||||
|
|
||||||
|
wr = buf + wcslen(insert);
|
||||||
|
memmove(wr, buf + skip, (wcslen(buf+skip)+1)*2);
|
||||||
|
memmove(buf,insert,wcslen(insert)*2);
|
||||||
|
while (*wr)
|
||||||
|
{
|
||||||
|
if (*wr == '/') *wr = '\\';
|
||||||
|
wr++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
} WDL_FIXALIGN;
|
||||||
|
|
||||||
|
#endif
|
||||||
8
oversampling/WDL/eel2/.gitignore
vendored
Normal file
8
oversampling/WDL/eel2/.gitignore
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
/*.obj
|
||||||
|
/asm-nseel-x64-macho.asm
|
||||||
|
/asm-nseel-x64.asm
|
||||||
|
/loose_eel.exe
|
||||||
|
|
||||||
|
!/asm-nseel-x64-macho.o
|
||||||
|
!/asm-nseel-x64.obj
|
||||||
|
!/asm-nseel-arm64ec.obj
|
||||||
167
oversampling/WDL/eel2/Makefile
Normal file
167
oversampling/WDL/eel2/Makefile
Normal file
@@ -0,0 +1,167 @@
|
|||||||
|
CC=gcc
|
||||||
|
CFLAGS=-g -DWDL_FFT_REALSIZE=8 -Wall -Wno-unused-function -Wno-multichar -Wno-unused-result -Wshadow -Wtype-limits
|
||||||
|
LFLAGS=
|
||||||
|
CXX=g++
|
||||||
|
|
||||||
|
ifdef DEBUG
|
||||||
|
CFLAGS += -D_DEBUG -O0 -DWDL_CHECK_FOR_NON_UTF8_FOPEN
|
||||||
|
else
|
||||||
|
CFLAGS += -DNDEBUG -O
|
||||||
|
endif
|
||||||
|
|
||||||
|
CFLAGS += -D_FILE_OFFSET_BITS=64
|
||||||
|
|
||||||
|
OBJS=nseel-caltab.o nseel-compiler.o nseel-eval.o nseel-lextab.o nseel-ram.o nseel-yylex.o nseel-cfunc.o fft.o
|
||||||
|
|
||||||
|
SWELL_OBJS=
|
||||||
|
LICE_OBJS=
|
||||||
|
|
||||||
|
OBJS2=
|
||||||
|
|
||||||
|
UNAME_S := $(shell uname -s)
|
||||||
|
ARCH := $(shell uname -m)
|
||||||
|
|
||||||
|
ifeq ($(ARCH), aarch64)
|
||||||
|
ifeq ($(shell $(CC) -dumpmachine | cut -f 1 -d -), arm)
|
||||||
|
# helper for armv7l userspace on aarch64 cpu
|
||||||
|
ARCH := armv7l
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
|
ifeq ($(UNAME_S),Darwin)
|
||||||
|
CC=clang
|
||||||
|
CXX=clang++
|
||||||
|
CFLAGS += -arch $(ARCH)
|
||||||
|
endif
|
||||||
|
|
||||||
|
ifeq ($(ARCH),arm64)
|
||||||
|
CFLAGS += -fsigned-char
|
||||||
|
else
|
||||||
|
ifneq ($(filter arm%,$(ARCH)),)
|
||||||
|
CFLAGS += -fsigned-char -mfpu=vfp -march=armv6t2 -marm
|
||||||
|
endif
|
||||||
|
ifeq ($(ARCH),aarch64)
|
||||||
|
CFLAGS += -fsigned-char
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
|
ifndef ALLOW_WARNINGS
|
||||||
|
ifneq ($(UNAME_S),Darwin)
|
||||||
|
CFLAGS += -Werror
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
ifndef DEPRECATED_WARNINGS
|
||||||
|
CFLAGS += -Wno-deprecated-declarations
|
||||||
|
endif
|
||||||
|
|
||||||
|
|
||||||
|
default: loose_eel eel_pp
|
||||||
|
|
||||||
|
nseel-compiler.o: glue*.h ns-eel*.h
|
||||||
|
nseel-cfunc.o: asm*.c ns-eel*.h
|
||||||
|
loose_eel.o: eel*.h ns-eel*.h
|
||||||
|
nseel-*.o: ns-eel*.h
|
||||||
|
|
||||||
|
vpath %.cpp ../lice ../swell
|
||||||
|
vpath %.mm ../swell
|
||||||
|
vpath %.c ../
|
||||||
|
|
||||||
|
ifdef MAXLOOP
|
||||||
|
CFLAGS += -DNSEEL_LOOPFUNC_SUPPORT_MAXLEN=$(MAXLOOP)
|
||||||
|
else
|
||||||
|
CFLAGS += -DNSEEL_LOOPFUNC_SUPPORT_MAXLEN=0
|
||||||
|
endif
|
||||||
|
|
||||||
|
ifdef DISASSEMBLE
|
||||||
|
CFLAGS += -DEELSCRIPT_DO_DISASSEMBLE
|
||||||
|
endif
|
||||||
|
|
||||||
|
ifndef NO_GFX
|
||||||
|
LICE_OBJS += lice.o lice_image.o lice_line.o lice_ico.o lice_bmp.o lice_textnew.o lice_text.o lice_arc.o
|
||||||
|
CFLAGS += -DEEL_LICE_WANT_STANDALONE
|
||||||
|
|
||||||
|
ifeq ($(UNAME_S),Darwin)
|
||||||
|
CLANG_VER := $(shell clang --version|head -n 1| sed 's/.*version \([0-9][0-9]*\).*/\1/' )
|
||||||
|
CLANG_GT_9 := $(shell [ $(CLANG_VER) -gt 9 ] && echo true )
|
||||||
|
ifeq ($(CLANG_GT_9),true)
|
||||||
|
CFLAGS += -mmacosx-version-min=10.7 -stdlib=libc++
|
||||||
|
else
|
||||||
|
CFLAGS += -mmacosx-version-min=10.5
|
||||||
|
endif
|
||||||
|
SWELL_OBJS += swell-wnd.o swell-gdi.o swell.o swell-misc.o swell-dlg.o swell-menu.o swell-kb.o
|
||||||
|
LFLAGS += -lobjc -framework Cocoa -framework Carbon
|
||||||
|
else
|
||||||
|
|
||||||
|
CFLAGS += -DSWELL_LICE_GDI -DSWELL_EXTRA_MINIMAL
|
||||||
|
ifdef GDK2
|
||||||
|
CFLAGS += -DSWELL_TARGET_GDK=2 $(shell pkg-config --cflags gdk-2.0)
|
||||||
|
LFLAGS += $(shell pkg-config --libs gdk-2.0) -lX11 -lXi
|
||||||
|
else
|
||||||
|
CFLAGS += -DSWELL_TARGET_GDK=3 $(shell pkg-config --cflags gdk-3.0)
|
||||||
|
LFLAGS += $(shell pkg-config --libs gdk-3.0) -lX11 -lXi
|
||||||
|
endif
|
||||||
|
ifndef NOFREETYPE
|
||||||
|
CFLAGS += -DSWELL_FREETYPE $(shell pkg-config --cflags freetype2)
|
||||||
|
LFLAGS += $(shell pkg-config --libs freetype2)
|
||||||
|
endif
|
||||||
|
|
||||||
|
SWELL_OBJS += swell-wnd-generic.o swell-gdi-lice.o swell.o swell-misc-generic.o \
|
||||||
|
swell-dlg-generic.o swell-menu-generic.o swell-kb-generic.o \
|
||||||
|
swell-gdi-generic.o swell-ini.o swell-generic-gdk.o
|
||||||
|
|
||||||
|
LFLAGS += -ldl -lGL
|
||||||
|
endif
|
||||||
|
|
||||||
|
endif
|
||||||
|
|
||||||
|
ifdef PORTABLE
|
||||||
|
CFLAGS += -DEEL_TARGET_PORTABLE
|
||||||
|
else
|
||||||
|
ifeq ($(UNAME_S),Darwin)
|
||||||
|
ifeq ($(ARCH),x86_64)
|
||||||
|
ASM_FMT = macho64
|
||||||
|
NASM_OPTS = --prefix _
|
||||||
|
OBJS2 += asm-nseel-x64-sse.o
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
ifeq ($(UNAME_S),Linux)
|
||||||
|
ifeq ($(ARCH),x86_64)
|
||||||
|
ASM_FMT = elf64
|
||||||
|
NASM_OPTS =
|
||||||
|
OBJS2 += asm-nseel-x64-sse.o
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
|
asm-nseel-x64-sse.o: asm-nseel-x64-sse.asm
|
||||||
|
nasm -D AMD64ABI -f $(ASM_FMT) $(NASM_OPTS) asm-nseel-x64-sse.asm
|
||||||
|
|
||||||
|
endif
|
||||||
|
CXXFLAGS=$(CFLAGS)
|
||||||
|
|
||||||
|
ifeq ($(CXX),g++)
|
||||||
|
GCC_VER := $(shell $(CXX) --version|head -n 1| sed 's/.* \([0-9][0-9]*\)[.][0-9.]*/\1/' )
|
||||||
|
GCC_GT_10 := $(shell [ "$(GCC_VER)" -gt 10 ] && echo true )
|
||||||
|
ifeq ($(GCC_GT_10),true)
|
||||||
|
CXXFLAGS += -std=c++03
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
|
gen-yacc:
|
||||||
|
yacc -v -d eel2.y
|
||||||
|
|
||||||
|
gen-lex: # the output of this, lex.nseel.c, is unused because we have a handwritten parser instead
|
||||||
|
flex eel2.l
|
||||||
|
|
||||||
|
%.o : %.mm
|
||||||
|
$(CXX) $(CXXFLAGS) -c -o $@ $^
|
||||||
|
|
||||||
|
loose_eel: loose_eel.o $(OBJS) $(OBJS2) $(SWELL_OBJS) $(LICE_OBJS)
|
||||||
|
g++ -o $@ $^ $(CXXFLAGS) $(LFLAGS)
|
||||||
|
|
||||||
|
eel_pp: eel_pp.o $(OBJS) $(OBJS2)
|
||||||
|
g++ -o $@ $^ $(CXXFLAGS) $(LFLAGS)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
-rm -f -- loose_eel loose_eel.o eel_pp.o eel_pp $(OBJS) $(SWELL_OBJS) $(LICE_OBJS)
|
||||||
|
|
||||||
|
.PHONY: clean gen-lex gen-yacc
|
||||||
222
oversampling/WDL/eel2/a2i.php
Normal file
222
oversampling/WDL/eel2/a2i.php
Normal file
@@ -0,0 +1,222 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function process_file($infn, $outfn)
|
||||||
|
{
|
||||||
|
|
||||||
|
$in = fopen($infn,"r");
|
||||||
|
if (!$in) die("error opening input $infn\n");
|
||||||
|
$out = fopen($outfn,"w");
|
||||||
|
if (!$out) die("error opening output $outfn\n");
|
||||||
|
|
||||||
|
fputs($out,"// THIS FILE AUTOGENERATED FROM $infn by a2i.php\n\n");
|
||||||
|
|
||||||
|
$inblock=0;
|
||||||
|
$labelcnt=0;
|
||||||
|
|
||||||
|
while (($line = fgets($in)))
|
||||||
|
{
|
||||||
|
$line = rtrim($line);
|
||||||
|
if (trim($line) == "FUNCTION_MARKER")
|
||||||
|
{
|
||||||
|
fputs($out,"_emit 0x89;\n");
|
||||||
|
for ($tmp=0;$tmp<11;$tmp++) fputs($out,"_emit 0x90;\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$nowrite=0;
|
||||||
|
|
||||||
|
{
|
||||||
|
if (!$inblock)
|
||||||
|
{
|
||||||
|
if (strstr($line,"__asm__("))
|
||||||
|
{
|
||||||
|
$line=str_replace("__asm__(", "__asm {", $line);
|
||||||
|
$inblock=1;
|
||||||
|
if (isset($bthist)) unset($bthist);
|
||||||
|
if (isset($btfut)) unset($btfut);
|
||||||
|
$bthist = array();
|
||||||
|
$btfut = array();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($inblock)
|
||||||
|
{
|
||||||
|
if (substr(trim($line),-2) == ");")
|
||||||
|
{
|
||||||
|
$line = str_replace(");","}",$line);
|
||||||
|
$inblock=0;
|
||||||
|
}
|
||||||
|
|
||||||
|
$sline = strstr($line, "\"");
|
||||||
|
$lastchunk = strrchr($line,"\"");
|
||||||
|
if ($sline && $lastchunk && strlen($sline) != strlen($lastchunk))
|
||||||
|
{
|
||||||
|
$beg_restore = substr($line,0,-strlen($sline));
|
||||||
|
|
||||||
|
if (strlen($lastchunk)>1)
|
||||||
|
$end_restore = substr($line,1-strlen($lastchunk));
|
||||||
|
else $end_restore="";
|
||||||
|
|
||||||
|
$sline = substr($sline,1,strlen($sline)-1-strlen($lastchunk));
|
||||||
|
|
||||||
|
// get rid of chars we can ignore
|
||||||
|
$sline=preg_replace("/%\d+/","__TEMP_REPLACE__", $sline);
|
||||||
|
|
||||||
|
$sline=str_replace("\\n","", $sline);
|
||||||
|
$sline=str_replace("\"","", $sline);
|
||||||
|
$sline=str_replace("$","", $sline);
|
||||||
|
$sline=str_replace("%","", $sline);
|
||||||
|
|
||||||
|
|
||||||
|
// get rid of excess whitespace, especially around commas
|
||||||
|
$sline=str_replace(" "," ", $sline);
|
||||||
|
$sline=str_replace(" "," ", $sline);
|
||||||
|
$sline=str_replace(" "," ", $sline);
|
||||||
|
$sline=str_replace(", ",",", $sline);
|
||||||
|
$sline=str_replace(" ,",",", $sline);
|
||||||
|
|
||||||
|
$sline=preg_replace("/st\\(([0-9]+)\\)/","FPREG_$1",$sline);
|
||||||
|
|
||||||
|
|
||||||
|
if (preg_match("/^([0-9]+):/",trim($sline)))
|
||||||
|
{
|
||||||
|
$d = (int) $sline;
|
||||||
|
$a = strstr($sline,":");
|
||||||
|
if ($a) $sline = substr($a,1);
|
||||||
|
|
||||||
|
if (isset($btfut[$d]) && $btfut[$d] != "") $thislbl = $btfut[$d];
|
||||||
|
else $thislbl = "label_" . $labelcnt++;
|
||||||
|
|
||||||
|
$btfut[$d]="";
|
||||||
|
$bthist[$d] = $thislbl;
|
||||||
|
|
||||||
|
fputs($out,$thislbl . ":\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
$sploded = explode(" ",trim($sline));
|
||||||
|
if ($sline != "" && count($sploded)>0)
|
||||||
|
{
|
||||||
|
$inst = trim($sploded[0]);
|
||||||
|
$suffix = "";
|
||||||
|
|
||||||
|
$instline = strstr($sline,$inst);
|
||||||
|
$beg_restore .= substr($sline,0,-strlen($instline));
|
||||||
|
|
||||||
|
$parms = trim(substr($instline,strlen($inst)));
|
||||||
|
|
||||||
|
if ($inst=="j") $inst="jmp";
|
||||||
|
|
||||||
|
//if ($inst == "fdiv" && $parms == "") $inst="fdivr";
|
||||||
|
|
||||||
|
if ($inst != "call" && substr($inst,-2) == "ll") $suffix = "ll";
|
||||||
|
else if ($inst != "call" && $inst != "fmul" && substr($inst,-1) == "l") $suffix = "l";
|
||||||
|
else if (substr($inst,0,1)=="f" && $inst != "fcos" && $inst != "fsincos" && $inst != "fabs" && $inst != "fchs" && substr($inst,-1) == "s") $suffix = "s";
|
||||||
|
|
||||||
|
|
||||||
|
if ($suffix != "" && $inst != "jl") $inst = substr($inst,0,-strlen($suffix));
|
||||||
|
|
||||||
|
$parms = preg_replace("/\\((.{2,3}),(.{2,3})\\)/","($1+$2)",$parms);
|
||||||
|
|
||||||
|
$parms=preg_replace("/EEL_F_SUFFIX (-?[0-9]+)\\((.*)\\)/","qword ptr [$2+$1]",$parms);
|
||||||
|
$parms=preg_replace("/EEL_F_SUFFIX \\((.*)\\)/","qword ptr [$1]",$parms);
|
||||||
|
|
||||||
|
if ($inst == "sh" && $suffix == "ll") { $suffix="l"; $inst="shl"; }
|
||||||
|
|
||||||
|
if ($suffix == "ll" || ($suffix == "l" && substr($inst,0,1) == "f" && substr($inst,0,2) != "fi")) $suffixstr = "qword ptr ";
|
||||||
|
else if ($suffix == "l") $suffixstr = "dword ptr ";
|
||||||
|
else if ($suffix == "s") $suffixstr = "dword ptr ";
|
||||||
|
else $suffixstr = "";
|
||||||
|
$parms=preg_replace("/(-?[0-9]+)\\((.*)\\)/",$suffixstr . "[$2+$1]",$parms);
|
||||||
|
$parms=preg_replace("/\\((.*)\\)/",$suffixstr . "[$1]",$parms);
|
||||||
|
|
||||||
|
|
||||||
|
$parms=str_replace("EEL_F_SUFFIX","qword ptr", $parms);
|
||||||
|
|
||||||
|
$plist = explode(",",$parms);
|
||||||
|
if (count($plist) > 2) echo "Warning: too many parameters $parms!\n";
|
||||||
|
else if (count($plist)==2)
|
||||||
|
{
|
||||||
|
$parms = trim($plist[1]) . ", " . trim($plist[0]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($inst=="fsts") $inst="fstsw";
|
||||||
|
if ($inst=="call" && substr($parms,0,1) == "*") $parms=substr($parms,1);
|
||||||
|
if (substr($inst,0,1) == "j")
|
||||||
|
{
|
||||||
|
if (substr($parms,-1) == "f")
|
||||||
|
{
|
||||||
|
$d = (int) substr($parms,0,-1);
|
||||||
|
if (isset($btfut[$d]) && $btfut[$d] != "") $thislbl = $btfut[$d];
|
||||||
|
else $btfut[$d] = $thislbl = "label_" . $labelcnt++;
|
||||||
|
$parms = $thislbl;
|
||||||
|
}
|
||||||
|
else if (substr($parms,-1) == "b")
|
||||||
|
{
|
||||||
|
$d = (int) substr($parms,0,-1);
|
||||||
|
if ($bthist[$d]=="") echo "Error resolving label $parms\n";
|
||||||
|
$parms = $bthist[$d];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (stristr($parms,"[0xfefefefe]"))
|
||||||
|
{
|
||||||
|
if ($inst == "fmul" || $inst=="fadd" || $inst == "fcomp")
|
||||||
|
{
|
||||||
|
if ($inst=="fmul") $hdr="0x0D";
|
||||||
|
if ($inst=="fadd") $hdr="0x05";
|
||||||
|
if ($inst=="fcomp") $hdr="0x1D";
|
||||||
|
|
||||||
|
fputs($out,"_emit 0xDC; // $inst qword ptr [0xfefefefe]\n");
|
||||||
|
fputs($out,"_emit $hdr;\n");
|
||||||
|
fputs($out,"_emit 0xFE;\n");
|
||||||
|
fputs($out,"_emit 0xFE;\n");
|
||||||
|
fputs($out,"_emit 0xFE;\n");
|
||||||
|
fputs($out,"_emit 0xFE;\n");
|
||||||
|
$nowrite=1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$sline = $inst;
|
||||||
|
if ($parms !="") $sline .= " " . $parms;
|
||||||
|
$sline .= ";";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
$sline=preg_replace("/FPREG_([0-9]+)/","st($1)",$sline);
|
||||||
|
$line = $beg_restore . $sline . $end_restore;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$nowrite)
|
||||||
|
{
|
||||||
|
if (strstr($line,"__TEMP_REPLACE__"))
|
||||||
|
{
|
||||||
|
$a = strstr($line,"//REPLACE=");
|
||||||
|
if ($a === false) die ("__TEMP_REPLACE__ found, no REPLACE=\n");
|
||||||
|
$line=str_replace("__TEMP_REPLACE__",substr($a,10),$line);
|
||||||
|
}
|
||||||
|
fputs($out,$line . "\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($inblock) echo "Error (ended in __asm__ block???)\n";
|
||||||
|
|
||||||
|
|
||||||
|
fclose($in);
|
||||||
|
fclose($out);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
process_file("asm-nseel-x86-gcc.c" , "asm-nseel-x86-msvc.c");
|
||||||
|
// process_file("asm-miscfunc-x86-gcc.c" , "asm-miscfunc-x86-msvc.c");
|
||||||
|
//process_file("asm-megabuf-x86-gcc.c" , "asm-megabuf-x86-msvc.c");
|
||||||
|
|
||||||
|
?>
|
||||||
1270
oversampling/WDL/eel2/asm-nseel-aarch64-gcc.c
Normal file
1270
oversampling/WDL/eel2/asm-nseel-aarch64-gcc.c
Normal file
File diff suppressed because it is too large
Load Diff
1418
oversampling/WDL/eel2/asm-nseel-aarch64-msvc.asm
Normal file
1418
oversampling/WDL/eel2/asm-nseel-aarch64-msvc.asm
Normal file
File diff suppressed because it is too large
Load Diff
1308
oversampling/WDL/eel2/asm-nseel-arm-gcc.c
Normal file
1308
oversampling/WDL/eel2/asm-nseel-arm-gcc.c
Normal file
File diff suppressed because it is too large
Load Diff
1416
oversampling/WDL/eel2/asm-nseel-arm64ec.asm
Normal file
1416
oversampling/WDL/eel2/asm-nseel-arm64ec.asm
Normal file
File diff suppressed because it is too large
Load Diff
BIN
oversampling/WDL/eel2/asm-nseel-arm64ec.obj
Normal file
BIN
oversampling/WDL/eel2/asm-nseel-arm64ec.obj
Normal file
Binary file not shown.
BIN
oversampling/WDL/eel2/asm-nseel-multi-macho.o
Normal file
BIN
oversampling/WDL/eel2/asm-nseel-multi-macho.o
Normal file
Binary file not shown.
1436
oversampling/WDL/eel2/asm-nseel-ppc-gcc.c
Normal file
1436
oversampling/WDL/eel2/asm-nseel-ppc-gcc.c
Normal file
File diff suppressed because it is too large
Load Diff
BIN
oversampling/WDL/eel2/asm-nseel-x64-macho.o
Normal file
BIN
oversampling/WDL/eel2/asm-nseel-x64-macho.o
Normal file
Binary file not shown.
1357
oversampling/WDL/eel2/asm-nseel-x64-sse.asm
Normal file
1357
oversampling/WDL/eel2/asm-nseel-x64-sse.asm
Normal file
File diff suppressed because it is too large
Load Diff
BIN
oversampling/WDL/eel2/asm-nseel-x64.obj
Normal file
BIN
oversampling/WDL/eel2/asm-nseel-x64.obj
Normal file
Binary file not shown.
1332
oversampling/WDL/eel2/asm-nseel-x86-gcc.c
Normal file
1332
oversampling/WDL/eel2/asm-nseel-x86-gcc.c
Normal file
File diff suppressed because it is too large
Load Diff
3017
oversampling/WDL/eel2/asm-nseel-x86-msvc.c
Normal file
3017
oversampling/WDL/eel2/asm-nseel-x86-msvc.c
Normal file
File diff suppressed because it is too large
Load Diff
113
oversampling/WDL/eel2/eel2.l
Normal file
113
oversampling/WDL/eel2/eel2.l
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
%option reentrant
|
||||||
|
%option prefix="nseel"
|
||||||
|
%option bison-bridge
|
||||||
|
%option bison-locations
|
||||||
|
%option noyywrap
|
||||||
|
%option never-interactive
|
||||||
|
%option batch
|
||||||
|
%option nounput
|
||||||
|
|
||||||
|
%{
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define YY_USER_ACTION yylloc->first_line = yylineno;
|
||||||
|
|
||||||
|
#define YY_FATAL_ERROR(msg) { ((struct yyguts_t*)yyscanner)->yyextra_r->errVar=1; }
|
||||||
|
#define YY_INPUT(buf,result,max_size) { (result) = nseel_gets(yyextra,(buf),max_size); }
|
||||||
|
|
||||||
|
#define YY_EXTRA_TYPE compileContext *
|
||||||
|
|
||||||
|
#undef YY_BUF_SIZE
|
||||||
|
#define YY_BUF_SIZE (NSEEL_MAX_VARIABLE_NAMELEN*2)
|
||||||
|
|
||||||
|
#undef YY_READ_BUF_SIZE
|
||||||
|
#define YY_READ_BUF_SIZE (NSEEL_MAX_VARIABLE_NAMELEN)
|
||||||
|
|
||||||
|
#include "y.tab.h"
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#define YY_NO_UNISTD_H
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "ns-eel-int.h"
|
||||||
|
|
||||||
|
int nseel_gets(compileContext *ctx, char *buf, size_t sz);
|
||||||
|
|
||||||
|
#define PARSENUM *yylval = nseel_translate(yyextra,yytext, 0); return VALUE;
|
||||||
|
#define EEL_ACTION(x) return x;
|
||||||
|
|
||||||
|
#ifdef stdin
|
||||||
|
#undef stdin
|
||||||
|
#endif
|
||||||
|
#define stdin (0)
|
||||||
|
|
||||||
|
#ifdef stdout
|
||||||
|
#undef stdout
|
||||||
|
#endif
|
||||||
|
#define stdout (0)
|
||||||
|
|
||||||
|
static int g_fake_errno;
|
||||||
|
#ifdef errno
|
||||||
|
#undef errno
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define errno g_fake_errno
|
||||||
|
|
||||||
|
static void comment(yyscan_t yyscanner);
|
||||||
|
|
||||||
|
%}
|
||||||
|
|
||||||
|
%%
|
||||||
|
|
||||||
|
[0-9]+\.?[0-9]* PARSENUM;
|
||||||
|
\.[0-9]+ PARSENUM;
|
||||||
|
0[xX][0-9a-fA-F]* PARSENUM;
|
||||||
|
\$[xX][0-9a-fA-F]* PARSENUM;
|
||||||
|
\$\~[0-9]* PARSENUM;
|
||||||
|
\$[Ee] PARSENUM;
|
||||||
|
\$[Pp][Ii] PARSENUM;
|
||||||
|
\$[Pp][Hh][Ii] PARSENUM;
|
||||||
|
\$\'.\' PARSENUM;
|
||||||
|
\#[a-zA-Z0-9\._]* *yylval = nseel_translate(yyextra,yytext, 0); return STRING_IDENTIFIER;
|
||||||
|
\<\< return TOKEN_SHL;
|
||||||
|
\>\> return TOKEN_SHR;
|
||||||
|
\<= return TOKEN_LTE;
|
||||||
|
\>= return TOKEN_GTE;
|
||||||
|
== return TOKEN_EQ;
|
||||||
|
=== return TOKEN_EQ_EXACT;
|
||||||
|
\!= return TOKEN_NE;
|
||||||
|
\!== return TOKEN_NE_EXACT;
|
||||||
|
\&\& return TOKEN_LOGICAL_AND;
|
||||||
|
\|\| return TOKEN_LOGICAL_OR;
|
||||||
|
\+= return TOKEN_ADD_OP;
|
||||||
|
-= return TOKEN_SUB_OP;
|
||||||
|
%= return TOKEN_MOD_OP;
|
||||||
|
\|= return TOKEN_OR_OP;
|
||||||
|
\&= return TOKEN_AND_OP;
|
||||||
|
\~= return TOKEN_XOR_OP;
|
||||||
|
\/= return TOKEN_DIV_OP;
|
||||||
|
\*= return TOKEN_MUL_OP;
|
||||||
|
\^= return TOKEN_POW_OP;
|
||||||
|
|
||||||
|
[a-zA-Z_][a-zA-Z0-9\._]* &yylval = nseel_createCompiledValuePtr((compileContext *)yyextra, NULL, yytext); return IDENTIFIER;
|
||||||
|
|
||||||
|
[ \t\r\n]+ /* whitespace */
|
||||||
|
\/\/.*$ /* comment */
|
||||||
|
"/*" { comment(yyscanner); }
|
||||||
|
|
||||||
|
. return (int)yytext[0];
|
||||||
|
|
||||||
|
%%
|
||||||
|
|
||||||
|
static void comment(yyscan_t yyscanner)
|
||||||
|
{
|
||||||
|
int c,lc=0;
|
||||||
|
|
||||||
|
while (0 != (c = input(yyscanner)))
|
||||||
|
{
|
||||||
|
if (c == '/' && lc == '*') return;
|
||||||
|
lc = c;
|
||||||
|
}
|
||||||
|
// end of file, ignore for now
|
||||||
|
}
|
||||||
370
oversampling/WDL/eel2/eel2.vim
Normal file
370
oversampling/WDL/eel2/eel2.vim
Normal file
@@ -0,0 +1,370 @@
|
|||||||
|
" Vim syntax file
|
||||||
|
" This needs a lot of C-specific stuff removed, please help if you care =)
|
||||||
|
" Language: EEL2, based on:
|
||||||
|
"
|
||||||
|
"
|
||||||
|
" Language: C - Maintainer: Bram Moolenaar <Bram@vim.org> - Last Change: 2009 Nov 17
|
||||||
|
|
||||||
|
" Quit when a (custom) syntax file was already loaded
|
||||||
|
if exists("b:current_syntax")
|
||||||
|
finish
|
||||||
|
endif
|
||||||
|
|
||||||
|
" A bunch of useful C keywords
|
||||||
|
syn keyword cStatement function globals global local instance
|
||||||
|
syn keyword cRepeat while loop
|
||||||
|
syn keyword cRepeat sin cos tan sqrt log log10 asin acos atan atan2 exp abs sqr min max sign rand floor ceil invsqrt freembuf memcpy memset stack_psuh stack_pop stack_peek stack_exch
|
||||||
|
syn keyword cRepeat atomic_setifequal atomic_exch atomic_add atomic_set atomic_get convolve_c fft ifft fft_permute fft_ipermute fopen fread fgets fgetc fwrite fprintf fseek ftell feof fflush fclose
|
||||||
|
syn keyword cRepeat gfx_lineto gfx_lineto gfx_rectto gfx_rect gfx_line gfx_gradrect gfx_muladdrect gfx_deltablit gfx_transformblit gfx_blurto gfx_drawnumber gfx_drawchar gfx_drawstr gfx_measurestr gfx_printf gfx_setpixel gfx_getpixel gfx_getimgdim gfx_setimgdim gfx_loadimg gfx_blit gfx_blitext gfx_blit gfx_setfont gfx_getfont gfx_init gfx_quit gfx_getchar
|
||||||
|
syn keyword cRepeat mdct imdct sleep time time_precise tcp_listen tcp_listen_end tcp_connect tcp_send tcp_recv tcp_set_block tcp_close strlen
|
||||||
|
syn keyword cRepeat strcat strcpy strcmp stricmp strncat strncpy strncmp strnicmp str_setlen strcpy_from strcpy_substr strcpy_substr str_getchar str_setchar str_getchar str_setchar str_insert str_delsub sprintf printf match matchi
|
||||||
|
|
||||||
|
syn keyword cTodo contained TODO FIXME XXX
|
||||||
|
|
||||||
|
" It's easy to accidentally add a space after a backslash that was intended
|
||||||
|
" for line continuation. Some compilers allow it, which makes it
|
||||||
|
" unpredicatable and should be avoided.
|
||||||
|
syn match cBadContinuation contained "\\\s\+$"
|
||||||
|
|
||||||
|
" cCommentGroup allows adding matches for special things in comments
|
||||||
|
syn cluster cCommentGroup contains=cTodo,cBadContinuation
|
||||||
|
|
||||||
|
" String and Character constants
|
||||||
|
" Highlight special characters (those which have a backslash) differently
|
||||||
|
syn match cSpecial display contained "\\\(x\x\+\|\o\{1,3}\|.\|$\)"
|
||||||
|
if !exists("c_no_utf")
|
||||||
|
syn match cSpecial display contained "\\\(u\x\{4}\|U\x\{8}\)"
|
||||||
|
endif
|
||||||
|
if exists("c_no_cformat")
|
||||||
|
syn region cString start=+L\="+ skip=+\\\\\|\\"+ end=+"+ contains=cSpecial,@Spell
|
||||||
|
" cCppString: same as cString, but ends at end of line
|
||||||
|
syn region cCppString start=+L\="+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end='$' contains=cSpecial,@Spell
|
||||||
|
else
|
||||||
|
if !exists("c_no_c99") " ISO C99
|
||||||
|
syn match cFormat display "%\(\d\+\$\)\=[-+' #0*]*\(\d*\|\*\|\*\d\+\$\)\(\.\(\d*\|\*\|\*\d\+\$\)\)\=\([hlLjzt]\|ll\|hh\)\=\([aAbdiuoxXDOUfFeEgGcCsSpn]\|\[\^\=.[^]]*\]\)" contained
|
||||||
|
else
|
||||||
|
syn match cFormat display "%\(\d\+\$\)\=[-+' #0*]*\(\d*\|\*\|\*\d\+\$\)\(\.\(\d*\|\*\|\*\d\+\$\)\)\=\([hlL]\|ll\)\=\([bdiuoxXDOUfeEgGcCsSpn]\|\[\^\=.[^]]*\]\)" contained
|
||||||
|
endif
|
||||||
|
syn match cFormat display "%%" contained
|
||||||
|
syn region cString start=+L\="+ skip=+\\\\\|\\"+ end=+"+ contains=cSpecial,cFormat,@Spell
|
||||||
|
" cCppString: same as cString, but ends at end of line
|
||||||
|
syn region cCppString start=+L\="+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end='$' contains=cSpecial,cFormat,@Spell
|
||||||
|
endif
|
||||||
|
|
||||||
|
syn match cCharacter "L\='[^\\]'"
|
||||||
|
syn match cCharacter "L'[^']*'" contains=cSpecial
|
||||||
|
syn match cCharacter "'[^']*'" contains=cSpecial
|
||||||
|
if exists("c_gnu")
|
||||||
|
syn match cSpecialError "L\='\\[^'\"?\\abefnrtv]'"
|
||||||
|
syn match cSpecialCharacter "L\='\\['\"?\\abefnrtv]'"
|
||||||
|
else
|
||||||
|
syn match cSpecialError "L\='\\[^'\"?\\abfnrtv]'"
|
||||||
|
syn match cSpecialCharacter "L\='\\['\"?\\abfnrtv]'"
|
||||||
|
endif
|
||||||
|
syn match cSpecialCharacter display "L\='\\\o\{1,3}'"
|
||||||
|
syn match cSpecialCharacter display "'\\x\x\{1,2}'"
|
||||||
|
syn match cSpecialCharacter display "L'\\x\x\+'"
|
||||||
|
|
||||||
|
"when wanted, highlight trailing white space
|
||||||
|
if exists("c_space_errors")
|
||||||
|
if !exists("c_no_trail_space_error")
|
||||||
|
syn match cSpaceError display excludenl "\s\+$"
|
||||||
|
endif
|
||||||
|
if !exists("c_no_tab_space_error")
|
||||||
|
syn match cSpaceError display " \+\t"me=e-1
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
|
" This should be before cErrInParen to avoid problems with #define ({ xxx })
|
||||||
|
if exists("c_curly_error")
|
||||||
|
syntax match cCurlyError "}"
|
||||||
|
syntax region cBlock start="{" end="}" contains=ALLBUT,cCurlyError,@cParenGroup,cErrInParen,cCppParen,cErrInBracket,cCppBracket,cCppString,@Spell fold
|
||||||
|
else
|
||||||
|
syntax region cBlock start="{" end="}" transparent fold
|
||||||
|
endif
|
||||||
|
|
||||||
|
"catch errors caused by wrong parenthesis and brackets
|
||||||
|
" also accept <% for {, %> for }, <: for [ and :> for ] (C99)
|
||||||
|
" But avoid matching <::.
|
||||||
|
syn cluster cParenGroup contains=cParenError,cIncluded,cSpecial,cCommentSkip,cCommentString,cComment2String,@cCommentGroup,cCommentStartError,cUserCont,cUserLabel,cOctalZero,cCppOut,cCppOut2,cCppSkip,cFormat,cNumber,cFloat,cOctal,cOctalError,cNumbersCom
|
||||||
|
if exists("c_no_curly_error")
|
||||||
|
syn region cParen transparent start='(' end=')' contains=ALLBUT,@cParenGroup,cCppParen,cCppString,@Spell
|
||||||
|
" cCppParen: same as cParen but ends at end-of-line; used in cDefine
|
||||||
|
syn region cCppParen transparent start='(' skip='\\$' excludenl end=')' end='$' contained contains=ALLBUT,@cParenGroup,cParen,cString,@Spell
|
||||||
|
syn match cParenError display ")"
|
||||||
|
syn match cErrInParen display contained "^[{}]\|^<%\|^%>"
|
||||||
|
elseif exists("c_no_bracket_error")
|
||||||
|
syn region cParen transparent start='(' end=')' contains=ALLBUT,@cParenGroup,cCppParen,cCppString,@Spell
|
||||||
|
" cCppParen: same as cParen but ends at end-of-line; used in cDefine
|
||||||
|
syn region cCppParen transparent start='(' skip='\\$' excludenl end=')' end='$' contained contains=ALLBUT,@cParenGroup,cParen,cString,@Spell
|
||||||
|
syn match cParenError display ")"
|
||||||
|
syn match cErrInParen display contained "[{}]\|<%\|%>"
|
||||||
|
else
|
||||||
|
syn region cParen transparent start='(' end=')' contains=ALLBUT,@cParenGroup,cCppParen,cErrInBracket,cCppBracket,cCppString,@Spell
|
||||||
|
" cCppParen: same as cParen but ends at end-of-line; used in cDefine
|
||||||
|
syn region cCppParen transparent start='(' skip='\\$' excludenl end=')' end='$' contained contains=ALLBUT,@cParenGroup,cErrInBracket,cParen,cBracket,cString,@Spell
|
||||||
|
syn match cParenError display "[\])]"
|
||||||
|
syn match cErrInParen display contained "[\]{}]\|<%\|%>"
|
||||||
|
syn region cBracket transparent start='\[\|<::\@!' end=']\|:>' contains=ALLBUT,@cParenGroup,cErrInParen,cCppParen,cCppBracket,cCppString,@Spell
|
||||||
|
" cCppBracket: same as cParen but ends at end-of-line; used in cDefine
|
||||||
|
syn region cCppBracket transparent start='\[\|<::\@!' skip='\\$' excludenl end=']\|:>' end='$' contained contains=ALLBUT,@cParenGroup,cErrInParen,cParen,cBracket,cString,@Spell
|
||||||
|
syn match cErrInBracket display contained "[){}]\|<%\|%>"
|
||||||
|
endif
|
||||||
|
|
||||||
|
"integer number, or floating point number without a dot and with "f".
|
||||||
|
syn case ignore
|
||||||
|
syn match cNumbers display transparent "\<\d\|\.\d" contains=cNumber,cFloat,cOctalError,cOctal
|
||||||
|
" Same, but without octal error (for comments)
|
||||||
|
syn match cNumbersCom display contained transparent "\<\d\|\.\d" contains=cNumber,cFloat,cOctal
|
||||||
|
syn match cNumber display contained "\d\+\(u\=l\{0,2}\|ll\=u\)\>"
|
||||||
|
"hex number
|
||||||
|
syn match cNumber display contained "0x\x\+\(u\=l\{0,2}\|ll\=u\)\>"
|
||||||
|
" Flag the first zero of an octal number as something special
|
||||||
|
syn match cOctal display contained "0\o\+\(u\=l\{0,2}\|ll\=u\)\>" contains=cOctalZero
|
||||||
|
syn match cOctalZero display contained "\<0"
|
||||||
|
syn match cFloat display contained "\d\+f"
|
||||||
|
"floating point number, with dot, optional exponent
|
||||||
|
syn match cFloat display contained "\d\+\.\d*\(e[-+]\=\d\+\)\=[fl]\="
|
||||||
|
"floating point number, starting with a dot, optional exponent
|
||||||
|
syn match cFloat display contained "\.\d\+\(e[-+]\=\d\+\)\=[fl]\=\>"
|
||||||
|
"floating point number, without dot, with exponent
|
||||||
|
syn match cFloat display contained "\d\+e[-+]\=\d\+[fl]\=\>"
|
||||||
|
if !exists("c_no_c99")
|
||||||
|
"hexadecimal floating point number, optional leading digits, with dot, with exponent
|
||||||
|
syn match cFloat display contained "0x\x*\.\x\+p[-+]\=\d\+[fl]\=\>"
|
||||||
|
"hexadecimal floating point number, with leading digits, optional dot, with exponent
|
||||||
|
syn match cFloat display contained "0x\x\+\.\=p[-+]\=\d\+[fl]\=\>"
|
||||||
|
endif
|
||||||
|
|
||||||
|
" flag an octal number with wrong digits
|
||||||
|
syn match cOctalError display contained "0\o*[89]\d*"
|
||||||
|
syn case match
|
||||||
|
|
||||||
|
if exists("c_comment_strings")
|
||||||
|
" A comment can contain cString, cCharacter and cNumber.
|
||||||
|
" But a "*/" inside a cString in a cComment DOES end the comment! So we
|
||||||
|
" need to use a special type of cString: cCommentString, which also ends on
|
||||||
|
" "*/", and sees a "*" at the start of the line as comment again.
|
||||||
|
" Unfortunately this doesn't very well work for // type of comments :-(
|
||||||
|
syntax match cCommentSkip contained "^\s*\*\($\|\s\+\)"
|
||||||
|
syntax region cCommentString contained start=+L\=\\\@<!"+ skip=+\\\\\|\\"+ end=+"+ end=+\*/+me=s-1 contains=cSpecial,cCommentSkip
|
||||||
|
syntax region cComment2String contained start=+L\=\\\@<!"+ skip=+\\\\\|\\"+ end=+"+ end="$" contains=cSpecial
|
||||||
|
syntax region cCommentL start="//" skip="\\$" end="$" keepend contains=@cCommentGroup,cComment2String,cCharacter,cNumbersCom,cSpaceError,@Spell
|
||||||
|
if exists("c_no_comment_fold")
|
||||||
|
" Use "extend" here to have preprocessor lines not terminate halfway a
|
||||||
|
" comment.
|
||||||
|
syntax region cComment matchgroup=cCommentStart start="/\*" end="\*/" contains=@cCommentGroup,cCommentStartError,cCommentString,cCharacter,cNumbersCom,cSpaceError,@Spell extend
|
||||||
|
else
|
||||||
|
syntax region cComment matchgroup=cCommentStart start="/\*" end="\*/" contains=@cCommentGroup,cCommentStartError,cCommentString,cCharacter,cNumbersCom,cSpaceError,@Spell fold extend
|
||||||
|
endif
|
||||||
|
else
|
||||||
|
syn region cCommentL start="//" skip="\\$" end="$" keepend contains=@cCommentGroup,cSpaceError,@Spell
|
||||||
|
if exists("c_no_comment_fold")
|
||||||
|
syn region cComment matchgroup=cCommentStart start="/\*" end="\*/" contains=@cCommentGroup,cCommentStartError,cSpaceError,@Spell extend
|
||||||
|
else
|
||||||
|
syn region cComment matchgroup=cCommentStart start="/\*" end="\*/" contains=@cCommentGroup,cCommentStartError,cSpaceError,@Spell fold extend
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
" keep a // comment separately, it terminates a preproc. conditional
|
||||||
|
syntax match cCommentError display "\*/"
|
||||||
|
syntax match cCommentStartError display "/\*"me=e-1 contained
|
||||||
|
|
||||||
|
syn keyword cOperator sizeof
|
||||||
|
if exists("c_gnu")
|
||||||
|
syn keyword cStatement __asm__
|
||||||
|
syn keyword cOperator typeof __real__ __imag__
|
||||||
|
endif
|
||||||
|
syn keyword cType int long short char void
|
||||||
|
syn keyword cType signed unsigned float double
|
||||||
|
if !exists("c_no_ansi") || exists("c_ansi_typedefs")
|
||||||
|
syn keyword cType size_t ssize_t off_t wchar_t ptrdiff_t sig_atomic_t fpos_t
|
||||||
|
syn keyword cType clock_t time_t va_list jmp_buf FILE DIR div_t ldiv_t
|
||||||
|
syn keyword cType mbstate_t wctrans_t wint_t wctype_t
|
||||||
|
endif
|
||||||
|
if !exists("c_no_c99") " ISO C99
|
||||||
|
syn keyword cType bool complex
|
||||||
|
syn keyword cType int8_t int16_t int32_t int64_t
|
||||||
|
syn keyword cType uint8_t uint16_t uint32_t uint64_t
|
||||||
|
syn keyword cType int_least8_t int_least16_t int_least32_t int_least64_t
|
||||||
|
syn keyword cType uint_least8_t uint_least16_t uint_least32_t uint_least64_t
|
||||||
|
syn keyword cType int_fast8_t int_fast16_t int_fast32_t int_fast64_t
|
||||||
|
syn keyword cType uint_fast8_t uint_fast16_t uint_fast32_t uint_fast64_t
|
||||||
|
syn keyword cType intptr_t uintptr_t
|
||||||
|
syn keyword cType intmax_t uintmax_t
|
||||||
|
endif
|
||||||
|
if exists("c_gnu")
|
||||||
|
syn keyword cType __label__ __complex__ __volatile__
|
||||||
|
endif
|
||||||
|
|
||||||
|
syn keyword cStructure struct union enum typedef
|
||||||
|
syn keyword cStorageClass static register auto volatile extern const
|
||||||
|
if exists("c_gnu")
|
||||||
|
syn keyword cStorageClass inline __attribute__
|
||||||
|
endif
|
||||||
|
if !exists("c_no_c99")
|
||||||
|
syn keyword cStorageClass inline restrict
|
||||||
|
endif
|
||||||
|
|
||||||
|
if !exists("c_no_ansi") || exists("c_ansi_constants") || exists("c_gnu")
|
||||||
|
if exists("c_gnu")
|
||||||
|
syn keyword cConstant __GNUC__ __FUNCTION__ __PRETTY_FUNCTION__ __func__
|
||||||
|
endif
|
||||||
|
syn keyword cConstant __LINE__ __FILE__ __DATE__ __TIME__ __STDC__
|
||||||
|
syn keyword cConstant __STDC_VERSION__
|
||||||
|
syn keyword cConstant CHAR_BIT MB_LEN_MAX MB_CUR_MAX
|
||||||
|
syn keyword cConstant UCHAR_MAX UINT_MAX ULONG_MAX USHRT_MAX
|
||||||
|
syn keyword cConstant CHAR_MIN INT_MIN LONG_MIN SHRT_MIN
|
||||||
|
syn keyword cConstant CHAR_MAX INT_MAX LONG_MAX SHRT_MAX
|
||||||
|
syn keyword cConstant SCHAR_MIN SINT_MIN SLONG_MIN SSHRT_MIN
|
||||||
|
syn keyword cConstant SCHAR_MAX SINT_MAX SLONG_MAX SSHRT_MAX
|
||||||
|
if !exists("c_no_c99")
|
||||||
|
syn keyword cConstant __func__
|
||||||
|
syn keyword cConstant LLONG_MIN LLONG_MAX ULLONG_MAX
|
||||||
|
syn keyword cConstant INT8_MIN INT16_MIN INT32_MIN INT64_MIN
|
||||||
|
syn keyword cConstant INT8_MAX INT16_MAX INT32_MAX INT64_MAX
|
||||||
|
syn keyword cConstant UINT8_MAX UINT16_MAX UINT32_MAX UINT64_MAX
|
||||||
|
syn keyword cConstant INT_LEAST8_MIN INT_LEAST16_MIN INT_LEAST32_MIN INT_LEAST64_MIN
|
||||||
|
syn keyword cConstant INT_LEAST8_MAX INT_LEAST16_MAX INT_LEAST32_MAX INT_LEAST64_MAX
|
||||||
|
syn keyword cConstant UINT_LEAST8_MAX UINT_LEAST16_MAX UINT_LEAST32_MAX UINT_LEAST64_MAX
|
||||||
|
syn keyword cConstant INT_FAST8_MIN INT_FAST16_MIN INT_FAST32_MIN INT_FAST64_MIN
|
||||||
|
syn keyword cConstant INT_FAST8_MAX INT_FAST16_MAX INT_FAST32_MAX INT_FAST64_MAX
|
||||||
|
syn keyword cConstant UINT_FAST8_MAX UINT_FAST16_MAX UINT_FAST32_MAX UINT_FAST64_MAX
|
||||||
|
syn keyword cConstant INTPTR_MIN INTPTR_MAX UINTPTR_MAX
|
||||||
|
syn keyword cConstant INTMAX_MIN INTMAX_MAX UINTMAX_MAX
|
||||||
|
syn keyword cConstant PTRDIFF_MIN PTRDIFF_MAX SIG_ATOMIC_MIN SIG_ATOMIC_MAX
|
||||||
|
syn keyword cConstant SIZE_MAX WCHAR_MIN WCHAR_MAX WINT_MIN WINT_MAX
|
||||||
|
endif
|
||||||
|
syn keyword cConstant FLT_RADIX FLT_ROUNDS
|
||||||
|
syn keyword cConstant FLT_DIG FLT_MANT_DIG FLT_EPSILON
|
||||||
|
syn keyword cConstant DBL_DIG DBL_MANT_DIG DBL_EPSILON
|
||||||
|
syn keyword cConstant LDBL_DIG LDBL_MANT_DIG LDBL_EPSILON
|
||||||
|
syn keyword cConstant FLT_MIN FLT_MAX FLT_MIN_EXP FLT_MAX_EXP
|
||||||
|
syn keyword cConstant FLT_MIN_10_EXP FLT_MAX_10_EXP
|
||||||
|
syn keyword cConstant DBL_MIN DBL_MAX DBL_MIN_EXP DBL_MAX_EXP
|
||||||
|
syn keyword cConstant DBL_MIN_10_EXP DBL_MAX_10_EXP
|
||||||
|
syn keyword cConstant LDBL_MIN LDBL_MAX LDBL_MIN_EXP LDBL_MAX_EXP
|
||||||
|
syn keyword cConstant LDBL_MIN_10_EXP LDBL_MAX_10_EXP
|
||||||
|
syn keyword cConstant HUGE_VAL CLOCKS_PER_SEC NULL
|
||||||
|
syn keyword cConstant LC_ALL LC_COLLATE LC_CTYPE LC_MONETARY
|
||||||
|
syn keyword cConstant LC_NUMERIC LC_TIME
|
||||||
|
syn keyword cConstant SIG_DFL SIG_ERR SIG_IGN
|
||||||
|
syn keyword cConstant SIGABRT SIGFPE SIGILL SIGHUP SIGINT SIGSEGV SIGTERM
|
||||||
|
" Add POSIX signals as well...
|
||||||
|
syn keyword cConstant SIGABRT SIGALRM SIGCHLD SIGCONT SIGFPE SIGHUP
|
||||||
|
syn keyword cConstant SIGILL SIGINT SIGKILL SIGPIPE SIGQUIT SIGSEGV
|
||||||
|
syn keyword cConstant SIGSTOP SIGTERM SIGTRAP SIGTSTP SIGTTIN SIGTTOU
|
||||||
|
syn keyword cConstant SIGUSR1 SIGUSR2
|
||||||
|
syn keyword cConstant _IOFBF _IOLBF _IONBF BUFSIZ EOF WEOF
|
||||||
|
syn keyword cConstant FOPEN_MAX FILENAME_MAX L_tmpnam
|
||||||
|
syn keyword cConstant SEEK_CUR SEEK_END SEEK_SET
|
||||||
|
syn keyword cConstant TMP_MAX stderr stdin stdout
|
||||||
|
syn keyword cConstant EXIT_FAILURE EXIT_SUCCESS RAND_MAX
|
||||||
|
" Add POSIX errors as well
|
||||||
|
syn keyword cConstant E2BIG EACCES EAGAIN EBADF EBADMSG EBUSY
|
||||||
|
syn keyword cConstant ECANCELED ECHILD EDEADLK EDOM EEXIST EFAULT
|
||||||
|
syn keyword cConstant EFBIG EILSEQ EINPROGRESS EINTR EINVAL EIO EISDIR
|
||||||
|
syn keyword cConstant EMFILE EMLINK EMSGSIZE ENAMETOOLONG ENFILE ENODEV
|
||||||
|
syn keyword cConstant ENOENT ENOEXEC ENOLCK ENOMEM ENOSPC ENOSYS
|
||||||
|
syn keyword cConstant ENOTDIR ENOTEMPTY ENOTSUP ENOTTY ENXIO EPERM
|
||||||
|
syn keyword cConstant EPIPE ERANGE EROFS ESPIPE ESRCH ETIMEDOUT EXDEV
|
||||||
|
" math.h
|
||||||
|
syn keyword cConstant M_E M_LOG2E M_LOG10E M_LN2 M_LN10 M_PI M_PI_2 M_PI_4
|
||||||
|
syn keyword cConstant M_1_PI M_2_PI M_2_SQRTPI M_SQRT2 M_SQRT1_2
|
||||||
|
endif
|
||||||
|
if !exists("c_no_c99") " ISO C99
|
||||||
|
syn keyword cConstant true false
|
||||||
|
endif
|
||||||
|
|
||||||
|
" Accept %: for # (C99)
|
||||||
|
syn region cPreCondit start="^\s*\(%:\|#\)\s*\(if\|ifdef\|ifndef\|elif\)\>" skip="\\$" end="$" keepend contains=cComment,cCommentL,cCppString,cCharacter,cCppParen,cParenError,cNumbers,cCommentError,cSpaceError
|
||||||
|
syn match cPreCondit display "^\s*\(%:\|#\)\s*\(else\|endif\)\>"
|
||||||
|
if !exists("c_no_if0")
|
||||||
|
if !exists("c_no_if0_fold")
|
||||||
|
syn region cCppOut start="^\s*\(%:\|#\)\s*if\s\+0\+\>" end=".\@=\|$" contains=cCppOut2 fold
|
||||||
|
else
|
||||||
|
syn region cCppOut start="^\s*\(%:\|#\)\s*if\s\+0\+\>" end=".\@=\|$" contains=cCppOut2
|
||||||
|
endif
|
||||||
|
syn region cCppOut2 contained start="0" end="^\s*\(%:\|#\)\s*\(endif\>\|else\>\|elif\>\)" contains=cSpaceError,cCppSkip
|
||||||
|
syn region cCppSkip contained start="^\s*\(%:\|#\)\s*\(if\>\|ifdef\>\|ifndef\>\)" skip="\\$" end="^\s*\(%:\|#\)\s*endif\>" contains=cSpaceError,cCppSkip
|
||||||
|
endif
|
||||||
|
syn region cIncluded display contained start=+"+ skip=+\\\\\|\\"+ end=+"+
|
||||||
|
syn match cIncluded display contained "<[^>]*>"
|
||||||
|
syn match cInclude display "^\s*\(%:\|#\)\s*include\>\s*["<]" contains=cIncluded
|
||||||
|
"syn match cLineSkip "\\$"
|
||||||
|
syn cluster cPreProcGroup contains=cPreCondit,cIncluded,cInclude,cDefine,cErrInParen,cErrInBracket,cUserLabel,cSpecial,cOctalZero,cCppOut,cCppOut2,cCppSkip,cFormat,cNumber,cFloat,cOctal,cOctalError,cNumbersCom,cString,cCommentSkip,cCommentString,cComment2String,@cCommentGroup,cCommentStartError,cParen,cBracket,cMulti
|
||||||
|
syn region cDefine start="^\s*\(%:\|#\)\s*\(define\|undef\)\>" skip="\\$" end="$" keepend contains=ALLBUT,@cPreProcGroup,@Spell
|
||||||
|
syn region cPreProc start="^\s*\(%:\|#\)\s*\(pragma\>\|line\>\|warning\>\|warn\>\|error\>\)" skip="\\$" end="$" keepend contains=ALLBUT,@cPreProcGroup,@Spell
|
||||||
|
|
||||||
|
" Highlight User Labels
|
||||||
|
syn cluster cMultiGroup contains=cIncluded,cSpecial,cCommentSkip,cCommentString,cComment2String,@cCommentGroup,cCommentStartError,cUserCont,cUserLabel,cOctalZero,cCppOut,cCppOut2,cCppSkip,cFormat,cNumber,cFloat,cOctal,cOctalError,cNumbersCom,cCppParen,cCppBracket,cCppString
|
||||||
|
syn region cMulti transparent start='?' skip='::' end=':' end=')' end=';' contains=ALLBUT,@cMultiGroup,@Spell
|
||||||
|
" Avoid matching foo::bar() in C++ by requiring that the next char is not ':'
|
||||||
|
|
||||||
|
syn match cUserLabel display "\I\i*" contained
|
||||||
|
|
||||||
|
|
||||||
|
if exists("c_minlines")
|
||||||
|
let b:c_minlines = c_minlines
|
||||||
|
else
|
||||||
|
if !exists("c_no_if0")
|
||||||
|
let b:c_minlines = 50 " #if 0 constructs can be long
|
||||||
|
else
|
||||||
|
let b:c_minlines = 15 " mostly for () constructs
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
if exists("c_curly_error")
|
||||||
|
syn sync fromstart
|
||||||
|
else
|
||||||
|
exec "syn sync ccomment cComment minlines=" . b:c_minlines
|
||||||
|
endif
|
||||||
|
|
||||||
|
" Define the default highlighting.
|
||||||
|
" Only used when an item doesn't have highlighting yet
|
||||||
|
hi def link cFormat cSpecial
|
||||||
|
hi def link cCppString cString
|
||||||
|
hi def link cCommentL cComment
|
||||||
|
hi def link cCommentStart cComment
|
||||||
|
hi def link cUserLabel Label
|
||||||
|
hi def link cRepeat Repeat
|
||||||
|
hi def link cCharacter Character
|
||||||
|
hi def link cSpecialCharacter cSpecial
|
||||||
|
hi def link cNumber Number
|
||||||
|
hi def link cOctal Number
|
||||||
|
hi def link cOctalZero PreProc " link this to Error if you want
|
||||||
|
hi def link cFloat Float
|
||||||
|
hi def link cOctalError cError
|
||||||
|
hi def link cParenError cError
|
||||||
|
hi def link cErrInParen cError
|
||||||
|
hi def link cErrInBracket cError
|
||||||
|
hi def link cCommentError cError
|
||||||
|
hi def link cCommentStartError cError
|
||||||
|
hi def link cSpaceError cError
|
||||||
|
hi def link cSpecialError cError
|
||||||
|
hi def link cCurlyError cError
|
||||||
|
hi def link cOperator Operator
|
||||||
|
hi def link cStructure Structure
|
||||||
|
hi def link cStorageClass StorageClass
|
||||||
|
hi def link cInclude Include
|
||||||
|
hi def link cPreProc PreProc
|
||||||
|
hi def link cDefine Macro
|
||||||
|
hi def link cIncluded cString
|
||||||
|
hi def link cError Error
|
||||||
|
hi def link cStatement Statement
|
||||||
|
hi def link cPreCondit PreCondit
|
||||||
|
hi def link cType Type
|
||||||
|
hi def link cConstant Constant
|
||||||
|
hi def link cCommentString cString
|
||||||
|
hi def link cComment2String cString
|
||||||
|
hi def link cCommentSkip cComment
|
||||||
|
hi def link cString String
|
||||||
|
hi def link cComment Comment
|
||||||
|
hi def link cSpecial SpecialChar
|
||||||
|
hi def link cTodo Todo
|
||||||
|
hi def link cBadContinuation Error
|
||||||
|
hi def link cCppSkip cCppOut
|
||||||
|
hi def link cCppOut2 cCppOut
|
||||||
|
hi def link cCppOut Comment
|
||||||
|
|
||||||
|
let b:current_syntax = "eel2"
|
||||||
|
|
||||||
|
" vim: ts=8
|
||||||
376
oversampling/WDL/eel2/eel2.y
Normal file
376
oversampling/WDL/eel2/eel2.y
Normal file
@@ -0,0 +1,376 @@
|
|||||||
|
%pure-parser
|
||||||
|
%name-prefix="nseel"
|
||||||
|
%parse-param { compileContext* context }
|
||||||
|
%lex-param { void* scanner }
|
||||||
|
|
||||||
|
|
||||||
|
/* this will prevent y.tab.c from ever calling yydestruct(), since we do not use it and it is a waste */
|
||||||
|
%destructor {
|
||||||
|
#define yydestruct(a,b,c,d,e)
|
||||||
|
} VALUE
|
||||||
|
|
||||||
|
|
||||||
|
%{
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#include "y.tab.h"
|
||||||
|
#include "ns-eel-int.h"
|
||||||
|
|
||||||
|
#define scanner context->scanner
|
||||||
|
#define YY_(x) ("")
|
||||||
|
|
||||||
|
%}
|
||||||
|
|
||||||
|
%token VALUE IDENTIFIER TOKEN_SHL TOKEN_SHR
|
||||||
|
%token TOKEN_LTE TOKEN_GTE TOKEN_EQ TOKEN_EQ_EXACT TOKEN_NE TOKEN_NE_EXACT TOKEN_LOGICAL_AND TOKEN_LOGICAL_OR
|
||||||
|
%token TOKEN_ADD_OP TOKEN_SUB_OP TOKEN_MOD_OP TOKEN_OR_OP TOKEN_AND_OP TOKEN_XOR_OP TOKEN_DIV_OP TOKEN_MUL_OP TOKEN_POW_OP
|
||||||
|
%token STRING_LITERAL STRING_IDENTIFIER
|
||||||
|
%expect 75
|
||||||
|
%start program
|
||||||
|
|
||||||
|
%%
|
||||||
|
|
||||||
|
more_params:
|
||||||
|
expression
|
||||||
|
| expression ',' more_params
|
||||||
|
{
|
||||||
|
$$ = nseel_createMoreParametersOpcode(context,$1,$3);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
string:
|
||||||
|
STRING_LITERAL
|
||||||
|
| STRING_LITERAL string
|
||||||
|
{
|
||||||
|
((struct eelStringSegmentRec *)$1)->_next = (struct eelStringSegmentRec *)$2;
|
||||||
|
$$ = $1;
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
assignable_value:
|
||||||
|
IDENTIFIER
|
||||||
|
{
|
||||||
|
if (!($$ = nseel_resolve_named_symbol(context, $1, -1, NULL))) /* convert from purely named to namespace-relative, etc */
|
||||||
|
{
|
||||||
|
yyerror(&yyloc, context, "");
|
||||||
|
YYERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* we used to have VALUE in here rather than rvalue, to allow 1=1 1+=2 etc, but silly to,
|
||||||
|
though this breaks Vmorph, which does 1=1 for a nop, and Jonas DrumReaplacer, which does x = 0 = y = 0 */
|
||||||
|
| '(' expression ')'
|
||||||
|
{
|
||||||
|
$$ = $2;
|
||||||
|
}
|
||||||
|
| IDENTIFIER '(' expression ')' '(' expression ')'
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
if (!($$ = nseel_setCompiledFunctionCallParameters(context,$1, $3, 0, 0, $6, &err)))
|
||||||
|
{
|
||||||
|
if (err == -1) yyerror(&yylsp[-2], context, "");
|
||||||
|
else if (err == 0) yyerror(&yylsp[-6], context, "");
|
||||||
|
else yyerror(&yylsp[-3], context, ""); // parameter count wrong
|
||||||
|
|
||||||
|
YYERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
| IDENTIFIER '(' expression ')'
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
if (!($$ = nseel_setCompiledFunctionCallParameters(context,$1, $3, 0, 0, 0, &err)))
|
||||||
|
{
|
||||||
|
if (err == 0) yyerror(&yylsp[-3], context, "");
|
||||||
|
else yyerror(&yylsp[0], context, ""); // parameter count wrong
|
||||||
|
YYERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
| IDENTIFIER '(' ')'
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
if (!($$ = nseel_setCompiledFunctionCallParameters(context,$1, nseel_createCompiledValue(context,0.0), 0, 0, 0,&err)))
|
||||||
|
{
|
||||||
|
if (err == 0) yyerror(&yylsp[-2], context, ""); // function not found
|
||||||
|
else yyerror(&yylsp[0], context, ""); // parameter count wrong
|
||||||
|
YYERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
| IDENTIFIER '(' expression ',' expression ')'
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
if (!($$ = nseel_setCompiledFunctionCallParameters(context,$1, $3, $5, 0, 0,&err)))
|
||||||
|
{
|
||||||
|
if (err == 0) yyerror(&yylsp[-5], context, "");
|
||||||
|
else if (err == 2) yyerror(&yylsp[0], context, ""); // needs more than 2 parameters
|
||||||
|
else yyerror(&yylsp[-2], context, ""); // less than 2
|
||||||
|
YYERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
| IDENTIFIER '(' expression ',' expression ',' more_params ')'
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
if (!($$ = nseel_setCompiledFunctionCallParameters(context,$1, $3, $5, $7, 0, &err)))
|
||||||
|
{
|
||||||
|
if (err == 0) yyerror(&yylsp[-7], context, "");
|
||||||
|
else if (err==2) yyerror(&yylsp[0], context, ""); // needs more parameters
|
||||||
|
else if (err==4) yyerror(&yylsp[-4], context, ""); // needs single parameter
|
||||||
|
else yyerror(&yylsp[-2], context, ""); // less parm
|
||||||
|
YYERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
| rvalue '[' ']'
|
||||||
|
{
|
||||||
|
$$ = nseel_createMemoryAccess(context,$1,0);
|
||||||
|
}
|
||||||
|
| rvalue '[' expression ']'
|
||||||
|
{
|
||||||
|
$$ = nseel_createMemoryAccess(context,$1,$3);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
rvalue:
|
||||||
|
VALUE
|
||||||
|
| STRING_IDENTIFIER
|
||||||
|
| string
|
||||||
|
{
|
||||||
|
$$ = nseel_eelMakeOpcodeFromStringSegments(context,(struct eelStringSegmentRec *)$1);
|
||||||
|
}
|
||||||
|
| assignable_value
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
|
assignment:
|
||||||
|
rvalue
|
||||||
|
| assignable_value '=' if_else_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_ASSIGN,2,$1,$3);
|
||||||
|
}
|
||||||
|
| assignable_value TOKEN_ADD_OP if_else_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_ADD_OP,2,$1,$3);
|
||||||
|
}
|
||||||
|
| assignable_value TOKEN_SUB_OP if_else_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_SUB_OP,2,$1,$3);
|
||||||
|
}
|
||||||
|
| assignable_value TOKEN_MOD_OP if_else_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_MOD_OP,2,$1,$3);
|
||||||
|
}
|
||||||
|
| assignable_value TOKEN_OR_OP if_else_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_OR_OP,2,$1,$3);
|
||||||
|
}
|
||||||
|
| assignable_value TOKEN_AND_OP if_else_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_AND_OP,2,$1,$3);
|
||||||
|
}
|
||||||
|
| assignable_value TOKEN_XOR_OP if_else_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_XOR_OP,2,$1,$3);
|
||||||
|
}
|
||||||
|
| assignable_value TOKEN_DIV_OP if_else_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_DIV_OP,2,$1,$3);
|
||||||
|
}
|
||||||
|
| assignable_value TOKEN_MUL_OP if_else_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_MUL_OP,2,$1,$3);
|
||||||
|
}
|
||||||
|
| assignable_value TOKEN_POW_OP if_else_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_POW_OP,2,$1,$3);
|
||||||
|
}
|
||||||
|
| STRING_IDENTIFIER '=' if_else_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createFunctionByName(context,"strcpy",2,$1,$3,NULL);
|
||||||
|
}
|
||||||
|
| STRING_IDENTIFIER TOKEN_ADD_OP if_else_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createFunctionByName(context,"strcat",2,$1,$3,NULL);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
unary_expr:
|
||||||
|
assignment
|
||||||
|
| '+' unary_expr
|
||||||
|
{
|
||||||
|
$$ = $2;
|
||||||
|
}
|
||||||
|
| '-' unary_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_UMINUS,1,$2,0);
|
||||||
|
}
|
||||||
|
| '!' unary_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_NOT,1,$2,0);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
pow_expr:
|
||||||
|
unary_expr
|
||||||
|
| pow_expr '^' unary_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_POW,2,$1,$3);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
mod_expr:
|
||||||
|
pow_expr
|
||||||
|
| mod_expr '%' pow_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_MOD,2,$1,$3);
|
||||||
|
}
|
||||||
|
| mod_expr TOKEN_SHL pow_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_SHL,2,$1,$3);
|
||||||
|
}
|
||||||
|
| mod_expr TOKEN_SHR pow_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_SHR,2,$1,$3);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
div_expr:
|
||||||
|
mod_expr
|
||||||
|
| div_expr '/' mod_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_DIVIDE,2,$1,$3);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
|
mul_expr:
|
||||||
|
div_expr
|
||||||
|
| mul_expr '*' div_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_MULTIPLY,2,$1,$3);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
|
sub_expr:
|
||||||
|
mul_expr
|
||||||
|
| sub_expr '-' mul_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_SUB,2,$1,$3);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
add_expr:
|
||||||
|
sub_expr
|
||||||
|
| add_expr '+' sub_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_ADD,2,$1,$3);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
andor_expr:
|
||||||
|
add_expr
|
||||||
|
| andor_expr '&' add_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_AND,2,$1,$3);
|
||||||
|
}
|
||||||
|
| andor_expr '|' add_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_OR,2,$1,$3);
|
||||||
|
}
|
||||||
|
| andor_expr '~' add_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_XOR,2,$1,$3);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
cmp_expr:
|
||||||
|
andor_expr
|
||||||
|
| cmp_expr '<' andor_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_LT,2,$1,$3);
|
||||||
|
}
|
||||||
|
| cmp_expr '>' andor_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_GT,2,$1,$3);
|
||||||
|
}
|
||||||
|
| cmp_expr TOKEN_LTE andor_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_LTE,2,$1,$3);
|
||||||
|
}
|
||||||
|
| cmp_expr TOKEN_GTE andor_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_GTE,2,$1,$3);
|
||||||
|
}
|
||||||
|
| cmp_expr TOKEN_EQ andor_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_EQ,2,$1,$3);
|
||||||
|
}
|
||||||
|
| cmp_expr TOKEN_EQ_EXACT andor_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_EQ_EXACT,2,$1,$3);
|
||||||
|
}
|
||||||
|
| cmp_expr TOKEN_NE andor_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_NE,2,$1,$3);
|
||||||
|
}
|
||||||
|
| cmp_expr TOKEN_NE_EXACT andor_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_NE_EXACT,2,$1,$3);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
logical_and_or_expr:
|
||||||
|
cmp_expr
|
||||||
|
| logical_and_or_expr TOKEN_LOGICAL_AND cmp_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_LOGICAL_AND,2,$1,$3);
|
||||||
|
}
|
||||||
|
| logical_and_or_expr TOKEN_LOGICAL_OR cmp_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_LOGICAL_OR,2,$1,$3);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
if_else_expr:
|
||||||
|
logical_and_or_expr
|
||||||
|
| logical_and_or_expr '?' if_else_expr ':' if_else_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createIfElse(context, $1, $3, $5);
|
||||||
|
}
|
||||||
|
| logical_and_or_expr '?' ':' if_else_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createIfElse(context, $1, 0, $4);
|
||||||
|
}
|
||||||
|
| logical_and_or_expr '?' if_else_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createIfElse(context, $1, $3, 0);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
|
expression:
|
||||||
|
if_else_expr
|
||||||
|
| expression ';' if_else_expr
|
||||||
|
{
|
||||||
|
$$ = nseel_createSimpleCompiledFunction(context,FN_JOIN_STATEMENTS,2,$1,$3);
|
||||||
|
}
|
||||||
|
| expression ';'
|
||||||
|
{
|
||||||
|
$$ = $1;
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
|
program:
|
||||||
|
expression
|
||||||
|
{
|
||||||
|
if (@1.first_line) { }
|
||||||
|
context->result = $1;
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
|
%%
|
||||||
71
oversampling/WDL/eel2/eel_atomic.h
Normal file
71
oversampling/WDL/eel2/eel_atomic.h
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
#ifndef __EEL_ATOMIC_H__
|
||||||
|
#define __EEL_ATOMIC_H__
|
||||||
|
|
||||||
|
// requires these to be defined
|
||||||
|
//#define EEL_ATOMIC_SET_SCOPE(opaque) WDL_Mutex *mutex = (opaque?&((effectProcessor *)opaque)->m_atomic_mutex:&atomic_mutex);
|
||||||
|
//#define EEL_ATOMIC_ENTER mutex->Enter()
|
||||||
|
//#define EEL_ATOMIC_LEAVE mutex->Leave()
|
||||||
|
|
||||||
|
static EEL_F NSEEL_CGEN_CALL atomic_setifeq(void *opaque, EEL_F *a, EEL_F *cmp, EEL_F *nd)
|
||||||
|
{
|
||||||
|
EEL_F ret;
|
||||||
|
EEL_ATOMIC_SET_SCOPE(opaque)
|
||||||
|
EEL_ATOMIC_ENTER;
|
||||||
|
ret = *a;
|
||||||
|
if (fabs(ret - *cmp) < NSEEL_CLOSEFACTOR) *a = *nd;
|
||||||
|
EEL_ATOMIC_LEAVE;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F NSEEL_CGEN_CALL atomic_exch(void *opaque, EEL_F *a, EEL_F *b)
|
||||||
|
{
|
||||||
|
EEL_F tmp;
|
||||||
|
EEL_ATOMIC_SET_SCOPE(opaque)
|
||||||
|
EEL_ATOMIC_ENTER;
|
||||||
|
tmp = *b;
|
||||||
|
*b = *a;
|
||||||
|
*a = tmp;
|
||||||
|
EEL_ATOMIC_LEAVE;
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F NSEEL_CGEN_CALL atomic_add(void *opaque, EEL_F *a, EEL_F *b)
|
||||||
|
{
|
||||||
|
EEL_F tmp;
|
||||||
|
EEL_ATOMIC_SET_SCOPE(opaque)
|
||||||
|
EEL_ATOMIC_ENTER;
|
||||||
|
tmp = (*a += *b);
|
||||||
|
EEL_ATOMIC_LEAVE;
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F NSEEL_CGEN_CALL atomic_set(void *opaque, EEL_F *a, EEL_F *b)
|
||||||
|
{
|
||||||
|
EEL_F tmp;
|
||||||
|
EEL_ATOMIC_SET_SCOPE(opaque)
|
||||||
|
EEL_ATOMIC_ENTER;
|
||||||
|
tmp = *a = *b;
|
||||||
|
EEL_ATOMIC_LEAVE;
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F NSEEL_CGEN_CALL atomic_get(void *opaque, EEL_F *a)
|
||||||
|
{
|
||||||
|
EEL_F tmp;
|
||||||
|
EEL_ATOMIC_SET_SCOPE(opaque)
|
||||||
|
EEL_ATOMIC_ENTER;
|
||||||
|
tmp = *a;
|
||||||
|
EEL_ATOMIC_LEAVE;
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void EEL_atomic_register()
|
||||||
|
{
|
||||||
|
NSEEL_addfunc_retval("atomic_setifequal",3, NSEEL_PProc_THIS, &atomic_setifeq);
|
||||||
|
NSEEL_addfunc_retval("atomic_exch",2, NSEEL_PProc_THIS, &atomic_exch);
|
||||||
|
NSEEL_addfunc_retval("atomic_add",2, NSEEL_PProc_THIS, &atomic_add);
|
||||||
|
NSEEL_addfunc_retval("atomic_set",2, NSEEL_PProc_THIS, &atomic_set);
|
||||||
|
NSEEL_addfunc_retval("atomic_get",1, NSEEL_PProc_THIS, &atomic_get);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
81
oversampling/WDL/eel2/eel_eval.h
Normal file
81
oversampling/WDL/eel2/eel_eval.h
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
#ifndef _EEL_EVAL_H_
|
||||||
|
#define _EEL_EVAL_H_
|
||||||
|
|
||||||
|
#ifndef EEL_EVAL_GET_CACHED
|
||||||
|
#define EEL_EVAL_GET_CACHED(str, ch) (NULL)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef EEL_EVAL_SET_CACHED
|
||||||
|
#define EEL_EVAL_SET_CACHED(sv, ch) { NSEEL_code_free(ch); free(sv); }
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef EEL_EVAL_SCOPE_ENTER
|
||||||
|
#define EEL_EVAL_SCOPE_ENTER 1
|
||||||
|
#define EEL_EVAL_SCOPE_LEAVE
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static EEL_F NSEEL_CGEN_CALL _eel_eval(void *opaque, EEL_F *s)
|
||||||
|
{
|
||||||
|
NSEEL_VMCTX r = EEL_EVAL_GET_VMCTX(opaque);
|
||||||
|
NSEEL_CODEHANDLE ch = NULL;
|
||||||
|
char *sv=NULL;
|
||||||
|
if (r)
|
||||||
|
{
|
||||||
|
EEL_STRING_MUTEXLOCK_SCOPE
|
||||||
|
const char *str=EEL_STRING_GET_FOR_INDEX(*s,NULL);
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
if (!str)
|
||||||
|
{
|
||||||
|
EEL_STRING_DEBUGOUT("eval() passed invalid string handle %f",*s);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
if (str && *str)
|
||||||
|
{
|
||||||
|
sv=EEL_EVAL_GET_CACHED(str,ch);
|
||||||
|
if (!sv) sv=strdup(str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (sv)
|
||||||
|
{
|
||||||
|
if (!ch) ch = NSEEL_code_compile(r,sv,0);
|
||||||
|
if (ch)
|
||||||
|
{
|
||||||
|
if (EEL_EVAL_SCOPE_ENTER)
|
||||||
|
{
|
||||||
|
NSEEL_code_execute(ch);
|
||||||
|
EEL_EVAL_SCOPE_LEAVE
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("eval() reentrancy limit reached");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
EEL_EVAL_SET_CACHED(sv,ch);
|
||||||
|
return 1.0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
const char *err=NSEEL_code_getcodeerror(r);
|
||||||
|
if (err) EEL_STRING_DEBUGOUT("eval() error: %s",err);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
free(sv);
|
||||||
|
}
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EEL_eval_register()
|
||||||
|
{
|
||||||
|
NSEEL_addfunc_retval("eval",1,NSEEL_PProc_THIS,&_eel_eval);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef EEL_WANT_DOCUMENTATION
|
||||||
|
static const char *eel_eval_function_reference =
|
||||||
|
"eval\t\"code\"\tExecutes code passed in. Code can use functions, but functions created in code can't be used elsewhere.\0"
|
||||||
|
;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
396
oversampling/WDL/eel2/eel_fft.h
Normal file
396
oversampling/WDL/eel2/eel_fft.h
Normal file
@@ -0,0 +1,396 @@
|
|||||||
|
#ifndef __EEL_FFT_H_
|
||||||
|
#define __EEL_FFT_H_
|
||||||
|
|
||||||
|
#include "../fft.h"
|
||||||
|
#if WDL_FFT_REALSIZE != EEL_F_SIZE
|
||||||
|
#error WDL_FFT_REALSIZE -- EEL_F_SIZE size mismatch
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef EEL_FFT_MINBITLEN
|
||||||
|
#define EEL_FFT_MINBITLEN 4
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef EEL_FFT_MAXBITLEN
|
||||||
|
#define EEL_FFT_MAXBITLEN 15
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef EEL_FFT_MINBITLEN_REORDER
|
||||||
|
#define EEL_FFT_MINBITLEN_REORDER (EEL_FFT_MINBITLEN-1)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//#define EEL_SUPER_FAST_FFT_REORDERING // quite a bit faster (50-100%) than "normal", but uses a 256kb lookup
|
||||||
|
//#define EEL_SLOW_FFT_REORDERING // 20%-80% slower than normal, alloca() use, no reason to ever use this
|
||||||
|
|
||||||
|
#ifdef EEL_SUPER_FAST_FFT_REORDERING
|
||||||
|
static int *fft_reorder_table_for_bitsize(int bitsz)
|
||||||
|
{
|
||||||
|
static int s_tab[ (2 << EEL_FFT_MAXBITLEN) + 24*(EEL_FFT_MAXBITLEN-EEL_FFT_MINBITLEN_REORDER+1) ]; // big 256kb table, ugh
|
||||||
|
if (bitsz<=EEL_FFT_MINBITLEN_REORDER) return s_tab;
|
||||||
|
return s_tab + (1<<bitsz) + (bitsz-EEL_FFT_MINBITLEN_REORDER) * 24;
|
||||||
|
}
|
||||||
|
static void fft_make_reorder_table(int bitsz, int *tab)
|
||||||
|
{
|
||||||
|
const int fft_sz=1<<bitsz;
|
||||||
|
char flag[1<<EEL_FFT_MAXBITLEN];
|
||||||
|
int x;
|
||||||
|
int *tabstart = tab;
|
||||||
|
memset(flag,0,fft_sz);
|
||||||
|
|
||||||
|
for (x=0;x<fft_sz;x++)
|
||||||
|
{
|
||||||
|
int fx;
|
||||||
|
if (!flag[x] && (fx=WDL_fft_permute(fft_sz,x))!=x)
|
||||||
|
{
|
||||||
|
flag[x]=1;
|
||||||
|
*tab++ = x;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
flag[fx]=1;
|
||||||
|
*tab++ = fx;
|
||||||
|
fx = WDL_fft_permute(fft_sz, fx);
|
||||||
|
}
|
||||||
|
while (fx != x);
|
||||||
|
*tab++ = 0; // delimit a run
|
||||||
|
}
|
||||||
|
else flag[x]=1;
|
||||||
|
}
|
||||||
|
*tab++ = 0; // doublenull terminated
|
||||||
|
}
|
||||||
|
|
||||||
|
static void fft_reorder_buffer(int bitsz, WDL_FFT_COMPLEX *data, int fwd)
|
||||||
|
{
|
||||||
|
const int *tab=fft_reorder_table_for_bitsize(bitsz);
|
||||||
|
if (!fwd)
|
||||||
|
{
|
||||||
|
while (*tab)
|
||||||
|
{
|
||||||
|
const int sidx=*tab++;
|
||||||
|
WDL_FFT_COMPLEX a=data[sidx];
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
WDL_FFT_COMPLEX ta;
|
||||||
|
const int idx=*tab++;
|
||||||
|
if (!idx) break;
|
||||||
|
ta=data[idx];
|
||||||
|
data[idx]=a;
|
||||||
|
a=ta;
|
||||||
|
}
|
||||||
|
data[sidx] = a;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
while (*tab)
|
||||||
|
{
|
||||||
|
const int sidx=*tab++;
|
||||||
|
int lidx = sidx;
|
||||||
|
const WDL_FFT_COMPLEX sta=data[lidx];
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
const int idx=*tab++;
|
||||||
|
if (!idx) break;
|
||||||
|
|
||||||
|
data[lidx]=data[idx];
|
||||||
|
lidx=idx;
|
||||||
|
}
|
||||||
|
data[lidx] = sta;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
#ifndef EEL_SLOW_FFT_REORDERING
|
||||||
|
// moderate speed mode, minus the big 256k table
|
||||||
|
|
||||||
|
static void fft_reorder_buffer(int bitsz, WDL_FFT_COMPLEX *data, int fwd)
|
||||||
|
{
|
||||||
|
// this is a good compromise, quite a bit faster than out of place reordering, but no separate 256kb lookup required
|
||||||
|
/*
|
||||||
|
these generated via:
|
||||||
|
static void fft_make_reorder_table(int bitsz)
|
||||||
|
{
|
||||||
|
int fft_sz=1<<bitsz,x;
|
||||||
|
char flag[65536]={0,};
|
||||||
|
printf("static const int tab%d[]={ ",fft_sz);
|
||||||
|
for (x=0;x<fft_sz;x++)
|
||||||
|
{
|
||||||
|
int fx;
|
||||||
|
if (!flag[x] && (fx=WDL_fft_permute(fft_sz,x))!=x)
|
||||||
|
{
|
||||||
|
printf("%d, ",x);
|
||||||
|
do { flag[fx]=1; fx = WDL_fft_permute(fft_sz, fx); } while (fx != x);
|
||||||
|
}
|
||||||
|
flag[x]=1;
|
||||||
|
}
|
||||||
|
printf(" 0 };\n");
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
static const int tab4_8_32[]={ 1, 0 };
|
||||||
|
static const int tab16[]={ 1, 3, 0 };
|
||||||
|
static const int tab64[]={ 1, 3, 9, 0 };
|
||||||
|
static const int tab128[]={ 1, 3, 4, 9, 14, 0 };
|
||||||
|
static const int tab256[]={ 1, 3, 6, 12, 13, 14, 19, 0 };
|
||||||
|
static const int tab512[]={ 1, 4, 7, 9, 18, 50, 115, 0 };
|
||||||
|
static const int tab1024[]={ 1, 3, 4, 25, 26, 77, 79, 0 };
|
||||||
|
static const int tab2048[]={ 1, 58, 59, 106, 135, 206, 210, 212, 0 };
|
||||||
|
static const int tab4096[]={ 1, 3, 12, 25, 54, 221, 313, 431, 453, 0 };
|
||||||
|
static const int tab8192[]={ 1, 12, 18, 26, 30, 100, 101, 106, 113, 144, 150, 237, 244, 247, 386, 468, 513, 1210, 4839, 0 };
|
||||||
|
static const int tab16384[]={ 1, 3, 6, 24, 1219, 0 };
|
||||||
|
static const int tab32768[]={ 1, 3, 4, 7, 13, 18, 31, 64, 113, 145, 203, 246, 594, 956, 1871, 2439, 4959, 19175, 0 };
|
||||||
|
const int *tab;
|
||||||
|
|
||||||
|
switch (bitsz)
|
||||||
|
{
|
||||||
|
case 1: return; // no reorder necessary
|
||||||
|
case 2:
|
||||||
|
case 3:
|
||||||
|
case 5: tab = tab4_8_32; break;
|
||||||
|
case 4: tab=tab16; break;
|
||||||
|
case 6: tab=tab64; break;
|
||||||
|
case 7: tab=tab128; break;
|
||||||
|
case 8: tab=tab256; break;
|
||||||
|
case 9: tab=tab512; break;
|
||||||
|
case 10: tab=tab1024; break;
|
||||||
|
case 11: tab=tab2048; break;
|
||||||
|
case 12: tab=tab4096; break;
|
||||||
|
case 13: tab=tab8192; break;
|
||||||
|
case 14: tab=tab16384; break;
|
||||||
|
case 15: tab=tab32768; break;
|
||||||
|
default: return; // no reorder possible
|
||||||
|
}
|
||||||
|
|
||||||
|
const int fft_sz=1<<bitsz;
|
||||||
|
const int *tb2 = WDL_fft_permute_tab(fft_sz);
|
||||||
|
if (!tb2) return; // ugh
|
||||||
|
|
||||||
|
if (!fwd)
|
||||||
|
{
|
||||||
|
while (*tab)
|
||||||
|
{
|
||||||
|
const int sidx=*tab++;
|
||||||
|
WDL_FFT_COMPLEX a=data[sidx];
|
||||||
|
int idx=sidx;
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
WDL_FFT_COMPLEX ta;
|
||||||
|
idx=tb2[idx];
|
||||||
|
if (idx==sidx) break;
|
||||||
|
ta=data[idx];
|
||||||
|
data[idx]=a;
|
||||||
|
a=ta;
|
||||||
|
}
|
||||||
|
data[sidx] = a;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
while (*tab)
|
||||||
|
{
|
||||||
|
const int sidx=*tab++;
|
||||||
|
int lidx = sidx;
|
||||||
|
const WDL_FFT_COMPLEX sta=data[lidx];
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
const int idx=tb2[lidx];
|
||||||
|
if (idx==sidx) break;
|
||||||
|
|
||||||
|
data[lidx]=data[idx];
|
||||||
|
lidx=idx;
|
||||||
|
}
|
||||||
|
data[lidx] = sta;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // not fast ,not slow, just right
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//#define TIMING
|
||||||
|
//#include "../timing.h"
|
||||||
|
|
||||||
|
// 0=fw, 1=iv, 2=fwreal, 3=ireal, 4=permutec, 6=permuter
|
||||||
|
// low bit: is inverse
|
||||||
|
// second bit: was isreal, but no longer used
|
||||||
|
// third bit: is permute
|
||||||
|
static void FFT(int sizebits, EEL_F *data, int dir)
|
||||||
|
{
|
||||||
|
if (dir >= 4 && dir < 8)
|
||||||
|
{
|
||||||
|
if (dir == 4 || dir == 5)
|
||||||
|
{
|
||||||
|
//timingEnter(0);
|
||||||
|
#if defined(EEL_SUPER_FAST_FFT_REORDERING) || !defined(EEL_SLOW_FFT_REORDERING)
|
||||||
|
fft_reorder_buffer(sizebits,(WDL_FFT_COMPLEX*)data,dir==4);
|
||||||
|
#else
|
||||||
|
// old blech
|
||||||
|
const int flen=1<<sizebits;
|
||||||
|
int x;
|
||||||
|
EEL_F *tmp=(EEL_F*)alloca(sizeof(EEL_F)*flen*2);
|
||||||
|
const int flen2=flen+flen;
|
||||||
|
// reorder entries, now
|
||||||
|
memcpy(tmp,data,sizeof(EEL_F)*flen*2);
|
||||||
|
|
||||||
|
if (dir == 4)
|
||||||
|
{
|
||||||
|
for (x = 0; x < flen2; x += 2)
|
||||||
|
{
|
||||||
|
int y=WDL_fft_permute(flen,x/2)*2;
|
||||||
|
data[x]=tmp[y];
|
||||||
|
data[x+1]=tmp[y+1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (x = 0; x < flen2; x += 2)
|
||||||
|
{
|
||||||
|
int y=WDL_fft_permute(flen,x/2)*2;
|
||||||
|
data[y]=tmp[x];
|
||||||
|
data[y+1]=tmp[x+1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
//timingLeave(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (dir >= 0 && dir < 2)
|
||||||
|
{
|
||||||
|
WDL_fft((WDL_FFT_COMPLEX*)data,1<<sizebits,dir&1);
|
||||||
|
}
|
||||||
|
else if (dir >= 2 && dir < 4)
|
||||||
|
{
|
||||||
|
WDL_real_fft((WDL_FFT_REAL*)data,1<<sizebits,dir&1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static EEL_F * fft_func(int dir, EEL_F **blocks, EEL_F *start, EEL_F *length)
|
||||||
|
{
|
||||||
|
const int offs = (int)(*start + 0.0001);
|
||||||
|
const int itemSizeShift=(dir&2)?0:1;
|
||||||
|
int l=(int)(*length + 0.0001);
|
||||||
|
int bitl=0;
|
||||||
|
int ilen;
|
||||||
|
EEL_F *ptr;
|
||||||
|
while (l>1 && bitl < EEL_FFT_MAXBITLEN)
|
||||||
|
{
|
||||||
|
bitl++;
|
||||||
|
l>>=1;
|
||||||
|
}
|
||||||
|
if (bitl < ((dir&4) ? EEL_FFT_MINBITLEN_REORDER : EEL_FFT_MINBITLEN)) // smallest FFT is 16 item, smallest reorder is 8 item
|
||||||
|
{
|
||||||
|
return start;
|
||||||
|
}
|
||||||
|
ilen=1<<bitl;
|
||||||
|
|
||||||
|
|
||||||
|
// check to make sure we don't cross a boundary
|
||||||
|
if (offs/NSEEL_RAM_ITEMSPERBLOCK != (offs + (ilen<<itemSizeShift) - 1)/NSEEL_RAM_ITEMSPERBLOCK)
|
||||||
|
{
|
||||||
|
return start;
|
||||||
|
}
|
||||||
|
|
||||||
|
ptr=__NSEEL_RAMAlloc(blocks,offs);
|
||||||
|
if (!ptr || ptr==&nseel_ramalloc_onfail)
|
||||||
|
{
|
||||||
|
return start;
|
||||||
|
}
|
||||||
|
|
||||||
|
FFT(bitl,ptr,dir);
|
||||||
|
|
||||||
|
return start;
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F * NSEEL_CGEN_CALL eel_fft(EEL_F **blocks, EEL_F *start, EEL_F *length)
|
||||||
|
{
|
||||||
|
return fft_func(0,blocks,start,length);
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F * NSEEL_CGEN_CALL eel_ifft(EEL_F **blocks, EEL_F *start, EEL_F *length)
|
||||||
|
{
|
||||||
|
return fft_func(1,blocks,start,length);
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F * NSEEL_CGEN_CALL eel_fft_real(EEL_F **blocks, EEL_F *start, EEL_F *length)
|
||||||
|
{
|
||||||
|
return fft_func(2,blocks,start,length);
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F * NSEEL_CGEN_CALL eel_ifft_real(EEL_F **blocks, EEL_F *start, EEL_F *length)
|
||||||
|
{
|
||||||
|
return fft_func(3,blocks,start,length);
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F * NSEEL_CGEN_CALL eel_fft_permute(EEL_F **blocks, EEL_F *start, EEL_F *length)
|
||||||
|
{
|
||||||
|
return fft_func(4,blocks,start,length);
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F * NSEEL_CGEN_CALL eel_ifft_permute(EEL_F **blocks, EEL_F *start, EEL_F *length)
|
||||||
|
{
|
||||||
|
return fft_func(5,blocks,start,length);
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F * NSEEL_CGEN_CALL eel_convolve_c(EEL_F **blocks,EEL_F *dest, EEL_F *src, EEL_F *lenptr)
|
||||||
|
{
|
||||||
|
const int dest_offs = (int)(*dest + 0.0001);
|
||||||
|
const int src_offs = (int)(*src + 0.0001);
|
||||||
|
const int len = ((int)(*lenptr + 0.0001)) * 2;
|
||||||
|
EEL_F *srcptr,*destptr;
|
||||||
|
|
||||||
|
if (len < 1 || len > NSEEL_RAM_ITEMSPERBLOCK || dest_offs < 0 || src_offs < 0 ||
|
||||||
|
dest_offs >= NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK || src_offs >= NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK) return dest;
|
||||||
|
if ((dest_offs&(NSEEL_RAM_ITEMSPERBLOCK-1)) + len > NSEEL_RAM_ITEMSPERBLOCK) return dest;
|
||||||
|
if ((src_offs&(NSEEL_RAM_ITEMSPERBLOCK-1)) + len > NSEEL_RAM_ITEMSPERBLOCK) return dest;
|
||||||
|
|
||||||
|
srcptr = __NSEEL_RAMAlloc(blocks,src_offs);
|
||||||
|
if (!srcptr || srcptr==&nseel_ramalloc_onfail) return dest;
|
||||||
|
destptr = __NSEEL_RAMAlloc(blocks,dest_offs);
|
||||||
|
if (!destptr || destptr==&nseel_ramalloc_onfail) return dest;
|
||||||
|
|
||||||
|
|
||||||
|
WDL_fft_complexmul((WDL_FFT_COMPLEX*)destptr,(WDL_FFT_COMPLEX*)srcptr,(len/2)&~1);
|
||||||
|
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EEL_fft_register()
|
||||||
|
{
|
||||||
|
WDL_fft_init();
|
||||||
|
#if defined(EEL_SUPER_FAST_FFT_REORDERING)
|
||||||
|
if (!fft_reorder_table_for_bitsize(EEL_FFT_MINBITLEN_REORDER)[0])
|
||||||
|
{
|
||||||
|
int x;
|
||||||
|
for (x=EEL_FFT_MINBITLEN_REORDER;x<=EEL_FFT_MAXBITLEN;x++) fft_make_reorder_table(x,fft_reorder_table_for_bitsize(x));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
NSEEL_addfunc_retptr("convolve_c",3,NSEEL_PProc_RAM,&eel_convolve_c);
|
||||||
|
NSEEL_addfunc_retptr("fft",2,NSEEL_PProc_RAM,&eel_fft);
|
||||||
|
NSEEL_addfunc_retptr("ifft",2,NSEEL_PProc_RAM,&eel_ifft);
|
||||||
|
NSEEL_addfunc_retptr("fft_real",2,NSEEL_PProc_RAM,&eel_fft_real);
|
||||||
|
NSEEL_addfunc_retptr("ifft_real",2,NSEEL_PProc_RAM,&eel_ifft_real);
|
||||||
|
NSEEL_addfunc_retptr("fft_permute",2,NSEEL_PProc_RAM,&eel_fft_permute);
|
||||||
|
NSEEL_addfunc_retptr("fft_ipermute",2,NSEEL_PProc_RAM,&eel_ifft_permute);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef EEL_WANT_DOCUMENTATION
|
||||||
|
static const char *eel_fft_function_reference =
|
||||||
|
"convolve_c\tdest,src,size\tMultiplies each of size complex pairs in dest by the complex pairs in src. Often used for convolution.\0"
|
||||||
|
"fft\tbuffer,size\tPerforms a FFT on the data in the local memory buffer at the offset specified by the first parameter. The size of the FFT is specified "
|
||||||
|
"by the second parameter, which must be 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, or 32768. The outputs are permuted, so if "
|
||||||
|
"you plan to use them in-order, call fft_permute(buffer, size) before and fft_ipermute(buffer,size) after your in-order use. Your inputs or "
|
||||||
|
"outputs will need to be scaled down by 1/size, if used.\n"
|
||||||
|
"Note that fft()/ifft() require real / imaginary input pairs, so a 256 point FFT actually works with 512 items.\n"
|
||||||
|
"Note that fft()/ifft() must NOT cross a 65,536 item boundary, so be sure to specify the offset accordingly.\0"
|
||||||
|
"ifft\tbuffer,size\tPerform an inverse FFT. For more information see fft().\0"
|
||||||
|
"fft_real\tbuffer,size\tPerforms an FFT, but takes size input samples and produces size/2 complex output pairs. Usually used along with fft_permute(size/2). Inputs/outputs will need to be scaled by 0.5/size.\0"
|
||||||
|
"ifft_real\tbuffer,size\tPerforms an inverse FFT, but takes size/2 complex input pairs and produces size real output values. Usually used along with fft_ipermute(size/2).\0"
|
||||||
|
"fft_permute\tbuffer,size\tPermute the output of fft() to have bands in-order. See fft() for more information.\0"
|
||||||
|
"fft_ipermute\tbuffer,size\tPermute the input for ifft(), taking bands from in-order to the order ifft() requires. See fft() for more information.\0"
|
||||||
|
;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
262
oversampling/WDL/eel2/eel_files.h
Normal file
262
oversampling/WDL/eel2/eel_files.h
Normal file
@@ -0,0 +1,262 @@
|
|||||||
|
#ifndef __EEL_FILES_H__
|
||||||
|
#define __EEL_FILES_H__
|
||||||
|
|
||||||
|
// should include eel_strings.h before this, probably
|
||||||
|
//#define EEL_FILE_OPEN(fn,mode) ((instance)opaque)->OpenFile(fn,mode)
|
||||||
|
//#define EEL_FILE_GETFP(fp) ((instance)opaque)->GetFileFP(fp)
|
||||||
|
//#define EEL_FILE_CLOSE(fpindex) ((instance)opaque)->CloseFile(fpindex)
|
||||||
|
|
||||||
|
static EEL_F NSEEL_CGEN_CALL _eel_fopen(void *opaque, EEL_F *fn_index, EEL_F *mode_index)
|
||||||
|
{
|
||||||
|
EEL_STRING_MUTEXLOCK_SCOPE
|
||||||
|
const char *fn = EEL_STRING_GET_FOR_INDEX(*fn_index,NULL);
|
||||||
|
const char *mode = EEL_STRING_GET_FOR_INDEX(*mode_index,NULL);
|
||||||
|
if (!fn || !mode) return 0;
|
||||||
|
return (EEL_F) EEL_FILE_OPEN(fn,mode);
|
||||||
|
}
|
||||||
|
static EEL_F NSEEL_CGEN_CALL _eel_fclose(void *opaque, EEL_F *fpp)
|
||||||
|
{
|
||||||
|
EEL_F ret=EEL_FILE_CLOSE((int)*fpp);
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
if (ret < 0) EEL_STRING_DEBUGOUT("fclose(): file handle %f not valid",*fpp);
|
||||||
|
#endif
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
static EEL_F NSEEL_CGEN_CALL _eel_fgetc(void *opaque, EEL_F *fpp)
|
||||||
|
{
|
||||||
|
EEL_STRING_MUTEXLOCK_SCOPE
|
||||||
|
FILE *fp = EEL_FILE_GETFP((int)*fpp);
|
||||||
|
if (fp) return (EEL_F)fgetc(fp);
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("fgetc(): file handle %f not valid",*fpp);
|
||||||
|
#endif
|
||||||
|
return -1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F NSEEL_CGEN_CALL _eel_ftell(void *opaque, EEL_F *fpp)
|
||||||
|
{
|
||||||
|
EEL_STRING_MUTEXLOCK_SCOPE
|
||||||
|
FILE *fp = EEL_FILE_GETFP((int)*fpp);
|
||||||
|
if (fp) return (EEL_F)ftell(fp);
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("ftell(): file handle %f not valid",*fpp);
|
||||||
|
#endif
|
||||||
|
return -1.0;
|
||||||
|
}
|
||||||
|
static EEL_F NSEEL_CGEN_CALL _eel_fflush(void *opaque, EEL_F *fpp)
|
||||||
|
{
|
||||||
|
EEL_STRING_MUTEXLOCK_SCOPE
|
||||||
|
FILE *fp = EEL_FILE_GETFP((int)*fpp);
|
||||||
|
if (fp) { fflush(fp); return 0.0; }
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("fflush(): file handle %f not valid",*fpp);
|
||||||
|
#endif
|
||||||
|
return -1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F NSEEL_CGEN_CALL _eel_feof(void *opaque, EEL_F *fpp)
|
||||||
|
{
|
||||||
|
EEL_STRING_MUTEXLOCK_SCOPE
|
||||||
|
FILE *fp = EEL_FILE_GETFP((int)*fpp);
|
||||||
|
if (fp) return feof(fp) ? 1.0 : 0.0;
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("feof(): file handle %f not valid",*fpp);
|
||||||
|
#endif
|
||||||
|
return -1.0;
|
||||||
|
}
|
||||||
|
static EEL_F NSEEL_CGEN_CALL _eel_fseek(void *opaque, EEL_F *fpp, EEL_F *offset, EEL_F *wh)
|
||||||
|
{
|
||||||
|
EEL_STRING_MUTEXLOCK_SCOPE
|
||||||
|
FILE *fp = EEL_FILE_GETFP((int)*fpp);
|
||||||
|
if (fp) return fseek(fp, (int) *offset, *wh<0 ? SEEK_SET : *wh > 0 ? SEEK_END : SEEK_CUR);
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("fseek(): file handle %f not valid",*fpp);
|
||||||
|
#endif
|
||||||
|
return -1.0;
|
||||||
|
}
|
||||||
|
static EEL_F NSEEL_CGEN_CALL _eel_fgets(void *opaque, EEL_F *fpp, EEL_F *strOut)
|
||||||
|
{
|
||||||
|
EEL_STRING_MUTEXLOCK_SCOPE
|
||||||
|
EEL_STRING_STORAGECLASS *wr=NULL;
|
||||||
|
EEL_STRING_GET_FOR_WRITE(*strOut, &wr);
|
||||||
|
|
||||||
|
FILE *fp = EEL_FILE_GETFP((int)*fpp);
|
||||||
|
if (!fp)
|
||||||
|
{
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("fgets(): file handle %f not valid",*fpp);
|
||||||
|
#endif
|
||||||
|
if (wr) wr->Set("");
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
char buf[16384];
|
||||||
|
buf[0]=0;
|
||||||
|
fgets(buf,sizeof(buf),fp);
|
||||||
|
if (wr)
|
||||||
|
{
|
||||||
|
wr->Set(buf);
|
||||||
|
return (EEL_F)wr->GetLength();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("fgets: bad destination specifier passed %f, throwing away %d bytes",*strOut, (int)strlen(buf));
|
||||||
|
#endif
|
||||||
|
return (int)strlen(buf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static EEL_F NSEEL_CGEN_CALL _eel_fread(void *opaque, EEL_F *fpp, EEL_F *strOut, EEL_F *flen)
|
||||||
|
{
|
||||||
|
int use_len = (int) *flen;
|
||||||
|
if (use_len < 1) return 0.0;
|
||||||
|
|
||||||
|
EEL_STRING_MUTEXLOCK_SCOPE
|
||||||
|
EEL_STRING_STORAGECLASS *wr=NULL;
|
||||||
|
EEL_STRING_GET_FOR_WRITE(*strOut, &wr);
|
||||||
|
if (!wr)
|
||||||
|
{
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("fread: bad destination specifier passed %f, not reading %d bytes",*strOut, use_len);
|
||||||
|
#endif
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE *fp = EEL_FILE_GETFP((int)*fpp);
|
||||||
|
if (!fp)
|
||||||
|
{
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("fread(): file handle %f not valid",*fpp);
|
||||||
|
#endif
|
||||||
|
if (wr) wr->Set("");
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
wr->SetLen(use_len);
|
||||||
|
if (wr->GetLength() != use_len)
|
||||||
|
{
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("fread: error allocating storage for read of %d bytes", use_len);
|
||||||
|
#endif
|
||||||
|
return -1.0;
|
||||||
|
}
|
||||||
|
use_len = (int)fread((char *)wr->Get(),1,use_len,fp);
|
||||||
|
wr->SetLen(use_len > 0 ? use_len : 0, true);
|
||||||
|
return (EEL_F) use_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F NSEEL_CGEN_CALL _eel_fwrite(void *opaque, EEL_F *fpp, EEL_F *strOut, EEL_F *flen)
|
||||||
|
{
|
||||||
|
EEL_STRING_MUTEXLOCK_SCOPE
|
||||||
|
int use_len = (int) *flen;
|
||||||
|
|
||||||
|
EEL_STRING_STORAGECLASS *wr=NULL;
|
||||||
|
const char *str=EEL_STRING_GET_FOR_INDEX(*strOut, &wr);
|
||||||
|
if (!wr && !str)
|
||||||
|
{
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("fwrite: bad source specifier passed %f, not writing %d bytes",*strOut, use_len);
|
||||||
|
#endif
|
||||||
|
return -1.0;
|
||||||
|
}
|
||||||
|
if (!wr)
|
||||||
|
{
|
||||||
|
const int ssl = (int)strlen(str);
|
||||||
|
if (use_len < 1 || use_len > ssl) use_len = ssl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (use_len < 1 || use_len > wr->GetLength()) use_len = wr->GetLength();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (use_len < 1) return 0.0;
|
||||||
|
|
||||||
|
FILE *fp = EEL_FILE_GETFP((int)*fpp);
|
||||||
|
if (!fp)
|
||||||
|
{
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("fwrite(): file handle %f not valid",*fpp);
|
||||||
|
#endif
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (EEL_F) fwrite(str,1,use_len,fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F NSEEL_CGEN_CALL _eel_fprintf(void *opaque, INT_PTR nparam, EEL_F **parm)
|
||||||
|
{
|
||||||
|
if (opaque && nparam > 1)
|
||||||
|
{
|
||||||
|
EEL_STRING_MUTEXLOCK_SCOPE
|
||||||
|
FILE *fp = EEL_FILE_GETFP((int)*(parm[0]));
|
||||||
|
if (!fp)
|
||||||
|
{
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("fprintf(): file handle %f not valid",parm[0][0]);
|
||||||
|
#endif
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
EEL_STRING_STORAGECLASS *wr_src=NULL;
|
||||||
|
const char *fmt = EEL_STRING_GET_FOR_INDEX(*(parm[1]),&wr_src);
|
||||||
|
if (fmt)
|
||||||
|
{
|
||||||
|
char buf[16384];
|
||||||
|
const int len = eel_format_strings(opaque,fmt,wr_src?(fmt+wr_src->GetLength()) : NULL, buf,(int)sizeof(buf),(int)nparam-2,parm+2);
|
||||||
|
|
||||||
|
if (len >= 0)
|
||||||
|
{
|
||||||
|
return (EEL_F) fwrite(buf,1,len,fp);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("fprintf: bad format string %s",fmt);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("fprintf: bad format specifier passed %f",*(parm[1]));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EEL_file_register()
|
||||||
|
{
|
||||||
|
NSEEL_addfunc_retval("fopen",2,NSEEL_PProc_THIS,&_eel_fopen);
|
||||||
|
|
||||||
|
NSEEL_addfunc_retval("fread",3,NSEEL_PProc_THIS,&_eel_fread);
|
||||||
|
NSEEL_addfunc_retval("fgets",2,NSEEL_PProc_THIS,&_eel_fgets);
|
||||||
|
NSEEL_addfunc_retval("fgetc",1,NSEEL_PProc_THIS,&_eel_fgetc);
|
||||||
|
|
||||||
|
NSEEL_addfunc_retval("fwrite",3,NSEEL_PProc_THIS,&_eel_fwrite);
|
||||||
|
NSEEL_addfunc_varparm("fprintf",2,NSEEL_PProc_THIS,&_eel_fprintf);
|
||||||
|
|
||||||
|
NSEEL_addfunc_retval("fseek",3,NSEEL_PProc_THIS,&_eel_fseek);
|
||||||
|
NSEEL_addfunc_retval("ftell",1,NSEEL_PProc_THIS,&_eel_ftell);
|
||||||
|
NSEEL_addfunc_retval("feof",1,NSEEL_PProc_THIS,&_eel_feof);
|
||||||
|
NSEEL_addfunc_retval("fflush",1,NSEEL_PProc_THIS,&_eel_fflush);
|
||||||
|
NSEEL_addfunc_retval("fclose",1,NSEEL_PProc_THIS,&_eel_fclose);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef EEL_WANT_DOCUMENTATION
|
||||||
|
static const char *eel_file_function_reference =
|
||||||
|
"fopen\t\"fn\",\"mode\"\tOpens a file \"fn\" with mode \"mode\". For read, use \"r\" or \"rb\", write \"w\" or \"wb\". Returns a positive integer on success.\0"
|
||||||
|
"fclose\tfp\tCloses a file previously opened with fopen().\0"
|
||||||
|
"fread\tfp,#str,length\tReads from file fp into #str, up to length bytes. Returns actual length read, or negative if error.\0"
|
||||||
|
"fgets\tfp,#str\tReads a line from file fp into #str. Returns length of #str read.\0"
|
||||||
|
"fgetc\tfp\tReads a character from file fp, returns -1 if EOF.\0"
|
||||||
|
"fwrite\tfp,#str,len\tWrites up to len characters of #str to file fp. If len is less than 1, the full contents of #str will be written. Returns the number of bytes written to file.\0"
|
||||||
|
"fprintf\tfp,\"format\"[,...]\tFormats a string and writes it to file fp. For more information on format specifiers, see sprintf(). Returns bytes written to file.\0"
|
||||||
|
"fseek\tfp,offset,whence\tSeeks file fp, offset bytes from whence reference. Whence negative specifies start of file, positive whence specifies end of file, and zero whence specifies current file position.\0"
|
||||||
|
"ftell\tfp\tRetunrs the current file position.\0"
|
||||||
|
"feof\tfp\tReturns nonzero if the file fp is at the end of file.\0"
|
||||||
|
"fflush\tfp\tIf file fp is open for writing, flushes out any buffered data to disk.\0"
|
||||||
|
;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
104
oversampling/WDL/eel2/eel_import.h
Normal file
104
oversampling/WDL/eel2/eel_import.h
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
/*******************************************************************************************
|
||||||
|
* imported EEL
|
||||||
|
******************************************************************************************/
|
||||||
|
|
||||||
|
void (*NSEEL_addfunc_ret_type)(const char *name, int np, int ret_type, NSEEL_PPPROC pproc, void *fptr, eel_function_table *destination); // ret_type=-1 for bool, 1 for value, 0 for ptr
|
||||||
|
void (*NSEEL_addfunc_varparm_ex)(const char *name, int min_np, int want_exact, NSEEL_PPPROC pproc, EEL_F (NSEEL_CGEN_CALL *fptr)(void *, INT_PTR, EEL_F **), eel_function_table *destination);
|
||||||
|
|
||||||
|
|
||||||
|
NSEEL_VMCTX (*NSEEL_VM_alloc)(); // return a handle
|
||||||
|
void (*NSEEL_VM_SetGRAM)(NSEEL_VMCTX, void **);
|
||||||
|
void (*NSEEL_VM_free)(NSEEL_VMCTX ctx); // free when done with a VM and ALL of its code have been freed, as well
|
||||||
|
void (*NSEEL_VM_SetFunctionTable)(NSEEL_VMCTX, eel_function_table *tab); // use NULL to use default (global) table
|
||||||
|
EEL_F *(*NSEEL_VM_regvar)(NSEEL_VMCTX ctx, const char *name); // register a variable (before compilation)
|
||||||
|
|
||||||
|
void (*NSEEL_VM_SetCustomFuncThis)(NSEEL_VMCTX ctx, void *thisptr);
|
||||||
|
NSEEL_CODEHANDLE (*NSEEL_code_compile_ex)(NSEEL_VMCTX ctx, const char *code, int lineoffs, int flags);
|
||||||
|
void (*NSEEL_VM_set_var_resolver)(NSEEL_VMCTX ctx, EEL_F *(*res)(void *userctx, const char *name), void *userctx);
|
||||||
|
char *(*NSEEL_code_getcodeerror)(NSEEL_VMCTX ctx);
|
||||||
|
void (*NSEEL_code_execute)(NSEEL_CODEHANDLE code);
|
||||||
|
void (*NSEEL_code_free)(NSEEL_CODEHANDLE code);
|
||||||
|
EEL_F *(*nseel_int_register_var)(compileContext *ctx, const char *name, int isReg, const char **namePtrOut);
|
||||||
|
void (*NSEEL_VM_enumallvars)(NSEEL_VMCTX ctx, int (*func)(const char *name, EEL_F *val, void *ctx), void *userctx);
|
||||||
|
EEL_F *(*NSEEL_VM_getramptr)(NSEEL_VMCTX ctx, unsigned int offs, int *validAmt);
|
||||||
|
void ** (*eel_gmem_attach)(const char *nm, bool is_alloc);
|
||||||
|
void (*eel_fft_register)(eel_function_table*);
|
||||||
|
|
||||||
|
struct eelStringSegmentRec {
|
||||||
|
struct eelStringSegmentRec *_next;
|
||||||
|
const char *str_start; // escaped characters, including opening/trailing characters
|
||||||
|
int str_len;
|
||||||
|
};
|
||||||
|
void (*NSEEL_VM_SetStringFunc)(NSEEL_VMCTX ctx,
|
||||||
|
EEL_F (*onString)(void *caller_this, struct eelStringSegmentRec *list),
|
||||||
|
EEL_F (*onNamedString)(void *caller_this, const char *name));
|
||||||
|
|
||||||
|
// call with NULL to calculate size, or non-null to generate to buffer (returning size used -- will not null terminate, caller responsibility)
|
||||||
|
int (*nseel_stringsegments_tobuf)(char *bufOut, int bufout_sz, struct eelStringSegmentRec *list);
|
||||||
|
|
||||||
|
void *(*NSEEL_PProc_RAM)(void *data, int data_size, struct _compileContext *ctx);
|
||||||
|
void *(*NSEEL_PProc_THIS)(void *data, int data_size, struct _compileContext *ctx);
|
||||||
|
|
||||||
|
void (*eel_enterfp)(int s[2]);
|
||||||
|
void (*eel_leavefp)(int s[2]);
|
||||||
|
|
||||||
|
|
||||||
|
eel_function_table g_eel_function_table;
|
||||||
|
#define NSEEL_ADDFUNC_DESTINATION (&g_eel_function_table)
|
||||||
|
|
||||||
|
//
|
||||||
|
// adds a function that returns a value (EEL_F)
|
||||||
|
#define NSEEL_addfunc_retval(name,np,pproc,fptr) \
|
||||||
|
NSEEL_addfunc_ret_type(name,np,1,pproc,(void *)(fptr),NSEEL_ADDFUNC_DESTINATION)
|
||||||
|
|
||||||
|
// adds a function that returns a pointer (EEL_F*)
|
||||||
|
#define NSEEL_addfunc_retptr(name,np,pproc,fptr) \
|
||||||
|
NSEEL_addfunc_ret_type(name,np,0,pproc,(void *)(fptr),NSEEL_ADDFUNC_DESTINATION)
|
||||||
|
|
||||||
|
// adds a void or bool function
|
||||||
|
#define NSEEL_addfunc_retbool(name,np,pproc,fptr) \
|
||||||
|
NSEEL_addfunc_ret_type(name,np,-1,pproc,(void *)(fptr),NSEEL_ADDFUNC_DESTINATION)
|
||||||
|
|
||||||
|
// adds a function that takes min_np or more parameters (func sig needs to be EEL_F func(void *ctx, INT_PTR np, EEL_F **parms)
|
||||||
|
#define NSEEL_addfunc_varparm(name, min_np, pproc, fptr) \
|
||||||
|
NSEEL_addfunc_varparm_ex(name,min_np,0,pproc,fptr,NSEEL_ADDFUNC_DESTINATION)
|
||||||
|
|
||||||
|
// adds a function that takes np parameters via func: sig needs to be EEL_F func(void *ctx, INT_PTR np, EEL_F **parms)
|
||||||
|
#define NSEEL_addfunc_exparms(name, np, pproc, fptr) \
|
||||||
|
NSEEL_addfunc_varparm_ex(name,np,1,pproc,fptr,NSEEL_ADDFUNC_DESTINATION)
|
||||||
|
|
||||||
|
class eel_string_context_state;
|
||||||
|
|
||||||
|
#define __NS_EELINT_H__
|
||||||
|
|
||||||
|
#define EEL_IMPORT_ALL(IMPORT_FUNC) \
|
||||||
|
IMPORT_FUNC(NSEEL_addfunc_ret_type) \
|
||||||
|
IMPORT_FUNC(NSEEL_addfunc_varparm_ex) \
|
||||||
|
IMPORT_FUNC(NSEEL_VM_free) \
|
||||||
|
IMPORT_FUNC(NSEEL_VM_SetFunctionTable) \
|
||||||
|
IMPORT_FUNC(NSEEL_VM_regvar) \
|
||||||
|
IMPORT_FUNC(NSEEL_VM_SetCustomFuncThis) \
|
||||||
|
IMPORT_FUNC(NSEEL_code_compile_ex) \
|
||||||
|
IMPORT_FUNC(NSEEL_code_getcodeerror) \
|
||||||
|
IMPORT_FUNC(NSEEL_code_execute) \
|
||||||
|
IMPORT_FUNC(NSEEL_code_free) \
|
||||||
|
IMPORT_FUNC(NSEEL_PProc_THIS) \
|
||||||
|
IMPORT_FUNC(NSEEL_PProc_RAM) \
|
||||||
|
IMPORT_FUNC(NSEEL_VM_SetStringFunc) \
|
||||||
|
IMPORT_FUNC(NSEEL_VM_enumallvars) \
|
||||||
|
IMPORT_FUNC(NSEEL_VM_getramptr) \
|
||||||
|
IMPORT_FUNC(NSEEL_VM_SetGRAM) \
|
||||||
|
IMPORT_FUNC(eel_gmem_attach) \
|
||||||
|
IMPORT_FUNC(eel_fft_register) \
|
||||||
|
IMPORT_FUNC(nseel_stringsegments_tobuf) \
|
||||||
|
IMPORT_FUNC(nseel_int_register_var) \
|
||||||
|
IMPORT_FUNC(eel_leavefp) \
|
||||||
|
IMPORT_FUNC(eel_enterfp) \
|
||||||
|
IMPORT_FUNC(NSEEL_VM_set_var_resolver) \
|
||||||
|
IMPORT_FUNC(NSEEL_VM_alloc) /* keep NSEEL_VM_alloc last */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************************
|
||||||
|
* END of imported EEL
|
||||||
|
******************************************************************************************/
|
||||||
3223
oversampling/WDL/eel2/eel_lice.h
Normal file
3223
oversampling/WDL/eel2/eel_lice.h
Normal file
File diff suppressed because it is too large
Load Diff
782
oversampling/WDL/eel2/eel_mdct.h
Normal file
782
oversampling/WDL/eel2/eel_mdct.h
Normal file
@@ -0,0 +1,782 @@
|
|||||||
|
#ifndef _EEL_MDCT_H_
|
||||||
|
#define _EEL_MDCT_H_
|
||||||
|
|
||||||
|
|
||||||
|
#include "ns-eel-int.h"
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <malloc.h>
|
||||||
|
#endif
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#define EEL_DCT_MINBITLEN 5
|
||||||
|
#define EEL_DCT_MAXBITLEN 12
|
||||||
|
|
||||||
|
|
||||||
|
#define PI 3.1415926535897932384626433832795
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int n;
|
||||||
|
int log2n;
|
||||||
|
EEL_F *trig;
|
||||||
|
int *bitrev;
|
||||||
|
EEL_F scale;
|
||||||
|
EEL_F *window;
|
||||||
|
} mdct_lookup;
|
||||||
|
|
||||||
|
static void mdct(EEL_F *in, EEL_F *out, int len)
|
||||||
|
{
|
||||||
|
int k;
|
||||||
|
EEL_F pioverlen = PI * 0.5 / (EEL_F)len;
|
||||||
|
for (k = 0; k < len / 2; k ++)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
EEL_F d = 0.0;
|
||||||
|
for (i = 0; i < len; i ++)
|
||||||
|
{
|
||||||
|
d += in[i] * cos(pioverlen * (2.0 * i + 1.0 + len * 0.5) * (2.0 * k + 1.0));
|
||||||
|
}
|
||||||
|
out[k] = (EEL_F)d;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void imdct(EEL_F *in, EEL_F *out, int len)
|
||||||
|
{
|
||||||
|
int k;
|
||||||
|
EEL_F fourovern = 4.0 / (EEL_F)len;
|
||||||
|
EEL_F pioverlen = PI * 0.5 / (EEL_F)len;
|
||||||
|
for (k = 0; k < len; k ++)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
EEL_F d = 0.0;
|
||||||
|
for (i = 0; i < len / 2; i ++)
|
||||||
|
{
|
||||||
|
d += in[i] * cos(pioverlen * (2.0 * k + 1.0 + len * 0.5) * (2 * i + 1.0));
|
||||||
|
}
|
||||||
|
out[k] = (EEL_F)(d * fourovern);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// MDCT/iMDCT borrowed from Vorbis, thanks xiph!
|
||||||
|
|
||||||
|
|
||||||
|
#define cPI3_8 .38268343236508977175
|
||||||
|
#define cPI2_8 .70710678118654752441
|
||||||
|
#define cPI1_8 .92387953251128675613
|
||||||
|
|
||||||
|
#define FLOAT_CONV(x) ((EEL_F) ( x ))
|
||||||
|
#define MULT_NORM(x) (x)
|
||||||
|
#define HALVE(x) ((x)*.5f)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* 8 point butterfly (in place, 4 register) */
|
||||||
|
static void mdct_butterfly_8(EEL_F *x) {
|
||||||
|
EEL_F r0 = x[6] + x[2];
|
||||||
|
EEL_F r1 = x[6] - x[2];
|
||||||
|
EEL_F r2 = x[4] + x[0];
|
||||||
|
EEL_F r3 = x[4] - x[0];
|
||||||
|
|
||||||
|
x[6] = r0 + r2;
|
||||||
|
x[4] = r0 - r2;
|
||||||
|
|
||||||
|
r0 = x[5] - x[1];
|
||||||
|
r2 = x[7] - x[3];
|
||||||
|
x[0] = r1 + r0;
|
||||||
|
x[2] = r1 - r0;
|
||||||
|
|
||||||
|
r0 = x[5] + x[1];
|
||||||
|
r1 = x[7] + x[3];
|
||||||
|
x[3] = r2 + r3;
|
||||||
|
x[1] = r2 - r3;
|
||||||
|
x[7] = r1 + r0;
|
||||||
|
x[5] = r1 - r0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 16 point butterfly (in place, 4 register) */
|
||||||
|
static void mdct_butterfly_16(EEL_F *x) {
|
||||||
|
EEL_F r0 = x[1] - x[9];
|
||||||
|
EEL_F r1 = x[0] - x[8];
|
||||||
|
|
||||||
|
x[8] += x[0];
|
||||||
|
x[9] += x[1];
|
||||||
|
x[0] = MULT_NORM((r0 + r1) * cPI2_8);
|
||||||
|
x[1] = MULT_NORM((r0 - r1) * cPI2_8);
|
||||||
|
|
||||||
|
r0 = x[3] - x[11];
|
||||||
|
r1 = x[10] - x[2];
|
||||||
|
x[10] += x[2];
|
||||||
|
x[11] += x[3];
|
||||||
|
x[2] = r0;
|
||||||
|
x[3] = r1;
|
||||||
|
|
||||||
|
r0 = x[12] - x[4];
|
||||||
|
r1 = x[13] - x[5];
|
||||||
|
x[12] += x[4];
|
||||||
|
x[13] += x[5];
|
||||||
|
x[4] = MULT_NORM((r0 - r1) * cPI2_8);
|
||||||
|
x[5] = MULT_NORM((r0 + r1) * cPI2_8);
|
||||||
|
|
||||||
|
r0 = x[14] - x[6];
|
||||||
|
r1 = x[15] - x[7];
|
||||||
|
x[14] += x[6];
|
||||||
|
x[15] += x[7];
|
||||||
|
x[6] = r0;
|
||||||
|
x[7] = r1;
|
||||||
|
|
||||||
|
mdct_butterfly_8(x);
|
||||||
|
mdct_butterfly_8(x + 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 32 point butterfly (in place, 4 register) */
|
||||||
|
static void mdct_butterfly_32(EEL_F *x) {
|
||||||
|
EEL_F r0 = x[30] - x[14];
|
||||||
|
EEL_F r1 = x[31] - x[15];
|
||||||
|
|
||||||
|
x[30] += x[14];
|
||||||
|
x[31] += x[15];
|
||||||
|
x[14] = r0;
|
||||||
|
x[15] = r1;
|
||||||
|
|
||||||
|
r0 = x[28] - x[12];
|
||||||
|
r1 = x[29] - x[13];
|
||||||
|
x[28] += x[12];
|
||||||
|
x[29] += x[13];
|
||||||
|
x[12] = MULT_NORM( r0 * cPI1_8 - r1 * cPI3_8 );
|
||||||
|
x[13] = MULT_NORM( r0 * cPI3_8 + r1 * cPI1_8 );
|
||||||
|
|
||||||
|
r0 = x[26] - x[10];
|
||||||
|
r1 = x[27] - x[11];
|
||||||
|
x[26] += x[10];
|
||||||
|
x[27] += x[11];
|
||||||
|
x[10] = MULT_NORM(( r0 - r1 ) * cPI2_8);
|
||||||
|
x[11] = MULT_NORM(( r0 + r1 ) * cPI2_8);
|
||||||
|
|
||||||
|
r0 = x[24] - x[8];
|
||||||
|
r1 = x[25] - x[9];
|
||||||
|
x[24] += x[8];
|
||||||
|
x[25] += x[9];
|
||||||
|
x[8] = MULT_NORM( r0 * cPI3_8 - r1 * cPI1_8 );
|
||||||
|
x[9] = MULT_NORM( r1 * cPI3_8 + r0 * cPI1_8 );
|
||||||
|
|
||||||
|
r0 = x[22] - x[6];
|
||||||
|
r1 = x[7] - x[23];
|
||||||
|
x[22] += x[6];
|
||||||
|
x[23] += x[7];
|
||||||
|
x[6] = r1;
|
||||||
|
x[7] = r0;
|
||||||
|
|
||||||
|
r0 = x[4] - x[20];
|
||||||
|
r1 = x[5] - x[21];
|
||||||
|
x[20] += x[4];
|
||||||
|
x[21] += x[5];
|
||||||
|
x[4] = MULT_NORM( r1 * cPI1_8 + r0 * cPI3_8 );
|
||||||
|
x[5] = MULT_NORM( r1 * cPI3_8 - r0 * cPI1_8 );
|
||||||
|
|
||||||
|
r0 = x[2] - x[18];
|
||||||
|
r1 = x[3] - x[19];
|
||||||
|
x[18] += x[2];
|
||||||
|
x[19] += x[3];
|
||||||
|
x[2] = MULT_NORM(( r1 + r0 ) * cPI2_8);
|
||||||
|
x[3] = MULT_NORM(( r1 - r0 ) * cPI2_8);
|
||||||
|
|
||||||
|
r0 = x[0] - x[16];
|
||||||
|
r1 = x[1] - x[17];
|
||||||
|
x[16] += x[0];
|
||||||
|
x[17] += x[1];
|
||||||
|
x[0] = MULT_NORM( r1 * cPI3_8 + r0 * cPI1_8 );
|
||||||
|
x[1] = MULT_NORM( r1 * cPI1_8 - r0 * cPI3_8 );
|
||||||
|
|
||||||
|
mdct_butterfly_16(x);
|
||||||
|
mdct_butterfly_16(x + 16);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* N point first stage butterfly (in place, 2 register) */
|
||||||
|
static void mdct_butterfly_first(EEL_F *T,
|
||||||
|
EEL_F *x,
|
||||||
|
int points) {
|
||||||
|
|
||||||
|
EEL_F *x1 = x + points - 8;
|
||||||
|
EEL_F *x2 = x + (points >> 1) - 8;
|
||||||
|
EEL_F r0;
|
||||||
|
EEL_F r1;
|
||||||
|
|
||||||
|
do {
|
||||||
|
|
||||||
|
r0 = x1[6] - x2[6];
|
||||||
|
r1 = x1[7] - x2[7];
|
||||||
|
x1[6] += x2[6];
|
||||||
|
x1[7] += x2[7];
|
||||||
|
x2[6] = MULT_NORM(r1 * T[1] + r0 * T[0]);
|
||||||
|
x2[7] = MULT_NORM(r1 * T[0] - r0 * T[1]);
|
||||||
|
|
||||||
|
r0 = x1[4] - x2[4];
|
||||||
|
r1 = x1[5] - x2[5];
|
||||||
|
x1[4] += x2[4];
|
||||||
|
x1[5] += x2[5];
|
||||||
|
x2[4] = MULT_NORM(r1 * T[5] + r0 * T[4]);
|
||||||
|
x2[5] = MULT_NORM(r1 * T[4] - r0 * T[5]);
|
||||||
|
|
||||||
|
r0 = x1[2] - x2[2];
|
||||||
|
r1 = x1[3] - x2[3];
|
||||||
|
x1[2] += x2[2];
|
||||||
|
x1[3] += x2[3];
|
||||||
|
x2[2] = MULT_NORM(r1 * T[9] + r0 * T[8]);
|
||||||
|
x2[3] = MULT_NORM(r1 * T[8] - r0 * T[9]);
|
||||||
|
|
||||||
|
r0 = x1[0] - x2[0];
|
||||||
|
r1 = x1[1] - x2[1];
|
||||||
|
x1[0] += x2[0];
|
||||||
|
x1[1] += x2[1];
|
||||||
|
x2[0] = MULT_NORM(r1 * T[13] + r0 * T[12]);
|
||||||
|
x2[1] = MULT_NORM(r1 * T[12] - r0 * T[13]);
|
||||||
|
|
||||||
|
x1 -= 8;
|
||||||
|
x2 -= 8;
|
||||||
|
T += 16;
|
||||||
|
|
||||||
|
} while(x2 >= x);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* N/stage point generic N stage butterfly (in place, 2 register) */
|
||||||
|
static void mdct_butterfly_generic(EEL_F *T,
|
||||||
|
EEL_F *x,
|
||||||
|
int points,
|
||||||
|
int trigint) {
|
||||||
|
|
||||||
|
EEL_F *x1 = x + points - 8;
|
||||||
|
EEL_F *x2 = x + (points >> 1) - 8;
|
||||||
|
EEL_F r0;
|
||||||
|
EEL_F r1;
|
||||||
|
|
||||||
|
do {
|
||||||
|
|
||||||
|
r0 = x1[6] - x2[6];
|
||||||
|
r1 = x1[7] - x2[7];
|
||||||
|
x1[6] += x2[6];
|
||||||
|
x1[7] += x2[7];
|
||||||
|
x2[6] = MULT_NORM(r1 * T[1] + r0 * T[0]);
|
||||||
|
x2[7] = MULT_NORM(r1 * T[0] - r0 * T[1]);
|
||||||
|
|
||||||
|
T += trigint;
|
||||||
|
|
||||||
|
r0 = x1[4] - x2[4];
|
||||||
|
r1 = x1[5] - x2[5];
|
||||||
|
x1[4] += x2[4];
|
||||||
|
x1[5] += x2[5];
|
||||||
|
x2[4] = MULT_NORM(r1 * T[1] + r0 * T[0]);
|
||||||
|
x2[5] = MULT_NORM(r1 * T[0] - r0 * T[1]);
|
||||||
|
|
||||||
|
T += trigint;
|
||||||
|
|
||||||
|
r0 = x1[2] - x2[2];
|
||||||
|
r1 = x1[3] - x2[3];
|
||||||
|
x1[2] += x2[2];
|
||||||
|
x1[3] += x2[3];
|
||||||
|
x2[2] = MULT_NORM(r1 * T[1] + r0 * T[0]);
|
||||||
|
x2[3] = MULT_NORM(r1 * T[0] - r0 * T[1]);
|
||||||
|
|
||||||
|
T += trigint;
|
||||||
|
|
||||||
|
r0 = x1[0] - x2[0];
|
||||||
|
r1 = x1[1] - x2[1];
|
||||||
|
x1[0] += x2[0];
|
||||||
|
x1[1] += x2[1];
|
||||||
|
x2[0] = MULT_NORM(r1 * T[1] + r0 * T[0]);
|
||||||
|
x2[1] = MULT_NORM(r1 * T[0] - r0 * T[1]);
|
||||||
|
|
||||||
|
T += trigint;
|
||||||
|
x1 -= 8;
|
||||||
|
x2 -= 8;
|
||||||
|
|
||||||
|
} while(x2 >= x);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void mdct_butterflies(mdct_lookup *init,
|
||||||
|
EEL_F *x,
|
||||||
|
int points) {
|
||||||
|
|
||||||
|
EEL_F *T = init->trig;
|
||||||
|
int stages = init->log2n - 5;
|
||||||
|
int i, j;
|
||||||
|
|
||||||
|
if(--stages > 0) {
|
||||||
|
mdct_butterfly_first(T, x, points);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(i = 1; --stages > 0; i++) {
|
||||||
|
for(j = 0; j < (1 << i); j++)
|
||||||
|
mdct_butterfly_generic(T, x + (points >> i)*j, points >> i, 4 << i);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(j = 0; j < points; j += 32)
|
||||||
|
mdct_butterfly_32(x + j);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static void mdct_bitreverse(mdct_lookup *init,
|
||||||
|
EEL_F *x) {
|
||||||
|
int n = init->n;
|
||||||
|
int *bit = init->bitrev;
|
||||||
|
EEL_F *w0 = x;
|
||||||
|
EEL_F *w1 = x = w0 + (n >> 1);
|
||||||
|
EEL_F *T = init->trig + n;
|
||||||
|
|
||||||
|
do {
|
||||||
|
EEL_F *x0 = x + bit[0];
|
||||||
|
EEL_F *x1 = x + bit[1];
|
||||||
|
|
||||||
|
EEL_F r0 = x0[1] - x1[1];
|
||||||
|
EEL_F r1 = x0[0] + x1[0];
|
||||||
|
EEL_F r2 = MULT_NORM(r1 * T[0] + r0 * T[1]);
|
||||||
|
EEL_F r3 = MULT_NORM(r1 * T[1] - r0 * T[0]);
|
||||||
|
|
||||||
|
w1 -= 4;
|
||||||
|
|
||||||
|
r0 = HALVE(x0[1] + x1[1]);
|
||||||
|
r1 = HALVE(x0[0] - x1[0]);
|
||||||
|
|
||||||
|
w0[0] = r0 + r2;
|
||||||
|
w1[2] = r0 - r2;
|
||||||
|
w0[1] = r1 + r3;
|
||||||
|
w1[3] = r3 - r1;
|
||||||
|
|
||||||
|
x0 = x + bit[2];
|
||||||
|
x1 = x + bit[3];
|
||||||
|
|
||||||
|
r0 = x0[1] - x1[1];
|
||||||
|
r1 = x0[0] + x1[0];
|
||||||
|
r2 = MULT_NORM(r1 * T[2] + r0 * T[3]);
|
||||||
|
r3 = MULT_NORM(r1 * T[3] - r0 * T[2]);
|
||||||
|
|
||||||
|
r0 = HALVE(x0[1] + x1[1]);
|
||||||
|
r1 = HALVE(x0[0] - x1[0]);
|
||||||
|
|
||||||
|
w0[2] = r0 + r2;
|
||||||
|
w1[0] = r0 - r2;
|
||||||
|
w0[3] = r1 + r3;
|
||||||
|
w1[1] = r3 - r1;
|
||||||
|
|
||||||
|
T += 4;
|
||||||
|
bit += 4;
|
||||||
|
w0 += 4;
|
||||||
|
|
||||||
|
} while(w0 < w1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void megabuf_mdct_apply_window(void *init, EEL_F *inbuf, EEL_F *outbuf)
|
||||||
|
{
|
||||||
|
mdct_lookup *p = (mdct_lookup *)init;
|
||||||
|
EEL_F *w;
|
||||||
|
int cnt;
|
||||||
|
if (!p) return;
|
||||||
|
|
||||||
|
w = p->window;
|
||||||
|
if (!w) return;
|
||||||
|
|
||||||
|
cnt = p->n / 2;
|
||||||
|
while (cnt--) *outbuf++ = *inbuf++ * *w++;
|
||||||
|
cnt = p->n / 2;
|
||||||
|
while (cnt--) *outbuf++ = *inbuf++ * *--w;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static void *megabuf_mdct_init(int n) {
|
||||||
|
mdct_lookup *lookup = (mdct_lookup *)calloc(sizeof(mdct_lookup), 1);
|
||||||
|
int i;
|
||||||
|
EEL_F c = (PI / (EEL_F) n);
|
||||||
|
int *bitrev;
|
||||||
|
EEL_F *T;
|
||||||
|
int n2, log2n;
|
||||||
|
if (!lookup) return 0;
|
||||||
|
|
||||||
|
lookup->n = n;
|
||||||
|
lookup->window = (EEL_F *)calloc(sizeof(EEL_F), n / 2);
|
||||||
|
if (!lookup->window) return lookup;
|
||||||
|
|
||||||
|
for (i = 0; i < n / 2; i ++)
|
||||||
|
{
|
||||||
|
lookup->window[i] = sin(c * (i + 0.5));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (n <= 32) return lookup;
|
||||||
|
bitrev = (int*)calloc(sizeof(int), (n / 4));
|
||||||
|
lookup->bitrev = bitrev;
|
||||||
|
if (!bitrev) return lookup;
|
||||||
|
|
||||||
|
T = (EEL_F*)calloc(sizeof(EEL_F), (n + n / 4));
|
||||||
|
lookup->trig = T;
|
||||||
|
if (!T) return lookup;
|
||||||
|
|
||||||
|
n2 = n >> 1;
|
||||||
|
log2n = lookup->log2n = (int)(log((double)n) / log(2.0) + 0.5);
|
||||||
|
|
||||||
|
/* trig lookups... */
|
||||||
|
|
||||||
|
for(i = 0; i < n / 4; i++) {
|
||||||
|
T[i * 2] = FLOAT_CONV(cos((PI / n) * (4 * i)));
|
||||||
|
T[i * 2 + 1] = FLOAT_CONV(-sin((PI / n) * (4 * i)));
|
||||||
|
T[n2 + i * 2] = FLOAT_CONV(cos((PI / (2 * n)) * (2 * i + 1)));
|
||||||
|
T[n2 + i * 2 + 1] = FLOAT_CONV(sin((PI / (2 * n)) * (2 * i + 1)));
|
||||||
|
}
|
||||||
|
for(i = 0; i < n / 8; i++) {
|
||||||
|
T[n + i * 2] = FLOAT_CONV(cos((PI / n) * (4 * i + 2)) * .5);
|
||||||
|
T[n + i * 2 + 1] = FLOAT_CONV(-sin((PI / n) * (4 * i + 2)) * .5);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* bitreverse lookup... */
|
||||||
|
|
||||||
|
{
|
||||||
|
int mask = (1 << (log2n - 1)) - 1, j;
|
||||||
|
int msb = 1 << (log2n - 2);
|
||||||
|
for(i = 0; i < n / 8; i++) {
|
||||||
|
int acc = 0;
|
||||||
|
for(j = 0; msb >> j; j++)
|
||||||
|
if((msb >> j)&i)acc |= 1 << j;
|
||||||
|
bitrev[i * 2] = ((~acc)&mask) - 1;
|
||||||
|
bitrev[i * 2 + 1] = acc;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lookup->scale = FLOAT_CONV(4.f / n);
|
||||||
|
return lookup;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void megabuf_mdct_backward(void *init, EEL_F *in, EEL_F *out) {
|
||||||
|
mdct_lookup *lookup = (mdct_lookup *)init;
|
||||||
|
int n, n2, n4;
|
||||||
|
EEL_F *iX, *oX, *T;
|
||||||
|
if (!lookup) return;
|
||||||
|
n = lookup->n;
|
||||||
|
if (n <= 32 || !lookup->bitrev || !lookup->trig)
|
||||||
|
{
|
||||||
|
imdct(in, out, n);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
n2 = n >> 1;
|
||||||
|
n4 = n >> 2;
|
||||||
|
|
||||||
|
/* rotate */
|
||||||
|
|
||||||
|
iX = in + n2 - 7;
|
||||||
|
oX = out + n2 + n4;
|
||||||
|
T = lookup->trig + n4;
|
||||||
|
|
||||||
|
do {
|
||||||
|
oX -= 4;
|
||||||
|
oX[0] = MULT_NORM(-iX[2] * T[3] - iX[0] * T[2]);
|
||||||
|
oX[1] = MULT_NORM (iX[0] * T[3] - iX[2] * T[2]);
|
||||||
|
oX[2] = MULT_NORM(-iX[6] * T[1] - iX[4] * T[0]);
|
||||||
|
oX[3] = MULT_NORM (iX[4] * T[1] - iX[6] * T[0]);
|
||||||
|
iX -= 8;
|
||||||
|
T += 4;
|
||||||
|
} while(iX >= in);
|
||||||
|
|
||||||
|
iX = in + n2 - 8;
|
||||||
|
oX = out + n2 + n4;
|
||||||
|
T = lookup->trig + n4;
|
||||||
|
|
||||||
|
do {
|
||||||
|
T -= 4;
|
||||||
|
oX[0] = MULT_NORM (iX[4] * T[3] + iX[6] * T[2]);
|
||||||
|
oX[1] = MULT_NORM (iX[4] * T[2] - iX[6] * T[3]);
|
||||||
|
oX[2] = MULT_NORM (iX[0] * T[1] + iX[2] * T[0]);
|
||||||
|
oX[3] = MULT_NORM (iX[0] * T[0] - iX[2] * T[1]);
|
||||||
|
iX -= 8;
|
||||||
|
oX += 4;
|
||||||
|
} while(iX >= in);
|
||||||
|
|
||||||
|
mdct_butterflies(lookup, out + n2, n2);
|
||||||
|
mdct_bitreverse(lookup, out);
|
||||||
|
|
||||||
|
/* roatate + window */
|
||||||
|
|
||||||
|
{
|
||||||
|
EEL_F *oX1 = out + n2 + n4;
|
||||||
|
EEL_F *oX2 = out + n2 + n4;
|
||||||
|
iX = out;
|
||||||
|
T = lookup->trig + n2;
|
||||||
|
|
||||||
|
do {
|
||||||
|
oX1 -= 4;
|
||||||
|
|
||||||
|
oX1[3] = MULT_NORM (iX[0] * T[1] - iX[1] * T[0]);
|
||||||
|
oX2[0] = -MULT_NORM (iX[0] * T[0] + iX[1] * T[1]);
|
||||||
|
|
||||||
|
oX1[2] = MULT_NORM (iX[2] * T[3] - iX[3] * T[2]);
|
||||||
|
oX2[1] = -MULT_NORM (iX[2] * T[2] + iX[3] * T[3]);
|
||||||
|
|
||||||
|
oX1[1] = MULT_NORM (iX[4] * T[5] - iX[5] * T[4]);
|
||||||
|
oX2[2] = -MULT_NORM (iX[4] * T[4] + iX[5] * T[5]);
|
||||||
|
|
||||||
|
oX1[0] = MULT_NORM (iX[6] * T[7] - iX[7] * T[6]);
|
||||||
|
oX2[3] = -MULT_NORM (iX[6] * T[6] + iX[7] * T[7]);
|
||||||
|
|
||||||
|
oX2 += 4;
|
||||||
|
iX += 8;
|
||||||
|
T += 8;
|
||||||
|
} while(iX < oX1);
|
||||||
|
|
||||||
|
iX = out + n2 + n4;
|
||||||
|
oX1 = out + n4;
|
||||||
|
oX2 = oX1;
|
||||||
|
|
||||||
|
do {
|
||||||
|
oX1 -= 4;
|
||||||
|
iX -= 4;
|
||||||
|
|
||||||
|
oX2[0] = -(oX1[3] = iX[3]);
|
||||||
|
oX2[1] = -(oX1[2] = iX[2]);
|
||||||
|
oX2[2] = -(oX1[1] = iX[1]);
|
||||||
|
oX2[3] = -(oX1[0] = iX[0]);
|
||||||
|
|
||||||
|
oX2 += 4;
|
||||||
|
} while(oX2 < iX);
|
||||||
|
|
||||||
|
iX = out + n2 + n4;
|
||||||
|
oX1 = out + n2 + n4;
|
||||||
|
oX2 = out + n2;
|
||||||
|
do {
|
||||||
|
oX1 -= 4;
|
||||||
|
oX1[0] = iX[3];
|
||||||
|
oX1[1] = iX[2];
|
||||||
|
oX1[2] = iX[1];
|
||||||
|
oX1[3] = iX[0];
|
||||||
|
iX += 4;
|
||||||
|
} while(oX1 > oX2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void megabuf_mdct_forward(void *init, EEL_F *in, EEL_F *out) {
|
||||||
|
mdct_lookup *lookup = (mdct_lookup *)init;
|
||||||
|
int n, n2, n4, n8;
|
||||||
|
EEL_F *w, *w2;
|
||||||
|
if (!lookup) return;
|
||||||
|
|
||||||
|
n = lookup->n;
|
||||||
|
if (n <= 32 || !lookup->bitrev || !lookup->trig)
|
||||||
|
{
|
||||||
|
mdct(in, out, n);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
n2 = n >> 1;
|
||||||
|
n4 = n >> 2;
|
||||||
|
n8 = n >> 3;
|
||||||
|
EEL_F oldw[1<<EEL_DCT_MAXBITLEN];
|
||||||
|
w = oldw;
|
||||||
|
w2 = w + n2;
|
||||||
|
|
||||||
|
/* rotate */
|
||||||
|
|
||||||
|
/* window + rotate + step 1 */
|
||||||
|
|
||||||
|
{
|
||||||
|
EEL_F r0;
|
||||||
|
EEL_F r1;
|
||||||
|
EEL_F *x0 = in + n2 + n4;
|
||||||
|
EEL_F *x1 = x0 + 1;
|
||||||
|
EEL_F *T = lookup->trig + n2;
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
for(i = 0; i < n8; i += 2) {
|
||||||
|
x0 -= 4;
|
||||||
|
T -= 2;
|
||||||
|
r0 = x0[2] + x1[0];
|
||||||
|
r1 = x0[0] + x1[2];
|
||||||
|
w2[i] = MULT_NORM(r1 * T[1] + r0 * T[0]);
|
||||||
|
w2[i + 1] = MULT_NORM(r1 * T[0] - r0 * T[1]);
|
||||||
|
x1 += 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
x1 = in + 1;
|
||||||
|
|
||||||
|
for(; i < n2 - n8; i += 2) {
|
||||||
|
T -= 2;
|
||||||
|
x0 -= 4;
|
||||||
|
r0 = x0[2] - x1[0];
|
||||||
|
r1 = x0[0] - x1[2];
|
||||||
|
w2[i] = MULT_NORM(r1 * T[1] + r0 * T[0]);
|
||||||
|
w2[i + 1] = MULT_NORM(r1 * T[0] - r0 * T[1]);
|
||||||
|
x1 += 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
x0 = in + n;
|
||||||
|
|
||||||
|
for(; i < n2; i += 2) {
|
||||||
|
T -= 2;
|
||||||
|
x0 -= 4;
|
||||||
|
r0 = -x0[2] - x1[0];
|
||||||
|
r1 = -x0[0] - x1[2];
|
||||||
|
w2[i] = MULT_NORM(r1 * T[1] + r0 * T[0]);
|
||||||
|
w2[i + 1] = MULT_NORM(r1 * T[0] - r0 * T[1]);
|
||||||
|
x1 += 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
mdct_butterflies(lookup, w + n2, n2);
|
||||||
|
mdct_bitreverse(lookup, w);
|
||||||
|
|
||||||
|
/* roatate + window */
|
||||||
|
|
||||||
|
T = lookup->trig + n2;
|
||||||
|
x0 = out + n2;
|
||||||
|
|
||||||
|
for(i = 0; i < n4; i++) {
|
||||||
|
x0--;
|
||||||
|
out[i] = MULT_NORM((w[0] * T[0] + w[1] * T[1]) * lookup->scale);
|
||||||
|
x0[0] = MULT_NORM((w[0] * T[1] - w[1] * T[0]) * lookup->scale);
|
||||||
|
w += 2;
|
||||||
|
T += 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
|
||||||
|
static void dct(EEL_F *in, EEL_F *out, int len)
|
||||||
|
{
|
||||||
|
int k;
|
||||||
|
EEL_F wk = sqrt(2.0 / len);
|
||||||
|
EEL_F overtwolen = 0.5 / (EEL_F)len;
|
||||||
|
for (k = 0; k < len; k ++)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
EEL_F d = 0.0;
|
||||||
|
for (n = 0; n < len; n ++)
|
||||||
|
{
|
||||||
|
int an = n + 1;
|
||||||
|
d += in[n] * cos(PI * (2.0 * n + 1.0) * (EEL_F)k * overtwolen);
|
||||||
|
}
|
||||||
|
if (!k) d /= sqrt(len);
|
||||||
|
else d *= wk;
|
||||||
|
out[k] = (EEL_F)d;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void idct(EEL_F *in, EEL_F *out, int len)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
EEL_F dd0 = 1.0 / sqrt(len);
|
||||||
|
EEL_F dd1 = sqrt(2.0 / len);
|
||||||
|
EEL_F overtwolen = 0.5 / len;
|
||||||
|
for (n = 0; n < len; n ++)
|
||||||
|
{
|
||||||
|
int k;
|
||||||
|
EEL_F d = 0.0;
|
||||||
|
for (k = 0; k < len; k ++)
|
||||||
|
{
|
||||||
|
EEL_F dd;
|
||||||
|
if (!k) dd = dd0 * in[k];
|
||||||
|
else dd = dd1 * in[k];
|
||||||
|
d += dd * cos(PI * (2.0 * n + 1.0) * k * overtwolen);
|
||||||
|
}
|
||||||
|
out[n] = (EEL_F)d;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
// 0 is megabuf blocks
|
||||||
|
// 1 is need_free flag
|
||||||
|
|
||||||
|
|
||||||
|
static EEL_F * NSEEL_CGEN_CALL mdct_func(int dir, EEL_F **blocks, EEL_F *start, EEL_F *length)
|
||||||
|
{
|
||||||
|
int l = (int)(*length + 0.0001);
|
||||||
|
int offs = (int)(*start + 0.0001);
|
||||||
|
int bitl = 0;
|
||||||
|
int ilen;
|
||||||
|
int bidx;
|
||||||
|
EEL_F *ptr;
|
||||||
|
while (l > 1 && bitl < EEL_DCT_MAXBITLEN)
|
||||||
|
{
|
||||||
|
bitl++;
|
||||||
|
l >>= 1;
|
||||||
|
}
|
||||||
|
if (bitl < EEL_DCT_MINBITLEN)
|
||||||
|
{
|
||||||
|
return start;
|
||||||
|
}
|
||||||
|
ilen = 1 << bitl;
|
||||||
|
|
||||||
|
bidx = bitl - EEL_DCT_MINBITLEN;
|
||||||
|
|
||||||
|
|
||||||
|
// check to make sure we don't cross a boundary
|
||||||
|
if (offs / NSEEL_RAM_ITEMSPERBLOCK != (offs + ilen * 2 - 1) / NSEEL_RAM_ITEMSPERBLOCK)
|
||||||
|
{
|
||||||
|
return start;
|
||||||
|
}
|
||||||
|
|
||||||
|
ptr = __NSEEL_RAMAlloc(blocks, offs);
|
||||||
|
if (!ptr || ptr == &nseel_ramalloc_onfail)
|
||||||
|
{
|
||||||
|
return start;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ilen > 1)
|
||||||
|
{
|
||||||
|
static void *mdct_ctxs[1 + EEL_DCT_MAXBITLEN - EEL_DCT_MINBITLEN];
|
||||||
|
|
||||||
|
if (!mdct_ctxs[bidx])
|
||||||
|
{
|
||||||
|
NSEEL_HOSTSTUB_EnterMutex();
|
||||||
|
if (!mdct_ctxs[bidx])
|
||||||
|
mdct_ctxs[bidx] = megabuf_mdct_init(ilen);
|
||||||
|
NSEEL_HOSTSTUB_LeaveMutex();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mdct_ctxs[bidx])
|
||||||
|
{
|
||||||
|
EEL_F buf[1 << EEL_DCT_MAXBITLEN];
|
||||||
|
if (dir < 0)
|
||||||
|
{
|
||||||
|
megabuf_mdct_backward(mdct_ctxs[bidx], ptr, buf);
|
||||||
|
megabuf_mdct_apply_window(mdct_ctxs[bidx], buf, ptr);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
megabuf_mdct_apply_window(mdct_ctxs[bidx], ptr, buf);
|
||||||
|
megabuf_mdct_forward(mdct_ctxs[bidx], buf, ptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return start;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static EEL_F * NSEEL_CGEN_CALL megabuf_mdct(EEL_F **blocks, EEL_F *start, EEL_F *length)
|
||||||
|
{
|
||||||
|
return mdct_func(0, blocks, start, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F * NSEEL_CGEN_CALL megabuf_imdct(EEL_F **blocks, EEL_F *start, EEL_F *length)
|
||||||
|
{
|
||||||
|
return mdct_func(-1, blocks, start, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
void EEL_mdct_register()
|
||||||
|
{
|
||||||
|
NSEEL_addfunc_retptr("mdct", 2, NSEEL_PProc_RAM, &megabuf_mdct);
|
||||||
|
NSEEL_addfunc_retptr("imdct", 2, NSEEL_PProc_RAM, &megabuf_imdct);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef EEL_WANT_DOCUMENTATION
|
||||||
|
static const char *eel_mdct_function_reference =
|
||||||
|
"mdct\tbuffer,length\tPerforms a windowed modified DCT, taking length inputs and producing length/2 outputs. buffer must not cross a 65,536 item boundary, and length must be 64, 128, 256, 512, 2048 or 4096.\0"
|
||||||
|
"imdct\tbuffer,length\tPerforms a windowed inverse modified DCT, taking length/2 inputs and producing length outputs. buffer must not cross a 65,536 item boundary, and length must be 64, 128, 256, 512, 2048 or 4096.\0"
|
||||||
|
;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
73
oversampling/WDL/eel2/eel_misc.h
Normal file
73
oversampling/WDL/eel2/eel_misc.h
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
#ifndef _EEL_MISC_H_
|
||||||
|
#define _EEL_MISC_H_
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#include <sys/time.h>
|
||||||
|
#endif
|
||||||
|
#include <time.h>
|
||||||
|
// some generic EEL functions for things like time
|
||||||
|
|
||||||
|
#ifndef EEL_MISC_NO_SLEEP
|
||||||
|
static EEL_F NSEEL_CGEN_CALL _eel_sleep(void *opaque, EEL_F *amt)
|
||||||
|
{
|
||||||
|
if (*amt >= 0.0)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
if (*amt > 30000000.0) Sleep(30000000);
|
||||||
|
else Sleep((DWORD)(*amt+0.5));
|
||||||
|
#else
|
||||||
|
if (*amt > 30000000.0) usleep(((useconds_t)30000000)*1000);
|
||||||
|
else usleep((useconds_t)(*amt*1000.0+0.5));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static EEL_F * NSEEL_CGEN_CALL _eel_time(void *opaque, EEL_F *v)
|
||||||
|
{
|
||||||
|
*v = (EEL_F) time(NULL);
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F * NSEEL_CGEN_CALL _eel_time_precise(void *opaque, EEL_F *v)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
LARGE_INTEGER freq,now;
|
||||||
|
QueryPerformanceFrequency(&freq);
|
||||||
|
QueryPerformanceCounter(&now);
|
||||||
|
*v = (double)now.QuadPart / (double)freq.QuadPart;
|
||||||
|
// *v = (EEL_F)timeGetTime() * 0.001;
|
||||||
|
#else
|
||||||
|
struct timeval tm={0,};
|
||||||
|
gettimeofday(&tm,NULL);
|
||||||
|
*v = tm.tv_sec + tm.tv_usec*0.000001;
|
||||||
|
#endif
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void EEL_misc_register()
|
||||||
|
{
|
||||||
|
#ifndef EEL_MISC_NO_SLEEP
|
||||||
|
NSEEL_addfunc_retval("sleep",1,NSEEL_PProc_THIS,&_eel_sleep);
|
||||||
|
#endif
|
||||||
|
NSEEL_addfunc_retptr("time",1,NSEEL_PProc_THIS,&_eel_time);
|
||||||
|
NSEEL_addfunc_retptr("time_precise",1,NSEEL_PProc_THIS,&_eel_time_precise);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef EEL_WANT_DOCUMENTATION
|
||||||
|
static const char *eel_misc_function_reference =
|
||||||
|
#ifndef EEL_MISC_NO_SLEEP
|
||||||
|
"sleep\tms\tYields the CPU for the millisecond count specified, calling Sleep() on Windows or usleep() on other platforms.\0"
|
||||||
|
#endif
|
||||||
|
"time\t[&val]\tSets the parameter (or a temporary buffer if omitted) to the number of seconds since January 1, 1970, and returns a reference to that value. "
|
||||||
|
"The granularity of the value returned is 1 second.\0"
|
||||||
|
"time_precise\t[&val]\tSets the parameter (or a temporary buffer if omitted) to a system-local timestamp in seconds, and returns a reference to that value. "
|
||||||
|
"The granularity of the value returned is system defined (but generally significantly smaller than one second).\0"
|
||||||
|
;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
564
oversampling/WDL/eel2/eel_net.h
Normal file
564
oversampling/WDL/eel2/eel_net.h
Normal file
@@ -0,0 +1,564 @@
|
|||||||
|
#ifndef _EEL_NET_H_
|
||||||
|
#define _EEL_NET_H_
|
||||||
|
|
||||||
|
// x = tcp_listen(port[,interface, connected_ip_out]) poll this, returns connection id > 0, or <0 on error, or 0 if no new connect -- interface only valid on first call (or after tcp_listen_end(port))
|
||||||
|
// tcp_listen_end(port);
|
||||||
|
|
||||||
|
// connection = tcp_connect(host, port[, block]) // connection id > 0 on ok
|
||||||
|
// tcp_set_block(connection, block?)
|
||||||
|
// tcp_close(connection)
|
||||||
|
|
||||||
|
// tcp_send(connection, string[, length]) // can return 0 if block, -1 if error, otherwise returns length sent
|
||||||
|
// tcp_recv(connection, string[, maxlength]) // 0 on nothing, -1 on error, otherwise returns length recv'd
|
||||||
|
|
||||||
|
|
||||||
|
// need:
|
||||||
|
// #define EEL_NET_GET_CONTEXT(opaque) (((sInst *)opaque)->m_net_state)
|
||||||
|
|
||||||
|
// you must pass a JNL_AsyncDNS object to eel_net_state to support nonblocking connect with DNS resolution, otherwise DNS will block
|
||||||
|
// #define EEL_NET_NO_SYNC_DNS -- never ever call gethostbyname() synchronously, may disable DNS for blocking connect, or if a JNL_IAsyncDNS is not provided.
|
||||||
|
|
||||||
|
#ifndef EEL_NET_MAXSEND
|
||||||
|
#define EEL_NET_MAXSEND (EEL_STRING_MAXUSERSTRING_LENGTH_HINT+4096)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#include "../jnetlib/netinc.h"
|
||||||
|
#define JNL_NO_IMPLEMENTATION
|
||||||
|
#include "../jnetlib/asyncdns.h"
|
||||||
|
|
||||||
|
class eel_net_state
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
enum { STATE_FREE=0, STATE_RESOLVING, STATE_CONNECTED, STATE_ERR };
|
||||||
|
enum { CONNECTION_ID_BASE=0x110000 };
|
||||||
|
|
||||||
|
eel_net_state(int max_con, JNL_IAsyncDNS *dns);
|
||||||
|
~eel_net_state();
|
||||||
|
|
||||||
|
struct connection_state {
|
||||||
|
char *hostname; // set during resolve only
|
||||||
|
SOCKET sock;
|
||||||
|
int state; // STATE_RESOLVING...
|
||||||
|
int port;
|
||||||
|
bool blockmode;
|
||||||
|
};
|
||||||
|
WDL_TypedBuf<connection_state> m_cons;
|
||||||
|
WDL_IntKeyedArray<SOCKET> m_listens;
|
||||||
|
JNL_IAsyncDNS *m_dns;
|
||||||
|
|
||||||
|
EEL_F onConnect(char *hostNameOwned, int port, int block);
|
||||||
|
EEL_F onClose(void *opaque, EEL_F handle);
|
||||||
|
EEL_F set_block(void *opaque, EEL_F handle, bool block);
|
||||||
|
EEL_F onListen(void *opaque, EEL_F handle, int mode, EEL_F *ifStr, EEL_F *ipOut);
|
||||||
|
|
||||||
|
int __run_connect(connection_state *cs, unsigned int ip);
|
||||||
|
int __run(connection_state *cs);
|
||||||
|
int do_send(void *opaque, EEL_F h, const char *src, int len);
|
||||||
|
int do_recv(void *opaque, EEL_F h, char *buf, int maxlen);
|
||||||
|
#ifdef _WIN32
|
||||||
|
bool m_had_socketlib_init;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
eel_net_state::eel_net_state(int max_con, JNL_IAsyncDNS *dns)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
m_had_socketlib_init=false;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
m_cons.Resize(max_con);
|
||||||
|
int x;
|
||||||
|
for (x=0;x<m_cons.GetSize();x++)
|
||||||
|
{
|
||||||
|
m_cons.Get()[x].state = STATE_FREE;
|
||||||
|
m_cons.Get()[x].sock = INVALID_SOCKET;
|
||||||
|
m_cons.Get()[x].hostname = NULL;
|
||||||
|
}
|
||||||
|
m_dns=dns;
|
||||||
|
}
|
||||||
|
|
||||||
|
eel_net_state::~eel_net_state()
|
||||||
|
{
|
||||||
|
int x;
|
||||||
|
for (x=0;x<m_cons.GetSize();x++)
|
||||||
|
{
|
||||||
|
SOCKET s=m_cons.Get()[x].sock;
|
||||||
|
if (s != INVALID_SOCKET)
|
||||||
|
{
|
||||||
|
shutdown(s,SHUT_RDWR);
|
||||||
|
closesocket(s);
|
||||||
|
}
|
||||||
|
free(m_cons.Get()[x].hostname);
|
||||||
|
}
|
||||||
|
for (x=0;x<m_listens.GetSize();x++)
|
||||||
|
{
|
||||||
|
SOCKET s=m_listens.Enumerate(x);
|
||||||
|
shutdown(s, SHUT_RDWR);
|
||||||
|
closesocket(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
EEL_F eel_net_state::onConnect(char *hostNameOwned, int port, int block)
|
||||||
|
{
|
||||||
|
int x;
|
||||||
|
#ifdef _WIN32
|
||||||
|
if (!m_had_socketlib_init)
|
||||||
|
{
|
||||||
|
m_had_socketlib_init=1;
|
||||||
|
WSADATA wsaData;
|
||||||
|
WSAStartup(MAKEWORD(1, 1), &wsaData);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
for(x=0;x<m_cons.GetSize();x++)
|
||||||
|
{
|
||||||
|
connection_state *s=m_cons.Get()+x;
|
||||||
|
if (s->state == STATE_FREE)
|
||||||
|
{
|
||||||
|
unsigned int ip=inet_addr(hostNameOwned);
|
||||||
|
if (m_dns && ip == INADDR_NONE && !block)
|
||||||
|
{
|
||||||
|
const int r=m_dns->resolve(hostNameOwned,&ip);
|
||||||
|
if (r<0) break; // error!
|
||||||
|
|
||||||
|
if (r>0) ip = INADDR_NONE;
|
||||||
|
}
|
||||||
|
#ifndef EEL_NET_NO_SYNC_DNS
|
||||||
|
else if (ip == INADDR_NONE)
|
||||||
|
{
|
||||||
|
struct hostent *he = gethostbyname(hostNameOwned);
|
||||||
|
if (he) ip = *(int *)he->h_addr;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
if (hostNameOwned || ip != INADDR_NONE)
|
||||||
|
{
|
||||||
|
if (ip != INADDR_NONE)
|
||||||
|
{
|
||||||
|
free(hostNameOwned);
|
||||||
|
hostNameOwned=NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
s->state = STATE_RESOLVING;
|
||||||
|
s->hostname = hostNameOwned;
|
||||||
|
s->blockmode = !!block;
|
||||||
|
s->port = port;
|
||||||
|
if (hostNameOwned || __run_connect(s,ip)) return x + CONNECTION_ID_BASE;
|
||||||
|
|
||||||
|
s->state=STATE_FREE;
|
||||||
|
s->hostname=NULL;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(hostNameOwned);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
EEL_F eel_net_state::onListen(void *opaque, EEL_F handle, int mode, EEL_F *ifStr, EEL_F *ipOut)
|
||||||
|
{
|
||||||
|
const int port = (int) handle;
|
||||||
|
if (port < 1 || port > 65535)
|
||||||
|
{
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("tcp_listen(%d): invalid port specified, will never succeed",port);
|
||||||
|
#endif
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
#ifdef _WIN32
|
||||||
|
if (!m_had_socketlib_init)
|
||||||
|
{
|
||||||
|
m_had_socketlib_init=1;
|
||||||
|
WSADATA wsaData;
|
||||||
|
WSAStartup(MAKEWORD(1, 1), &wsaData);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
SOCKET *sockptr = m_listens.GetPtr(port);
|
||||||
|
if (mode<0)
|
||||||
|
{
|
||||||
|
if (!sockptr) return -1.0;
|
||||||
|
|
||||||
|
SOCKET ss=*sockptr;
|
||||||
|
m_listens.Delete(port);
|
||||||
|
if (ss != INVALID_SOCKET)
|
||||||
|
{
|
||||||
|
shutdown(ss, SHUT_RDWR);
|
||||||
|
closesocket(ss);
|
||||||
|
}
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
if (!sockptr)
|
||||||
|
{
|
||||||
|
struct sockaddr_in sin;
|
||||||
|
memset((char *) &sin, 0,sizeof(sin));
|
||||||
|
if (ifStr)
|
||||||
|
{
|
||||||
|
EEL_STRING_MUTEXLOCK_SCOPE
|
||||||
|
const char *fn = EEL_STRING_GET_FOR_INDEX(*ifStr,NULL);
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
if (!fn) EEL_STRING_DEBUGOUT("tcp_listen(%d): bad string identifier %f for second parameter (interface)",port,*ifStr);
|
||||||
|
#endif
|
||||||
|
if (fn && *fn) sin.sin_addr.s_addr=inet_addr(fn);
|
||||||
|
|
||||||
|
}
|
||||||
|
if (!sin.sin_addr.s_addr || sin.sin_addr.s_addr==INADDR_NONE) sin.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
sin.sin_family = AF_INET;
|
||||||
|
sin.sin_port = htons( (short) port );
|
||||||
|
SOCKET sock = socket(AF_INET,SOCK_STREAM,0);
|
||||||
|
if (sock != INVALID_SOCKET)
|
||||||
|
{
|
||||||
|
SET_SOCK_DEFAULTS(sock);
|
||||||
|
SET_SOCK_BLOCK(sock,0);
|
||||||
|
if (bind(sock,(struct sockaddr *)&sin,sizeof(sin)) || listen(sock,8)==-1)
|
||||||
|
{
|
||||||
|
shutdown(sock, SHUT_RDWR);
|
||||||
|
closesocket(sock);
|
||||||
|
sock=INVALID_SOCKET;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
//if (sock == INVALID_SOCKET) EEL_STRING_DEBUGOUT("tcp_listen(%d): failed listening on port",port);
|
||||||
|
// we report -1 to the caller, no need to error message
|
||||||
|
#endif
|
||||||
|
m_listens.Insert(port,sock);
|
||||||
|
sockptr = m_listens.GetPtr(port);
|
||||||
|
}
|
||||||
|
if (!sockptr || *sockptr == INVALID_SOCKET) return -1;
|
||||||
|
|
||||||
|
struct sockaddr_in saddr;
|
||||||
|
socklen_t length = sizeof(struct sockaddr_in);
|
||||||
|
SOCKET newsock = accept(*sockptr, (struct sockaddr *) &saddr, &length);
|
||||||
|
if (newsock == INVALID_SOCKET)
|
||||||
|
{
|
||||||
|
return 0; // nothing to report here
|
||||||
|
}
|
||||||
|
SET_SOCK_DEFAULTS(newsock);
|
||||||
|
|
||||||
|
int x;
|
||||||
|
for(x=0;x<m_cons.GetSize();x++)
|
||||||
|
{
|
||||||
|
connection_state *cs=m_cons.Get()+x;
|
||||||
|
if (cs->state == STATE_FREE)
|
||||||
|
{
|
||||||
|
cs->state=STATE_CONNECTED;
|
||||||
|
free(cs->hostname);
|
||||||
|
cs->hostname=NULL;
|
||||||
|
cs->sock = newsock;
|
||||||
|
cs->blockmode=true;
|
||||||
|
cs->port=0;
|
||||||
|
if (ipOut)
|
||||||
|
{
|
||||||
|
EEL_STRING_MUTEXLOCK_SCOPE
|
||||||
|
WDL_FastString *ws=NULL;
|
||||||
|
EEL_STRING_GET_FOR_WRITE(*ipOut,&ws);
|
||||||
|
if (ws)
|
||||||
|
{
|
||||||
|
const unsigned int a = ntohl(saddr.sin_addr.s_addr);
|
||||||
|
ws->SetFormatted(128,"%d.%d.%d.%d",(a>>24)&0xff,(a>>16)&0xff,(a>>8)&0xff,a&0xff);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("tcp_listen(%d): bad string identifier %f for third parameter (IP-out)",port,*ipOut);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return x + CONNECTION_ID_BASE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
shutdown(newsock, SHUT_RDWR);
|
||||||
|
closesocket(newsock);
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int eel_net_state::__run_connect(connection_state *cs, unsigned int ip)
|
||||||
|
{
|
||||||
|
SOCKET s=socket(AF_INET,SOCK_STREAM,0);
|
||||||
|
if (s == INVALID_SOCKET) return 0;
|
||||||
|
SET_SOCK_DEFAULTS(s);
|
||||||
|
|
||||||
|
if (!cs->blockmode) SET_SOCK_BLOCK(s,0);
|
||||||
|
|
||||||
|
struct sockaddr_in sa={0,};
|
||||||
|
sa.sin_family=AF_INET;
|
||||||
|
sa.sin_addr.s_addr = ip;
|
||||||
|
sa.sin_port = htons(cs->port);
|
||||||
|
if (!connect(s,(struct sockaddr *)&sa,16) || (!cs->blockmode && JNL_ERRNO == JNL_EINPROGRESS))
|
||||||
|
{
|
||||||
|
cs->state = STATE_CONNECTED;
|
||||||
|
cs->sock = s;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
shutdown(s, SHUT_RDWR);
|
||||||
|
closesocket(s);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int eel_net_state::__run(connection_state *cs)
|
||||||
|
{
|
||||||
|
if (cs->sock != INVALID_SOCKET) return 0;
|
||||||
|
|
||||||
|
if (!cs->hostname) return -1;
|
||||||
|
|
||||||
|
unsigned int ip=INADDR_NONE;
|
||||||
|
const int r=m_dns ? m_dns->resolve(cs->hostname,&ip) : -1;
|
||||||
|
if (r>0) return 0;
|
||||||
|
|
||||||
|
free(cs->hostname);
|
||||||
|
cs->hostname=NULL;
|
||||||
|
|
||||||
|
if (r<0 || !__run_connect(cs,ip))
|
||||||
|
{
|
||||||
|
cs->state = STATE_ERR;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int eel_net_state::do_recv(void *opaque, EEL_F h, char *buf, int maxlen)
|
||||||
|
{
|
||||||
|
const int idx=(int)h-CONNECTION_ID_BASE;
|
||||||
|
if (idx>=0 && idx<m_cons.GetSize())
|
||||||
|
{
|
||||||
|
connection_state *s=m_cons.Get()+idx;
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
if (s->sock == INVALID_SOCKET && !s->hostname)
|
||||||
|
EEL_STRING_DEBUGOUT("tcp_recv: connection identifier %f is not open",h);
|
||||||
|
#endif
|
||||||
|
if (__run(s) || s->sock == INVALID_SOCKET) return s->state == STATE_ERR ? -1 : 0;
|
||||||
|
|
||||||
|
if (maxlen == 0) return 0;
|
||||||
|
|
||||||
|
const int rv=(int)recv(s->sock,buf,maxlen,0);
|
||||||
|
if (rv < 0 && !s->blockmode && (JNL_ERRNO == JNL_EWOULDBLOCK || JNL_ERRNO == JNL_ENOTCONN)) return 0;
|
||||||
|
|
||||||
|
if (!rv) return -1; // TCP, 0=connection terminated
|
||||||
|
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("tcp_recv: connection identifier %f is out of range",h);
|
||||||
|
#endif
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int eel_net_state::do_send(void *opaque, EEL_F h, const char *src, int len)
|
||||||
|
{
|
||||||
|
const int idx=(int)h-CONNECTION_ID_BASE;
|
||||||
|
if (idx>=0 && idx<m_cons.GetSize())
|
||||||
|
{
|
||||||
|
connection_state *s=m_cons.Get()+idx;
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
if (s->sock == INVALID_SOCKET && !s->hostname)
|
||||||
|
EEL_STRING_DEBUGOUT("tcp_send: connection identifier %f is not open",h);
|
||||||
|
#endif
|
||||||
|
if (__run(s) || s->sock == INVALID_SOCKET) return s->state == STATE_ERR ? -1 : 0;
|
||||||
|
const int rv=(int)send(s->sock,src,len,0);
|
||||||
|
if (rv < 0 && !s->blockmode && (JNL_ERRNO == JNL_EWOULDBLOCK || JNL_ERRNO == JNL_ENOTCONN)) return 0;
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("tcp_send: connection identifier %f out of range",h);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
EEL_F eel_net_state::set_block(void *opaque, EEL_F handle, bool block)
|
||||||
|
{
|
||||||
|
int idx=(int)handle-CONNECTION_ID_BASE;
|
||||||
|
if (idx>=0 && idx<m_cons.GetSize())
|
||||||
|
{
|
||||||
|
connection_state *s=m_cons.Get()+idx;
|
||||||
|
if (s->blockmode != block)
|
||||||
|
{
|
||||||
|
s->blockmode=block;
|
||||||
|
if (s->sock != INVALID_SOCKET)
|
||||||
|
{
|
||||||
|
SET_SOCK_BLOCK(s->sock,(block?1:0));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
if (!s->hostname) EEL_STRING_DEBUGOUT("tcp_set_block: connection identifier %f is not open",handle);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("tcp_set_block: connection identifier %f out of range",handle);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
EEL_F eel_net_state::onClose(void *opaque, EEL_F handle)
|
||||||
|
{
|
||||||
|
int idx=(int)handle-CONNECTION_ID_BASE;
|
||||||
|
if (idx>=0 && idx<m_cons.GetSize())
|
||||||
|
{
|
||||||
|
connection_state *s=m_cons.Get()+idx;
|
||||||
|
const bool hadhn = !!s->hostname;
|
||||||
|
free(s->hostname);
|
||||||
|
s->hostname = NULL;
|
||||||
|
s->state = STATE_ERR;
|
||||||
|
if (s->sock != INVALID_SOCKET)
|
||||||
|
{
|
||||||
|
shutdown(s->sock,SHUT_RDWR);
|
||||||
|
closesocket(s->sock);
|
||||||
|
s->sock = INVALID_SOCKET;
|
||||||
|
return 1.0;
|
||||||
|
}
|
||||||
|
else if (!hadhn)
|
||||||
|
{
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("tcp_close: connection identifier %f is not open",handle);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("tcp_close: connection identifier %f is out of range",handle);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static EEL_F NSEEL_CGEN_CALL _eel_tcp_connect(void *opaque, INT_PTR np, EEL_F **parms)
|
||||||
|
{
|
||||||
|
eel_net_state *ctx;
|
||||||
|
if (np > 1 && NULL != (ctx=EEL_NET_GET_CONTEXT(opaque)))
|
||||||
|
{
|
||||||
|
char *dest=NULL;
|
||||||
|
{
|
||||||
|
EEL_STRING_MUTEXLOCK_SCOPE
|
||||||
|
const char *fn = EEL_STRING_GET_FOR_INDEX(parms[0][0],NULL);
|
||||||
|
if (fn) dest=strdup(fn);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("tcp_connect(): host string identifier %f invalid",parms[0][0]);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (dest) return ctx->onConnect(dest, (int) (parms[1][0]+0.5), np < 3 || parms[2][0] >= 0.5);
|
||||||
|
}
|
||||||
|
return -1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F NSEEL_CGEN_CALL _eel_tcp_set_block(void *opaque, EEL_F *handle, EEL_F *bl)
|
||||||
|
{
|
||||||
|
eel_net_state *ctx;
|
||||||
|
if (NULL != (ctx=EEL_NET_GET_CONTEXT(opaque))) return ctx->set_block(opaque,*handle, *bl >= 0.5);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F NSEEL_CGEN_CALL _eel_tcp_close(void *opaque, EEL_F *handle)
|
||||||
|
{
|
||||||
|
eel_net_state *ctx;
|
||||||
|
if (NULL != (ctx=EEL_NET_GET_CONTEXT(opaque))) return ctx->onClose(opaque,*handle);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F NSEEL_CGEN_CALL _eel_tcp_recv(void *opaque, INT_PTR np, EEL_F **parms)
|
||||||
|
{
|
||||||
|
eel_net_state *ctx;
|
||||||
|
if (np > 1 && NULL != (ctx=EEL_NET_GET_CONTEXT(opaque)))
|
||||||
|
{
|
||||||
|
char buf[EEL_STRING_MAXUSERSTRING_LENGTH_HINT];
|
||||||
|
int ml = np > 2 ? (int)parms[2][0] : 4096;
|
||||||
|
if (ml < 0 || ml > EEL_STRING_MAXUSERSTRING_LENGTH_HINT) ml = EEL_STRING_MAXUSERSTRING_LENGTH_HINT;
|
||||||
|
|
||||||
|
ml=ctx->do_recv(opaque,parms[0][0],buf,ml);
|
||||||
|
|
||||||
|
{
|
||||||
|
EEL_STRING_MUTEXLOCK_SCOPE
|
||||||
|
WDL_FastString *ws=NULL;
|
||||||
|
EEL_STRING_GET_FOR_WRITE(parms[1][0],&ws);
|
||||||
|
if (ws)
|
||||||
|
{
|
||||||
|
if (ml<=0) ws->Set("");
|
||||||
|
else ws->SetRaw(buf,ml);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ml;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F NSEEL_CGEN_CALL _eel_tcp_send(void *opaque, INT_PTR np, EEL_F **parms)
|
||||||
|
{
|
||||||
|
eel_net_state *ctx;
|
||||||
|
if (np > 1 && NULL != (ctx=EEL_NET_GET_CONTEXT(opaque)))
|
||||||
|
{
|
||||||
|
char buf[EEL_NET_MAXSEND];
|
||||||
|
|
||||||
|
int l;
|
||||||
|
{
|
||||||
|
EEL_STRING_MUTEXLOCK_SCOPE
|
||||||
|
WDL_FastString *ws=NULL;
|
||||||
|
const char *fn = EEL_STRING_GET_FOR_INDEX(parms[1][0],&ws);
|
||||||
|
l = ws ? ws->GetLength() : (int) strlen(fn);
|
||||||
|
if (np > 2)
|
||||||
|
{
|
||||||
|
int al=(int)parms[2][0];
|
||||||
|
if (al<0) al=0;
|
||||||
|
if (al<l) l=al;
|
||||||
|
}
|
||||||
|
if (l > 0) memcpy(buf,fn,l);
|
||||||
|
}
|
||||||
|
if (l>0) return ctx->do_send(opaque,parms[0][0],buf,l);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F NSEEL_CGEN_CALL _eel_tcp_listen(void *opaque, INT_PTR np, EEL_F **parms)
|
||||||
|
{
|
||||||
|
eel_net_state *ctx;
|
||||||
|
if (NULL != (ctx=EEL_NET_GET_CONTEXT(opaque))) return ctx->onListen(opaque,parms[0][0],1,np>1?parms[1]:NULL,np>2?parms[2]:NULL);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F NSEEL_CGEN_CALL _eel_tcp_listen_end(void *opaque, EEL_F *handle)
|
||||||
|
{
|
||||||
|
eel_net_state *ctx;
|
||||||
|
if (NULL != (ctx=EEL_NET_GET_CONTEXT(opaque))) return ctx->onListen(opaque,*handle,-1,NULL,NULL);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void EEL_tcp_register()
|
||||||
|
{
|
||||||
|
NSEEL_addfunc_varparm("tcp_listen",1,NSEEL_PProc_THIS,&_eel_tcp_listen);
|
||||||
|
NSEEL_addfunc_retval("tcp_listen_end",1,NSEEL_PProc_THIS,&_eel_tcp_listen_end);
|
||||||
|
|
||||||
|
NSEEL_addfunc_varparm("tcp_connect",2,NSEEL_PProc_THIS,&_eel_tcp_connect);
|
||||||
|
NSEEL_addfunc_varparm("tcp_send",2,NSEEL_PProc_THIS,&_eel_tcp_send);
|
||||||
|
NSEEL_addfunc_varparm("tcp_recv",2,NSEEL_PProc_THIS,&_eel_tcp_recv);
|
||||||
|
|
||||||
|
NSEEL_addfunc_retval("tcp_set_block",2,NSEEL_PProc_THIS,&_eel_tcp_set_block);
|
||||||
|
NSEEL_addfunc_retval("tcp_close",1,NSEEL_PProc_THIS,&_eel_tcp_close);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef EEL_WANT_DOCUMENTATION
|
||||||
|
const char *eel_net_function_reference =
|
||||||
|
"tcp_listen\tport[,\"interface\",#ip_out]\tListens on port specified. Returns less than 0 if could not listen, 0 if no new connection available, or greater than 0 (as a TCP connection ID) if a new connection was made. If a connection made and #ip_out specified, it will be set to the remote IP. interface can be empty for all interfaces, otherwise an interface IP as a string.\0"
|
||||||
|
"tcp_listen_end\tport\tEnds listening on port specified.\0"
|
||||||
|
"tcp_connect\t\"address\",port[,block]\tCreate a new TCP connection to address:port. If block is specified and 0, connection will be made nonblocking. Returns TCP connection ID greater than 0 on success.\0"
|
||||||
|
"tcp_send\tconnection,\"str\"[,len]\tSends a string to connection. Returns -1 on error, 0 if connection is non-blocking and would block, otherwise returns length sent. If len is specified and not less than 1, only the first len bytes of the string parameter will be sent.\0"
|
||||||
|
"tcp_recv\tconnection,#str[,maxlen]\tReceives data from a connection to #str. If maxlen is specified, no more than maxlen bytes will be received. If non-blocking, 0 will be returned if would block. Returns less than 0 if error.\0"
|
||||||
|
"tcp_set_block\tconnection,block\tSets whether a connection blocks.\0"
|
||||||
|
"tcp_close\tconnection\tCloses a TCP connection created by tcp_listen() or tcp_connect().\0"
|
||||||
|
;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
47
oversampling/WDL/eel2/eel_pp.cpp
Normal file
47
oversampling/WDL/eel2/eel_pp.cpp
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
#include "../wdlstring.h"
|
||||||
|
#include "../ptrlist.h"
|
||||||
|
#include "eel_pproc.h"
|
||||||
|
|
||||||
|
void NSEEL_HOSTSTUB_EnterMutex() { }
|
||||||
|
void NSEEL_HOSTSTUB_LeaveMutex() { }
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
if (argc != 2)
|
||||||
|
{
|
||||||
|
fprintf(stderr,"Usage: %s [scriptfile | -]\n",argv[0]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
FILE *fp = strcmp(argv[1],"-") ? fopen(argv[1],"rb") : stdin;
|
||||||
|
if (!fp)
|
||||||
|
{
|
||||||
|
fprintf(stderr,"Error: could not open %s\n",argv[1]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
WDL_FastString file_str, pp_str;
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
char buf[4096];
|
||||||
|
if (!fgets(buf,sizeof(buf),fp)) break;
|
||||||
|
file_str.Append(buf);
|
||||||
|
}
|
||||||
|
if (fp != stdin) fclose(fp);
|
||||||
|
|
||||||
|
EEL2_PreProcessor pproc;
|
||||||
|
const char *err = pproc.preprocess(file_str.Get(),&pp_str);
|
||||||
|
if (err)
|
||||||
|
{
|
||||||
|
fprintf(stderr,"Error: %s\n",err);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%s",pp_str.Get());
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
469
oversampling/WDL/eel2/eel_pproc.h
Normal file
469
oversampling/WDL/eel2/eel_pproc.h
Normal file
@@ -0,0 +1,469 @@
|
|||||||
|
#ifndef _EEL2_PREPROC_H_
|
||||||
|
#define _EEL2_PREPROC_H_
|
||||||
|
#include "ns-eel-int.h"
|
||||||
|
#include "../win32_utf8.h"
|
||||||
|
|
||||||
|
#define EEL2_PREPROCESS_OPEN_TOKEN "<?"
|
||||||
|
|
||||||
|
class EEL2_PreProcessor
|
||||||
|
{
|
||||||
|
enum { LITERAL_BASE = 100000 };
|
||||||
|
public:
|
||||||
|
EEL2_PreProcessor(int max_sz = 64<<20, int max_include_depth=20)
|
||||||
|
{
|
||||||
|
m_max_sz = max_sz;
|
||||||
|
m_fsout = NULL;
|
||||||
|
m_vm = NSEEL_VM_alloc();
|
||||||
|
m_max_include_depth = max_include_depth;
|
||||||
|
m_output_linecnt = 0;
|
||||||
|
m_cur_depth = 0;
|
||||||
|
NSEEL_VM_SetCustomFuncThis(m_vm, this);
|
||||||
|
NSEEL_VM_SetStringFunc(m_vm, addStringCallback, NULL);
|
||||||
|
if (!m_ftab.list_size)
|
||||||
|
{
|
||||||
|
NSEEL_addfunc_varparm_ex("printf",1,0,NSEEL_PProc_THIS,&pp_printf,&m_ftab);
|
||||||
|
NSEEL_addfunc_varparm_ex("include",1,1,NSEEL_PProc_THIS,&pp_include,&m_ftab);
|
||||||
|
}
|
||||||
|
NSEEL_VM_SetFunctionTable(m_vm, &m_ftab);
|
||||||
|
m_suppress = NSEEL_VM_regvar(m_vm, "_suppress");
|
||||||
|
}
|
||||||
|
|
||||||
|
void define(const char *name, double val)
|
||||||
|
{
|
||||||
|
EEL_F *v = NSEEL_VM_regvar(m_vm,name);
|
||||||
|
if (v) *v = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
~EEL2_PreProcessor()
|
||||||
|
{
|
||||||
|
for (int x = 0; x < m_code_handles.GetSize(); x ++)
|
||||||
|
NSEEL_code_free((NSEEL_CODEHANDLE) m_code_handles.Get(x));
|
||||||
|
m_literal_strings.Empty(true,free);
|
||||||
|
if (m_vm) NSEEL_VM_free(m_vm);
|
||||||
|
m_suppress = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear_line_info()
|
||||||
|
{
|
||||||
|
m_line_tab.Resize(0);
|
||||||
|
}
|
||||||
|
const char *preprocess(const char *str, WDL_FastString *fs)
|
||||||
|
{
|
||||||
|
if (!m_vm || !m_suppress)
|
||||||
|
return "preprocessor: memory error";
|
||||||
|
if (!m_cur_depth)
|
||||||
|
{
|
||||||
|
m_line_tab.Resize(0);
|
||||||
|
m_output_linecnt = 0;
|
||||||
|
*m_suppress = 0.0;
|
||||||
|
}
|
||||||
|
int input_linecnt = 0;
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
const bool suppress = m_suppress && *m_suppress > 0.0;
|
||||||
|
|
||||||
|
int lc = 0;
|
||||||
|
const char *tag = str;
|
||||||
|
while (*tag && strncmp(tag,EEL2_PREPROCESS_OPEN_TOKEN,2)) if (*tag++ == '\n') lc++;
|
||||||
|
|
||||||
|
if (lc)
|
||||||
|
{
|
||||||
|
input_linecnt += lc;
|
||||||
|
if (suppress)
|
||||||
|
add_line_inf(m_output_linecnt,lc);
|
||||||
|
else
|
||||||
|
m_output_linecnt += lc;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!*tag)
|
||||||
|
{
|
||||||
|
if (!suppress) fs->Append(str);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (!suppress && tag > str) fs->Append(str,(int)(tag-str));
|
||||||
|
|
||||||
|
tag += 2;
|
||||||
|
while (*tag == ' ' || *tag == '\t') tag++;
|
||||||
|
str = tag;
|
||||||
|
lc = 0;
|
||||||
|
while (*str && strncmp(str,"?>",2)) if (*str++ == '\n') lc++;
|
||||||
|
|
||||||
|
if (!*str)
|
||||||
|
{
|
||||||
|
m_tmp.SetFormatted(512, "%d: unterminated preprocessor " EEL2_PREPROCESS_OPEN_TOKEN " block", input_linecnt+1);
|
||||||
|
return m_tmp.Get();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lc)
|
||||||
|
{
|
||||||
|
input_linecnt += lc;
|
||||||
|
add_line_inf(m_output_linecnt,lc);
|
||||||
|
}
|
||||||
|
if (str > tag)
|
||||||
|
{
|
||||||
|
m_tmp.Set(tag,(int)(str-tag));
|
||||||
|
NSEEL_CODEHANDLE ch = NSEEL_code_compile_ex(m_vm, m_tmp.Get(), 0, NSEEL_CODE_COMPILE_FLAG_COMMONFUNCS);
|
||||||
|
if (!ch)
|
||||||
|
{
|
||||||
|
const char *err = NSEEL_code_getcodeerror(m_vm);
|
||||||
|
if (err)
|
||||||
|
{
|
||||||
|
const int line_ref = atoi(err);
|
||||||
|
while (*err >= '0' && *err <= '9') err++;
|
||||||
|
m_tmp.SetFormatted(512,"%d: preprocessor%s%s",input_linecnt+line_ref,*err && *err != ':' ? ": ":"",err);
|
||||||
|
return m_tmp.Get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
lc = 0;
|
||||||
|
const int oldlen = fs->GetLength();
|
||||||
|
m_fsout = fs;
|
||||||
|
NSEEL_code_execute(ch);
|
||||||
|
m_fsout = NULL;
|
||||||
|
m_code_handles.Add(ch);
|
||||||
|
for (int x = oldlen; x < fs->GetLength(); x ++) if (fs->Get()[x] == '\n') lc++;
|
||||||
|
if (lc)
|
||||||
|
{
|
||||||
|
add_line_inf(m_output_linecnt,-lc);
|
||||||
|
m_output_linecnt += lc;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
str += 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *translate_error_line(const char *err_line)
|
||||||
|
{
|
||||||
|
if (m_line_tab.GetSize()<2) return err_line;
|
||||||
|
int l = atoi(err_line)-1;
|
||||||
|
if (l<0) return err_line;
|
||||||
|
|
||||||
|
// tab is a list of pairs
|
||||||
|
// [<output position>, delta]
|
||||||
|
// delta>0 if input lines were skipped
|
||||||
|
// delta<0 if output lines were added
|
||||||
|
|
||||||
|
int nl = l;
|
||||||
|
for (int x = m_line_tab.GetSize()-2; x >= 0; x -= 2)
|
||||||
|
{
|
||||||
|
int p = m_line_tab.Get()[x];
|
||||||
|
if (nl > p)
|
||||||
|
{
|
||||||
|
int delta = m_line_tab.Get()[x+1];
|
||||||
|
nl += delta;
|
||||||
|
if (nl < p) nl = p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (l == nl) return err_line;
|
||||||
|
|
||||||
|
while (*err_line >= '0' && *err_line <= '9') err_line++;
|
||||||
|
if (*err_line == ':') err_line++;
|
||||||
|
m_tmp.SetFormatted(512,"%d:%s",1 + nl,err_line);
|
||||||
|
return m_tmp.Get();
|
||||||
|
}
|
||||||
|
|
||||||
|
NSEEL_VMCTX m_vm;
|
||||||
|
WDL_PtrList<char> m_literal_strings;
|
||||||
|
WDL_FastString m_tmp, *m_fsout;
|
||||||
|
WDL_TypedBuf<int> m_line_tab; // expose this in case the caller wants to keep copies around
|
||||||
|
EEL_F *m_suppress;
|
||||||
|
int m_max_sz;
|
||||||
|
int m_cur_depth, m_max_include_depth;
|
||||||
|
int m_output_linecnt;
|
||||||
|
static eel_function_table m_ftab;
|
||||||
|
WDL_PtrList<void> m_code_handles;
|
||||||
|
WDL_PtrList<const char> m_include_paths;
|
||||||
|
|
||||||
|
void add_line_inf(int output_linecnt, int lc)
|
||||||
|
{
|
||||||
|
if (!m_cur_depth)
|
||||||
|
{
|
||||||
|
m_line_tab.Add(output_linecnt); // log lc lines of input skipped
|
||||||
|
m_line_tab.Add(lc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F addStringCallback(void *opaque, struct eelStringSegmentRec *list)
|
||||||
|
{
|
||||||
|
EEL2_PreProcessor *_this = (EEL2_PreProcessor*)opaque;
|
||||||
|
if (!_this) return -1.0;
|
||||||
|
|
||||||
|
const int sz = nseel_stringsegments_tobuf(NULL,0,list);
|
||||||
|
char *ns = (char *)malloc(sz+1);
|
||||||
|
if (WDL_NOT_NORMALLY(!ns)) return -1.0;
|
||||||
|
nseel_stringsegments_tobuf(ns,sz,list);
|
||||||
|
|
||||||
|
const int nstr = _this->m_literal_strings.GetSize();
|
||||||
|
for (int x=0;x<nstr;x++)
|
||||||
|
{
|
||||||
|
char *s = _this->m_literal_strings.Get(x);
|
||||||
|
if (!strcmp(s,ns))
|
||||||
|
{
|
||||||
|
free(ns);
|
||||||
|
return x + LITERAL_BASE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_this->m_literal_strings.Add(ns);
|
||||||
|
return nstr + LITERAL_BASE;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *GetString(EEL_F v)
|
||||||
|
{
|
||||||
|
if (v >= LITERAL_BASE && v < LITERAL_BASE + m_literal_strings.GetSize())
|
||||||
|
return m_literal_strings.Get((int) (v - LITERAL_BASE));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int eel_validate_format_specifier(const char *fmt_in, char *typeOut,
|
||||||
|
char *fmtOut, int fmtOut_sz,
|
||||||
|
char *varOut, int varOut_sz,
|
||||||
|
int *varOut_used
|
||||||
|
)
|
||||||
|
{
|
||||||
|
const char *fmt = fmt_in+1;
|
||||||
|
int state=0;
|
||||||
|
if (fmt_in[0] != '%') return 0; // ugh passed a non-specifier
|
||||||
|
|
||||||
|
*varOut_used = 0;
|
||||||
|
*varOut = 0;
|
||||||
|
|
||||||
|
if (fmtOut_sz-- < 2) return 0;
|
||||||
|
*fmtOut++ = '%';
|
||||||
|
|
||||||
|
while (*fmt)
|
||||||
|
{
|
||||||
|
const char c = *fmt++;
|
||||||
|
if (fmtOut_sz < 2) return 0;
|
||||||
|
|
||||||
|
if (c == 'f'|| c=='e' || c=='E' || c=='g' || c=='G' || c == 'd' || c == 'u' ||
|
||||||
|
c == 'x' || c == 'X' || c == 'c' || c == 'C' || c =='s' || c=='S' || c=='i')
|
||||||
|
{
|
||||||
|
*typeOut = c;
|
||||||
|
fmtOut[0] = c;
|
||||||
|
fmtOut[1] = 0;
|
||||||
|
return (int) (fmt - fmt_in);
|
||||||
|
}
|
||||||
|
else if (c == '.')
|
||||||
|
{
|
||||||
|
*fmtOut++ = c; fmtOut_sz--;
|
||||||
|
if (state&(2)) break;
|
||||||
|
state |= 2;
|
||||||
|
}
|
||||||
|
else if (c == '+')
|
||||||
|
{
|
||||||
|
*fmtOut++ = c; fmtOut_sz--;
|
||||||
|
if (state&(32|16|8|4)) break;
|
||||||
|
state |= 8;
|
||||||
|
}
|
||||||
|
else if (c == '-' || c == ' ')
|
||||||
|
{
|
||||||
|
*fmtOut++ = c; fmtOut_sz--;
|
||||||
|
if (state&(32|16|8|4)) break;
|
||||||
|
state |= 16;
|
||||||
|
}
|
||||||
|
else if (c >= '0' && c <= '9')
|
||||||
|
{
|
||||||
|
*fmtOut++ = c; fmtOut_sz--;
|
||||||
|
state|=4;
|
||||||
|
}
|
||||||
|
else if (c == '{')
|
||||||
|
{
|
||||||
|
if (state & 64) break;
|
||||||
|
state|=64;
|
||||||
|
if (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) return 0; // symbol name can't start with 0-9 or .
|
||||||
|
|
||||||
|
while (*fmt != '}')
|
||||||
|
{
|
||||||
|
if ((*fmt >= 'a' && *fmt <= 'z') ||
|
||||||
|
(*fmt >= 'A' && *fmt <= 'Z') ||
|
||||||
|
(*fmt >= '0' && *fmt <= '9') ||
|
||||||
|
*fmt == '_' || *fmt == '.' || *fmt == '#')
|
||||||
|
{
|
||||||
|
if (varOut_sz < 2) return 0;
|
||||||
|
*varOut++ = *fmt++;
|
||||||
|
varOut_sz -- ;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0; // bad character in variable name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt++;
|
||||||
|
*varOut = 0;
|
||||||
|
*varOut_used=1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int eel_format_strings(void *opaque, const char *fmt, const char *fmt_end, char *buf, int buf_sz, int num_fmt_parms, EEL_F **fmt_parms)
|
||||||
|
{
|
||||||
|
EEL2_PreProcessor *_this = (EEL2_PreProcessor*)opaque;
|
||||||
|
int fmt_parmpos = 0;
|
||||||
|
char *op = buf;
|
||||||
|
while ((fmt_end ? fmt < fmt_end : *fmt) && op < buf+buf_sz-128)
|
||||||
|
{
|
||||||
|
if (fmt[0] == '%' && fmt[1] == '%')
|
||||||
|
{
|
||||||
|
*op++ = '%';
|
||||||
|
fmt+=2;
|
||||||
|
}
|
||||||
|
else if (fmt[0] == '%')
|
||||||
|
{
|
||||||
|
char ct=0;
|
||||||
|
char fs[128];
|
||||||
|
char varname[128];
|
||||||
|
int varname_used=0;
|
||||||
|
const int l=eel_validate_format_specifier(fmt,&ct,fs,sizeof(fs),varname,sizeof(varname),&varname_used);
|
||||||
|
if (!l || !ct)
|
||||||
|
{
|
||||||
|
*op=0;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const EEL_F *varptr = NULL;
|
||||||
|
if (!varname_used)
|
||||||
|
{
|
||||||
|
if (fmt_parmpos < num_fmt_parms) varptr = fmt_parms[fmt_parmpos];
|
||||||
|
fmt_parmpos++;
|
||||||
|
}
|
||||||
|
double v = varptr ? (double)*varptr : 0.0;
|
||||||
|
|
||||||
|
if (ct == 's' || ct=='S')
|
||||||
|
{
|
||||||
|
const char *str = _this->GetString(v);
|
||||||
|
const int maxl=(int) (buf+buf_sz - 2 - op);
|
||||||
|
snprintf(op,maxl,fs,str ? str : "");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (ct == 'x' || ct == 'X' || ct == 'd' || ct == 'u' || ct=='i')
|
||||||
|
{
|
||||||
|
snprintf(op,64,fs,(int) (v));
|
||||||
|
}
|
||||||
|
else if (ct == 'c')
|
||||||
|
{
|
||||||
|
*op++=(char) (int)v;
|
||||||
|
*op=0;
|
||||||
|
}
|
||||||
|
else if (ct == 'C')
|
||||||
|
{
|
||||||
|
const unsigned int iv = (unsigned int) v;
|
||||||
|
int bs = 0;
|
||||||
|
if (iv & 0xff000000) bs=24;
|
||||||
|
else if (iv & 0x00ff0000) bs=16;
|
||||||
|
else if (iv & 0x0000ff00) bs=8;
|
||||||
|
while (bs>=0)
|
||||||
|
{
|
||||||
|
const char c=(char) (iv>>bs);
|
||||||
|
*op++=c?c:' ';
|
||||||
|
bs-=8;
|
||||||
|
}
|
||||||
|
*op=0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
snprintf(op,64,fs,v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (*op) op++;
|
||||||
|
|
||||||
|
fmt += l;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
*op++ = *fmt++;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
*op=0;
|
||||||
|
return (int) (op - buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F NSEEL_CGEN_CALL pp_printf(void *opaque, INT_PTR num_param, EEL_F **parms)
|
||||||
|
{
|
||||||
|
if (num_param>0 && opaque)
|
||||||
|
{
|
||||||
|
EEL2_PreProcessor *_this = (EEL2_PreProcessor*)opaque;
|
||||||
|
const char *fmt = _this->GetString(parms[0][0]);
|
||||||
|
if (fmt)
|
||||||
|
{
|
||||||
|
char buf[16384];
|
||||||
|
const int len = eel_format_strings(opaque,fmt,NULL,buf,(int)sizeof(buf), (int)num_param-1, parms+1);
|
||||||
|
|
||||||
|
if (len >= 0)
|
||||||
|
{
|
||||||
|
if (_this->m_fsout && _this->m_fsout->GetLength() < _this->m_max_sz)
|
||||||
|
{
|
||||||
|
_this->m_fsout->Append(buf,len);
|
||||||
|
}
|
||||||
|
return 1.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static EEL_F NSEEL_CGEN_CALL pp_include(void *opaque, INT_PTR num_param, EEL_F **parms)
|
||||||
|
{
|
||||||
|
if (num_param>0 && opaque)
|
||||||
|
{
|
||||||
|
EEL2_PreProcessor *_this = (EEL2_PreProcessor*)opaque;
|
||||||
|
if (_this->m_cur_depth >= _this->m_max_include_depth) return -1.0;
|
||||||
|
|
||||||
|
const char *fn = _this->GetString(parms[0][0]);
|
||||||
|
if (!fn || !*fn) return -2.0;
|
||||||
|
|
||||||
|
WDL_FastString fullfn;
|
||||||
|
for (int x = _this->m_include_paths.GetSize()-1; x >= 0; x--)
|
||||||
|
{
|
||||||
|
const char *p = _this->m_include_paths.Get(x);
|
||||||
|
if (p && *p)
|
||||||
|
{
|
||||||
|
fullfn.Set(p);
|
||||||
|
fullfn.Append(WDL_DIRCHAR_STR);
|
||||||
|
fullfn.Append(fn);
|
||||||
|
FILE *fp = fopenUTF8(fullfn.Get(),"rb");
|
||||||
|
if (fp)
|
||||||
|
{
|
||||||
|
double rv = 0.0;
|
||||||
|
_this->m_cur_depth++;
|
||||||
|
fullfn.Set("");
|
||||||
|
while (fullfn.GetLength() < (4<<20))
|
||||||
|
{
|
||||||
|
char buf[512];
|
||||||
|
if (!fgets(buf,sizeof(buf),fp)) break;
|
||||||
|
fullfn.Append(buf);
|
||||||
|
}
|
||||||
|
fclose(fp);
|
||||||
|
|
||||||
|
WDL_FastString *outp = _this->m_fsout;
|
||||||
|
if (_this->preprocess(fullfn.Get(),outp))
|
||||||
|
{
|
||||||
|
rv = -3.0;
|
||||||
|
}
|
||||||
|
_this->m_fsout = outp;
|
||||||
|
|
||||||
|
_this->m_cur_depth--;
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -4.0;
|
||||||
|
}
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
eel_function_table EEL2_PreProcessor::m_ftab;
|
||||||
|
|
||||||
|
#endif
|
||||||
1668
oversampling/WDL/eel2/eel_strings.h
Normal file
1668
oversampling/WDL/eel2/eel_strings.h
Normal file
File diff suppressed because it is too large
Load Diff
834
oversampling/WDL/eel2/eelscript.h
Normal file
834
oversampling/WDL/eel2/eelscript.h
Normal file
@@ -0,0 +1,834 @@
|
|||||||
|
#ifndef _WIN32
|
||||||
|
#include <unistd.h>
|
||||||
|
#ifndef EELSCRIPT_NO_LICE
|
||||||
|
#include "../swell/swell.h"
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "../wdltypes.h"
|
||||||
|
#include "../ptrlist.h"
|
||||||
|
#include "../wdlstring.h"
|
||||||
|
#include "../assocarray.h"
|
||||||
|
#include "../queue.h"
|
||||||
|
#include "../mutex.h"
|
||||||
|
#include "../win32_utf8.h"
|
||||||
|
#include "ns-eel.h"
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef EELSCRIPT_MAX_FILE_HANDLES
|
||||||
|
#define EELSCRIPT_MAX_FILE_HANDLES 512
|
||||||
|
#endif
|
||||||
|
#ifndef EELSCRIPT_FILE_HANDLE_INDEX_BASE
|
||||||
|
#define EELSCRIPT_FILE_HANDLE_INDEX_BASE 1000000
|
||||||
|
#endif
|
||||||
|
#ifndef EEL_STRING_MAXUSERSTRING_LENGTH_HINT
|
||||||
|
#define EEL_STRING_MAXUSERSTRING_LENGTH_HINT (1<<16) // 64KB per string max
|
||||||
|
#endif
|
||||||
|
#ifndef EEL_STRING_MAX_USER_STRINGS
|
||||||
|
#define EEL_STRING_MAX_USER_STRINGS 32768
|
||||||
|
#endif
|
||||||
|
#ifndef EEL_STRING_LITERAL_BASE
|
||||||
|
#define EEL_STRING_LITERAL_BASE 2000000
|
||||||
|
#endif
|
||||||
|
#ifndef EELSCRIPT_LICE_MAX_IMAGES
|
||||||
|
#define EELSCRIPT_LICE_MAX_IMAGES 1024
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef EELSCRIPT_LICE_MAX_FONTS
|
||||||
|
#define EELSCRIPT_LICE_MAX_FONTS 128
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef EELSCRIPT_NET_MAXCON
|
||||||
|
#define EELSCRIPT_NET_MAXCON 4096
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef EELSCRIPT_LICE_CLASSNAME
|
||||||
|
#define EELSCRIPT_LICE_CLASSNAME "eelscript_gfx"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
// #define EELSCRIPT_NO_NET
|
||||||
|
// #define EELSCRIPT_NO_LICE
|
||||||
|
// #define EELSCRIPT_NO_FILE
|
||||||
|
// #define EELSCRIPT_NO_FFT
|
||||||
|
// #define EELSCRIPT_NO_MDCT
|
||||||
|
// #define EELSCRIPT_NO_EVAL
|
||||||
|
|
||||||
|
class eel_string_context_state;
|
||||||
|
#ifndef EELSCRIPT_NO_NET
|
||||||
|
class eel_net_state;
|
||||||
|
#endif
|
||||||
|
#ifndef EELSCRIPT_NO_LICE
|
||||||
|
class eel_lice_state;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef EELSCRIPT_NO_PREPROC
|
||||||
|
#include "eel_pproc.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
class eelScriptInst {
|
||||||
|
public:
|
||||||
|
|
||||||
|
static int init();
|
||||||
|
|
||||||
|
eelScriptInst();
|
||||||
|
virtual ~eelScriptInst();
|
||||||
|
|
||||||
|
NSEEL_CODEHANDLE compile_code(const char *code, const char **err);
|
||||||
|
int runcode(const char *code, int showerr, const char *showerrfn, bool canfree, bool ignoreEndOfInputChk, bool doExec);
|
||||||
|
int loadfile(const char *fn, const char *callerfn, bool allowstdin);
|
||||||
|
|
||||||
|
NSEEL_VMCTX m_vm;
|
||||||
|
|
||||||
|
WDL_PtrList<void> m_code_freelist;
|
||||||
|
|
||||||
|
#ifndef EELSCRIPT_NO_FILE
|
||||||
|
FILE *m_handles[EELSCRIPT_MAX_FILE_HANDLES];
|
||||||
|
virtual EEL_F OpenFile(const char *fn, const char *mode)
|
||||||
|
{
|
||||||
|
if (!*fn || !*mode) return 0.0;
|
||||||
|
#ifndef EELSCRIPT_NO_STDIO
|
||||||
|
if (!strcmp(fn,"stdin")) return 1;
|
||||||
|
if (!strcmp(fn,"stdout")) return 2;
|
||||||
|
if (!strcmp(fn,"stderr")) return 3;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
WDL_FastString fnstr(fn);
|
||||||
|
if (!translateFilename(&fnstr,mode)) return 0.0;
|
||||||
|
|
||||||
|
int x;
|
||||||
|
for (x=0;x<EELSCRIPT_MAX_FILE_HANDLES && m_handles[x];x++);
|
||||||
|
if (x>= EELSCRIPT_MAX_FILE_HANDLES) return 0.0;
|
||||||
|
|
||||||
|
FILE *fp = fopenUTF8(fnstr.Get(),mode);
|
||||||
|
if (!fp) return 0.0;
|
||||||
|
m_handles[x]=fp;
|
||||||
|
return x + EELSCRIPT_FILE_HANDLE_INDEX_BASE;
|
||||||
|
}
|
||||||
|
virtual EEL_F CloseFile(int fp_idx)
|
||||||
|
{
|
||||||
|
fp_idx-=EELSCRIPT_FILE_HANDLE_INDEX_BASE;
|
||||||
|
if (fp_idx>=0 && fp_idx<EELSCRIPT_MAX_FILE_HANDLES && m_handles[fp_idx])
|
||||||
|
{
|
||||||
|
fclose(m_handles[fp_idx]);
|
||||||
|
m_handles[fp_idx]=0;
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
return -1.0;
|
||||||
|
}
|
||||||
|
virtual FILE *GetFileFP(int fp_idx)
|
||||||
|
{
|
||||||
|
#ifndef EELSCRIPT_NO_STDIO
|
||||||
|
if (fp_idx==1) return stdin;
|
||||||
|
if (fp_idx==2) return stdout;
|
||||||
|
if (fp_idx==3) return stderr;
|
||||||
|
#endif
|
||||||
|
fp_idx-=EELSCRIPT_FILE_HANDLE_INDEX_BASE;
|
||||||
|
if (fp_idx>=0 && fp_idx<EELSCRIPT_MAX_FILE_HANDLES) return m_handles[fp_idx];
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
virtual bool translateFilename(WDL_FastString *fs, const char *mode) { return true; }
|
||||||
|
|
||||||
|
virtual bool GetFilenameForParameter(EEL_F idx, WDL_FastString *fs, int iswrite);
|
||||||
|
|
||||||
|
eel_string_context_state *m_string_context;
|
||||||
|
#ifndef EELSCRIPT_NO_NET
|
||||||
|
eel_net_state *m_net_state;
|
||||||
|
#endif
|
||||||
|
#ifndef EELSCRIPT_NO_LICE
|
||||||
|
eel_lice_state *m_gfx_state;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef EELSCRIPT_NO_EVAL
|
||||||
|
struct evalCacheEnt {
|
||||||
|
char *str;
|
||||||
|
NSEEL_CODEHANDLE ch;
|
||||||
|
};
|
||||||
|
int m_eval_depth;
|
||||||
|
WDL_TypedBuf<evalCacheEnt> m_eval_cache;
|
||||||
|
virtual char *evalCacheGet(const char *str, NSEEL_CODEHANDLE *ch);
|
||||||
|
virtual void evalCacheDispose(char *key, NSEEL_CODEHANDLE ch);
|
||||||
|
WDL_Queue m_defer_eval, m_atexit_eval;
|
||||||
|
void runCodeQ(WDL_Queue *q, const char *fname);
|
||||||
|
void runAtExitCode()
|
||||||
|
{
|
||||||
|
runCodeQ(&m_atexit_eval,"atexit");
|
||||||
|
m_atexit_eval.Clear(); // make sure nothing gets added in atexit(), in case the user called runAtExitCode before destroying
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
virtual bool run_deferred(); // requires eval support to be useful
|
||||||
|
virtual bool has_deferred();
|
||||||
|
|
||||||
|
|
||||||
|
WDL_StringKeyedArray<bool> m_loaded_fnlist; // imported file list (to avoid repeats)
|
||||||
|
|
||||||
|
#ifndef EELSCRIPT_NO_PREPROC
|
||||||
|
EEL2_PreProcessor m_preproc;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
//#define EEL_STRINGS_MUTABLE_LITERALS
|
||||||
|
//#define EEL_STRING_WANT_MUTEX
|
||||||
|
|
||||||
|
|
||||||
|
#define EEL_STRING_GET_CONTEXT_POINTER(opaque) (((eelScriptInst *)opaque)->m_string_context)
|
||||||
|
#ifndef EEL_STRING_STDOUT_WRITE
|
||||||
|
#ifndef EELSCRIPT_NO_STDIO
|
||||||
|
#define EEL_STRING_STDOUT_WRITE(x,len) { fwrite(x,len,1,stdout); fflush(stdout); }
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
#include "eel_strings.h"
|
||||||
|
|
||||||
|
#include "eel_misc.h"
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef EELSCRIPT_NO_FILE
|
||||||
|
#define EEL_FILE_OPEN(fn,mode) ((eelScriptInst*)opaque)->OpenFile(fn,mode)
|
||||||
|
#define EEL_FILE_GETFP(fp) ((eelScriptInst*)opaque)->GetFileFP(fp)
|
||||||
|
#define EEL_FILE_CLOSE(fpindex) ((eelScriptInst*)opaque)->CloseFile(fpindex)
|
||||||
|
|
||||||
|
#include "eel_files.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef EELSCRIPT_NO_FFT
|
||||||
|
#include "eel_fft.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef EELSCRIPT_NO_MDCT
|
||||||
|
#include "eel_mdct.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef EELSCRIPT_NO_NET
|
||||||
|
#define EEL_NET_GET_CONTEXT(opaque) (((eelScriptInst *)opaque)->m_net_state)
|
||||||
|
#include "eel_net.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef EELSCRIPT_NO_LICE
|
||||||
|
#ifndef EEL_LICE_WANT_STANDALONE
|
||||||
|
#define EEL_LICE_WANT_STANDALONE
|
||||||
|
#endif
|
||||||
|
#ifndef EELSCRIPT_LICE_NOUPDATE
|
||||||
|
#define EEL_LICE_WANT_STANDALONE_UPDATE // gfx_update() which runs message pump and updates screen etc
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define EEL_LICE_GET_FILENAME_FOR_STRING(idx, fs, p) (((eelScriptInst*)opaque)->GetFilenameForParameter(idx,fs,p))
|
||||||
|
#define EEL_LICE_GET_CONTEXT(opaque) ((opaque) ? (((eelScriptInst *)opaque)->m_gfx_state) : NULL)
|
||||||
|
#include "eel_lice.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef EELSCRIPT_NO_EVAL
|
||||||
|
#define EEL_EVAL_GET_CACHED(str, ch) ((eelScriptInst *)opaque)->evalCacheGet(str,&(ch))
|
||||||
|
#define EEL_EVAL_SET_CACHED(str, ch) ((eelScriptInst *)opaque)->evalCacheDispose(str,ch)
|
||||||
|
#define EEL_EVAL_GET_VMCTX(opaque) (((eelScriptInst *)opaque)->m_vm)
|
||||||
|
#define EEL_EVAL_SCOPE_ENTER (((eelScriptInst *)opaque)->m_eval_depth < 3 ? \
|
||||||
|
++((eelScriptInst *)opaque)->m_eval_depth : 0)
|
||||||
|
#define EEL_EVAL_SCOPE_LEAVE ((eelScriptInst *)opaque)->m_eval_depth--;
|
||||||
|
#include "eel_eval.h"
|
||||||
|
|
||||||
|
static EEL_F NSEEL_CGEN_CALL _eel_defer(void *opaque, EEL_F *s)
|
||||||
|
{
|
||||||
|
EEL_STRING_MUTEXLOCK_SCOPE
|
||||||
|
const char *str=EEL_STRING_GET_FOR_INDEX(*s,NULL);
|
||||||
|
if (str && *str && *s >= EEL_STRING_MAX_USER_STRINGS) // don't allow defer(0) etc
|
||||||
|
{
|
||||||
|
eelScriptInst *inst = (eelScriptInst *)opaque;
|
||||||
|
if (inst->m_defer_eval.Available() < EEL_STRING_MAXUSERSTRING_LENGTH_HINT)
|
||||||
|
{
|
||||||
|
inst->m_defer_eval.Add(str,strlen(str)+1);
|
||||||
|
return 1.0;
|
||||||
|
}
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("defer(): too much defer() code already added, ignoring");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
else if (!str)
|
||||||
|
{
|
||||||
|
EEL_STRING_DEBUGOUT("defer(): invalid string identifier specified %f",*s);
|
||||||
|
}
|
||||||
|
else if (*s < EEL_STRING_MAX_USER_STRINGS)
|
||||||
|
{
|
||||||
|
EEL_STRING_DEBUGOUT("defer(): user string identifier %f specified but not allowed",*s);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
static EEL_F NSEEL_CGEN_CALL _eel_atexit(void *opaque, EEL_F *s)
|
||||||
|
{
|
||||||
|
EEL_STRING_MUTEXLOCK_SCOPE
|
||||||
|
const char *str=EEL_STRING_GET_FOR_INDEX(*s,NULL);
|
||||||
|
if (str && *str && *s >= EEL_STRING_MAX_USER_STRINGS) // don't allow atexit(0) etc
|
||||||
|
{
|
||||||
|
eelScriptInst *inst = (eelScriptInst *)opaque;
|
||||||
|
if (inst->m_atexit_eval.Available() < EEL_STRING_MAXUSERSTRING_LENGTH_HINT)
|
||||||
|
{
|
||||||
|
inst->m_atexit_eval.Add(str,strlen(str)+1);
|
||||||
|
return 1.0;
|
||||||
|
}
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("atexit(): too much atexit() code already added, ignoring");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
else if (!str)
|
||||||
|
{
|
||||||
|
EEL_STRING_DEBUGOUT("atexit(): invalid string identifier specified %f",*s);
|
||||||
|
}
|
||||||
|
else if (*s < EEL_STRING_MAX_USER_STRINGS)
|
||||||
|
{
|
||||||
|
EEL_STRING_DEBUGOUT("atexit(): user string identifier %f specified but not allowed",*s);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#define opaque ((void *)this)
|
||||||
|
|
||||||
|
eelScriptInst::eelScriptInst() : m_loaded_fnlist(false)
|
||||||
|
{
|
||||||
|
#ifndef EELSCRIPT_NO_FILE
|
||||||
|
memset(m_handles,0,sizeof(m_handles));
|
||||||
|
#endif
|
||||||
|
m_vm = NSEEL_VM_alloc();
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
if (!m_vm) EEL_STRING_DEBUGOUT("NSEEL_VM_alloc(): failed");
|
||||||
|
#endif
|
||||||
|
NSEEL_VM_SetCustomFuncThis(m_vm,this);
|
||||||
|
#ifdef NSEEL_ADDFUNC_DESTINATION
|
||||||
|
NSEEL_VM_SetFunctionTable(m_vm,NSEEL_ADDFUNC_DESTINATION);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
m_string_context = new eel_string_context_state;
|
||||||
|
eel_string_initvm(m_vm);
|
||||||
|
#ifndef EELSCRIPT_NO_NET
|
||||||
|
m_net_state = new eel_net_state(EELSCRIPT_NET_MAXCON,NULL);
|
||||||
|
#endif
|
||||||
|
#ifndef EELSCRIPT_NO_LICE
|
||||||
|
m_gfx_state = new eel_lice_state(m_vm,this,EELSCRIPT_LICE_MAX_IMAGES,EELSCRIPT_LICE_MAX_FONTS);
|
||||||
|
|
||||||
|
m_gfx_state->resetVarsToStock();
|
||||||
|
#endif
|
||||||
|
#ifndef EELSCRIPT_NO_EVAL
|
||||||
|
m_eval_depth=0;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
eelScriptInst::~eelScriptInst()
|
||||||
|
{
|
||||||
|
#ifndef EELSCRIPT_NO_EVAL
|
||||||
|
if (m_atexit_eval.GetSize()>0) runAtExitCode();
|
||||||
|
#endif
|
||||||
|
int x;
|
||||||
|
m_code_freelist.Empty((void (*)(void *))NSEEL_code_free);
|
||||||
|
#ifndef EELSCRIPT_NO_EVAL
|
||||||
|
for (x=0;x<m_eval_cache.GetSize();x++)
|
||||||
|
{
|
||||||
|
free(m_eval_cache.Get()[x].str);
|
||||||
|
NSEEL_code_free(m_eval_cache.Get()[x].ch);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (m_vm) NSEEL_VM_free(m_vm);
|
||||||
|
|
||||||
|
#ifndef EELSCRIPT_NO_FILE
|
||||||
|
for (x=0;x<EELSCRIPT_MAX_FILE_HANDLES;x++)
|
||||||
|
{
|
||||||
|
if (m_handles[x]) fclose(m_handles[x]);
|
||||||
|
m_handles[x]=0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
delete m_string_context;
|
||||||
|
#ifndef EELSCRIPT_NO_NET
|
||||||
|
delete m_net_state;
|
||||||
|
#endif
|
||||||
|
#ifndef EELSCRIPT_NO_LICE
|
||||||
|
delete m_gfx_state;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
bool eelScriptInst::GetFilenameForParameter(EEL_F idx, WDL_FastString *fs, int iswrite)
|
||||||
|
{
|
||||||
|
const char *fmt = EEL_STRING_GET_FOR_INDEX(idx,NULL);
|
||||||
|
if (!fmt) return false;
|
||||||
|
fs->Set(fmt);
|
||||||
|
return translateFilename(fs,iswrite?"w":"r");
|
||||||
|
}
|
||||||
|
|
||||||
|
NSEEL_CODEHANDLE eelScriptInst::compile_code(const char *code, const char **err)
|
||||||
|
{
|
||||||
|
if (!m_vm)
|
||||||
|
{
|
||||||
|
*err = "EEL VM not initialized";
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef EELSCRIPT_NO_PREPROC
|
||||||
|
WDL_FastString str;
|
||||||
|
if (strstr(code,EEL2_PREPROCESS_OPEN_TOKEN))
|
||||||
|
{
|
||||||
|
const char *pperr = m_preproc.preprocess(code,&str);
|
||||||
|
if (pperr)
|
||||||
|
{
|
||||||
|
*err = pperr;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
code = str.Get();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
m_preproc.clear_line_info();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
NSEEL_CODEHANDLE ch = NSEEL_code_compile_ex(m_vm, code, 0, NSEEL_CODE_COMPILE_FLAG_COMMONFUNCS);
|
||||||
|
if (ch)
|
||||||
|
{
|
||||||
|
m_string_context->update_named_vars(m_vm);
|
||||||
|
m_code_freelist.Add((void*)ch);
|
||||||
|
return ch;
|
||||||
|
}
|
||||||
|
*err = NSEEL_code_getcodeerror(m_vm);
|
||||||
|
#ifndef EELSCRIPT_NO_PREPROC
|
||||||
|
if (*err) *err = m_preproc.translate_error_line(*err);
|
||||||
|
#endif
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int eelScriptInst::runcode(const char *codeptr, int showerr, const char *showerrfn, bool canfree, bool ignoreEndOfInputChk, bool doExec)
|
||||||
|
{
|
||||||
|
if (m_vm)
|
||||||
|
{
|
||||||
|
const char *err = NULL;
|
||||||
|
NSEEL_CODEHANDLE code = NULL;
|
||||||
|
#ifndef EELSCRIPT_NO_PREPROC
|
||||||
|
WDL_FastString str;
|
||||||
|
if (strstr(codeptr,EEL2_PREPROCESS_OPEN_TOKEN))
|
||||||
|
{
|
||||||
|
err = m_preproc.preprocess(codeptr,&str);
|
||||||
|
if (err) goto on_preproc_error;
|
||||||
|
codeptr = str.Get();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
m_preproc.clear_line_info();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
code = NSEEL_code_compile_ex(m_vm,codeptr,0,canfree ? 0 : NSEEL_CODE_COMPILE_FLAG_COMMONFUNCS);
|
||||||
|
if (code) m_string_context->update_named_vars(m_vm);
|
||||||
|
|
||||||
|
if (!code && (err=NSEEL_code_getcodeerror(m_vm)))
|
||||||
|
{
|
||||||
|
if (!ignoreEndOfInputChk && (NSEEL_code_geterror_flag(m_vm)&1)) return 1;
|
||||||
|
#ifndef EELSCRIPT_NO_PREPROC
|
||||||
|
err = m_preproc.translate_error_line(err);
|
||||||
|
on_preproc_error:
|
||||||
|
#endif
|
||||||
|
if (showerr)
|
||||||
|
{
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
if (showerr==2)
|
||||||
|
{
|
||||||
|
EEL_STRING_DEBUGOUT("Warning: %s:%s",WDL_get_filepart(showerrfn),err);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
EEL_STRING_DEBUGOUT("%s:%s",WDL_get_filepart(showerrfn),err);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (code)
|
||||||
|
{
|
||||||
|
#ifdef EELSCRIPT_DO_DISASSEMBLE
|
||||||
|
codeHandleType *p = (codeHandleType*)code;
|
||||||
|
|
||||||
|
char buf[512];
|
||||||
|
buf[0]=0;
|
||||||
|
#ifdef _WIN32
|
||||||
|
GetTempPath(sizeof(buf)-64,buf);
|
||||||
|
lstrcatn(buf,"jsfx-out",sizeof(buf));
|
||||||
|
#else
|
||||||
|
lstrcpyn_safe(buf,"/tmp/jsfx-out",sizeof(buf));
|
||||||
|
#endif
|
||||||
|
FILE *fp = fopenUTF8(buf,"wb");
|
||||||
|
if (fp)
|
||||||
|
{
|
||||||
|
fwrite(p->code,1,p->code_size,fp);
|
||||||
|
fclose(fp);
|
||||||
|
char buf2[2048];
|
||||||
|
#ifdef _WIN32
|
||||||
|
snprintf(buf2,sizeof(buf2),"disasm \"%s\"",buf);
|
||||||
|
#else
|
||||||
|
#ifdef __aarch64__
|
||||||
|
snprintf(buf2,sizeof(buf2), "objdump -D -b binary -maarch64 \"%s\"",buf);
|
||||||
|
#elif defined(__arm__)
|
||||||
|
snprintf(buf2,sizeof(buf2), "objdump -D -b binary -m arm \"%s\"",buf);
|
||||||
|
#elif defined(__LP64__)
|
||||||
|
#ifdef __APPLE__
|
||||||
|
snprintf(buf2,sizeof(buf2),"distorm3 --b64 \"%s\"",buf);
|
||||||
|
#else
|
||||||
|
snprintf(buf2,sizeof(buf2),"objdump -D -b binary -m i386:x86-64 \"%s\"",buf);
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
snprintf(buf2,sizeof(buf2),"distorm3 --b32 \"%s\"",buf);
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
system(buf2);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (doExec) NSEEL_code_execute(code);
|
||||||
|
if (canfree) NSEEL_code_free(code);
|
||||||
|
else m_code_freelist.Add((void*)code);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
FILE *eelscript_resolvePath(WDL_FastString &usefn, const char *fn, const char *callerfn)
|
||||||
|
{
|
||||||
|
// resolve path relative to current
|
||||||
|
int x;
|
||||||
|
bool had_abs=false;
|
||||||
|
for (x=0;x<2; x ++)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
if (!x && ((fn[0] == '\\' && fn[1] == '\\') || (fn[0] && fn[1] == ':')))
|
||||||
|
#else
|
||||||
|
if (!x && fn[0] == '/')
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
usefn.Set(fn);
|
||||||
|
had_abs=true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
const char *fnu = fn;
|
||||||
|
if (x)
|
||||||
|
{
|
||||||
|
while (*fnu) fnu++;
|
||||||
|
while (fnu >= fn && *fnu != '\\' && *fnu != '/') fnu--;
|
||||||
|
if (fnu < fn) break;
|
||||||
|
fnu++;
|
||||||
|
}
|
||||||
|
|
||||||
|
usefn.Set(callerfn);
|
||||||
|
int l=usefn.GetLength();
|
||||||
|
while (l > 0 && usefn.Get()[l-1] != '\\' && usefn.Get()[l-1] != '/') l--;
|
||||||
|
if (l > 0)
|
||||||
|
{
|
||||||
|
usefn.SetLen(l);
|
||||||
|
usefn.Append(fnu);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
usefn.Set(fnu);
|
||||||
|
}
|
||||||
|
int last_slash_pos=-1;
|
||||||
|
for (l = 0; l < usefn.GetLength(); l ++)
|
||||||
|
{
|
||||||
|
if (usefn.Get()[l] == '/' || usefn.Get()[l] == '\\')
|
||||||
|
{
|
||||||
|
if (usefn.Get()[l+1] == '.' && usefn.Get()[l+2] == '.' &&
|
||||||
|
(usefn.Get()[l+3] == '/' || usefn.Get()[l+3] == '\\'))
|
||||||
|
{
|
||||||
|
if (last_slash_pos >= 0)
|
||||||
|
usefn.DeleteSub(last_slash_pos, l+3-last_slash_pos);
|
||||||
|
else
|
||||||
|
usefn.DeleteSub(0,l+3+1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
last_slash_pos=l;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// take currentfn, remove filename part, add fnu
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE *fp = fopenUTF8(usefn.Get(),"r");
|
||||||
|
if (fp) return fp;
|
||||||
|
}
|
||||||
|
if (had_abs) usefn.Set(fn);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int eelScriptInst::loadfile(const char *fn, const char *callerfn, bool allowstdin)
|
||||||
|
{
|
||||||
|
WDL_FastString usefn;
|
||||||
|
FILE *fp = NULL;
|
||||||
|
if (!strcmp(fn,"-"))
|
||||||
|
{
|
||||||
|
if (callerfn)
|
||||||
|
{
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
EEL_STRING_DEBUGOUT("@import: can't import \"-\" (stdin)");
|
||||||
|
#endif
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (allowstdin)
|
||||||
|
{
|
||||||
|
fp = stdin;
|
||||||
|
fn = "(stdin)";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (!callerfn)
|
||||||
|
{
|
||||||
|
fp = fopenUTF8(fn,"r");
|
||||||
|
if (fp) m_loaded_fnlist.Insert(fn,true);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fp = eelscript_resolvePath(usefn,fn,callerfn);
|
||||||
|
if (fp)
|
||||||
|
{
|
||||||
|
if (m_loaded_fnlist.Get(usefn.Get()))
|
||||||
|
{
|
||||||
|
fclose(fp);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
m_loaded_fnlist.Insert(usefn.Get(),true);
|
||||||
|
fn = usefn.Get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!fp)
|
||||||
|
{
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
if (callerfn)
|
||||||
|
EEL_STRING_DEBUGOUT("Warning: @import could not open '%s'",fn);
|
||||||
|
else
|
||||||
|
EEL_STRING_DEBUGOUT("Error opening %s",fn);
|
||||||
|
#endif
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef EELSCRIPT_NO_PREPROC
|
||||||
|
WDL_FastString incpath(fn);
|
||||||
|
incpath.remove_filepart();
|
||||||
|
m_preproc.m_include_paths.Add(incpath.Get());
|
||||||
|
#endif
|
||||||
|
|
||||||
|
WDL_FastString code;
|
||||||
|
char line[4096];
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
line[0]=0;
|
||||||
|
fgets(line,sizeof(line),fp);
|
||||||
|
if (!line[0]) break;
|
||||||
|
if (!strnicmp(line,"@import",7) && isspace((unsigned char)line[7]))
|
||||||
|
{
|
||||||
|
char *p=line+7;
|
||||||
|
while (isspace((unsigned char)*p)) p++;
|
||||||
|
|
||||||
|
char *ep=p;
|
||||||
|
while (*ep) ep++;
|
||||||
|
while (ep>p && isspace((unsigned char)ep[-1])) ep--;
|
||||||
|
*ep=0;
|
||||||
|
|
||||||
|
if (*p) loadfile(p,fn,false);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
code.Append(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (fp != stdin) fclose(fp);
|
||||||
|
|
||||||
|
int rv = runcode(code.Get(),callerfn ? 2 : 1, fn,false,true,!callerfn);
|
||||||
|
|
||||||
|
#ifndef EELSCRIPT_NO_PREPROC
|
||||||
|
m_preproc.m_include_paths.Delete(m_preproc.m_include_paths.GetSize()-1);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *eelScriptInst::evalCacheGet(const char *str, NSEEL_CODEHANDLE *ch)
|
||||||
|
{
|
||||||
|
// should mutex protect if multiple threads access this eelScriptInst context
|
||||||
|
int x=m_eval_cache.GetSize();
|
||||||
|
while (--x >= 0)
|
||||||
|
{
|
||||||
|
char *ret;
|
||||||
|
if (!strcmp(ret=m_eval_cache.Get()[x].str, str))
|
||||||
|
{
|
||||||
|
*ch = m_eval_cache.Get()[x].ch;
|
||||||
|
m_eval_cache.Delete(x);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void eelScriptInst::evalCacheDispose(char *key, NSEEL_CODEHANDLE ch)
|
||||||
|
{
|
||||||
|
// should mutex protect if multiple threads access this eelScriptInst context
|
||||||
|
evalCacheEnt ecc;
|
||||||
|
ecc.str= key;
|
||||||
|
ecc.ch = ch;
|
||||||
|
if (m_eval_cache.GetSize() > 1024)
|
||||||
|
{
|
||||||
|
NSEEL_code_free(m_eval_cache.Get()->ch);
|
||||||
|
free(m_eval_cache.Get()->str);
|
||||||
|
m_eval_cache.Delete(0);
|
||||||
|
}
|
||||||
|
m_eval_cache.Add(ecc);
|
||||||
|
}
|
||||||
|
|
||||||
|
int eelScriptInst::init()
|
||||||
|
{
|
||||||
|
EEL_string_register();
|
||||||
|
#ifndef EELSCRIPT_NO_FILE
|
||||||
|
EEL_file_register();
|
||||||
|
#endif
|
||||||
|
#ifndef EELSCRIPT_NO_FFT
|
||||||
|
EEL_fft_register();
|
||||||
|
#endif
|
||||||
|
#ifndef EELSCRIPT_NO_MDCT
|
||||||
|
EEL_mdct_register();
|
||||||
|
#endif
|
||||||
|
EEL_misc_register();
|
||||||
|
#ifndef EELSCRIPT_NO_EVAL
|
||||||
|
EEL_eval_register();
|
||||||
|
NSEEL_addfunc_retval("defer",1,NSEEL_PProc_THIS,&_eel_defer);
|
||||||
|
NSEEL_addfunc_retval("runloop", 1, NSEEL_PProc_THIS, &_eel_defer);
|
||||||
|
NSEEL_addfunc_retval("atexit",1,NSEEL_PProc_THIS,&_eel_atexit);
|
||||||
|
#endif
|
||||||
|
#ifndef EELSCRIPT_NO_NET
|
||||||
|
EEL_tcp_register();
|
||||||
|
#endif
|
||||||
|
#ifndef EELSCRIPT_NO_LICE
|
||||||
|
eel_lice_register();
|
||||||
|
#ifdef _WIN32
|
||||||
|
eel_lice_register_standalone(GetModuleHandle(NULL),EELSCRIPT_LICE_CLASSNAME,NULL,NULL);
|
||||||
|
#else
|
||||||
|
eel_lice_register_standalone(NULL,EELSCRIPT_LICE_CLASSNAME,NULL,NULL);
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool eelScriptInst::has_deferred()
|
||||||
|
{
|
||||||
|
#ifndef EELSCRIPT_NO_EVAL
|
||||||
|
return m_defer_eval.Available() && m_vm;
|
||||||
|
#else
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef EELSCRIPT_NO_EVAL
|
||||||
|
void eelScriptInst::runCodeQ(WDL_Queue *q, const char *callername)
|
||||||
|
{
|
||||||
|
const int endptr = q->Available();
|
||||||
|
int offs = 0;
|
||||||
|
while (offs < endptr)
|
||||||
|
{
|
||||||
|
if (q->Available() < endptr) break; // should never happen, but safety first!
|
||||||
|
|
||||||
|
const char *ptr = (const char *)q->Get() + offs;
|
||||||
|
offs += strlen(ptr)+1;
|
||||||
|
|
||||||
|
NSEEL_CODEHANDLE ch=NULL;
|
||||||
|
char *sv=evalCacheGet(ptr,&ch);
|
||||||
|
|
||||||
|
if (!sv) sv=strdup(ptr);
|
||||||
|
if (!ch) ch=NSEEL_code_compile(m_vm,sv,0);
|
||||||
|
if (!ch)
|
||||||
|
{
|
||||||
|
free(sv);
|
||||||
|
#ifdef EEL_STRING_DEBUGOUT
|
||||||
|
const char *err = NSEEL_code_getcodeerror(m_vm);
|
||||||
|
if (err) EEL_STRING_DEBUGOUT("%s: error in code: %s",callername,err);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
NSEEL_code_execute(ch);
|
||||||
|
evalCacheDispose(sv,ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
q->Advance(endptr);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
bool eelScriptInst::run_deferred()
|
||||||
|
{
|
||||||
|
#ifndef EELSCRIPT_NO_EVAL
|
||||||
|
if (!m_defer_eval.Available()||!m_vm) return false;
|
||||||
|
|
||||||
|
runCodeQ(&m_defer_eval,"defer");
|
||||||
|
m_defer_eval.Compact();
|
||||||
|
return m_defer_eval.Available()>0;
|
||||||
|
#else
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef EEL_WANT_DOCUMENTATION
|
||||||
|
#include "ns-eel-func-ref.h"
|
||||||
|
|
||||||
|
void EELScript_GenerateFunctionList(WDL_PtrList<const char> *fs)
|
||||||
|
{
|
||||||
|
const char *p = nseel_builtin_function_reference;
|
||||||
|
while (*p) { fs->Add(p); p += strlen(p) + 1; }
|
||||||
|
p = eel_strings_function_reference;
|
||||||
|
while (*p) { fs->Add(p); p += strlen(p) + 1; }
|
||||||
|
p = eel_misc_function_reference;
|
||||||
|
while (*p) { fs->Add(p); p += strlen(p) + 1; }
|
||||||
|
#ifndef EELSCRIPT_NO_EVAL
|
||||||
|
fs->Add("atexit\t\"code\"\t"
|
||||||
|
#ifndef EELSCRIPT_HELP_NO_DEFER_DESC
|
||||||
|
"Adds code to be executed when the script finishes."
|
||||||
|
#endif
|
||||||
|
);
|
||||||
|
fs->Add("defer\t\"code\"\t"
|
||||||
|
#ifndef EELSCRIPT_HELP_NO_DEFER_DESC
|
||||||
|
"Adds code which will be executed some small amount of time after the current code finishes. Identical to runloop()"
|
||||||
|
#endif
|
||||||
|
);
|
||||||
|
fs->Add("runloop\t\"code\"\t"
|
||||||
|
#ifndef EELSCRIPT_HELP_NO_DEFER_DESC
|
||||||
|
"Adds code which will be executed some small amount of time after the current code finishes. Identical to defer()"
|
||||||
|
#endif
|
||||||
|
);
|
||||||
|
|
||||||
|
p = eel_eval_function_reference;
|
||||||
|
while (*p) { fs->Add(p); p += strlen(p) + 1; }
|
||||||
|
#endif
|
||||||
|
#ifndef EELSCRIPT_NO_NET
|
||||||
|
p = eel_net_function_reference;
|
||||||
|
while (*p) { fs->Add(p); p += strlen(p) + 1; }
|
||||||
|
#endif
|
||||||
|
#ifndef EELSCRIPT_NO_FFT
|
||||||
|
p = eel_fft_function_reference;
|
||||||
|
while (*p) { fs->Add(p); p += strlen(p) + 1; }
|
||||||
|
#endif
|
||||||
|
#ifndef EELSCRIPT_NO_FILE
|
||||||
|
p = eel_file_function_reference;
|
||||||
|
while (*p) { fs->Add(p); p += strlen(p) + 1; }
|
||||||
|
#endif
|
||||||
|
#ifndef EELSCRIPT_NO_MDCT
|
||||||
|
p = eel_mdct_function_reference;
|
||||||
|
while (*p) { fs->Add(p); p += strlen(p) + 1; }
|
||||||
|
#endif
|
||||||
|
#ifndef EELSCRIPT_NO_LICE
|
||||||
|
p = eel_lice_function_reference;
|
||||||
|
while (*p) { fs->Add(p); p += strlen(p) + 1; }
|
||||||
|
#endif
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#undef opaque
|
||||||
448
oversampling/WDL/eel2/glue_aarch64.h
Normal file
448
oversampling/WDL/eel2/glue_aarch64.h
Normal file
@@ -0,0 +1,448 @@
|
|||||||
|
#ifndef _NSEEL_GLUE_AARCH64_H_
|
||||||
|
#define _NSEEL_GLUE_AARCH64_H_
|
||||||
|
|
||||||
|
#define GLUE_MOD_IS_64
|
||||||
|
|
||||||
|
// x0=return value, first parm, x1-x2 parms (x3-x7 more params)
|
||||||
|
// x8 return struct?
|
||||||
|
// x9-x15 temporary
|
||||||
|
// x16-x17 = PLT, linker
|
||||||
|
// x18 reserved (TLS)
|
||||||
|
// x19-x28 callee-saved
|
||||||
|
// x19 = worktable
|
||||||
|
// x20 = ramtable
|
||||||
|
// x21 = consttab
|
||||||
|
// x22 = worktable ptr
|
||||||
|
// x23-x28 spare
|
||||||
|
// x29 frame pointer
|
||||||
|
// x30 link register
|
||||||
|
// x31 SP/zero
|
||||||
|
|
||||||
|
// x0=p1
|
||||||
|
// x1=p2
|
||||||
|
// x2=p3
|
||||||
|
|
||||||
|
// d0 is return value for fp?
|
||||||
|
// d/v/f0-7 = arguments/results
|
||||||
|
// 8-15 callee saved
|
||||||
|
// 16-31 temporary
|
||||||
|
|
||||||
|
// v8-v15 spill registers
|
||||||
|
#define GLUE_MAX_SPILL_REGS 8
|
||||||
|
#define GLUE_SAVE_TO_SPILL_SIZE(x) (4)
|
||||||
|
#define GLUE_RESTORE_SPILL_TO_FPREG2_SIZE(x) (4)
|
||||||
|
|
||||||
|
static void GLUE_RESTORE_SPILL_TO_FPREG2(void *b, int ws)
|
||||||
|
{
|
||||||
|
*(unsigned int *)b = 0x1e604101 + (ws<<5); // fmov d1, d8+ws
|
||||||
|
}
|
||||||
|
static void GLUE_SAVE_TO_SPILL(void *b, int ws)
|
||||||
|
{
|
||||||
|
*(unsigned int *)b = 0x1e604008 + ws; // fmov d8+ws, d0
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#define GLUE_HAS_FPREG2 1
|
||||||
|
|
||||||
|
static const unsigned int GLUE_COPY_FPSTACK_TO_FPREG2[] = { 0x1e604001 }; // fmov d1, d0
|
||||||
|
static unsigned int GLUE_POP_STACK_TO_FPREG2[] = {
|
||||||
|
0xfc4107e1 // ldr d1, [sp], #16
|
||||||
|
};
|
||||||
|
|
||||||
|
#define GLUE_MAX_FPSTACK_SIZE 0 // no stack support
|
||||||
|
#define GLUE_MAX_JMPSIZE ((1<<20) - 1024) // maximum relative jump size
|
||||||
|
|
||||||
|
// endOfInstruction is end of jump with relative offset, offset passed in is offset from end of dest instruction.
|
||||||
|
// 0 = current instruction
|
||||||
|
static void GLUE_JMP_SET_OFFSET(void *endOfInstruction, int offset)
|
||||||
|
{
|
||||||
|
unsigned int *a = (unsigned int*) endOfInstruction - 1;
|
||||||
|
offset += 4;
|
||||||
|
offset >>= 2; // as dwords
|
||||||
|
if ((a[0] & 0xFC000000) == 0x14000000)
|
||||||
|
{
|
||||||
|
// NC b = 0x14 + 26 bit offset
|
||||||
|
a[0] = 0x14000000 | (offset & 0x3FFFFFF);
|
||||||
|
}
|
||||||
|
else if ((a[0] & 0xFF000000) == 0x54000000)
|
||||||
|
{
|
||||||
|
// condb = 0x54 + 20 bit offset + 5 bit condition: 0=eq, 1=ne, b=lt, c=gt, d=le, a=ge
|
||||||
|
a[0] = 0x54000000 | (a[0] & 0xF) | ((offset & 0x7FFFF) << 5);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static const unsigned int GLUE_JMP_NC[] = { 0x14000000 };
|
||||||
|
|
||||||
|
static const unsigned int GLUE_JMP_IF_P1_Z[]=
|
||||||
|
{
|
||||||
|
0x7100001f, // cmp w0, #0
|
||||||
|
0x54000000, // b.eq
|
||||||
|
};
|
||||||
|
static const unsigned int GLUE_JMP_IF_P1_NZ[]=
|
||||||
|
{
|
||||||
|
0x7100001f, // cmp w0, #0
|
||||||
|
0x54000001, // b.ne
|
||||||
|
};
|
||||||
|
|
||||||
|
#define GLUE_MOV_PX_DIRECTVALUE_TOFPREG2_SIZE 16 // wr=-2, sets d1
|
||||||
|
#define GLUE_MOV_PX_DIRECTVALUE_SIZE 12
|
||||||
|
static void GLUE_MOV_PX_DIRECTVALUE_GEN(void *b, INT_PTR v, int wv)
|
||||||
|
{
|
||||||
|
static const unsigned int tab[3] = {
|
||||||
|
0xd2800000, // mov x0, #0000 (val<<5) | reg
|
||||||
|
0xf2a00000, // movk x0, #0000, lsl 16 (val<<5) | reg
|
||||||
|
0xf2c00000, // movk x0, #0000, lsl 32 (val<<5) | reg
|
||||||
|
};
|
||||||
|
// 0xABAAA, B is register, A are bits of word
|
||||||
|
unsigned int *p=(unsigned int *)b;
|
||||||
|
int wvo = wv;
|
||||||
|
if (wv<0) wv=0;
|
||||||
|
p[0] = tab[0] | wv | ((v&0xFFFF)<<5);
|
||||||
|
p[1] = tab[1] | wv | (((v>>16)&0xFFFF)<<5);
|
||||||
|
p[2] = tab[2] | wv | (((v>>32)&0xFFFF)<<5);
|
||||||
|
if (wvo == -2) p[3] = 0xfd400001; // ldr d1, [x0]
|
||||||
|
}
|
||||||
|
|
||||||
|
const static unsigned int GLUE_FUNC_ENTER[2] = { 0xa9bf7bfd, 0x910003fd }; // stp x29, x30, [sp, #-16]! ; mov x29, sp
|
||||||
|
#define GLUE_FUNC_ENTER_SIZE 4
|
||||||
|
const static unsigned int GLUE_FUNC_LEAVE[1] = { 0 }; // let GLUE_RET pop
|
||||||
|
#define GLUE_FUNC_LEAVE_SIZE 0
|
||||||
|
const static unsigned int GLUE_RET[]={ 0xa8c17bfd, 0xd65f03c0 }; // ldp x29,x30, [sp], #16 ; ret
|
||||||
|
|
||||||
|
static int GLUE_RESET_WTP(unsigned char *out, void *ptr)
|
||||||
|
{
|
||||||
|
const static unsigned int GLUE_SET_WTP_FROM_R19 = 0xaa1303f6; // mov r22, r19
|
||||||
|
if (out) memcpy(out,&GLUE_SET_WTP_FROM_R19,sizeof(GLUE_SET_WTP_FROM_R19));
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const static unsigned int GLUE_PUSH_P1[1]={ 0xf81f0fe0 }; // str x0, [sp, #-16]!
|
||||||
|
|
||||||
|
#define GLUE_STORE_P1_TO_STACK_AT_OFFS_SIZE(offs) ((offs)>=32768 ? 8 : 4)
|
||||||
|
static void GLUE_STORE_P1_TO_STACK_AT_OFFS(void *b, int offs)
|
||||||
|
{
|
||||||
|
if (offs >= 32768)
|
||||||
|
{
|
||||||
|
// add x1, sp, (offs/4096) lsl 12
|
||||||
|
*(unsigned int *)b = 0x914003e1 + ((offs>>12)<<10);
|
||||||
|
|
||||||
|
// str x0, [x1, #offs & 4095]
|
||||||
|
offs &= 4095;
|
||||||
|
offs <<= 10-3;
|
||||||
|
offs &= 0x7FFC00;
|
||||||
|
((unsigned int *)b)[1] = 0xf9000020 + offs;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// str x0, [sp, #offs]
|
||||||
|
offs <<= 10-3;
|
||||||
|
offs &= 0x7FFC00;
|
||||||
|
*(unsigned int *)b = 0xf90003e0 + offs;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_MOVE_PX_STACKPTR_SIZE 4
|
||||||
|
static void GLUE_MOVE_PX_STACKPTR_GEN(void *b, int wv)
|
||||||
|
{
|
||||||
|
// mov xX, sp
|
||||||
|
*(unsigned int *)b = 0x910003e0 + wv;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_MOVE_STACK_SIZE 4
|
||||||
|
static void GLUE_MOVE_STACK(void *b, int amt)
|
||||||
|
{
|
||||||
|
if (amt>=0)
|
||||||
|
{
|
||||||
|
if (amt >= 4096)
|
||||||
|
*(unsigned int*)b = 0x914003ff | (((amt+4095)>>12)<<10);
|
||||||
|
else
|
||||||
|
*(unsigned int*)b = 0x910003ff | (amt << 10);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
amt = -amt;
|
||||||
|
if (amt >= 4096)
|
||||||
|
*(unsigned int*)b = 0xd14003ff | (((amt+4095)>>12)<<10);
|
||||||
|
else
|
||||||
|
*(unsigned int*)b = 0xd10003ff | (amt << 10);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_POP_PX_SIZE 4
|
||||||
|
static void GLUE_POP_PX(void *b, int wv)
|
||||||
|
{
|
||||||
|
((unsigned int *)b)[0] = 0xf84107e0 | wv; // ldr x, [sp], 16
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_SET_PX_FROM_P1_SIZE 4
|
||||||
|
static void GLUE_SET_PX_FROM_P1(void *b, int wv)
|
||||||
|
{
|
||||||
|
*(unsigned int *)b = 0xaa0003e0 | wv;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const unsigned int GLUE_PUSH_P1PTR_AS_VALUE[] =
|
||||||
|
{
|
||||||
|
0xfd400007, // ldr d7, [x0]
|
||||||
|
0xfc1f0fe7, // str d7, [sp, #-16]!
|
||||||
|
};
|
||||||
|
|
||||||
|
static int GLUE_POP_VALUE_TO_ADDR(unsigned char *buf, void *destptr)
|
||||||
|
{
|
||||||
|
if (buf)
|
||||||
|
{
|
||||||
|
unsigned int *bufptr = (unsigned int *)buf;
|
||||||
|
*bufptr++ = 0xfc4107e7; // ldr d7, [sp], #16
|
||||||
|
GLUE_MOV_PX_DIRECTVALUE_GEN(bufptr, (INT_PTR)destptr,0);
|
||||||
|
bufptr += GLUE_MOV_PX_DIRECTVALUE_SIZE/4;
|
||||||
|
*bufptr++ = 0xfd000007; // str d7, [x0]
|
||||||
|
}
|
||||||
|
return 2*4 + GLUE_MOV_PX_DIRECTVALUE_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int GLUE_COPY_VALUE_AT_P1_TO_PTR(unsigned char *buf, void *destptr)
|
||||||
|
{
|
||||||
|
if (buf)
|
||||||
|
{
|
||||||
|
unsigned int *bufptr = (unsigned int *)buf;
|
||||||
|
*bufptr++ = 0xfd400007; // ldr d7, [x0]
|
||||||
|
GLUE_MOV_PX_DIRECTVALUE_GEN(bufptr, (INT_PTR)destptr,0);
|
||||||
|
bufptr += GLUE_MOV_PX_DIRECTVALUE_SIZE/4;
|
||||||
|
*bufptr++ = 0xfd000007; // str d7, [x0]
|
||||||
|
}
|
||||||
|
return 2*4 + GLUE_MOV_PX_DIRECTVALUE_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#define GLUE_CALL_CODE(bp, cp, rt) do { \
|
||||||
|
GLUE_SCR_TYPE f; \
|
||||||
|
static const double consttab[] = { \
|
||||||
|
NSEEL_CLOSEFACTOR, \
|
||||||
|
0.0, \
|
||||||
|
1.0, \
|
||||||
|
-1.0, \
|
||||||
|
-0.5, /* for invsqrt */ \
|
||||||
|
1.5, \
|
||||||
|
}; \
|
||||||
|
if (!(h->compile_flags&NSEEL_CODE_COMPILE_FLAG_NOFPSTATE) && \
|
||||||
|
!((f=glue_getscr())&(1<<24))) { \
|
||||||
|
glue_setscr(f|(1<<24)); \
|
||||||
|
eel_callcode64(bp, cp, rt, (void *)consttab); \
|
||||||
|
glue_setscr(f); \
|
||||||
|
} else eel_callcode64(bp, cp, rt, (void *)consttab);\
|
||||||
|
} while(0)
|
||||||
|
|
||||||
|
#ifndef _MSC_VER
|
||||||
|
static void eel_callcode64(INT_PTR bp, INT_PTR cp, INT_PTR rt, void *consttab)
|
||||||
|
{
|
||||||
|
__asm__(
|
||||||
|
"mov x1, %2\n"
|
||||||
|
"mov x2, %3\n"
|
||||||
|
"mov x3, %1\n"
|
||||||
|
"mov x0, %0\n"
|
||||||
|
"stp x29, x30, [sp, #-64]!\n"
|
||||||
|
"stp x18, x20, [sp, 16]\n"
|
||||||
|
"stp x21, x19, [sp, 32]\n"
|
||||||
|
"stp x22, x23, [sp, 48]\n"
|
||||||
|
"mov x29, sp\n"
|
||||||
|
"mov x19, x3\n"
|
||||||
|
"mov x20, x1\n"
|
||||||
|
"mov x21, x2\n"
|
||||||
|
"blr x0\n"
|
||||||
|
"ldp x29, x30, [sp], 16\n"
|
||||||
|
"ldp x18, x20, [sp], 16\n"
|
||||||
|
"ldp x21, x19, [sp], 16\n"
|
||||||
|
"ldp x22, x23, [sp], 16\n"
|
||||||
|
::"r" (cp), "r" (bp), "r" (rt), "r" (consttab) :"x0","x1","x2","x3","x4","x5","x6","x7",
|
||||||
|
"x8","x9","x10","x11","x12","x13","x14","x15",
|
||||||
|
"v8","v9","v10","v11","v12","v13","v14","v15");
|
||||||
|
|
||||||
|
};
|
||||||
|
#else
|
||||||
|
void eel_callcode64(INT_PTR bp, INT_PTR cp, INT_PTR rt, void *consttab);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static unsigned char *EEL_GLUE_set_immediate(void *_p, INT_PTR newv)
|
||||||
|
{
|
||||||
|
unsigned int *p=(unsigned int *)_p;
|
||||||
|
WDL_ASSERT(!(newv>>48));
|
||||||
|
// 0xd2800000, // mov x0, #0000 (val<<5) | reg
|
||||||
|
// 0xf2a00000, // movk x0, #0000, lsl 16 (val<<5) | reg
|
||||||
|
// 0xf2c00000, // movk x0, #0000, lsl 32 (val<<5) | reg
|
||||||
|
while (((p[0]>>5)&0xffff)!=0xdead ||
|
||||||
|
((p[1]>>5)&0xffff)!=0xbeef ||
|
||||||
|
((p[2]>>5)&0xffff)!=0xbeef) p++;
|
||||||
|
|
||||||
|
p[0] = (p[0] & 0xFFE0001F) | ((newv&0xffff)<<5);
|
||||||
|
p[1] = (p[1] & 0xFFE0001F) | (((newv>>16)&0xffff)<<5);
|
||||||
|
p[2] = (p[2] & 0xFFE0001F) | (((newv>>32)&0xffff)<<5);
|
||||||
|
|
||||||
|
return (unsigned char *)(p+2);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_SET_PX_FROM_WTP_SIZE sizeof(int)
|
||||||
|
static void GLUE_SET_PX_FROM_WTP(void *b, int wv)
|
||||||
|
{
|
||||||
|
*(unsigned int *)b = 0xaa1603e0 + wv; // mov x, x22
|
||||||
|
}
|
||||||
|
|
||||||
|
static int GLUE_POP_FPSTACK_TO_PTR(unsigned char *buf, void *destptr)
|
||||||
|
{
|
||||||
|
if (buf)
|
||||||
|
{
|
||||||
|
unsigned int *bufptr = (unsigned int *)buf;
|
||||||
|
GLUE_MOV_PX_DIRECTVALUE_GEN(bufptr, (INT_PTR)destptr,0);
|
||||||
|
bufptr += GLUE_MOV_PX_DIRECTVALUE_SIZE/4;
|
||||||
|
|
||||||
|
*bufptr++ = 0xfd000000; // str d0, [x0]
|
||||||
|
}
|
||||||
|
return GLUE_MOV_PX_DIRECTVALUE_SIZE + sizeof(int);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_POP_FPSTACK_SIZE 0
|
||||||
|
static const unsigned int GLUE_POP_FPSTACK[1] = { 0 }; // no need to pop, not a stack
|
||||||
|
|
||||||
|
static const unsigned int GLUE_POP_FPSTACK_TOSTACK[] = {
|
||||||
|
0xfc1f0fe0, // str d0, [sp, #-16]!
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
static const unsigned int GLUE_POP_FPSTACK_TO_WTP[] = {
|
||||||
|
0xfc0086c0, // str d0, [x22], #8
|
||||||
|
};
|
||||||
|
|
||||||
|
#define GLUE_PUSH_VAL_AT_PX_TO_FPSTACK_SIZE 4
|
||||||
|
static void GLUE_PUSH_VAL_AT_PX_TO_FPSTACK(void *b, int wv)
|
||||||
|
{
|
||||||
|
*(unsigned int *)b = 0xfd400000 + (wv<<5); // ldr d0, [xX]
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_POP_FPSTACK_TO_WTP_TO_PX_SIZE (sizeof(GLUE_POP_FPSTACK_TO_WTP) + GLUE_SET_PX_FROM_WTP_SIZE)
|
||||||
|
static void GLUE_POP_FPSTACK_TO_WTP_TO_PX(unsigned char *buf, int wv)
|
||||||
|
{
|
||||||
|
GLUE_SET_PX_FROM_WTP(buf,wv);
|
||||||
|
memcpy(buf + GLUE_SET_PX_FROM_WTP_SIZE,GLUE_POP_FPSTACK_TO_WTP,sizeof(GLUE_POP_FPSTACK_TO_WTP));
|
||||||
|
};
|
||||||
|
|
||||||
|
static const unsigned int GLUE_SET_P1_Z[] = { 0x52800000 }; // mov w0, #0
|
||||||
|
static const unsigned int GLUE_SET_P1_NZ[] = { 0x52800020 }; // mov w0, #1
|
||||||
|
|
||||||
|
|
||||||
|
static void *GLUE_realAddress(void *fn, int *size)
|
||||||
|
{
|
||||||
|
while ((*(int*)fn & 0xFC000000) == 0x14000000)
|
||||||
|
{
|
||||||
|
int offset = (*(int*)fn & 0x3FFFFFF);
|
||||||
|
if (offset & 0x2000000)
|
||||||
|
offset |= 0xFC000000;
|
||||||
|
|
||||||
|
fn = (int*)fn + offset;
|
||||||
|
}
|
||||||
|
static const unsigned int sig[] = {
|
||||||
|
#ifndef _MSC_VER
|
||||||
|
0xaa0003e0,
|
||||||
|
#endif
|
||||||
|
0xaa0103e1,
|
||||||
|
#ifndef _MSC_VER
|
||||||
|
0xaa0203e2
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
unsigned char *p = (unsigned char *)fn;
|
||||||
|
|
||||||
|
while (memcmp(p,sig,sizeof(sig))) p+=4;
|
||||||
|
p+=sizeof(sig);
|
||||||
|
fn = p;
|
||||||
|
|
||||||
|
while (memcmp(p,sig,sizeof(sig))) p+=4;
|
||||||
|
*size = p - (unsigned char *)fn;
|
||||||
|
return fn;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef _MSC_VER
|
||||||
|
#define GLUE_SCR_TYPE unsigned long
|
||||||
|
static unsigned long __attribute__((unused)) glue_getscr()
|
||||||
|
{
|
||||||
|
unsigned long rv;
|
||||||
|
asm volatile ( "mrs %0, fpcr" : "=r" (rv));
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
static void __attribute__((unused)) glue_setscr(unsigned long v)
|
||||||
|
{
|
||||||
|
asm volatile ( "msr fpcr, %0" :: "r"(v));
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
#define GLUE_SCR_TYPE unsigned long long
|
||||||
|
GLUE_SCR_TYPE glue_getscr();
|
||||||
|
void glue_setscr(unsigned long long);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void eel_enterfp(int _s[2])
|
||||||
|
{
|
||||||
|
GLUE_SCR_TYPE *s = (GLUE_SCR_TYPE*)_s;
|
||||||
|
s[0] = glue_getscr();
|
||||||
|
glue_setscr(s[0] | (1<<24));
|
||||||
|
}
|
||||||
|
void eel_leavefp(int _s[2])
|
||||||
|
{
|
||||||
|
const GLUE_SCR_TYPE *s = (GLUE_SCR_TYPE*)_s;
|
||||||
|
glue_setscr(s[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_HAS_FUSE 1
|
||||||
|
static int GLUE_FUSE(compileContext *ctx, unsigned char *code, int left_size, int right_size, int fuse_flags, int spill_reg)
|
||||||
|
{
|
||||||
|
if (left_size>=4 && right_size == 4)
|
||||||
|
{
|
||||||
|
unsigned int instr = ((unsigned int *)code)[-1];
|
||||||
|
if (spill_reg >= 0 && (instr & 0xfffffc1f) == 0x1e604001) // fmov d1, dX
|
||||||
|
{
|
||||||
|
const int src_reg = (instr>>5)&0x1f;
|
||||||
|
if (src_reg == spill_reg + 8)
|
||||||
|
{
|
||||||
|
instr = ((unsigned int *)code)[0];
|
||||||
|
if ((instr & 0xffffcfff) == 0x1e600820)
|
||||||
|
{
|
||||||
|
((unsigned int *)code)[-1] = instr + ((src_reg-1) << 5);
|
||||||
|
return -4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef _M_ARM64EC
|
||||||
|
#define DEF_F1(n) static double eel_##n(double a) { return n(a); }
|
||||||
|
#define DEF_F2(n) static double eel_##n(double a, double b) { return n(a,b); }
|
||||||
|
DEF_F1(cos)
|
||||||
|
#define cos eel_cos
|
||||||
|
DEF_F1(sin)
|
||||||
|
#define sin eel_sin
|
||||||
|
DEF_F1(tan)
|
||||||
|
#define tan eel_tan
|
||||||
|
DEF_F1(log)
|
||||||
|
#define log eel_log
|
||||||
|
DEF_F1(log10)
|
||||||
|
#define log10 eel_log10
|
||||||
|
DEF_F1(acos)
|
||||||
|
#define acos eel_acos
|
||||||
|
DEF_F1(asin)
|
||||||
|
#define asin eel_asin
|
||||||
|
DEF_F1(atan)
|
||||||
|
#define atan eel_atan
|
||||||
|
DEF_F1(exp)
|
||||||
|
#define exp eel_exp
|
||||||
|
DEF_F2(pow)
|
||||||
|
#define pow eel_pow
|
||||||
|
DEF_F2(atan2)
|
||||||
|
#define atan2 eel_atan2
|
||||||
|
// ceil and floor will be wrapped by defs in nseel-compiler.c
|
||||||
|
|
||||||
|
#pragma comment(lib,"onecore.lib")
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
335
oversampling/WDL/eel2/glue_arm.h
Normal file
335
oversampling/WDL/eel2/glue_arm.h
Normal file
@@ -0,0 +1,335 @@
|
|||||||
|
#ifndef _NSEEL_GLUE_ARM_H_
|
||||||
|
#define _NSEEL_GLUE_ARM_H_
|
||||||
|
|
||||||
|
// r0=return value, first parm, r1-r2 parms
|
||||||
|
// r3+ should be reserved
|
||||||
|
// blx addr
|
||||||
|
// stmfd sp!, {register list, lr}
|
||||||
|
// ldmfd sp!, {register list, pc}
|
||||||
|
|
||||||
|
// let's make r8 = worktable
|
||||||
|
// let's make r7 = ramtable
|
||||||
|
// r6 = consttab
|
||||||
|
// r5 = worktable ptr
|
||||||
|
|
||||||
|
// r0=p1
|
||||||
|
// r1=p2
|
||||||
|
// r2=p3
|
||||||
|
|
||||||
|
// d0 is return value?
|
||||||
|
|
||||||
|
|
||||||
|
#define GLUE_HAS_FPREG2 1
|
||||||
|
|
||||||
|
static const unsigned int GLUE_COPY_FPSTACK_TO_FPREG2[] = {
|
||||||
|
0xeeb01b40 // fcpyd d1, d0
|
||||||
|
};
|
||||||
|
|
||||||
|
static unsigned int GLUE_POP_STACK_TO_FPREG2[] = {
|
||||||
|
0xed9d1b00,// vldr d1, [sp]
|
||||||
|
0xe28dd008,// add sp, sp, #8
|
||||||
|
};
|
||||||
|
|
||||||
|
#define GLUE_MAX_SPILL_REGS 8
|
||||||
|
#define GLUE_SAVE_TO_SPILL_SIZE(x) (4)
|
||||||
|
#define GLUE_RESTORE_SPILL_TO_FPREG2_SIZE(x) (4)
|
||||||
|
|
||||||
|
static void GLUE_RESTORE_SPILL_TO_FPREG2(void *b, int ws)
|
||||||
|
{
|
||||||
|
*(unsigned int *)b = 0xeeb01b48 + ws; // fcpyd d1, d8+ws
|
||||||
|
}
|
||||||
|
static void GLUE_SAVE_TO_SPILL(void *b, int ws)
|
||||||
|
{
|
||||||
|
*(unsigned int *)b = 0xeeb08b40 + (ws<<12); // fcpyd d8+ws, d0
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#define GLUE_MAX_FPSTACK_SIZE 0 // no stack support
|
||||||
|
#define GLUE_MAX_JMPSIZE ((1<<25) - 1024) // maximum relative jump size
|
||||||
|
|
||||||
|
// endOfInstruction is end of jump with relative offset, offset passed in is offset from end of dest instruction.
|
||||||
|
// TODO: verify, but offset probably from next instruction (PC is ahead)
|
||||||
|
#define GLUE_JMP_SET_OFFSET(endOfInstruction,offset) (((int *)(endOfInstruction))[-1] = (((int *)(endOfInstruction))[-1]&0xFF000000)|((((offset)>>2)-1)))
|
||||||
|
|
||||||
|
// /=conditional=always = 0xE
|
||||||
|
// |/= 101(L), so 8+2+0 = 10 = A
|
||||||
|
static const unsigned int GLUE_JMP_NC[] = { 0xEA000000 };
|
||||||
|
|
||||||
|
static const unsigned int GLUE_JMP_IF_P1_Z[]=
|
||||||
|
{
|
||||||
|
0xe1100000, // tst r0, r0
|
||||||
|
0x0A000000, // branch if Z set
|
||||||
|
};
|
||||||
|
static const unsigned int GLUE_JMP_IF_P1_NZ[]=
|
||||||
|
{
|
||||||
|
0xe1100000, // tst r0, r0
|
||||||
|
0x1A000000, // branch if Z clear
|
||||||
|
};
|
||||||
|
|
||||||
|
#define GLUE_MOV_PX_DIRECTVALUE_TOFPREG2_SIZE 12 // wr=-2, sets d1
|
||||||
|
#define GLUE_MOV_PX_DIRECTVALUE_SIZE 8
|
||||||
|
static void GLUE_MOV_PX_DIRECTVALUE_GEN(void *b, INT_PTR v, int wv)
|
||||||
|
{
|
||||||
|
// requires ARMv6thumb2 or later
|
||||||
|
const unsigned int reg_add = wdl_max(wv,0) << 12;
|
||||||
|
static const unsigned int tab[2] = {
|
||||||
|
0xe3000000, // movw r0, #0000
|
||||||
|
0xe3400000, // movt r0, #0000
|
||||||
|
};
|
||||||
|
// 0xABAAA, B is register, A are bits of word
|
||||||
|
unsigned int *p=(unsigned int *)b;
|
||||||
|
p[0] = tab[0] | reg_add | (v&0xfff) | ((v&0xf000)<<4);
|
||||||
|
p[1] = tab[1] | reg_add | ((v>>16)&0xfff) | ((v&0xf0000000)>>12);
|
||||||
|
if (wv == -2) p[2] = 0xed901b00; // fldd d1, [r0]
|
||||||
|
}
|
||||||
|
|
||||||
|
const static unsigned int GLUE_FUNC_ENTER[1] = { 0xe92d4010 }; // push {r4, lr}
|
||||||
|
#define GLUE_FUNC_ENTER_SIZE 4
|
||||||
|
const static unsigned int GLUE_FUNC_LEAVE[1] = { 0 }; // let GLUE_RET pop
|
||||||
|
#define GLUE_FUNC_LEAVE_SIZE 0
|
||||||
|
const static unsigned int GLUE_RET[]={ 0xe8bd8010 }; // pop {r4, pc}
|
||||||
|
|
||||||
|
static int GLUE_RESET_WTP(unsigned char *out, void *ptr)
|
||||||
|
{
|
||||||
|
const static unsigned int GLUE_SET_WTP_FROM_R8 = 0xe1a05008; // mov r5, r8
|
||||||
|
if (out) memcpy(out,&GLUE_SET_WTP_FROM_R8,sizeof(GLUE_SET_WTP_FROM_R8));
|
||||||
|
return sizeof(GLUE_SET_WTP_FROM_R8);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const static unsigned int GLUE_PUSH_P1[1]={ 0xe52d0008 }; // push {r0}, aligned to 8
|
||||||
|
|
||||||
|
|
||||||
|
static int arm_encode_constforalu(int amt)
|
||||||
|
{
|
||||||
|
int nrot = 16;
|
||||||
|
while (amt >= 0x100 && nrot > 1)
|
||||||
|
{
|
||||||
|
// ARM encodes integers for ALU operations as rotated right by third nibble*2
|
||||||
|
amt = (amt + 3)>>2;
|
||||||
|
nrot--;
|
||||||
|
}
|
||||||
|
return ((nrot&15) << 8) | amt;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#define GLUE_STORE_P1_TO_STACK_AT_OFFS_SIZE(x) ((x)>=4096 ? 8 : 4)
|
||||||
|
static void GLUE_STORE_P1_TO_STACK_AT_OFFS(void *b, int offs)
|
||||||
|
{
|
||||||
|
if (offs >= 4096)
|
||||||
|
{
|
||||||
|
// add r2, sp, (offs&~4095)
|
||||||
|
*(unsigned int *)b = 0xe28d2000 | arm_encode_constforalu(offs&~4095);
|
||||||
|
// str r0, [r2, offs&4095]
|
||||||
|
((unsigned int *)b)[1] = 0xe5820000 + (offs&4095);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// str r0, [sp, #offs]
|
||||||
|
*(unsigned int *)b = 0xe58d0000 + offs;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_MOVE_PX_STACKPTR_SIZE 4
|
||||||
|
static void GLUE_MOVE_PX_STACKPTR_GEN(void *b, int wv)
|
||||||
|
{
|
||||||
|
// mov rX, sp
|
||||||
|
*(unsigned int *)b = 0xe1a0000d + (wv<<12);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_MOVE_STACK_SIZE 4
|
||||||
|
static void GLUE_MOVE_STACK(void *b, int amt)
|
||||||
|
{
|
||||||
|
unsigned int instr = 0xe28dd000;
|
||||||
|
if (amt < 0)
|
||||||
|
{
|
||||||
|
instr = 0xe24dd000;
|
||||||
|
amt=-amt;
|
||||||
|
}
|
||||||
|
*(unsigned int*)b = instr | arm_encode_constforalu(amt);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_POP_PX_SIZE 4
|
||||||
|
static void GLUE_POP_PX(void *b, int wv)
|
||||||
|
{
|
||||||
|
((unsigned int *)b)[0] = 0xe49d0008 | (wv<<12); // pop {rX}, aligned to 8
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_SET_PX_FROM_P1_SIZE 4
|
||||||
|
static void GLUE_SET_PX_FROM_P1(void *b, int wv)
|
||||||
|
{
|
||||||
|
*(unsigned int *)b = 0xe1a00000 | (wv<<12); // mov rX, r0
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const unsigned int GLUE_PUSH_P1PTR_AS_VALUE[] =
|
||||||
|
{
|
||||||
|
0xed907b00, // fldd d7, [r0]
|
||||||
|
0xe24dd008, // sub sp, sp, #8
|
||||||
|
0xed8d7b00, // fstd d7, [sp]
|
||||||
|
};
|
||||||
|
|
||||||
|
static int GLUE_POP_VALUE_TO_ADDR(unsigned char *buf, void *destptr)
|
||||||
|
{
|
||||||
|
if (buf)
|
||||||
|
{
|
||||||
|
unsigned int *bufptr = (unsigned int *)buf;
|
||||||
|
*bufptr++ = 0xed9d7b00; // fldd d7, [sp]
|
||||||
|
*bufptr++ = 0xe28dd008; // add sp, sp, #8
|
||||||
|
GLUE_MOV_PX_DIRECTVALUE_GEN(bufptr, (INT_PTR)destptr,0);
|
||||||
|
bufptr += GLUE_MOV_PX_DIRECTVALUE_SIZE/4;
|
||||||
|
*bufptr++ = 0xed807b00; // fstd d7, [r0]
|
||||||
|
}
|
||||||
|
return 3*4 + GLUE_MOV_PX_DIRECTVALUE_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int GLUE_COPY_VALUE_AT_P1_TO_PTR(unsigned char *buf, void *destptr)
|
||||||
|
{
|
||||||
|
if (buf)
|
||||||
|
{
|
||||||
|
unsigned int *bufptr = (unsigned int *)buf;
|
||||||
|
*bufptr++ = 0xed907b00; // fldd d7, [r0]
|
||||||
|
GLUE_MOV_PX_DIRECTVALUE_GEN(bufptr, (INT_PTR)destptr,0);
|
||||||
|
bufptr += GLUE_MOV_PX_DIRECTVALUE_SIZE/4;
|
||||||
|
*bufptr++ = 0xed807b00; // fstd d7, [r0]
|
||||||
|
}
|
||||||
|
return 2*4 + GLUE_MOV_PX_DIRECTVALUE_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef _MSC_VER
|
||||||
|
#define GLUE_CALL_CODE(bp, cp, rt) do { \
|
||||||
|
unsigned int f; \
|
||||||
|
if (!(h->compile_flags&NSEEL_CODE_COMPILE_FLAG_NOFPSTATE) && \
|
||||||
|
!((f=glue_getscr())&(1<<24))) { \
|
||||||
|
glue_setscr(f|(1<<24)); \
|
||||||
|
eel_callcode32(bp, cp, rt); \
|
||||||
|
glue_setscr(f); \
|
||||||
|
} else eel_callcode32(bp, cp, rt);\
|
||||||
|
} while(0)
|
||||||
|
|
||||||
|
static const double __consttab[] = {
|
||||||
|
NSEEL_CLOSEFACTOR,
|
||||||
|
0.0,
|
||||||
|
1.0,
|
||||||
|
-1.0,
|
||||||
|
-0.5, // for invsqrt
|
||||||
|
1.5,
|
||||||
|
};
|
||||||
|
|
||||||
|
static void eel_callcode32(INT_PTR bp, INT_PTR cp, INT_PTR rt)
|
||||||
|
{
|
||||||
|
__asm__ volatile(
|
||||||
|
"mov r7, %2\n"
|
||||||
|
"mov r6, %3\n"
|
||||||
|
"mov r8, %1\n"
|
||||||
|
"mov r0, %0\n"
|
||||||
|
"mov r1, sp\n"
|
||||||
|
"bic sp, sp, #7\n"
|
||||||
|
"push {r1, lr}\n"
|
||||||
|
"blx r0\n"
|
||||||
|
"pop {r1, lr}\n"
|
||||||
|
"mov sp, r1\n"
|
||||||
|
::"r" (cp), "r" (bp), "r" (rt), "r" (__consttab) :
|
||||||
|
"r5", "r6", "r7", "r8", "r10",
|
||||||
|
"d8","d9","d10","d11","d12","d13","d14","d15");
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static unsigned char *EEL_GLUE_set_immediate(void *_p, INT_PTR newv)
|
||||||
|
{
|
||||||
|
unsigned int *p=(unsigned int *)_p;
|
||||||
|
while ((p[0]&0x000F0FFF) != 0x000d0ead &&
|
||||||
|
(p[1]&0x000F0FFF) != 0x000b0eef) p++;
|
||||||
|
p[0] = (p[0]&0xFFF0F000) | (newv&0xFFF) | ((newv << 4) & 0xF0000);
|
||||||
|
p[1] = (p[1]&0xFFF0F000) | ((newv>>16)&0xFFF) | ((newv >> 12)&0xF0000);
|
||||||
|
|
||||||
|
return (unsigned char *)(p+1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_SET_PX_FROM_WTP_SIZE sizeof(int)
|
||||||
|
static void GLUE_SET_PX_FROM_WTP(void *b, int wv)
|
||||||
|
{
|
||||||
|
*(unsigned int *)b = 0xe1a00005 + (wv<<12); // mov rX, r5
|
||||||
|
}
|
||||||
|
|
||||||
|
static int GLUE_POP_FPSTACK_TO_PTR(unsigned char *buf, void *destptr)
|
||||||
|
{
|
||||||
|
if (buf)
|
||||||
|
{
|
||||||
|
unsigned int *bufptr = (unsigned int *)buf;
|
||||||
|
GLUE_MOV_PX_DIRECTVALUE_GEN(bufptr, (INT_PTR)destptr,0);
|
||||||
|
bufptr += GLUE_MOV_PX_DIRECTVALUE_SIZE/4;
|
||||||
|
|
||||||
|
*bufptr++ = 0xed800b00; // fstd d0, [r0]
|
||||||
|
}
|
||||||
|
return GLUE_MOV_PX_DIRECTVALUE_SIZE + sizeof(int);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_POP_FPSTACK_SIZE 0
|
||||||
|
static const unsigned int GLUE_POP_FPSTACK[1] = { 0 }; // no need to pop, not a stack
|
||||||
|
|
||||||
|
static const unsigned int GLUE_POP_FPSTACK_TOSTACK[] = {
|
||||||
|
0xe24dd008, // sub sp, sp, #8
|
||||||
|
0xed8d0b00, // fstd d0, [sp]
|
||||||
|
};
|
||||||
|
|
||||||
|
static const unsigned int GLUE_POP_FPSTACK_TO_WTP[] = {
|
||||||
|
0xed850b00, // fstd d0, [r5]
|
||||||
|
0xe2855008, // add r5, r5, #8
|
||||||
|
};
|
||||||
|
|
||||||
|
#define GLUE_PUSH_VAL_AT_PX_TO_FPSTACK_SIZE 4
|
||||||
|
static void GLUE_PUSH_VAL_AT_PX_TO_FPSTACK(void *b, int wv)
|
||||||
|
{
|
||||||
|
*(unsigned int *)b = 0xed900b00 + (wv<<16); // fldd d0, [rX]
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_POP_FPSTACK_TO_WTP_TO_PX_SIZE (sizeof(GLUE_POP_FPSTACK_TO_WTP) + GLUE_SET_PX_FROM_WTP_SIZE)
|
||||||
|
static void GLUE_POP_FPSTACK_TO_WTP_TO_PX(unsigned char *buf, int wv)
|
||||||
|
{
|
||||||
|
GLUE_SET_PX_FROM_WTP(buf,wv);
|
||||||
|
memcpy(buf + GLUE_SET_PX_FROM_WTP_SIZE,GLUE_POP_FPSTACK_TO_WTP,sizeof(GLUE_POP_FPSTACK_TO_WTP));
|
||||||
|
};
|
||||||
|
|
||||||
|
static const unsigned int GLUE_SET_P1_Z[] = { 0xe3a00000 }; // mov r0, #0
|
||||||
|
static const unsigned int GLUE_SET_P1_NZ[] = { 0xe3a00001 }; // mov r0, #1
|
||||||
|
|
||||||
|
|
||||||
|
static void *GLUE_realAddress(void *fn, int *size)
|
||||||
|
{
|
||||||
|
static const unsigned int sig[3] = { 0xe1a00000, 0xe1a01001, 0xe1a02002 };
|
||||||
|
unsigned char *p = (unsigned char *)fn;
|
||||||
|
|
||||||
|
while (memcmp(p,sig,sizeof(sig))) p+=4;
|
||||||
|
p+=sizeof(sig);
|
||||||
|
fn = p;
|
||||||
|
|
||||||
|
while (memcmp(p,sig,sizeof(sig))) p+=4;
|
||||||
|
*size = p - (unsigned char *)fn;
|
||||||
|
return fn;
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned int __attribute__((unused)) glue_getscr()
|
||||||
|
{
|
||||||
|
unsigned int rv;
|
||||||
|
asm volatile ( "fmrx %0, fpscr" : "=r" (rv));
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
static void __attribute__((unused)) glue_setscr(unsigned int v)
|
||||||
|
{
|
||||||
|
asm volatile ( "fmxr fpscr, %0" :: "r"(v));
|
||||||
|
}
|
||||||
|
|
||||||
|
void eel_enterfp(int s[2])
|
||||||
|
{
|
||||||
|
s[0] = glue_getscr();
|
||||||
|
glue_setscr(s[0] | (1<<24)); // could also do 3<<22 for RTZ
|
||||||
|
}
|
||||||
|
void eel_leavefp(int s[2])
|
||||||
|
{
|
||||||
|
glue_setscr(s[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
1029
oversampling/WDL/eel2/glue_port.h
Normal file
1029
oversampling/WDL/eel2/glue_port.h
Normal file
File diff suppressed because it is too large
Load Diff
285
oversampling/WDL/eel2/glue_ppc.h
Normal file
285
oversampling/WDL/eel2/glue_ppc.h
Normal file
@@ -0,0 +1,285 @@
|
|||||||
|
#ifndef _NSEEL_GLUE_PPC_H_
|
||||||
|
#define _NSEEL_GLUE_PPC_H_
|
||||||
|
|
||||||
|
#define GLUE_MAX_FPSTACK_SIZE 0 // no stack support
|
||||||
|
#define GLUE_MAX_JMPSIZE 30000 // maximum relative jump size for this arch (if not defined, any jump is possible)
|
||||||
|
|
||||||
|
|
||||||
|
// endOfInstruction is end of jump with relative offset, offset passed in is offset from end of dest instruction.
|
||||||
|
// on PPC the offset needs to be from the start of the instruction (hence +4), and also the low two bits are flags so
|
||||||
|
// we make sure they are clear (they should always be clear, anyway, since we always generate 4 byte instructions)
|
||||||
|
#define GLUE_JMP_SET_OFFSET(endOfInstruction,offset) (((short *)(endOfInstruction))[-1] = ((offset) + 4) & 0xFFFC)
|
||||||
|
|
||||||
|
static const unsigned char GLUE_JMP_NC[] = { 0x48,0, 0, 0, }; // b <offset>
|
||||||
|
|
||||||
|
static const unsigned int GLUE_JMP_IF_P1_Z[]=
|
||||||
|
{
|
||||||
|
0x2f830000, //cmpwi cr7, r3, 0
|
||||||
|
0x419e0000, // beq cr7, offset-bytes-from-startofthisinstruction
|
||||||
|
};
|
||||||
|
static const unsigned int GLUE_JMP_IF_P1_NZ[]=
|
||||||
|
{
|
||||||
|
0x2f830000, //cmpwi cr7, r3, 0
|
||||||
|
0x409e0000, // bne cr7, offset-bytes-from-startofthisinstruction
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#define GLUE_MOV_PX_DIRECTVALUE_SIZE 8
|
||||||
|
static void GLUE_MOV_PX_DIRECTVALUE_GEN(void *b, INT_PTR v, int wv)
|
||||||
|
{
|
||||||
|
static const unsigned short tab[3][2] = {
|
||||||
|
{0x3C60, 0x6063}, // addis r3, r0, hw -- ori r3,r3, lw
|
||||||
|
{0x3DC0, 0x61CE}, // addis r14, r0, hw -- ori r14, r14, lw
|
||||||
|
{0x3DE0, 0x61EF}, // addis r15, r0, hw -- oris r15, r15, lw
|
||||||
|
};
|
||||||
|
unsigned int uv=(unsigned int)v;
|
||||||
|
unsigned short *p=(unsigned short *)b;
|
||||||
|
|
||||||
|
*p++ = tab[wv][0]; // addis rX, r0, hw
|
||||||
|
*p++ = (uv>>16)&0xffff;
|
||||||
|
*p++ = tab[wv][1]; // ori rX, rX, lw
|
||||||
|
*p++ = uv&0xffff;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// mflr r5
|
||||||
|
// stwu r5, -16(r1)
|
||||||
|
const static unsigned int GLUE_FUNC_ENTER[2] = { 0x7CA802A6, 0x94A1FFF0 };
|
||||||
|
#define GLUE_FUNC_ENTER_SIZE 8
|
||||||
|
|
||||||
|
// lwz r5, 0(r1)
|
||||||
|
// addi r1, r1, 16
|
||||||
|
// mtlr r5
|
||||||
|
const static unsigned int GLUE_FUNC_LEAVE[3] = { 0x80A10000, 0x38210010, 0x7CA803A6 };
|
||||||
|
#define GLUE_FUNC_LEAVE_SIZE 12
|
||||||
|
|
||||||
|
const static unsigned int GLUE_RET[]={0x4E800020}; // blr
|
||||||
|
|
||||||
|
static int GLUE_RESET_WTP(unsigned char *out, void *ptr)
|
||||||
|
{
|
||||||
|
const static unsigned int GLUE_SET_WTP_FROM_R17=0x7E308B78; // mr r16 (dest), r17 (src)
|
||||||
|
if (out) memcpy(out,&GLUE_SET_WTP_FROM_R17,sizeof(GLUE_SET_WTP_FROM_R17));
|
||||||
|
return sizeof(GLUE_SET_WTP_FROM_R17);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// stwu r3, -16(r1)
|
||||||
|
const static unsigned int GLUE_PUSH_P1[1]={ 0x9461FFF0};
|
||||||
|
|
||||||
|
|
||||||
|
#define GLUE_POP_PX_SIZE 8
|
||||||
|
static void GLUE_POP_PX(void *b, int wv)
|
||||||
|
{
|
||||||
|
static const unsigned int tab[3] ={
|
||||||
|
0x80610000, // lwz r3, 0(r1)
|
||||||
|
0x81c10000, // lwz r14, 0(r1)
|
||||||
|
0x81e10000, // lwz r15, 0(r1)
|
||||||
|
};
|
||||||
|
((unsigned int *)b)[0] = tab[wv];
|
||||||
|
((unsigned int *)b)[1] = 0x38210010; // addi r1,r1, 16
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_SET_PX_FROM_P1_SIZE 4
|
||||||
|
static void GLUE_SET_PX_FROM_P1(void *b, int wv)
|
||||||
|
{
|
||||||
|
static const unsigned int tab[3]={
|
||||||
|
0x7c631b78, // never used: mr r3, r3
|
||||||
|
0x7c6e1b78, // mr r14, r3
|
||||||
|
0x7c6f1b78, // mr r15, r3
|
||||||
|
};
|
||||||
|
*(unsigned int *)b = tab[wv];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// lfd f2, 0(r3)
|
||||||
|
// stfdu f2, -16(r1)
|
||||||
|
static const unsigned int GLUE_PUSH_P1PTR_AS_VALUE[] = { 0xC8430000, 0xDC41FFF0 };
|
||||||
|
|
||||||
|
static int GLUE_POP_VALUE_TO_ADDR(unsigned char *buf, void *destptr)
|
||||||
|
{
|
||||||
|
// lfd f2, 0(r1)
|
||||||
|
// addi r1,r1,16
|
||||||
|
// GLUE_MOV_PX_DIRECTVALUE_GEN / GLUE_MOV_PX_DIRECTVALUE_SIZE (r3)
|
||||||
|
// stfd f2, 0(r3)
|
||||||
|
if (buf)
|
||||||
|
{
|
||||||
|
unsigned int *bufptr = (unsigned int *)buf;
|
||||||
|
*bufptr++ = 0xC8410000;
|
||||||
|
*bufptr++ = 0x38210010;
|
||||||
|
GLUE_MOV_PX_DIRECTVALUE_GEN(bufptr, (INT_PTR)destptr,0);
|
||||||
|
bufptr += GLUE_MOV_PX_DIRECTVALUE_SIZE/4;
|
||||||
|
*bufptr++ = 0xd8430000;
|
||||||
|
}
|
||||||
|
return 2*4 + GLUE_MOV_PX_DIRECTVALUE_SIZE + 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int GLUE_COPY_VALUE_AT_P1_TO_PTR(unsigned char *buf, void *destptr)
|
||||||
|
{
|
||||||
|
// lfd f2, 0(r3)
|
||||||
|
// GLUE_MOV_PX_DIRECTVALUE_GEN / GLUE_MOV_PX_DIRECTVALUE_SIZE (r3)
|
||||||
|
// stfd f2, 0(r3)
|
||||||
|
|
||||||
|
if (buf)
|
||||||
|
{
|
||||||
|
unsigned int *bufptr = (unsigned int *)buf;
|
||||||
|
*bufptr++ = 0xc8430000;
|
||||||
|
GLUE_MOV_PX_DIRECTVALUE_GEN(bufptr, (INT_PTR)destptr,0);
|
||||||
|
bufptr += GLUE_MOV_PX_DIRECTVALUE_SIZE/4;
|
||||||
|
*bufptr++ = 0xd8430000;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 4 + GLUE_MOV_PX_DIRECTVALUE_SIZE + 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void GLUE_CALL_CODE(INT_PTR bp, INT_PTR cp, INT_PTR rt)
|
||||||
|
{
|
||||||
|
static const double consttab[] = {
|
||||||
|
NSEEL_CLOSEFACTOR,
|
||||||
|
4503601774854144.0 /* 0x43300000, 0x80000000, used for integer conversion*/,
|
||||||
|
};
|
||||||
|
// we could have r18 refer to the current user-stack pointer, someday, perhaps
|
||||||
|
__asm__(
|
||||||
|
"subi r1, r1, 128\n"
|
||||||
|
"stfd f31, 8(r1)\n"
|
||||||
|
"stfd f30, 16(r1)\n"
|
||||||
|
"stmw r13, 32(r1)\n"
|
||||||
|
"mtctr %0\n"
|
||||||
|
"mr r17, %1\n"
|
||||||
|
"mr r13, %2\n"
|
||||||
|
"lfd f31, 0(%3)\n"
|
||||||
|
"lfd f30, 8(%3)\n"
|
||||||
|
"subi r17, r17, 8\n"
|
||||||
|
"mflr r0\n"
|
||||||
|
"stw r0, 24(r1)\n"
|
||||||
|
"bctrl\n"
|
||||||
|
"lwz r0, 24(r1)\n"
|
||||||
|
"mtlr r0\n"
|
||||||
|
"lmw r13, 32(r1)\n"
|
||||||
|
"lfd f31, 8(r1)\n"
|
||||||
|
"lfd f30, 16(r1)\n"
|
||||||
|
"addi r1, r1, 128\n"
|
||||||
|
::"r" (cp), "r" (bp), "r" (rt), "r" (consttab));
|
||||||
|
};
|
||||||
|
|
||||||
|
static unsigned char *EEL_GLUE_set_immediate(void *_p, INT_PTR newv)
|
||||||
|
{
|
||||||
|
// 64 bit ppc would take some work
|
||||||
|
unsigned int *p=(unsigned int *)_p;
|
||||||
|
while ((p[0]&0x0000FFFF) != 0x0000dead &&
|
||||||
|
(p[1]&0x0000FFFF) != 0x0000beef) p++;
|
||||||
|
p[0] = (p[0]&0xFFFF0000) | (((newv)>>16)&0xFFFF);
|
||||||
|
p[1] = (p[1]&0xFFFF0000) | ((newv)&0xFFFF);
|
||||||
|
|
||||||
|
return (unsigned char *)(p+1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_SET_PX_FROM_WTP_SIZE sizeof(int)
|
||||||
|
static void GLUE_SET_PX_FROM_WTP(void *b, int wv)
|
||||||
|
{
|
||||||
|
static const unsigned int tab[3]={
|
||||||
|
0x7e038378, // mr r3, r16
|
||||||
|
0x7e0e8378, // mr r14, r16
|
||||||
|
0x7e0f8378, // mr r15, r16
|
||||||
|
};
|
||||||
|
*(unsigned int *)b = tab[wv];
|
||||||
|
}
|
||||||
|
static int GLUE_POP_FPSTACK_TO_PTR(unsigned char *buf, void *destptr)
|
||||||
|
{
|
||||||
|
// set r3 to destptr
|
||||||
|
// stfd f1, 0(r3)
|
||||||
|
if (buf)
|
||||||
|
{
|
||||||
|
unsigned int *bufptr = (unsigned int *)buf;
|
||||||
|
GLUE_MOV_PX_DIRECTVALUE_GEN(bufptr, (INT_PTR)destptr,0);
|
||||||
|
bufptr += GLUE_MOV_PX_DIRECTVALUE_SIZE/4;
|
||||||
|
|
||||||
|
*bufptr++ = 0xD8230000; // stfd f1, 0(r3)
|
||||||
|
}
|
||||||
|
return GLUE_MOV_PX_DIRECTVALUE_SIZE + sizeof(int);
|
||||||
|
}
|
||||||
|
#define GLUE_POP_FPSTACK_SIZE 0
|
||||||
|
static const unsigned int GLUE_POP_FPSTACK[1] = { 0 }; // no need to pop, not a stack
|
||||||
|
|
||||||
|
static const unsigned int GLUE_POP_FPSTACK_TOSTACK[] = {
|
||||||
|
0xdc21fff0, // stfdu f1, -16(r1)
|
||||||
|
};
|
||||||
|
|
||||||
|
static const unsigned int GLUE_POP_FPSTACK_TO_WTP[] = {
|
||||||
|
0xdc300008, // stfdu f1, 8(r16)
|
||||||
|
};
|
||||||
|
|
||||||
|
#define GLUE_PUSH_VAL_AT_PX_TO_FPSTACK_SIZE 4
|
||||||
|
static void GLUE_PUSH_VAL_AT_PX_TO_FPSTACK(void *b, int wv)
|
||||||
|
{
|
||||||
|
static const unsigned int tab[3] = {
|
||||||
|
0xC8230000, // lfd f1, 0(r3)
|
||||||
|
0xC82E0000, // lfd f1, 0(r14)
|
||||||
|
0xC82F0000, // lfd f1, 0(r15)
|
||||||
|
};
|
||||||
|
*(unsigned int *)b = tab[wv];
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_POP_FPSTACK_TO_WTP_TO_PX_SIZE (sizeof(GLUE_POP_FPSTACK_TO_WTP) + GLUE_SET_PX_FROM_WTP_SIZE)
|
||||||
|
static void GLUE_POP_FPSTACK_TO_WTP_TO_PX(unsigned char *buf, int wv)
|
||||||
|
{
|
||||||
|
memcpy(buf,GLUE_POP_FPSTACK_TO_WTP,sizeof(GLUE_POP_FPSTACK_TO_WTP));
|
||||||
|
GLUE_SET_PX_FROM_WTP(buf + sizeof(GLUE_POP_FPSTACK_TO_WTP),wv); // ppc preincs the WTP, so we do this after
|
||||||
|
};
|
||||||
|
|
||||||
|
static unsigned int GLUE_POP_STACK_TO_FPSTACK[1] = { 0 }; // todo
|
||||||
|
|
||||||
|
|
||||||
|
static const unsigned int GLUE_SET_P1_Z[] = { 0x38600000 }; // li r3, 0
|
||||||
|
static const unsigned int GLUE_SET_P1_NZ[] = { 0x38600001 }; // li r3, 1
|
||||||
|
|
||||||
|
|
||||||
|
static void *GLUE_realAddress(void *fn, int *size)
|
||||||
|
{
|
||||||
|
// magic numbers: mr r0,r0 ; mr r1,r1 ; mr r2, r2
|
||||||
|
static const unsigned char sig[12] = { 0x7c, 0x00, 0x03, 0x78, 0x7c, 0x21, 0x0b, 0x78, 0x7c, 0x42, 0x13, 0x78 };
|
||||||
|
unsigned char *p = (unsigned char *)fn;
|
||||||
|
|
||||||
|
while (memcmp(p,sig,sizeof(sig))) p+=4;
|
||||||
|
p+=sizeof(sig);
|
||||||
|
fn = p;
|
||||||
|
|
||||||
|
while (memcmp(p,sig,sizeof(sig))) p+=4;
|
||||||
|
*size = p - (unsigned char *)fn;
|
||||||
|
return fn;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_STORE_P1_TO_STACK_AT_OFFS_SIZE(x) 4
|
||||||
|
static void GLUE_STORE_P1_TO_STACK_AT_OFFS(void *b, int offs)
|
||||||
|
{
|
||||||
|
// limited to 32k offset
|
||||||
|
*(unsigned int *)b = 0x90610000 + (offs&0xffff);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_MOVE_PX_STACKPTR_SIZE 4
|
||||||
|
static void GLUE_MOVE_PX_STACKPTR_GEN(void *b, int wv)
|
||||||
|
{
|
||||||
|
static const unsigned int tab[3] =
|
||||||
|
{
|
||||||
|
0x7c230b78, // mr r3, r1
|
||||||
|
0x7c2e0b78, // mr r14, r1
|
||||||
|
0x7c2f0b78, // mr r15, r1
|
||||||
|
};
|
||||||
|
* (unsigned int *)b = tab[wv];
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_MOVE_STACK_SIZE 4
|
||||||
|
static void GLUE_MOVE_STACK(void *b, int amt)
|
||||||
|
{
|
||||||
|
// this should be updated to allow for more than 32k moves, but no real need
|
||||||
|
((unsigned int *)b)[0] = 0x38210000 + (amt&0xffff); // addi r1,r1, amt
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// end of ppc
|
||||||
|
|
||||||
|
#endif
|
||||||
678
oversampling/WDL/eel2/glue_x86.h
Normal file
678
oversampling/WDL/eel2/glue_x86.h
Normal file
@@ -0,0 +1,678 @@
|
|||||||
|
#ifndef _NSEEL_GLUE_X86_H_
|
||||||
|
#define _NSEEL_GLUE_X86_H_
|
||||||
|
|
||||||
|
#define GLUE_MAX_FPSTACK_SIZE 8
|
||||||
|
|
||||||
|
// endOfInstruction is end of jump with relative offset, offset is offset from end of instruction to jump to
|
||||||
|
#define GLUE_JMP_SET_OFFSET(endOfInstruction,offset) (((int *)(endOfInstruction))[-1] = (offset))
|
||||||
|
|
||||||
|
static const unsigned char GLUE_JMP_NC[] = { 0xE9, 0,0,0,0, }; // jmp<offset>
|
||||||
|
static const unsigned char GLUE_JMP_IF_P1_Z[] = {0x85, 0xC0, 0x0F, 0x84, 0,0,0,0 }; // test eax, eax, jz
|
||||||
|
static const unsigned char GLUE_JMP_IF_P1_NZ[] = {0x85, 0xC0, 0x0F, 0x85, 0,0,0,0 }; // test eax, eax, jnz
|
||||||
|
|
||||||
|
#define GLUE_FUNC_ENTER_SIZE 0
|
||||||
|
#define GLUE_FUNC_LEAVE_SIZE 0
|
||||||
|
const static unsigned int GLUE_FUNC_ENTER[1];
|
||||||
|
const static unsigned int GLUE_FUNC_LEAVE[1];
|
||||||
|
|
||||||
|
// x86
|
||||||
|
// stack is 16 byte aligned
|
||||||
|
// when pushing values to stack, alignment pushed first, then value (value is at the lower address)
|
||||||
|
// when pushing pointers to stack, alignment pushed first, then pointer (pointer is at the lower address)
|
||||||
|
|
||||||
|
static const unsigned char GLUE_PUSH_P1PTR_AS_VALUE[] =
|
||||||
|
{
|
||||||
|
0x83, 0xEC, 8, /* sub esp, 8 */
|
||||||
|
0xff, 0x70, 0x4, /* push dword [eax+4] */
|
||||||
|
0xff, 0x30, /* push dword [eax] */
|
||||||
|
};
|
||||||
|
|
||||||
|
static int GLUE_POP_VALUE_TO_ADDR(unsigned char *buf, void *destptr)
|
||||||
|
{
|
||||||
|
if (buf)
|
||||||
|
{
|
||||||
|
*buf++ = 0xB8; *(void **) buf = destptr; buf+=4; // mov eax, directvalue
|
||||||
|
|
||||||
|
*buf++ = 0x8f; *buf++ = 0x00; // pop dword [eax]
|
||||||
|
*buf++ = 0x8f; *buf++ = 0x40; *buf++ = 4; // pop dword [eax+4]
|
||||||
|
|
||||||
|
*buf++ = 0x59; // pop ecx (alignment)
|
||||||
|
*buf++ = 0x59; // pop ecx (alignment)
|
||||||
|
}
|
||||||
|
|
||||||
|
return 12;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int GLUE_COPY_VALUE_AT_P1_TO_PTR(unsigned char *buf, void *destptr)
|
||||||
|
{
|
||||||
|
if (buf)
|
||||||
|
{
|
||||||
|
*buf++ = 0x8B; *buf++ = 0x38; // mov edi, [eax]
|
||||||
|
*buf++ = 0x8B; *buf++ = 0x48; *buf++ = 0x04; // mov ecx, [eax+4]
|
||||||
|
|
||||||
|
|
||||||
|
*buf++ = 0xB8; *(void **) buf = destptr; buf+=4; // mov eax, directvalue
|
||||||
|
*buf++ = 0x89; *buf++ = 0x38; // mov [eax], edi
|
||||||
|
*buf++ = 0x89; *buf++ = 0x48; *buf++ = 0x04; // mov [eax+4], ecx
|
||||||
|
}
|
||||||
|
|
||||||
|
return 2 + 3 + 5 + 2 + 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int GLUE_POP_FPSTACK_TO_PTR(unsigned char *buf, void *destptr)
|
||||||
|
{
|
||||||
|
if (buf)
|
||||||
|
{
|
||||||
|
*buf++ = 0xB8; *(void **) buf = destptr; buf+=4; // mov eax, directvalue
|
||||||
|
*buf++ = 0xDD; *buf++ = 0x18; // fstp qword [eax]
|
||||||
|
}
|
||||||
|
return 1+4+2;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#define GLUE_MOV_PX_DIRECTVALUE_SIZE 5
|
||||||
|
#define GLUE_MOV_PX_DIRECTVALUE_TOSTACK_SIZE 6 // length when wv == -1
|
||||||
|
|
||||||
|
static void GLUE_MOV_PX_DIRECTVALUE_GEN(void *b, INT_PTR v, int wv)
|
||||||
|
{
|
||||||
|
if (wv==-1)
|
||||||
|
{
|
||||||
|
const static unsigned char t[2] = {0xDD, 0x05};
|
||||||
|
memcpy(b,t,2);
|
||||||
|
b= ((unsigned char *)b)+2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
const static unsigned char tab[3] = {
|
||||||
|
0xB8 /* mov eax, dv*/,
|
||||||
|
0xBF /* mov edi, dv */ ,
|
||||||
|
0xB9 /* mov ecx, dv */
|
||||||
|
};
|
||||||
|
*((unsigned char *)b) = tab[wv]; // mov eax, dv
|
||||||
|
b= ((unsigned char *)b)+1;
|
||||||
|
}
|
||||||
|
*(INT_PTR *)b = v;
|
||||||
|
}
|
||||||
|
const static unsigned char GLUE_PUSH_P1[4]={0x83, 0xEC, 12, 0x50}; // sub esp, 12, push eax
|
||||||
|
|
||||||
|
#define GLUE_STORE_P1_TO_STACK_AT_OFFS_SIZE(x) 7
|
||||||
|
static void GLUE_STORE_P1_TO_STACK_AT_OFFS(void *b, int offs)
|
||||||
|
{
|
||||||
|
((unsigned char *)b)[0] = 0x89; // mov [esp+offs], eax
|
||||||
|
((unsigned char *)b)[1] = 0x84;
|
||||||
|
((unsigned char *)b)[2] = 0x24;
|
||||||
|
*(int *)((unsigned char *)b+3) = offs;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_MOVE_PX_STACKPTR_SIZE 2
|
||||||
|
static void GLUE_MOVE_PX_STACKPTR_GEN(void *b, int wv)
|
||||||
|
{
|
||||||
|
static const unsigned char tab[3][GLUE_MOVE_PX_STACKPTR_SIZE]=
|
||||||
|
{
|
||||||
|
{ 0x89, 0xe0 }, // mov eax, esp
|
||||||
|
{ 0x89, 0xe7 }, // mov edi, esp
|
||||||
|
{ 0x89, 0xe1 }, // mov ecx, esp
|
||||||
|
};
|
||||||
|
memcpy(b,tab[wv],GLUE_MOVE_PX_STACKPTR_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_MOVE_STACK_SIZE 6
|
||||||
|
static void GLUE_MOVE_STACK(void *b, int amt)
|
||||||
|
{
|
||||||
|
((unsigned char *)b)[0] = 0x81;
|
||||||
|
if (amt <0)
|
||||||
|
{
|
||||||
|
((unsigned char *)b)[1] = 0xEC;
|
||||||
|
*(int *)((char*)b+2) = -amt; // sub esp, -amt
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
((unsigned char *)b)[1] = 0xc4;
|
||||||
|
*(int *)((char*)b+2) = amt; // add esp, amt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#define GLUE_POP_PX_SIZE 4
|
||||||
|
static void GLUE_POP_PX(void *b, int wv)
|
||||||
|
{
|
||||||
|
static const unsigned char tab[3][GLUE_POP_PX_SIZE]=
|
||||||
|
{
|
||||||
|
{0x58,/*pop eax*/ 0x83, 0xC4, 12 /* add esp, 12*/},
|
||||||
|
{0x5F,/*pop edi*/ 0x83, 0xC4, 12},
|
||||||
|
{0x59,/*pop ecx*/ 0x83, 0xC4, 12},
|
||||||
|
};
|
||||||
|
memcpy(b,tab[wv],GLUE_POP_PX_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_SET_PX_FROM_P1_SIZE 2
|
||||||
|
static void GLUE_SET_PX_FROM_P1(void *b, int wv)
|
||||||
|
{
|
||||||
|
static const unsigned char tab[3][GLUE_SET_PX_FROM_P1_SIZE]={
|
||||||
|
{0x90,0x90}, // should never be used! (nopnop)
|
||||||
|
{0x89,0xC7}, // mov edi, eax
|
||||||
|
{0x89,0xC1}, // mov ecx, eax
|
||||||
|
};
|
||||||
|
memcpy(b,tab[wv],GLUE_SET_PX_FROM_P1_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_POP_FPSTACK_SIZE 2
|
||||||
|
static const unsigned char GLUE_POP_FPSTACK[2] = { 0xDD, 0xD8 }; // fstp st0
|
||||||
|
|
||||||
|
static const unsigned char GLUE_POP_FPSTACK_TOSTACK[] = {
|
||||||
|
0x83, 0xEC, 16, // sub esp, 16
|
||||||
|
0xDD, 0x1C, 0x24 // fstp qword (%esp)
|
||||||
|
};
|
||||||
|
|
||||||
|
static const unsigned char GLUE_POP_STACK_TO_FPSTACK[] = {
|
||||||
|
0xDD, 0x04, 0x24, // fld qword (%esp)
|
||||||
|
0x83, 0xC4, 16 // add esp, 16
|
||||||
|
};
|
||||||
|
|
||||||
|
static const unsigned char GLUE_POP_FPSTACK_TO_WTP[] = {
|
||||||
|
0xDD, 0x1E, /* fstp qword [esi] */
|
||||||
|
0x83, 0xC6, 8, /* add esi, 8 */
|
||||||
|
};
|
||||||
|
|
||||||
|
#define GLUE_SET_PX_FROM_WTP_SIZE 2
|
||||||
|
static void GLUE_SET_PX_FROM_WTP(void *b, int wv)
|
||||||
|
{
|
||||||
|
static const unsigned char tab[3][GLUE_SET_PX_FROM_WTP_SIZE]={
|
||||||
|
{0x89,0xF0}, // mov eax, esi
|
||||||
|
{0x89,0xF7}, // mov edi, esi
|
||||||
|
{0x89,0xF1}, // mov ecx, esi
|
||||||
|
};
|
||||||
|
memcpy(b,tab[wv],GLUE_SET_PX_FROM_WTP_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_PUSH_VAL_AT_PX_TO_FPSTACK_SIZE 2
|
||||||
|
static void GLUE_PUSH_VAL_AT_PX_TO_FPSTACK(void *b, int wv)
|
||||||
|
{
|
||||||
|
static const unsigned char tab[3][GLUE_PUSH_VAL_AT_PX_TO_FPSTACK_SIZE]={
|
||||||
|
{0xDD,0x00}, // fld qword [eax]
|
||||||
|
{0xDD,0x07}, // fld qword [edi]
|
||||||
|
{0xDD,0x01}, // fld qword [ecx]
|
||||||
|
};
|
||||||
|
memcpy(b,tab[wv],GLUE_PUSH_VAL_AT_PX_TO_FPSTACK_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_POP_FPSTACK_TO_WTP_TO_PX_SIZE (GLUE_SET_PX_FROM_WTP_SIZE + sizeof(GLUE_POP_FPSTACK_TO_WTP))
|
||||||
|
static void GLUE_POP_FPSTACK_TO_WTP_TO_PX(unsigned char *buf, int wv)
|
||||||
|
{
|
||||||
|
GLUE_SET_PX_FROM_WTP(buf,wv);
|
||||||
|
memcpy(buf + GLUE_SET_PX_FROM_WTP_SIZE,GLUE_POP_FPSTACK_TO_WTP,sizeof(GLUE_POP_FPSTACK_TO_WTP));
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
const static unsigned char GLUE_RET=0xC3;
|
||||||
|
|
||||||
|
static int GLUE_RESET_WTP(unsigned char *out, void *ptr)
|
||||||
|
{
|
||||||
|
if (out)
|
||||||
|
{
|
||||||
|
*out++ = 0xBE; // mov esi, constant
|
||||||
|
memcpy(out,&ptr,sizeof(void *));
|
||||||
|
out+=sizeof(void *);
|
||||||
|
}
|
||||||
|
return 1+sizeof(void *);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#pragma warning(push)
|
||||||
|
#pragma warning(disable: 4731)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define GLUE_TABPTR_IGNORED
|
||||||
|
#define GLUE_CALL_CODE(bp, cp, rt) do { \
|
||||||
|
if (h->compile_flags&NSEEL_CODE_COMPILE_FLAG_NOFPSTATE) eel_callcode32_fast(cp, rt); \
|
||||||
|
else eel_callcode32(cp, rt);\
|
||||||
|
} while(0)
|
||||||
|
|
||||||
|
static void eel_callcode32(INT_PTR cp, INT_PTR ramptr)
|
||||||
|
{
|
||||||
|
#ifndef NSEEL_EEL1_COMPAT_MODE
|
||||||
|
short oldsw, newsw;
|
||||||
|
#endif
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
|
||||||
|
__asm
|
||||||
|
{
|
||||||
|
#ifndef NSEEL_EEL1_COMPAT_MODE
|
||||||
|
fnstcw [oldsw]
|
||||||
|
mov ax, [oldsw]
|
||||||
|
or ax, 0x23F // 53 or 64 bit precision (depending on whether 0x100 is set), and masking all exceptions
|
||||||
|
mov [newsw], ax
|
||||||
|
fldcw [newsw]
|
||||||
|
#endif
|
||||||
|
|
||||||
|
mov eax, cp
|
||||||
|
mov ebx, ramptr
|
||||||
|
|
||||||
|
pushad
|
||||||
|
mov ebp, esp
|
||||||
|
and esp, -16
|
||||||
|
|
||||||
|
// on win32, which _MSC_VER implies, we keep things aligned to 16 bytes, and if we call a win32 function,
|
||||||
|
// the stack is 16 byte aligned before the call, meaning that if calling a function with no frame pointer,
|
||||||
|
// the stack would be aligned to a 16 byte boundary +4, which isn't good for performance. Having said that,
|
||||||
|
// normally we compile with frame pointers (which brings that to 16 byte + 8, which is fine), or ICC, which
|
||||||
|
// for nontrivial functions will align the stack itself (for very short functions, it appears to weigh the
|
||||||
|
// cost of aligning the stack vs that of the slower misaligned double accesses).
|
||||||
|
|
||||||
|
// it may be worthwhile (at some point) to put some logic in the code that calls out to functions
|
||||||
|
// (generic1parm etc) to detect which alignment would be most optimal.
|
||||||
|
sub esp, 12
|
||||||
|
call eax
|
||||||
|
mov esp, ebp
|
||||||
|
popad
|
||||||
|
#ifndef NSEEL_EEL1_COMPAT_MODE
|
||||||
|
fldcw [oldsw]
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
#else // gcc x86
|
||||||
|
__asm__(
|
||||||
|
#ifndef NSEEL_EEL1_COMPAT_MODE
|
||||||
|
"fnstcw %2\n"
|
||||||
|
"movw %2, %%ax\n"
|
||||||
|
"orw $0x23F, %%ax\n" // 53 or 64 bit precision (depending on whether 0x100 is set), and masking all exceptions
|
||||||
|
"movw %%ax, %3\n"
|
||||||
|
"fldcw %3\n"
|
||||||
|
#endif
|
||||||
|
"pushl %%ebx\n"
|
||||||
|
"movl %%ecx, %%ebx\n"
|
||||||
|
"pushl %%ebp\n"
|
||||||
|
"movl %%esp, %%ebp\n"
|
||||||
|
"andl $-16, %%esp\n" // align stack to 16 bytes
|
||||||
|
"subl $12, %%esp\n" // call will push 4 bytes on stack, align for that
|
||||||
|
"call *%%edx\n"
|
||||||
|
"leave\n"
|
||||||
|
"popl %%ebx\n"
|
||||||
|
#ifndef NSEEL_EEL1_COMPAT_MODE
|
||||||
|
"fldcw %2\n"
|
||||||
|
#endif
|
||||||
|
::
|
||||||
|
"d" (cp), "c" (ramptr)
|
||||||
|
#ifndef NSEEL_EEL1_COMPAT_MODE
|
||||||
|
, "m" (oldsw), "m" (newsw)
|
||||||
|
#endif
|
||||||
|
: "%eax","%esi","%edi");
|
||||||
|
#endif //gcc x86
|
||||||
|
}
|
||||||
|
|
||||||
|
void eel_enterfp(int s[2])
|
||||||
|
{
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
__asm
|
||||||
|
{
|
||||||
|
mov ecx, s
|
||||||
|
fnstcw [ecx]
|
||||||
|
mov ax, [ecx]
|
||||||
|
or ax, 0x23F // 53 or 64 bit precision (depending on whether 0x100 is set), and masking all exceptions
|
||||||
|
mov [ecx+4], ax
|
||||||
|
fldcw [ecx+4]
|
||||||
|
};
|
||||||
|
#else
|
||||||
|
__asm__(
|
||||||
|
"fnstcw (%%ecx)\n"
|
||||||
|
"movw (%%ecx), %%ax\n"
|
||||||
|
"orw $0x23F, %%ax\n" // 53 or 64 bit precision (depending on whether 0x100 is set), and masking all exceptions
|
||||||
|
"movw %%ax, 4(%%ecx)\n"
|
||||||
|
"fldcw 4(%%ecx)\n"
|
||||||
|
:: "c" (s) : "%eax");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
void eel_leavefp(int s[2])
|
||||||
|
{
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
__asm
|
||||||
|
{
|
||||||
|
mov ecx, s
|
||||||
|
fldcw [ecx]
|
||||||
|
};
|
||||||
|
#else
|
||||||
|
__asm__(
|
||||||
|
"fldcw (%%ecx)\n"
|
||||||
|
:: "c" (s) : "%eax");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static void eel_callcode32_fast(INT_PTR cp, INT_PTR ramptr)
|
||||||
|
{
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
|
||||||
|
__asm
|
||||||
|
{
|
||||||
|
mov eax, cp
|
||||||
|
mov ebx, ramptr
|
||||||
|
|
||||||
|
pushad
|
||||||
|
mov ebp, esp
|
||||||
|
and esp, -16
|
||||||
|
|
||||||
|
// on win32, which _MSC_VER implies, we keep things aligned to 16 bytes, and if we call a win32 function,
|
||||||
|
// the stack is 16 byte aligned before the call, meaning that if calling a function with no frame pointer,
|
||||||
|
// the stack would be aligned to a 16 byte boundary +4, which isn't good for performance. Having said that,
|
||||||
|
// normally we compile with frame pointers (which brings that to 16 byte + 8, which is fine), or ICC, which
|
||||||
|
// for nontrivial functions will align the stack itself (for very short functions, it appears to weigh the
|
||||||
|
// cost of aligning the stack vs that of the slower misaligned double accesses).
|
||||||
|
|
||||||
|
// it may be worthwhile (at some point) to put some logic in the code that calls out to functions
|
||||||
|
// (generic1parm etc) to detect which alignment would be most optimal.
|
||||||
|
sub esp, 12
|
||||||
|
call eax
|
||||||
|
mov esp, ebp
|
||||||
|
popad
|
||||||
|
};
|
||||||
|
|
||||||
|
#else // gcc x86
|
||||||
|
__asm__(
|
||||||
|
"pushl %%ebx\n"
|
||||||
|
"movl %%ecx, %%ebx\n"
|
||||||
|
"pushl %%ebp\n"
|
||||||
|
"movl %%esp, %%ebp\n"
|
||||||
|
"andl $-16, %%esp\n" // align stack to 16 bytes
|
||||||
|
"subl $12, %%esp\n" // call will push 4 bytes on stack, align for that
|
||||||
|
"call *%%edx\n"
|
||||||
|
"leave\n"
|
||||||
|
"popl %%ebx\n"
|
||||||
|
::
|
||||||
|
"d" (cp), "c" (ramptr)
|
||||||
|
: "%eax","%esi","%edi");
|
||||||
|
#endif //gcc x86
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#pragma warning(pop)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
static unsigned char *EEL_GLUE_set_immediate(void *_p, INT_PTR newv)
|
||||||
|
{
|
||||||
|
char *p=(char*)_p;
|
||||||
|
INT_PTR scan = 0xFEFEFEFE;
|
||||||
|
while (*(INT_PTR *)p != scan) p++;
|
||||||
|
*(INT_PTR *)p = newv;
|
||||||
|
return (unsigned char *) (((INT_PTR*)p)+1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define INT_TO_LECHARS(x) ((x)&0xff),(((x)>>8)&0xff), (((x)>>16)&0xff), (((x)>>24)&0xff)
|
||||||
|
|
||||||
|
|
||||||
|
#define GLUE_INLINE_LOOPS
|
||||||
|
|
||||||
|
#define GLUE_LOOP_LOADCNT_SIZE (nseel_has_sse3() ? sizeof(GLUE_LOOP_LOADCNT_SSE3) : sizeof(GLUE_LOOP_LOADCNT_NOSSE3))
|
||||||
|
#define GLUE_LOOP_LOADCNT (nseel_has_sse3() ? GLUE_LOOP_LOADCNT_SSE3 : GLUE_LOOP_LOADCNT_NOSSE3)
|
||||||
|
static const unsigned char GLUE_LOOP_LOADCNT_SSE3[]={
|
||||||
|
0xdb, 0x0e, // fisttp dword [esi]
|
||||||
|
0x8B, 0x0E, // mov ecx, [esi]
|
||||||
|
0x81, 0xf9, 1,0,0,0, // cmp ecx, 1
|
||||||
|
0x0F, 0x8C, 0,0,0,0, // JL <skipptr>
|
||||||
|
};
|
||||||
|
|
||||||
|
static const unsigned char GLUE_LOOP_LOADCNT_NOSSE3[]={
|
||||||
|
0xd9, 0x7e, 0x04, // fnstcw [esi+4]
|
||||||
|
0x66, 0x8b, 0x46, 0x04, // mov ax, [esi+4]
|
||||||
|
0x66, 0x0d, 0x00, 0x0c, // or ax, 0xC00
|
||||||
|
0x66, 0x89, 0x46, 0x08, // mov [esi+8], ax
|
||||||
|
0xd9, 0x6e, 0x08, // fldcw [esi+8]
|
||||||
|
0xDB, 0x1E, // fistp dword [esi]
|
||||||
|
0xd9, 0x6e, 0x04, // fldcw [esi+4]
|
||||||
|
0x8B, 0x0E, // mov ecx, [esi]
|
||||||
|
0x81, 0xf9, 1,0,0,0, // cmp ecx, 1
|
||||||
|
0x0F, 0x8C, 0,0,0,0, // JL <skipptr>
|
||||||
|
};
|
||||||
|
|
||||||
|
#if NSEEL_LOOPFUNC_SUPPORT_MAXLEN > 0
|
||||||
|
#define GLUE_LOOP_CLAMPCNT_SIZE sizeof(GLUE_LOOP_CLAMPCNT)
|
||||||
|
static const unsigned char GLUE_LOOP_CLAMPCNT[]={
|
||||||
|
0x81, 0xf9, INT_TO_LECHARS(NSEEL_LOOPFUNC_SUPPORT_MAXLEN), // cmp ecx, NSEEL_LOOPFUNC_SUPPORT_MAXLEN
|
||||||
|
0x0F, 0x8C, 5,0,0,0, // JL over-the-mov
|
||||||
|
0xB9, INT_TO_LECHARS(NSEEL_LOOPFUNC_SUPPORT_MAXLEN), // mov ecx, NSEEL_LOOPFUNC_SUPPORT_MAXLEN
|
||||||
|
};
|
||||||
|
#else
|
||||||
|
|
||||||
|
#define GLUE_LOOP_CLAMPCNT_SIZE 0
|
||||||
|
#define GLUE_LOOP_CLAMPCNT ""
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define GLUE_LOOP_BEGIN_SIZE sizeof(GLUE_LOOP_BEGIN)
|
||||||
|
static const unsigned char GLUE_LOOP_BEGIN[]={
|
||||||
|
0x56, //push esi
|
||||||
|
0x51, // push ecx
|
||||||
|
0x81, 0xEC, 0x08, 0,0,0, // sub esp, 8
|
||||||
|
};
|
||||||
|
static const unsigned char GLUE_LOOP_END[]={
|
||||||
|
0x81, 0xC4, 0x08, 0,0,0, // add esp, 8
|
||||||
|
0x59, //pop ecx
|
||||||
|
0x5E, // pop esi
|
||||||
|
0x49, // dec ecx
|
||||||
|
0x0f, 0x85, 0,0,0,0, // jnz ...
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#if NSEEL_LOOPFUNC_SUPPORT_MAXLEN > 0
|
||||||
|
#define GLUE_WHILE_SETUP_SIZE sizeof(GLUE_WHILE_SETUP)
|
||||||
|
static const unsigned char GLUE_WHILE_SETUP[]={
|
||||||
|
0xB9, INT_TO_LECHARS(NSEEL_LOOPFUNC_SUPPORT_MAXLEN), // mov ecx, NSEEL_LOOPFUNC_SUPPORT_MAXLEN
|
||||||
|
};
|
||||||
|
static const unsigned char GLUE_WHILE_BEGIN[]={
|
||||||
|
0x56, //push esi
|
||||||
|
0x51, // push ecx
|
||||||
|
0x81, 0xEC, 0x08, 0,0,0, // sub esp, 8
|
||||||
|
};
|
||||||
|
static const unsigned char GLUE_WHILE_END[]={
|
||||||
|
0x81, 0xC4, 0x08, 0,0,0, // add esp, 8
|
||||||
|
0x59, //pop ecx
|
||||||
|
0x5E, // pop esi
|
||||||
|
|
||||||
|
|
||||||
|
0x49, // dec ecx
|
||||||
|
0x0f, 0x84, 0,0,0,0, // jz endpt
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#define GLUE_WHILE_SETUP_SIZE 0
|
||||||
|
#define GLUE_WHILE_SETUP ""
|
||||||
|
#define GLUE_WHILE_END_NOJUMP
|
||||||
|
static const unsigned char GLUE_WHILE_BEGIN[]={
|
||||||
|
0x56, //push esi
|
||||||
|
0x81, 0xEC, 12, 0,0,0, // sub esp, 12
|
||||||
|
};
|
||||||
|
static const unsigned char GLUE_WHILE_END[]={
|
||||||
|
0x81, 0xC4, 12, 0,0,0, // add esp, 12
|
||||||
|
0x5E, // pop esi
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static const unsigned char GLUE_WHILE_CHECK_RV[] = {
|
||||||
|
0x85, 0xC0, // test eax, eax
|
||||||
|
0x0F, 0x85, 0,0,0,0 // jnz looppt
|
||||||
|
};
|
||||||
|
|
||||||
|
static const unsigned char GLUE_SET_P1_Z[] = { 0x29, 0xC0 }; // sub eax, eax
|
||||||
|
static const unsigned char GLUE_SET_P1_NZ[] = { 0xb0, 0x01 }; // mov al, 1
|
||||||
|
|
||||||
|
static const unsigned char GLUE_FXCH[] = {0xd9, 0xc9};
|
||||||
|
|
||||||
|
#define GLUE_HAS_FLDZ
|
||||||
|
static const unsigned char GLUE_FLDZ[] = {0xd9, 0xee};
|
||||||
|
#define GLUE_HAS_FLD1
|
||||||
|
static const unsigned char GLUE_FLD1[] = {0xd9, 0xe8};
|
||||||
|
|
||||||
|
static EEL_F negativezeropointfive=-0.5f;
|
||||||
|
static EEL_F onepointfive=1.5f;
|
||||||
|
#define GLUE_INVSQRT_NEEDREPL &negativezeropointfive, &onepointfive,
|
||||||
|
|
||||||
|
|
||||||
|
#define GLUE_HAS_NATIVE_TRIGSQRTLOG
|
||||||
|
|
||||||
|
|
||||||
|
void nseel_asm_or(void);
|
||||||
|
void nseel_asm_or0(void);
|
||||||
|
void nseel_asm_or_op(void);
|
||||||
|
void nseel_asm_and(void);
|
||||||
|
void nseel_asm_and_op(void);
|
||||||
|
void nseel_asm_xor(void);
|
||||||
|
void nseel_asm_xor_op(void);
|
||||||
|
void nseel_asm_shl(void);
|
||||||
|
void nseel_asm_shr(void);
|
||||||
|
void nseel_asm_mod(void);
|
||||||
|
void nseel_asm_mod_op(void);
|
||||||
|
void nseel_asm_stack_peek(void);
|
||||||
|
void _asm_gmegabuf(void);
|
||||||
|
void _asm_megabuf(void);
|
||||||
|
|
||||||
|
static struct roundinftab {
|
||||||
|
void *fn;
|
||||||
|
char istores; // number of fistp's
|
||||||
|
char flag; // 0=fistpll, 1=fistpl, 2=fistpl 4(%esp)
|
||||||
|
int newsz;
|
||||||
|
void *newfn;
|
||||||
|
} s_round_fixes[] = {
|
||||||
|
{ nseel_asm_or, 2, },
|
||||||
|
{ nseel_asm_or_op, 2, },
|
||||||
|
{ nseel_asm_or0, 1, },
|
||||||
|
{ nseel_asm_and, 2, },
|
||||||
|
{ nseel_asm_and_op, 2, },
|
||||||
|
{ nseel_asm_xor, 2, },
|
||||||
|
{ nseel_asm_xor_op, 2, },
|
||||||
|
{ nseel_asm_shl, 2, 1, },
|
||||||
|
{ nseel_asm_shr, 2, 1, },
|
||||||
|
{ nseel_asm_mod, 2, 1, },
|
||||||
|
{ nseel_asm_mod_op, 2, 1, },
|
||||||
|
{ nseel_asm_stack_peek, 1, 1, },
|
||||||
|
{ _asm_megabuf, 1, 1, },
|
||||||
|
{ _asm_gmegabuf, 1, 2, },
|
||||||
|
};
|
||||||
|
|
||||||
|
static void eel_fixup_sse3(unsigned char *p, unsigned char *endp, int np, int flag)
|
||||||
|
{
|
||||||
|
const int isz = flag == 2 ? 3 : 2;
|
||||||
|
while (p+isz <= endp && np > 0)
|
||||||
|
{
|
||||||
|
if (flag == 0 && p[0] == 0xdf && (p[1]&0xbe) == 0x3e)
|
||||||
|
{
|
||||||
|
*p++ = 0xdd;
|
||||||
|
*p -= 0x30;
|
||||||
|
if (*p & 0x40) p++;
|
||||||
|
np--;
|
||||||
|
}
|
||||||
|
else if (flag == 1 && p[0] == 0xdb && (p[1]&0xbe) == 0x1e)
|
||||||
|
{
|
||||||
|
*++p -= 0x10;
|
||||||
|
if (*p & 0x40) p++;
|
||||||
|
np--;
|
||||||
|
}
|
||||||
|
else if (flag == 2 && p[0] == 0xdb && (p[1]&0xbf) == 0x1c && p[2] == 0x24)
|
||||||
|
{
|
||||||
|
*++p -= 0x10;
|
||||||
|
if (*p & 0x40) p++;
|
||||||
|
p++;
|
||||||
|
np--;
|
||||||
|
}
|
||||||
|
p++;
|
||||||
|
}
|
||||||
|
WDL_ASSERT(np == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int nseel_has_sse3()
|
||||||
|
{
|
||||||
|
static char c;
|
||||||
|
if (!c)
|
||||||
|
{
|
||||||
|
int features = 1;
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
__asm {
|
||||||
|
mov eax, 1
|
||||||
|
cpuid
|
||||||
|
mov [features], ecx
|
||||||
|
};
|
||||||
|
#else
|
||||||
|
__asm__(
|
||||||
|
"movl $1, %%eax\n"
|
||||||
|
"pushl %%ebx\n"
|
||||||
|
"pushl %%edx\n"
|
||||||
|
"cpuid\n"
|
||||||
|
"popl %%edx\n"
|
||||||
|
"popl %%ebx\n"
|
||||||
|
: "=c" (features) : : "%eax");
|
||||||
|
#endif
|
||||||
|
c=(features&1) ? 1 : -1;
|
||||||
|
}
|
||||||
|
return c>0;
|
||||||
|
}
|
||||||
|
static void *GLUE_realAddress(void *fn, int *size)
|
||||||
|
{
|
||||||
|
static const unsigned char sig[12] = { 0x89, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90 };
|
||||||
|
unsigned char *p = (unsigned char *)fn;
|
||||||
|
|
||||||
|
size_t rmatch;
|
||||||
|
const size_t nmatch = sizeof(s_round_fixes) / sizeof(s_round_fixes[0]);
|
||||||
|
for (rmatch = 0; rmatch < nmatch && s_round_fixes[rmatch].fn != fn; rmatch++);
|
||||||
|
if (rmatch < nmatch && s_round_fixes[rmatch].newfn && s_round_fixes[rmatch].newsz)
|
||||||
|
{
|
||||||
|
*size = s_round_fixes[rmatch].newsz;
|
||||||
|
return s_round_fixes[rmatch].newfn;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(_DEBUG) && defined(_MSC_VER)
|
||||||
|
if (*p == 0xE9) // this means jump to the following address (debug stub)
|
||||||
|
{
|
||||||
|
p += 5 + *(int *)(p+1);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
while (memcmp(p,sig,sizeof(sig))) p++;
|
||||||
|
p+=sizeof(sig);
|
||||||
|
fn = p;
|
||||||
|
|
||||||
|
while (memcmp(p,sig,sizeof(sig))) p++;
|
||||||
|
*size = p - (unsigned char *)fn;
|
||||||
|
|
||||||
|
if (rmatch < nmatch)
|
||||||
|
{
|
||||||
|
static const unsigned char prefix[] = {
|
||||||
|
0xd9, 0x7e, 0x10, // fnstcw [esi+16]
|
||||||
|
0x66, 0x8b, 0x46, 0x10, // mov ax, [esi+16]
|
||||||
|
0x66, 0x0d, 0x00, 0x0c, // or ax, 0xC00
|
||||||
|
0x66, 0x89, 0x46, 0x14, // mov [esi+20], ax
|
||||||
|
0xd9, 0x6e, 0x14, // fldcw [esi+20]
|
||||||
|
};
|
||||||
|
static const unsigned char postfix[] = {
|
||||||
|
0xd9, 0x6e, 0x10, // fldcw [esi+16]
|
||||||
|
};
|
||||||
|
|
||||||
|
const int has_sse = nseel_has_sse3();
|
||||||
|
|
||||||
|
unsigned char *tmp = (unsigned char *) malloc(*size + (has_sse ? 0 : sizeof(prefix) + sizeof(postfix)));
|
||||||
|
if (tmp)
|
||||||
|
{
|
||||||
|
if (has_sse)
|
||||||
|
{
|
||||||
|
memcpy(tmp,fn,*size);
|
||||||
|
eel_fixup_sse3(tmp,tmp + *size,s_round_fixes[rmatch].istores, s_round_fixes[rmatch].flag);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
memcpy(tmp,prefix,sizeof(prefix));
|
||||||
|
memcpy(tmp+sizeof(prefix),fn,*size);
|
||||||
|
memcpy(tmp+sizeof(prefix)+*size,postfix,sizeof(postfix));
|
||||||
|
|
||||||
|
*size += sizeof(prefix) + sizeof(postfix);
|
||||||
|
}
|
||||||
|
fn = tmp;
|
||||||
|
s_round_fixes[rmatch].newsz = *size;
|
||||||
|
s_round_fixes[rmatch].newfn = fn;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fn;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
638
oversampling/WDL/eel2/glue_x86_64_sse.h
Normal file
638
oversampling/WDL/eel2/glue_x86_64_sse.h
Normal file
@@ -0,0 +1,638 @@
|
|||||||
|
#ifndef _NSEEL_GLUE_X86_64_SSE_H_
|
||||||
|
#define _NSEEL_GLUE_X86_64_SSE_H_
|
||||||
|
|
||||||
|
// SSE version (needs the appropriate .o linked!)
|
||||||
|
#define GLUE_MOD_IS_64
|
||||||
|
#define GLUE_PREFER_NONFP_DV_ASSIGNS
|
||||||
|
#define GLUE_HAS_FPREG2 1
|
||||||
|
|
||||||
|
static const unsigned int GLUE_COPY_FPSTACK_TO_FPREG2[] = { 0xc8100ff2 }; // movsd %xmm0,%xmm1
|
||||||
|
static unsigned char GLUE_POP_STACK_TO_FPREG2[] = {
|
||||||
|
0xf2, 0x0f, 0x10, 0x0c, 0x24, // movsd (%rsp), %xmm1
|
||||||
|
0x48, 0x81, 0xC4, 16, 0,0,0, // add rsp, 16
|
||||||
|
};
|
||||||
|
|
||||||
|
// spill registers
|
||||||
|
#define GLUE_MAX_SPILL_REGS 4
|
||||||
|
#ifdef _WIN32
|
||||||
|
// win64: xmm6-xmm15 are nonvolatile, so we use xmm6-xmm9 as spill registers (xmm8/xmm9 are 5 byte encodings)
|
||||||
|
#define GLUE_SAVE_TO_SPILL_SIZE(x) (4 + ((x)>1))
|
||||||
|
#define GLUE_RESTORE_SPILL_TO_FPREG2_SIZE(x) (4 + ((x)>1))
|
||||||
|
|
||||||
|
static void GLUE_RESTORE_SPILL_TO_FPREG2(void *b, int ws)
|
||||||
|
{
|
||||||
|
if (ws < 2)
|
||||||
|
{
|
||||||
|
*(unsigned int *)b = 0xce100ff2 + (ws<<24); // movsd xmm1, xmm6+ws
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// movsd xmm1, xmm8 + (ws-2)
|
||||||
|
*(unsigned int *)b = 0x100f41f2;
|
||||||
|
((unsigned char *)b)[4] = 0xc8 + (ws-2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static void GLUE_SAVE_TO_SPILL(void *b, int ws)
|
||||||
|
{
|
||||||
|
if (ws < 2)
|
||||||
|
{
|
||||||
|
*(unsigned int *)b = 0xf0100ff2 + (ws<<27); // movsd xmm6+ws, xmm0
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// movsd xmm8+(ws-2), xmm0
|
||||||
|
*(unsigned int *)b = 0x100f44f2;
|
||||||
|
((unsigned char *)b)[4] = 0xc0 + ((ws-2)<<3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
// non-win32: our function stubs preserve xmm4-xmm7
|
||||||
|
|
||||||
|
#ifdef _DEBUG
|
||||||
|
#define GLUE_VALIDATE_SPILLS
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef GLUE_VALIDATE_SPILLS
|
||||||
|
|
||||||
|
static unsigned char save_validate[]={
|
||||||
|
0x48,0x83,0xec,0x10, // subq $16, %rsp
|
||||||
|
0xf2,0x0f,0x11,0x04,0x24, // movsd %xmm0, (%rsp)
|
||||||
|
0x66,0x48,0x0f,0x6e,0xe4, // movq %rsp, %xmm4 (+ws<<3)
|
||||||
|
};
|
||||||
|
|
||||||
|
static unsigned char restore_validate[] = {
|
||||||
|
0xf2, 0x0f, 0x10, 0xcc, // movsd %xmm7, %xmm1 (+ws)
|
||||||
|
0x66, 0x48, 0x0f, 0x6e, 0xdc, // movq %rsp, %xmm3
|
||||||
|
0x66, 0x0f, 0x2e, 0xd9, // ucomisd %xmm1, %xmm3
|
||||||
|
0x74, 0x02, // je 2 <skip>
|
||||||
|
0xcd, 0x03, // int $3
|
||||||
|
0xf2, 0x0f, 0x10, 0x0c, 0x24, // movsd (%rsp), %xmm1
|
||||||
|
0x48, 0x83, 0xc4, 0x10, // addq $16, %rsp
|
||||||
|
};
|
||||||
|
#define GLUE_SAVE_TO_SPILL_SIZE(x) (sizeof(save_validate))
|
||||||
|
#define GLUE_RESTORE_SPILL_TO_FPREG2_SIZE(x) (sizeof(restore_validate))
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#define GLUE_SAVE_TO_SPILL_SIZE(x) (4)
|
||||||
|
#define GLUE_RESTORE_SPILL_TO_FPREG2_SIZE(x) (4)
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static void GLUE_RESTORE_SPILL_TO_FPREG2(void *b, int ws)
|
||||||
|
{
|
||||||
|
#ifdef GLUE_VALIDATE_SPILLS
|
||||||
|
char *p = (char*) b;
|
||||||
|
memcpy(p,restore_validate,sizeof(restore_validate));
|
||||||
|
p[3] += ws;
|
||||||
|
#else
|
||||||
|
*(unsigned int *)b = 0xcc100ff2 + (ws<<24); // movsd xmm1, xmm4+ws
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
static void GLUE_SAVE_TO_SPILL(void *b, int ws)
|
||||||
|
{
|
||||||
|
#ifdef GLUE_VALIDATE_SPILLS
|
||||||
|
char *p = (char*) b;
|
||||||
|
memcpy(p,save_validate,sizeof(save_validate));
|
||||||
|
p[sizeof(save_validate)-1] += ws<<3;
|
||||||
|
#else
|
||||||
|
*(unsigned int *)b = 0xe0100ff2 + (ws<<27); // movsd xmm4+ws, xmm0
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define GLUE_MAX_FPSTACK_SIZE 0
|
||||||
|
#define GLUE_JMP_SET_OFFSET(endOfInstruction,offset) (((int *)(endOfInstruction))[-1] = (int) (offset))
|
||||||
|
|
||||||
|
static const unsigned char GLUE_JMP_NC[] = { 0xE9, 0,0,0,0, }; // jmp<offset>
|
||||||
|
static const unsigned char GLUE_JMP_IF_P1_Z[] = {0x85, 0xC0, 0x0F, 0x84, 0,0,0,0 }; // test eax, eax, jz
|
||||||
|
static const unsigned char GLUE_JMP_IF_P1_NZ[] = {0x85, 0xC0, 0x0F, 0x85, 0,0,0,0 }; // test eax, eax, jnz
|
||||||
|
|
||||||
|
|
||||||
|
#define GLUE_FUNC_ENTER_SIZE 0
|
||||||
|
#define GLUE_FUNC_LEAVE_SIZE 0
|
||||||
|
const static unsigned int GLUE_FUNC_ENTER[1];
|
||||||
|
const static unsigned int GLUE_FUNC_LEAVE[1];
|
||||||
|
|
||||||
|
// on x86-64:
|
||||||
|
// stack is always 16 byte aligned
|
||||||
|
// pushing values to the stack (for eel functions) has alignment pushed first, then value (value is at the lower address)
|
||||||
|
// pushing pointers to the stack has the pointer pushed first, then the alignment (pointer is at the higher address)
|
||||||
|
#define GLUE_MOV_PX_DIRECTVALUE_SIZE 10
|
||||||
|
#define GLUE_MOV_PX_DIRECTVALUE_TOSTACK_SIZE 14 // wr=-1, sets xmm0
|
||||||
|
#define GLUE_MOV_PX_DIRECTVALUE_TOFPREG2_SIZE 14 // wr=-2, sets xmm1
|
||||||
|
static void GLUE_MOV_PX_DIRECTVALUE_GEN(void *b, INT_PTR v, int wr) {
|
||||||
|
const static unsigned short tab[3] =
|
||||||
|
{
|
||||||
|
0xB848 /* mov rax, dv*/,
|
||||||
|
0xBF48 /* mov rdi, dv */ ,
|
||||||
|
0xB948 /* mov rcx, dv */
|
||||||
|
};
|
||||||
|
unsigned short *bb = (unsigned short *)b;
|
||||||
|
*bb++ = tab[wdl_max(wr,0)]; // mov rax, directvalue
|
||||||
|
*(INT_PTR *)bb = v;
|
||||||
|
if (wr == -2) *(unsigned int *)(bb + 4) = 0x08100ff2; // movsd (%rax), %xmm1
|
||||||
|
else if (wr == -1) *(unsigned int *)(bb + 4) = 0x00100ff2; // movsd (%rax), %xmm0
|
||||||
|
}
|
||||||
|
|
||||||
|
const static unsigned char GLUE_PUSH_P1[2]={ 0x50,0x50}; // push rax (pointer); push rax (alignment)
|
||||||
|
|
||||||
|
#define GLUE_STORE_P1_TO_STACK_AT_OFFS_SIZE(x) 8
|
||||||
|
static void GLUE_STORE_P1_TO_STACK_AT_OFFS(void *b, int offs)
|
||||||
|
{
|
||||||
|
((unsigned char *)b)[0] = 0x48; // mov [rsp+offs], rax
|
||||||
|
((unsigned char *)b)[1] = 0x89;
|
||||||
|
((unsigned char *)b)[2] = 0x84;
|
||||||
|
((unsigned char *)b)[3] = 0x24;
|
||||||
|
*(int *)((unsigned char *)b+4) = offs;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_MOVE_PX_STACKPTR_SIZE 3
|
||||||
|
static void GLUE_MOVE_PX_STACKPTR_GEN(void *b, int wv)
|
||||||
|
{
|
||||||
|
static const unsigned char tab[3][GLUE_MOVE_PX_STACKPTR_SIZE]=
|
||||||
|
{
|
||||||
|
{ 0x48, 0x89, 0xe0 }, // mov rax, rsp
|
||||||
|
{ 0x48, 0x89, 0xe7 }, // mov rdi, rsp
|
||||||
|
{ 0x48, 0x89, 0xe1 }, // mov rcx, rsp
|
||||||
|
};
|
||||||
|
memcpy(b,tab[wv],GLUE_MOVE_PX_STACKPTR_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_MOVE_STACK_SIZE 7
|
||||||
|
static void GLUE_MOVE_STACK(void *b, int amt)
|
||||||
|
{
|
||||||
|
((unsigned char *)b)[0] = 0x48;
|
||||||
|
((unsigned char *)b)[1] = 0x81;
|
||||||
|
if (amt < 0)
|
||||||
|
{
|
||||||
|
((unsigned char *)b)[2] = 0xEC;
|
||||||
|
*(int *)((char*)b+3) = -amt; // sub rsp, -amt32
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
((unsigned char *)b)[2] = 0xc4;
|
||||||
|
*(int *)((char*)b+3) = amt; // add rsp, amt32
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_POP_PX_SIZE 2
|
||||||
|
static void GLUE_POP_PX(void *b, int wv)
|
||||||
|
{
|
||||||
|
static const unsigned char tab[3][GLUE_POP_PX_SIZE]=
|
||||||
|
{
|
||||||
|
{0x58,/*pop rax*/ 0x58}, // pop alignment, then pop pointer
|
||||||
|
{0x5F,/*pop rdi*/ 0x5F},
|
||||||
|
{0x59,/*pop rcx*/ 0x59},
|
||||||
|
};
|
||||||
|
memcpy(b,tab[wv],GLUE_POP_PX_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
static const unsigned char GLUE_PUSH_P1PTR_AS_VALUE[] =
|
||||||
|
{
|
||||||
|
0x50, /*push rax - for alignment */
|
||||||
|
0xff, 0x30, /* push qword [rax] */
|
||||||
|
};
|
||||||
|
|
||||||
|
static int GLUE_POP_VALUE_TO_ADDR(unsigned char *buf, void *destptr) // trashes P2 (rdi) and P3 (rcx)
|
||||||
|
{
|
||||||
|
if (buf)
|
||||||
|
{
|
||||||
|
*buf++ = 0x48; *buf++ = 0xB9; *(void **) buf = destptr; buf+=8; // mov rcx, directvalue
|
||||||
|
*buf++ = 0x8f; *buf++ = 0x01; // pop qword [rcx]
|
||||||
|
*buf++ = 0x5F ; // pop rdi (alignment, safe to trash rdi though)
|
||||||
|
}
|
||||||
|
return 1+10+2;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int GLUE_COPY_VALUE_AT_P1_TO_PTR(unsigned char *buf, void *destptr) // trashes P2/P3
|
||||||
|
{
|
||||||
|
if (buf)
|
||||||
|
{
|
||||||
|
*buf++ = 0x48; *buf++ = 0xB9; *(void **) buf = destptr; buf+=8; // mov rcx, directvalue
|
||||||
|
*buf++ = 0x48; *buf++ = 0x8B; *buf++ = 0x38; // mov rdi, [rax]
|
||||||
|
*buf++ = 0x48; *buf++ = 0x89; *buf++ = 0x39; // mov [rcx], rdi
|
||||||
|
}
|
||||||
|
|
||||||
|
return 3 + 10 + 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int GLUE_POP_FPSTACK_TO_PTR(unsigned char *buf, void *destptr)
|
||||||
|
{
|
||||||
|
if (buf)
|
||||||
|
{
|
||||||
|
*buf++ = 0x48;
|
||||||
|
*buf++ = 0xB8;
|
||||||
|
*(void **) buf = destptr; buf+=8; // mov rax, directvalue
|
||||||
|
|
||||||
|
*buf++ = 0xf2; // movsd %xmm0, (%rax)
|
||||||
|
*buf++ = 0x0f;
|
||||||
|
*buf++ = 0x11;
|
||||||
|
*buf++ = 0x00;
|
||||||
|
}
|
||||||
|
return 2+8+4;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#define GLUE_SET_PX_FROM_P1_SIZE 3
|
||||||
|
static void GLUE_SET_PX_FROM_P1(void *b, int wv)
|
||||||
|
{
|
||||||
|
static const unsigned char tab[3][GLUE_SET_PX_FROM_P1_SIZE]={
|
||||||
|
{0x90,0x90,0x90}, // should never be used! (nopnop)
|
||||||
|
{0x48,0x89,0xC7}, // mov rdi, rax
|
||||||
|
{0x48,0x89,0xC1}, // mov rcx, rax
|
||||||
|
};
|
||||||
|
memcpy(b,tab[wv],GLUE_SET_PX_FROM_P1_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#define GLUE_POP_FPSTACK_SIZE 0
|
||||||
|
static const unsigned char GLUE_POP_FPSTACK[1] = { 0 };
|
||||||
|
|
||||||
|
static const unsigned char GLUE_POP_FPSTACK_TOSTACK[] = {
|
||||||
|
0x48, 0x81, 0xEC, 16, 0,0,0, // sub rsp, 16
|
||||||
|
0xf2, 0x0f, 0x11, 0x04, 0x24, // movsd xmm0, (%rsp)
|
||||||
|
};
|
||||||
|
|
||||||
|
static const unsigned char GLUE_POP_FPSTACK_TO_WTP[] = {
|
||||||
|
0xf2, 0x0f, 0x11, 0x06, // movsd xmm0, (%rsi)
|
||||||
|
0x48, 0x81, 0xC6, 8, 0,0,0,/* add rsi, 8 */
|
||||||
|
};
|
||||||
|
|
||||||
|
#define GLUE_SET_PX_FROM_WTP_SIZE 3
|
||||||
|
static void GLUE_SET_PX_FROM_WTP(void *b, int wv)
|
||||||
|
{
|
||||||
|
static const unsigned char tab[3][GLUE_SET_PX_FROM_WTP_SIZE]={
|
||||||
|
{0x48, 0x89,0xF0}, // mov rax, rsi
|
||||||
|
{0x48, 0x89,0xF7}, // mov rdi, rsi
|
||||||
|
{0x48, 0x89,0xF1}, // mov rcx, rsi
|
||||||
|
};
|
||||||
|
memcpy(b,tab[wv],GLUE_SET_PX_FROM_WTP_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_PUSH_VAL_AT_PX_TO_FPSTACK_SIZE 4
|
||||||
|
static void GLUE_PUSH_VAL_AT_PX_TO_FPSTACK(void *b, int wv)
|
||||||
|
{
|
||||||
|
static const unsigned char tab[3][GLUE_PUSH_VAL_AT_PX_TO_FPSTACK_SIZE]={
|
||||||
|
{0xf2, 0x0f, 0x10, 0x00}, // movsd (%rax), %xmm0
|
||||||
|
{0xf2, 0x0f, 0x10, 0x07}, // movsd (%rdi), %xmm0
|
||||||
|
{0xf2, 0x0f, 0x10, 0x01}, // movsd (%rcx), %xmm0
|
||||||
|
};
|
||||||
|
memcpy(b,tab[wv],GLUE_PUSH_VAL_AT_PX_TO_FPSTACK_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_POP_FPSTACK_TO_WTP_TO_PX_SIZE (GLUE_SET_PX_FROM_WTP_SIZE + sizeof(GLUE_POP_FPSTACK_TO_WTP))
|
||||||
|
static void GLUE_POP_FPSTACK_TO_WTP_TO_PX(unsigned char *buf, int wv)
|
||||||
|
{
|
||||||
|
GLUE_SET_PX_FROM_WTP(buf,wv);
|
||||||
|
memcpy(buf + GLUE_SET_PX_FROM_WTP_SIZE,GLUE_POP_FPSTACK_TO_WTP,sizeof(GLUE_POP_FPSTACK_TO_WTP));
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
const static unsigned char GLUE_RET=0xC3;
|
||||||
|
|
||||||
|
static int GLUE_RESET_WTP(unsigned char *out, void *ptr)
|
||||||
|
{
|
||||||
|
if (out)
|
||||||
|
{
|
||||||
|
*out++ = 0x48;
|
||||||
|
*out++ = 0xBE; // mov rsi, constant64
|
||||||
|
*(void **)out = ptr;
|
||||||
|
out+=sizeof(void *);
|
||||||
|
}
|
||||||
|
return 2+sizeof(void *);
|
||||||
|
}
|
||||||
|
|
||||||
|
extern void eel_callcode64(INT_PTR code, INT_PTR ram_tab);
|
||||||
|
extern void eel_callcode64_fast(INT_PTR code, INT_PTR ram_tab);
|
||||||
|
#define GLUE_CALL_CODE(bp, cp, rt) do { \
|
||||||
|
if (h->compile_flags&NSEEL_CODE_COMPILE_FLAG_NOFPSTATE) eel_callcode64_fast(cp, rt); \
|
||||||
|
else eel_callcode64(cp, rt);\
|
||||||
|
} while(0)
|
||||||
|
#define GLUE_TABPTR_IGNORED
|
||||||
|
|
||||||
|
static unsigned char *EEL_GLUE_set_immediate(void *_p, INT_PTR newv)
|
||||||
|
{
|
||||||
|
char *p=(char*)_p;
|
||||||
|
INT_PTR scan = 0xFEFEFEFEFEFEFEFE;
|
||||||
|
while (*(INT_PTR *)p != scan) p++;
|
||||||
|
*(INT_PTR *)p = newv;
|
||||||
|
return (unsigned char *) (((INT_PTR*)p)+1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define INT_TO_LECHARS(x) ((x)&0xff),(((x)>>8)&0xff), (((x)>>16)&0xff), (((x)>>24)&0xff)
|
||||||
|
|
||||||
|
#define GLUE_INLINE_LOOPS
|
||||||
|
|
||||||
|
static const unsigned char GLUE_LOOP_LOADCNT[]={
|
||||||
|
0xf2, 0x48, 0x0f, 0x2c, 0xc8, // cvttsd2si %xmm0, %rcx
|
||||||
|
0x48, 0x81, 0xf9, 1,0,0,0, // cmp rcx, 1
|
||||||
|
0x0F, 0x8C, 0,0,0,0, // JL <skipptr>
|
||||||
|
};
|
||||||
|
|
||||||
|
#if NSEEL_LOOPFUNC_SUPPORT_MAXLEN > 0
|
||||||
|
#define GLUE_LOOP_CLAMPCNT_SIZE sizeof(GLUE_LOOP_CLAMPCNT)
|
||||||
|
static const unsigned char GLUE_LOOP_CLAMPCNT[]={
|
||||||
|
0x48, 0x81, 0xf9, INT_TO_LECHARS(NSEEL_LOOPFUNC_SUPPORT_MAXLEN), // cmp rcx, NSEEL_LOOPFUNC_SUPPORT_MAXLEN
|
||||||
|
0x0F, 0x8C, 10,0,0,0, // JL over-the-mov
|
||||||
|
0x48, 0xB9, INT_TO_LECHARS(NSEEL_LOOPFUNC_SUPPORT_MAXLEN), 0,0,0,0, // mov rcx, NSEEL_LOOPFUNC_SUPPORT_MAXLEN
|
||||||
|
};
|
||||||
|
#else
|
||||||
|
#define GLUE_LOOP_CLAMPCNT_SIZE 0
|
||||||
|
#define GLUE_LOOP_CLAMPCNT ""
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define GLUE_LOOP_BEGIN_SIZE sizeof(GLUE_LOOP_BEGIN)
|
||||||
|
static const unsigned char GLUE_LOOP_BEGIN[]={
|
||||||
|
0x56, //push rsi
|
||||||
|
0x51, // push rcx
|
||||||
|
};
|
||||||
|
static const unsigned char GLUE_LOOP_END[]={
|
||||||
|
0x59, //pop rcx
|
||||||
|
0x5E, // pop rsi
|
||||||
|
0xff, 0xc9, // dec rcx
|
||||||
|
0x0f, 0x85, 0,0,0,0, // jnz ...
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#if NSEEL_LOOPFUNC_SUPPORT_MAXLEN > 0
|
||||||
|
static const unsigned char GLUE_WHILE_SETUP[]={
|
||||||
|
0x48, 0xB9, INT_TO_LECHARS(NSEEL_LOOPFUNC_SUPPORT_MAXLEN), 0,0,0,0, // mov rcx, NSEEL_LOOPFUNC_SUPPORT_MAXLEN
|
||||||
|
};
|
||||||
|
#define GLUE_WHILE_SETUP_SIZE sizeof(GLUE_WHILE_SETUP)
|
||||||
|
|
||||||
|
static const unsigned char GLUE_WHILE_BEGIN[]={
|
||||||
|
0x56, //push rsi
|
||||||
|
0x51, // push rcx
|
||||||
|
};
|
||||||
|
static const unsigned char GLUE_WHILE_END[]={
|
||||||
|
0x59, //pop rcx
|
||||||
|
0x5E, // pop rsi
|
||||||
|
|
||||||
|
0xff, 0xc9, // dec rcx
|
||||||
|
0x0f, 0x84, 0,0,0,0, // jz endpt
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#else
|
||||||
|
#define GLUE_WHILE_SETUP ""
|
||||||
|
#define GLUE_WHILE_SETUP_SIZE 0
|
||||||
|
#define GLUE_WHILE_END_NOJUMP
|
||||||
|
|
||||||
|
static const unsigned char GLUE_WHILE_BEGIN[]={
|
||||||
|
0x56, //push rsi
|
||||||
|
0x51, // push rcx
|
||||||
|
};
|
||||||
|
static const unsigned char GLUE_WHILE_END[]={
|
||||||
|
0x59, //pop rcx
|
||||||
|
0x5E, // pop rsi
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
static const unsigned char GLUE_WHILE_CHECK_RV[] = {
|
||||||
|
0x85, 0xC0, // test eax, eax
|
||||||
|
0x0F, 0x85, 0,0,0,0 // jnz looppt
|
||||||
|
};
|
||||||
|
|
||||||
|
static const unsigned char GLUE_SET_P1_Z[] = { 0x48, 0x29, 0xC0 }; // sub rax, rax
|
||||||
|
static const unsigned char GLUE_SET_P1_NZ[] = { 0xb0, 0x01 }; // mov al, 1
|
||||||
|
|
||||||
|
|
||||||
|
#define GLUE_HAS_FLDZ
|
||||||
|
static const unsigned char GLUE_FLDZ[] = {
|
||||||
|
0x0f, 0x57, 0xc0 //xorps %xmm0, %xmm0
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static EEL_F negativezeropointfive=-0.5f;
|
||||||
|
static EEL_F onepointfive=1.5f;
|
||||||
|
#define GLUE_INVSQRT_NEEDREPL &negativezeropointfive, &onepointfive,
|
||||||
|
|
||||||
|
|
||||||
|
static void *GLUE_realAddress(void *fn, int *size)
|
||||||
|
{
|
||||||
|
static const unsigned char new_sig[8] = { 0x89, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x00 };
|
||||||
|
int sz = 0;
|
||||||
|
while (memcmp((char*)fn + sz,new_sig,sizeof(new_sig))) sz++;
|
||||||
|
*size = sz;
|
||||||
|
return fn;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GLUE_HAS_FUSE 1
|
||||||
|
static int GLUE_FUSE(compileContext *ctx, unsigned char *code, int left_size, int right_size, int fuse_flags, int spill_reg)
|
||||||
|
{
|
||||||
|
const UINT_PTR base = (UINT_PTR) ctx->ram_state->blocks;
|
||||||
|
const int is_sse_op = right_size == 4 && // add/mul/sub/min/max
|
||||||
|
code[0] == 0xf2 &&
|
||||||
|
code[1] == 0x0f &&
|
||||||
|
code[3] == 0xc1 && // low nibble is xmm1
|
||||||
|
(code[2] == 0x58 || code[2] == 0x59 || code[2] == 0x5c || code[2]==0x5d || code[2] == 0x5f);
|
||||||
|
|
||||||
|
if (spill_reg >= 0)
|
||||||
|
{
|
||||||
|
#ifndef GLUE_VALIDATE_SPILLS
|
||||||
|
if (is_sse_op)
|
||||||
|
{
|
||||||
|
char tmp[32];
|
||||||
|
const int sz = GLUE_RESTORE_SPILL_TO_FPREG2_SIZE(spill_reg);
|
||||||
|
GLUE_RESTORE_SPILL_TO_FPREG2(tmp,spill_reg);
|
||||||
|
if (left_size>=sz && !memcmp(code-sz,tmp,sz))
|
||||||
|
{
|
||||||
|
code[-2] = code[2]; // modify the movsd into an addsd
|
||||||
|
code[-1] -= 8; // movsd uses 0xc8+(xmmX&7), addsd etc use 0xc0
|
||||||
|
return -4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (left_size==28)
|
||||||
|
{
|
||||||
|
// if const64_1 is within a 32-bit offset of ctx->ram_blocks->blocks, we can use [r12+offs]
|
||||||
|
if (code[-28] == 0x48 && code[-27] == 0xb8 && // mov rax, const64_1
|
||||||
|
*(int *)(code - 18) == 0x08100ff2 && // movsd xmm1, [rax]
|
||||||
|
code[-14] == 0x48 && code[-13] == 0xb8 && // mov rax, const64_2
|
||||||
|
*(int *)(code - 4) == 0x00100ff2 // movsd xmm0, [rax]
|
||||||
|
)
|
||||||
|
{
|
||||||
|
UINT_PTR c1, c2;
|
||||||
|
INT_PTR c2offs,c1offs;
|
||||||
|
unsigned char opc[3];
|
||||||
|
int wrpos = -28;
|
||||||
|
if (is_sse_op) memcpy(opc,code,3);
|
||||||
|
memcpy(&c1,code-26,8);
|
||||||
|
memcpy(&c2,code-12,8);
|
||||||
|
|
||||||
|
#define PTR_32_OK(x) ((x) == (INT_PTR)(int)(x))
|
||||||
|
c2offs = c2-base;
|
||||||
|
if (!PTR_32_OK(c2offs))
|
||||||
|
{
|
||||||
|
code[wrpos++] = 0x48;
|
||||||
|
code[wrpos++] = 0xb8;
|
||||||
|
memcpy(code+wrpos,&c2,8); // mov rax, const64_2
|
||||||
|
wrpos += 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
c1offs = c1-base;
|
||||||
|
if (!PTR_32_OK(c1offs))
|
||||||
|
{
|
||||||
|
code[wrpos++] = 0x48;
|
||||||
|
code[wrpos++] = 0xbf;
|
||||||
|
memcpy(code+wrpos,&c1,8); // mov rdi, const64_1
|
||||||
|
wrpos += 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!PTR_32_OK(c2offs))
|
||||||
|
{
|
||||||
|
*(int *)(code+wrpos) = 0x00100ff2; // movsd xmm0, [rax]
|
||||||
|
wrpos += 4;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// movsd xmm0, [r12+offs]
|
||||||
|
code[wrpos++] = 0xf2;
|
||||||
|
code[wrpos++] = 0x41;
|
||||||
|
code[wrpos++] = 0x0f;
|
||||||
|
code[wrpos++] = 0x10;
|
||||||
|
code[wrpos++] = 0x84;
|
||||||
|
code[wrpos++] = 0x24;
|
||||||
|
*(int *)(code+wrpos) = (int)c2offs;
|
||||||
|
wrpos += 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_sse_op)
|
||||||
|
{
|
||||||
|
// load xmm1 from rdi/c1offs
|
||||||
|
if (!PTR_32_OK(c1offs))
|
||||||
|
{
|
||||||
|
*(int *)(code+wrpos) = 0x0f100ff2; // movsd xmm1, [rdi]
|
||||||
|
wrpos += 4;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// movsd xmm1, [r12+offs]
|
||||||
|
code[wrpos++] = 0xf2;
|
||||||
|
code[wrpos++] = 0x41;
|
||||||
|
code[wrpos++] = 0x0f;
|
||||||
|
code[wrpos++] = 0x10;
|
||||||
|
code[wrpos++] = 0x8c;
|
||||||
|
code[wrpos++] = 0x24;
|
||||||
|
*(int *)(code+wrpos) = (int)c1offs;
|
||||||
|
wrpos += 4;
|
||||||
|
}
|
||||||
|
if (wrpos<0) memmove(code+wrpos,code,right_size);
|
||||||
|
return wrpos;
|
||||||
|
}
|
||||||
|
|
||||||
|
// fuse to sse op
|
||||||
|
if (!PTR_32_OK(c1offs))
|
||||||
|
{
|
||||||
|
memcpy(code+wrpos,opc,3);
|
||||||
|
code[wrpos+3] = 0x07; // [rdi]
|
||||||
|
wrpos += 4;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// mul/add/sub/min/max/sd xmm0, [r12+offs]
|
||||||
|
code[wrpos++] = opc[0]; // 0xf2
|
||||||
|
code[wrpos++] = 0x41;
|
||||||
|
code[wrpos++] = opc[1]; // 0x0f
|
||||||
|
code[wrpos++] = opc[2]; // 0x58 etc
|
||||||
|
code[wrpos++] = 0x84;
|
||||||
|
code[wrpos++] = 0x24;
|
||||||
|
*(int *)(code+wrpos) = (int)c1offs;
|
||||||
|
wrpos += 4;
|
||||||
|
}
|
||||||
|
return wrpos - right_size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ((fuse_flags&1) && left_size >= 14)
|
||||||
|
{
|
||||||
|
if (code[-14] == 0x48 && code[-13] == 0xb8 && // mov rax, const64_2
|
||||||
|
*(int *)(code - 4) == 0x00100ff2) // movsd xmm0, [rax]
|
||||||
|
{
|
||||||
|
INT_PTR c1;
|
||||||
|
memcpy(&c1,code-12,8);
|
||||||
|
c1 -= base;
|
||||||
|
if (PTR_32_OK(c1))
|
||||||
|
{
|
||||||
|
// movsd xmm0, [r12+offs]
|
||||||
|
int wrpos = -14;
|
||||||
|
code[wrpos++] = 0xf2;
|
||||||
|
code[wrpos++] = 0x41;
|
||||||
|
code[wrpos++] = 0x0f;
|
||||||
|
code[wrpos++] = 0x10;
|
||||||
|
code[wrpos++] = 0x84;
|
||||||
|
code[wrpos++] = 0x24;
|
||||||
|
*(int *)(code+wrpos) = (int)c1;
|
||||||
|
wrpos += 4;
|
||||||
|
if (wrpos<0) memmove(code+wrpos,code,right_size);
|
||||||
|
return wrpos;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (left_size == 20 && right_size == 9 &&
|
||||||
|
code[-20]==0x48 && code[-19] == 0xbf && // mov rdi, const64_1
|
||||||
|
code[-10]==0x48 && code[-9] == 0xb8 // mov rax, const64_2
|
||||||
|
)
|
||||||
|
{
|
||||||
|
static unsigned char assign_copy[9] = { 0x48, 0x8b, 0x10, // mov rdx, [rax]
|
||||||
|
0x48, 0x89, 0x17, // mov [rdi], rdx
|
||||||
|
0x48, 0x89, 0xf8, // mov rax, rdi
|
||||||
|
};
|
||||||
|
if (!memcmp(code,assign_copy,9))
|
||||||
|
{
|
||||||
|
int wrpos = -20;
|
||||||
|
INT_PTR c1,c2; // c1 is dest, c2 is src
|
||||||
|
memcpy(&c1,code-18,8);
|
||||||
|
memcpy(&c2,code-8,8);
|
||||||
|
|
||||||
|
if (!PTR_32_OK(c2-base))
|
||||||
|
{
|
||||||
|
code[wrpos++] = 0x48; // mov rdi, src
|
||||||
|
code[wrpos++] = 0xbf;
|
||||||
|
memcpy(code+wrpos,&c2,8);
|
||||||
|
wrpos +=8;
|
||||||
|
}
|
||||||
|
|
||||||
|
code[wrpos++] = 0x48; // mov rax, dest
|
||||||
|
code[wrpos++] = 0xb8;
|
||||||
|
memcpy(code+wrpos,&c1,8);
|
||||||
|
wrpos +=8;
|
||||||
|
|
||||||
|
if (PTR_32_OK(c2-base))
|
||||||
|
{
|
||||||
|
// mov rdx, [r12+offs]
|
||||||
|
code[wrpos++] = 0x49;
|
||||||
|
code[wrpos++] = 0x8b;
|
||||||
|
code[wrpos++] = 0x94;
|
||||||
|
code[wrpos++] = 0x24;
|
||||||
|
*(int *)(code+wrpos) = (int)(c2-base);
|
||||||
|
wrpos += 4;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
code[wrpos++] = 0x48; // mov rdx, [rdi]
|
||||||
|
code[wrpos++] = 0x8b;
|
||||||
|
code[wrpos++] = 0x17;
|
||||||
|
}
|
||||||
|
|
||||||
|
code[wrpos++] = 0x48; // mov [rax], rdx
|
||||||
|
code[wrpos++] = 0x89;
|
||||||
|
code[wrpos++] = 0x10;
|
||||||
|
|
||||||
|
return wrpos - right_size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
165
oversampling/WDL/eel2/loose_eel.cpp
Normal file
165
oversampling/WDL/eel2/loose_eel.cpp
Normal file
@@ -0,0 +1,165 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include "../mutex.h"
|
||||||
|
|
||||||
|
int g_verbose, g_interactive;
|
||||||
|
|
||||||
|
static void writeToStandardError(const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list arglist;
|
||||||
|
va_start(arglist, fmt);
|
||||||
|
vfprintf(stderr,fmt,arglist);
|
||||||
|
fprintf(stderr,"\n");
|
||||||
|
fflush(stderr);
|
||||||
|
va_end(arglist);
|
||||||
|
}
|
||||||
|
#define EEL_STRING_DEBUGOUT writeToStandardError // no parameters, since it takes varargs
|
||||||
|
|
||||||
|
#ifndef EEL_LICE_WANT_STANDALONE
|
||||||
|
#define EELSCRIPT_NO_LICE
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "eelscript.h"
|
||||||
|
|
||||||
|
|
||||||
|
void NSEEL_HOSTSTUB_EnterMutex() { }
|
||||||
|
void NSEEL_HOSTSTUB_LeaveMutex() { }
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
bool want_args = true;
|
||||||
|
int argpos = 1;
|
||||||
|
const char *scriptfn = argv[0];
|
||||||
|
while (argpos < argc && argv[argpos][0] == '-' && argv[argpos][1])
|
||||||
|
{
|
||||||
|
if (!strcmp(argv[argpos],"-v")) g_verbose++;
|
||||||
|
else if (!strcmp(argv[argpos],"-i")) g_interactive++;
|
||||||
|
else if (!strcmp(argv[argpos],"--no-args")) want_args=false;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fprintf(stderr,"Usage: %s [-v] [--no-args] [-i | scriptfile | -]\n",argv[0]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
argpos++;
|
||||||
|
}
|
||||||
|
if (argpos < argc && !g_interactive)
|
||||||
|
{
|
||||||
|
scriptfn = argv[argpos++];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
#ifndef _WIN32
|
||||||
|
if (!g_interactive && isatty(0))
|
||||||
|
#else
|
||||||
|
if (1)
|
||||||
|
#endif
|
||||||
|
g_interactive=1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (eelScriptInst::init())
|
||||||
|
{
|
||||||
|
fprintf(stderr,"NSEEL_init(): error initializing\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef EELSCRIPT_NO_LICE
|
||||||
|
#ifdef __APPLE__
|
||||||
|
SWELL_InitAutoRelease();
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
WDL_FastString code,t;
|
||||||
|
|
||||||
|
eelScriptInst inst;
|
||||||
|
if (want_args)
|
||||||
|
{
|
||||||
|
#ifndef EELSCRIPT_DO_DISASSEMBLE
|
||||||
|
const int argv_offs = 1<<22;
|
||||||
|
code.SetFormatted(64,"argc=0; argv=%d;\n",argv_offs);
|
||||||
|
int x;
|
||||||
|
for (x=argpos-1;x<argc;x++)
|
||||||
|
{
|
||||||
|
code.AppendFormatted(64,"argv[argc]=%d; argc+=1;\n",
|
||||||
|
inst.m_string_context->AddString(new WDL_FastString(x<argpos ? scriptfn : argv[x])));
|
||||||
|
}
|
||||||
|
inst.runcode(code.Get(),2,"__cmdline__",true,true,true);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
if (g_interactive)
|
||||||
|
{
|
||||||
|
#ifndef EELSCRIPT_NO_LICE
|
||||||
|
if (inst.m_gfx_state && inst.m_gfx_state->m_gfx_clear) inst.m_gfx_state->m_gfx_clear[0] = -1;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
printf("EEL interactive mode, type quit to quit, abort to abort multiline entry\n");
|
||||||
|
EEL_F *resultVar = NSEEL_VM_regvar(inst.m_vm,"__result");
|
||||||
|
code.Set("");
|
||||||
|
char line[4096];
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
#ifndef EELSCRIPT_NO_LICE
|
||||||
|
_gfx_update(&inst,NULL);
|
||||||
|
#endif
|
||||||
|
if (!code.Get()[0]) printf("EEL> ");
|
||||||
|
else printf("> ");
|
||||||
|
fflush(stdout);
|
||||||
|
line[0]=0;
|
||||||
|
fgets(line,sizeof(line),stdin);
|
||||||
|
if (!line[0]) break;
|
||||||
|
code.Append(line);
|
||||||
|
while (line[0] && (
|
||||||
|
line[strlen(line)-1] == '\r' ||
|
||||||
|
line[strlen(line)-1] == '\n' ||
|
||||||
|
line[strlen(line)-1] == '\t' ||
|
||||||
|
line[strlen(line)-1] == ' '
|
||||||
|
)) line[strlen(line)-1]=0;
|
||||||
|
|
||||||
|
if (!strcmp(line,"quit")) break;
|
||||||
|
if (!strcmp(line,"abort")) code.Set("");
|
||||||
|
|
||||||
|
#ifndef EELSCRIPT_DO_DISASSEMBLE
|
||||||
|
t.Set("__result = (");
|
||||||
|
#else
|
||||||
|
t.Set("");
|
||||||
|
#endif
|
||||||
|
t.Append(code.Get());
|
||||||
|
#ifndef EELSCRIPT_DO_DISASSEMBLE
|
||||||
|
t.Append(");");
|
||||||
|
#endif
|
||||||
|
int res=inst.runcode(t.Get(),false,"",true,true,true); // allow free, since functions can't be defined locally
|
||||||
|
if (!res)
|
||||||
|
{
|
||||||
|
if (resultVar) printf("=%g ",*resultVar);
|
||||||
|
code.Set("");
|
||||||
|
}
|
||||||
|
else // try compiling again allowing function definitions (and not allowing free)
|
||||||
|
// but show errors if not continuation
|
||||||
|
{
|
||||||
|
res=inst.runcode(code.Get(),true,"(stdin)", false,false,true);
|
||||||
|
if (res<=0) code.Set("");
|
||||||
|
// res>0 means need more lines
|
||||||
|
}
|
||||||
|
while (inst.run_deferred());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
inst.loadfile(scriptfn,NULL,true);
|
||||||
|
while (inst.run_deferred());
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
INT_PTR SWELLAppMain(int msg, INT_PTR parm1, INT_PTR parm2)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
194
oversampling/WDL/eel2/loose_eel.dsp
Normal file
194
oversampling/WDL/eel2/loose_eel.dsp
Normal file
@@ -0,0 +1,194 @@
|
|||||||
|
# Microsoft Developer Studio Project File - Name="loose_eel" - Package Owner=<4>
|
||||||
|
# Microsoft Developer Studio Generated Build File, Format Version 6.00
|
||||||
|
# ** DO NOT EDIT **
|
||||||
|
|
||||||
|
# TARGTYPE "Win32 (x86) Console Application" 0x0103
|
||||||
|
|
||||||
|
CFG=loose_eel - Win32 Debug
|
||||||
|
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
|
||||||
|
!MESSAGE use the Export Makefile command and run
|
||||||
|
!MESSAGE
|
||||||
|
!MESSAGE NMAKE /f "loose_eel.mak".
|
||||||
|
!MESSAGE
|
||||||
|
!MESSAGE You can specify a configuration when running NMAKE
|
||||||
|
!MESSAGE by defining the macro CFG on the command line. For example:
|
||||||
|
!MESSAGE
|
||||||
|
!MESSAGE NMAKE /f "loose_eel.mak" CFG="loose_eel - Win32 Debug"
|
||||||
|
!MESSAGE
|
||||||
|
!MESSAGE Possible choices for configuration are:
|
||||||
|
!MESSAGE
|
||||||
|
!MESSAGE "loose_eel - Win32 Release" (based on "Win32 (x86) Console Application")
|
||||||
|
!MESSAGE "loose_eel - Win32 Debug" (based on "Win32 (x86) Console Application")
|
||||||
|
!MESSAGE
|
||||||
|
|
||||||
|
# Begin Project
|
||||||
|
# PROP AllowPerConfigDependencies 0
|
||||||
|
# PROP Scc_ProjName ""
|
||||||
|
# PROP Scc_LocalPath ""
|
||||||
|
CPP=cl.exe
|
||||||
|
RSC=rc.exe
|
||||||
|
|
||||||
|
!IF "$(CFG)" == "loose_eel - Win32 Release"
|
||||||
|
|
||||||
|
# PROP BASE Use_MFC 0
|
||||||
|
# PROP BASE Use_Debug_Libraries 0
|
||||||
|
# PROP BASE Output_Dir "Release"
|
||||||
|
# PROP BASE Intermediate_Dir "Release"
|
||||||
|
# PROP BASE Target_Dir ""
|
||||||
|
# PROP Use_MFC 0
|
||||||
|
# PROP Use_Debug_Libraries 0
|
||||||
|
# PROP Output_Dir "Release"
|
||||||
|
# PROP Intermediate_Dir "Release"
|
||||||
|
# PROP Ignore_Export_Lib 0
|
||||||
|
# PROP Target_Dir ""
|
||||||
|
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
|
||||||
|
# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D WDL_FFT_REALSIZE=8 /D NSEEL_LOOPFUNC_SUPPORT_MAXLEN=0 /D "EEL_LICE_WANT_STANDALONE" /YX /FD /c
|
||||||
|
# ADD BASE RSC /l 0x409 /d "NDEBUG"
|
||||||
|
# ADD RSC /l 0x409 /d "NDEBUG"
|
||||||
|
BSC32=bscmake.exe
|
||||||
|
# ADD BASE BSC32 /nologo
|
||||||
|
# ADD BSC32 /nologo
|
||||||
|
LINK32=link.exe
|
||||||
|
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
|
||||||
|
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib wsock32.lib /nologo /subsystem:console /machine:I386
|
||||||
|
|
||||||
|
!ELSEIF "$(CFG)" == "loose_eel - Win32 Debug"
|
||||||
|
|
||||||
|
# PROP BASE Use_MFC 0
|
||||||
|
# PROP BASE Use_Debug_Libraries 1
|
||||||
|
# PROP BASE Output_Dir "Debug"
|
||||||
|
# PROP BASE Intermediate_Dir "Debug"
|
||||||
|
# PROP BASE Target_Dir ""
|
||||||
|
# PROP Use_MFC 0
|
||||||
|
# PROP Use_Debug_Libraries 1
|
||||||
|
# PROP Output_Dir "Debug"
|
||||||
|
# PROP Intermediate_Dir "Debug"
|
||||||
|
# PROP Ignore_Export_Lib 0
|
||||||
|
# PROP Target_Dir ""
|
||||||
|
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
|
||||||
|
# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D WDL_FFT_REALSIZE=8 /D NSEEL_LOOPFUNC_SUPPORT_MAXLEN=0 /D "EEL_LICE_WANT_STANDALONE" /YX /FD /GZ /c
|
||||||
|
# ADD BASE RSC /l 0x409 /d "_DEBUG"
|
||||||
|
# ADD RSC /l 0x409 /d "_DEBUG"
|
||||||
|
BSC32=bscmake.exe
|
||||||
|
# ADD BASE BSC32 /nologo
|
||||||
|
# ADD BSC32 /nologo
|
||||||
|
LINK32=link.exe
|
||||||
|
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
|
||||||
|
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib wsock32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
|
||||||
|
|
||||||
|
!ENDIF
|
||||||
|
|
||||||
|
# Begin Target
|
||||||
|
|
||||||
|
# Name "loose_eel - Win32 Release"
|
||||||
|
# Name "loose_eel - Win32 Debug"
|
||||||
|
# Begin Group "Source Files"
|
||||||
|
|
||||||
|
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
|
||||||
|
# Begin Group "eel2"
|
||||||
|
|
||||||
|
# PROP Default_Filter ""
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=".\ns-eel-addfuncs.h"
|
||||||
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=".\ns-eel-int.h"
|
||||||
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=".\ns-eel.h"
|
||||||
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=".\nseel-caltab.c"
|
||||||
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=".\nseel-cfunc.c"
|
||||||
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=".\nseel-compiler.c"
|
||||||
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=".\nseel-eval.c"
|
||||||
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=".\nseel-lextab.c"
|
||||||
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=".\nseel-ram.c"
|
||||||
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=".\nseel-yylex.c"
|
||||||
|
# End Source File
|
||||||
|
# End Group
|
||||||
|
# Begin Group "lice"
|
||||||
|
|
||||||
|
# PROP Default_Filter ""
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=..\lice\lice.cpp
|
||||||
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=..\lice\lice_arc.cpp
|
||||||
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=..\lice\lice_bmp.cpp
|
||||||
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=..\lice\lice_ico.cpp
|
||||||
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=..\lice\lice_image.cpp
|
||||||
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=..\lice\lice_line.cpp
|
||||||
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=..\lice\lice_lvg.cpp
|
||||||
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=..\lice\lice_pcx.cpp
|
||||||
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=..\lice\lice_text.cpp
|
||||||
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=..\lice\lice_textnew.cpp
|
||||||
|
# End Source File
|
||||||
|
# End Group
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=..\fft.c
|
||||||
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=.\loose_eel.cpp
|
||||||
|
# End Source File
|
||||||
|
# End Group
|
||||||
|
# Begin Group "Header Files"
|
||||||
|
|
||||||
|
# PROP Default_Filter "h;hpp;hxx;hm;inl"
|
||||||
|
# End Group
|
||||||
|
# Begin Group "Resource Files"
|
||||||
|
|
||||||
|
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
|
||||||
|
# End Group
|
||||||
|
# End Target
|
||||||
|
# End Project
|
||||||
29
oversampling/WDL/eel2/loose_eel.dsw
Normal file
29
oversampling/WDL/eel2/loose_eel.dsw
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
Microsoft Developer Studio Workspace File, Format Version 6.00
|
||||||
|
# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
|
Project: "loose_eel"=.\loose_eel.dsp - Package Owner=<4>
|
||||||
|
|
||||||
|
Package=<5>
|
||||||
|
{{{
|
||||||
|
}}}
|
||||||
|
|
||||||
|
Package=<4>
|
||||||
|
{{{
|
||||||
|
}}}
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
|
Global:
|
||||||
|
|
||||||
|
Package=<5>
|
||||||
|
{{{
|
||||||
|
}}}
|
||||||
|
|
||||||
|
Package=<3>
|
||||||
|
{{{
|
||||||
|
}}}
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
28
oversampling/WDL/eel2/loose_eel.sln
Normal file
28
oversampling/WDL/eel2/loose_eel.sln
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Express 2013 for Windows Desktop
|
||||||
|
VisualStudioVersion = 12.0.30110.0
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "loose_eel", "loose_eel.vcxproj", "{893B9C0E-73CA-4843-A3BC-8A3FF9055FA8}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Win32 = Debug|Win32
|
||||||
|
Debug|x64 = Debug|x64
|
||||||
|
Release|Win32 = Release|Win32
|
||||||
|
Release|x64 = Release|x64
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{893B9C0E-73CA-4843-A3BC-8A3FF9055FA8}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||||
|
{893B9C0E-73CA-4843-A3BC-8A3FF9055FA8}.Debug|Win32.Build.0 = Debug|Win32
|
||||||
|
{893B9C0E-73CA-4843-A3BC-8A3FF9055FA8}.Debug|x64.ActiveCfg = Debug|x64
|
||||||
|
{893B9C0E-73CA-4843-A3BC-8A3FF9055FA8}.Debug|x64.Build.0 = Debug|x64
|
||||||
|
{893B9C0E-73CA-4843-A3BC-8A3FF9055FA8}.Release|Win32.ActiveCfg = Release|Win32
|
||||||
|
{893B9C0E-73CA-4843-A3BC-8A3FF9055FA8}.Release|Win32.Build.0 = Release|Win32
|
||||||
|
{893B9C0E-73CA-4843-A3BC-8A3FF9055FA8}.Release|x64.ActiveCfg = Release|x64
|
||||||
|
{893B9C0E-73CA-4843-A3BC-8A3FF9055FA8}.Release|x64.Build.0 = Release|x64
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
190
oversampling/WDL/eel2/loose_eel.vcxproj
Normal file
190
oversampling/WDL/eel2/loose_eel.vcxproj
Normal file
@@ -0,0 +1,190 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<ItemGroup Label="ProjectConfigurations">
|
||||||
|
<ProjectConfiguration Include="Debug|Win32">
|
||||||
|
<Configuration>Debug</Configuration>
|
||||||
|
<Platform>Win32</Platform>
|
||||||
|
</ProjectConfiguration>
|
||||||
|
<ProjectConfiguration Include="Debug|x64">
|
||||||
|
<Configuration>Debug</Configuration>
|
||||||
|
<Platform>x64</Platform>
|
||||||
|
</ProjectConfiguration>
|
||||||
|
<ProjectConfiguration Include="Release|Win32">
|
||||||
|
<Configuration>Release</Configuration>
|
||||||
|
<Platform>Win32</Platform>
|
||||||
|
</ProjectConfiguration>
|
||||||
|
<ProjectConfiguration Include="Release|x64">
|
||||||
|
<Configuration>Release</Configuration>
|
||||||
|
<Platform>x64</Platform>
|
||||||
|
</ProjectConfiguration>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Object Include="asm-nseel-x64.obj">
|
||||||
|
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
|
||||||
|
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
|
||||||
|
</Object>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include=".\nseel-caltab.c" />
|
||||||
|
<ClCompile Include=".\nseel-cfunc.c" />
|
||||||
|
<ClCompile Include=".\nseel-compiler.c" />
|
||||||
|
<ClCompile Include=".\nseel-eval.c" />
|
||||||
|
<ClCompile Include=".\nseel-lextab.c" />
|
||||||
|
<ClCompile Include=".\nseel-ram.c" />
|
||||||
|
<ClCompile Include=".\nseel-yylex.c" />
|
||||||
|
<ClCompile Include="..\lice\lice.cpp" />
|
||||||
|
<ClCompile Include="..\lice\lice_arc.cpp" />
|
||||||
|
<ClCompile Include="..\lice\lice_bmp.cpp" />
|
||||||
|
<ClCompile Include="..\lice\lice_ico.cpp" />
|
||||||
|
<ClCompile Include="..\lice\lice_image.cpp" />
|
||||||
|
<ClCompile Include="..\lice\lice_line.cpp" />
|
||||||
|
<ClCompile Include="..\lice\lice_lvg.cpp" />
|
||||||
|
<ClCompile Include="..\lice\lice_pcx.cpp" />
|
||||||
|
<ClCompile Include="..\lice\lice_text.cpp" />
|
||||||
|
<ClCompile Include="..\lice\lice_textnew.cpp" />
|
||||||
|
<ClCompile Include="..\fft.c" />
|
||||||
|
<ClCompile Include=".\loose_eel.cpp" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClInclude Include=".\ns-eel-addfuncs.h" />
|
||||||
|
<ClInclude Include=".\ns-eel-int.h" />
|
||||||
|
<ClInclude Include=".\ns-eel.h" />
|
||||||
|
</ItemGroup>
|
||||||
|
<PropertyGroup Label="Globals">
|
||||||
|
<ProjectGuid>{893B9C0E-73CA-4843-A3BC-8A3FF9055FA8}</ProjectGuid>
|
||||||
|
<Keyword>Win32Proj</Keyword>
|
||||||
|
<RootNamespace>loose_eel</RootNamespace>
|
||||||
|
</PropertyGroup>
|
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>true</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v120</PlatformToolset>
|
||||||
|
<CharacterSet>MultiByte</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>true</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v120</PlatformToolset>
|
||||||
|
<CharacterSet>MultiByte</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>false</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v120</PlatformToolset>
|
||||||
|
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||||
|
<CharacterSet>MultiByte</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>false</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v120</PlatformToolset>
|
||||||
|
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||||
|
<CharacterSet>MultiByte</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||||
|
<ImportGroup Label="ExtensionSettings">
|
||||||
|
</ImportGroup>
|
||||||
|
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||||
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||||
|
</ImportGroup>
|
||||||
|
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
|
||||||
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||||
|
</ImportGroup>
|
||||||
|
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||||
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||||
|
</ImportGroup>
|
||||||
|
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
|
||||||
|
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||||
|
</ImportGroup>
|
||||||
|
<PropertyGroup Label="UserMacros" />
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||||
|
<LinkIncremental>true</LinkIncremental>
|
||||||
|
<IntDir>$(Configuration)_new\</IntDir>
|
||||||
|
<OutDir>$(SolutionDir)$(Configuration)_new\</OutDir>
|
||||||
|
<TargetExt>.exe</TargetExt>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||||
|
<LinkIncremental>true</LinkIncremental>
|
||||||
|
<TargetExt>.exe</TargetExt>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||||
|
<LinkIncremental>false</LinkIncremental>
|
||||||
|
<IntDir>$(Configuration)_new\</IntDir>
|
||||||
|
<OutDir>$(SolutionDir)$(Configuration)_new\</OutDir>
|
||||||
|
<TargetExt>.exe</TargetExt>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||||
|
<LinkIncremental>false</LinkIncremental>
|
||||||
|
<TargetExt>.exe</TargetExt>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<Optimization>Disabled</Optimization>
|
||||||
|
<PreprocessorDefinitions>WIN32;BUILD_WINDOWS;_CRT_SECURE_NO_WARNINGS;_CRT_NONSTDC_NO_WARNINGS;WDL_FFT_REALSIZE=8;EEL_LICE_WANT_STANDALONE;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<MultiProcessorCompilation>true</MultiProcessorCompilation>
|
||||||
|
<MinimalRebuild>false</MinimalRebuild>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<SubSystem>Console</SubSystem>
|
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;winmm.lib;wsock32.lib;comctl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<Optimization>Disabled</Optimization>
|
||||||
|
<PreprocessorDefinitions>WIN32;BUILD_WINDOWS;_CRT_SECURE_NO_WARNINGS;_CRT_NONSTDC_NO_WARNINGS;WDL_FFT_REALSIZE=8;EEL_LICE_WANT_STANDALONE;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<MultiProcessorCompilation>true</MultiProcessorCompilation>
|
||||||
|
<MinimalRebuild>false</MinimalRebuild>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<SubSystem>Console</SubSystem>
|
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;winmm.lib;wsock32.lib;comctl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||||
|
<Optimization>MaxSpeed</Optimization>
|
||||||
|
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||||
|
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||||
|
<PreprocessorDefinitions>WIN32;BUILD_WINDOWS;_CRT_SECURE_NO_WARNINGS;_CRT_NONSTDC_NO_WARNINGS;WDL_FFT_REALSIZE=8;EEL_LICE_WANT_STANDALONE;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<MultiProcessorCompilation>true</MultiProcessorCompilation>
|
||||||
|
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<SubSystem>Console</SubSystem>
|
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||||
|
<OptimizeReferences>true</OptimizeReferences>
|
||||||
|
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;winmm.lib;wsock32.lib;comctl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||||
|
<Optimization>MaxSpeed</Optimization>
|
||||||
|
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||||
|
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||||
|
<PreprocessorDefinitions>WIN32;BUILD_WINDOWS;_CRT_SECURE_NO_WARNINGS;_CRT_NONSTDC_NO_WARNINGS;WDL_FFT_REALSIZE=8;EEL_LICE_WANT_STANDALONE;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<MultiProcessorCompilation>true</MultiProcessorCompilation>
|
||||||
|
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<SubSystem>Console</SubSystem>
|
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||||
|
<OptimizeReferences>true</OptimizeReferences>
|
||||||
|
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;winmm.lib;wsock32.lib;comctl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
</ImportGroup>
|
||||||
|
</Project>
|
||||||
95
oversampling/WDL/eel2/loose_eel.vcxproj.filters
Normal file
95
oversampling/WDL/eel2/loose_eel.vcxproj.filters
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<ItemGroup>
|
||||||
|
<Filter Include="Source Files">
|
||||||
|
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||||
|
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||||
|
</Filter>
|
||||||
|
<Filter Include="Header Files">
|
||||||
|
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||||
|
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
|
||||||
|
</Filter>
|
||||||
|
<Filter Include="Resource Files">
|
||||||
|
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||||
|
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||||
|
</Filter>
|
||||||
|
<Filter Include="Source Files\lice">
|
||||||
|
<UniqueIdentifier>{3A983C0A-E7C1-449D-B869-8CC1CE792C2F}</UniqueIdentifier>
|
||||||
|
</Filter>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include=".\nseel-caltab.c">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include=".\nseel-cfunc.c">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include=".\nseel-compiler.c">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include=".\nseel-eval.c">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include=".\nseel-lextab.c">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include=".\nseel-ram.c">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include=".\nseel-yylex.c">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\lice\lice.cpp">
|
||||||
|
<Filter>Source Files\lice</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\lice\lice_arc.cpp">
|
||||||
|
<Filter>Source Files\lice</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\lice\lice_bmp.cpp">
|
||||||
|
<Filter>Source Files\lice</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\lice\lice_ico.cpp">
|
||||||
|
<Filter>Source Files\lice</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\lice\lice_image.cpp">
|
||||||
|
<Filter>Source Files\lice</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\lice\lice_line.cpp">
|
||||||
|
<Filter>Source Files\lice</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\lice\lice_lvg.cpp">
|
||||||
|
<Filter>Source Files\lice</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\lice\lice_pcx.cpp">
|
||||||
|
<Filter>Source Files\lice</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\lice\lice_text.cpp">
|
||||||
|
<Filter>Source Files\lice</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\lice\lice_textnew.cpp">
|
||||||
|
<Filter>Source Files\lice</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\fft.c">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include=".\loose_eel.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClInclude Include=".\ns-eel-addfuncs.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include=".\ns-eel-int.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include=".\ns-eel.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Object Include="asm-nseel-x64.obj">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</Object>
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
||||||
63
oversampling/WDL/eel2/makefile.vc
Normal file
63
oversampling/WDL/eel2/makefile.vc
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
|
||||||
|
EEL_SOURCE = nseel-caltab.c nseel-cfunc.c nseel-compiler.c nseel-eval.c nseel-lextab.c nseel-ram.c nseel-yylex.c
|
||||||
|
|
||||||
|
LICE_SOURCE = ../lice/lice.cpp ../lice/lice_image.cpp ../lice/lice_line.cpp ../lice/lice_ico.cpp ../lice/lice_bmp.cpp ../lice/lice_textnew.cpp ../lice/lice_text.cpp ../lice/lice_arc.cpp
|
||||||
|
|
||||||
|
FFT_SOURCE = ../fft.c
|
||||||
|
CFLAGS = /DWDL_FFT_REALSIZE=8 /DEEL_LICE_WANT_STANDALONE
|
||||||
|
|
||||||
|
CFLAGS = $(CFLAGS) -DNSEEL_LOOPFUNC_SUPPORT_MAXLEN=0
|
||||||
|
|
||||||
|
LFLAGS = shell32.lib user32.lib comdlg32.lib
|
||||||
|
|
||||||
|
!ifndef VC6
|
||||||
|
CFLAGS = $(CFLAGS) /MT
|
||||||
|
!else
|
||||||
|
CFLAGS = $(CFLAGS) /MD
|
||||||
|
LFLAGS = $(LFLAGS) /OPT:NOWIN98
|
||||||
|
!endif
|
||||||
|
|
||||||
|
!ifdef x64
|
||||||
|
!ifndef PORTABLE
|
||||||
|
EEL_SOURCE = $(EEL_SOURCE) asm-nseel-x64.obj
|
||||||
|
!endif
|
||||||
|
!endif
|
||||||
|
|
||||||
|
!ifdef arm64
|
||||||
|
!ifndef PORTABLE
|
||||||
|
EEL_SOURCE = $(EEL_SOURCE) asm-nseel-aarch64-msvc.obj
|
||||||
|
!endif
|
||||||
|
!endif
|
||||||
|
|
||||||
|
!ifdef arm64ec
|
||||||
|
!ifndef PORTABLE
|
||||||
|
EEL_SOURCE = $(EEL_SOURCE) asm-nseel-arm64ec.obj
|
||||||
|
!endif
|
||||||
|
!endif
|
||||||
|
|
||||||
|
|
||||||
|
default: loose_eel.exe
|
||||||
|
|
||||||
|
!ifdef PORTABLE
|
||||||
|
CFLAGS = $(CFLAGS) -DEEL_TARGET_PORTABLE
|
||||||
|
!endif
|
||||||
|
|
||||||
|
loose_eel.cpp: eel*.h ns-eel*.h
|
||||||
|
|
||||||
|
$(EEL_SOURCE): ns-eel*.h
|
||||||
|
|
||||||
|
|
||||||
|
loose_eel.exe: loose_eel.cpp $(EEL_SOURCE) $(FFT_SOURCE) $(LICE_SOURCE) ..\win32_utf8.c
|
||||||
|
cl $(CFLAGS) $** /link wsock32.lib user32.lib gdi32.lib advapi32.lib $(LFLAGS) /out:$@
|
||||||
|
|
||||||
|
eel_disasm.exe: eel_disasm.cpp $(EEL_SOURCE)
|
||||||
|
cl $(CFLAGS) $** /link wsock32.lib user32.lib gdi32.lib advapi32.lib $(LFLAGS) /out:$@
|
||||||
|
|
||||||
|
asm-nseel-aarch64-msvc.obj: asm-nseel-aarch64-msvc.asm
|
||||||
|
armasm64 asm-nseel-aarch64-msvc.asm
|
||||||
|
|
||||||
|
asm-nseel-arm64ec.obj: asm-nseel-arm64ec.asm
|
||||||
|
armasm64 /machine arm64EC asm-nseel-arm64ec.asm
|
||||||
|
|
||||||
|
test: eel_disasm.exe
|
||||||
|
eel_disasm "buf[a] = x*y"
|
||||||
63
oversampling/WDL/eel2/ns-eel-addfuncs.h
Normal file
63
oversampling/WDL/eel2/ns-eel-addfuncs.h
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
/*
|
||||||
|
Nullsoft Expression Evaluator Library (NS-EEL)
|
||||||
|
Copyright (C) 1999-2003 Nullsoft, Inc.
|
||||||
|
|
||||||
|
ns-eel-addfuncs.h: defines macros useful for adding functions to the compiler
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied
|
||||||
|
warranty. In no event will the authors be held liable for any damages
|
||||||
|
arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
|
including commercial applications, and to alter it and redistribute it
|
||||||
|
freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not
|
||||||
|
claim that you wrote the original software. If you use this software
|
||||||
|
in a product, an acknowledgment in the product documentation would be
|
||||||
|
appreciated but is not required.
|
||||||
|
2. Altered source versions must be plainly marked as such, and must not be
|
||||||
|
misrepresented as being the original software.
|
||||||
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __NS_EEL_ADDFUNCS_H__
|
||||||
|
#define __NS_EEL_ADDFUNCS_H__
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct _compileContext;
|
||||||
|
|
||||||
|
void *NSEEL_PProc_RAM(void *data, int data_size, struct _compileContext *ctx);
|
||||||
|
void *NSEEL_PProc_THIS(void *data, int data_size, struct _compileContext *ctx);
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef EEL_TARGET_PORTABLE
|
||||||
|
|
||||||
|
extern EEL_BC_TYPE _asm_generic3parm[]; // 3 double * parms, returning double *
|
||||||
|
extern EEL_BC_TYPE _asm_generic3parm_retd[]; // 3 double * parms, returning double
|
||||||
|
extern EEL_BC_TYPE _asm_generic2parm[]; // 2 double * parms, returning double *
|
||||||
|
extern EEL_BC_TYPE _asm_generic2parm_retd[]; // 2 double * parms, returning double
|
||||||
|
extern EEL_BC_TYPE _asm_generic2xparm_retd[]; // 2 double * parms, returning double
|
||||||
|
extern EEL_BC_TYPE _asm_generic1parm[]; // 1 double * parms, returning double *
|
||||||
|
extern EEL_BC_TYPE _asm_generic1parm_retd[]; // 1 double * parms, returning double
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
void _asm_generic3parm(void); // 3 double * parms, returning double *
|
||||||
|
void _asm_generic3parm_retd(void); // 3 double * parms, returning double
|
||||||
|
void _asm_generic2parm(void); // 2 double * parms, returning double *
|
||||||
|
void _asm_generic2parm_retd(void); // 2 double * parms, returning double
|
||||||
|
void _asm_generic2xparm_retd(void); // 2 double * parms, returning double
|
||||||
|
void _asm_generic1parm(void); // 1 double * parms, returning double *
|
||||||
|
void _asm_generic1parm_retd(void); // 1 double * parms, returning double
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
#endif//__NS_EEL_ADDFUNCS_H__
|
||||||
64
oversampling/WDL/eel2/ns-eel-func-ref.h
Normal file
64
oversampling/WDL/eel2/ns-eel-func-ref.h
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
#ifndef _NSEEL_FUNC_REF_H_
|
||||||
|
#define _NSEEL_FUNC_REF_H_
|
||||||
|
|
||||||
|
#include "ns-eel.h"
|
||||||
|
#define TMP_MKSTR2(x) #x
|
||||||
|
#define TMP_MKSTR(x) TMP_MKSTR2(x)
|
||||||
|
|
||||||
|
const char *nseel_builtin_function_reference=
|
||||||
|
"while\texpression\tExecutes expression until expression evaluates to zero"
|
||||||
|
#if NSEEL_LOOPFUNC_SUPPORT_MAXLEN
|
||||||
|
", or until " TMP_MKSTR(NSEEL_LOOPFUNC_SUPPORT_MAXLEN) "iterations occur"
|
||||||
|
#endif
|
||||||
|
". An alternate and more useful syntax is while (expression) ( statements ), which evaluates statements after "
|
||||||
|
"every non-zero evaluation of expression.\0"
|
||||||
|
"loop\tcount,expression\tEvaluates count once, and then executes expression count"
|
||||||
|
#if NSEEL_LOOPFUNC_SUPPORT_MAXLEN
|
||||||
|
", but not more than " TMP_MKSTR(NSEEL_LOOPFUNC_SUPPORT_MAXLEN) ","
|
||||||
|
#endif
|
||||||
|
" times.\0"
|
||||||
|
"sin\tangle\tReturns the sine of the angle specified (specified in radians -- to convert from degrees to radians, multiply by $pi/180, or 0.017453).\0"
|
||||||
|
"cos\tangle\tReturns the cosine of the angle specified (specified in radians).\0"
|
||||||
|
"tan\tangle\tReturns the tangent of the angle specified (specified in radians).\0"
|
||||||
|
"sqrt\tvalue\tReturns the square root of the parameter. If the parameter is negative, the return value is undefined.\0"
|
||||||
|
"log\tvalue\tReturns the natural logarithm (base e) of the parameter. If the value is not greater than 0, the return value is undefined.\0"
|
||||||
|
"log10\tvalue\tReturns the base-10 logarithm of the parameter. If the value is not greater than 0, the return value is undefined.\0"
|
||||||
|
"asin\tvalue\tReturns the arc sine of the value specified (return value is in radians). If the parameter is not between -1.0 and 1.0 inclusive, the return value is undefined.\0"
|
||||||
|
"acos\tvalue\tReturns the arc cosine of the value specified (return value is in radians). If the parameter is not between -1.0 and 1.0 inclusive, the return value is undefined.\0"
|
||||||
|
"atan\tvalue\tReturns the arc tangent of the value specified (return value is in radians). If the parameter is not between -1.0 and 1.0 inclusive, the return value is undefined.\0"
|
||||||
|
"atan2\tnumerator,denominator\tReturns the arc tangent of the numerator divided by the denominator, allowing the denominator to be 0, and using their signs to produce a more meaningful result.\0"
|
||||||
|
"exp\texponent\tReturns the number e ($e, approximately 2.718) raised to the parameter-th power. This function is significantly faster than pow() or the ^ operator.\0"
|
||||||
|
"abs\tvalue\tReturns the absolute value of the parameter.\0"
|
||||||
|
"sqr\tvalue\tReturns the square of the parameter (similar to value*value, but only evaluating value once).\0"
|
||||||
|
"min\t&value,&value\tReturns (by reference) the minimum value of the two parameters. Since min() returns by reference, expressions such as min(x,y) = 5 are possible.\0"
|
||||||
|
"max\t&value,&value\tReturns (by reference) the maximum value of the two parameters. Since max() returns by reference, expressions such as max(x,y) = 5 are possible.\0"
|
||||||
|
"sign\tvalue\tReturns 1.0 if the parameter is greater than 0, -1.0 if the parameter is less than 0, or 0 if the parameter is 0.\0"
|
||||||
|
"floor\tvalue\tReturns the value rounded to the next lowest integer (floor(3.9)==3, floor(-3.1)==-4).\0"
|
||||||
|
"ceil\tvalue\tReturns the value rounded to the next highest integer (ceil(3.1)==4, ceil(-3.9)==-3).\0"
|
||||||
|
"invsqrt\tvalue\tReturns a fast inverse square root (1/sqrt(x)) approximation of the parameter.\0"
|
||||||
|
"freembuf\taddress\tHints the runtime that memory above the address specified may no longer be used. The runtime may, at its leisure, choose to lose the contents of memory above the address specified.\0"
|
||||||
|
"memcpy\tdest,src,length\tCopies length items of memory from src to dest. Regions are permitted to overlap.\0"
|
||||||
|
"memset\toffset,value,length\tSets length items of memory at offset to value.\0"
|
||||||
|
"mem_get_values\toffset, ...\tReads values from memory starting at offset into variables specified. Slower than regular memory reads for less than a few variables, faster for more than a few. Undefined behavior if used with more than 32767 variables.\0"
|
||||||
|
"mem_set_values\toffset, ...\tWrites values to memory starting at offset from variables specified. Slower than regular memory writes for less than a few variables, faster for more than a few. Undefined behavior if used with more than 32767 variables.\0"
|
||||||
|
"mem_multiply_sum\tsrc1,src2,length\tCalculates the sum of the products of values pointed to by src1 and src2. If src2 is -1, then calculates the sum of squares of src1, if -2, the sum of the absolute values of src, if -3, calculates the sum of the values of src1. Other negative values are undefined.\0"
|
||||||
|
"mem_insert_shuffle\tbuf,len,value\tShuffles contents of buf right by 1, inserts value at buf[0], returns previous buf[len-1].\0"
|
||||||
|
"stack_push\t&value\tPushes value onto the user stack, returns a reference to the parameter.\0"
|
||||||
|
"stack_pop\t&value\tPops a value from the user stack into value, or into a temporary buffer if value is not specified, and returns a reference to where the stack was popped. Note that no checking is done to determine if the stack is empty, and as such stack_pop() will never fail.\0"
|
||||||
|
"stack_peek\tindex\tReturns a reference to the item on the top of the stack (if index is 0), or to the Nth item on the stack if index is greater than 0. \0"
|
||||||
|
"stack_exch\t&value\tExchanges a value with the top of the stack, and returns a reference to the parameter (with the new value).\0"
|
||||||
|
#ifdef NSEEL_EEL1_COMPAT_MODE
|
||||||
|
"rand\tmax\tReturns a pseudorandom non-negative integer number less than the parameter.\0"
|
||||||
|
"sigmoid\tvalue,constraint\tReturns 1.0/(1+exp(-x * (constraint))), or 0 if a divide by 0 would occur.\0"
|
||||||
|
"band\tx,y\tReturns 1 if both x and y evaluate to nonzero, 0 if otherwise. Both parameters are always evaluated.\0"
|
||||||
|
"bor\tx,y\tReturns 1 if either x or y evaluate to nonzero, 0 if otherwise. Both parameters are always evaluated.\0"
|
||||||
|
"exec2\tx,y\tEvaluates x, then evaluates and returns y.\0"
|
||||||
|
"exec3\tx,y,z\tEvaluates x, evaluates y, then evaluates and returns z.\0"
|
||||||
|
#else
|
||||||
|
"rand\t[max]\tReturns a pseudorandom real number between 0 and the parameter, inclusive. If the parameter is omitted or less than 1.0, 1.0 is used as a maximum instead.\0"
|
||||||
|
|
||||||
|
#endif
|
||||||
|
;
|
||||||
|
#undef TMP_MKSTR
|
||||||
|
|
||||||
|
#endif
|
||||||
331
oversampling/WDL/eel2/ns-eel-int.h
Normal file
331
oversampling/WDL/eel2/ns-eel-int.h
Normal file
@@ -0,0 +1,331 @@
|
|||||||
|
/*
|
||||||
|
Nullsoft Expression Evaluator Library (NS-EEL)
|
||||||
|
Copyright (C) 1999-2003 Nullsoft, Inc.
|
||||||
|
|
||||||
|
ns-eel-int.h: internal code definition header.
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied
|
||||||
|
warranty. In no event will the authors be held liable for any damages
|
||||||
|
arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
|
including commercial applications, and to alter it and redistribute it
|
||||||
|
freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not
|
||||||
|
claim that you wrote the original software. If you use this software
|
||||||
|
in a product, an acknowledgment in the product documentation would be
|
||||||
|
appreciated but is not required.
|
||||||
|
2. Altered source versions must be plainly marked as such, and must not be
|
||||||
|
misrepresented as being the original software.
|
||||||
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __NS_EELINT_H__
|
||||||
|
#define __NS_EELINT_H__
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#else
|
||||||
|
#include "../wdltypes.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "ns-eel.h"
|
||||||
|
#include "ns-eel-addfuncs.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
enum {
|
||||||
|
|
||||||
|
// these ignore fn in opcodes, just use fntype to determine function
|
||||||
|
FN_MULTIPLY=0,
|
||||||
|
FN_DIVIDE,
|
||||||
|
FN_JOIN_STATEMENTS,
|
||||||
|
FN_DENORMAL_LIKELY,
|
||||||
|
FN_DENORMAL_UNLIKELY,
|
||||||
|
FN_ADD,
|
||||||
|
FN_SUB,
|
||||||
|
FN_AND,
|
||||||
|
FN_OR,
|
||||||
|
FN_UMINUS,
|
||||||
|
FN_NOT,
|
||||||
|
FN_NOTNOT,
|
||||||
|
FN_XOR,
|
||||||
|
FN_SHL,
|
||||||
|
FN_SHR,
|
||||||
|
FN_MOD,
|
||||||
|
FN_POW,
|
||||||
|
FN_LT,
|
||||||
|
FN_GT,
|
||||||
|
FN_LTE,
|
||||||
|
FN_GTE,
|
||||||
|
FN_EQ,
|
||||||
|
FN_EQ_EXACT,
|
||||||
|
FN_NE,
|
||||||
|
FN_NE_EXACT,
|
||||||
|
FN_LOGICAL_AND,
|
||||||
|
FN_LOGICAL_OR,
|
||||||
|
FN_IF_ELSE,
|
||||||
|
FN_MEMORY,
|
||||||
|
FN_GMEMORY,
|
||||||
|
FN_NONCONST_BEGIN,
|
||||||
|
FN_ASSIGN=FN_NONCONST_BEGIN,
|
||||||
|
|
||||||
|
FN_ADD_OP,
|
||||||
|
FN_SUB_OP,
|
||||||
|
FN_MOD_OP,
|
||||||
|
FN_OR_OP,
|
||||||
|
FN_AND_OP,
|
||||||
|
FN_XOR_OP,
|
||||||
|
FN_DIV_OP,
|
||||||
|
FN_MUL_OP,
|
||||||
|
FN_POW_OP,
|
||||||
|
|
||||||
|
FN_WHILE,
|
||||||
|
FN_LOOP,
|
||||||
|
|
||||||
|
FUNCTYPE_SIMPLEMAX,
|
||||||
|
|
||||||
|
|
||||||
|
FUNCTYPE_FUNCTIONTYPEREC=1000, // fn is a functionType *
|
||||||
|
FUNCTYPE_EELFUNC, // fn is a _codeHandleFunctionRec *
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#define YYSTYPE opcodeRec *
|
||||||
|
|
||||||
|
#define NSEEL_CLOSEFACTOR 0.00001
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct opcodeRec opcodeRec;
|
||||||
|
|
||||||
|
typedef struct _codeHandleFunctionRec
|
||||||
|
{
|
||||||
|
struct _codeHandleFunctionRec *next; // main linked list (only used for high level functions)
|
||||||
|
struct _codeHandleFunctionRec *derivedCopies; // separate linked list, head being the main function, other copies being derived versions
|
||||||
|
|
||||||
|
void *startptr; // compiled code (may be cleared + recompiled when shared)
|
||||||
|
opcodeRec *opcodes;
|
||||||
|
|
||||||
|
int startptr_size; // 0=no code. -1 = needs calculation. >0 = size.
|
||||||
|
int startptr_base_size; // initially calculated size of root function
|
||||||
|
int tmpspace_req;
|
||||||
|
|
||||||
|
int num_params;
|
||||||
|
|
||||||
|
int rvMode; // RETURNVALUE_*
|
||||||
|
int fpStackUsage; // 0-8, usually
|
||||||
|
int canHaveDenormalOutput;
|
||||||
|
|
||||||
|
// local storage's first items are the parameters, then locals. Note that the opcodes will reference localstorage[] via VARPTRPTR, but
|
||||||
|
// the values localstorage[x] points are reallocated from context-to-context, if it is a common function.
|
||||||
|
|
||||||
|
// separately allocated list of pointers, the contents of the list should be zeroed on context changes if a common function
|
||||||
|
// note that when making variations on a function (context), it is shared, but since it is zeroed on context changes, it is context-local
|
||||||
|
int localstorage_size;
|
||||||
|
EEL_F **localstorage;
|
||||||
|
|
||||||
|
int isCommonFunction;
|
||||||
|
int usesNamespaces;
|
||||||
|
unsigned int parameterAsNamespaceMask;
|
||||||
|
|
||||||
|
char fname[NSEEL_MAX_FUNCSIG_NAME+1];
|
||||||
|
} _codeHandleFunctionRec;
|
||||||
|
|
||||||
|
typedef struct _llBlock {
|
||||||
|
struct _llBlock *next;
|
||||||
|
int sizeused;
|
||||||
|
int sizealloc;
|
||||||
|
// data follows
|
||||||
|
} llBlock;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
llBlock *blocks_code, *blocks_data;
|
||||||
|
void *workTable; // references a chunk in blocks_data
|
||||||
|
|
||||||
|
void *code;
|
||||||
|
int code_size; // in case the caller wants to write it out
|
||||||
|
int code_stats[4];
|
||||||
|
|
||||||
|
int want_stack;
|
||||||
|
void *stack; // references a chunk in blocks_data, somewhere within the complete NSEEL_STACK_SIZE aligned at NSEEL_STACK_SIZE
|
||||||
|
|
||||||
|
void *ramPtr;
|
||||||
|
|
||||||
|
int workTable_size; // size (minus padding/extra space) of workTable -- only used if EEL_VALIDATE_WORKTABLE_USE set, but might be handy to have around too
|
||||||
|
int compile_flags;
|
||||||
|
} codeHandleType;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
EEL_F *value;
|
||||||
|
int refcnt;
|
||||||
|
char isreg;
|
||||||
|
char str[1];
|
||||||
|
} varNameRec;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
void *ptr;
|
||||||
|
int size, alloc;
|
||||||
|
} eel_growbuf;
|
||||||
|
#define EEL_GROWBUF(type) union { eel_growbuf _growbuf; type *_tval; }
|
||||||
|
#define EEL_GROWBUF_RESIZE(gb, newsz) __growbuf_resize(&(gb)->_growbuf, (newsz)*(int)sizeof((gb)->_tval[0])) // <0 to free, does not realloc down otherwise
|
||||||
|
#define EEL_GROWBUF_GET(gb) ((gb)->_tval)
|
||||||
|
#define EEL_GROWBUF_GET_SIZE(gb) ((gb)->_growbuf.size/(int)sizeof((gb)->_tval[0]))
|
||||||
|
|
||||||
|
typedef struct _compileContext
|
||||||
|
{
|
||||||
|
eel_function_table *registered_func_tab;
|
||||||
|
const char *(*func_check)(const char *fn_name, void *user); // return error message if not permitted
|
||||||
|
void *func_check_user;
|
||||||
|
|
||||||
|
EEL_GROWBUF(varNameRec *) varNameList;
|
||||||
|
EEL_F *varValueStore;
|
||||||
|
int varValueStore_left;
|
||||||
|
|
||||||
|
int errVar,gotEndOfInput;
|
||||||
|
opcodeRec *result;
|
||||||
|
char last_error_string[256];
|
||||||
|
|
||||||
|
void *scanner;
|
||||||
|
const char *rdbuf_start, *rdbuf, *rdbuf_end;
|
||||||
|
|
||||||
|
llBlock *tmpblocks, // used while compiling, and freed after compiling
|
||||||
|
|
||||||
|
*blocks_head_code, // used while compiling, transferred to code context (whole pages marked as executable)
|
||||||
|
*blocks_head_data, // used while compiling, transferred to code context
|
||||||
|
|
||||||
|
*ctx_pblocks; // persistent blocks, stores data used by varTable_Names, varTable_Values, etc.
|
||||||
|
|
||||||
|
int l_stats[4]; // source bytes, static code bytes, call code bytes, data bytes
|
||||||
|
int has_used_global_vars;
|
||||||
|
|
||||||
|
_codeHandleFunctionRec *functions_local, *functions_common;
|
||||||
|
|
||||||
|
// state used while generating functions
|
||||||
|
int optimizeDisableFlags;
|
||||||
|
int current_compile_flags;
|
||||||
|
struct opcodeRec *directValueCache; // linked list using fn as next
|
||||||
|
|
||||||
|
int isSharedFunctions;
|
||||||
|
int isGeneratingCommonFunction;
|
||||||
|
int function_usesNamespaces;
|
||||||
|
int function_globalFlag; // set if restrict globals to function_localTable_Names[2]
|
||||||
|
// [0] is parameter+local symbols (combined space)
|
||||||
|
// [1] is symbols which get implied "this." if used
|
||||||
|
// [2] is globals permitted
|
||||||
|
int function_localTable_Size[3]; // for parameters only
|
||||||
|
char **function_localTable_Names[3]; // lists of pointers
|
||||||
|
EEL_F **function_localTable_ValuePtrs;
|
||||||
|
const char *function_curName; // name of current function
|
||||||
|
|
||||||
|
EEL_F (*onString)(void *caller_this, struct eelStringSegmentRec *list);
|
||||||
|
EEL_F (*onNamedString)(void *caller_this, const char *name);
|
||||||
|
|
||||||
|
EEL_F *(*getVariable)(void *userctx, const char *name);
|
||||||
|
void *getVariable_userctx;
|
||||||
|
|
||||||
|
codeHandleType *tmpCodeHandle;
|
||||||
|
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
WDL_UINT64 sign_mask[2];
|
||||||
|
WDL_UINT64 abs_mask[2];
|
||||||
|
int needfree;
|
||||||
|
int maxblocks;
|
||||||
|
double closefact;
|
||||||
|
EEL_F *blocks[NSEEL_RAM_BLOCKS];
|
||||||
|
} *ram_state; // allocated from blocks with 16 byte alignment
|
||||||
|
|
||||||
|
void *gram_blocks;
|
||||||
|
|
||||||
|
void *caller_this;
|
||||||
|
}
|
||||||
|
compileContext;
|
||||||
|
|
||||||
|
#define NSEEL_NPARAMS_FLAG_CONST 0x80000
|
||||||
|
typedef struct functionType {
|
||||||
|
const char *name;
|
||||||
|
void *afunc;
|
||||||
|
int nParams;
|
||||||
|
void *replptrs[4];
|
||||||
|
NSEEL_PPPROC pProc;
|
||||||
|
} functionType;
|
||||||
|
|
||||||
|
functionType *nseel_getFunctionByName(compileContext *ctx, const char *name, int *mchk); // sets mchk (if non-NULL) to how far allowed to scan forward for duplicate names
|
||||||
|
functionType *nseel_enumFunctions(compileContext *ctx, int idx);
|
||||||
|
|
||||||
|
opcodeRec *nseel_createCompiledValue(compileContext *ctx, EEL_F value);
|
||||||
|
opcodeRec *nseel_createCompiledValuePtr(compileContext *ctx, EEL_F *addrValue, const char *namestr);
|
||||||
|
|
||||||
|
opcodeRec *nseel_createMoreParametersOpcode(compileContext *ctx, opcodeRec *code1, opcodeRec *code2);
|
||||||
|
opcodeRec *nseel_createSimpleCompiledFunction(compileContext *ctx, int fn, int np, opcodeRec *code1, opcodeRec *code2);
|
||||||
|
opcodeRec *nseel_createMemoryAccess(compileContext *ctx, opcodeRec *code1, opcodeRec *code2);
|
||||||
|
opcodeRec *nseel_createIfElse(compileContext *ctx, opcodeRec *code1, opcodeRec *code2, opcodeRec *code3);
|
||||||
|
opcodeRec *nseel_createFunctionByName(compileContext *ctx, const char *name, int np, opcodeRec *code1, opcodeRec *code2, opcodeRec *code3);
|
||||||
|
|
||||||
|
// converts a generic identifier (VARPTR) opcode into either an actual variable reference (parmcnt = -1),
|
||||||
|
// or if parmcnt >= 0, to a function call (see nseel_setCompiledFunctionCallParameters())
|
||||||
|
opcodeRec *nseel_resolve_named_symbol(compileContext *ctx, opcodeRec *rec, int parmcnt, int *errOut);
|
||||||
|
|
||||||
|
// sets parameters and calculates parameter count for opcode, and calls nseel_resolve_named_symbol() with the right
|
||||||
|
// parameter count
|
||||||
|
opcodeRec *nseel_setCompiledFunctionCallParameters(compileContext *ctx, opcodeRec *fn, opcodeRec *code1, opcodeRec *code2, opcodeRec *code3, opcodeRec *postCode, int *errOut);
|
||||||
|
// errOut will be set if return NULL:
|
||||||
|
// -1 if postCode set when not wanted (i.e. not while())
|
||||||
|
// 0 if func not found,
|
||||||
|
// 1 if function requires 2+ parameters but was given more
|
||||||
|
// 2 if function needs more parameters
|
||||||
|
// 4 if function requires 1 parameter but was given more
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
struct eelStringSegmentRec *nseel_createStringSegmentRec(compileContext *ctx, const char *str, int len);
|
||||||
|
opcodeRec *nseel_eelMakeOpcodeFromStringSegments(compileContext *ctx, struct eelStringSegmentRec *rec);
|
||||||
|
|
||||||
|
EEL_F *nseel_int_register_var(compileContext *ctx, const char *name, int isReg, const char **namePtrOut);
|
||||||
|
_codeHandleFunctionRec *eel_createFunctionNamespacedInstance(compileContext *ctx, _codeHandleFunctionRec *fr, const char *nameptr);
|
||||||
|
|
||||||
|
typedef struct nseel_globalVarItem
|
||||||
|
{
|
||||||
|
EEL_F data;
|
||||||
|
struct nseel_globalVarItem *_next;
|
||||||
|
char name[1]; // varlen, does not include _global. prefix
|
||||||
|
} nseel_globalVarItem;
|
||||||
|
|
||||||
|
extern nseel_globalVarItem *nseel_globalreg_list; // if NSEEL_EEL1_COMPAT_MODE, must use NSEEL_getglobalregs() for regxx values
|
||||||
|
|
||||||
|
#include "y.tab.h"
|
||||||
|
|
||||||
|
// nseel_simple_tokenizer will return comments as tokens if state is non-NULL
|
||||||
|
const char *nseel_simple_tokenizer(const char **ptr, const char *endptr, int *lenOut, int *state);
|
||||||
|
int nseel_filter_escaped_string(char *outbuf, int outbuf_sz, const char *rdptr, size_t rdptr_size, char delim_char); // returns length used, minus NUL char
|
||||||
|
|
||||||
|
opcodeRec *nseel_translate(compileContext *ctx, const char *tmp, size_t tmplen); // tmplen=0 for nul-term
|
||||||
|
int nseel_lookup(compileContext *ctx, opcodeRec **opOut, const char *sname);
|
||||||
|
|
||||||
|
EEL_F * NSEEL_CGEN_CALL __NSEEL_RAMAlloc(EEL_F **blocks, unsigned int w);
|
||||||
|
EEL_F * NSEEL_CGEN_CALL __NSEEL_RAMAllocGMEM(EEL_F ***blocks, unsigned int w);
|
||||||
|
EEL_F * NSEEL_CGEN_CALL __NSEEL_RAM_MemSet(EEL_F **blocks,EEL_F *dest, EEL_F *v, EEL_F *lenptr);
|
||||||
|
EEL_F * NSEEL_CGEN_CALL __NSEEL_RAM_MemFree(void *blocks, EEL_F *which);
|
||||||
|
EEL_F * NSEEL_CGEN_CALL __NSEEL_RAM_MemTop(void *blocks, EEL_F *which);
|
||||||
|
EEL_F * NSEEL_CGEN_CALL __NSEEL_RAM_MemCpy(EEL_F **blocks,EEL_F *dest, EEL_F *src, EEL_F *lenptr);
|
||||||
|
EEL_F NSEEL_CGEN_CALL __NSEEL_RAM_MemSumProducts(EEL_F **blocks,EEL_F *dest, EEL_F *src, EEL_F *lenptr);
|
||||||
|
EEL_F NSEEL_CGEN_CALL __NSEEL_RAM_MemInsertShuffle(EEL_F **blocks,EEL_F *buf, EEL_F *len, EEL_F *value);
|
||||||
|
EEL_F NSEEL_CGEN_CALL __NSEEL_RAM_Mem_SetValues(EEL_F **blocks, INT_PTR np, EEL_F **parms);
|
||||||
|
EEL_F NSEEL_CGEN_CALL __NSEEL_RAM_Mem_GetValues(EEL_F **blocks, INT_PTR np, EEL_F **parms);
|
||||||
|
|
||||||
|
extern EEL_F nseel_ramalloc_onfail; // address returned by __NSEEL_RAMAlloc et al on failure
|
||||||
|
extern EEL_F * volatile nseel_gmembuf_default; // can free/zero this on DLL unload if needed
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#endif//__NS_EELINT_H__
|
||||||
264
oversampling/WDL/eel2/ns-eel.h
Normal file
264
oversampling/WDL/eel2/ns-eel.h
Normal file
@@ -0,0 +1,264 @@
|
|||||||
|
/*
|
||||||
|
Nullsoft Expression Evaluator Library (NS-EEL)
|
||||||
|
Copyright (C) 1999-2003 Nullsoft, Inc.
|
||||||
|
|
||||||
|
ns-eel.h: main application interface header
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied
|
||||||
|
warranty. In no event will the authors be held liable for any damages
|
||||||
|
arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
|
including commercial applications, and to alter it and redistribute it
|
||||||
|
freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not
|
||||||
|
claim that you wrote the original software. If you use this software
|
||||||
|
in a product, an acknowledgment in the product documentation would be
|
||||||
|
appreciated but is not required.
|
||||||
|
2. Altered source versions must be plainly marked as such, and must not be
|
||||||
|
misrepresented as being the original software.
|
||||||
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef __NS_EEL_H__
|
||||||
|
#define __NS_EEL_H__
|
||||||
|
|
||||||
|
// put standard includes here
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#ifndef EEL_F_SIZE
|
||||||
|
#define EEL_F_SIZE 8
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "../wdltypes.h"
|
||||||
|
|
||||||
|
typedef double EEL_F WDL_FIXALIGN;
|
||||||
|
typedef double *EEL_F_PTR;
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#define NSEEL_CGEN_CALL __cdecl
|
||||||
|
#else
|
||||||
|
#define NSEEL_CGEN_CALL
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// host should implement these (can be empty stub functions if no VM will execute code in multiple threads at once)
|
||||||
|
|
||||||
|
// implement if you will be running the code in same VM from multiple threads,
|
||||||
|
// or VMs that have the same GRAM pointer from different threads, or multiple
|
||||||
|
// VMs that have a NULL GRAM pointer from multiple threads.
|
||||||
|
// if you give each VM it's own unique GRAM and only run each VM in one thread, then you can leave it blank.
|
||||||
|
|
||||||
|
// or if you're daring....
|
||||||
|
|
||||||
|
void NSEEL_HOSTSTUB_EnterMutex();
|
||||||
|
void NSEEL_HOSTSTUB_LeaveMutex();
|
||||||
|
|
||||||
|
|
||||||
|
int NSEEL_init(); // returns nonzero on failure (only if EEL_VALIDATE_FSTUBS defined), otherwise the same as NSEEL_quit(), and completely optional
|
||||||
|
void NSEEL_quit(); // clears any added functions
|
||||||
|
|
||||||
|
|
||||||
|
// adds a function that returns a value (EEL_F)
|
||||||
|
#define NSEEL_addfunc_retval(name,np,pproc,fptr) \
|
||||||
|
NSEEL_addfunc_ret_type(name,np,1,pproc,(void *)(fptr),NSEEL_ADDFUNC_DESTINATION)
|
||||||
|
|
||||||
|
// adds a function that returns a pointer (EEL_F*)
|
||||||
|
#define NSEEL_addfunc_retptr(name,np,pproc,fptr) \
|
||||||
|
NSEEL_addfunc_ret_type(name,np,0,pproc,(void *)(fptr),NSEEL_ADDFUNC_DESTINATION)
|
||||||
|
|
||||||
|
// adds a void or bool function
|
||||||
|
#define NSEEL_addfunc_retbool(name,np,pproc,fptr) \
|
||||||
|
NSEEL_addfunc_ret_type(name,np,-1,pproc,(void *)(fptr),NSEEL_ADDFUNC_DESTINATION)
|
||||||
|
|
||||||
|
// adds a function that takes min_np or more parameters (func sig needs to be EEL_F func(void *ctx, INT_PTR np, EEL_F **parms)
|
||||||
|
#define NSEEL_addfunc_varparm(name, min_np, pproc, fptr) \
|
||||||
|
NSEEL_addfunc_varparm_ex(name,min_np,0,pproc,fptr,NSEEL_ADDFUNC_DESTINATION)
|
||||||
|
|
||||||
|
// adds a function that takes np parameters via func: sig needs to be EEL_F func(void *ctx, INT_PTR np, EEL_F **parms)
|
||||||
|
#define NSEEL_addfunc_exparms(name, np, pproc, fptr) \
|
||||||
|
NSEEL_addfunc_varparm_ex(name,np,1,pproc,fptr,NSEEL_ADDFUNC_DESTINATION)
|
||||||
|
|
||||||
|
|
||||||
|
// deprecated
|
||||||
|
#define NSEEL_addfunction(name,nparms,code,len) NSEEL_addfunctionex((name),(nparms),(code),(len),0,0)
|
||||||
|
#define NSEEL_addfunctionex(name,nparms,code,len,pproc,fptr) NSEEL_addfunctionex2((name),(nparms),(code),(len),(pproc),(fptr),0, NSEEL_ADDFUNC_DESTINATION)
|
||||||
|
|
||||||
|
#ifndef NSEEL_ADDFUNC_DESTINATION
|
||||||
|
#define NSEEL_ADDFUNC_DESTINATION (NULL)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct functionType;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
struct functionType *list;
|
||||||
|
int list_size;
|
||||||
|
} eel_function_table;
|
||||||
|
|
||||||
|
struct _compileContext;
|
||||||
|
typedef void *(*NSEEL_PPPROC)(void *data, int data_size, struct _compileContext *userfunc_data);
|
||||||
|
void NSEEL_addfunctionex2(const char *name, int nparms, char *code_startaddr, int code_len, NSEEL_PPPROC pproc, void *fptr, void *fptr2, eel_function_table *destination);
|
||||||
|
|
||||||
|
void NSEEL_addfunc_ret_type(const char *name, int np, int ret_type, NSEEL_PPPROC pproc, void *fptr, eel_function_table *destination); // ret_type=-1 for bool, 1 for value, 0 for ptr
|
||||||
|
void NSEEL_addfunc_varparm_ex(const char *name, int min_np, int want_exact, NSEEL_PPPROC pproc, EEL_F (NSEEL_CGEN_CALL *fptr)(void *, INT_PTR, EEL_F **), eel_function_table *destination);
|
||||||
|
void NSEEL_addfunc_varparm_ctxptr(const char *name, int min_np, int want_exact, void *ctxptr, EEL_F (NSEEL_CGEN_CALL *fptr)(void *, INT_PTR, EEL_F **), eel_function_table *destination);
|
||||||
|
void NSEEL_addfunc_varparm_ctxptr2(const char *name, int min_np, int want_exact, NSEEL_PPPROC pproc, void *ctx, EEL_F (NSEEL_CGEN_CALL *fptr)(void *, void *,INT_PTR, EEL_F **), eel_function_table *destination);
|
||||||
|
|
||||||
|
int *NSEEL_getstats(); // returns a pointer to 5 ints... source bytes, static code bytes, call code bytes, data bytes, number of code handles
|
||||||
|
|
||||||
|
typedef void *NSEEL_VMCTX;
|
||||||
|
typedef void *NSEEL_CODEHANDLE;
|
||||||
|
|
||||||
|
NSEEL_VMCTX NSEEL_VM_alloc(); // return a handle
|
||||||
|
void NSEEL_VM_free(NSEEL_VMCTX ctx); // free when done with a VM and ALL of its code have been freed, as well
|
||||||
|
|
||||||
|
void NSEEL_VM_SetFunctionTable(NSEEL_VMCTX, eel_function_table *tab); // use NULL to use default (global) table
|
||||||
|
|
||||||
|
// validateFunc can return error message if not permitted
|
||||||
|
void NSEEL_VM_SetFunctionValidator(NSEEL_VMCTX, const char * (*validateFunc)(const char *fn_name, void *user), void *user);
|
||||||
|
|
||||||
|
void NSEEL_VM_remove_unused_vars(NSEEL_VMCTX _ctx);
|
||||||
|
void NSEEL_VM_clear_var_refcnts(NSEEL_VMCTX _ctx);
|
||||||
|
void NSEEL_VM_remove_all_nonreg_vars(NSEEL_VMCTX _ctx);
|
||||||
|
void NSEEL_VM_enumallvars(NSEEL_VMCTX ctx, int (*func)(const char *name, EEL_F *val, void *ctx), void *userctx); // return false from func to stop
|
||||||
|
|
||||||
|
EEL_F *NSEEL_VM_regvar(NSEEL_VMCTX ctx, const char *name); // register a variable (before compilation)
|
||||||
|
EEL_F *NSEEL_VM_getvar(NSEEL_VMCTX ctx, const char *name); // get a variable (if registered or created by code)
|
||||||
|
int NSEEL_VM_get_var_refcnt(NSEEL_VMCTX _ctx, const char *name); // returns -1 if not registered, or >=0
|
||||||
|
void NSEEL_VM_set_var_resolver(NSEEL_VMCTX ctx, EEL_F *(*res)(void *userctx, const char *name), void *userctx);
|
||||||
|
|
||||||
|
void NSEEL_VM_freeRAM(NSEEL_VMCTX ctx); // clears and frees all (VM) RAM used
|
||||||
|
void NSEEL_VM_freeRAMIfCodeRequested(NSEEL_VMCTX); // call after code to free the script-requested memory
|
||||||
|
int NSEEL_VM_wantfreeRAM(NSEEL_VMCTX ctx); // want NSEEL_VM_freeRAMIfCodeRequested?
|
||||||
|
|
||||||
|
// if you set this, it uses a local GMEM context.
|
||||||
|
// Must be set before compilation.
|
||||||
|
// void *p=NULL;
|
||||||
|
// NSEEL_VM_SetGRAM(ctx,&p);
|
||||||
|
// .. do stuff
|
||||||
|
// NSEEL_VM_FreeGRAM(&p);
|
||||||
|
void NSEEL_VM_SetGRAM(NSEEL_VMCTX ctx, void **gram);
|
||||||
|
void NSEEL_VM_FreeGRAM(void **ufd); // frees a gmem context.
|
||||||
|
void NSEEL_VM_SetCustomFuncThis(NSEEL_VMCTX ctx, void *thisptr);
|
||||||
|
|
||||||
|
EEL_F *NSEEL_VM_getramptr(NSEEL_VMCTX ctx, unsigned int offs, int *validCount);
|
||||||
|
EEL_F *NSEEL_VM_getramptr_noalloc(NSEEL_VMCTX ctx, unsigned int offs, int *validCount);
|
||||||
|
|
||||||
|
|
||||||
|
// set 0 to query. returns actual value used (limits, granularity apply -- see NSEEL_RAM_BLOCKS)
|
||||||
|
int NSEEL_VM_setramsize(NSEEL_VMCTX ctx, int maxent);
|
||||||
|
void NSEEL_VM_preallocram(NSEEL_VMCTX ctx, int maxent); // maxent=-1 for all allocated
|
||||||
|
|
||||||
|
|
||||||
|
struct eelStringSegmentRec {
|
||||||
|
struct eelStringSegmentRec *_next;
|
||||||
|
const char *str_start; // escaped characters, including opening/trailing characters
|
||||||
|
int str_len;
|
||||||
|
};
|
||||||
|
void NSEEL_VM_SetStringFunc(NSEEL_VMCTX ctx,
|
||||||
|
EEL_F (*onString)(void *caller_this, struct eelStringSegmentRec *list),
|
||||||
|
EEL_F (*onNamedString)(void *caller_this, const char *name));
|
||||||
|
|
||||||
|
// call with NULL to calculate size, or non-null to generate to buffer (returning size used -- will not null terminate, caller responsibility)
|
||||||
|
int nseel_stringsegments_tobuf(char *bufOut, int bufout_sz, struct eelStringSegmentRec *list);
|
||||||
|
|
||||||
|
|
||||||
|
NSEEL_CODEHANDLE NSEEL_code_compile(NSEEL_VMCTX ctx, const char *code, int lineoffs);
|
||||||
|
#define NSEEL_CODE_COMPILE_FLAG_COMMONFUNCS 1 // allows that code's functions to be used in other code (note you shouldn't destroy that codehandle without destroying others first if used)
|
||||||
|
#define NSEEL_CODE_COMPILE_FLAG_COMMONFUNCS_RESET 2 // resets common code functions
|
||||||
|
#define NSEEL_CODE_COMPILE_FLAG_NOFPSTATE 4 // hint that the FPU/SSE state should be good-to-go
|
||||||
|
#define NSEEL_CODE_COMPILE_FLAG_ONLY_BUILTIN_FUNCTIONS 8 // very restrictive mode (only math functions really)
|
||||||
|
|
||||||
|
NSEEL_CODEHANDLE NSEEL_code_compile_ex(NSEEL_VMCTX ctx, const char *code, int lineoffs, int flags);
|
||||||
|
|
||||||
|
char *NSEEL_code_getcodeerror(NSEEL_VMCTX ctx);
|
||||||
|
int NSEEL_code_geterror_flag(NSEEL_VMCTX ctx);
|
||||||
|
void NSEEL_code_execute(NSEEL_CODEHANDLE code);
|
||||||
|
void NSEEL_code_free(NSEEL_CODEHANDLE code);
|
||||||
|
int *NSEEL_code_getstats(NSEEL_CODEHANDLE code); // 4 ints...source bytes, static code bytes, call code bytes, data bytes
|
||||||
|
|
||||||
|
|
||||||
|
// global memory control/view
|
||||||
|
extern unsigned int NSEEL_RAM_limitmem; // if nonzero, memory limit for user data, in bytes
|
||||||
|
extern unsigned int NSEEL_RAM_memused;
|
||||||
|
extern int NSEEL_RAM_memused_errors;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// configuration:
|
||||||
|
|
||||||
|
// use the handwritten lexer -- the flex (eel2.l generated) lexer mostly works, but doesn't support string parsing at the moment
|
||||||
|
// this mode is faster and uses less ram than eel2.l anyway, so leave it on
|
||||||
|
#define NSEEL_SUPER_MINIMAL_LEXER
|
||||||
|
|
||||||
|
// #define NSEEL_EEL1_COMPAT_MODE // supports old behaviors (continue after failed compile), old functions _bnot etc. disables string support (strings were used as comments in eel1 etc)
|
||||||
|
|
||||||
|
#define NSEEL_MAX_VARIABLE_NAMELEN 128 // define this to override the max variable length
|
||||||
|
#define NSEEL_MAX_EELFUNC_PARAMETERS 40
|
||||||
|
#define NSEEL_MAX_FUNCSIG_NAME 2048 // longer than variable maxlen, due to multiple namespaces
|
||||||
|
|
||||||
|
// maximum loop length (0 for unlimited)
|
||||||
|
#ifndef NSEEL_LOOPFUNC_SUPPORT_MAXLEN
|
||||||
|
#define NSEEL_LOOPFUNC_SUPPORT_MAXLEN 1048576
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define NSEEL_MAX_FUNCTION_SIZE_FOR_INLINE 2048
|
||||||
|
|
||||||
|
// when a VM ctx doesn't have a GRAM context set, make the global one this big
|
||||||
|
#define NSEEL_SHARED_GRAM_SIZE (1<<20)
|
||||||
|
|
||||||
|
//#define EEL_DUMP_OPS // used for testing frontend parser/logic changes
|
||||||
|
|
||||||
|
// note: if you wish to change NSEEL_RAM_*, and your target is x86-64, you will
|
||||||
|
// need to edit asm-nseel-x64-sse.asm to match
|
||||||
|
|
||||||
|
// 512 * 65536 = 32 million entries maximum (256MB RAM)
|
||||||
|
// default is limited to 128 * 65536 = 8 million entries (64MB RAM)
|
||||||
|
|
||||||
|
// default to 8 million entries, use NSEEL_VM_setramsize() to change at runtime
|
||||||
|
#define NSEEL_RAM_BLOCKS_DEFAULTMAX 128
|
||||||
|
|
||||||
|
// 512 entry block table maximum (2k/4k per VM)
|
||||||
|
#define NSEEL_RAM_BLOCKS_LOG2 9
|
||||||
|
|
||||||
|
// 65536 items per block (512KB)
|
||||||
|
#define NSEEL_RAM_ITEMSPERBLOCK_LOG2 16
|
||||||
|
|
||||||
|
#define NSEEL_RAM_BLOCKS (1 << NSEEL_RAM_BLOCKS_LOG2)
|
||||||
|
#define NSEEL_RAM_ITEMSPERBLOCK (1<<NSEEL_RAM_ITEMSPERBLOCK_LOG2)
|
||||||
|
|
||||||
|
#define NSEEL_STACK_SIZE 4096 // about 64k overhead if the stack functions are used in a given code handle
|
||||||
|
|
||||||
|
// arch neutral mode, runs about 1/8th speed or so
|
||||||
|
//#define EEL_TARGET_PORTABLE
|
||||||
|
|
||||||
|
#ifdef EEL_TARGET_PORTABLE
|
||||||
|
#ifdef EEL_PORTABLE_TAILCALL
|
||||||
|
typedef void (*EEL_BC_TYPE)(void *next_inst, void *state);
|
||||||
|
#else
|
||||||
|
#define EEL_BC_TYPE int
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef NSEEL_EEL1_COMPAT_MODE
|
||||||
|
double *NSEEL_getglobalregs();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void eel_enterfp(int s[2]);
|
||||||
|
void eel_leavefp(int s[2]);
|
||||||
|
|
||||||
|
extern void *(*nseel_gmem_calloc)(size_t,size_t); // set this to the calloc() implementation used by the context that will call NSEEL_VM_FreeGRAM()
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif//__NS_EEL_H__
|
||||||
1
oversampling/WDL/eel2/nseel-caltab.c
Normal file
1
oversampling/WDL/eel2/nseel-caltab.c
Normal file
@@ -0,0 +1 @@
|
|||||||
|
// no longer used
|
||||||
147
oversampling/WDL/eel2/nseel-cfunc.c
Normal file
147
oversampling/WDL/eel2/nseel-cfunc.c
Normal file
@@ -0,0 +1,147 @@
|
|||||||
|
/*
|
||||||
|
Expression Evaluator Library (NS-EEL) v2
|
||||||
|
Copyright (C) 2004-2013 Cockos Incorporated
|
||||||
|
Copyright (C) 1999-2003 Nullsoft, Inc.
|
||||||
|
|
||||||
|
nseel-cfunc.c: assembly/C implementation of operator/function templates
|
||||||
|
This file should be ideally compiled with optimizations towards "minimize size"
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied
|
||||||
|
warranty. In no event will the authors be held liable for any damages
|
||||||
|
arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
|
including commercial applications, and to alter it and redistribute it
|
||||||
|
freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not
|
||||||
|
claim that you wrote the original software. If you use this software
|
||||||
|
in a product, an acknowledgment in the product documentation would be
|
||||||
|
appreciated but is not required.
|
||||||
|
2. Altered source versions must be plainly marked as such, and must not be
|
||||||
|
misrepresented as being the original software.
|
||||||
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include "ns-eel-int.h"
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// these are used by our assembly code
|
||||||
|
|
||||||
|
|
||||||
|
#define N 624
|
||||||
|
#define M 397
|
||||||
|
#define MATRIX_A 0x9908b0dfUL /* constant vector a */
|
||||||
|
#define UPPER_MASK 0x80000000UL /* most significant w-r bits */
|
||||||
|
#define LOWER_MASK 0x7fffffffUL /* least significant r bits */
|
||||||
|
|
||||||
|
static unsigned int genrand_int32(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
unsigned int y;
|
||||||
|
static unsigned int mag01[2]={0x0UL, MATRIX_A};
|
||||||
|
/* mag01[x] = x * MATRIX_A for x=0,1 */
|
||||||
|
|
||||||
|
static unsigned int mt[N]; /* the array for the state vector */
|
||||||
|
static unsigned int __idx;
|
||||||
|
|
||||||
|
unsigned int mti = __idx;
|
||||||
|
|
||||||
|
if (!mti)
|
||||||
|
{
|
||||||
|
unsigned int s=0x4141f00d;
|
||||||
|
mt[0]= s & 0xffffffffUL;
|
||||||
|
for (mti=1; mti<N; mti++)
|
||||||
|
{
|
||||||
|
mt[mti] =
|
||||||
|
(1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
|
||||||
|
/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
|
||||||
|
/* In the previous versions, MSBs of the seed affect */
|
||||||
|
/* only MSBs of the array mt[]. */
|
||||||
|
/* 2002/01/09 modified by Makoto Matsumoto */
|
||||||
|
mt[mti] &= 0xffffffffUL;
|
||||||
|
/* for >32 bit machines */
|
||||||
|
}
|
||||||
|
__idx = N; // mti = N (from loop)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mti >= N) { /* generate N words at one time */
|
||||||
|
int kk;
|
||||||
|
__idx = 1;
|
||||||
|
|
||||||
|
for (kk=0;kk<N-M;kk++) {
|
||||||
|
y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
|
||||||
|
mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
|
||||||
|
}
|
||||||
|
for (;kk<N-1;kk++) {
|
||||||
|
y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
|
||||||
|
mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
|
||||||
|
}
|
||||||
|
y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
|
||||||
|
mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
|
||||||
|
|
||||||
|
mti = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
__idx++;
|
||||||
|
|
||||||
|
y = mt[mti];
|
||||||
|
|
||||||
|
/* Tempering */
|
||||||
|
y ^= (y >> 11);
|
||||||
|
y ^= (y << 7) & 0x9d2c5680UL;
|
||||||
|
y ^= (y << 15) & 0xefc60000UL;
|
||||||
|
y ^= (y >> 18);
|
||||||
|
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------------------------------------------
|
||||||
|
EEL_F NSEEL_CGEN_CALL nseel_int_rand(EEL_F f)
|
||||||
|
{
|
||||||
|
EEL_F x=floor(f);
|
||||||
|
if (x < 1.0) x=1.0;
|
||||||
|
|
||||||
|
#ifdef NSEEL_EEL1_COMPAT_MODE
|
||||||
|
return (EEL_F)(genrand_int32()%(int)x);
|
||||||
|
#else
|
||||||
|
return (EEL_F) (genrand_int32()*(1.0/(double)0xFFFFFFFF)*x);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef EEL_TARGET_PORTABLE
|
||||||
|
|
||||||
|
#ifdef __ppc__
|
||||||
|
#include "asm-nseel-ppc-gcc.c"
|
||||||
|
#elif defined(__aarch64__)
|
||||||
|
#include "asm-nseel-aarch64-gcc.c"
|
||||||
|
#elif defined(_M_ARM64) || defined(_M_ARM64EC)
|
||||||
|
// add asm-nseel-aarch64-msvc.obj / asm-nseel-arm64ec.obj to project
|
||||||
|
#elif defined(__arm__)
|
||||||
|
#include "asm-nseel-arm-gcc.c"
|
||||||
|
#elif defined (_M_ARM) && _M_ARM == 7
|
||||||
|
// vc on ARM, tbd
|
||||||
|
#else
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#ifdef _WIN64
|
||||||
|
//nasm
|
||||||
|
#else
|
||||||
|
#include "asm-nseel-x86-msvc.c"
|
||||||
|
#endif
|
||||||
|
#elif !defined(__LP64__) && !defined(_WIN64)
|
||||||
|
#define EEL_F_SUFFIX "l"
|
||||||
|
#define FUNCTION_MARKER "\n.byte 0x89,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90\n"
|
||||||
|
#include "asm-nseel-x86-gcc.c"
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
5819
oversampling/WDL/eel2/nseel-compiler.c
Normal file
5819
oversampling/WDL/eel2/nseel-compiler.c
Normal file
File diff suppressed because it is too large
Load Diff
469
oversampling/WDL/eel2/nseel-eval.c
Normal file
469
oversampling/WDL/eel2/nseel-eval.c
Normal file
@@ -0,0 +1,469 @@
|
|||||||
|
/*
|
||||||
|
Expression Evaluator Library (NS-EEL) v2
|
||||||
|
Copyright (C) 2004-2013 Cockos Incorporated
|
||||||
|
Copyright (C) 1999-2003 Nullsoft, Inc.
|
||||||
|
|
||||||
|
nseel-eval.c
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied
|
||||||
|
warranty. In no event will the authors be held liable for any damages
|
||||||
|
arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
|
including commercial applications, and to alter it and redistribute it
|
||||||
|
freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not
|
||||||
|
claim that you wrote the original software. If you use this software
|
||||||
|
in a product, an acknowledgment in the product documentation would be
|
||||||
|
appreciated but is not required.
|
||||||
|
2. Altered source versions must be plainly marked as such, and must not be
|
||||||
|
misrepresented as being the original software.
|
||||||
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include "ns-eel-int.h"
|
||||||
|
#include "../wdlcstring.h"
|
||||||
|
|
||||||
|
|
||||||
|
static const char *nseel_skip_space_and_comments(const char *p, const char *endptr)
|
||||||
|
{
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
while (p < endptr && isspace((unsigned char)p[0])) p++;
|
||||||
|
if (p >= endptr-1 || *p != '/') return p;
|
||||||
|
|
||||||
|
if (p[1]=='/')
|
||||||
|
{
|
||||||
|
while (p < endptr && *p != '\r' && *p != '\n') p++;
|
||||||
|
}
|
||||||
|
else if (p[1] == '*')
|
||||||
|
{
|
||||||
|
p+=2;
|
||||||
|
while (p < endptr-1 && (p[0] != '*' || p[1] != '/')) p++;
|
||||||
|
p+=2;
|
||||||
|
if (p>=endptr) return endptr;
|
||||||
|
}
|
||||||
|
else return p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// removes any escaped characters, also will convert pairs delim_char into single delim_chars
|
||||||
|
int nseel_filter_escaped_string(char *outbuf, int outbuf_sz, const char *rdptr, size_t rdptr_size, char delim_char)
|
||||||
|
{
|
||||||
|
int outpos = 0;
|
||||||
|
const char *rdptr_end = rdptr + rdptr_size;
|
||||||
|
while (rdptr < rdptr_end && outpos < outbuf_sz-1)
|
||||||
|
{
|
||||||
|
char thisc=*rdptr;
|
||||||
|
if (thisc == '\\' && rdptr < rdptr_end-1)
|
||||||
|
{
|
||||||
|
const char nc = rdptr[1];
|
||||||
|
if (nc == 'r' || nc == 'R') { thisc = '\r'; }
|
||||||
|
else if (nc == 'n' || nc == 'N') { thisc = '\n'; }
|
||||||
|
else if (nc == 't' || nc == 'T') { thisc = '\t'; }
|
||||||
|
else if (nc == 'b' || nc == 'B') { thisc = '\b'; }
|
||||||
|
else if ((nc >= '0' && nc <= '9') || nc == 'x' || nc == 'X')
|
||||||
|
{
|
||||||
|
unsigned char c=0;
|
||||||
|
char base_shift = 3;
|
||||||
|
char num_top = '7';
|
||||||
|
|
||||||
|
rdptr++; // skip backslash
|
||||||
|
if (nc > '9') // implies xX
|
||||||
|
{
|
||||||
|
base_shift = 4;
|
||||||
|
num_top = '9';
|
||||||
|
rdptr ++; // skip x
|
||||||
|
}
|
||||||
|
|
||||||
|
while (rdptr < rdptr_end)
|
||||||
|
{
|
||||||
|
char tc=*rdptr;
|
||||||
|
if (tc >= '0' && tc <= num_top)
|
||||||
|
{
|
||||||
|
c = (c<<base_shift) + tc - '0';
|
||||||
|
}
|
||||||
|
else if (base_shift==4)
|
||||||
|
{
|
||||||
|
if (tc >= 'a' && tc <= 'f')
|
||||||
|
{
|
||||||
|
c = (c<<base_shift) + (tc - 'a' + 10);
|
||||||
|
}
|
||||||
|
else if (tc >= 'A' && tc <= 'F')
|
||||||
|
{
|
||||||
|
c = (c<<base_shift) + (tc - 'A' + 10);
|
||||||
|
}
|
||||||
|
else break;
|
||||||
|
}
|
||||||
|
else break;
|
||||||
|
|
||||||
|
rdptr++;
|
||||||
|
}
|
||||||
|
outbuf[outpos++] = (char)c;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else // \c where c is an unknown character drops the backslash -- works for \, ', ", etc
|
||||||
|
{
|
||||||
|
thisc = nc;
|
||||||
|
}
|
||||||
|
rdptr+=2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (thisc == delim_char) break;
|
||||||
|
rdptr++;
|
||||||
|
}
|
||||||
|
outbuf[outpos++] = thisc;
|
||||||
|
}
|
||||||
|
outbuf[outpos]=0;
|
||||||
|
return outpos;
|
||||||
|
}
|
||||||
|
|
||||||
|
int nseel_stringsegments_tobuf(char *bufOut, int bufout_sz, struct eelStringSegmentRec *list) // call with NULL to calculate size, or non-null to generate to buffer (returning size used)
|
||||||
|
{
|
||||||
|
int pos=0;
|
||||||
|
while (list)
|
||||||
|
{
|
||||||
|
if (!bufOut)
|
||||||
|
{
|
||||||
|
pos += list->str_len;
|
||||||
|
}
|
||||||
|
else if (list->str_len > 1)
|
||||||
|
{
|
||||||
|
if (pos >= bufout_sz) break;
|
||||||
|
pos += nseel_filter_escaped_string(bufOut + pos, bufout_sz-pos, list->str_start+1, list->str_len-1, list->str_start[0]);
|
||||||
|
}
|
||||||
|
list = list->_next;
|
||||||
|
}
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// state can be NULL, it will be set if finished with unterminated thing: 1 for multiline comment, ' or " for string
|
||||||
|
const char *nseel_simple_tokenizer(const char **ptr, const char *endptr, int *lenOut, int *state)
|
||||||
|
{
|
||||||
|
const char *p = *ptr;
|
||||||
|
const char *rv = p;
|
||||||
|
char delim;
|
||||||
|
|
||||||
|
if (state) // if state set, returns comments as tokens
|
||||||
|
{
|
||||||
|
if (*state == 1) goto in_comment;
|
||||||
|
|
||||||
|
#ifndef NSEEL_EEL1_COMPAT_MODE
|
||||||
|
if (*state == '\'' || *state == '\"')
|
||||||
|
{
|
||||||
|
delim = (char)*state;
|
||||||
|
goto in_string;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// skip any whitespace
|
||||||
|
while (p < endptr && isspace((unsigned char)p[0])) p++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// state not passed, skip comments (do not return them as tokens)
|
||||||
|
p = nseel_skip_space_and_comments(p,endptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (p >= endptr)
|
||||||
|
{
|
||||||
|
*ptr = endptr;
|
||||||
|
*lenOut = 0;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
rv=p;
|
||||||
|
|
||||||
|
if (*p == '$' && p+3 < endptr && p[1] == '\'' && p[3] == '\'')
|
||||||
|
{
|
||||||
|
p+=4;
|
||||||
|
}
|
||||||
|
else if (state && *p == '/' && p < endptr-1 && (p[1] == '/' || p[1] == '*'))
|
||||||
|
{
|
||||||
|
if (p[1] == '/')
|
||||||
|
{
|
||||||
|
while (p < endptr && *p != '\r' && *p != '\n') p++; // advance to end of line
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (state) *state=1;
|
||||||
|
p+=2;
|
||||||
|
in_comment:
|
||||||
|
while (p < endptr)
|
||||||
|
{
|
||||||
|
const char c = *p++;
|
||||||
|
if (c == '*' && p < endptr && *p == '/')
|
||||||
|
{
|
||||||
|
p++;
|
||||||
|
if (state) *state=0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (isalnum((unsigned char)*p) || *p == '_' || *p == '#' || *p == '$' || (*p == '.' && p < endptr-1 && p[1] >= '0' && p[1] <= '9'))
|
||||||
|
{
|
||||||
|
if (*p == '$' && p < endptr-1 && p[1] == '~') p++;
|
||||||
|
p++;
|
||||||
|
while (p < endptr && (isalnum((unsigned char)*p) || *p == '_' || *p == '.')) p++;
|
||||||
|
}
|
||||||
|
#ifndef NSEEL_EEL1_COMPAT_MODE
|
||||||
|
else if (*p == '\'' || *p == '\"')
|
||||||
|
{
|
||||||
|
delim = *p++;
|
||||||
|
if (state) *state=delim;
|
||||||
|
in_string:
|
||||||
|
|
||||||
|
while (p < endptr)
|
||||||
|
{
|
||||||
|
const char c = *p++;
|
||||||
|
if (p < endptr && c == '\\') p++; // skip escaped characters
|
||||||
|
else if (c == delim)
|
||||||
|
{
|
||||||
|
if (state) *state=0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
else
|
||||||
|
{
|
||||||
|
p++;
|
||||||
|
}
|
||||||
|
*ptr = p;
|
||||||
|
*lenOut = (int) (p - rv);
|
||||||
|
return p>rv ? rv : NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef NSEEL_SUPER_MINIMAL_LEXER
|
||||||
|
|
||||||
|
int nseellex(opcodeRec **output, YYLTYPE * yylloc_param, compileContext *scctx)
|
||||||
|
{
|
||||||
|
int rv=0,toklen=0;
|
||||||
|
const char *rdptr = scctx->rdbuf;
|
||||||
|
const char *endptr = scctx->rdbuf_end;
|
||||||
|
const char *tok = nseel_simple_tokenizer(&rdptr,endptr,&toklen,NULL);
|
||||||
|
*output = 0;
|
||||||
|
if (tok)
|
||||||
|
{
|
||||||
|
rv = tok[0];
|
||||||
|
if ((rv == '0' || rv == '$') && toklen > 1 && (tok[1] == 'x' || tok[1] == 'X')) // 0xf00 or $xf00
|
||||||
|
{
|
||||||
|
int x;
|
||||||
|
for (x = 2; x < toklen; x ++)
|
||||||
|
if (!((tok[x] >= '0' && tok[x] <= '9') ||
|
||||||
|
(tok[x] >= 'a' && tok[x] <= 'f') ||
|
||||||
|
(tok[x] >= 'A' && tok[x] <= 'F')))
|
||||||
|
{
|
||||||
|
tok += x;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
*output = x == toklen && toklen > 2 ? nseel_translate(scctx,tok,toklen) : NULL;
|
||||||
|
if (*output) rv=VALUE;
|
||||||
|
}
|
||||||
|
#ifndef NSEEL_EEL1_COMPAT_MODE
|
||||||
|
else if (rv == '#' && scctx->onNamedString)
|
||||||
|
{
|
||||||
|
*output = nseel_translate(scctx,tok,toklen);
|
||||||
|
if (*output) rv=STRING_IDENTIFIER;
|
||||||
|
}
|
||||||
|
else if (rv == '\'')
|
||||||
|
{
|
||||||
|
if (toklen > 1 && tok[toklen-1] == '\'')
|
||||||
|
{
|
||||||
|
*output = nseel_translate(scctx, tok, toklen);
|
||||||
|
if (*output) rv = VALUE;
|
||||||
|
}
|
||||||
|
else scctx->gotEndOfInput|=8;
|
||||||
|
}
|
||||||
|
else if (rv == '\"' && scctx->onString)
|
||||||
|
{
|
||||||
|
if (toklen > 1 && tok[toklen-1] == '\"')
|
||||||
|
{
|
||||||
|
*output = (opcodeRec *)nseel_createStringSegmentRec(scctx,tok,toklen);
|
||||||
|
if (*output) rv = STRING_LITERAL;
|
||||||
|
}
|
||||||
|
else scctx->gotEndOfInput|=16;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
else if (isalpha((unsigned char)rv) || rv == '_')
|
||||||
|
{
|
||||||
|
char buf[NSEEL_MAX_VARIABLE_NAMELEN*2];
|
||||||
|
if (toklen > sizeof(buf) - 1) toklen=sizeof(buf) - 1;
|
||||||
|
memcpy(buf,tok,toklen);
|
||||||
|
buf[toklen]=0;
|
||||||
|
*output = nseel_createCompiledValuePtr(scctx, NULL, buf);
|
||||||
|
if (*output) rv = IDENTIFIER;
|
||||||
|
}
|
||||||
|
else if ((rv >= '0' && rv <= '9') || (rv == '.' && toklen > 1 && tok[1] >= '0' && tok[1] <= '9')) // 123.45 or .45
|
||||||
|
{
|
||||||
|
int x, pcnt = 0;
|
||||||
|
for (x = 0; x < toklen; x ++)
|
||||||
|
{
|
||||||
|
if (tok[x] == '.' ? (++pcnt > 1) : (tok[x] < '0' || tok[x] > '9'))
|
||||||
|
{
|
||||||
|
tok += x;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*output = x == toklen ? nseel_translate(scctx,tok,toklen) : NULL;
|
||||||
|
if (*output) rv=VALUE;
|
||||||
|
}
|
||||||
|
else if (rv == '$' && toklen > 1)
|
||||||
|
{
|
||||||
|
if (tok[1] == '~')
|
||||||
|
{
|
||||||
|
int x;
|
||||||
|
for (x = 2; x < toklen; x ++)
|
||||||
|
if (tok[x] < '0' || tok[x] > '9')
|
||||||
|
{
|
||||||
|
tok += x;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (x<toklen || toklen == 2) toklen = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
*output = toklen > 1 ? nseel_translate(scctx,tok,toklen) : NULL;
|
||||||
|
if (*output) rv=VALUE;
|
||||||
|
}
|
||||||
|
else if (rv == '<')
|
||||||
|
{
|
||||||
|
const char nc=*rdptr;
|
||||||
|
if (nc == '<')
|
||||||
|
{
|
||||||
|
rdptr++;
|
||||||
|
rv=TOKEN_SHL;
|
||||||
|
}
|
||||||
|
else if (nc == '=')
|
||||||
|
{
|
||||||
|
rdptr++;
|
||||||
|
rv=TOKEN_LTE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (rv == '>')
|
||||||
|
{
|
||||||
|
const char nc=*rdptr;
|
||||||
|
if (nc == '>')
|
||||||
|
{
|
||||||
|
rdptr++;
|
||||||
|
rv=TOKEN_SHR;
|
||||||
|
}
|
||||||
|
else if (nc == '=')
|
||||||
|
{
|
||||||
|
rdptr++;
|
||||||
|
rv=TOKEN_GTE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (rv == '&' && *rdptr == '&')
|
||||||
|
{
|
||||||
|
rdptr++;
|
||||||
|
rv = TOKEN_LOGICAL_AND;
|
||||||
|
}
|
||||||
|
else if (rv == '|' && *rdptr == '|')
|
||||||
|
{
|
||||||
|
rdptr++;
|
||||||
|
rv = TOKEN_LOGICAL_OR;
|
||||||
|
}
|
||||||
|
else if (*rdptr == '=')
|
||||||
|
{
|
||||||
|
switch (rv)
|
||||||
|
{
|
||||||
|
case '+': rv=TOKEN_ADD_OP; rdptr++; break;
|
||||||
|
case '-': rv=TOKEN_SUB_OP; rdptr++; break;
|
||||||
|
case '%': rv=TOKEN_MOD_OP; rdptr++; break;
|
||||||
|
case '|': rv=TOKEN_OR_OP; rdptr++; break;
|
||||||
|
case '&': rv=TOKEN_AND_OP; rdptr++; break;
|
||||||
|
case '~': rv=TOKEN_XOR_OP; rdptr++; break;
|
||||||
|
case '/': rv=TOKEN_DIV_OP; rdptr++; break;
|
||||||
|
case '*': rv=TOKEN_MUL_OP; rdptr++; break;
|
||||||
|
case '^': rv=TOKEN_POW_OP; rdptr++; break;
|
||||||
|
case '!':
|
||||||
|
rdptr++;
|
||||||
|
if (rdptr < endptr && *rdptr == '=')
|
||||||
|
{
|
||||||
|
rdptr++;
|
||||||
|
rv=TOKEN_NE_EXACT;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
rv=TOKEN_NE;
|
||||||
|
break;
|
||||||
|
case '=':
|
||||||
|
rdptr++;
|
||||||
|
if (rdptr < endptr && *rdptr == '=')
|
||||||
|
{
|
||||||
|
rdptr++;
|
||||||
|
rv=TOKEN_EQ_EXACT;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
rv=TOKEN_EQ;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
scctx->rdbuf = rdptr;
|
||||||
|
yylloc_param->first_column = (int)(tok - scctx->rdbuf_start);
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void nseelerror(YYLTYPE *pos,compileContext *ctx, const char *str)
|
||||||
|
{
|
||||||
|
ctx->errVar=pos->first_column>0?pos->first_column:(int)(ctx->rdbuf_end - ctx->rdbuf_start);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
int nseel_gets(compileContext *ctx, char *buf, size_t sz)
|
||||||
|
{
|
||||||
|
int n=0;
|
||||||
|
const char *endptr = ctx->rdbuf_end;
|
||||||
|
const char *rdptr = ctx->rdbuf;
|
||||||
|
if (!rdptr) return 0;
|
||||||
|
|
||||||
|
while (n < sz && rdptr < endptr) buf[n++] = *rdptr++;
|
||||||
|
ctx->rdbuf=rdptr;
|
||||||
|
return n;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//#define EEL_TRACE_LEX
|
||||||
|
|
||||||
|
#ifdef EEL_TRACE_LEX
|
||||||
|
#define nseellex nseellex2
|
||||||
|
|
||||||
|
#endif
|
||||||
|
#include "lex.nseel.c"
|
||||||
|
|
||||||
|
#ifdef EEL_TRACE_LEX
|
||||||
|
|
||||||
|
#undef nseellex
|
||||||
|
|
||||||
|
int nseellex(YYSTYPE * yylval_param, YYLTYPE * yylloc_param , yyscan_t yyscanner)
|
||||||
|
{
|
||||||
|
int a=nseellex2(yylval_param,yylloc_param,yyscanner);
|
||||||
|
|
||||||
|
wdl_log("tok: %c (%d)\n",a,a);
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
#endif//EEL_TRACE_LEX
|
||||||
|
|
||||||
|
|
||||||
|
void nseelerror(YYLTYPE *pos,compileContext *ctx, const char *str)
|
||||||
|
{
|
||||||
|
ctx->errVar=pos->first_column>0?pos->first_column:(int)(ctx->rdbuf_end - ctx->rdbuf_start);
|
||||||
|
}
|
||||||
|
#endif // !NSEEL_SUPER_MINIMAL_LEXER
|
||||||
1
oversampling/WDL/eel2/nseel-lextab.c
Normal file
1
oversampling/WDL/eel2/nseel-lextab.c
Normal file
@@ -0,0 +1 @@
|
|||||||
|
// no longer used
|
||||||
580
oversampling/WDL/eel2/nseel-ram.c
Normal file
580
oversampling/WDL/eel2/nseel-ram.c
Normal file
@@ -0,0 +1,580 @@
|
|||||||
|
/*
|
||||||
|
Expression Evaluator Library (NS-EEL) v2
|
||||||
|
Copyright (C) 2004-2013 Cockos Incorporated
|
||||||
|
Copyright (C) 1999-2003 Nullsoft, Inc.
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied
|
||||||
|
warranty. In no event will the authors be held liable for any damages
|
||||||
|
arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
|
including commercial applications, and to alter it and redistribute it
|
||||||
|
freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not
|
||||||
|
claim that you wrote the original software. If you use this software
|
||||||
|
in a product, an acknowledgment in the product documentation would be
|
||||||
|
appreciated but is not required.
|
||||||
|
2. Altered source versions must be plainly marked as such, and must not be
|
||||||
|
misrepresented as being the original software.
|
||||||
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "ns-eel.h"
|
||||||
|
#include "ns-eel-int.h"
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <malloc.h>
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#define inline __inline
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
unsigned int NSEEL_RAM_limitmem=0;
|
||||||
|
unsigned int NSEEL_RAM_memused=0;
|
||||||
|
int NSEEL_RAM_memused_errors=0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int NSEEL_VM_wantfreeRAM(NSEEL_VMCTX ctx)
|
||||||
|
{
|
||||||
|
if (ctx)
|
||||||
|
{
|
||||||
|
compileContext *c=(compileContext*)ctx;
|
||||||
|
if (c->ram_state->needfree)
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void NSEEL_VM_freeRAMIfCodeRequested(NSEEL_VMCTX ctx) // check to see if our free flag was set
|
||||||
|
{
|
||||||
|
if (ctx)
|
||||||
|
{
|
||||||
|
compileContext *c=(compileContext*)ctx;
|
||||||
|
if (c->ram_state->needfree)
|
||||||
|
{
|
||||||
|
NSEEL_HOSTSTUB_EnterMutex();
|
||||||
|
{
|
||||||
|
INT_PTR startpos=((INT_PTR)c->ram_state->needfree)-1;
|
||||||
|
EEL_F **blocks = c->ram_state->blocks;
|
||||||
|
INT_PTR pos=0;
|
||||||
|
int x;
|
||||||
|
for (x = 0; x < NSEEL_RAM_BLOCKS; x ++)
|
||||||
|
{
|
||||||
|
if (pos >= startpos)
|
||||||
|
{
|
||||||
|
if (blocks[x])
|
||||||
|
{
|
||||||
|
if (NSEEL_RAM_memused >= sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK)
|
||||||
|
NSEEL_RAM_memused -= sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK;
|
||||||
|
else NSEEL_RAM_memused_errors++;
|
||||||
|
free(blocks[x]);
|
||||||
|
blocks[x]=0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pos+=NSEEL_RAM_ITEMSPERBLOCK;
|
||||||
|
}
|
||||||
|
c->ram_state->needfree=0;
|
||||||
|
}
|
||||||
|
NSEEL_HOSTSTUB_LeaveMutex();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
EEL_F nseel_ramalloc_onfail;
|
||||||
|
EEL_F * volatile nseel_gmembuf_default;
|
||||||
|
|
||||||
|
|
||||||
|
void *(*nseel_gmem_calloc)(size_t a, size_t b);
|
||||||
|
|
||||||
|
EEL_F * NSEEL_CGEN_CALL __NSEEL_RAMAllocGMEM(EEL_F ***blocks, unsigned int w)
|
||||||
|
{
|
||||||
|
if (blocks)
|
||||||
|
{
|
||||||
|
EEL_F **pblocks=*blocks;
|
||||||
|
|
||||||
|
if (w < NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK)
|
||||||
|
{
|
||||||
|
const unsigned int whichblock = w/NSEEL_RAM_ITEMSPERBLOCK;
|
||||||
|
EEL_F *p=NULL;
|
||||||
|
if (!pblocks || !(p=pblocks[whichblock]))
|
||||||
|
{
|
||||||
|
NSEEL_HOSTSTUB_EnterMutex();
|
||||||
|
if (!nseel_gmem_calloc) nseel_gmem_calloc=calloc;
|
||||||
|
|
||||||
|
if (!(pblocks=*blocks)) pblocks = *blocks = (EEL_F **)nseel_gmem_calloc(sizeof(EEL_F *),NSEEL_RAM_BLOCKS);
|
||||||
|
else p = pblocks[whichblock];
|
||||||
|
|
||||||
|
if (!p && pblocks)
|
||||||
|
{
|
||||||
|
p=pblocks[whichblock]=(EEL_F *)nseel_gmem_calloc(sizeof(EEL_F),NSEEL_RAM_ITEMSPERBLOCK);
|
||||||
|
}
|
||||||
|
NSEEL_HOSTSTUB_LeaveMutex();
|
||||||
|
}
|
||||||
|
if (p) return p + (w&(NSEEL_RAM_ITEMSPERBLOCK-1));
|
||||||
|
}
|
||||||
|
return &nseel_ramalloc_onfail;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!nseel_gmembuf_default)
|
||||||
|
{
|
||||||
|
NSEEL_HOSTSTUB_EnterMutex();
|
||||||
|
if (!nseel_gmembuf_default) nseel_gmembuf_default=(EEL_F*)calloc(sizeof(EEL_F),NSEEL_SHARED_GRAM_SIZE);
|
||||||
|
NSEEL_HOSTSTUB_LeaveMutex();
|
||||||
|
if (!nseel_gmembuf_default) return &nseel_ramalloc_onfail;
|
||||||
|
}
|
||||||
|
|
||||||
|
return nseel_gmembuf_default+(((unsigned int)w)&((NSEEL_SHARED_GRAM_SIZE)-1));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
EEL_F * NSEEL_CGEN_CALL __NSEEL_RAMAlloc(EEL_F **pblocks, unsigned int w)
|
||||||
|
{
|
||||||
|
// fprintf(stderr,"got request at %d, %d\n",w/NSEEL_RAM_ITEMSPERBLOCK, w&(NSEEL_RAM_ITEMSPERBLOCK-1));
|
||||||
|
if (w < NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK)
|
||||||
|
{
|
||||||
|
unsigned int whichblock = w/NSEEL_RAM_ITEMSPERBLOCK;
|
||||||
|
EEL_F *p=pblocks[whichblock];
|
||||||
|
if (!p && whichblock < ((unsigned int *)pblocks)[-3]) // pblocks -1/-2 are closefact, -3 is maxblocks
|
||||||
|
{
|
||||||
|
NSEEL_HOSTSTUB_EnterMutex();
|
||||||
|
|
||||||
|
if (!(p=pblocks[whichblock]))
|
||||||
|
{
|
||||||
|
|
||||||
|
const int msize=sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK;
|
||||||
|
if (!NSEEL_RAM_limitmem || NSEEL_RAM_memused+msize < NSEEL_RAM_limitmem)
|
||||||
|
{
|
||||||
|
p=pblocks[whichblock]=(EEL_F *)calloc(sizeof(EEL_F),NSEEL_RAM_ITEMSPERBLOCK);
|
||||||
|
if (p) NSEEL_RAM_memused+=msize;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
NSEEL_HOSTSTUB_LeaveMutex();
|
||||||
|
}
|
||||||
|
if (p) return p + (w&(NSEEL_RAM_ITEMSPERBLOCK-1));
|
||||||
|
}
|
||||||
|
// fprintf(stderr,"ret 0\n");
|
||||||
|
return &nseel_ramalloc_onfail;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
EEL_F * NSEEL_CGEN_CALL __NSEEL_RAM_MemFree(void *blocks, EEL_F *which)
|
||||||
|
{
|
||||||
|
// blocks points to ram_state->blocks, so back it up past closefact and maxblocks to needfree
|
||||||
|
int *flag = (int *)((char *)blocks - sizeof(double) - 2*sizeof(int));
|
||||||
|
int d=(int)(*which);
|
||||||
|
if (d < 0) d=0;
|
||||||
|
if (d < flag[1]*NSEEL_RAM_ITEMSPERBLOCK) flag[0]=1+d;
|
||||||
|
return which;
|
||||||
|
}
|
||||||
|
|
||||||
|
EEL_F * NSEEL_CGEN_CALL __NSEEL_RAM_MemTop(void *blocks, EEL_F *which)
|
||||||
|
{
|
||||||
|
// blocks points to ram_state->blocks, so back it up past closefact to maxblocks
|
||||||
|
const int *flag = (int *)((char *)blocks - sizeof(double) - sizeof(int));
|
||||||
|
*which = flag[0]*NSEEL_RAM_ITEMSPERBLOCK;
|
||||||
|
return which;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
EEL_F NSEEL_CGEN_CALL __NSEEL_RAM_MemInsertShuffle(EEL_F **blocks,EEL_F *buf, EEL_F *lenptr, EEL_F *value)
|
||||||
|
{
|
||||||
|
int src_offs = (int)*buf;
|
||||||
|
int len = (int)*lenptr;
|
||||||
|
int copy_len;
|
||||||
|
|
||||||
|
EEL_F ret = *value;
|
||||||
|
|
||||||
|
unsigned int sbidx = (unsigned int)src_offs / NSEEL_RAM_ITEMSPERBLOCK;
|
||||||
|
|
||||||
|
if (len < 1 || src_offs < 0) return 0.0;
|
||||||
|
|
||||||
|
src_offs = src_offs&(NSEEL_RAM_ITEMSPERBLOCK-1);
|
||||||
|
copy_len = wdl_min(len,NSEEL_RAM_ITEMSPERBLOCK - src_offs);
|
||||||
|
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
EEL_F *srcptr;
|
||||||
|
if (sbidx >= NSEEL_RAM_BLOCKS) break;
|
||||||
|
|
||||||
|
srcptr = blocks[sbidx];
|
||||||
|
|
||||||
|
if (WDL_unlikely(!srcptr))
|
||||||
|
{
|
||||||
|
srcptr = __NSEEL_RAMAlloc(blocks,sbidx * NSEEL_RAM_ITEMSPERBLOCK);
|
||||||
|
if (srcptr==&nseel_ramalloc_onfail) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
len-=copy_len;
|
||||||
|
srcptr += src_offs;
|
||||||
|
while (copy_len-- > 0)
|
||||||
|
{
|
||||||
|
EEL_F v = *srcptr;
|
||||||
|
*srcptr++ = ret;
|
||||||
|
ret = v;
|
||||||
|
}
|
||||||
|
if (!len) break;
|
||||||
|
|
||||||
|
sbidx++;
|
||||||
|
src_offs=0;
|
||||||
|
copy_len = wdl_min(len,NSEEL_RAM_ITEMSPERBLOCK);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
EEL_F NSEEL_CGEN_CALL __NSEEL_RAM_MemSumProducts(EEL_F **blocks,EEL_F *dest, EEL_F *src, EEL_F *lenptr)
|
||||||
|
{
|
||||||
|
int src_offs = (int)*src;
|
||||||
|
int len = (int)*lenptr;
|
||||||
|
|
||||||
|
EEL_F sum = 0.0;
|
||||||
|
|
||||||
|
if (len < 1 || src_offs < 0) return 0.0;
|
||||||
|
|
||||||
|
if (*dest < 0.0)
|
||||||
|
{
|
||||||
|
int copy_len;
|
||||||
|
unsigned int sbidx = (unsigned int)src_offs / NSEEL_RAM_ITEMSPERBLOCK;
|
||||||
|
src_offs = src_offs&(NSEEL_RAM_ITEMSPERBLOCK-1);
|
||||||
|
copy_len = wdl_min(len,NSEEL_RAM_ITEMSPERBLOCK - src_offs);
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
const EEL_F *srcptr;
|
||||||
|
if (sbidx >= NSEEL_RAM_BLOCKS) break;
|
||||||
|
|
||||||
|
srcptr = blocks[sbidx];
|
||||||
|
|
||||||
|
if (WDL_likely(srcptr))
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
srcptr += src_offs;
|
||||||
|
if (*dest == -1.0)
|
||||||
|
for (i = 0; i < copy_len; i ++) sum += srcptr[i] * srcptr[i];
|
||||||
|
else if (*dest == -2.0)
|
||||||
|
for (i = 0; i < copy_len; i ++) sum += fabs(srcptr[i]);
|
||||||
|
else
|
||||||
|
for (i = 0; i < copy_len; i ++) sum += srcptr[i];
|
||||||
|
}
|
||||||
|
len-=copy_len;
|
||||||
|
if (!len) break;
|
||||||
|
|
||||||
|
sbidx++;
|
||||||
|
src_offs=0;
|
||||||
|
copy_len = wdl_min(len,NSEEL_RAM_ITEMSPERBLOCK);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
unsigned int dest_offs = (unsigned int) (int)*dest;
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
unsigned int sbidx = (unsigned int)src_offs / NSEEL_RAM_ITEMSPERBLOCK;
|
||||||
|
const int sbo = (src_offs&(NSEEL_RAM_ITEMSPERBLOCK-1));
|
||||||
|
unsigned int dbidx = dest_offs / NSEEL_RAM_ITEMSPERBLOCK;
|
||||||
|
const int dbo = (dest_offs&(NSEEL_RAM_ITEMSPERBLOCK-1));
|
||||||
|
|
||||||
|
const int copy_len = wdl_min(len,NSEEL_RAM_ITEMSPERBLOCK - wdl_max(dbo,sbo));
|
||||||
|
|
||||||
|
const EEL_F *srcptr, *destptr;
|
||||||
|
|
||||||
|
if (sbidx >= NSEEL_RAM_BLOCKS || dbidx >= NSEEL_RAM_BLOCKS) break;
|
||||||
|
srcptr = blocks[sbidx];
|
||||||
|
destptr = blocks[dbidx];
|
||||||
|
if (WDL_likely(srcptr && destptr))
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
srcptr += sbo;
|
||||||
|
destptr += dbo;
|
||||||
|
for (i = 0; i < copy_len; i ++) sum += destptr[i] * srcptr[i];
|
||||||
|
}
|
||||||
|
len-=copy_len;
|
||||||
|
if (!len) break;
|
||||||
|
|
||||||
|
src_offs += copy_len;
|
||||||
|
dest_offs += copy_len;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
EEL_F * NSEEL_CGEN_CALL __NSEEL_RAM_MemCpy(EEL_F **blocks,EEL_F *dest, EEL_F *src, EEL_F *lenptr)
|
||||||
|
{
|
||||||
|
const int mem_size=NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK;
|
||||||
|
int dest_offs = (int)(*dest + 0.0001);
|
||||||
|
int src_offs = (int)(*src + 0.0001);
|
||||||
|
int len = (int)(*lenptr + 0.0001);
|
||||||
|
int want_mmove=0;
|
||||||
|
|
||||||
|
// trim to front
|
||||||
|
if (src_offs<0)
|
||||||
|
{
|
||||||
|
len += src_offs;
|
||||||
|
dest_offs -= src_offs;
|
||||||
|
src_offs=0;
|
||||||
|
}
|
||||||
|
if (dest_offs<0)
|
||||||
|
{
|
||||||
|
len += dest_offs;
|
||||||
|
src_offs -= dest_offs;
|
||||||
|
dest_offs=0;
|
||||||
|
}
|
||||||
|
if (src_offs + len > mem_size) len = mem_size-src_offs;
|
||||||
|
if (dest_offs + len > mem_size) len = mem_size-dest_offs;
|
||||||
|
|
||||||
|
if (src_offs == dest_offs || len < 1) return dest;
|
||||||
|
|
||||||
|
if (src_offs < dest_offs && src_offs+len > dest_offs)
|
||||||
|
{
|
||||||
|
// if src_offs < dest_offs and overlapping, must copy right to left
|
||||||
|
if ((dest_offs - src_offs) < NSEEL_RAM_ITEMSPERBLOCK) want_mmove = 1;
|
||||||
|
src_offs += len;
|
||||||
|
dest_offs += len;
|
||||||
|
while (len > 0)
|
||||||
|
{
|
||||||
|
const int maxdlen=((dest_offs-1)&(NSEEL_RAM_ITEMSPERBLOCK-1)) + 1;
|
||||||
|
const int maxslen=((src_offs-1)&(NSEEL_RAM_ITEMSPERBLOCK-1)) + 1;
|
||||||
|
int copy_len = len;
|
||||||
|
EEL_F *srcptr,*destptr;
|
||||||
|
|
||||||
|
if (copy_len > maxdlen) copy_len=maxdlen;
|
||||||
|
if (copy_len > maxslen) copy_len=maxslen;
|
||||||
|
|
||||||
|
srcptr = __NSEEL_RAMAlloc(blocks,src_offs - copy_len);
|
||||||
|
destptr = __NSEEL_RAMAlloc(blocks,dest_offs - copy_len);
|
||||||
|
if (srcptr==&nseel_ramalloc_onfail || destptr==&nseel_ramalloc_onfail) break;
|
||||||
|
|
||||||
|
if (want_mmove) memmove(destptr,srcptr,sizeof(EEL_F)*copy_len);
|
||||||
|
else memcpy(destptr,srcptr,sizeof(EEL_F)*copy_len);
|
||||||
|
src_offs-=copy_len;
|
||||||
|
dest_offs-=copy_len;
|
||||||
|
len-=copy_len;
|
||||||
|
}
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dest_offs < src_offs && dest_offs+len > src_offs)
|
||||||
|
{
|
||||||
|
// if dest_offs < src_offs and overlapping, and less than NSEEL_RAM_ITEMSPERBLOCK apart, use memmove()
|
||||||
|
if ((src_offs-dest_offs) < NSEEL_RAM_ITEMSPERBLOCK) want_mmove = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (len > 0)
|
||||||
|
{
|
||||||
|
const int maxdlen=NSEEL_RAM_ITEMSPERBLOCK - (dest_offs&(NSEEL_RAM_ITEMSPERBLOCK-1));
|
||||||
|
const int maxslen=NSEEL_RAM_ITEMSPERBLOCK - (src_offs&(NSEEL_RAM_ITEMSPERBLOCK-1));
|
||||||
|
int copy_len = len;
|
||||||
|
EEL_F *srcptr,*destptr;
|
||||||
|
|
||||||
|
if (copy_len > maxdlen) copy_len=maxdlen;
|
||||||
|
if (copy_len > maxslen) copy_len=maxslen;
|
||||||
|
|
||||||
|
srcptr = __NSEEL_RAMAlloc(blocks,src_offs);
|
||||||
|
destptr = __NSEEL_RAMAlloc(blocks,dest_offs);
|
||||||
|
if (srcptr==&nseel_ramalloc_onfail || destptr==&nseel_ramalloc_onfail) break;
|
||||||
|
|
||||||
|
if (want_mmove) memmove(destptr,srcptr,sizeof(EEL_F)*copy_len);
|
||||||
|
else memcpy(destptr,srcptr,sizeof(EEL_F)*copy_len);
|
||||||
|
src_offs+=copy_len;
|
||||||
|
dest_offs+=copy_len;
|
||||||
|
len-=copy_len;
|
||||||
|
}
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
EEL_F * NSEEL_CGEN_CALL __NSEEL_RAM_MemSet(EEL_F **blocks,EEL_F *dest, EEL_F *v, EEL_F *lenptr)
|
||||||
|
{
|
||||||
|
int offs = (int)(*dest + 0.0001);
|
||||||
|
int len = (int)(*lenptr + 0.0001);
|
||||||
|
EEL_F t;
|
||||||
|
if (offs<0)
|
||||||
|
{
|
||||||
|
len += offs;
|
||||||
|
offs=0;
|
||||||
|
}
|
||||||
|
if (offs >= NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK) return dest;
|
||||||
|
|
||||||
|
if (offs+len > NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK) len = NSEEL_RAM_BLOCKS*NSEEL_RAM_ITEMSPERBLOCK - offs;
|
||||||
|
|
||||||
|
if (len < 1) return dest;
|
||||||
|
|
||||||
|
|
||||||
|
t=*v; // set value
|
||||||
|
|
||||||
|
// int lastBlock=-1;
|
||||||
|
while (len > 0)
|
||||||
|
{
|
||||||
|
int lcnt;
|
||||||
|
EEL_F *ptr=__NSEEL_RAMAlloc(blocks,offs);
|
||||||
|
if (ptr==&nseel_ramalloc_onfail) break;
|
||||||
|
|
||||||
|
lcnt=NSEEL_RAM_ITEMSPERBLOCK-(offs&(NSEEL_RAM_ITEMSPERBLOCK-1));
|
||||||
|
if (lcnt > len) lcnt=len;
|
||||||
|
|
||||||
|
len -= lcnt;
|
||||||
|
offs += lcnt;
|
||||||
|
|
||||||
|
while (lcnt--)
|
||||||
|
{
|
||||||
|
*ptr++=t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static inline int __getset_values(EEL_F **blocks, int isset, int len, EEL_F **parms)
|
||||||
|
{
|
||||||
|
int offs, lout=0;
|
||||||
|
unsigned int pageidx, sub_offs;
|
||||||
|
if (--len < 1) return 0;
|
||||||
|
offs = (int)(parms++[0][0] + 0.0001);
|
||||||
|
|
||||||
|
if (offs<=0)
|
||||||
|
{
|
||||||
|
len += offs;
|
||||||
|
parms -= offs;
|
||||||
|
offs=0;
|
||||||
|
pageidx=sub_offs=0;
|
||||||
|
if (len<1) return 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sub_offs = ((unsigned int)offs) & (NSEEL_RAM_ITEMSPERBLOCK-1);
|
||||||
|
pageidx = ((unsigned int)offs)>>NSEEL_RAM_ITEMSPERBLOCK_LOG2;
|
||||||
|
if (pageidx>=NSEEL_RAM_BLOCKS) return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
int lcnt=NSEEL_RAM_ITEMSPERBLOCK-sub_offs;
|
||||||
|
EEL_F *ptr=blocks[pageidx];
|
||||||
|
if (!ptr)
|
||||||
|
{
|
||||||
|
ptr = __NSEEL_RAMAlloc(blocks,offs + lout);
|
||||||
|
if (ptr==&nseel_ramalloc_onfail) return lout;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ptr += sub_offs;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lcnt >= len)
|
||||||
|
{
|
||||||
|
// this page satisfies the request (normal behavior)
|
||||||
|
lout += len;
|
||||||
|
if (isset) while (len--) *ptr++=parms++[0][0];
|
||||||
|
else while (len--) parms++[0][0] = *ptr++;
|
||||||
|
return lout;
|
||||||
|
}
|
||||||
|
|
||||||
|
// crossing a page boundary
|
||||||
|
len -= lcnt;
|
||||||
|
lout += lcnt;
|
||||||
|
if (isset) while (lcnt--) *ptr++=parms++[0][0];
|
||||||
|
else while (lcnt--) parms++[0][0] = *ptr++;
|
||||||
|
|
||||||
|
if (len <= 0 || ++pageidx >= NSEEL_RAM_BLOCKS) return lout;
|
||||||
|
sub_offs=0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
EEL_F NSEEL_CGEN_CALL __NSEEL_RAM_Mem_SetValues(EEL_F **blocks, INT_PTR np, EEL_F **parms)
|
||||||
|
{
|
||||||
|
return __getset_values(blocks,1,(int)np,parms);
|
||||||
|
}
|
||||||
|
|
||||||
|
EEL_F NSEEL_CGEN_CALL __NSEEL_RAM_Mem_GetValues(EEL_F **blocks, INT_PTR np, EEL_F **parms)
|
||||||
|
{
|
||||||
|
return __getset_values(blocks,0,(int)np,parms);
|
||||||
|
}
|
||||||
|
|
||||||
|
void NSEEL_VM_SetGRAM(NSEEL_VMCTX ctx, void **gram)
|
||||||
|
{
|
||||||
|
if (ctx)
|
||||||
|
{
|
||||||
|
compileContext *c=(compileContext*)ctx;
|
||||||
|
c->gram_blocks = gram;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void NSEEL_VM_freeRAM(NSEEL_VMCTX ctx)
|
||||||
|
{
|
||||||
|
if (ctx)
|
||||||
|
{
|
||||||
|
int x;
|
||||||
|
compileContext *c=(compileContext*)ctx;
|
||||||
|
EEL_F **blocks = c->ram_state->blocks;
|
||||||
|
for (x = 0; x < NSEEL_RAM_BLOCKS; x ++)
|
||||||
|
{
|
||||||
|
if (blocks[x])
|
||||||
|
{
|
||||||
|
if (NSEEL_RAM_memused >= sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK)
|
||||||
|
NSEEL_RAM_memused -= sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK;
|
||||||
|
else NSEEL_RAM_memused_errors++;
|
||||||
|
free(blocks[x]);
|
||||||
|
blocks[x]=0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c->ram_state->needfree=0; // no need to free anymore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void NSEEL_VM_FreeGRAM(void **ufd)
|
||||||
|
{
|
||||||
|
if (ufd[0])
|
||||||
|
{
|
||||||
|
EEL_F **blocks = (EEL_F **)ufd[0];
|
||||||
|
int x;
|
||||||
|
for (x = 0; x < NSEEL_RAM_BLOCKS; x ++)
|
||||||
|
{
|
||||||
|
if (blocks[x])
|
||||||
|
{
|
||||||
|
if (NSEEL_RAM_memused >= sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK)
|
||||||
|
NSEEL_RAM_memused -= sizeof(EEL_F) * NSEEL_RAM_ITEMSPERBLOCK;
|
||||||
|
else NSEEL_RAM_memused_errors++;
|
||||||
|
}
|
||||||
|
free(blocks[x]);
|
||||||
|
blocks[x]=0;
|
||||||
|
}
|
||||||
|
free(blocks);
|
||||||
|
ufd[0]=0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
EEL_F *NSEEL_VM_getramptr(NSEEL_VMCTX ctx, unsigned int offs, int *validCount)
|
||||||
|
{
|
||||||
|
EEL_F *d=__NSEEL_RAMAlloc(ctx ? ((compileContext*)ctx)->ram_state->blocks : 0,offs);
|
||||||
|
if (!d || d == &nseel_ramalloc_onfail) return NULL;
|
||||||
|
if (validCount) *validCount = NSEEL_RAM_ITEMSPERBLOCK - (offs%NSEEL_RAM_ITEMSPERBLOCK);
|
||||||
|
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
|
||||||
|
EEL_F *NSEEL_VM_getramptr_noalloc(NSEEL_VMCTX ctx, unsigned int offs, int *validCount)
|
||||||
|
{
|
||||||
|
EEL_F *d;
|
||||||
|
compileContext *cc = (compileContext *)ctx;
|
||||||
|
|
||||||
|
if (!cc ||
|
||||||
|
offs >= NSEEL_RAM_ITEMSPERBLOCK*NSEEL_RAM_BLOCKS ||
|
||||||
|
NULL == (d = cc->ram_state->blocks[offs/NSEEL_RAM_ITEMSPERBLOCK])
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (validCount) *validCount = 0;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
offs %= NSEEL_RAM_ITEMSPERBLOCK;
|
||||||
|
if (validCount) *validCount = NSEEL_RAM_ITEMSPERBLOCK - offs;
|
||||||
|
return d + offs;
|
||||||
|
}
|
||||||
43
oversampling/WDL/eel2/nseel-yylex.c
Normal file
43
oversampling/WDL/eel2/nseel-yylex.c
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
Expression Evaluator Library (NS-EEL)
|
||||||
|
Copyright (C) 2004-2013 Cockos Incorporated
|
||||||
|
Copyright (C) 1999-2003 Nullsoft, Inc.
|
||||||
|
|
||||||
|
nseel-yylex.c
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied
|
||||||
|
warranty. In no event will the authors be held liable for any damages
|
||||||
|
arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
|
including commercial applications, and to alter it and redistribute it
|
||||||
|
freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not
|
||||||
|
claim that you wrote the original software. If you use this software
|
||||||
|
in a product, an acknowledgment in the product documentation would be
|
||||||
|
appreciated but is not required.
|
||||||
|
2. Altered source versions must be plainly marked as such, and must not be
|
||||||
|
misrepresented as being the original software.
|
||||||
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include "ns-eel-int.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# define YYMALLOC malloc
|
||||||
|
# define YYFREE free
|
||||||
|
|
||||||
|
int nseellex(void * yylval_param,void * yylloc_param ,void *yyscanner);
|
||||||
|
void nseelerror(void *pos,compileContext *ctx, const char *str);
|
||||||
|
|
||||||
|
// inhibit a warning:
|
||||||
|
static void WDL_STATICFUNC_UNUSED yydestruct(const char *yymsg, int yytype, YYSTYPE *yyvaluep, YYLTYPE *yylocationp, compileContext* context);
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "y.tab.c"
|
||||||
|
|
||||||
8
oversampling/WDL/eel2/regenerate-x64-objects.sh
Normal file
8
oversampling/WDL/eel2/regenerate-x64-objects.sh
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# regenerates x86_64 objects
|
||||||
|
rm -f asm-nseel-x64-macho.o asm-nseel-x64.obj foo.o
|
||||||
|
nasm -f win64 asm-nseel-x64-sse.asm -o asm-nseel-x64.obj || echo "error assembling win64 object"
|
||||||
|
nasm -D AMD64ABI -f macho64 --prefix _ asm-nseel-x64-sse.asm -o asm-nseel-x64-macho.o || echo "error assembling macOS x86_64"
|
||||||
|
echo > foo.c
|
||||||
|
clang -arch arm64 -c -o foo.o foo.c || echo "error compiling arm64 stub"
|
||||||
|
lipo foo.o asm-nseel-x64-macho.o -create -output asm-nseel-multi-macho.o || echo "error making asm-nseel-multi-macho.o"
|
||||||
|
rm -f -- foo.c foo.o
|
||||||
50
oversampling/WDL/eel2/scripts/circle.eel
Normal file
50
oversampling/WDL/eel2/scripts/circle.eel
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
gfx_init("vis",1024,768);
|
||||||
|
|
||||||
|
zp=-0.84;
|
||||||
|
fill=0;
|
||||||
|
radj=1;
|
||||||
|
while ((c=gfx_getchar())!=27 && c >= 0)
|
||||||
|
(
|
||||||
|
c == 'f' ? fill=!fill;
|
||||||
|
c == 'j' ? jitter=!jitter;
|
||||||
|
c == 'm' ? radj = !radj;
|
||||||
|
|
||||||
|
gfx_r=gfx_g=gfx_b=1;
|
||||||
|
gfx_a=1;
|
||||||
|
gfx_x=gfx_y=0;
|
||||||
|
gfx_printf("[f]ill=%s, [j]itter=%s, [m]ove=%s [%f] %d,%d",fill?"on":"off",jitter?"on":"off",radj?"on":"off",zp,mouse_x,mouse_y);
|
||||||
|
gfx_a=0.25;
|
||||||
|
|
||||||
|
|
||||||
|
radj ? zp+=0.03;
|
||||||
|
gfx_getchar('up') ? zp+=0.03;
|
||||||
|
gfx_getchar('down') ? zp-=0.03;
|
||||||
|
zp2+=0.1;
|
||||||
|
rd = (1+sin(zp*1.3))*(gfx_w/8-16) + 3;
|
||||||
|
jitter ? (
|
||||||
|
xoffs=0.5+sin(zp2*6.7)*0.5;
|
||||||
|
yoffs=0.5+sin(zp2*7.7)*0.5;
|
||||||
|
rd|=0;
|
||||||
|
rd += 0.5+sin(zp2*3.1)*0.5
|
||||||
|
) : ( xoffs=yoffs=0; rd|= 0;);
|
||||||
|
|
||||||
|
|
||||||
|
gfx_circle(xoffs+(gfx_w/4)|0,yoffs+(gfx_h/2)|0,rd, fill,0);
|
||||||
|
gfx_circle(xoffs+(gfx_w*3/4)|0,yoffs+(gfx_h/2)|0,rd, fill,1);
|
||||||
|
|
||||||
|
gfx_mode=4+(1<<4); // filtering off, additive
|
||||||
|
gfx_a=1;
|
||||||
|
zsz=20;
|
||||||
|
outsz=gfx_w/4;
|
||||||
|
gfx_blit(-1,0,0,
|
||||||
|
gfx_w/4-zsz, gfx_h/2-zsz, zsz*2,zsz*2,
|
||||||
|
0,gfx_h-outsz,outsz,outsz);
|
||||||
|
|
||||||
|
gfx_blit(-1,0,0,
|
||||||
|
gfx_w*3/4-zsz, gfx_h/2-zsz, zsz*2,zsz*2,
|
||||||
|
gfx_w-outsz,gfx_h-outsz,outsz,outsz);
|
||||||
|
gfx_mode=0;
|
||||||
|
|
||||||
|
gfx_update();
|
||||||
|
sleep(30);
|
||||||
|
);
|
||||||
20
oversampling/WDL/eel2/scripts/eval_test.eel
Normal file
20
oversampling/WDL/eel2/scripts/eval_test.eel
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
argc < 2 ? (
|
||||||
|
printf("Usage: %s [script]\n",argv[0]);
|
||||||
|
|
||||||
|
) : (
|
||||||
|
printf("loading '%s'\n",argv[1]);
|
||||||
|
x = fopen(argv[1],"r");
|
||||||
|
!x ? (
|
||||||
|
printf("Error opening '%s'\n",argv[1])
|
||||||
|
) : (
|
||||||
|
#str="";
|
||||||
|
while (fgets(x,#line)) ( #str += #line; );
|
||||||
|
fclose(x);
|
||||||
|
loop(30,
|
||||||
|
start_t = time_precise();
|
||||||
|
eval(#str) || printf("error evaluating script\n");
|
||||||
|
start_t = time_precise()-start_t;
|
||||||
|
printf("finished in %f milliseconds\n",start_t*1000.0);
|
||||||
|
);
|
||||||
|
);
|
||||||
|
);
|
||||||
24
oversampling/WDL/eel2/scripts/gfx_test.eel
Normal file
24
oversampling/WDL/eel2/scripts/gfx_test.eel
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
gfx_init("vis",1024,768);
|
||||||
|
|
||||||
|
gfx_clear=-1;
|
||||||
|
while ((c=gfx_getchar())!=27 && c >= 0)
|
||||||
|
(
|
||||||
|
c == ' ' ? (mode += 1) >= 2 ? mode=0;
|
||||||
|
t=time_precise();
|
||||||
|
zsc = 0.01*cos(t*0.73);
|
||||||
|
// gfx_x=gfx_y=0; gfx_blurto(gfx_w,gfx_h);
|
||||||
|
gfx_blit(-1,0,0.3*(sin(t*0.3)^2),gfx_w*zsc,gfx_h*zsc, gfx_w*(1-2*zsc),gfx_h*(1-2*zsc), 0,0,gfx_w,gfx_h);
|
||||||
|
gfx_r=(cos(t)+1.0)*0.5;
|
||||||
|
gfx_g=(cos(t*1.74)+1.0)*0.5;
|
||||||
|
gfx_b=(cos(t*1.2+0.56)+1.0)*0.5;
|
||||||
|
gfx_a=0.15;
|
||||||
|
sz=gfx_w*0.03;
|
||||||
|
loop(20,
|
||||||
|
mode == 1 ?
|
||||||
|
gfx_circle(rand(gfx_w-sz),rand(gfx_h-sz),sz,1) :
|
||||||
|
gfx_rect(rand(gfx_w-sz),rand(gfx_h-sz),sz,sz);
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
gfx_update();
|
||||||
|
);
|
||||||
29
oversampling/WDL/eel2/scripts/gfx_test_defer.eel
Normal file
29
oversampling/WDL/eel2/scripts/gfx_test_defer.eel
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
gfx_init("vis",1024,768);
|
||||||
|
|
||||||
|
gfx_clear=-1;
|
||||||
|
|
||||||
|
function frame()
|
||||||
|
(
|
||||||
|
c=gfx_getchar();
|
||||||
|
c == ' ' ? (mode += 1) >= 2 ? mode=0;
|
||||||
|
t=time_precise();
|
||||||
|
zsc = 0.01*cos(t*0.73);
|
||||||
|
// gfx_x=gfx_y=0; gfx_blurto(gfx_w,gfx_h);
|
||||||
|
gfx_blit(-1,0,0.3*(sin(t*0.3)^2),gfx_w*zsc,gfx_h*zsc, gfx_w*(1-2*zsc),gfx_h*(1-2*zsc), 0,0,gfx_w,gfx_h);
|
||||||
|
gfx_r=(cos(t)+1.0)*0.5;
|
||||||
|
gfx_g=(cos(t*1.74)+1.0)*0.5;
|
||||||
|
gfx_b=(cos(t*1.2+0.56)+1.0)*0.5;
|
||||||
|
gfx_a=0.15;
|
||||||
|
sz=gfx_w*0.03;
|
||||||
|
loop(20,
|
||||||
|
mode == 1 ?
|
||||||
|
gfx_circle(rand(gfx_w-sz),rand(gfx_h-sz),sz,1) :
|
||||||
|
gfx_rect(rand(gfx_w-sz),rand(gfx_h-sz),sz,sz);
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
gfx_update();
|
||||||
|
c>=0 && c != 27 ? defer("frame()");
|
||||||
|
);
|
||||||
|
|
||||||
|
frame();
|
||||||
208
oversampling/WDL/eel2/scripts/gpx_edit.eel
Normal file
208
oversampling/WDL/eel2/scripts/gpx_edit.eel
Normal file
@@ -0,0 +1,208 @@
|
|||||||
|
// does not work for all .gpx files
|
||||||
|
// writes to output on every mouseup after edit
|
||||||
|
// move points with mouse, alt+click to delete
|
||||||
|
// click/drag to pan
|
||||||
|
// mousewheel to zoom
|
||||||
|
|
||||||
|
tab = 10000;
|
||||||
|
tabsz = 0;
|
||||||
|
|
||||||
|
is_dirty = 0;
|
||||||
|
circle_size = 5;
|
||||||
|
|
||||||
|
function scale(v) global() ( (v - this.min) / (this.max-this.min) );
|
||||||
|
function unscale(v) global() ( v * (this.max-this.min) + this.min);
|
||||||
|
function zoom(sc) global() local(c h) (
|
||||||
|
h = (this.max - this.min) * sc;
|
||||||
|
c = (this.max + this.min) * .5;
|
||||||
|
this.max = c + h*.5;
|
||||||
|
this.min = c - h*.5;
|
||||||
|
);
|
||||||
|
function include(v) global() ( this.min = min(v,this.min); this.max = max(v,this.max); );
|
||||||
|
|
||||||
|
function scroll(amt) global() ( amt *= (this.max-this.min); this.max += amt; this.min += amt; );
|
||||||
|
function zoom_view(sc) ( v_lon.zoom(sc); v_lat.zoom(sc); );
|
||||||
|
function scroll_view(dx, dy) ( v_lat.scroll(-dy/gfx_h); v_lon.scroll(-dx/gfx_w); );
|
||||||
|
|
||||||
|
function hit_test(x,y,p) global(tab tabsz circle_size gfx_w gfx_h v_lat.scale v_lon.scale) local(p hit) (
|
||||||
|
hit = -1;
|
||||||
|
while (p < tabsz && hit < 0) (
|
||||||
|
sqr(v_lat.scale(tab[p*2+1])*gfx_h-y)+sqr(v_lon.scale(tab[p*2])*gfx_w-x) < circle_size*circle_size ? hit = p;
|
||||||
|
p += 1;
|
||||||
|
);
|
||||||
|
hit
|
||||||
|
);
|
||||||
|
|
||||||
|
function linearize(p,sz, slon, slat, elon, elat, rev) local(x) global()
|
||||||
|
(
|
||||||
|
rev ? ( x=slon; slon=elon; elon=x; x=slat; slat=elat; elat=x; );
|
||||||
|
elon = (elon - slon) / sz;
|
||||||
|
elat = (elat - slat) / sz;
|
||||||
|
loop(sz,
|
||||||
|
p[0] = slon;
|
||||||
|
p[1] = slat;
|
||||||
|
slon += elon;
|
||||||
|
slat += elat;
|
||||||
|
p += 2;
|
||||||
|
);
|
||||||
|
);
|
||||||
|
|
||||||
|
function make_lowpass(f filtsize filtpos) global() local(windowpos sincpos x)
|
||||||
|
(
|
||||||
|
x = 0;
|
||||||
|
loop(filtsize,
|
||||||
|
x == filtsize/2 ? (
|
||||||
|
f[x] = 1.0;
|
||||||
|
) : (
|
||||||
|
windowpos = 2 * x * $pi / filtsize;
|
||||||
|
sincpos = filtpos * $pi * (x - filtsize/2);
|
||||||
|
|
||||||
|
// blackman-harris * window
|
||||||
|
f[x] = (0.35875 - 0.48829 * cos(windowpos) + 0.14128 * cos(2*windowpos) - 0.01168 * cos(3*windowpos)) * sin(sincpos) / sincpos;
|
||||||
|
);
|
||||||
|
x+=1;
|
||||||
|
);
|
||||||
|
);
|
||||||
|
|
||||||
|
function lowpass() local(src sz i j lpf_pos v sx sy filt p) global(tab tabsz)
|
||||||
|
(
|
||||||
|
lpf_pos>0.001 ? lpf_pos *= 0.95 : lpf_pos = 0.9;
|
||||||
|
sz = 64;
|
||||||
|
src = tab+tabsz*2 + sz*4 + 1024;
|
||||||
|
filt = src + tabsz*2 + sz*4 + 1024;
|
||||||
|
make_lowpass(filt, sz, lpf_pos);
|
||||||
|
memcpy(src, tab, tabsz*2);
|
||||||
|
i = src;
|
||||||
|
j = src + tabsz*2;
|
||||||
|
loop(sz/2,
|
||||||
|
memcpy(i-2,i,2);
|
||||||
|
memcpy(j,j-2,2);
|
||||||
|
j+=2;
|
||||||
|
i-=2;
|
||||||
|
);
|
||||||
|
i = 0;
|
||||||
|
loop(tabsz,
|
||||||
|
tab[i*2] != 10000 ? (
|
||||||
|
p = j = sx = sy = 0;
|
||||||
|
loop(sz,
|
||||||
|
v = src[(i+j-sz/2)*2];
|
||||||
|
v != 10000 ? (
|
||||||
|
p += filt[j];
|
||||||
|
sx += v * filt[j];
|
||||||
|
sy += src[(i+j-sz/2)*2 + 1] * filt[j];
|
||||||
|
);
|
||||||
|
j+=1;
|
||||||
|
);
|
||||||
|
tab[i*2] = sx/p;
|
||||||
|
tab[i*2+1] = sy/p;
|
||||||
|
);
|
||||||
|
i+=1;
|
||||||
|
);
|
||||||
|
|
||||||
|
);
|
||||||
|
|
||||||
|
function do_file(srcfn, destfn) local(fp fpout p lat lon skipping) (
|
||||||
|
(fp = fopen(srcfn,"r")) > 0 ? (
|
||||||
|
destfn < 0 || (fpout = fopen(destfn,"w")) > 0 ? (
|
||||||
|
p = tab;
|
||||||
|
skipping = 0;
|
||||||
|
while (fgets(fp,#line)) (
|
||||||
|
match("%s<trkpt lat=\"%f\" lon=\"%f\">%s",#line,#lead,lat,lon,#trail) ? (
|
||||||
|
destfn < 0 ? ( tabsz+=1; v_lat.include(p[1] = -lat); v_lon.include(p[0] = lon);) :
|
||||||
|
( !(skipping = p[0] == 10000) ? fprintf(fpout,"%s<trkpt lat=\"%.7f\" lon=\"%.7f\">%s",#lead,-p[1],p[0],#trail); );
|
||||||
|
p += 2;
|
||||||
|
) : destfn >= 0 ? ( skipping ? ( match("*</trkpt>*",#line) ? skipping = 0; ) : fwrite(fpout,#line,0) );
|
||||||
|
);
|
||||||
|
destfn >= 0 ? fclose(fpout);
|
||||||
|
);
|
||||||
|
fclose(fp);
|
||||||
|
);
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
function run() (
|
||||||
|
mouse_wheel ? ( zoom_view(mouse_wheel < 0 ? 1.1 : 1/1.1); mouse_wheel = 0;);
|
||||||
|
(mouse_cap&1)? (
|
||||||
|
!(last_mouse_cap & 1) ? (
|
||||||
|
cap_mode >= 0 && (mouse_cap&4) ? (
|
||||||
|
tmp = hit_test(mouse_x,mouse_y,0);
|
||||||
|
tmp >= 0 ? (
|
||||||
|
tmp < cap_mode ? (
|
||||||
|
cap_cnt = cap_mode+cap_cnt - tmp;
|
||||||
|
cap_mode = tmp;
|
||||||
|
) : (
|
||||||
|
cap_cnt = max(cap_mode+cap_cnt, tmp) - cap_mode;
|
||||||
|
);
|
||||||
|
) : cap_mode = tmp;
|
||||||
|
) : (
|
||||||
|
cap_mode = hit_test(mouse_x,mouse_y,0);
|
||||||
|
cap_mode >= 0 ? (
|
||||||
|
cap_cnt = 1;
|
||||||
|
while (hit_test(mouse_x,mouse_y,cap_mode+cap_cnt) >= 0) ( cap_cnt += 1; );
|
||||||
|
);
|
||||||
|
);
|
||||||
|
cap_start_lon = v_lon.unscale(mouse_x/gfx_w);
|
||||||
|
cap_start_lat = v_lat.unscale(mouse_y/gfx_h);
|
||||||
|
) : cap_mode >= 0 ? (
|
||||||
|
cap_cnt > 1 && (mouse_cap&4) ? (
|
||||||
|
linearize(tab + cap_mode*2,cap_cnt, cap_start_lon, cap_start_lat,
|
||||||
|
v_lon.unscale(mouse_x/gfx_w), v_lat.unscale(mouse_y/gfx_h),
|
||||||
|
mouse_cap&8);
|
||||||
|
) : (
|
||||||
|
tab[cap_mode*2] = v_lon.unscale(mouse_x/gfx_w);
|
||||||
|
tab[cap_mode*2+1] = v_lat.unscale(mouse_y/gfx_h);
|
||||||
|
);
|
||||||
|
is_dirty = 1;
|
||||||
|
) : scroll_view(mouse_x-last_mouse_x,mouse_y-last_mouse_y);
|
||||||
|
) : (
|
||||||
|
(last_mouse_cap & 1) && (last_mouse_cap & 16) && cap_mode >= 0 ? (
|
||||||
|
tab[cap_mode*2] = 10000;
|
||||||
|
is_dirty = 1;
|
||||||
|
);
|
||||||
|
is_dirty ? ( do_file(argv[1], argv[2]); is_dirty = 0; );
|
||||||
|
);
|
||||||
|
last_mouse_cap = mouse_cap;
|
||||||
|
last_mouse_x = mouse_x;
|
||||||
|
last_mouse_y = mouse_y;
|
||||||
|
|
||||||
|
p = tab;
|
||||||
|
hadprev = dist = 0;
|
||||||
|
loop(tabsz,
|
||||||
|
p[0] != 10000 ? (
|
||||||
|
x = v_lon.scale(p[0])*gfx_w;
|
||||||
|
y = v_lat.scale(p[1])*gfx_h;
|
||||||
|
hadprev ? (
|
||||||
|
gfx_set(0,.5,.7);
|
||||||
|
gfx_line(v_lon.scale(llon)*gfx_w,v_lat.scale(llat)*gfx_h,x,y);
|
||||||
|
dist += sqrt(sqr((p[0] - llon) * cos(llat*$pi/180.0)*69.172) + sqr((p[1] - llat)*60));
|
||||||
|
);
|
||||||
|
cap_mode == (p-tab)/2 ? gfx_set(1,0,1) : gfx_set(0,1,0);
|
||||||
|
gfx_circle(x,y,circle_size);
|
||||||
|
llon = p[0];
|
||||||
|
llat = p[1];
|
||||||
|
hadprev = 1;
|
||||||
|
);
|
||||||
|
p += 2;
|
||||||
|
);
|
||||||
|
|
||||||
|
gfx_x=gfx_y=0;
|
||||||
|
gfx_set(1,1,0);
|
||||||
|
gfx_printf("distance (approx) %.2f mi\n",dist);
|
||||||
|
|
||||||
|
gfx_update();
|
||||||
|
c = gfx_getchar();
|
||||||
|
c == 'l' ? ( lowpass(); is_dirty = 1; );
|
||||||
|
c >= 0 && c != 27 ? defer("run()");
|
||||||
|
);
|
||||||
|
|
||||||
|
argc < 2 ? printf("Usage: %s file.gpx output.gpx\n",argv[0]) : (
|
||||||
|
v_lat.min = v_lon.min = 100000;
|
||||||
|
v_lat.max = v_lon.max = -100000;
|
||||||
|
do_file(argv[1],-1);
|
||||||
|
tabsz > 0 ? (
|
||||||
|
zoom_view(1.3);
|
||||||
|
gfx_init("gpx edit",1024,768);
|
||||||
|
defer("run()");
|
||||||
|
) : printf("Error: %s has no <trkpt lines matching\n",argv[1]);
|
||||||
|
);
|
||||||
|
|
||||||
180
oversampling/WDL/eel2/scripts/httpd.eel
Normal file
180
oversampling/WDL/eel2/scripts/httpd.eel
Normal file
@@ -0,0 +1,180 @@
|
|||||||
|
// asynchronous HTTP server in EEL
|
||||||
|
|
||||||
|
|
||||||
|
function httpd.init(port, maxcon, table, stringoffs, wwwroot) global()
|
||||||
|
(
|
||||||
|
this.maxcon = maxcon;
|
||||||
|
this.tab = table;
|
||||||
|
this.port = port;
|
||||||
|
this.stringtab = stringoffs;
|
||||||
|
this.string_recsz = 2;
|
||||||
|
this.recsz = 3;
|
||||||
|
#this.wwwroot = wwwroot;
|
||||||
|
);
|
||||||
|
|
||||||
|
function httpd.getrecval(conid, parm) global()
|
||||||
|
(
|
||||||
|
conid >= 0 && conid < this.maxcon ? (
|
||||||
|
parm == 'sock' ? this.tab + conid*this.recsz + 0 : // socket
|
||||||
|
parm == 'st' ? this.tab + conid*this.recsz + 1 : // state
|
||||||
|
parm == 'fh' ? this.tab + conid*this.recsz + 2 : // file handle
|
||||||
|
0)
|
||||||
|
: 0
|
||||||
|
);
|
||||||
|
|
||||||
|
function httpd.set(conid, parm, value) local(x) global()
|
||||||
|
(
|
||||||
|
x = httpd.getrecval(conid,parm);
|
||||||
|
x>=0 ? x[]=value;
|
||||||
|
);
|
||||||
|
function httpd.get(conid, parm) local(x) global()
|
||||||
|
(
|
||||||
|
x=httpd.getrecval(conid,parm);
|
||||||
|
x>=0?x[];
|
||||||
|
);
|
||||||
|
function httpd.getbuf(conid, w) global()
|
||||||
|
(
|
||||||
|
conid >=0 && conid < this.maxcon ? this.stringtab + this.string_recsz*conid + w : -1;
|
||||||
|
);
|
||||||
|
function httpd.close(conid) global()
|
||||||
|
(
|
||||||
|
conid >=0 && conid < this.maxcon ?
|
||||||
|
(
|
||||||
|
tcp_close(httpd.get(conid,'sock'));
|
||||||
|
fclose(httpd.get(conid,'fh'));
|
||||||
|
httpd.set(conid,'sock',0);
|
||||||
|
httpd.set(conid,'fh',0);
|
||||||
|
);
|
||||||
|
);
|
||||||
|
function httpd.file_for_req(req, response) local(fn,fp) global() (
|
||||||
|
!strcmp(req,"") || !strcmp(req,"/") ? req = "/index.html";
|
||||||
|
|
||||||
|
str_getchar(req,0) == '/' &&
|
||||||
|
!match("*..*",req) &&
|
||||||
|
(fp=fopen(fn = strcat(#=#this.wwwroot,req),"rb")) > 0 ?
|
||||||
|
(
|
||||||
|
fseek(fp,0,1);
|
||||||
|
printf("GET %s -- 200: %s\n",req,fn);
|
||||||
|
strcat(response,sprintf(#, "HTTP/1.1 200 OK\r\n"
|
||||||
|
"Content-length: %d\r\n"
|
||||||
|
"Content-type: text/html\r\n"
|
||||||
|
"\r\n",
|
||||||
|
ftell(fp)));
|
||||||
|
fseek(fp,0,-1);
|
||||||
|
fp;
|
||||||
|
) : (
|
||||||
|
printf("GET %s -- 404\n",req);
|
||||||
|
|
||||||
|
fn = "The file specified could not be found.";
|
||||||
|
strcat(response,sprintf(#, "HTTP/1.1 404 NOT FOUND\r\n"
|
||||||
|
"Content-length: %d\r\n"
|
||||||
|
"Content-type: text/plain\r\n"
|
||||||
|
"\r\n"
|
||||||
|
"%s", strlen(fn),fn));
|
||||||
|
0;
|
||||||
|
);
|
||||||
|
);
|
||||||
|
|
||||||
|
// run input and output buffers
|
||||||
|
function httpd.runcon(idx) local(sock,x,str,rv,buffers) global() (
|
||||||
|
rv=0;
|
||||||
|
sock = httpd.get(idx,'sock');
|
||||||
|
sock > 0 ?
|
||||||
|
(
|
||||||
|
x=tcp_recv(sock,str=#);
|
||||||
|
x<0 ? (
|
||||||
|
httpd.close(idx);
|
||||||
|
) : (
|
||||||
|
buffers=httpd.getbuf(idx,0);
|
||||||
|
x>0 ? strcat(buffers,str);
|
||||||
|
rv+=x;
|
||||||
|
|
||||||
|
strlen(buffers+1)>0 ?
|
||||||
|
(
|
||||||
|
(x=tcp_send(sock,buffers+1)) > 0 ?
|
||||||
|
(
|
||||||
|
rv+=1;
|
||||||
|
str_delsub(buffers+1.1,0,x);
|
||||||
|
);
|
||||||
|
);
|
||||||
|
);
|
||||||
|
);
|
||||||
|
rv;
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
function httpd.run() local(sock, x, i, str, rv, t,hdrs,httpdver) global() (
|
||||||
|
str=#;
|
||||||
|
hdrs=#;
|
||||||
|
rv=0;
|
||||||
|
sock=tcp_listen(this.port,"",str);
|
||||||
|
sock > 0 ?
|
||||||
|
(
|
||||||
|
i=0;
|
||||||
|
while (i < this.maxcon && httpd.get(i,'sock')) ( i += 1; );
|
||||||
|
i < this.maxcon ? (
|
||||||
|
tcp_set_block(sock,0);
|
||||||
|
httpd.set(i,'sock',sock);
|
||||||
|
httpd.set(i,'st',0);
|
||||||
|
|
||||||
|
x=0;
|
||||||
|
loop(this.string_recsz, strcpy(httpd.getbuf(i,x), ""); x+=1; );
|
||||||
|
printf("httpd.run(): connection from '%s'\n",str);
|
||||||
|
rv+=1;
|
||||||
|
|
||||||
|
) : (
|
||||||
|
printf("httpd.run(): dropping connect from '%s', slots full\n",str);
|
||||||
|
tcp_close(x);
|
||||||
|
);
|
||||||
|
);
|
||||||
|
|
||||||
|
i=0;
|
||||||
|
while (i < this.maxcon)
|
||||||
|
(
|
||||||
|
this.httpd.runcon(i) ?
|
||||||
|
(
|
||||||
|
t = httpd.getbuf(i,0);
|
||||||
|
this.httpd.get(i,'st') == 0 ?
|
||||||
|
(
|
||||||
|
match("GET %S HTTP/1.%1d%S",t,str,httpdver,hdrs) &&
|
||||||
|
str_getchar(hdrs,0)=='\r' && str_getchar(hdrs,1)=='\n' &&
|
||||||
|
!strcmp(strcpy_substr(#,hdrs,-4),"\r\n\r\n")
|
||||||
|
?
|
||||||
|
(
|
||||||
|
httpd.set(i,'st',1 |
|
||||||
|
((httpdver==0 || matchi("\r\nConnection:*close\r\n",hdrs))?2:0)
|
||||||
|
);
|
||||||
|
|
||||||
|
httpd.set(i,'fh',httpd.file_for_req(str,t+1));
|
||||||
|
);
|
||||||
|
);
|
||||||
|
|
||||||
|
httpd.get(i,'fh') && !strlen(t+1) ? fread(httpd.get(i,'fh'),t+1,4096);
|
||||||
|
|
||||||
|
httpd.get(i,'st') == 3 ? (
|
||||||
|
strlen(t+1) == 0 ? (
|
||||||
|
httpd.close(i);
|
||||||
|
printf("sent data, closing\n");
|
||||||
|
);
|
||||||
|
);
|
||||||
|
|
||||||
|
rv+=1;
|
||||||
|
);
|
||||||
|
i+=1;
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
rv;
|
||||||
|
);
|
||||||
|
|
||||||
|
argc != 3 || strlen(argv[1])<1 || !match("%d",argv[2],port) ? (
|
||||||
|
printf("Usage: \n\t%s www_root port\n",argv[0]);
|
||||||
|
) : (
|
||||||
|
srv.httpd.init(port, 100, 100000, 10000, argv[1]);
|
||||||
|
// string [conidx] = send buffe
|
||||||
|
while(1)
|
||||||
|
(
|
||||||
|
srv.httpd.run() || Sleep(10);
|
||||||
|
);
|
||||||
|
);
|
||||||
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user