From feb72389aa4e1070fcbc888280ea65b178176460 Mon Sep 17 00:00:00 2001 From: ZoRo Date: Thu, 26 Aug 2021 09:48:17 +0100 Subject: Update source to be compatible with emscripten --- Build/Makefile | 5 ++-- Build/index.html | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ NaiveFFT/fft.c | 8 +++--- NaiveFFT/fft.h | 19 +++++++++++--- NaiveFFT/main.c | 8 ++++++ 5 files changed, 110 insertions(+), 10 deletions(-) create mode 100644 Build/index.html diff --git a/Build/Makefile b/Build/Makefile index e19df12..f56677d 100644 --- a/Build/Makefile +++ b/Build/Makefile @@ -7,10 +7,11 @@ EM_LDFALGS=-s LLD_REPORT_UNDEFINED make: $(CC) -c $(SOURCEDIR)/main.c -g3 - $(CC) main.o -o NaiveFFT $(LDFLAGS) + $(CC) -c $(SOURCEDIR)/fft.c -g3 + $(CC) main.o fft.o -o NaiveFFT $(LDFLAGS) emcc: - $(EMCC) $(SOURCEDIR)/main.c ../NaiveFFT/fft.c -s WASM=1 -O3 -o index.js $(EM_LDFALGS) -s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]' + $(EMCC) $(SOURCEDIR)/main.c $(SOURCEDIR)/fft.c -s WASM=1 -O3 -o index.js $(EM_LDFALGS) -s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]' web: diff --git a/Build/index.html b/Build/index.html new file mode 100644 index 0000000..8f510ac --- /dev/null +++ b/Build/index.html @@ -0,0 +1,80 @@ + + + + + WebAssembly Example + + + + + + + + + + + FFT demo + + + + + + + \ No newline at end of file diff --git a/NaiveFFT/fft.c b/NaiveFFT/fft.c index cfcf3fa..b4c7d33 100644 --- a/NaiveFFT/fft.c +++ b/NaiveFFT/fft.c @@ -11,7 +11,7 @@ //calc the fft -void fft_if(double *x_i, double *x_q, int n, int inv) { +void KEEPALIVE fft_if(double *x_i, double *x_q, int n, int inv) { int i=0,j=0,k=0,m=0, irem=0, sign; double tq,ti; @@ -109,7 +109,7 @@ void fft_if(double *x_i, double *x_q, int n, int inv) { #define complex_mul_re(a_re, a_im, b_re, b_im) (a_re * b_re - a_im * b_im) #define complex_mul_im(a_re, a_im, b_re, b_im) (a_re * b_im + a_im * b_re) //https://github.com/rshuston/FFT-C/blob/master/libfft/fft.c -void ffti_shuffle_1(double *x_i, double *x_q, uint64_t n) { +void KEEPALIVE ffti_shuffle_1(double *x_i, double *x_q, uint64_t n) { int Nd2 = n>>1; int Nm1 = n-1; int i,j; @@ -156,7 +156,7 @@ void ffti_shuffle_1(double *x_i, double *x_q, uint64_t n) { j ^= bits; } } -void fft_1(double *x_i, double *x_q, uint64_t n, uint64_t inv) { +void KEEPALIVE fft_1(double *x_i, double *x_q, uint64_t n, uint64_t inv) { uint64_t n_log2; uint64_t r; uint64_t m, md2; @@ -225,7 +225,7 @@ void fft_1(double *x_i, double *x_q, uint64_t n, uint64_t inv) { } //dft works fine -void dft(double *x_i, double *x_q, int n, int inv) { +void KEEPALIVE dft(double *x_i, double *x_q, int n, int inv) { double Wn,Wk; //static array //double Xi[DATA_SIZE],Xq[DATA_SIZE]; diff --git a/NaiveFFT/fft.h b/NaiveFFT/fft.h index f1ee783..80ae6cc 100644 --- a/NaiveFFT/fft.h +++ b/NaiveFFT/fft.h @@ -11,10 +11,21 @@ #include #include #include +#include +#include +#if __EMSCRIPTEN__ +#include +#endif -void fft_if(double *x_i, double *x_q, int n, int inv); -void ffti_shuffle_1(double *x_i, double *x_q, uint64_t n); -void fft_1(double *x_i, double *x_q, uint64_t n, uint64_t inv); -void dft(double *x_i, double *x_q, int n, int inv); +#ifdef __EMSCRIPTEN__ +#define KEEPALIVE EMSCRIPTEN_KEEPALIVE +#else +#define KEEPALIVE +#endif + +void KEEPALIVE fft_if(double *x_i, double *x_q, int n, int inv); +void KEEPALIVE ffti_shuffle_1(double *x_i, double *x_q, uint64_t n); +void KEEPALIVE fft_1(double *x_i, double *x_q, uint64_t n, uint64_t inv); +void KEEPALIVE dft(double *x_i, double *x_q, int n, int inv); #endif /* fft_h */ diff --git a/NaiveFFT/main.c b/NaiveFFT/main.c index 036bb79..6372c79 100644 --- a/NaiveFFT/main.c +++ b/NaiveFFT/main.c @@ -14,6 +14,10 @@ #include #include +#if __EMSCRIPTEN__ +#include +#endif + #include "fft.h" //test data array @@ -26,6 +30,9 @@ double data_q[DATA_SIZE] = {0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f}; int main(int argc, const char * argv[]) { +#if __EMSCRIPTEN__ + printf("Wasm NaiveFFT module loaded\n"); +#else int i; // insert code here... @@ -95,5 +102,6 @@ int main(int argc, const char * argv[]) { strftime(buffer, 32, "%H:%M:%S", tm_info); puts(buffer); } +#endif return 0; } -- cgit v1.2.3