-
Notifications
You must be signed in to change notification settings - Fork 3
/
nseel-compiler.c
8188 lines (8163 loc) · 310 KB
/
nseel-compiler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Expression Evaluator Library (NS-EEL) v2
Copyright (C) 2004-2013 Cockos Incorporated
Copyright (C) 1999-2003 Nullsoft, Inc.
nseel-compiler.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 <math.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <ctype.h>
#include <float.h>
#include "eelCommon.h"
#include "glue_port.h"
#include <assert.h>
#include "eel_matrix.h"
#include "numericSys/FFTConvolver.h"
#include "numericSys/HPFloat/xpre.h"
static void lstrcpyn_safe(char *o, const char *in, int32_t count)
{
if (count > 0)
{
while (--count > 0 && *in) *o++ = *in++;
*o = 0;
}
}
static void lstrcatn(char *o, const char *in, int32_t count)
{
if (count > 0)
{
while (*o) { if (--count < 1) return; o++; }
while (--count > 0 && *in) *o++ = *in++;
*o = 0;
}
}
#define STB_SPRINTF_IMPLEMENTATION
#include "stb_sprintf.h"
static void snprintf_append(char *o, int32_t count, const char *format, ...)
{
if (count > 0)
{
va_list va;
while (*o) { if (--count < 1) return; o++; }
va_start(va, format);
stbsp_vsnprintf(o, count, format, va);
va_end(va);
}
}
#define NSEEL_VARS_MALLOC_CHUNKSIZE 8
#define RET_MINUS1_FAIL(x) return -1;
#define MIN_COMPUTABLE_SIZE 32 // always use at least this big of a temp storage table (and reset the temp ptr when it goes past this boundary)
#define COMPUTABLE_EXTRA_SPACE 16 // safety buffer, if EEL_VALIDATE_WORKTABLE_USE set, used for magic-value-checking
/*
P1 is rightmost parameter
P2 is second rightmost, if any
P3 is third rightmost, if any
registers on x86 are (RAX etc on x86-64)
P1(ret) EAX
P2 EDI
P3 ECX
WTP RSI
x86_64: r12 is a pointer to ram_state
x86_64: r13 is a pointer to closenessfactor
registers on PPC are:
P1(ret) r3
P2 r14
P3 r15
WTP r16 (r17 has the original value)
r13 is a pointer to ram_state
ppc uses f31 and f30 and others for certain constants
*/
// used by //#eel-no-optimize:xxx, in 0
#define OPTFLAG_NO_FPSTACK 2
#define OPTFLAG_NO_INLINEFUNC 4
#define MAX_SUB_NAMESPACES 32
typedef struct
{
const char *namespacePathToThis;
const char *subParmInfo[MAX_SUB_NAMESPACES];
} namespaceInformation;
static int32_t nseel_evallib_stats[5]; // source bytes, static code bytes, call code bytes, data bytes, segments
int32_t *NSEEL_getstats()
{
return nseel_evallib_stats;
}
static int32_t findLineNumber(const char *exp, int32_t byteoffs)
{
int32_t lc = 0;
while (byteoffs-- > 0 && *exp) if (*exp++ == '\n') lc++;
return lc;
}
static void *__newBlock(llBlock **start, int32_t size);
#define OPCODE_IS_TRIVIAL(x) ((x)->opcodeType <= OPCODETYPE_VARPTRPTR)
enum {
OPCODETYPE_DIRECTVALUE = 0,
OPCODETYPE_DIRECTVALUE_TEMPSTRING, // like directvalue, but will generate a new tempstring value on generate
OPCODETYPE_VALUE_FROM_NAMESPACENAME, // this.* or namespace.* are encoded this way
OPCODETYPE_VARPTR,
OPCODETYPE_VARPTRPTR,
OPCODETYPE_FUNC1,
OPCODETYPE_FUNC2,
OPCODETYPE_FUNC3,
OPCODETYPE_FUNCX,
OPCODETYPE_MOREPARAMS,
OPCODETYPE_INVALID,
};
struct opcodeRec
{
int32_t opcodeType;
int32_t fntype;
void *fn;
union {
struct opcodeRec *parms[3];
struct {
float directValue;
float *valuePtr; // if direct value, valuePtr can be cached
} dv;
} parms;
int32_t namespaceidx;
// OPCODETYPE_VALUE_FROM_NAMESPACENAME (relname is either empty or blah)
// OPCODETYPE_VARPTR if it represents a global variable, will be nonempty
// OPCODETYPE_FUNC* with fntype=FUNCTYPE_EELFUNC
const char *relname;
};
float *dataSectionToRamDisk(void *opaque, size_t len)
{
compileContext *c = (compileContext*)opaque;
return c->ram_state + (NSEEL_RAM_ITEMSPERBLOCK - len);
}
static void *newTmpBlock(compileContext *ctx, int32_t size)
{
const int32_t align = 8;
const int32_t a1 = align - 1;
char *p = (char*)__newBlock(&ctx->tmpblocks_head, size + a1);
return p + ((align - (((INT_PTR)p)&a1))&a1);
}
static void *__newBlock_align(compileContext *ctx, int32_t size, int32_t align, int32_t isForCode)
{
const int32_t a1 = align - 1;
char *p = (char*)__newBlock(
(
isForCode < 0 ? (isForCode == -2 ? &ctx->pblocks : &ctx->tmpblocks_head) :
isForCode > 0 ? &ctx->blocks_head :
&ctx->blocks_head_data), size + a1);
return p + ((align - (((INT_PTR)p)&a1))&a1);
}
static opcodeRec *newOpCode(compileContext *ctx, const char *str, int32_t opType)
{
const size_t strszfull = str ? strlen(str) : 0;
const size_t str_sz = min(NSEEL_MAX_VARIABLE_NAMELEN, strszfull);
opcodeRec *rec = (opcodeRec*)__newBlock_align(ctx,
(int32_t)(sizeof(opcodeRec) + (str_sz > 0 ? str_sz + 1 : 0)),
8, ctx->isSharedFunctions ? 0 : -1);
if (rec)
{
memset(rec, 0, sizeof(*rec));
rec->opcodeType = opType;
if (str_sz > 0)
{
char *p = (char *)(rec + 1);
memcpy(p, str, str_sz);
p[str_sz] = 0;
rec->relname = p;
}
else
{
rec->relname = "";
}
}
return rec;
}
#define newCodeBlock(x,a) __newBlock_align(ctx,x,a,1)
#define newDataBlock(x,a) __newBlock_align(ctx,x,a,0)
#define newCtxDataBlock(x,a) __newBlock_align(ctx,x,a,-2)
static void freeBlocks(llBlock **start);
#ifndef DECL_ASMFUNC
#define DECL_ASMFUNC(x) \
void nseel_asm_##x(void); \
void nseel_asm_##x##_end(void);
void _asm_megabuf(void);
void _asm_megabuf_end(void);
#endif
#define FUNCTIONTYPE_PARAMETERCOUNTMASK 0xff
#define BIF_NPARAMS_MASK 0x7ffff00
#define BIF_RETURNSONSTACK 0x0000100
#define BIF_LASTPARMONSTACK 0x0000200
#define BIF_RETURNSBOOL 0x0000400
#define BIF_LASTPARM_ASBOOL 0x0000800
// 0x00?0000 -- taken by FP stack flags
#define BIF_TAKES_VARPARM 0x0400000
#define BIF_TAKES_VARPARM_EX 0x0C00000 // this is like varparm but check count exactly
#define BIF_WONTMAKEDENORMAL 0x0100000
#define BIF_CLEARDENORMAL 0x0200000
#if defined(GLUE_HAS_FXCH) && GLUE_MAX_FPSTACK_SIZE > 0
#define BIF_SECONDLASTPARMST 0x0001000 // use with BIF_LASTPARMONSTACK only (last two parameters get passed on fp stack)
#define BIF_LAZYPARMORDERING 0x0002000 // allow optimizer to avoid fxch when using BIF_TWOPARMSONFPSTACK_LAZY etc
#define BIF_REVERSEFPORDER 0x0004000 // force a fxch (reverse order of last two parameters on fp stack, used by comparison functions)
#ifndef BIF_FPSTACKUSE
#define BIF_FPSTACKUSE(x) (((x)>=0&&(x)<8) ? ((7-(x))<<16):0)
#endif
#ifndef BIF_GETFPSTACKUSE
#define BIF_GETFPSTACKUSE(x) (7 - (((x)>>16)&7))
#endif
#else
// do not support fp stack use unless GLUE_HAS_FXCH and GLUE_MAX_FPSTACK_SIZE>0
#define BIF_SECONDLASTPARMST 0
#define BIF_LAZYPARMORDERING 0
#define BIF_REVERSEFPORDER 0
#define BIF_FPSTACKUSE(x) 0
#define BIF_GETFPSTACKUSE(x) 0
#endif
#define BIF_TWOPARMSONFPSTACK (BIF_SECONDLASTPARMST|BIF_LASTPARMONSTACK)
#define BIF_TWOPARMSONFPSTACK_LAZY (BIF_LAZYPARMORDERING|BIF_SECONDLASTPARMST|BIF_LASTPARMONSTACK)
float NSEEL_CGEN_CALL nseel_int_rand(float f);
#define FNPTR_HAS_CONDITIONAL_EXEC(op) \
(op->fntype == FN_LOGICAL_AND || \
op->fntype == FN_LOGICAL_OR || \
op->fntype == FN_IF_ELSE || \
op->fntype == FN_WHILE || \
op->fntype == FN_LOOP)
// Add custom functions
static const unsigned char base64_table[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/**
* base64_encode - Base64 encode
* @src: Data to be encoded
* @len: Length of the data to be encoded
* @out_len: Pointer to output length variable, or %NULL if not used
* Returns: Allocated buffer of out_len bytes of encoded data,
* or %NULL on failure
*
* Caller is responsible for freeing the returned buffer. Returned buffer is
* nul terminated to make it easier to use as a C string. The nul terminator is
* not included in out_len.
*/
static unsigned char* base64_encode(const unsigned char *src, size_t len, size_t *out_len)
{
unsigned char *out, *pos;
const unsigned char *end, *in;
size_t olen;
olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */
olen += olen / 72; /* line feeds */
olen++; /* nul termination */
if (olen < len)
return NULL; /* integer overflow */
out = (unsigned char*)malloc(olen);
if (out == NULL)
return NULL;
end = src + len;
in = src;
pos = out;
while (end - in >= 3) {
*pos++ = base64_table[in[0] >> 2];
*pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
*pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
*pos++ = base64_table[in[2] & 0x3f];
in += 3;
}
if (end - in) {
*pos++ = base64_table[in[0] >> 2];
if (end - in == 1) {
*pos++ = base64_table[(in[0] & 0x03) << 4];
*pos++ = '=';
}
else {
*pos++ = base64_table[((in[0] & 0x03) << 4) |
(in[1] >> 4)];
*pos++ = base64_table[(in[1] & 0x0f) << 2];
}
*pos++ = '=';
}
*pos = '\0';
if (out_len)
*out_len = pos - out;
return out;
}
/**
* base64_decode - Base64 decode
* @src: Data to be decoded
* @len: Length of the data to be decoded
* @out_len: Pointer to output length variable
* Returns: Allocated buffer of out_len bytes of decoded data,
* or %NULL on failure
*
* Caller is responsible for freeing the returned buffer.
*/
static unsigned char* base64_decode(const unsigned char *src, size_t len, size_t *out_len)
{
unsigned char dtable[256], *out, *pos, block[4], tmp;
size_t i, count, olen;
int32_t pad = 0;
memset(dtable, 0x80, 256);
for (i = 0; i < sizeof(base64_table) - 1; i++)
dtable[base64_table[i]] = (unsigned char)i;
dtable['='] = 0;
count = 0;
for (i = 0; i < len; i++) {
if (dtable[src[i]] != 0x80)
count++;
}
if (count == 0 || count % 4)
return NULL;
olen = count / 4 * 3;
pos = out = malloc(olen);
if (out == NULL)
return NULL;
count = 0;
for (i = 0; i < len; i++) {
tmp = dtable[src[i]];
if (tmp == 0x80)
continue;
if (src[i] == '=')
pad++;
block[count] = tmp;
count++;
if (count == 4) {
*pos++ = (block[0] << 2) | (block[1] >> 4);
*pos++ = (block[1] << 4) | (block[2] >> 2);
*pos++ = (block[2] << 6) | block[3];
count = 0;
if (pad) {
if (pad == 1)
pos--;
else if (pad == 2)
pos -= 2;
else {
/* Invalid padding */
free(out);
return NULL;
}
break;
}
}
}
*out_len = pos - out;
return out;
}
#include <time.h>
#ifndef _WIN32
#include <unistd.h>
#include <sys/time.h>
#endif
static float NSEEL_CGEN_CALL _eel_sleep(float amt)
{
if (amt >= 0.0f)
{
#ifdef _WIN32
if (amt > 30000000.0) Sleep(30000000);
else Sleep((DWORD)(amt + 0.5f));
#else
if (amt > 30000000.0) usleep(((useconds_t)30000000) * 1000);
else usleep((useconds_t)(amt * 1000.0 + 0.5f));
#endif
}
return 0.0f;
}
static float NSEEL_CGEN_CALL _eel_time(void *opaque)
{
return (float)time(NULL);
}
static float NSEEL_CGEN_CALL _eel_time_precise(void *opaque)
{
#ifdef _WIN32
LARGE_INTEGER freq, now;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&now);
return (float)now.QuadPart / (float)freq.QuadPart;
#else
struct timeval tm = { 0, };
gettimeofday(&tm, NULL);
return (float)tm.tv_sec + (float)tm.tv_usec * 0.000001;
#endif
}
void discreteHartleyTransform(double *A, const int32_t nPoints, const double *sinTab)
{
int32_t i, j, n, n2, theta_inc, nptDiv2;
double alpha, beta;
for (i = 0; i < nPoints; i += 4)
{
const double x0 = A[i];
const double x1 = A[i + 1];
const double x2 = A[i + 2];
const double x3 = A[i + 3];
const double y0 = x0 + x1;
const double y1 = x0 - x1;
const double y2 = x2 + x3;
const double y3 = x2 - x3;
A[i] = y0 + y2;
A[i + 2] = y0 - y2;
A[i + 1] = y1 + y3;
A[i + 3] = y1 - y3;
}
for (i = 0; i < nPoints; i += 8)
{
alpha = A[i];
beta = A[i + 4];
A[i] = alpha + beta;
A[i + 4] = alpha - beta;
alpha = A[i + 2];
beta = A[i + 6];
A[i + 2] = alpha + beta;
A[i + 6] = alpha - beta;
alpha = A[i + 1];
const double beta1 = 0.70710678118654752440084436210485*(A[i + 5] + A[i + 7]);
const double beta2 = 0.70710678118654752440084436210485*(A[i + 5] - A[i + 7]);
A[i + 1] = alpha + beta1;
A[i + 5] = alpha - beta1;
alpha = A[i + 3];
A[i + 3] = alpha + beta2;
A[i + 7] = alpha - beta2;
}
n = 16;
n2 = 8;
theta_inc = nPoints >> 4;
nptDiv2 = nPoints >> 2;
while (n <= nPoints)
{
for (i = 0; i < nPoints; i += n)
{
int32_t theta = theta_inc;
const int32_t n4 = n2 >> 1;
alpha = A[i];
beta = A[i + n2];
A[i] = alpha + beta;
A[i + n2] = alpha - beta;
alpha = A[i + n4];
beta = A[i + n2 + n4];
A[i + n4] = alpha + beta;
A[i + n2 + n4] = alpha - beta;
for (j = 1; j < n4; j++)
{
double sinval = sinTab[theta];
double cosval = sinTab[theta + nptDiv2];
double alpha1 = A[i + j];
double alpha2 = A[i - j + n2];
double beta1 = A[i + j + n2] * cosval + A[i - j + n] * sinval;
double beta2 = A[i + j + n2] * sinval - A[i - j + n] * cosval;
theta += theta_inc;
A[i + j] = alpha1 + beta1;
A[i + j + n2] = alpha1 - beta1;
A[i - j + n2] = alpha2 + beta2;
A[i - j + n] = alpha2 - beta2;
}
}
n <<= 1;
n2 <<= 1;
theta_inc >>= 1;
}
}
void discreteHartleyTransformFloat(float *A, const int32_t nPoints, const float *sinTab)
{
int32_t i, j, n, n2, theta_inc, nptDiv2;
float alpha, beta;
for (i = 0; i < nPoints; i += 4)
{
const float x0 = A[i];
const float x1 = A[i + 1];
const float x2 = A[i + 2];
const float x3 = A[i + 3];
const float y0 = x0 + x1;
const float y1 = x0 - x1;
const float y2 = x2 + x3;
const float y3 = x2 - x3;
A[i] = y0 + y2;
A[i + 2] = y0 - y2;
A[i + 1] = y1 + y3;
A[i + 3] = y1 - y3;
}
for (i = 0; i < nPoints; i += 8)
{
alpha = A[i];
beta = A[i + 4];
A[i] = alpha + beta;
A[i + 4] = alpha - beta;
alpha = A[i + 2];
beta = A[i + 6];
A[i + 2] = alpha + beta;
A[i + 6] = alpha - beta;
alpha = A[i + 1];
const float beta1 = 0.70710678118654752440084436210485f*(A[i + 5] + A[i + 7]);
const float beta2 = 0.70710678118654752440084436210485f*(A[i + 5] - A[i + 7]);
A[i + 1] = alpha + beta1;
A[i + 5] = alpha - beta1;
alpha = A[i + 3];
A[i + 3] = alpha + beta2;
A[i + 7] = alpha - beta2;
}
n = 16;
n2 = 8;
theta_inc = nPoints >> 4;
nptDiv2 = nPoints >> 2;
while (n <= nPoints)
{
for (i = 0; i < nPoints; i += n)
{
int32_t theta = theta_inc;
const int32_t n4 = n2 >> 1;
alpha = A[i];
beta = A[i + n2];
A[i] = alpha + beta;
A[i + n2] = alpha - beta;
alpha = A[i + n4];
beta = A[i + n2 + n4];
A[i + n4] = alpha + beta;
A[i + n2 + n4] = alpha - beta;
for (j = 1; j < n4; j++)
{
float sinval = sinTab[theta];
float cosval = sinTab[theta + nptDiv2];
float alpha1 = A[i + j];
float alpha2 = A[i - j + n2];
float beta1 = A[i + j + n2] * cosval + A[i - j + n] * sinval;
float beta2 = A[i + j + n2] * sinval - A[i - j + n] * cosval;
theta += theta_inc;
A[i + j] = alpha1 + beta1;
A[i + j + n2] = alpha1 - beta1;
A[i - j + n2] = alpha2 + beta2;
A[i - j + n] = alpha2 - beta2;
}
}
n <<= 1;
n2 <<= 1;
theta_inc >>= 1;
}
}
#define M_PIDouble 3.1415926535897932384626433832795
void getAsymmetricWindow(float *analysisWnd, float *synthesisWnd, int32_t k, int32_t m, float freq_temporal)
{
int32_t i;
if ((k / m) < 4)
freq_temporal = 1.0f;
if (freq_temporal > 9.0f)
freq_temporal = 9.0f;
memset(synthesisWnd, 0, k * sizeof(float));
int32_t n = ((k - m) << 1) + 2;
for (i = 0; i < k - m; ++i)
analysisWnd[i] = (float)pow(0.5 * (1.0 - cos(2.0 * M_PIDouble * (i + 1.0) / (double)n)), freq_temporal);
n = (m << 1) + 2;
if (freq_temporal > 1.02)
freq_temporal = 1.02;
for (i = k - m; i < k; ++i)
analysisWnd[i] = (float)pow(sqrt(0.5 * (1.0 - cos(2.0 * M_PIDouble * ((m + i - (k - m)) + 1.0) / (double)n))), freq_temporal);
n = m << 1;
for (i = k - (m << 1); i < k; ++i)
synthesisWnd[i - (k - (m << 1))] = (float)(0.5 * (1.0 - cos(2.0 * M_PIDouble * (double)(i - (k - (m << 1))) / (double)n))) / analysisWnd[i];
}
void getwnd(float *wnd, unsigned int m, unsigned int n, char *mode)
{
unsigned int i;
double x;
if (!strcmp(mode, "hann"))
{
for (i = 0; i < m; i++)
{
x = i / (double)(n - 1);
wnd[i] = (float)(0.5 - 0.5 * cos(2 * M_PIDouble * x));
}
}
else if (!strcmp(mode, "hamming"))
{
for (i = 0; i < m; i++)
{
x = i / (double)(n - 1);
wnd[i] = (float)(0.54 - 0.46 * cos(2 * M_PIDouble * x));
}
}
else if (!strcmp(mode, "blackman"))
{
for (i = 0; i < m; i++)
{
x = i / (double)(n - 1);
wnd[i] = (float)(0.42 - 0.5 * cos(2 * M_PIDouble * x) + 0.08 * cos(4 * M_PIDouble * x));
}
}
else if (!strcmp(mode, "flattop"))
{
double a0 = 0.21557895;
double a1 = 0.41663158;
double a2 = 0.277263158;
double a3 = 0.083578947;
double a4 = 0.006947368;
for (i = 0; i < m; i++)
{
x = i / (double)(n - 1);
wnd[i] = (float)(a0 - a1 * cos(2 * M_PIDouble * x) + a2 * cos(4 * M_PIDouble * x) - a3 * cos(6 * M_PIDouble * x) + a4 * cos(8 * M_PIDouble * x));
}
}
}
void genWnd(float *wnd, unsigned int N, char *type)
{
unsigned int plus1 = N + 1;
unsigned int half;
unsigned int i;
if (plus1 % 2 == 0)
{
half = plus1 / 2;
getwnd(wnd, half, plus1, type);
for (i = 0; i < half - 1; i++)
wnd[i + half] = wnd[half - i - 1];
}
else
{
half = (plus1 + 1) / 2;
getwnd(wnd, half, plus1, type);
for (i = 0; i < half - 2; i++)
wnd[i + half] = wnd[half - i - 2];
}
}
void STFT_DynInit(int32_t *indexFw, float *analysisWnd)
{
int32_t i;
int32_t ovpSmps = indexFw[0] / indexFw[1];
int32_t bufferSize = (indexFw[0] * 6) + indexFw[3] + indexFw[3] + (int32_t)((float)(indexFw[0] * sizeof(uint32_t)) / (float)(sizeof(float) / sizeof(uint32_t)));
memset(analysisWnd, 0, bufferSize * sizeof(float));
float *synthesisWnd = analysisWnd + indexFw[0];
uint32_t *bitRevTbl = (uint32_t*)(analysisWnd + (indexFw[0] * 6) + indexFw[3] + indexFw[3]);
uint32_t bitsConst = 0;
uint32_t v = indexFw[0];
while (v > 1)
{
++bitsConst;
v >>= 1;
}
for (i = 0; i < indexFw[0]; ++i)
{
uint32_t bits = bitsConst;
uint32_t x = i;
bitRevTbl[i] = 0;
while (bits--)
{
bitRevTbl[i] = (bitRevTbl[i] + bitRevTbl[i]) + (x & 1);
x >>= 1;
}
}
float *sineTbl = synthesisWnd + indexFw[0];
float pi2dN = (M_PIDouble * 2.0f) / indexFw[0];
for (i = 0; i < indexFw[0]; ++i)
sineTbl[i] = (float)sin(pi2dN * i);
getAsymmetricWindow(analysisWnd, synthesisWnd, indexFw[0], ovpSmps, indexFw[5] / (float)32767);
// Pre-shift window function
for (i = 0; i < indexFw[0] - indexFw[2]; i++)
synthesisWnd[i] = synthesisWnd[i] * (1.0f / indexFw[0]) * 0.5f;
}
int32_t STFTCartesian(float *indexer, float *analysisWnd, float *ptr)
{
int32_t *indexFw = (int32_t*)indexer;
float *mSineTab = analysisWnd + indexFw[0] * 2;
float *mInput = mSineTab + indexFw[0];
float *mTempBuffer = mInput + indexFw[0];
uint32_t *bitRevTbl = (uint32_t*)(analysisWnd + (indexFw[0] * 6) + indexFw[3] + indexFw[3]);
int32_t i, symIdx;
for (i = 0; i < indexFw[0]; ++i)
mTempBuffer[bitRevTbl[i]] = mInput[(i + indexFw[4]) & (indexFw[0] - 1)] * analysisWnd[i];
discreteHartleyTransformFloat(mTempBuffer, indexFw[0], mSineTab);
ptr[0] = mTempBuffer[0] * 2.0f;
ptr[1] = 0.0f;
float lR, lI;
for (i = 1; i < ((indexFw[0] >> 1) + 1); i++)
{
symIdx = indexFw[0] - i;
lR = mTempBuffer[i] + mTempBuffer[symIdx];
lI = mTempBuffer[i] - mTempBuffer[symIdx];
ptr[i << 1] = lR;
ptr[(i << 1) + 1] = -lI;
}
return indexFw[0] + 2;
}
int32_t STFTCartesianInverse(float *indexer, float *analysisWnd, float *ptr)
{
int32_t *indexFw = (int32_t*)indexer;
float *synthesisWnd = analysisWnd + indexFw[0];
float *mSineTab = synthesisWnd + indexFw[0];
float *timeDomainOut = mSineTab + indexFw[0] * 3;
float *mOutputBuffer = timeDomainOut + indexFw[0];
float *mOverlapStage2Ldash = mOutputBuffer + indexFw[3];
uint32_t *bitRevTbl = (uint32_t*)(analysisWnd + (indexFw[0] * 6) + indexFw[3] + indexFw[3]);
int32_t i;
timeDomainOut[0] = ptr[0];
float lR, lI;
for (i = 1; i < ((indexFw[0] >> 1) + 1); i++)
{
lR = ptr[i << 1];
lI = -ptr[(i << 1) + 1];
timeDomainOut[bitRevTbl[i]] = (lR + lI);
timeDomainOut[bitRevTbl[indexFw[0] - i]] = (lR - lI);
}
discreteHartleyTransformFloat(timeDomainOut, indexFw[0], mSineTab);
for (i = 0; i < indexFw[0] - indexFw[2]; i++)
timeDomainOut[i] = timeDomainOut[i + indexFw[2]] * synthesisWnd[i];
for (i = 0; i < indexFw[3]; ++i)
{
mOutputBuffer[i] = mOverlapStage2Ldash[i] + timeDomainOut[i];
mOverlapStage2Ldash[i] = timeDomainOut[indexFw[3] + i];
}
return indexFw[3];
}
static float NSEEL_CGEN_CALL stftInit(void *opaque, INT_PTR num_param, float **parms)
{
compileContext *c = (compileContext*)opaque;
float *blocks = c->ram_state;
float *start1 = parms[0];
int32_t offs1 = (int32_t)(*start1 + NSEEL_CLOSEFACTOR);
float *start2 = parms[1];
int32_t offs2 = (int32_t)(*start2 + NSEEL_CLOSEFACTOR);
float *indexer = __NSEEL_RAMAlloc(blocks, (uint64_t)offs1);
float *stftFloatStruct = __NSEEL_RAMAlloc(blocks, (uint64_t)offs2);
int32_t *indexFw = (int32_t*)indexer;
STFT_DynInit(indexFw, stftFloatStruct);
return (float)indexFw[3];
}
void STFT_SetWnd(int32_t *indexFw, float *stftFloatStruct, float *desired_analysisWnd, float *desired_synthesisWnd)
{
int32_t i;
float *synthesisWnd = stftFloatStruct + indexFw[0];
for (i = 0; i < indexFw[0]; i++)
stftFloatStruct[i] = desired_analysisWnd[i];
for (i = 0; i < indexFw[0] - indexFw[2]; i++)
synthesisWnd[i] = desired_synthesisWnd[i] * (1.0f / indexFw[0]) * 0.5f;
}
static float NSEEL_CGEN_CALL stftSetAsymWnd(void *opaque, INT_PTR num_param, float **parms)
{
compileContext *c = (compileContext *)opaque;
float *blocks = c->ram_state;
float *start1 = parms[0];
int32_t offs1 = (int32_t)(*start1 + NSEEL_CLOSEFACTOR);
float *start2 = parms[1];
int32_t offs2 = (int32_t)(*start2 + NSEEL_CLOSEFACTOR);
float *start3 = parms[2];
int32_t offs3 = (int32_t)(*start3 + NSEEL_CLOSEFACTOR);
float *start4 = parms[3];
int32_t offs4 = (int32_t)(*start4 + NSEEL_CLOSEFACTOR);
float *indexer = __NSEEL_RAMAlloc(blocks, (uint64_t)offs1);
float *stftFloatStruct = __NSEEL_RAMAlloc(blocks, (uint64_t)offs2);
float *desired_analysisWnd = __NSEEL_RAMAlloc(blocks, (uint64_t)offs3);
float *desired_synthesisWnd = __NSEEL_RAMAlloc(blocks, (uint64_t)offs4);
int32_t *indexFw = (int32_t *)indexer;
STFT_SetWnd(indexFw, stftFloatStruct, desired_analysisWnd, desired_synthesisWnd);
return 0;
}
static float NSEEL_CGEN_CALL stftGetWindowPower(void *opaque, INT_PTR num_param, float **parms)
{
compileContext *c = (compileContext*)opaque;
float *blocks = c->ram_state;
float *start1 = parms[0];
int32_t offs1 = (int32_t)(*start1 + NSEEL_CLOSEFACTOR);
float *start2 = parms[1];
int32_t offs2 = (int32_t)(*start2 + NSEEL_CLOSEFACTOR);
float *indexer = __NSEEL_RAMAlloc(blocks, (uint64_t)offs1);
float *stftFloatStruct = __NSEEL_RAMAlloc(blocks, (uint64_t)offs2);
int32_t *indexFw = (int32_t*)indexer;
float sumOfPreWindow = 0.0f;
for (int i = 0; i < indexFw[0]; i++)
sumOfPreWindow += stftFloatStruct[i];
return 1.0f / sumOfPreWindow;
}
static float NSEEL_CGEN_CALL stftForward(void *opaque, INT_PTR num_param, float **parms)
{
compileContext *c = (compileContext*)opaque;
float *blocks = c->ram_state;
int32_t offs = (int32_t)(*parms[0] + NSEEL_CLOSEFACTOR);
float *ptr = __NSEEL_RAMAlloc(blocks, (uint64_t)offs);
int32_t offs1 = (int32_t)(*parms[1] + NSEEL_CLOSEFACTOR);
int32_t offs2 = (int32_t)(*parms[2] + NSEEL_CLOSEFACTOR);
float *indexer = __NSEEL_RAMAlloc(blocks, (uint64_t)offs1);
float *stftFloatStruct = __NSEEL_RAMAlloc(blocks, (uint64_t)offs2);
int32_t *indexFw = (int32_t*)indexer;
float *mInput = stftFloatStruct + indexFw[0] * 3;
memcpy(&mInput[indexFw[4]], ptr, indexFw[3] * sizeof(float));
indexFw[4] = (indexFw[4] + indexFw[3]) & (indexFw[0] - 1);
return (float)STFTCartesian(indexer, stftFloatStruct, ptr);
}
static float NSEEL_CGEN_CALL stftBackward(void *opaque, INT_PTR num_param, float **parms)
{
compileContext *c = (compileContext*)opaque;
float *blocks = c->ram_state;
int32_t offs = (int32_t)(*parms[0] + NSEEL_CLOSEFACTOR);
float *ptr = __NSEEL_RAMAlloc(blocks, (uint64_t)offs);
int32_t offs1 = (int32_t)(*parms[1] + NSEEL_CLOSEFACTOR);
int32_t offs2 = (int32_t)(*parms[2] + NSEEL_CLOSEFACTOR);
float *indexer = __NSEEL_RAMAlloc(blocks, (uint64_t)offs1);
float *stftFloatStruct = __NSEEL_RAMAlloc(blocks, (uint64_t)offs2);
int32_t *indexFw = (int32_t*)indexer;
int32_t ret;
ret = STFTCartesianInverse(indexer, stftFloatStruct, ptr);
memcpy(ptr, stftFloatStruct + indexFw[0] * 6, indexFw[3] * sizeof(float));
return (float)ret;
}
int32_t STFT_DynConstructor(float *indexer, int32_t fftLen, int32_t analysisOvp, float tf_res)
{
int32_t ovpSmps = fftLen / analysisOvp;
int32_t sampleShift = (fftLen - (ovpSmps << 1));
int32_t *indexFw = (int32_t*)indexer;
indexFw[0] = fftLen;
indexFw[1] = analysisOvp;
indexFw[2] = sampleShift;
indexFw[3] = ovpSmps;
indexFw[4] = 0;
indexFw[5] = (int32_t)(tf_res * (float)32767);
return ((fftLen * 6) + ovpSmps + ovpSmps + (int32_t)((float)(fftLen * sizeof(uint32_t)) / (float)(sizeof(float) / sizeof(uint32_t))));
}
static float NSEEL_CGEN_CALL stftCheckMemoryRequirement(void *opaque, INT_PTR num_param, float **parms)
{
compileContext *c = (compileContext*)opaque;
float *blocks = c->ram_state;
uint32_t offs1 = (uint32_t)(*parms[0] + NSEEL_CLOSEFACTOR);
float *indexer = __NSEEL_RAMAlloc(blocks, (uint64_t)offs1);
int32_t fftlen = (int32_t)(*parms[1] + NSEEL_CLOSEFACTOR);
int32_t analyOv = (int32_t)(*parms[2] + NSEEL_CLOSEFACTOR);
return (float)STFT_DynConstructor(indexer, fftlen, analyOv, *parms[3]);
}
static float NSEEL_CGEN_CALL stftStructSize(void *opaque)
{
return (float)(6 * sizeof(int32_t));
}
static inline double ipowp(double x, long n)
{
assert(n >= 0);
double z = 1.0;
while (n != 0)
{
if ((n & 1) != 0)
z *= x;
n >>= 1;
x *= x;
}
return z;
}
static inline void compute_transition_param(double *k, double *q, double transition)
{
assert(transition > 0);
assert(transition < 0.5);
*k = tan((1 - transition * 2) * M_PIDouble / 4);
*k *= *k;
assert(*k < 1);
assert(*k > 0);
double kksqrt = pow(1.0 - *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);
}
static inline double compute_acc_num(double q, int order, int c)
{
assert(c >= 1);
assert(c < order * 2);
int i = 0;
int j = 1;
double acc = 0;
double q_ii1;
do
{
q_ii1 = ipowp(q, i * (i + 1));
q_ii1 *= sin((i * 2 + 1) * c * M_PIDouble / order) * j;
acc += q_ii1;
j = -j;
++i;
} while (fabs(q_ii1) > 1e-100);
return acc;
}
static inline double compute_acc_den(double q, int order, int c)
{
assert(c >= 1);
assert(c < order * 2);
int i = 1;
int j = -1;
double acc = 0;
double q_i2;
do
{
q_i2 = ipowp(q, i * i);
q_i2 *= cos(i * 2 * c * M_PIDouble / order) * j;
acc += q_i2;
j = -j;
++i;
} while (fabs(q_i2) > 1e-100);
return acc;
}
static inline double compute_coef(int index, double k, double q, int order)
{
assert(index >= 0);
assert(index * 2 < order);
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;
}
static inline void compute_coefs_spec_order_tbw(double coef_arr[], 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;
// Coefficient calculation
for (int index = 0; index < nbr_coefs; ++index)
coef_arr[index] = compute_coef(index, k, q, order);
}
static inline double IIR2thOrder(double *Xi, double *b, double z[2])
{
double Yi = *b * *Xi + z[0];
z[0] = z[1];
z[1] = *b * Yi - *Xi;
return Yi;
}
typedef struct
{
unsigned int stages;
double *path, *z;
double imOld;
} IIRHilbert;
static inline void ProcessIIRHilbert(IIRHilbert *hil, double Xi, double *re, double *im)
{
double imTmp;
imTmp = *re = Xi;
for (unsigned int j = 0; j < hil->stages; j++)
{
imTmp = IIR2thOrder(&imTmp, &hil->path[j], &hil->z[2 * j]);
*re = IIR2thOrder(re, &hil->path[hil->stages + j], &hil->z[hil->stages * 2 + 2 * j]);
}
*im = hil->imOld;
hil->imOld = imTmp;
}
static float NSEEL_CGEN_CALL iirHilbertProcess(float *blocks, float *offptr, float *x, float *y)
{
uint32_t offs1 = (uint32_t)(*offptr + NSEEL_CLOSEFACTOR);
IIRHilbert *hil = (IIRHilbert *)__NSEEL_RAMAlloc(blocks, (uint64_t)offs1);
uint32_t offs2 = (uint32_t)(*y + NSEEL_CLOSEFACTOR);
float *out = (float *)__NSEEL_RAMAlloc(blocks, (uint64_t)offs2);
double re, im;
ProcessIIRHilbert(hil, *x, &re, &im);
out[0] = re;
out[1] = im;
return 2;
}
static float NSEEL_CGEN_CALL iirHilbertInit(void *opaque, INT_PTR num_param, float **parms)
{
compileContext *c = (compileContext *)opaque;
float *blocks = c->ram_state;
float *start1 = parms[0];
int32_t offs1 = (int32_t)(*start1 + NSEEL_CLOSEFACTOR);
IIRHilbert *hil = (IIRHilbert *)__NSEEL_RAMAlloc(blocks, (uint64_t)offs1);
unsigned int numStages = (unsigned int)(*parms[1] + NSEEL_CLOSEFACTOR);
float transition = *parms[2];
unsigned int numCoefs = numStages << 1;
size_t memSize = sizeof(IIRHilbert) + (numCoefs + (numStages << 2)) * sizeof(double);
hil->stages = numStages;
double *coefs = (double *)malloc(numCoefs * sizeof(double));
hil->path = (double*)(hil + 1);
hil->z = hil->path + numCoefs;
memset(hil->z, 0, (hil->stages << 2) * sizeof(double));
compute_coefs_spec_order_tbw(coefs, numCoefs, transition);
unsigned int i;
// Phase reference path coefficients
for (i = 1; i < numCoefs; i += 2)
hil->path[i >> 1] = coefs[i];
// +90 deg path coefficients
for (i = 0; i < numCoefs; i += 2)
hil->path[hil->stages + (i >> 1)] = coefs[i];
free(coefs);
hil->imOld = 0.0;
return (float)(1 + memSize / sizeof(float));
}
typedef struct
{
float data;
float maximum, minimum;
} node;
typedef struct
{
unsigned int capacity, top;
node *items;
} stack;