Mercurial > octave-nkf
comparison liboctave/oct-inttypes.h @ 4952:bfd57b466752
[project @ 2004-09-01 00:49:05 by jwe]
author | jwe |
---|---|
date | Wed, 01 Sep 2004 00:49:06 +0000 |
parents | f6b63ff1119b |
children | 7a3a480e8645 |
comparison
equal
deleted
inserted
replaced
4951:0a21e1bf18c4 | 4952:bfd57b466752 |
---|---|
265 double tx = static_cast<double> (x.value ()); | 265 double tx = static_cast<double> (x.value ()); |
266 ival = OCTAVE_INT_FIT_TO_RANGE (t - tx, T); | 266 ival = OCTAVE_INT_FIT_TO_RANGE (t - tx, T); |
267 return *this; | 267 return *this; |
268 } | 268 } |
269 | 269 |
270 octave_int<T>& operator *= (const octave_int<T>& x) | |
271 { | |
272 double t = static_cast<double> (value ()); | |
273 double tx = static_cast<double> (x.value ()); | |
274 ival = OCTAVE_INT_FIT_TO_RANGE (t * tx, T); | |
275 return *this; | |
276 } | |
277 | |
278 octave_int<T>& operator /= (const octave_int<T>& x) | |
279 { | |
280 double t = static_cast<double> (value ()); | |
281 double tx = static_cast<double> (x.value ()); | |
282 ival = OCTAVE_INT_FIT_TO_RANGE (t / tx, T); | |
283 return *this; | |
284 } | |
285 | |
286 template <class T2> | |
287 octave_int<T>& operator <<= (const T2& x) | |
288 { | |
289 ival = ((ival << x) > std::numeric_limits<T>::max ()) ? 0 : (ival << x); | |
290 return *this; | |
291 } | |
292 | |
293 template <class T2> | |
294 octave_int<T>& operator >>= (const T2& x) | |
295 { | |
296 ival >>= x; | |
297 return *this; | |
298 } | |
299 | |
270 octave_int<T> min (void) const { return std::numeric_limits<T>::min (); } | 300 octave_int<T> min (void) const { return std::numeric_limits<T>::min (); } |
271 octave_int<T> max (void) const { return std::numeric_limits<T>::max (); } | 301 octave_int<T> max (void) const { return std::numeric_limits<T>::max (); } |
272 | 302 |
273 static int nbits (void) { return sizeof (T) * CHAR_BIT; } | 303 static int nbits (void) { return sizeof (T) * CHAR_BIT; } |
274 | 304 |
276 | 306 |
277 private: | 307 private: |
278 | 308 |
279 T ival; | 309 T ival; |
280 }; | 310 }; |
311 | |
312 template <class T> | |
313 T | |
314 pow (const T& a, const T& b) | |
315 { | |
316 T retval; | |
317 | |
318 T zero = T (0); | |
319 T one = T (1); | |
320 | |
321 if (b == zero) | |
322 retval = one; | |
323 else if (b < zero) | |
324 retval = zero; | |
325 else | |
326 { | |
327 T a_val = a; | |
328 T b_val = b; | |
329 | |
330 retval = a; | |
331 | |
332 b_val -= 1; | |
333 | |
334 while (b_val) | |
335 { | |
336 if (b_val & one) | |
337 retval = retval * a_val; | |
338 | |
339 b_val = b_val >> 1; | |
340 | |
341 if (b_val > zero) | |
342 a_val = a_val * a_val; | |
343 } | |
344 } | |
345 | |
346 return retval; | |
347 } | |
281 | 348 |
282 template <class T> | 349 template <class T> |
283 std::ostream& | 350 std::ostream& |
284 operator << (std::ostream& os, const octave_int<T>& ival) | 351 operator << (std::ostream& os, const octave_int<T>& ival) |
285 { | 352 { |
335 | 402 |
336 OCTAVE_INT_BITCMP_OP (&) | 403 OCTAVE_INT_BITCMP_OP (&) |
337 OCTAVE_INT_BITCMP_OP (|) | 404 OCTAVE_INT_BITCMP_OP (|) |
338 OCTAVE_INT_BITCMP_OP (^) | 405 OCTAVE_INT_BITCMP_OP (^) |
339 | 406 |
340 #define OCTAVE_INT_BITSHIFT_OP(OP) \ | 407 template <class T1, class T2> |
341 \ | 408 octave_int<T1> |
342 template <class T1, class T2> \ | 409 operator << (const octave_int<T1>& x, const T2& y) |
343 octave_int<T1> \ | 410 { |
344 operator OP (const octave_int<T1>& x, const T2& y) \ | 411 T1 retval = x; |
345 { \ | 412 return retval <<= y; |
346 return ((x.value () OP y) > std::numeric_limits<T1>::max ()) ? 0 : (x.value () OP y); \ | 413 } |
347 } | 414 |
348 | 415 template <class T1, class T2> |
349 OCTAVE_INT_BITSHIFT_OP (<<) | 416 octave_int<T1> |
350 OCTAVE_INT_BITSHIFT_OP (>>) | 417 operator >> (const octave_int<T1>& x, const T2& y) |
418 { | |
419 T1 retval = x; | |
420 return retval >>= y; | |
421 } | |
351 | 422 |
352 template <class T> | 423 template <class T> |
353 octave_int<T> | 424 octave_int<T> |
354 bitshift (const octave_int<T>& a, int n, | 425 bitshift (const octave_int<T>& a, int n, |
355 const octave_int<T>& mask = std::numeric_limits<T>::max ()) | 426 const octave_int<T>& mask = std::numeric_limits<T>::max ()) |
356 { | 427 { |
357 if (n > 0) | 428 if (n > 0) |
358 return (a.value () << n) & mask.value (); | 429 return (a << n) & mask; |
359 else if (n < 0) | 430 else if (n < 0) |
360 return (a.value () >> -n) & mask.value (); | 431 return (a >> -n) & mask; |
361 else | 432 else |
362 return a; | 433 return a; |
363 } | 434 } |
364 | 435 |
365 #define OCTAVE_INT_CMP_OP(OP) \ | 436 #define OCTAVE_INT_CMP_OP(OP) \ |