/[svn]/linuxsampler/trunk/src/common/RTMath.cpp
ViewVC logotype

Annotation of /linuxsampler/trunk/src/common/RTMath.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3581 - (hide annotations) (download)
Fri Aug 30 11:40:25 2019 UTC (4 years, 7 months ago) by schoenebeck
File size: 6097 byte(s)
NKSP: Allow more wider support of standard measuring units & 'final'ness.

* Raised compiler requirement to be C++14 compliant (due to severe
  restrictions regarding C-style aggregate initializer lists in C++11
  which are now massively used throughout the code base).

* NKSP VM API: Allow units and 'final'ness to be returned as result from
  built-in functions (added methods VMFunction::returnUnitType() and
  VMFunction::returnsFinal() for that purpose which must be implemented by
  built-in function implementations).

* NKSP language: Allow metric unit prefixes of numeric scalar and array
  variables to be changed freely at runtime (unlike unit types like Hz etc.
  which are still sticky parse-time features of variables which cannot be
  changed at runtime for the intentional sake of determinism).

* NKSP language: 'final' values are prohibited for array variables for now
  (attempt causes a parsers error).

* NKSP language: expressions with unit types (e.g. Hz) are prohibited for
  conditions of runtime control structures like if(), while(), select()
  (attempt causes a parser error).

* NKSP VM API: Allow built-in functions to perform their own, individual
  parse time checks of arguments going to be passed to the function at
  runtime (added method VMFunction::checkArgs() for that purpose).

* NKSP language: raise parser warning if only one operand of binary
  operators (like logical 'or' comparison) contain a 'final' value (because
  it would always yield in a 'final' result in such cases).

