/[svn]/linuxsampler/trunk/src/scriptvm/CoreVMFunctions.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/scriptvm/CoreVMFunctions.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3595 - (show annotations) (download)
Tue Sep 3 11:06:33 2019 UTC (4 years, 6 months ago) by schoenebeck
File size: 58806 byte(s)
* Fixed compiler errors with GCC 8.x.
* Bumped version (2.1.1.svn16).

1 /*
2 * Copyright (c) 2014-2019 Christian Schoenebeck
3 *
4 * http://www.linuxsampler.org
5 *
6 * This file is part of LinuxSampler and released under the same terms.
7 * See README file for details.
8 */
9
10 #include "CoreVMFunctions.h"
11
12 #include <iostream>
13 #include <algorithm> // for std::sort()
14 #include <math.h>
15 #include <stdlib.h>
16 #include "tree.h"
17 #include "ScriptVM.h"
18 #include "../common/RTMath.h"
19
20 namespace LinuxSampler {
21
22 inline bool _fEqualX(vmfloat a, vmfloat b) {
23 if (sizeof(vmfloat) == sizeof(float))
24 return RTMath::fEqual32(a, b);
25 else
26 return RTMath::fEqual64(a, b);
27 }
28
29 ///////////////////////////////////////////////////////////////////////////
30 // class VMEmptyResultFunction
31
32 VMFnResult* VMEmptyResultFunction::errorResult() {
33 result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED);
34 return &result;
35 }
36
37 VMFnResult* VMEmptyResultFunction::successResult() {
38 result.flags = STMT_SUCCESS;
39 return &result;
40 }
41
42 ///////////////////////////////////////////////////////////////////////////
43 // class VMIntResultFunction
44
45 VMFnResult* VMIntResultFunction::errorResult(vmint i) {
46 result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED);
47 result.value = i;
48 result.unitPrefixFactor = VM_NO_FACTOR;
49 return &result;
50 }
51
52 VMFnResult* VMIntResultFunction::successResult(vmint i) {
53 result.flags = STMT_SUCCESS;
54 result.value = i;
55 result.unitPrefixFactor = VM_NO_FACTOR;
56 return &result;
57 }
58
59 VMFnResult* VMIntResultFunction::errorResult(VMIntFnResDef res) {
60 result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED);
61 result.value = res.value;
62 result.unitPrefixFactor = res.unitFactor;
63 return &result;
64 }
65
66 VMFnResult* VMIntResultFunction::successResult(VMIntFnResDef res) {
67 result.flags = STMT_SUCCESS;
68 result.value = res.value;
69 result.unitPrefixFactor = res.unitFactor;
70 return &result;
71 }
72
73 ///////////////////////////////////////////////////////////////////////////
74 // class VMRealResultFunction
75
76 VMFnResult* VMRealResultFunction::errorResult(vmfloat f) {
77 result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED);
78 result.value = f;
79 result.unitPrefixFactor = VM_NO_FACTOR;
80 return &result;
81 }
82
83 VMFnResult* VMRealResultFunction::errorResult(VMRealFnResDef res) {
84 result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED);
85 result.value = res.value;
86 result.unitPrefixFactor = res.unitFactor;
87 return &result;
88 }
89
90 VMFnResult* VMRealResultFunction::successResult(vmfloat f) {
91 result.flags = STMT_SUCCESS;
92 result.value = f;
93 result.unitPrefixFactor = VM_NO_FACTOR;
94 return &result;
95 }
96
97 VMFnResult* VMRealResultFunction::successResult(VMRealFnResDef res) {
98 result.flags = STMT_SUCCESS;
99 result.value = res.value;
100 result.unitPrefixFactor = res.unitFactor;
101 return &result;
102 }
103
104 ///////////////////////////////////////////////////////////////////////////
105 // class VMStringResultFunction
106
107 VMFnResult* VMStringResultFunction::errorResult(const String& s) {
108 result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED);
109 result.value = s;
110 return &result;
111 }
112
113 VMFnResult* VMStringResultFunction::successResult(const String& s) {
114 result.flags = STMT_SUCCESS;
115 result.value = s;
116 return &result;
117 }
118
119 ///////////////////////////////////////////////////////////////////////////
120 // class VMNumberResultFunction
121
122 VMFnResult* VMNumberResultFunction::errorResult(vmint i) {
123 intResult.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED);
124 intResult.value = i;
125 intResult.unitPrefixFactor = VM_NO_FACTOR;
126 return &intResult;
127 }
128
129 VMFnResult* VMNumberResultFunction::errorResult(vmfloat f) {
130 realResult.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED);
131 realResult.value = f;
132 realResult.unitPrefixFactor = VM_NO_FACTOR;
133 return &realResult;
134 }
135
136 VMFnResult* VMNumberResultFunction::successResult(vmint i) {
137 intResult.flags = STMT_SUCCESS;
138 intResult.value = i;
139 intResult.unitPrefixFactor = VM_NO_FACTOR;
140 return &intResult;
141 }
142
143 VMFnResult* VMNumberResultFunction::successResult(vmfloat f) {
144 realResult.flags = STMT_SUCCESS;
145 realResult.value = f;
146 realResult.unitPrefixFactor = VM_NO_FACTOR;
147 return &realResult;
148 }
149
150 VMFnResult* VMNumberResultFunction::errorIntResult(VMIntFnResDef res) {
151 intResult.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED);
152 intResult.value = res.value;
153 intResult.unitPrefixFactor = res.unitFactor;
154 return &intResult;
155 }
156
157 VMFnResult* VMNumberResultFunction::errorRealResult(VMRealFnResDef res) {
158 realResult.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED);
159 realResult.value = res.value;
160 realResult.unitPrefixFactor = res.unitFactor;
161 return &realResult;
162 }
163
164 VMFnResult* VMNumberResultFunction::successIntResult(VMIntFnResDef res) {
165 intResult.flags = STMT_SUCCESS;
166 intResult.value = res.value;
167 intResult.unitPrefixFactor = res.unitFactor;
168 return &intResult;
169 }
170
171 VMFnResult* VMNumberResultFunction::successRealResult(VMRealFnResDef res) {
172 realResult.flags = STMT_SUCCESS;
173 realResult.value = res.value;
174 realResult.unitPrefixFactor = res.unitFactor;
175 return &realResult;
176 }
177
178 ///////////////////////////////////////////////////////////////////////////
179 // built-in script function: message()
180
181 bool CoreVMFunction_message::acceptsArgType(vmint iArg, ExprType_t type) const {
182 return type == INT_EXPR || type == REAL_EXPR || type == STRING_EXPR;
183 }
184
185 VMFnResult* CoreVMFunction_message::exec(VMFnArgs* args) {
186 if (!args->argsCount()) return errorResult();
187
188 uint64_t usecs = RTMath::unsafeMicroSeconds(RTMath::real_clock);
189
190 VMStringExpr* strExpr = dynamic_cast<VMStringExpr*>(args->arg(0));
191 if (strExpr) {
192 printf("[ScriptVM %.3f] %s\n", usecs/1000000.f, strExpr->evalStr().c_str());
193 return successResult();
194 }
195
196 VMRealExpr* realExpr = dynamic_cast<VMRealExpr*>(args->arg(0));
197 if (realExpr) {
198 printf("[ScriptVM %.3f] %f\n", usecs/1000000.f, realExpr->evalReal());
199 return successResult();
200 }
201
202 VMIntExpr* intExpr = dynamic_cast<VMIntExpr*>(args->arg(0));
203 if (intExpr) {
204 printf("[ScriptVM %.3f] %lld\n", usecs/1000000.f, (int64_t)intExpr->evalInt());
205 return successResult();
206 }
207
208 return errorResult();
209 }
210
211 ///////////////////////////////////////////////////////////////////////////
212 // built-in script function: exit()
213
214 vmint CoreVMFunction_exit::maxAllowedArgs() const {
215 return (vm->isExitResultEnabled()) ? 1 : 0;
216 }
217
218 bool CoreVMFunction_exit::acceptsArgType(vmint iArg, ExprType_t type) const {
219 if (!vm->isExitResultEnabled()) return false;
220 return type == INT_EXPR || type == REAL_EXPR || type == STRING_EXPR;
221 }
222
223 bool CoreVMFunction_exit::acceptsArgUnitType(vmint iArg, StdUnit_t type) const {
224 if (!vm->isExitResultEnabled()) return false;
225 return true;
226 }
227
228 bool CoreVMFunction_exit::acceptsArgUnitPrefix(vmint iArg, StdUnit_t type) const {
229 if (!vm->isExitResultEnabled()) return false;
230 return true;
231 }
232 bool CoreVMFunction_exit::acceptsArgFinal(vmint iArg) const {
233 if (!vm->isExitResultEnabled()) return false;
234 return true;
235 }
236
237 VMFnResult* CoreVMFunction_exit::exec(VMFnArgs* args) {
238 this->result.flags = STMT_ABORT_SIGNALLED;
239 if (vm->isExitResultEnabled() && args->argsCount()) {
240 ExecContext* ctx = dynamic_cast<ExecContext*>(vm->currentVMExecContext());
241 switch (args->arg(0)->exprType()) {
242 case INT_EXPR: {
243 VMIntExpr* expr = args->arg(0)->asInt();
244 ctx->exitRes.intLiteral = IntLiteral({
245 .value = expr->evalInt(),
246 .unitFactor = expr->unitFactor(),
247 .unitType = expr->unitType(),
248 .isFinal = expr->isFinal()
249 });
250 ctx->exitRes.value = &ctx->exitRes.intLiteral;
251 break;
252 }
253 case REAL_EXPR: {
254 VMRealExpr* expr = args->arg(0)->asReal();
255 ctx->exitRes.realLiteral = RealLiteral({
256 .value = expr->evalReal(),
257 .unitFactor = expr->unitFactor(),
258 .unitType = expr->unitType(),
259 .isFinal = expr->isFinal()
260 });
261 ctx->exitRes.value = &ctx->exitRes.realLiteral;
262 break;
263 }
264 case STRING_EXPR:
265 ctx->exitRes.stringLiteral = StringLiteral(
266 args->arg(0)->asString()->evalStr()
267 );
268 ctx->exitRes.value = &ctx->exitRes.stringLiteral;
269 break;
270 default:
271 ; // noop - just to shut up the compiler
272 }
273 }
274 return &result;
275 }
276
277 ///////////////////////////////////////////////////////////////////////////
278 // built-in script function: wait()
279
280 bool CoreVMFunction_wait::acceptsArgType(vmint iArg, ExprType_t type) const {
281 return type == INT_EXPR || type == REAL_EXPR;
282 }
283
284 bool CoreVMFunction_wait::acceptsArgUnitType(vmint iArg, StdUnit_t type) const {
285 return type == VM_NO_UNIT || type == VM_SECOND;
286 }
287
288 bool CoreVMFunction_wait::acceptsArgUnitPrefix(vmint iArg, StdUnit_t type) const {
289 return type == VM_SECOND; // only allow metric prefix(es) if 'seconds' is used as unit type
290 }
291
292 VMFnResult* CoreVMFunction_wait::exec(VMFnArgs* args) {
293 ExecContext* ctx = dynamic_cast<ExecContext*>(vm->currentVMExecContext());
294 VMNumberExpr* expr = args->arg(0)->asNumber();
295 StdUnit_t unit = expr->unitType();
296 vmint us = (unit) ? expr->evalCastInt(VM_MICRO) : expr->evalCastInt();
297 if (us < 0) {
298 wrnMsg("wait(): argument may not be negative! Aborting script!");
299 this->result.flags = STMT_ABORT_SIGNALLED;
300 } else if (us == 0) {
301 wrnMsg("wait(): argument may not be zero! Aborting script!");
302 this->result.flags = STMT_ABORT_SIGNALLED;
303 } else {
304 ctx->suspendMicroseconds = us;
305 this->result.flags = STMT_SUSPEND_SIGNALLED;
306 }
307 return &result;
308 }
309
310 ///////////////////////////////////////////////////////////////////////////
311 // built-in script function: abs()
312
313 ExprType_t CoreVMFunction_abs::returnType(VMFnArgs* args) {
314 return args->arg(0)->exprType();
315 }
316
317 StdUnit_t CoreVMFunction_abs::returnUnitType(VMFnArgs* args) {
318 return args->arg(0)->asNumber()->unitType();
319 }
320
321 bool CoreVMFunction_abs::returnsFinal(VMFnArgs* args) {
322 return args->arg(0)->asNumber()->isFinal();
323 }
324
325 bool CoreVMFunction_abs::acceptsArgType(vmint iArg, ExprType_t type) const {
326 return type == INT_EXPR || type == REAL_EXPR;
327 }
328
329 VMFnResult* CoreVMFunction_abs::exec(VMFnArgs* args) {
330 VMExpr* arg = args->arg(0);
331 if (arg->exprType() == REAL_EXPR) {
332 VMRealExpr* expr = arg->asReal();
333 return successRealResult({
334 .value = ::fabs(expr->evalReal()),
335 .unitFactor = expr->unitFactor()
336 });
337 } else {
338 VMIntExpr* expr = arg->asInt();
339 return successIntResult({
340 .value = ::abs(expr->evalInt()),
341 .unitFactor = expr->unitFactor()
342 });
343 }
344 }
345
346 ///////////////////////////////////////////////////////////////////////////
347 // built-in script function: random()
348
349 ExprType_t CoreVMFunction_random::returnType(VMFnArgs* args) {
350 return (args->arg(0)->exprType() == INT_EXPR &&
351 args->arg(1)->exprType() == INT_EXPR) ? INT_EXPR : REAL_EXPR;
352 }
353
354 StdUnit_t CoreVMFunction_random::returnUnitType(VMFnArgs* args) {
355 // we ensure in checkArgs() below (which is called before this method here)
356 // that both arguments must be of same unit type, so either one is fine here
357 return args->arg(0)->asNumber()->unitType();
358 }
359
360 bool CoreVMFunction_random::returnsFinal(VMFnArgs* args) {
361 return args->arg(0)->asNumber()->isFinal() ||
362 args->arg(1)->asNumber()->isFinal();
363 }
364
365 bool CoreVMFunction_random::acceptsArgType(vmint iArg, ExprType_t type) const {
366 return type == INT_EXPR || type == REAL_EXPR;
367 }
368
369 void CoreVMFunction_random::checkArgs(VMFnArgs* args,
370 std::function<void(String)> err,
371 std::function<void(String)> wrn)
372 {
373 // super class checks
374 Super::checkArgs(args, err, wrn);
375
376 // own checks ...
377 if (args->arg(0)->asNumber()->unitType() !=
378 args->arg(1)->asNumber()->unitType())
379 {
380 String a = unitTypeStr(args->arg(0)->asNumber()->unitType());
381 String b = unitTypeStr(args->arg(1)->asNumber()->unitType());
382 err("Argument 1 has unit type " + a + ", whereas argument 2 has unit type " + b + ".");
383 return;
384 }
385 if (args->arg(0)->asNumber()->isFinal() !=
386 args->arg(1)->asNumber()->isFinal())
387 {
388 String a = args->arg(0)->asNumber()->isFinal() ? "'final'" : "not 'final'";
389 String b = args->arg(1)->asNumber()->isFinal() ? "'final'" : "not 'final'";
390 wrn("Argument 1 is " + a + ", whereas argument 2 is " + b + ", function result will be final.");
391 }
392 }
393
394 VMFnResult* CoreVMFunction_random::exec(VMFnArgs* args) {
395 float f = float(::rand()) / float(RAND_MAX);
396
397 VMNumberExpr* arg0 = args->arg(0)->asNumber();
398 VMNumberExpr* arg1 = args->arg(1)->asNumber();
399
400 if (arg0->exprType() == INT_EXPR && arg1->exprType() == INT_EXPR) {
401 vmint iMin = args->arg(0)->asInt()->evalInt();
402 vmint iMax = args->arg(1)->asInt()->evalInt();
403 if (arg0->unitFactor() == arg1->unitFactor()) {
404 return successIntResult({
405 .value = vmint( iMin + roundf( f * float(iMax - iMin) ) ),
406 .unitFactor = arg0->unitFactor()
407 });
408 } else if (arg0->unitFactor() < arg1->unitFactor()) {
409 iMax = Unit::convIntToUnitFactor(iMax, arg1, arg0);
410 return successIntResult({
411 .value = vmint( iMin + roundf( f * float(iMax - iMin) ) ),
412 .unitFactor = arg0->unitFactor()
413 });
414 } else { // arg0->unitFactor() > arg1->unitFactor() ...
415 iMin = Unit::convIntToUnitFactor(iMin, arg0, arg1);
416 return successIntResult({
417 .value = vmint( iMin + roundf( f * float(iMax - iMin) ) ),
418 .unitFactor = arg1->unitFactor()
419 });
420 }
421 } else {
422 vmfloat fMin = arg0->evalCastReal();
423 vmfloat fMax = arg1->evalCastReal();
424 if (arg0->unitFactor() == arg1->unitFactor()) {
425 return successRealResult({
426 .value = fMin + f * (fMax - fMin),
427 .unitFactor = arg0->unitFactor()
428 });
429 } else if (arg0->unitFactor() < arg1->unitFactor()) {
430 fMax = Unit::convRealToUnitFactor(fMax, arg1, arg0);
431 return successRealResult({
432 .value = fMin + f * (fMax - fMin),
433 .unitFactor = arg0->unitFactor()
434 });
435 } else { // arg0->unitFactor() > arg1->unitFactor() ...
436 fMin = Unit::convRealToUnitFactor(fMin, arg0, arg1);
437 return successRealResult({
438 .value = fMin + f * (fMax - fMin),
439 .unitFactor = arg1->unitFactor()
440 });
441 }
442 }
443 }
444
445 ///////////////////////////////////////////////////////////////////////////
446 // built-in script function: num_elements()
447
448 bool CoreVMFunction_num_elements::acceptsArgType(vmint iArg, ExprType_t type) const {
449 return isArray(type);
450 }
451
452 VMFnResult* CoreVMFunction_num_elements::exec(VMFnArgs* args) {
453 return successResult( args->arg(0)->asArray()->arraySize() );
454 }
455
456 ///////////////////////////////////////////////////////////////////////////
457 // built-in script function: inc()
458
459 StdUnit_t CoreVMFunction_inc::returnUnitType(VMFnArgs* args) {
460 return args->arg(0)->asNumber()->unitType();
461 }
462
463 bool CoreVMFunction_inc::returnsFinal(VMFnArgs* args) {
464 return args->arg(0)->asNumber()->isFinal();
465 }
466
467 void CoreVMFunction_inc::checkArgs(VMFnArgs* args,
468 std::function<void(String)> err,
469 std::function<void(String)> wrn)
470 {
471 // super class checks
472 Super::checkArgs(args, err, wrn);
473
474 // own checks ...
475 if (args->arg(0)->asNumber()->unitType()) {
476 String unitType = unitTypeStr(args->arg(0)->asNumber()->unitType());
477 wrn("Argument has a unit type (" + unitType + "), only the number before the unit will be incremented by one.");
478 }
479 }
480
481 VMFnResult* CoreVMFunction_inc::exec(VMFnArgs* args) {
482 VMExpr* arg = args->arg(0);
483 VMIntExpr* in = dynamic_cast<VMIntExpr*>(arg);
484 VMVariable* out = dynamic_cast<VMVariable*>(arg);
485 vmint i = in->evalInt() + 1;
486 IntLiteral tmp({
487 .value = i,
488 .unitFactor = in->unitFactor()
489 });
490 out->assignExpr(&tmp);
491 return successResult({
492 .value = i,
493 .unitFactor = in->unitFactor()
494 });
495 }
496
497 ///////////////////////////////////////////////////////////////////////////
498 // built-in script function: dec()
499
500 StdUnit_t CoreVMFunction_dec::returnUnitType(VMFnArgs* args) {
501 return args->arg(0)->asNumber()->unitType();
502 }
503
504 bool CoreVMFunction_dec::returnsFinal(VMFnArgs* args) {
505 return args->arg(0)->asNumber()->isFinal();
506 }
507
508 void CoreVMFunction_dec::checkArgs(VMFnArgs* args,
509 std::function<void(String)> err,
510 std::function<void(String)> wrn)
511 {
512 // super class checks
513 Super::checkArgs(args, err, wrn);
514
515 // own checks ...
516 if (args->arg(0)->asNumber()->unitType()) {
517 String unitType = unitTypeStr(args->arg(0)->asNumber()->unitType());
518 wrn("Argument has a unit type (" + unitType + "), only the number before the unit will be decremented by one.");
519 }
520 }
521
522 VMFnResult* CoreVMFunction_dec::exec(VMFnArgs* args) {
523 VMExpr* arg = args->arg(0);
524 VMIntExpr* in = dynamic_cast<VMIntExpr*>(arg);
525 VMVariable* out = dynamic_cast<VMVariable*>(arg);
526 vmint i = in->evalInt() - 1;
527 IntLiteral tmp({
528 .value = i,
529 .unitFactor = in->unitFactor()
530 });
531 out->assignExpr(&tmp);
532 return successResult({
533 .value = i,
534 .unitFactor = in->unitFactor()
535 });
536 }
537
538 ///////////////////////////////////////////////////////////////////////////
539 // built-in script function: in_range()
540
541 bool CoreVMFunction_in_range::acceptsArgType(vmint iArg, ExprType_t type) const {
542 return type == INT_EXPR || type == REAL_EXPR;
543 }
544
545 void CoreVMFunction_in_range::checkArgs(VMFnArgs* args,
546 std::function<void(String)> err,
547 std::function<void(String)> wrn)
548 {
549 // super class checks
550 Super::checkArgs(args, err, wrn);
551
552 // own checks ...
553 if (args->arg(0)->asNumber()->unitType() !=
554 args->arg(1)->asNumber()->unitType() ||
555 args->arg(1)->asNumber()->unitType() !=
556 args->arg(2)->asNumber()->unitType())
557 {
558 String a = unitTypeStr(args->arg(0)->asNumber()->unitType());
559 String b = unitTypeStr(args->arg(1)->asNumber()->unitType());
560 String c = unitTypeStr(args->arg(2)->asNumber()->unitType());
561 err("Arguments must all have same unit, however argument 1 is " + a +
562 ", argument 2 is " + b + ", argument 3 is " + c + ".");
563 return;
564 }
565 if (args->arg(0)->exprType() != args->arg(1)->exprType() ||
566 args->arg(1)->exprType() != args->arg(2)->exprType())
567 {
568 String a = typeStr(args->arg(0)->exprType());
569 String b = typeStr(args->arg(1)->exprType());
570 String c = typeStr(args->arg(2)->exprType());
571 String r = typeStr(REAL_EXPR);
572 wrn("Argument 1 is " + a + ", argument 2 is " + b +
573 ", argument 3 is " + c + ", function result will be " + r + ".");
574 }
575 }
576
577 template<class T>
578 inline void _swapByValue(T& a, T& b) {
579 T tmp = a;
580 a = b;
581 b = tmp;
582 }
583
584 VMFnResult* CoreVMFunction_in_range::exec(VMFnArgs* args) {
585 VMNumberExpr* argNeedle = args->arg(0)->asNumber();
586 VMNumberExpr* argLo = args->arg(1)->asNumber();
587 VMNumberExpr* argHi = args->arg(2)->asNumber();
588
589 vmfloat needle = argNeedle->evalCastReal();
590 vmfloat lo = argLo->evalCastReal();
591 vmfloat hi = argHi->evalCastReal();
592
593 needle *= argNeedle->unitFactor();
594 lo *= argLo->unitFactor();
595 hi *= argHi->unitFactor();
596
597 if (lo > hi) _swapByValue(lo, hi);
598
599 return successResult(needle >= lo && needle <= hi);
600 }
601
602 ///////////////////////////////////////////////////////////////////////////
603 // built-in script function: sh_left()
604
605 bool CoreVMFunction_sh_left::returnsFinal(VMFnArgs* args) {
606 return args->arg(0)->asNumber()->isFinal();
607 }
608
609 VMFnResult* CoreVMFunction_sh_left::exec(VMFnArgs* args) {
610 vmint i = args->arg(0)->asInt()->evalInt();
611 vmint n = args->arg(1)->asInt()->evalInt();
612 return successResult(i << n);
613 }
614
615 ///////////////////////////////////////////////////////////////////////////
616 // built-in script function: sh_right()
617
618 bool CoreVMFunction_sh_right::returnsFinal(VMFnArgs* args) {
619 return args->arg(0)->asNumber()->isFinal();
620 }
621
622 VMFnResult* CoreVMFunction_sh_right::exec(VMFnArgs* args) {
623 vmint i = args->arg(0)->asInt()->evalInt();
624 vmint n = args->arg(1)->asInt()->evalInt();
625 return successResult(i >> n);
626 }
627
628 ///////////////////////////////////////////////////////////////////////////
629 // built-in script function: min()
630
631 ExprType_t CoreVMFunction_min::returnType(VMFnArgs* args) {
632 return (args->arg(0)->exprType() == REAL_EXPR ||
633 args->arg(1)->exprType() == REAL_EXPR) ? REAL_EXPR : INT_EXPR;
634 }
635
636 StdUnit_t CoreVMFunction_min::returnUnitType(VMFnArgs* args) {
637 return args->arg(0)->asNumber()->unitType();
638 }
639
640 bool CoreVMFunction_min::returnsFinal(VMFnArgs* args) {
641 return args->arg(0)->asNumber()->isFinal() ||
642 args->arg(1)->asNumber()->isFinal();
643 }
644
645 bool CoreVMFunction_min::acceptsArgType(vmint iArg, ExprType_t type) const {
646 return type == INT_EXPR || type == REAL_EXPR;
647 }
648
649 void CoreVMFunction_min::checkArgs(VMFnArgs* args,
650 std::function<void(String)> err,
651 std::function<void(String)> wrn)
652 {
653 // super class checks
654 Super::checkArgs(args, err, wrn);
655
656 // own checks ...
657 if (args->arg(0)->asNumber()->unitType() !=
658 args->arg(1)->asNumber()->unitType())
659 {
660 String a = unitTypeStr(args->arg(0)->asNumber()->unitType());
661 String b = unitTypeStr(args->arg(1)->asNumber()->unitType());
662 err("Argument 1 has unit type " + a + ", whereas argument 2 has unit type " + b + ".");
663 return;
664 }
665 if (args->arg(0)->exprType() != args->arg(1)->exprType()) {
666 String a = typeStr(args->arg(0)->exprType());
667 String b = typeStr(args->arg(1)->exprType());
668 String c = typeStr(REAL_EXPR);
669 wrn("Argument 1 is " + a + ", whereas argument 2 is " + b + ", function result will be " + c + ".");
670 return;
671 }
672 if (args->arg(0)->asNumber()->isFinal() !=
673 args->arg(1)->asNumber()->isFinal())
674 {
675 String a = args->arg(0)->asNumber()->isFinal() ? "'final'" : "not 'final'";
676 String b = args->arg(1)->asNumber()->isFinal() ? "'final'" : "not 'final'";
677 wrn("Argument 1 is " + a + ", whereas argument 2 is " + b + ", function result will be final.");
678 }
679 }
680
681 VMFnResult* CoreVMFunction_min::exec(VMFnArgs* args) {
682 VMNumberExpr* lhs = args->arg(0)->asNumber();
683 VMNumberExpr* rhs = args->arg(1)->asNumber();
684 if (lhs->exprType() == REAL_EXPR && rhs->exprType() == REAL_EXPR) {
685 vmfloat lm = lhs->asReal()->evalReal();
686 vmfloat rm = rhs->asReal()->evalReal();
687 vmfloat lprod = lm * lhs->unitFactor();
688 vmfloat rprod = rm * rhs->unitFactor();
689 return successRealResult({
690 .value = (lprod < rprod) ? lm : rm,
691 .unitFactor = (lprod < rprod) ? lhs->unitFactor() : rhs->unitFactor()
692 });
693 } else if (lhs->exprType() == REAL_EXPR && rhs->exprType() == INT_EXPR) {
694 vmfloat lm = lhs->asReal()->evalReal();
695 vmint rm = rhs->asInt()->evalInt();
696 vmfloat lprod = lm * lhs->unitFactor();
697 vmfloat rprod = rm * rhs->unitFactor();
698 return successRealResult({
699 .value = (lprod < rprod) ? lm : rm,
700 .unitFactor = (lprod < rprod) ? lhs->unitFactor() : rhs->unitFactor()
701 });
702 } else if (lhs->exprType() == INT_EXPR && rhs->exprType() == REAL_EXPR) {
703 vmint lm = lhs->asInt()->evalInt();
704 vmfloat rm = rhs->asReal()->evalReal();
705 vmfloat lprod = lm * lhs->unitFactor();
706 vmfloat rprod = rm * rhs->unitFactor();
707 return successRealResult({
708 .value = (lprod < rprod) ? lm : rm,
709 .unitFactor = (lprod < rprod) ? lhs->unitFactor() : rhs->unitFactor()
710 });
711 } else {
712 vmint lm = lhs->asInt()->evalInt();
713 vmint rm = rhs->asInt()->evalInt();
714 vmfloat lprod = lm * lhs->unitFactor();
715 vmfloat rprod = rm * rhs->unitFactor();
716 return successIntResult({
717 .value = (lprod < rprod) ? lm : rm,
718 .unitFactor = (lprod < rprod) ? lhs->unitFactor() : rhs->unitFactor()
719 });
720 }
721 }
722
723 ///////////////////////////////////////////////////////////////////////////
724 // built-in script function: max()
725
726 ExprType_t CoreVMFunction_max::returnType(VMFnArgs* args) {
727 return (args->arg(0)->exprType() == REAL_EXPR ||
728 args->arg(1)->exprType() == REAL_EXPR) ? REAL_EXPR : INT_EXPR;
729 }
730
731 StdUnit_t CoreVMFunction_max::returnUnitType(VMFnArgs* args) {
732 return args->arg(0)->asNumber()->unitType();
733 }
734
735 bool CoreVMFunction_max::returnsFinal(VMFnArgs* args) {
736 return args->arg(0)->asNumber()->isFinal() ||
737 args->arg(1)->asNumber()->isFinal();
738 }
739
740 bool CoreVMFunction_max::acceptsArgType(vmint iArg, ExprType_t type) const {
741 return type == INT_EXPR || type == REAL_EXPR;
742 }
743
744 void CoreVMFunction_max::checkArgs(VMFnArgs* args,
745 std::function<void(String)> err,
746 std::function<void(String)> wrn)
747 {
748 // super class checks
749 Super::checkArgs(args, err, wrn);
750
751 // own checks ...
752 if (args->arg(0)->asNumber()->unitType() !=
753 args->arg(1)->asNumber()->unitType())
754 {
755 String a = unitTypeStr(args->arg(0)->asNumber()->unitType());
756 String b = unitTypeStr(args->arg(1)->asNumber()->unitType());
757 err("Argument 1 has unit type " + a + ", whereas argument 2 has unit type " + b + ".");
758 return;
759 }
760 if (args->arg(0)->exprType() != args->arg(1)->exprType()) {
761 String a = typeStr(args->arg(0)->exprType());
762 String b = typeStr(args->arg(1)->exprType());
763 String c = typeStr(REAL_EXPR);
764 wrn("Argument 1 is " + a + ", whereas argument 2 is " + b + ", function result will be " + c + ".");
765 return;
766 }
767 if (args->arg(0)->asNumber()->isFinal() !=
768 args->arg(1)->asNumber()->isFinal())
769 {
770 String a = args->arg(0)->asNumber()->isFinal() ? "'final'" : "not 'final'";
771 String b = args->arg(1)->asNumber()->isFinal() ? "'final'" : "not 'final'";
772 wrn("Argument 1 is " + a + ", whereas argument 2 is " + b + ", function result will be final.");
773 }
774 }
775
776 VMFnResult* CoreVMFunction_max::exec(VMFnArgs* args) {
777 VMNumberExpr* lhs = args->arg(0)->asNumber();
778 VMNumberExpr* rhs = args->arg(1)->asNumber();
779 if (lhs->exprType() == REAL_EXPR && rhs->exprType() == REAL_EXPR) {
780 vmfloat lm = lhs->asReal()->evalReal();
781 vmfloat rm = rhs->asReal()->evalReal();
782 vmfloat lprod = lm * lhs->unitFactor();
783 vmfloat rprod = rm * rhs->unitFactor();
784 return successRealResult({
785 .value = (lprod > rprod) ? lm : rm,
786 .unitFactor = (lprod > rprod) ? lhs->unitFactor() : rhs->unitFactor()
787 });
788 } else if (lhs->exprType() == REAL_EXPR && rhs->exprType() == INT_EXPR) {
789 vmfloat lm = lhs->asReal()->evalReal();
790 vmint rm = rhs->asInt()->evalInt();
791 vmfloat lprod = lm * lhs->unitFactor();
792 vmfloat rprod = rm * rhs->unitFactor();
793 return successRealResult({
794 .value = (lprod > rprod) ? lm : rm,
795 .unitFactor = (lprod > rprod) ? lhs->unitFactor() : rhs->unitFactor()
796 });
797 } else if (lhs->exprType() == INT_EXPR && rhs->exprType() == REAL_EXPR) {
798 vmint lm = lhs->asInt()->evalInt();
799 vmfloat rm = rhs->asReal()->evalReal();
800 vmfloat lprod = lm * lhs->unitFactor();
801 vmfloat rprod = rm * rhs->unitFactor();
802 return successRealResult({
803 .value = (lprod > rprod) ? lm : rm,
804 .unitFactor = (lprod > rprod) ? lhs->unitFactor() : rhs->unitFactor()
805 });
806 } else {
807 vmint lm = lhs->asInt()->evalInt();
808 vmint rm = rhs->asInt()->evalInt();
809 vmfloat lprod = lm * lhs->unitFactor();
810 vmfloat rprod = rm * rhs->unitFactor();
811 return successIntResult({
812 .value = (lprod > rprod) ? lm : rm,
813 .unitFactor = (lprod > rprod) ? lhs->unitFactor() : rhs->unitFactor()
814 });
815 }
816 }
817
818 ///////////////////////////////////////////////////////////////////////////
819 // built-in script function: array_equal()
820
821 bool CoreVMFunction_array_equal::acceptsArgType(vmint iArg, ExprType_t type) const {
822 return isArray(type);
823 }
824
825 void CoreVMFunction_array_equal::checkArgs(VMFnArgs* args,
826 std::function<void(String)> err,
827 std::function<void(String)> wrn)
828 {
829 // super class checks
830 Super::checkArgs(args, err, wrn);
831
832 // own checks ...
833 if (args->arg(0)->exprType() != args->arg(1)->exprType()) {
834 String a = typeStr(args->arg(0)->exprType());
835 String b = typeStr(args->arg(1)->exprType());
836 err("Argument 1 is " + a + ", whereas argument 2 is " + b + ".");
837 return;
838 }
839 if (args->arg(0)->asArray()->arraySize() !=
840 args->arg(1)->asArray()->arraySize())
841 {
842 wrn("Result of function call is always false, since the passed two arrays were declared with different array sizes.");
843 }
844 }
845
846 VMFnResult* CoreVMFunction_array_equal::exec(VMFnArgs* args) {
847 VMArrayExpr* l = args->arg(0)->asArray();
848 VMArrayExpr* r = args->arg(1)->asArray();
849 if (l->arraySize() != r->arraySize()) {
850 //wrnMsg("array_equal(): the two arrays differ in size");
851 return successResult(0); // false
852 }
853 const vmint n = l->arraySize();
854 // checkArgs() above ensured that we either have INT_ARR_EXPR on both sides
855 // or REAL_ARR_EXPR on both sides, so we can simplify here (a bit)
856 if (l->exprType() == INT_ARR_EXPR) {
857 VMIntArrayExpr* lia = l->asIntArray();
858 VMIntArrayExpr* ria = r->asIntArray();
859 for (vmint i = 0; i < n; ++i) {
860 vmint lvalue = lia->evalIntElement(i);
861 vmint rvalue = ria->evalIntElement(i);
862 vmfloat lfactor = lia->unitFactorOfElement(i);
863 vmfloat rfactor = ria->unitFactorOfElement(i);
864 if (lfactor == rfactor) {
865 if (lvalue != rvalue)
866 return successResult(0); // false
867 else
868 continue;
869 }
870 if (lfactor < rfactor) {
871 if (lvalue != Unit::convIntToUnitFactor(rvalue, rfactor, lfactor))
872 return successResult(0); // false
873 else
874 continue;
875 } else {
876 if (rvalue != Unit::convIntToUnitFactor(lvalue, lfactor, rfactor))
877 return successResult(0); // false
878 else
879 continue;
880 }
881 }
882 } else {
883 VMRealArrayExpr* lra = l->asRealArray();
884 VMRealArrayExpr* rra = r->asRealArray();
885 for (vmint i = 0; i < n; ++i) {
886 vmfloat lvalue = lra->evalRealElement(i);
887 vmfloat rvalue = rra->evalRealElement(i);
888 vmfloat lfactor = lra->unitFactorOfElement(i);
889 vmfloat rfactor = rra->unitFactorOfElement(i);
890 if (lfactor == rfactor) {
891 if (!_fEqualX(lvalue, rvalue))
892 return successResult(0); // false
893 else
894 continue;
895 }
896 if (lfactor < rfactor) {
897 if (!_fEqualX(lvalue, Unit::convRealToUnitFactor(rvalue, rfactor, lfactor)))
898 return successResult(0); // false
899 else
900 continue;
901 } else {
902 if (!_fEqualX(rvalue, Unit::convRealToUnitFactor(lvalue, lfactor, rfactor)))
903 return successResult(0); // false
904 else
905 continue;
906 }
907 }
908 }
909 return successResult(1); // true
910 }
911
912 ///////////////////////////////////////////////////////////////////////////
913 // built-in script function: search()
914
915 bool CoreVMFunction_search::acceptsArgType(vmint iArg, ExprType_t type) const {
916 if (iArg == 0)
917 return isArray(type);
918 else
919 return type == INT_EXPR || type == REAL_EXPR;
920 }
921
922 void CoreVMFunction_search::checkArgs(VMFnArgs* args,
923 std::function<void(String)> err,
924 std::function<void(String)> wrn)
925 {
926 // super class checks
927 Super::checkArgs(args, err, wrn);
928
929 // own checks ...
930 if (args->arg(0)->exprType() == INT_ARR_EXPR &&
931 args->arg(1)->exprType() != INT_EXPR)
932 {
933 String a = typeStr(INT_ARR_EXPR);
934 String bIs = typeStr(args->arg(1)->exprType());
935 String bShould = typeStr(INT_EXPR);
936 err("Argument 1 is " + a + ", hence argument 2 should be " + bShould + ", is " + bIs + " though.");
937 return;
938 }
939 if (args->arg(0)->exprType() == REAL_ARR_EXPR &&
940 args->arg(1)->exprType() != REAL_EXPR)
941 {
942 String a = typeStr(REAL_ARR_EXPR);
943 String bIs = typeStr(args->arg(1)->exprType());
944 String bShould = typeStr(REAL_EXPR);
945 err("Argument 1 is " + a + ", hence argument 2 should be " + bShould + ", is " + bIs + " though.");
946 return;
947 }
948 }
949
950 VMFnResult* CoreVMFunction_search::exec(VMFnArgs* args) {
951 VMArrayExpr* a = args->arg(0)->asArray();
952 const vmint n = a->arraySize();
953 if (a->exprType() == INT_ARR_EXPR) {
954 const vmint needle = args->arg(1)->asInt()->evalInt();
955 VMIntArrayExpr* intArray = a->asIntArray();
956 for (vmint i = 0; i < n; ++i)
957 if (intArray->evalIntElement(i) == needle)
958 return successResult(i);
959 } else { // real array ...
960 const vmfloat needle = args->arg(1)->asReal()->evalReal();
961 VMRealArrayExpr* realArray = a->asRealArray();
962 for (vmint i = 0; i < n; ++i) {
963 const vmfloat value = realArray->evalRealElement(i);
964 if (_fEqualX(value, needle))
965 return successResult(i);
966 }
967 }
968 return successResult(-1); // not found
969 }
970
971 ///////////////////////////////////////////////////////////////////////////
972 // built-in script function: sort()
973
974 bool CoreVMFunction_sort::acceptsArgType(vmint iArg, ExprType_t type) const {
975 if (iArg == 0)
976 return isArray(type);
977 else
978 return type == INT_EXPR;
979 }
980
981 // The following structs and template classes act as adapters for allowing to
982 // use std sort algorithms on our arrays. It might look a bit more complicated
983 // than it ought to be, but there is one reason for the large amount of
984 // 'adapter' code below: the STL std algorithms rely on 'lvalues' to do their
985 // (e.g. sorting) jobs, that is they expect containers to have 'localizeable'
986 // data which essentially means their data should reside somewhere in memory and
987 // directly be accessible (readable and writable) there, which is not the case
988 // with our VM interfaces which actually always require virtual getter and
989 // setter methods to be called instead. So we must emulate lvalues by custom
990 // classes/structs which forward between our getters/setters and the lvalue
991 // access operators used by the STL std algorithms.
992
993 struct IntArrayAccessor {
994 static inline vmint getPrimaryValue(VMIntArrayExpr* arr, vmint index) {
995 return arr->evalIntElement(index);
996 }
997 static inline void setPrimaryValue(VMIntArrayExpr* arr, vmint index, vmint value) {
998 arr->assignIntElement(index, value);
999 }
1000 };
1001
1002 struct RealArrayAccessor {
1003 static inline vmfloat getPrimaryValue(VMRealArrayExpr* arr, vmint index) {
1004 return arr->evalRealElement(index);
1005 }
1006 static inline void setPrimaryValue(VMRealArrayExpr* arr, vmint index, vmfloat value) {
1007 arr->assignRealElement(index, value);
1008 }
1009 };
1010
1011 template<class T_array> // i.e. T_array is either VMIntArrayExpr or VMRealArrayExpr
1012 struct ArrElemPOD {
1013 T_array* m_array;
1014 vmint m_index;
1015 };
1016
1017 // This class is used for temporary values by std::sort().
1018 template<class T_value> // i.e. T_value is either vmint or vmfloat
1019 struct ScalarNmbrVal {
1020 T_value primValue;
1021 vmfloat unitFactor;
1022
1023 inline bool operator<(const ScalarNmbrVal& other) const {
1024 return getProdValue() < other.getProdValue();
1025 }
1026 inline bool operator>(const ScalarNmbrVal& other) const {
1027 return getProdValue() > other.getProdValue();
1028 }
1029 inline vmfloat getProdValue() const {
1030 // simple solution for both vmint and vmfloat, should be fine for just sorting
1031 return primValue * unitFactor;
1032 }
1033 };
1034
1035 // This class emulates lvalue access (access by reference) which is used by ArrExprIter::operator*() below.
1036 template<class T_array, // T_array is either VMIntArrayExpr or VMRealArrayExpr
1037 class T_value, // T_value is either vmint or vmfloat
1038 class T_accessor> // T_accessor is either IntArrayAccessor or RealArrayAccessor
1039 class ArrElemRef : protected ArrElemPOD<T_array> {
1040 public:
1041 typedef ::LinuxSampler::ScalarNmbrVal<T_value> ScalarNmbrVal; // GCC 8.x requires this very detailed form of typedef (that is ::LinuxSampler:: as prefix), IMO a GCC bug
1042
1043 inline ArrElemRef(T_array* a, vmint index) {
1044 this->m_array = a;
1045 this->m_index = index;
1046 }
1047 inline ArrElemRef(const ArrElemRef& ref) {
1048 this->m_array = ref.m_array;
1049 this->m_index = ref.m_index;
1050 }
1051 inline ArrElemRef& operator=(const ArrElemRef& e) {
1052 setPrimValue(e.getPrimValue());
1053 setUnitFactor(e.getUnitFactor());
1054 return *this;
1055 }
1056 inline ArrElemRef& operator=(ScalarNmbrVal value) {
1057 setPrimValue(value.primValue);
1058 setUnitFactor(value.unitFactor);
1059 return *this;
1060 }
1061 inline bool operator==(const ArrElemRef& e) const {
1062 return getProdValue() == e.getProdValue();
1063 }
1064 inline bool operator!=(const ArrElemRef& e) const {
1065 return !(operator==(e));
1066 }
1067 inline bool operator<(const ArrElemRef& e) const {
1068 return getProdValue() < e.getProdValue();
1069 }
1070 inline bool operator>(const ArrElemRef& e) const {
1071 return getProdValue() > e.getProdValue();
1072 }
1073 inline bool operator<=(const ArrElemRef& e) const {
1074 return getProdValue() <= e.getProdValue();
1075 }
1076 inline bool operator>=(const ArrElemRef& e) const {
1077 return getProdValue() >= e.getProdValue();
1078 }
1079 inline bool operator==(const ScalarNmbrVal& s) const {
1080 return getProdValue() == s.getProdValue();
1081 }
1082 inline bool operator!=(const ScalarNmbrVal& s) const {
1083 return !(operator==(s));
1084 }
1085 inline bool operator<(const ScalarNmbrVal& s) const {
1086 return getProdValue() < s.getProdValue();
1087 }
1088 inline bool operator>(const ScalarNmbrVal& s) const {
1089 return getProdValue() > s.getProdValue();
1090 }
1091 inline bool operator<=(const ScalarNmbrVal& s) const {
1092 return getProdValue() <= s.getProdValue();
1093 }
1094 inline bool operator>=(const ScalarNmbrVal& s) const {
1095 return getProdValue() >= s.getProdValue();
1096 }
1097 inline operator ScalarNmbrVal() {
1098 return {
1099 .primValue = getPrimValue() ,
1100 .unitFactor = getUnitFactor()
1101 };
1102 }
1103 protected:
1104 inline T_value getPrimValue() const {
1105 return T_accessor::getPrimaryValue( this->m_array, this->m_index );
1106 }
1107 inline void setPrimValue(T_value value) {
1108 T_accessor::setPrimaryValue( this->m_array, this->m_index, value );
1109 }
1110 inline vmfloat getUnitFactor() const {
1111 return this->m_array->unitFactorOfElement(this->m_index);
1112 }
1113 inline void setUnitFactor(vmfloat factor) {
1114 this->m_array->assignElementUnitFactor(this->m_index, factor);
1115 }
1116 inline vmfloat getProdValue() const {
1117 // simple solution for both vmint and vmfloat, should be fine for just sorting
1118 vmfloat primary = (vmfloat) getPrimValue();
1119 vmfloat factor = getUnitFactor();
1120 return primary * factor;
1121 }
1122
1123 // allow swap() functions below to access protected methods here
1124 friend void swap(class ArrElemRef<T_array,T_value,T_accessor> a,
1125 class ArrElemRef<T_array,T_value,T_accessor> b);
1126 };
1127
1128 // custom iterator class to be used by std:sort() on our VM arrays
1129 template<class T_array, class T_value, class T_accessor>
1130 class ArrExprIter : public ArrElemPOD<T_array> {
1131 public:
1132 typedef std::random_access_iterator_tag iterator_category;
1133 typedef ssize_t difference_type;
1134 typedef ::LinuxSampler::ArrElemRef<T_array, T_value, T_accessor> ArrElemRef; // GCC 8.x requires this very detailed form of typedef (that is ::LinuxSampler:: as prefix), IMO a GCC bug
1135 typedef ArrElemRef reference; // type used by STL for access by reference
1136 typedef void pointer; // type used by STL for -> operator result, we don't use that operator at all so just void it
1137 typedef ScalarNmbrVal<T_value> value_type; // type used by STL for temporary values
1138
1139 ArrExprIter(T_array* a, vmint index) {
1140 this->m_array = a;
1141 this->m_index = index;
1142 }
1143 ArrExprIter(const ArrElemRef& ref) {
1144 this->m_array = ref.m_array;
1145 this->m_index = ref.m_index;
1146 }
1147 inline ArrElemRef operator*() {
1148 return ArrElemRef(this->m_array, this->m_index);
1149 }
1150 inline ArrExprIter& operator++() { // prefix increment
1151 ++(this->m_index);
1152 return *this;
1153 }
1154 inline ArrExprIter& operator--() { // prefix decrement
1155 --(this->m_index);
1156 return *this;
1157 }
1158 inline ArrExprIter operator++(int) { // postfix increment
1159 ArrExprIter it = *this;
1160 ++(this->m_index);
1161 return it;
1162 }
1163 inline ArrExprIter operator--(int) { // postfix decrement
1164 ArrExprIter it = *this;
1165 --(this->m_index);
1166 return it;
1167 }
1168 inline ArrExprIter& operator+=(difference_type d) {
1169 this->m_index += d;
1170 return *this;
1171 }
1172 inline ArrExprIter& operator-=(difference_type d) {
1173 this->m_index -= d;
1174 return *this;
1175 }
1176 inline bool operator==(const ArrExprIter& other) const {
1177 return this->m_index == other.m_index;
1178 }
1179 inline bool operator!=(const ArrExprIter& other) const {
1180 return this->m_index != other.m_index;
1181 }
1182 inline bool operator<(const ArrExprIter& other) const {
1183 return this->m_index < other.m_index;
1184 }
1185 inline bool operator>(const ArrExprIter& other) const {
1186 return this->m_index > other.m_index;
1187 }
1188 inline bool operator<=(const ArrExprIter& other) const {
1189 return this->m_index <= other.m_index;
1190 }
1191 inline bool operator>=(const ArrExprIter& other) const {
1192 return this->m_index >= other.m_index;
1193 }
1194 inline difference_type operator+(const ArrExprIter& other) const {
1195 return this->m_index + other.m_index;
1196 }
1197 inline difference_type operator-(const ArrExprIter& other) const {
1198 return this->m_index - other.m_index;
1199 }
1200 inline ArrExprIter operator-(difference_type d) const {
1201 return ArrExprIter(this->m_array, this->m_index - d);
1202 }
1203 inline ArrExprIter operator+(difference_type d) const {
1204 return ArrExprIter(this->m_array, this->m_index + d);
1205 }
1206 inline ArrExprIter operator*(difference_type factor) const {
1207 return ArrExprIter(this->m_array, this->m_index * factor);
1208 }
1209 };
1210
1211 typedef ArrExprIter<VMIntArrayExpr,vmint,IntArrayAccessor> IntArrExprIter;
1212 typedef ArrExprIter<VMRealArrayExpr,vmfloat,RealArrayAccessor> RealArrExprIter;
1213
1214 // intentionally not a template function to avoid potential clashes with other (i.e. system's) swap() functions
1215 inline void swap(IntArrExprIter::ArrElemRef a,
1216 IntArrExprIter::ArrElemRef b)
1217 {
1218 vmint valueA = a.getPrimValue();
1219 vmint valueB = b.getPrimValue();
1220 vmfloat factorA = a.getUnitFactor();
1221 vmfloat factorB = b.getUnitFactor();
1222 a.setPrimValue(valueB);
1223 a.setUnitFactor(factorB);
1224 b.setPrimValue(valueA);
1225 b.setUnitFactor(factorA);
1226 }
1227
1228 // intentionally not a template function to avoid potential clashes with other (i.e. system's) swap() functions
1229 inline void swap(RealArrExprIter::ArrElemRef a,
1230 RealArrExprIter::ArrElemRef b)
1231 {
1232 vmfloat valueA = a.getPrimValue();
1233 vmfloat valueB = b.getPrimValue();
1234 vmfloat factorA = a.getUnitFactor();
1235 vmfloat factorB = b.getUnitFactor();
1236 a.setPrimValue(valueB);
1237 a.setUnitFactor(factorB);
1238 b.setPrimValue(valueA);
1239 b.setUnitFactor(factorA);
1240 }
1241
1242 // used to sort in descending order (unlike the default behaviour of std::sort() which is ascending order)
1243 template<class T> // T is either IntArrExprIter or RealArrExprIter
1244 struct DescArrExprSorter {
1245 inline bool operator()(const typename T::value_type a, const typename T::value_type b) const {
1246 return a > b;
1247 }
1248 };
1249
1250 VMFnResult* CoreVMFunction_sort::exec(VMFnArgs* args) {
1251 const bool bAscending =
1252 (args->argsCount() < 2) ? true : !args->arg(1)->asInt()->evalInt();
1253
1254 if (args->arg(0)->exprType() == INT_ARR_EXPR) {
1255 VMIntArrayExpr* a = args->arg(0)->asIntArray();
1256 vmint n = a->arraySize();
1257 IntArrExprIter itBegin(a, 0);
1258 IntArrExprIter itEnd(a, n);
1259 if (bAscending) {
1260 std::sort(itBegin, itEnd);
1261 } else {
1262 DescArrExprSorter<IntArrExprIter> sorter;
1263 std::sort(itBegin, itEnd, sorter);
1264 }
1265 } else {
1266 VMRealArrayExpr* a = args->arg(0)->asRealArray();
1267 vmint n = a->arraySize();
1268 RealArrExprIter itBegin(a, 0);
1269 RealArrExprIter itEnd(a, n);
1270 if (bAscending) {
1271 std::sort(itBegin, itEnd);
1272 } else {
1273 DescArrExprSorter<RealArrExprIter> sorter;
1274 std::sort(itBegin, itEnd, sorter);
1275 }
1276 }
1277
1278 return successResult();
1279 }
1280
1281 ///////////////////////////////////////////////////////////////////////////
1282 // built-in script function: real_to_int() and int()
1283
1284 StdUnit_t CoreVMFunction_real_to_int::returnUnitType(VMFnArgs* args) {
1285 return args->arg(0)->asNumber()->unitType();
1286 }
1287
1288 bool CoreVMFunction_real_to_int::returnsFinal(VMFnArgs* args) {
1289 return args->arg(0)->asNumber()->isFinal();
1290 }
1291
1292 VMFnResult* CoreVMFunction_real_to_int::exec(VMFnArgs* args) {
1293 VMRealExpr* realExpr = args->arg(0)->asReal();
1294 vmfloat f = realExpr->evalReal();
1295 return successResult({
1296 .value = vmint(f),
1297 .unitFactor = realExpr->unitFactor()
1298 });
1299 }
1300
1301 ///////////////////////////////////////////////////////////////////////////
1302 // built-in script function: int_to_real() and real()
1303
1304 StdUnit_t CoreVMFunction_int_to_real::returnUnitType(VMFnArgs* args) {
1305 return args->arg(0)->asNumber()->unitType();
1306 }
1307
1308 bool CoreVMFunction_int_to_real::returnsFinal(VMFnArgs* args) {
1309 return args->arg(0)->asNumber()->isFinal();
1310 }
1311
1312 VMFnResult* CoreVMFunction_int_to_real::exec(VMFnArgs* args) {
1313 VMIntExpr* intExpr = args->arg(0)->asInt();
1314 vmint i = intExpr->evalInt();
1315 return successResult({
1316 .value = vmfloat(i),
1317 .unitFactor = intExpr->unitFactor()
1318 });
1319 }
1320
1321 ///////////////////////////////////////////////////////////////////////////
1322 // built-in script function: round()
1323
1324 StdUnit_t CoreVMFunction_round::returnUnitType(VMFnArgs* args) {
1325 return args->arg(0)->asNumber()->unitType();
1326 }
1327
1328 bool CoreVMFunction_round::returnsFinal(VMFnArgs* args) {
1329 return args->arg(0)->asNumber()->isFinal();
1330 }
1331
1332 VMFnResult* CoreVMFunction_round::exec(VMFnArgs* args) {
1333 VMRealExpr* realExpr = args->arg(0)->asReal();
1334 vmfloat f = realExpr->evalReal();
1335 if (sizeof(vmfloat) == sizeof(float))
1336 f = ::roundf(f);
1337 else
1338 f = ::round(f);
1339 return successResult({
1340 .value = f,
1341 .unitFactor = realExpr->unitFactor()
1342 });
1343 }
1344
1345 ///////////////////////////////////////////////////////////////////////////
1346 // built-in script function: ceil()
1347
1348 StdUnit_t CoreVMFunction_ceil::returnUnitType(VMFnArgs* args) {
1349 return args->arg(0)->asNumber()->unitType();
1350 }
1351
1352 bool CoreVMFunction_ceil::returnsFinal(VMFnArgs* args) {
1353 return args->arg(0)->asNumber()->isFinal();
1354 }
1355
1356 VMFnResult* CoreVMFunction_ceil::exec(VMFnArgs* args) {
1357 VMRealExpr* realExpr = args->arg(0)->asReal();
1358 vmfloat f = realExpr->evalReal();
1359 if (sizeof(vmfloat) == sizeof(float))
1360 f = ::ceilf(f);
1361 else
1362 f = ::ceil(f);
1363 return successResult({
1364 .value = f,
1365 .unitFactor = realExpr->unitFactor()
1366 });
1367 }
1368
1369 ///////////////////////////////////////////////////////////////////////////
1370 // built-in script function: floor()
1371
1372 StdUnit_t CoreVMFunction_floor::returnUnitType(VMFnArgs* args) {
1373 return args->arg(0)->asNumber()->unitType();
1374 }
1375
1376 bool CoreVMFunction_floor::returnsFinal(VMFnArgs* args) {
1377 return args->arg(0)->asNumber()->isFinal();
1378 }
1379
1380 VMFnResult* CoreVMFunction_floor::exec(VMFnArgs* args) {
1381 VMRealExpr* realExpr = args->arg(0)->asReal();
1382 vmfloat f = realExpr->evalReal();
1383 if (sizeof(vmfloat) == sizeof(float))
1384 f = ::floorf(f);
1385 else
1386 f = ::floor(f);
1387 return successResult({
1388 .value = f,
1389 .unitFactor = realExpr->unitFactor()
1390 });
1391 }
1392
1393 ///////////////////////////////////////////////////////////////////////////
1394 // built-in script function: sqrt()
1395
1396 StdUnit_t CoreVMFunction_sqrt::returnUnitType(VMFnArgs* args) {
1397 return args->arg(0)->asNumber()->unitType();
1398 }
1399
1400 bool CoreVMFunction_sqrt::returnsFinal(VMFnArgs* args) {
1401 return args->arg(0)->asNumber()->isFinal();
1402 }
1403
1404 VMFnResult* CoreVMFunction_sqrt::exec(VMFnArgs* args) {
1405 VMRealExpr* realExpr = args->arg(0)->asReal();
1406 vmfloat f = realExpr->evalReal();
1407 if (sizeof(vmfloat) == sizeof(float))
1408 f = ::sqrtf(f);
1409 else
1410 f = ::sqrt(f);
1411 return successResult({
1412 .value = f,
1413 .unitFactor = realExpr->unitFactor()
1414 });
1415 }
1416
1417 ///////////////////////////////////////////////////////////////////////////
1418 // built-in script function: log()
1419
1420 StdUnit_t CoreVMFunction_log::returnUnitType(VMFnArgs* args) {
1421 return args->arg(0)->asNumber()->unitType();
1422 }
1423
1424 bool CoreVMFunction_log::returnsFinal(VMFnArgs* args) {
1425 return args->arg(0)->asNumber()->isFinal();
1426 }
1427
1428 VMFnResult* CoreVMFunction_log::exec(VMFnArgs* args) {
1429 VMRealExpr* realExpr = args->arg(0)->asReal();
1430 vmfloat f = realExpr->evalReal();
1431 if (sizeof(vmfloat) == sizeof(float))
1432 f = ::logf(f);
1433 else
1434 f = ::log(f);
1435 return successResult({
1436 .value = f,
1437 .unitFactor = realExpr->unitFactor()
1438 });
1439 }
1440
1441 ///////////////////////////////////////////////////////////////////////////
1442 // built-in script function: log2()
1443
1444 StdUnit_t CoreVMFunction_log2::returnUnitType(VMFnArgs* args) {
1445 return args->arg(0)->asNumber()->unitType();
1446 }
1447
1448 bool CoreVMFunction_log2::returnsFinal(VMFnArgs* args) {
1449 return args->arg(0)->asNumber()->isFinal();
1450 }
1451
1452 VMFnResult* CoreVMFunction_log2::exec(VMFnArgs* args) {
1453 VMRealExpr* realExpr = args->arg(0)->asReal();
1454 vmfloat f = realExpr->evalReal();
1455 if (sizeof(vmfloat) == sizeof(float))
1456 f = ::log2f(f);
1457 else
1458 f = ::log2(f);
1459 return successResult({
1460 .value = f,
1461 .unitFactor = realExpr->unitFactor()
1462 });
1463 }
1464
1465 ///////////////////////////////////////////////////////////////////////////
1466 // built-in script function: log10()
1467
1468 StdUnit_t CoreVMFunction_log10::returnUnitType(VMFnArgs* args) {
1469 return args->arg(0)->asNumber()->unitType();
1470 }
1471
1472 bool CoreVMFunction_log10::returnsFinal(VMFnArgs* args) {
1473 return args->arg(0)->asNumber()->isFinal();
1474 }
1475
1476 VMFnResult* CoreVMFunction_log10::exec(VMFnArgs* args) {
1477 VMRealExpr* realExpr = args->arg(0)->asReal();
1478 vmfloat f = realExpr->evalReal();
1479 if (sizeof(vmfloat) == sizeof(float))
1480 f = ::log10f(f);
1481 else
1482 f = ::log10(f);
1483 return successResult({
1484 .value = f,
1485 .unitFactor = realExpr->unitFactor()
1486 });
1487 }
1488
1489 ///////////////////////////////////////////////////////////////////////////
1490 // built-in script function: exp()
1491
1492 StdUnit_t CoreVMFunction_exp::returnUnitType(VMFnArgs* args) {
1493 return args->arg(0)->asNumber()->unitType();
1494 }
1495
1496 bool CoreVMFunction_exp::returnsFinal(VMFnArgs* args) {
1497 return args->arg(0)->asNumber()->isFinal();
1498 }
1499
1500 VMFnResult* CoreVMFunction_exp::exec(VMFnArgs* args) {
1501 VMRealExpr* realExpr = args->arg(0)->asReal();
1502 vmfloat f = realExpr->evalReal();
1503 if (sizeof(vmfloat) == sizeof(float))
1504 f = ::expf(f);
1505 else
1506 f = ::exp(f);
1507 return successResult({
1508 .value = f,
1509 .unitFactor = realExpr->unitFactor()
1510 });
1511 }
1512
1513 ///////////////////////////////////////////////////////////////////////////
1514 // built-in script function: pow()
1515
1516 bool CoreVMFunction_pow::acceptsArgUnitType(vmint iArg, StdUnit_t type) const {
1517 if (iArg == 0)
1518 return true;
1519 else
1520 return type == VM_NO_UNIT;
1521 }
1522
1523 bool CoreVMFunction_pow::acceptsArgUnitPrefix(vmint iArg, StdUnit_t type) const {
1524 return iArg == 0;
1525 }
1526
1527 StdUnit_t CoreVMFunction_pow::returnUnitType(VMFnArgs* args) {
1528 // pow() only allows unit for its 1st argument
1529 return args->arg(0)->asNumber()->unitType();
1530 }
1531
1532 bool CoreVMFunction_pow::returnsFinal(VMFnArgs* args) {
1533 // pow() only allows 'final'ness for its 1st argument
1534 return args->arg(0)->asNumber()->isFinal();
1535 }
1536
1537 VMFnResult* CoreVMFunction_pow::exec(VMFnArgs* args) {
1538 VMRealExpr* arg0 = args->arg(0)->asReal();
1539 VMRealExpr* arg1 = args->arg(1)->asReal();
1540 vmfloat a = arg0->evalReal();
1541 vmfloat b = arg1->evalReal();
1542 if (sizeof(vmfloat) == sizeof(float)) {
1543 return successResult({
1544 .value = ::powf(a,b),
1545 .unitFactor = arg0->unitFactor()
1546 });
1547 } else {
1548 return successResult({
1549 .value = ::pow(a,b),
1550 .unitFactor = arg0->unitFactor()
1551 });
1552 }
1553 }
1554
1555 ///////////////////////////////////////////////////////////////////////////
1556 // built-in script function: sin()
1557
1558 StdUnit_t CoreVMFunction_sin::returnUnitType(VMFnArgs* args) {
1559 return args->arg(0)->asNumber()->unitType();
1560 }
1561
1562 bool CoreVMFunction_sin::returnsFinal(VMFnArgs* args) {
1563 return args->arg(0)->asNumber()->isFinal();
1564 }
1565
1566 VMFnResult* CoreVMFunction_sin::exec(VMFnArgs* args) {
1567 VMRealExpr* realExpr = args->arg(0)->asReal();
1568 vmfloat f = realExpr->evalReal();
1569 if (sizeof(vmfloat) == sizeof(float))
1570 f = ::sinf(f);
1571 else
1572 f = ::sin(f);
1573 return successResult({
1574 .value = f,
1575 .unitFactor = realExpr->unitFactor()
1576 });
1577 }
1578
1579 ///////////////////////////////////////////////////////////////////////////
1580 // built-in script function: cos()
1581
1582 StdUnit_t CoreVMFunction_cos::returnUnitType(VMFnArgs* args) {
1583 return args->arg(0)->asNumber()->unitType();
1584 }
1585
1586 bool CoreVMFunction_cos::returnsFinal(VMFnArgs* args) {
1587 return args->arg(0)->asNumber()->isFinal();
1588 }
1589
1590 VMFnResult* CoreVMFunction_cos::exec(VMFnArgs* args) {
1591 VMRealExpr* realExpr = args->arg(0)->asReal();
1592 vmfloat f = realExpr->evalReal();
1593 if (sizeof(vmfloat) == sizeof(float))
1594 f = ::cosf(f);
1595 else
1596 f = ::cos(f);
1597 return successResult({
1598 .value = f,
1599 .unitFactor = realExpr->unitFactor()
1600 });
1601 }
1602
1603 ///////////////////////////////////////////////////////////////////////////
1604 // built-in script function: tan()
1605
1606 StdUnit_t CoreVMFunction_tan::returnUnitType(VMFnArgs* args) {
1607 return args->arg(0)->asNumber()->unitType();
1608 }
1609
1610 bool CoreVMFunction_tan::returnsFinal(VMFnArgs* args) {
1611 return args->arg(0)->asNumber()->isFinal();
1612 }
1613
1614 VMFnResult* CoreVMFunction_tan::exec(VMFnArgs* args) {
1615 VMRealExpr* realExpr = args->arg(0)->asReal();
1616 vmfloat f = realExpr->evalReal();
1617 if (sizeof(vmfloat) == sizeof(float))
1618 f = ::tanf(f);
1619 else
1620 f = ::tan(f);
1621 return successResult({
1622 .value = f,
1623 .unitFactor = realExpr->unitFactor()
1624 });
1625 }
1626
1627 ///////////////////////////////////////////////////////////////////////////
1628 // built-in script function: asin()
1629
1630 StdUnit_t CoreVMFunction_asin::returnUnitType(VMFnArgs* args) {
1631 return args->arg(0)->asNumber()->unitType();
1632 }
1633
1634 bool CoreVMFunction_asin::returnsFinal(VMFnArgs* args) {
1635 return args->arg(0)->asNumber()->isFinal();
1636 }
1637
1638 VMFnResult* CoreVMFunction_asin::exec(VMFnArgs* args) {
1639 VMRealExpr* realExpr = args->arg(0)->asReal();
1640 vmfloat f = realExpr->evalReal();
1641 if (sizeof(vmfloat) == sizeof(float))
1642 f = ::asinf(f);
1643 else
1644 f = ::asin(f);
1645 return successResult({
1646 .value = f,
1647 .unitFactor = realExpr->unitFactor()
1648 });
1649 }
1650
1651 ///////////////////////////////////////////////////////////////////////////
1652 // built-in script function: acos()
1653
1654 StdUnit_t CoreVMFunction_acos::returnUnitType(VMFnArgs* args) {
1655 return args->arg(0)->asNumber()->unitType();
1656 }
1657
1658 bool CoreVMFunction_acos::returnsFinal(VMFnArgs* args) {
1659 return args->arg(0)->asNumber()->isFinal();
1660 }
1661
1662 VMFnResult* CoreVMFunction_acos::exec(VMFnArgs* args) {
1663 VMRealExpr* realExpr = args->arg(0)->asReal();
1664 vmfloat f = realExpr->evalReal();
1665 if (sizeof(vmfloat) == sizeof(float))
1666 f = ::acosf(f);
1667 else
1668 f = ::acos(f);
1669 return successResult({
1670 .value = f,
1671 .unitFactor = realExpr->unitFactor()
1672 });
1673 }
1674
1675 ///////////////////////////////////////////////////////////////////////////
1676 // built-in script function: atan()
1677
1678 StdUnit_t CoreVMFunction_atan::returnUnitType(VMFnArgs* args) {
1679 return args->arg(0)->asNumber()->unitType();
1680 }
1681
1682 bool CoreVMFunction_atan::returnsFinal(VMFnArgs* args) {
1683 return args->arg(0)->asNumber()->isFinal();
1684 }
1685
1686 VMFnResult* CoreVMFunction_atan::exec(VMFnArgs* args) {
1687 VMRealExpr* realExpr = args->arg(0)->asReal();
1688 vmfloat f = realExpr->evalReal();
1689 if (sizeof(vmfloat) == sizeof(float))
1690 f = ::atanf(f);
1691 else
1692 f = ::atan(f);
1693 return successResult({
1694 .value = f,
1695 .unitFactor = realExpr->unitFactor()
1696 });
1697 }
1698
1699 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC