00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef INTS
00022 #define INTS
00023 #include <spuc.h>
00024 #include <stdlib.h>
00025 #include <math.h>
00026 namespace SPUC {
00027 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00028
00031
00032
00033
00038 class int_s
00039 {
00040 typedef long natural;
00041
00042 public:
00043 natural q;
00044 natural mask;
00045 natural bits;
00046 natural sign;
00047 natural bitpos;
00048
00049
00050 int_s() {
00051 q = 0;
00052 bits=32;
00053 mask = 0;
00054 sign = 0;
00055 bitpos=0;
00056 }
00057 int_s(natural y) {
00058 q = y;
00059 bits=32;
00060 mask = 0;
00061 sign = 0;
00062 bitpos=0;
00063 }
00064
00065 public:
00066 inline int_s operator =(const int_s& y) {
00067 q = y.q;
00068 sign = (y.q & MASK_BIT(bits));
00069 if ( ((mask&q)!=mask) || ((mask&q)!=0) ) {
00070 q &= ~mask;
00071 if (sign) q |= mask;
00072 }
00073 return *this;
00074 }
00075 inline int_s operator =(const natural& y) {
00076 q = y;
00077 sign = (y & MASK_BIT(bits));
00078 if ( ((mask&q)!=mask) || ((mask&q)!=0) ) {
00079 q &= ~mask;
00080 if (sign) q |= mask;
00081 }
00082 return *this;
00083 }
00084 friend int_s operator ,(int_s r, int_s l);
00085 friend int_s operator %(int_s r, int_s l);
00086 friend int_s operator %(int_s r, natural l);
00087 friend int_s operator %(natural r, int_s l);
00088 friend int_s operator +(int_s r, int_s l);
00089 friend int_s operator +(int_s r, natural l);
00090 friend int_s operator +(natural r, int_s l);
00091 friend int_s operator -(int_s r, int_s l);
00092 friend int_s operator -(natural r, int_s l);
00093 friend int_s operator -(int_s r, natural l);
00094 friend int_s operator -(int_s r);
00095 friend int_s operator &(int_s r, int_s l);
00096 friend int_s operator &(int_s r, natural l);
00097 friend int_s operator &(natural r, int_s l);
00098 friend int_s operator ^(int_s r, int_s l);
00099 friend int_s operator ^(int_s r, natural l);
00100 friend int_s operator ^(natural r, int_s l);
00101 friend int_s operator |(int_s r, int_s l);
00102 friend int_s operator |(int_s r, natural l);
00103 friend int_s operator |(natural r, int_s l);
00104 friend int_s operator *(int_s r, int_s l);
00105 friend int_s operator *(natural r, int_s l);
00106 friend int_s operator *(int_s r, natural l);
00107 friend int_s operator /(int_s r, int_s l);
00108 friend int_s operator /(int_s r, natural l);
00109 friend int_s operator /(natural r, int_s l);
00110 friend int_s operator <<(int_s r, const natural shift) ;
00111 friend int_s operator >>(int_s r, const natural shift) ;
00112
00113 int_s operator ++() {
00114 if (q==~mask) q=mask;
00115 else q++;
00116 return *this;
00117 };
00118
00119 int_s operator --() {
00120 if (q==mask) q=~mask;
00121 else q--;
00122 return *this;
00123 };
00124
00125 int_s operator ++(int) {
00126 if (q==~mask) q=mask;
00127 else q++;
00128 return *this;
00129 };
00130
00131 int_s operator --(int) {
00132 if (q==mask) q=~mask;
00133 else q--;
00134 return *this;
00135 };
00136
00137 int_s operator +=(int_s r) {
00138 q += r.q;
00139 if ( ((mask&q)!=mask) || ((mask&q)!=0) ) q &= ~mask;
00140 return *this;
00141 };
00142
00143 int_s operator -=(int_s r){
00144 q -= r.q;
00145 if ( ((mask&q)!=mask) || ((mask&q)!=0) ) q &= ~mask;
00146 return *this;
00147 };
00148
00149 int_s operator *=(int_s r) {
00150 q *= r.q;
00151 if ( ((mask&q)!=mask) || ((mask&q)!=0) ) q &= ~mask;
00152 return *this;
00153 };
00154
00155 int_s operator /=(int_s r){
00156 if (r.q != 0) q = q/r.q;
00157 else q = 0;
00158 return *this;
00159 };
00160
00161 int_s operator <<=(const natural shift) {
00162 q = q << shift;
00163 return *this;
00164 };
00165
00166 int_s operator >>=(const natural shift) {
00167 q = q >> shift;
00168 return *this;
00169 }
00170
00171
00172 int_s operator ^=(int_s r) {
00173 q ^= r.q;
00174 return *this;
00175 };
00176
00177 int_s operator &=(int_s r) {
00178 q &= r.q;
00179 return *this;
00180 };
00181
00182 int_s operator |=(int_s r) {
00183 if (r.q<0) q |= r.q;
00184 else q |= (r.q & ~mask);
00185 return *this;
00186 };
00187
00188 inline natural operator ~() {return(~q);}
00189 inline bool operator !() {return(!q);}
00190 inline bool operator ==(int_s r) { return ((q == r.q)); };
00191 inline bool operator ==(natural r) { return ((q == r)); };
00192 inline bool operator !=(int_s r) { return ((q != r.q)); };
00193 inline bool operator !=(natural r) { return ((q != r)); };
00194 inline bool operator >(int_s r) { return ((q > r.q)); };
00195 inline bool operator >(natural r) { return ((q > r)); };
00196 inline bool operator <(int_s r) { return ((q < r.q)); };
00197 inline bool operator <(natural r) { return ((q < r)); };
00198 inline int_s magsq(int_s y) {
00199 int_s x;
00200 x.q = y.q*y.q;
00201 x.mask = -1 << (y.bits * 2);
00202 return(x);
00203 };
00204
00205 operator const natural () const { return(q); }
00206 operator const double () const { return((double)q); }
00207
00208 int_s round(int_s in, natural rbits) {
00209 double scale = 1.0/(double)MASK_BIT(rbits);
00210 return(int_s((natural)floor((double)(scale*in.q)+0.5)));
00211 };
00212
00213 int_s saturate(int_s in, natural rbits) {
00214 int_s out;
00215 natural low_mask = MASK_LOW(rbits);
00216 if (labs(in.q) > low_mask) out.q = (in.q>0) ? low_mask : ~low_mask;
00217 else out.q = in.q;
00218 return(out);
00219 }
00220
00221 int_s clip(int_s in, int_s min_clp, int_s max_clp) {
00222 int_s out = CLIP(in, max_clp, min_clp);
00223 return out;
00224 }
00225
00226 inline bool bit(natural i) { return (q & MASK_BIT(i)) ? 1 : 0; }
00227 bool or_bits(int_s r) {
00228 natural x=r.q;
00229 bool s=0;
00230 for (int i=0;i<bits;i++) {
00231 s |= x & 0x1;
00232 x >>= 1;
00233 }
00234 return(s);
00235 }
00236
00237
00238 };
00239
00240 int_s operator %(int_s r, int_s l) {
00241 int_s x;
00242 x.q = r.q % l.q;
00243 return(x);
00244 }
00245 int_s operator %(int_s r, natural l) {
00246 int_s x;
00247 x.q = r.q % l;
00248 return(x);
00249 }
00250 int_s operator %(natural r, int_s l) {
00251 int_s x;
00252 x.q = r % l.q;
00253 return(x);
00254 }
00255
00256 int_s operator +(int_s r, int_s l) {
00257 int_s x;
00258 x.q = r.q + l.q;
00259 return(x);
00260 }
00261 int_s operator +(int_s r, natural l) {
00262 int_s x;
00263 x.q = r.q + l;
00264 return(x);
00265 }
00266 int_s operator +(natural r, int_s l) {
00267 int_s x;
00268 x.q = r + l.q;
00269 return(x);
00270 }
00271 int_s operator -(int_s r, int_s l) {
00272 int_s x;
00273 x.q = r.q - l.q;
00274 return(x);
00275 }
00276 int_s operator -(natural r, int_s l) {
00277 int_s x;
00278 x.q = r - l.q;
00279 return(x);
00280 }
00281 int_s operator -(int_s r, natural l) {
00282 int_s x;
00283 x.q = r.q - l;
00284 return(x);
00285 }
00286 int_s operator -(int_s r) {
00287 int_s x;
00288 x.q = -r.q;
00289 return(x);
00290 }
00291 int_s operator &(int_s r, int_s l) {
00292 int_s x;
00293 x.q = r.q & l.q;
00294 return(x);
00295 }
00296 int_s operator &(int_s r, natural l) {
00297 int_s x;
00298 x.q = r.q & l;
00299 return(x);
00300 }
00301
00302 int_s operator &(natural r, int_s l) {
00303 int_s x;
00304 x.q = r & l.q;
00305 return(x);
00306 }
00307
00308 int_s operator ^(int_s r, int_s l) {
00309 int_s x;
00310 x.q = r.q ^ l.q;
00311 return(x);
00312 }
00313 int_s operator ^(int_s r, natural l) {
00314 int_s x;
00315 x.q = r.q ^ l;
00316 return(x);
00317 }
00318 int_s operator ^(natural r, int_s l) {
00319 int_s x;
00320 x.q = r ^ l.q;
00321 return(x);
00322 }
00323
00324 int_s operator |(int_s r, int_s l) {
00325 int_s x;
00326 x.q = r.q | l.q;
00327 return(x);
00328 }
00329 int_s operator |(int_s r, natural l) {
00330 int_s x;
00331 x.q = r.q | l;
00332 return(x);
00333 }
00334 int_s operator |(natural r, int_s l) {
00335 int_s x;
00336 x.q = r | l.q;
00337 return(x);
00338 }
00339
00340 int_s operator *(int_s r, int_s l) {
00341 int_s x;
00342 x.q = r.q*l.q;
00343 return(x);
00344 };
00345 int_s operator *(natural r, int_s l) {
00346 int_s x;
00347 x.q = r*l.q;
00348 return(x);
00349 };
00350 int_s operator *(int_s r, natural l) {
00351 int_s x;
00352 x.q = r.q*l;
00353 return(x);
00354 };
00355
00356 int_s operator /(int_s r, int_s l) {
00357 int_s x;
00358 x.q = r.q/l.q;
00359 return(x);
00360 };
00361 int_s operator /(int_s r, natural l) {
00362 int_s x;
00363 x.q = r.q/l;
00364 return(x);
00365 };
00366 int_s operator /(natural r, int_s l) {
00367 int_s x;
00368 x.q = r/l.q;
00369 return(x);
00370 };
00371
00372 int_s operator <<(int_s r, const natural shift) {
00373 int_s x;
00374 x.q = r.q << shift;
00375 x.mask = r.mask << shift;
00376 return(x);
00377 };
00378
00379 int_s operator >>(int_s r, const natural shift) {
00380 int_s x;
00381 x.q = r.q >> shift;
00382 x.mask = r.mask >> shift;
00383 return(x);
00384 };
00385 int_s operator ,(int_s r, int_s l) {
00386 int_s x;
00387 if (l.q > 0) x.q = (r.q << l.bits) | l.q;
00388 else x.q = (r.q << l.bits) | (l.q & ~l.mask);
00389 return(x);
00390 }
00391 }
00392 #endif
00393 #endif