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
|
/*
VASP modular - vector assembling signal processor / objects for Max/MSP and PD
Copyright (c) 2002 Thomas Grill (xovo@gmx.net)
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "license.txt," in this distribution.
*/
/*! \file vasp__ctrl.cpp
\brief Methods for handling of vector data for real, complex and multi-vector cases.
*/
#include "main.h"
#include "opbase.h"
#include "classes.h"
#include "vecblk.h"
#include "util.h"
/*! \brief Corrects for the common vector frame count
\param frms frame count to correct
\param bl new frame count
\return true if a correction was made
*/
static BL corrlen(I &frms,I bl,I bf = -1,I bo = 0)
{
if(bf < 0) bf = bl;
BL corr = false;
BL all = frms < 0;
if(all)
frms = bl;
else if(frms > bl) {
// longer than vector length -> correct
frms = bl;
corr = true;
}
if(bo+frms > bf) {
// now check if buffer size is exceeded
// post("%s - %s vector (%s) exceeds buffer size: cropped",op,bli == 0?"src":"dst",bref->Name());
frms = bf-bo;
if(frms < 0) frms = 0;
corr = true;
}
return corr;
}
inline BL corrlen(I &frms,VBuffer &b)
{
return corrlen(frms,b.Length(),b.Frames(),b.Offset());
}
/*! \brief Make real vector block for unary operations.
\param op operation name
\param src source vasp
\param dst optional destination vasp
\return struct with vector data
\remark working size is maximum common vector size
*/
RVecBlock *VaspOp::GetRVecs(const C *op,Vasp &src,Vasp *dst)
{
I nvecs = src.Vectors();
if(dst && dst->Ok() && dst->Vectors() != nvecs) {
nvecs = min(nvecs,dst->Vectors());
post("%s - src/dst vector number not equal -> taking minimum",op);
}
RVecBlock *ret = new RVecBlock(nvecs);
BL ok = true,dlens = false;
I tfrms = -1;
Vasp *vbl[2] = {&src,dst};
for(I bli = 0; bli < 2; ++bli)
for(I ci = 0; ok && ci < nvecs; ++ci) {
VBuffer *bref = NULL;
if(vbl[bli] && vbl[bli]->Ok()) {
bref = vbl[bli]->Buffer(ci);
if(!bref->Data()) {
post("%s - %s vector (%s) is invalid",op,bli == 0?"src":"dst",bref->Name());
delete bref; bref = NULL;
ok = false;
}
else
dlens = dlens || corrlen(tfrms,*bref);
}
if(bli == 0)
ret->Src(ci,bref);
else
ret->Dst(ci,bref);
}
if(dlens) post("%s - vector length has been limited to maximum common length (%i)",op,tfrms);
ret->Frames(tfrms < 0?0:tfrms);
if(ok) return ret;
else { delete ret; return NULL; }
}
/*! \brief Make real vector block for unary operations.
\param op operation name
\param src source vasp
\param dst optional destination vasp
\param full true if imaginary part is compulsory
\return struct with vector data
*/
CVecBlock *VaspOp::GetCVecs(const C *op,Vasp &src,Vasp *dst,BL full)
{
I nvecs = src.Vectors();
if(dst && dst->Ok() && dst->Vectors() != nvecs) {
nvecs = min(nvecs,dst->Vectors());
post("%s - src/dst vector number not equal -> taking minimum",op);
}
I pairs = nvecs/2;
if(nvecs != pairs*2)
if(full) {
post("%s - number of vectors is odd - not allowed",op);
return NULL;
}
else {
post("%s - number of vectors is odd - omitting last vector",op);
}
CVecBlock *ret = new CVecBlock(pairs);
BL ok = true,dlens = false;
I tfrms = -1;
Vasp *vbl[2] = {&src,dst};
for(I bli = 0; bli < 2; ++bli)
for(I ci = 0; ci < pairs; ++ci) {
VBuffer *bre = NULL,*bim = NULL; // complex channels
if(vbl[bli] && vbl[bli]->Ok()) {
const C *vnm = bli == 0?"src":"dst";
bre = vbl[bli]->Buffer(ci*2);
bim = vbl[bli]->Buffer(ci*2+1); // complex channels
if(!bre->Data()) {
post("%s - real %s vector (%s) is invalid",op,vnm,bre->Name());
delete bre; bre = NULL;
ok = false;
}
if(bim && !bim->Data()) {
post("%s - imag %s vector (%s) is invalid",op,vnm,bim->Name());
delete bim; bim = NULL;
ok = false;
}
// check against common vector length
if(bre) {
dlens = dlens || corrlen(tfrms,*bre);
}
if(bim) {
dlens = dlens || corrlen(tfrms,*bim);
}
}
if(bli == 0)
ret->Src(ci,bre,bim);
else
ret->Dst(ci,bre,bim);
}
if(dlens) post("%s - vector src/dst length has been limited to maximum common length (%i)",op,tfrms);
ret->Frames(tfrms < 0?0:tfrms);
if(ok) return ret;
else { delete ret; return NULL; }
}
/*! \brief Make real vector block for binary operations.
\param op operation name
\param src source vasp
\param arg argument vasp
\param dst optional destination vasp
\param multi 0 off/1 on/-1 auto... controls whether argument vector is single- or multi-vectored
\return struct with vector data
*/
RVecBlock *VaspOp::GetRVecs(const C *op,Vasp &src,const Vasp &arg,Vasp *dst,I multi,BL ssize)
{
if(!arg.Ok()) {
post("%s - invalid argument vasp detected and ignored",op);
return NULL;
}
I nvecs = src.Vectors();
if(dst && dst->Ok() && dst->Vectors() != nvecs) {
nvecs = min(nvecs,dst->Vectors());
post("%s - src/dst vector number not equal -> taking minimum",op);
}
RVecBlock *ret;
if(multi < 0) { // auto mode
multi = arg.Vectors() > 1;
}
if(multi) {
if(arg.Vectors() < nvecs) {
nvecs = arg.Vectors();
post("%s - too few arg vectors, operating on only first %i vectors",op,nvecs);
}
ret = new RVecBlock(nvecs,nvecs,1);
for(I i = 0; i < nvecs; ++i)
ret->Arg(i,arg.Buffer(i));
}
else {
if(arg.Vectors() > 1) {
post("%s - using only first arg vector for all operations",op);
}
ret = new RVecBlock(nvecs,nvecs,1);
for(I i = 0; i < nvecs; ++i)
ret->Arg(i,arg.Buffer(0));
}
BL ok = true,dlens = false,dalens = false;
I tfrms = -1,afrms = -1;
for(I ci = 0; ok && ci < nvecs; ++ci) {
VBuffer *bref = src.Buffer(ci);
VBuffer *barg = ret->Arg(multi?ci:0);
VBuffer *bdst = dst && dst->Ok()?dst->Buffer(ci):NULL;
if(barg && (multi || ci == 0) && !barg->Data()) {
post("%s - arg vector (%s) is invalid",op,barg->Name());
ok = false; break; // really break?
}
else if(!bref->Data()) {
post("%s - src vector (%s) is invalid",op,bref->Name());
ok = false; break; // really break?
}
// check src/dst frame lengths
dlens = dlens || corrlen(tfrms,*bref);
if(bdst) dlens = dlens || corrlen(tfrms,*bdst);
// check arg frame length
if(barg) dalens = dalens || corrlen(afrms,*barg);
ret->Src(ci,bref);
if(bdst) ret->Dst(ci,bdst);
}
if(dlens) post("%s - vector src/dst length has been limited to maximum common length (%i)",op,tfrms);
if(dalens) post("%s - vector arg length has been limited to maximum common length (%i)",op,afrms);
if(ssize) {
if(corrlen(tfrms,afrms))
post("%s - vector src/dst and arg lengths are unequal -> set to max. common length (%i)",op,tfrms);
afrms = tfrms;
}
ret->Frames(tfrms < 0?0:tfrms);
ret->ArgFrames(afrms < 0?0:afrms);
if(ok) return ret;
else { delete ret; return NULL; }
}
/*! \brief Make real complex block for binary operations.
\param op operation name
\param src source vasp
\param arg argument vasp
\param dst optional destination vasp
\param multi 0 off/1 on/-1 auto... controls whether argument vector is single- or multi-vectored
\param full true if imaginary part is compulsory
\return struct with vector data
*/
CVecBlock *VaspOp::GetCVecs(const C *op,Vasp &src,const Vasp &arg,Vasp *dst,I multi,BL ssize,BL full)
{
if(!arg.Ok()) {
post("%s - invalid argument vasp detected and ignored",op);
return NULL;
}
I nvecs = src.Vectors();
if(dst && dst->Ok() && dst->Vectors() != nvecs) {
nvecs = min(nvecs,dst->Vectors());
post("%s - src/dst vector number not equal -> taking minimum",op);
}
I pairs = nvecs/2;
CVecBlock *ret;
if(multi < 0) { // auto mode
multi = arg.Vectors() > 2; // more than one argument pair -> multi
}
if(multi) {
I apairs = arg.Vectors()/2;
if(arg.Vectors() != apairs*2)
if(full) {
post("%s - number of arg vectors is odd - not allowed",op);
return NULL;
}
else {
post("%s - number of arg vectors is odd - assuming complex part as 0",op);
++apairs;
}
if(apairs < pairs) {
pairs = apairs;
post("%s - too few arg vectors, operating on only first %i vector pairs",op,pairs);
}
ret = new CVecBlock(pairs,pairs,1);
for(I i = 0; i < pairs; ++i)
ret->Arg(i,arg.Buffer(i*2),arg.Buffer(i*2+1));
}
else {
if(arg.Vectors() > 2) {
post("%s - using only first arg vector pair for all operations",op);
}
ret = new CVecBlock(pairs,pairs,1);
for(I i = 0; i < pairs; ++i)
ret->Arg(i,arg.Buffer(0),arg.Buffer(1));
}
BL ok = true,dlens = false,dalens = false;
I tfrms = -1,afrms = -1;
{
if(nvecs != pairs*2) {
post("%s - number of src vectors is odd - omitting last vector",op);
// clear superfluous vector?
}
for(I ci = 0; ok && ci < pairs; ++ci) {
// --- arg stuff ----------------
VBuffer *brarg = ret->ReArg(ci),*biarg = ret->ImArg(ci);
if(multi || ci == 0) {
if(!brarg->Data()) {
post("%s - real arg vector (%s) is invalid",op,brarg->Name());
ok = false; break;
}
else if(biarg && !biarg->Data()) {
post("%s - imag arg vector (%s) is invalid",op,biarg->Name());
ok = false; break;
}
}
// check against common arg length
if(brarg) dalens = dalens || corrlen(afrms,*brarg);
if(biarg) dalens = dalens || corrlen(afrms,*biarg);
// --- src/dst stuff ----------------
VBuffer *brref = src.Buffer(ci*2),*biref = src.Buffer(ci*2+1);
VBuffer *brdst,*bidst;
if(dst && dst->Ok()) brdst = dst->Buffer(ci*2),bidst = dst->Buffer(ci*2+1);
else brdst = bidst = NULL;
if(!brref->Data()) {
post("%s - real src vector (%s) is invalid",op,brref->Name());
ok = false; break; // really break?
}
else if(biref && !biref->Data()) {
post("%s - imag src vector (%s) is invalid",op,biref->Name());
ok = false; break; // really break?
}
else {
dlens = dlens || corrlen(tfrms,*brref);
if(biref) dlens = dlens || corrlen(tfrms,*biref);
if(brdst) dlens = dlens || corrlen(tfrms,*brdst);
if(bidst) dlens = dlens || corrlen(tfrms,*bidst);
}
ret->Src(ci,brref,biref);
if(brdst) ret->Dst(ci,brdst,bidst);
}
}
if(dlens) post("%s - vector src/dst length has been limited to maximum common length (%i)",op,tfrms);
if(dalens) post("%s - vector arg length has been limited to maximum common length (%i)",op,afrms);
if(ssize) {
if(corrlen(tfrms,afrms))
post("%s - vector src/dst and arg lengths are unequal -> set to max. common length (%i)",op,tfrms);
afrms = tfrms;
}
ret->Frames(tfrms < 0?0:tfrms);
ret->ArgFrames(afrms < 0?0:afrms);
if(ok) return ret;
else { delete ret; return NULL; }
}
/*! \brief Run the operation on the various real vectors.
\param vecs src/arg/dst vector block
\param fun operative function
\param p parameter block for operative function
\return normalized vasp or NULL on error
\todo set overlap flag
\remark operative function must be capable of handling reversed direction
*/
Vasp *VaspOp::DoOp(RVecBlock *vecs,VecOp::opfun *fun,OpParam &p,BL symm)
{
BL ok = true;
if(vecs->ArgBlks() && (!p.arg || p.args < vecs->ArgBlks())) {
post("%s - not enough argument blocks",p.opname);
ok = false;
}
const I scnt = symm?2:1;
for(I i = 0; ok && i < vecs->Vecs(); ++i)
for(I si = 0; ok && si < scnt; ++si) {
p.frames = vecs->Frames();
VBuffer *s = vecs->Src(i),*d = vecs->Dst(i);
p.rsdt = s->Pointer(),p.rss = s->Channels();
if(d) p.rddt = d->Pointer(),p.rds = d->Channels();
else p.rddt = p.rsdt,p.rds = p.rss;
for(I bi = 0; bi < vecs->ArgBlks(); ++bi) {
VBuffer *a = vecs->Arg(i,bi);
p.arg[bi].SetV(a?a->Pointer():NULL,a?a->Channels():0);
}
if(!symm)
p.symm = -1;
else {
const I hcnt = p.frames/2;
p.oddrem = p.frames != 2*hcnt;
if((p.symm = si) == 0) {
p.frames = hcnt+(p.oddrem?1:0);
}
else {
const I r = p.frames-hcnt;
p.frames = hcnt;
p.rsdt += r*p.rss,p.rddt += r*p.rds;
// What to do with arguments in symmetric mode?
// let the object decide!!
}
}
{ // ---- Check out and try to resolve overlap situation ------------
BL sovr = p.SR_In(); // check whether dst is before src
if(p.HasArg()) {
// has argument
if(sovr) {
// src/dst needs reversal -> check if ok for arg/dst
p.ovrlap = true;
if(p.AR_Can())
p.R_Rev(); // Revert vectors
else {
post("%s - vector overlap situation can't be resolved",p.opname);
ok = false;
}
}
else if(p.AR_In()) {
// arg/dst needs reversal -> check if ok for src/dst
p.ovrlap = true;
if(p.SR_Can())
p.R_Rev(); // Revert vectors
else {
post("%s - vector overlap situation can't be resolved",p.opname);
ok = false;
}
}
}
else { // No arg
if(sovr) {
p.ovrlap = true;
p.R_Rev(); // if overlapping revert vectors
}
else
p.ovrlap = p.SR_Ovr();
}
}
ok = fun(p);
#ifdef FLEXT_THREAD
flext_base::ThrYield();
#endif
}
return ok?vecs->ResVasp():NULL;
}
/*! \brief Run the operation on the various complex vector pairs.
\param vecs src/arg/dst vector block
\param fun operative function
\param p parameter block for operative function
\return normalized vasp or NULL on error
\todo set overlap flag
\remark operative function must be capable of handling reversed direction
*/
Vasp *VaspOp::DoOp(CVecBlock *vecs,VecOp::opfun *fun,OpParam &p,BL symm)
{
BL ok = true;
if(vecs->ArgBlks() && (!p.arg || p.args < vecs->ArgBlks())) {
post("%s - not enough argument blocks",p.opname);
ok = false;
}
const I scnt = symm?2:1;
for(I i = 0; ok && i < vecs->Pairs(); ++i)
for(I si = 0; ok && si < scnt; ++si) {
p.frames = vecs->Frames();
VBuffer *rsv = vecs->ReSrc(i),*isv = vecs->ImSrc(i);
p.rsdt = rsv->Pointer(),p.rss = rsv->Channels();
p.isdt = isv->Pointer(),p.iss = isv->Channels();
VBuffer *rdv = vecs->ReDst(i),*idv = vecs->ImDst(i);
if(rdv) {
p.rddt = rdv->Pointer(),p.rds = rdv->Channels();
if(idv) p.iddt = idv->Pointer(),p.ids = idv->Channels();
else p.iddt = NULL; //,p.ids = 0; // Can that be NULL??
}
else {
p.rddt = p.rsdt,p.rds = p.rss,p.iddt = p.isdt,p.ids = p.iss;
}
for(I bi = 0; bi < vecs->ArgBlks(); ++bi) {
VBuffer *rav = vecs->ReArg(i,bi),*iav = vecs->ImArg(i,bi);
p.arg[bi].SetV(rav?rav->Pointer():NULL,rav?rav->Channels():0,iav?iav->Pointer():NULL,iav?iav->Channels():0);
}
if(!symm)
p.symm = -1;
else {
const I hcnt = p.frames/2;
p.oddrem = p.frames != 2*hcnt;
if((p.symm = si) == 0) {
p.frames = hcnt+(p.oddrem?1:0);
}
else {
const I r = p.frames-hcnt;
p.frames = hcnt;
p.rsdt += r*p.rss,p.isdt += r*p.iss;
p.rddt += r*p.rds;
if(p.iddt) p.iddt += r*p.ids; // Can that be NULL??
// What to do with arguments?
// let objects decide!!
}
}
{ // ---- Check out and try to resolve overlap situation ------------
BL sovr = p.SR_In(); // check whether dst is before src
if(sovr && !p.SI_Can()) {
post("%s - src/dst overlap of re/im vectors not resolvable",p.opname);
ok = false;
}
if(ok && p.HasArg()) {
// has argument
if(sovr) {
// src/dst needs reversal -> check if ok for arg/dst
p.ovrlap = true;
if(p.AR_Can() && p.AI_Can())
p.C_Rev(); // Revert vectors
else {
post("%s - vector overlap situation can't be resolved",p.opname);
ok = false;
}
}
else if(p.AR_In() || p.AI_In()) {
// arg/dst needs reversal -> check if ok for src/dst
p.ovrlap = true;
if(p.AR_Can() && p.AI_Can() && p.SR_Can() && p.SI_Can())
p.C_Rev(); // Revert vectors
else {
post("%s - vector overlap situation can't be resolved",p.opname);
ok = false;
}
}
}
else { // No arg
if(sovr) {
p.ovrlap = true;
p.C_Rev(); // if overlapping revert vectors
}
else
p.ovrlap = p.SR_Ovr() || p.SI_Ovr();
}
}
ok = fun(p);
#ifdef FLEXT_THREAD
flext_base::ThrYield();
#endif
}
return ok?vecs->ResVasp():NULL;
}
|