forked from thargor6/mb3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFFT.pas
412 lines (392 loc) · 10.1 KB
/
FFT.pas
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
unit FFT;
interface
uses TypeDefinitions;
var
FFTlength: Integer = 0;
pFFTcos, pFFTsin, pFFTreal, pFFTimag, pFFTresult: array of Double;
pFFTbitrev: array of Cardinal;
FFTMap: TLightMap;
implementation
uses Maps, DivUtils;
procedure fill0bytes(const p: Pointer; const anz: Integer; const useSSE: Boolean);
var x, offs, x4: Integer;
p1: PByte;
pc: PCardinal;
begin
p1 := p;
if useSSE then
begin
offs := 16 - (Cardinal(p1) and $0000000F);
if offs = 16 then offs := 0;
if offs <= anz then
begin
for x := 1 to offs do
begin
p1^ := 0;
Inc(p1);
end;
x4 := (anz - offs) shr 4;
if x4 > 0 then
begin
asm
push eax
push ecx
mov ecx, x4
mov eax, p1
xorps xmm0, xmm0
@loop: movaps [eax], xmm0
add eax, 16
sub ecx, 1
jnz @loop
mov p1, eax
pop ecx
pop eax
end;
offs := offs + (x4 shl 4);
end;
end
else offs := 0;
end
else
begin
offs := anz shr 2;
pc := PCardinal(p1);
for x := 1 to offs do
begin
pc^ := 0;
Inc(pc);
end;
p1 := PByte(pc);
offs := offs shl 2;
end;
for x := offs to anz - 1 do
begin
p1^ := 0;
Inc(p1);
end;
end;
function fftlaenge(const xx: Integer): Integer;
begin
Result := 1;
while Result < xx do Result := Result shl 1;
end;
function fftzlaenge(const anfang, ende: Integer): Integer;
var
xx: Integer;
begin
xx := ende - anfang + 1;
if xx < 1 then xx := 1;
Result := 1;
while Result < xx do Result := Result shl 1;
end;
procedure FFTini(Bleng: Integer);
var
x, fp1, adrbrev, adrnorm, l, Flength: Integer;
wif: Double;
begin
Flength := fftlaenge(Bleng);
if Flength <> FFTlength then
begin
fp1 := Flength + 1;
if Length(pFFTbitrev) < fp1 then
begin
SetLength(pFFTcos, fp1);
SetLength(pFFTsin, fp1);
SetLength(pFFTreal, fp1);
SetLength(pFFTimag, fp1);
SetLength(pFFTresult, fp1);
SetLength(pFFTbitrev, fp1);
end;
FFTlength := Flength;
end;
wif := 2 * Pi / FFTlength;
for x := 0 to FFTlength do
begin
pFFTsin[x] := -Sin(wif * x);
pFFTcos[x] := Cos(wif * x);
end;
pFFTbitrev[0] := 0;
adrbrev := 0;
for adrnorm := 1 to FFTlength - 1 do
begin
l := FFTlength div 2;
while adrbrev + l > FFTlength - 1 do
l := l shr 1;
adrbrev := adrbrev mod l + l;
if adrbrev >= adrnorm then
begin
pFFTbitrev[adrnorm] := adrbrev;
pFFTbitrev[adrbrev] := adrnorm;
end;
end;
end;
procedure loeschfftr;
begin
Fill0Bytes(@pFFTreal[0], (FFTlength + 1) shl 3, SupportSSE);
end;
procedure loeschffti;
begin
Fill0Bytes(@pFFTimag[0], (FFTlength + 1) shl 3, SupportSSE);
end;
procedure verdopplefftzeile;
var
x: Integer;
pdr1, pdr2, pdi1, pdi2: PDouble;
begin
pdr1 := @pfftreal[1];
pdr2 := pdouble(Cardinal(pdr1) + (FFTlength - 2) * 8);
pdi1 := @pfftimag[1];
pdi2 := pdouble(Cardinal(pdi1) + (FFTlength - 2) * 8);
for x := 1 to FFTlength div 2 do
begin
pdr2^ := pdr1^;
pdi2^ := -pdi1^;
Inc(pdr1);
Dec(pdr2);
Inc(pdi1);
Dec(pdi2);
end;
end;
procedure doFFT(const d: Double);
var
pe1, pe2, pe3, pe4: PDouble;
tabnr, l, fl2, fl3, i, j, m, ischritt: Integer;
tmpreal, tmpimag, wichreal, wichimag: Double;
begin
fl2 := FFTlength shr 1;
fl3 := fl2;
l := 1;
if SupportSSE2 then
begin
asm
push edx
push ecx
push ebx
push eax
push esi
push edi
mov ebx, pFFTreal
mov ecx, pFFTimag
movsd xmm7, d
shufpd xmm7, xmm7, 0
mov eax, l
@loo0: shl eax, 1 // while l<=fl2
mov edi, eax // war: ischritt, eax
xor eax, eax // eax=m
mov tabnr, eax
@loo1: mov edx, tabnr // for m:=0 to l-1
mov esi, pFFTcos
movlpd xmm3, [esi + edx * 8]
mov esi, pFFTsin
movlpd xmm4, [esi + edx * 8]
shufpd xmm3, xmm3, 0 // xmm3 = [wichreal, wichreal]
shufpd xmm4, xmm4, 0 // xmm4 = [wichimag, wichimag]
mulpd xmm4, xmm7 // xorpd xmm4, [sign]
mov edx, eax // edx=i=m
@loo2: mov esi, edx
add esi, l // j=i+l
movlpd xmm0, [ebx + esi * 8] // hi lo
movhpd xmm0, [ecx + esi * 8] // xmm0 = [imag, real]
movapd xmm1, xmm0
shufpd xmm1, xmm1, 1 // xmm1 = [real, imag] (,1=swap)
mulpd xmm0, xmm3 // xmm0 = [imag*wichreal, real*wichreal]
mulpd xmm1, xmm4 // xmm1 = [real*wichimag, imag*wichimag]
movapd xmm2, xmm0
addpd xmm0, xmm1 // xmm0 = [i*wr+r*wi, r*wr+i*wi]
subpd xmm2, xmm1 // xmm2 = [i*wr-r*wi, r*wr-i*wi]
shufpd xmm2, xmm0, 2 // xmm2 = [i*wr+r*wi, r*wr-i*wi]?
// tmpimag tmpreal
movlpd xmm0, [ebx + edx * 8]
movhpd xmm0, [ecx + edx * 8] // xmm0 = [imag_i, real_i]
movapd xmm1, xmm0
subpd xmm0, xmm2
addpd xmm1, xmm2
movlpd [ebx + esi * 8], xmm0
movhpd [ecx + esi * 8], xmm0
movlpd [ebx + edx * 8], xmm1
movhpd [ecx + edx * 8], xmm1
add edx, edi
cmp edx, fftlength
jl @loo2
mov esi, fl3
add tabnr, esi
add eax, 1
cmp eax, l // for m:=0 to l-1
jl @loo1
shr fl3, 1
mov eax, edi // ischritt
mov l, eax
cmp eax, fl2 // while l<=fl2
jle @loo0
pop edi
pop esi
pop eax
pop ebx
pop ecx
pop edx
end;
end
else
while l <= fl2 do
begin
ischritt := l shl 1;
tabnr := 0;
for m := 0 to l - 1 do
begin
i := m;
wichreal := pFFTcos[tabnr];
wichimag := pFFTsin[tabnr] * d;
repeat
j := i + l;
pe1 := @pFFTreal[j];
pe2 := @pFFTimag[j];
tmpreal := wichreal * pe1^ - wichimag * pe2^;
tmpimag := wichreal * pe2^ + wichimag * pe1^;
pe3 := @pFFTreal[i];
pe4 := @pFFTimag[i];
pe1^ := pe3^ - tmpreal;
pe2^ := pe4^ - tmpimag;
pe3^ := pe3^ + tmpreal;
pe4^ := pe4^ + tmpimag;
inc(i, ischritt);
until i >= FFTlength;
Inc(tabnr, fl3);
end;
l := ischritt;
fl3 := fl3 shr 1;
end;
end;
{procedure calccFFTrow_inv(row: Integer; vertical: Boolean);
var
pd1, pd2: PDouble;
x, c: Integer;
fftl1d: Double;
ps: PSingle;
pcBitrev: PCardinal;
begin
if fftlength > 0 then
begin
fftl1d := 1 / FFTlength;
verdopplefftzeile; // real gespiegelt nach oben, imag inv gespiegelt nach oben
pd1 := @pFFTresult[0];
pd2 := @pFFTreal[0];
for x := 0 to FFTlength - 1 do
begin
pd1^ := pd2^ * fftl1d;
Inc(pd1);
Inc(pd2);
end;
pd2 := @pFFTreal[0];
pcBitrev := @pFFTbitrev[0];
for x := 0 to FFTlength - 1 do // pfftreal:=bitrev[pfftergebnis]
begin
pd2^ := pFFTresult[pcBitrev^];
inc(pd2);
inc(pcBitrev);
end;
pd1 := @pFFTresult[0];
pd2 := @pFFTimag[0];
for x := 0 to FFTlength - 1 do
begin
pd1^ := pd2^ * FFTl1d;
Inc(pd1);
Inc(pd2);
end;
pd2 := @pFFTimag[0];
pcBitrev := @pFFTbitrev[0];
for x := 0 to FFTlength - 1 do
begin
pd2^ := pFFTresult[pcBitrev^];
Inc(pd2);
Inc(pcBitrev);
end;
doFFT(-1.0);
with FFTMap do
begin
if (LMWidth <> FFTlength) or (LMHeight <> FFTlength) then
begin //new Map
LMWidth := FFTlength;
LMHeight := FFTlength;
SetLength(LMa, LMWidth * LMHeight);
end;
if not vertical then
begin
ps := @LMa[row * LMWidth];
for x := 0 to FFTlength - 1 do
begin
ps^ := pFFTreal[x];
Inc(ps);
end;
end
else
begin
ps := @LMa[row];
for x := 0 to FFTlength - 1 do
begin
ps^ := pFFTreal[x];
Inc(ps, LMWidth);
end;
end;
end;
end;
end; }
// MakeNoiseMap(var Map: TLightMap; size, seed: Integer; FalloffX, FalloffY: Single);
{procedure ddFFTtoMap(var Map: TLightMap);
var ps1, ps2: PSingle;
x, y, z, xp, h1, wid, wid2: Integer;
pcBitrev, pc: pcardinal;
begin
if (Map.LMWidth <> FFTMap.LMWidth + 3) or (Map.LMHeight <> FFTMap.LMHeight + 3) then
begin
Map.LMWidth := FFTMap.LMWidth + 3;
Map.LMHeight := FFTMap.LMHeight + 3;
SetLength(Map.LMa, Map.LMWidth * Map.LMHeight);
end;
wid := FFTlength + 2;
wid2 := wid div 2;
for x := 0 to FFTMap.LMWidth - 1 do with FFTMap do
begin
ps1 := @LMa[x];
ps2 := PSingle(Integer(ps1) + wid * 2);
pcBitrev := @pFFTbitrev[0];
for y := 1 to h1 do
begin
pFFTreal[pcBitrev^] := ps1^;
pFFTimag[pcBitrev^] := ps2^;
Inc(ps1, LMWidth);
Inc(ps2, LMWidth);
Inc(pcBitrev);
end;
doFFT(-1.0);
ps1 := @Map.LMa[x];
ps2 := PSingle(Cardinal(ps1) + wid * 2);
for y := 0 to FFTMap.LMHeight - 1 do
begin
ps1^ := pFFTreal[xp];
ps2^ := pFFTimag[xp];
inc(ps1, Map.LMWidth);
inc(ps2, Map.LMWidth);
inc(xp);
end;
end;
ps1 := @p[z][0];
ps2 := psingle(Cardinal(ps1) + wid * 2);
for y := 0 to pred(height) do
begin
loeschfftr;
loeschffti;
for x := 0 to breite2 - 1 do
begin
pFFTreal[x] := ps1^;
pFFTimag[x] := ps2^;
inc(ps1);
inc(ps2);
end;
berechneFFTzeile_inv(result, z, horizontal, false, y, 0, pred(breite));
inc(ps1, breite2);
inc(ps2, breite2);
end;
end; }
Initialization
FreeLightMap(@FFTMap);
end.