FAUST compiler  0.9.9.6b8
interval.hh
Go to the documentation of this file.
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;
39  double lo;
40  double hi;
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 
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 pow(const interval& x, const interval& y)
214 {
215  if (x.lo > 0.0) {
216  double a = pow(x.lo,y.lo);
217  double b = pow(x.lo,y.hi);
218  double c = pow(x.hi,y.lo);
219  double d = pow(x.hi,y.hi);
220  return interval(min4(a,b,c,d), max4(a,b,c,d));
221  } else {
222  return interval();
223  }
224 }
225 
226 inline interval abs(const interval& x)
227 {
228  if (x.valid) {
229  if (x.lo >= 0) {
230  return x;
231  } else if (x.hi < 0) {
232  return interval(fabs(x.hi), fabs(x.lo));
233  } else {
234  return interval(0, max(fabs(x.lo), x.hi));
235  }
236  } else {
237  return x;
238  }
239 }
240 
241 #endif
interval operator+(const interval &x, const interval &y)
Definition: interval.hh:74
bool valid
true if it is a valid interval
Definition: interval.hh:38
interval(const interval &r)
Definition: interval.hh:45
interval operator^(const interval &, const interval &)
Definition: interval.hh:152
interval operator&(const interval &x, const interval &y)
Definition: interval.hh:122
double lo
minimal value
Definition: interval.hh:39
interval pow(const interval &x, const interval &y)
Definition: interval.hh:213
interval(double n)
Definition: interval.hh:43
interval abs(const interval &x)
Definition: interval.hh:226
int bitmask(double x)
Convert a number 1bbb..b into a bit mask 1111..1 of same width.
Definition: interval.hh:114
interval operator>=(const interval &, const interval &)
Definition: interval.hh:186
interval operator<(const interval &, const interval &)
Definition: interval.hh:171
double max(double x, double y)
Definition: interval.hh:60
interval operator!=(const interval &, const interval &)
Definition: interval.hh:196
interval operator-(const interval &x, const interval &y)
Definition: interval.hh:79
bool isconst()
Definition: interval.hh:47
ostream & operator<<(ostream &dst, const interval &i)
Definition: interval.hh:50
interval operator*(const interval &x, const interval &y)
Definition: interval.hh:84
interval reunion(const interval &x, const interval &y)
Definition: interval.hh:64
interval()
Definition: interval.hh:42
double max4(double a, double b, double c, double d)
Definition: interval.hh:62
interval operator|(const interval &x, const interval &y)
Definition: interval.hh:143
interval operator/(const interval &x, const interval &y)
Definition: interval.hh:97
interval operator==(const interval &, const interval &)
Definition: interval.hh:191
double min4(double a, double b, double c, double d)
Definition: interval.hh:61
double min(double x, double y)
Definition: interval.hh:59
interval operator>>(const interval &, const interval &)
Definition: interval.hh:162
interval operator<=(const interval &, const interval &)
Definition: interval.hh:176
double hi
maximal value
Definition: interval.hh:40
interval(double n, double m)
Definition: interval.hh:44
interval operator%(const interval &x, const interval &y)
Definition: interval.hh:104
interval operator>(const interval &, const interval &)
Definition: interval.hh:181