* NKSP language: Allow comparison (=, #, <, >, <=, >=) of values with
  different metric unit prefixes, which will behave as expected (e.g.
  result of expression '1000us < 2ms' is true).

* NKSP language: Allow adding values with different metric unit prefixes
  (e.g. result of expression '100Hz + 5kHz' is '5100Hz').

* NKSP language: Allow subtracting values with different metric unit
  prefixes (e.g. result of expression '1ms - 20us' is '980us').

* NKSP language: Allow multiplying with any metric unit prefixes
  (e.g. result of expression '2k * 3ms' is '6s'), however multiplications
  with unit types on both sides (e.g. '2s * 2s') is still prohibited since
  we don't have any considerable practical use for a term like '4s^2'
  (hence any attempt multiplying two unit types still causes parser error).

* NKSP language: Allow dividing by any metric unit prefixes and allow
  division of same unit type on both sides (e.g. expression '8kHz / 1000Hz'
  yields in unit free result '8'). So this is now a way to cast units away
  e.g. for passing the result to other expressions, certain function calls
  or variables which are not accepting any units (or that specific unit).

* NKSP language: integer arrays and real number arrays can now be converted
  to strings (e.g. for dumping their content with message() calls for
  script debugging purposes).

* NKSP language: expressions and variables with units are now correctly
  casted to strings (e.g. with message() calls).

* NKSP language: comparing real numbers for equalness (e.g. '~foo = 3.1') or
  unequalness (e.g. '~foo # 3.1') is now less strict and takes the expected
  floating point tolerances into account.

* NKSP VM API: Added methods VMScalarNumberExpr::evalCastInt() and
  VMScalarNumberExpr::evalCastReal().

* NKSP VM API: Added base class 'VMNumberArrayExpr' for classes
  'VMIntArrayExpr' and 'VMRealArrayExpr'.

* NKSP VM API: replaced all unitPrefix() (parse time) methods by
  unitFactor() (runtime) methods.

* Built-in function "exit()" supports now returning units and 'final'ness
  exclusively for test cases.

* The following built-in functions support now units as well: "abs()",
  "random()", "inc()", "dec()", "in_range()", "min()", "max()",
  "real_to_int()", "int()", "int_to_real()" and "real()".

* Built-in functions "array_equal()", "search()" and "sort()" support now
  real number arrays (correctly) as well.

* Added individual parse time checks of arguments to be passed to built-in
  functions "random()", "inc()", "dec()", "in_range()", "min()", "max()",
  "array_equal()" and "search()" specific for their individual purposes.

* Test cases: Added massive amount of NKSP test cases for standard
  measuring units and 'final' operator usage cases.

* Test cases: Added NKSP test cases for (floating point tolerance aware)
  real number equalness / unequalness comparison.

* Bumped version (2.1.1.svn8).

1 schoenebeck 53 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5 schoenebeck 56 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 schoenebeck 3575 * Copyright (C) 2005 - 2019 Christian Schoenebeck *
7 schoenebeck 53 * *
8     * This program is free software; you can redistribute it and/or modify *
9     * it under the terms of the GNU General Public License as published by *
10     * the Free Software Foundation; either version 2 of the License, or *
11     * (at your option) any later version. *
12     * *
13     * This program is distributed in the hope that it will be useful, *
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16     * GNU General Public License for more details. *
17     * *
18     * You should have received a copy of the GNU General Public License *
19     * along with this program; if not, write to the Free Software *
20     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21     * MA 02111-1307 USA *
22     ***************************************************************************/
23    
24     #include "RTMath.h"
25    
26 schoenebeck 2942 // for unsafeMicroSeconds() implementation
27     #if !defined(WIN32) && !defined(__APPLE__)
28     # include <time.h>
29     #endif
30    
31 schoenebeck 1212 static float CentsToFreqTable[CONFIG_MAX_PITCH * 1200 * 2 + 1]; // +-1200 cents per octave
32    
33 schoenebeck 319 float* RTMathBase::pCentsToFreqTable(InitCentsToFreqTable());
34 schoenebeck 53
35 schoenebeck 361 #if defined(__APPLE__)
36     #include <mach/mach_time.h>
37     typedef uint64_t time_stamp_t;
38     #endif
39    
40 schoenebeck 328 /*
41     * Creates a real time stamp for the current moment. Out of efficiency this
42     * is implemented in inline assembly for each CPU independently; we currently
43     * don't use a generic solution for CPUs that are not yet covered by the
44     * assembly code, instead an error message is prompted on compile time, forcing
45     * the user to contact us.
46     */
47     RTMathBase::time_stamp_t RTMathBase::CreateTimeStamp() {
48     #if defined(__i386__) || defined(__x86_64__)
49     uint64_t t;
50     __asm__ __volatile__ ("rdtsc" : "=A" (t));
51 schoenebeck 3054 return time_stamp_t(t >> 8);
52 schoenebeck 328 #elif defined(__ia64__)
53     time_stamp_t t;
54     __asm__ __volatile__ ("mov %0=ar.itc" : "=r"(t));
55     return t;
56     #elif defined(__powerpc__)
57     time_stamp_t t;
58     __asm__ __volatile__ (
59     "98: mftb %0\n"
60     "99:\n"
61     ".section __ftr_fixup,\"a\"\n"
62     " .long %1\n"
63     " .long 0\n"
64     " .long 98b\n"
65     " .long 99b\n"
66     ".previous"
67     : "=r" (t) : "i" (0x00000100)
68     );
69     return t;
70     #elif defined(__alpha__)
71     time_stamp_t t;
72     __asm__ __volatile__ ("rpcc %0" : "=r"(t));
73     return t;
74 schoenebeck 361 #elif defined(__APPLE__)
75 schoenebeck 3054 return (time_stamp_t) mach_absolute_time();
76 schoenebeck 328 #else // we don't want to use a slow generic solution
77     # error "Sorry, LinuxSampler lacks time stamp code for your system."
78     # error "Please report this error and the CPU you are using to the LinuxSampler developers mailing list!"
79     #endif
80     }
81    
82 schoenebeck 2942 RTMathBase::usecs_t RTMathBase::unsafeMicroSeconds(clock_source_t source) {
83     #if defined(WIN32)
84     LARGE_INTEGER t;
85     LARGE_INTEGER f;
86     QueryPerformanceCounter(&t);
87     if (!QueryPerformanceFrequency(&f)) return 0;
88 schoenebeck 2943 return usecs_t( double(t.QuadPart) / double(f.QuadPart) * 1000000.0 );
89 schoenebeck 2942 #elif defined(__APPLE__)
90     static mach_timebase_info_data_t tb;
91     double t = mach_absolute_time();
92     // if this method is run for the first time, get the internal time base
93     if (!tb.denom) mach_timebase_info(&tb); // get nanoseconds per tick
94     // convert from internal (abstract) time value to microseconds
95     return usecs_t( t * double(tb.numer) / double(tb.denom) / 1000.0 );
96     #else
97     timespec t;
98     clockid_t cid;
99     switch (source) {
100     case process_clock: cid = CLOCK_PROCESS_CPUTIME_ID; break;
101     case thread_clock: cid = CLOCK_THREAD_CPUTIME_ID; break;
102     case real_clock: cid = CLOCK_MONOTONIC; break;
103     default: return 0;
104     }
105     clock_gettime(cid, &t);
106     return usecs_t( (double(t.tv_sec) * 1000000000.0 + double(t.tv_nsec)) / 1000.0 );
107     #endif
108     }
109    
110 schoenebeck 3575 bool RTMathBase::fEqual32(float a, float b) {
111 schoenebeck 3581 if (a == b) return true;
112    
113 schoenebeck 3575 if (isinf(a) || isinf(b))
114     return isinf(a) == isinf(b);
115     if (isnan(a) || isnan(b))
116     return isnan(a) == isnan(b);
117    
118     const int bits = 23 /* float32 type fraction bits */ - 4 /* arbitrarily reduced bit tolerance */;
119    
120     if (a == 0.f)
121     return fabs(b) < (1.0 / pow(2, bits));
122     if (b == 0.f)
123     return fabs(a) < (1.0 / pow(2, bits));
124    
125     const double epsilon = fabs(a / pow(2.0, bits));
126     return fabs(b - a) <= epsilon;
127     }
128    
129     bool RTMathBase::fEqual64(double a, double b) {
130 schoenebeck 3581 if (a == b) return true;
131    
132 schoenebeck 3575 if (isinf(a) || isinf(b))
133     return isinf(a) == isinf(b);
134     if (isnan(a) || isnan(b))
135     return isnan(a) == isnan(b);
136    
137     const int bits = 52 /* float64 type fraction bits */ - 4 /* arbitrarily reduced bit tolerance */;
138    
139     if (a == 0.f)
140     return fabs(b) < (1.0 / pow(2, bits));
141     if (b == 0.f)
142     return fabs(a) < (1.0 / pow(2, bits));
143    
144     const double epsilon = fabs(a / pow(2.0, bits));
145     return fabs(b - a) <= epsilon;
146     }
147    
148 schoenebeck 53 /**
149     * Will automatically be called once to initialize the 'Cents to frequency
150     * ratio' table.
151     */
152 schoenebeck 319 float* RTMathBase::InitCentsToFreqTable() {
153 schoenebeck 554 float* pMiddleOfTable = &CentsToFreqTable[CONFIG_MAX_PITCH * 1200];
154 persson 799 for (int i = -CONFIG_MAX_PITCH * 1200; i <= CONFIG_MAX_PITCH * 1200; i++) {
155 schoenebeck 53 pMiddleOfTable[i] = pow(TWELVEHUNDREDTH_ROOT_OF_TWO, i);
156     }
157     return pMiddleOfTable;
158     }

  ViewVC Help
Powered by ViewVC