OOP initial commit.
[Faustine.git] / interpretor / faust-0.9.47mr3 / compiler / signals / interval.hh
1 /************************************************************************
2 ************************************************************************
3 FAUST compiler
4 Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
5 ---------------------------------------------------------------------
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 ************************************************************************
20 ************************************************************************/
21
22
23
24 #ifndef __signals_intervals__
25 #define __signals_intervals__
26
27 #include <math.h>
28 #include <iostream>
29
30 #ifdef WIN32
31 inline double log2(double e) { return log(e)/log(double(2)); }
32 #endif
33
34 using namespace std;
35
36 struct interval
37 {
38 bool valid; ///< true if it is a valid interval
39 double lo; ///< minimal value
40 double hi; ///< maximal value
41
42 interval () : valid(false), lo(-HUGE_VAL), hi(HUGE_VAL) {}
43 interval (double n) : valid(true), lo(n), hi(n) {}
44 interval (double n, double m) : valid(true), lo(min(n,m)), hi(max(n,m)) {}
45 interval (const interval& r) : valid(r.valid), lo(r.lo), hi(r.hi) {}
46
47 bool isconst() { return valid & (lo == hi); }
48 };
49
50 inline ostream& operator<<(ostream& dst, const interval& i)
51 {
52 if (i.valid) {
53 return dst << "interval(" << i.lo << ", " << i.hi << ")";
54 } else {
55 return dst << "interval()";
56 }
57 }
58
59 inline double min(double x, double y) { return (x<y) ? x:y; }
60 inline double max(double x, double y) { return (x>y) ? x:y; }
61 inline double min4(double a, double b, double c, double d) { return min(min(a,b),min(c,d)); }
62 inline double max4(double a, double b, double c, double d) { return max(max(a,b),max(c,d)); }
63
64 inline interval reunion(const interval& x, const interval& y)
65 {
66 if (x.valid & y.valid) {
67 return interval(min(x.lo,y.lo), max(x.hi,y.hi));
68 } else {
69 return interval();
70 }
71 }
72
73
74 inline interval operator+(const interval& x, const interval& y)
75 {
76 return (x.valid&y.valid) ? interval(x.lo+y.lo, x.hi+y.hi) : interval();
77 }
78
79 inline interval operator-(const interval& x, const interval& y)
80 {
81 return (x.valid & y.valid) ? interval(x.lo-y.hi, x.hi-y.lo) : interval();;
82 }
83
84 inline interval operator*(const interval& x, const interval& y)
85 {
86 if (x.valid&y.valid) {
87 double a=x.lo*y.lo;
88 double b=x.lo*y.hi;
89 double c=x.hi*y.lo;
90 double d=x.hi*y.hi;
91 return interval(min4(a,b,c,d), max4(a,b,c,d));
92 } else {
93 return interval();
94 }
95 }
96
97 inline interval operator/(const interval& x, const interval& y)
98 {
99 return (x.valid && y.valid && (y.lo > 0 | y.hi < 0))
100 ? x * interval(1/y.hi,1/y.lo)
101 : interval();
102 }
103
104 inline interval operator%(const interval& x, const interval& y)
105 {
106 return (x.valid && y.valid && x.lo >= 0 && y.lo > 0)
107 ? interval(0,y.hi)
108 : interval();
109 }
110
111 /**
112 * Convert a number 1bbb..b into a bit mask 1111..1 of same width
113 */
114 inline int bitmask (double x) {
115 int v = int(x);
116 for (int i=1; i<32; i*=2) { v |= v>>i; }
117 return v;
118 }
119
120 //----------------------booleans&bits--------------------------------------
121
122 inline interval operator&(const interval& x, const interval& y)
123 {
124 if (x.valid && y.valid) {
125 if (x.lo >= 0 & y.lo >= 0) {
126 return interval(0, bitmask(x.hi) & bitmask(y.hi));
127 } else if (y.lo >= 0) {
128 return interval(0, bitmask(y.hi));
129 } else if (x.lo >= 0) {
130 return interval(0, bitmask(y.hi));
131 } else {
132 return interval();
133 }
134 } else if (x.valid & x.lo >= 0) {
135 return interval(0, bitmask(x.hi));
136 } else if (y.valid & y.lo >= 0) {
137 return interval(0, bitmask(y.hi));
138 } else {
139 return interval();
140 }
141 }
142
143 inline interval operator|(const interval& x, const interval& y)
144 {
145 if (x.valid && y.valid && x.lo >= 0 && y.lo >= 0) {
146 return interval(0, bitmask(x.hi) | bitmask(y.hi));
147 } else {
148 return interval();
149 }
150 }
151
152 inline interval operator^(const interval&, const interval&)
153 {
154 return interval();
155 }
156
157 inline interval operator<<(const interval&, const interval&)
158 {
159 return interval();
160 }
161
162 inline interval operator>>(const interval&, const interval&)
163 {
164 return interval();
165 }
166
167 // ---------------------comparaisons------------------------------
168 // note : les comparaisons ne portent pas sur les intervals
169 // mais l'interval des comparaisons de signaux
170
171 inline interval operator<(const interval&, const interval&)
172 {
173 return interval(0,1);
174 }
175
176 inline interval operator<=(const interval&, const interval&)
177 {
178 return interval(0,1);
179 }
180
181 inline interval operator>(const interval&, const interval&)
182 {
183 return interval(0,1);
184 }
185
186 inline interval operator>=(const interval&, const interval&)
187 {
188 return interval(0,1);
189 }
190
191 inline interval operator==(const interval&, const interval&)
192 {
193 return interval(0,1);
194 }
195
196 inline interval operator!=(const interval&, const interval&)
197 {
198 return interval(0,1);
199 }
200
201 //-----------------------------------------------------------------------
202
203 inline interval min(const interval& x, const interval& y)
204 {
205 return interval(min(x.lo,y.lo), min(x.hi,y.hi));
206 }
207
208 inline interval max(const interval& x, const interval& y)
209 {
210 return interval(max(x.lo,y.lo), max(x.hi,y.hi));
211 }
212
213 inline interval abs(const interval& x)
214 {
215 if (x.valid) {
216 if (x.lo >= 0) {
217 return x;
218 } else if (x.hi < 0) {
219 return interval(fabs(x.hi), fabs(x.lo));
220 } else {
221 return interval(0, max(fabs(x.lo), x.hi));
222 }
223 } else {
224 return x;
225 }
226 }
227
228 #endif