748f526b94add099508191b137c3ed5fe0371700
[Faustine.git] / interpretor / faust-0.9.47mr3 / compiler / tlib / num.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 /*****************************************************************************
25 ******************************************************************************
26 NUM
27 Y. Orlarey, (c) Grame 2002
28 ------------------------------------------------------------------------------
29 Nums are tagged unions of ints and floats. Nums are completly described by
30 the num.h file, there is no num.cpp file.
31
32 API:
33 ----
34 num(10); : num with int content
35 num(3.14159); : num with double content
36
37 num op num : op is any C binary operator
38
39 type(num) : 0 = int, 1 = double
40 int(num); : int content of num or conversion
41 double(num); : double content of num or conversion
42
43 History :
44 ---------
45 2002-02-08 : First version
46
47 ******************************************************************************
48 *****************************************************************************/
49
50
51 #ifndef __NUM__
52 #define __NUM__
53
54
55 //-------------------------------------------------------------------------
56 // Class num = (int x (int + double))
57 //-------------------------------------------------------------------------
58 class num
59 {
60 int fType;
61 union {
62 int i;
63 double f;
64 } fData;
65
66 public:
67 // constructeurs
68 num (int x=0) : fType(0) { fData.i = x; }
69 //num (double x) : fType(1) { fData.f = (double)x; }
70 num (double x) : fType(1) { fData.f = x; }
71 num (const num& n) : fType(n.fType) { fData.i = n.fData.i; }
72
73 num& operator = (int n) { fType = 0; fData.i = n; return *this; }
74 num& operator = (double n) { fType = 1; fData.f = n; return *this; }
75
76 // predicats
77 bool operator == (const num& n) const { return fType == n.fType && fData.i == n.fData.i; }
78 bool operator != (const num& n) const { return fType != n.fType || fData.i != n.fData.i; }
79
80 // accessors
81 int type() const { return fType; }
82 operator int() const { return (fType) ? int(fData.f) : fData.i; }
83 operator double() const { return (fType) ? fData.f : double(fData.i); }
84
85 };
86
87
88 inline int isfloat (const num& n) { return n.type(); }
89
90 inline const num operator+ (const num& x, const num& y)
91 { return (isfloat(x)|isfloat(y)) ? num(double(x)+double(y)) : num(int(x)+int(y)); }
92
93 inline const num operator- (const num& x, const num& y)
94 { return (isfloat(x)|isfloat(y)) ? num(double(x)-double(y)) : num(int(x)-int(y)); }
95
96 inline const num operator* (const num& x, const num& y)
97 { return (isfloat(x)|isfloat(y)) ? num(double(x)*double(y)) : num(int(x)*int(y)); }
98
99 inline const num operator/ (const num& x, const num& y)
100 { return (isfloat(x)|isfloat(y)) ? num(double(x)/double(y)) : num(int(x)/int(y)); }
101
102 inline const num operator% (const num& x, const num& y)
103 { return num(int(x)%int(y)); }
104
105
106 // operations sur les bits
107 inline const num operator<< (const num& x, const num& y)
108 { return num(int(x)<<int(y)); }
109
110 inline const num operator>> (const num& x, const num& y)
111 { return num(int(x)>>int(y)); }
112
113
114 // operations booléennes sur les bits
115 inline const num operator& (const num& x, const num& y)
116 { return num(int(x)&int(y)); }
117
118 inline const num operator| (const num& x, const num& y)
119 { return num(int(x)|int(y)); }
120
121 inline const num operator^ (const num& x, const num& y)
122 { return num(int(x)^int(y)); }
123
124
125 // operations de comparaison
126 inline const num operator> (const num& x, const num& y)
127 { return (isfloat(x)|isfloat(y)) ? num(double(x)>double(y)) : num(int(x)>int(y)); }
128
129 inline const num operator< (const num& x, const num& y)
130 { return (isfloat(x)|isfloat(y)) ? num(double(x)<double(y)) : num(int(x)<int(y)); }
131
132 inline const num operator>= (const num& x, const num& y)
133 { return (isfloat(x)|isfloat(y)) ? num(double(x)>=double(y)) : num(int(x)>=int(y)); }
134
135 inline const num operator<= (const num& x, const num& y)
136 { return (isfloat(x)|isfloat(y)) ? num(double(x)<=double(y)) : num(int(x)<=int(y)); }
137
138 inline const num operator== (const num& x, const num& y)
139 { return (isfloat(x)|isfloat(y)) ? num(double(x)==double(y)) : num(int(x)==int(y)); }
140
141 inline const num operator!= (const num& x, const num& y)
142 { return (isfloat(x)|isfloat(y)) ? num(double(x)!=double(y)) : num(int(x)!=int(y)); }
143
144
145 #endif