comparison liboctave/lo-utils.cc @ 7789:82be108cc558

First attempt at single precision tyeps * * * corrections to qrupdate single precision routines * * * prefer demotion to single over promotion to double * * * Add single precision support to log2 function * * * Trivial PROJECT file update * * * Cache optimized hermitian/transpose methods * * * Add tests for tranpose/hermitian and ChangeLog entry for new transpose code
author David Bateman <dbateman@free.fr>
date Sun, 27 Apr 2008 22:34:17 +0200
parents 845ca0affec0
children eb63fbe60fab
comparison
equal deleted inserted replaced
7788:45f5faba05a2 7789:82be108cc558
60 return std::numeric_limits<octave_idx_type>::min (); 60 return std::numeric_limits<octave_idx_type>::min ();
61 else 61 else
62 return static_cast<octave_idx_type> ((x > 0) ? (x + 0.5) : (x - 0.5)); 62 return static_cast<octave_idx_type> ((x > 0) ? (x + 0.5) : (x - 0.5));
63 } 63 }
64 64
65 octave_idx_type
66 NINTbig (float x)
67 {
68 if (x > std::numeric_limits<octave_idx_type>::max ())
69 return std::numeric_limits<octave_idx_type>::max ();
70 else if (x < std::numeric_limits<octave_idx_type>::min ())
71 return std::numeric_limits<octave_idx_type>::min ();
72 else
73 return static_cast<octave_idx_type> ((x > 0) ? (x + 0.5) : (x - 0.5));
74 }
75
65 int 76 int
66 NINT (double x) 77 NINT (double x)
67 { 78 {
68 if (x > std::numeric_limits<int>::max ()) 79 if (x > std::numeric_limits<int>::max ())
69 return std::numeric_limits<int>::max (); 80 return std::numeric_limits<int>::max ();
71 return std::numeric_limits<int>::min (); 82 return std::numeric_limits<int>::min ();
72 else 83 else
73 return static_cast<int> ((x > 0) ? (x + 0.5) : (x - 0.5)); 84 return static_cast<int> ((x > 0) ? (x + 0.5) : (x - 0.5));
74 } 85 }
75 86
87 int
88 NINT (float x)
89 {
90 if (x > std::numeric_limits<int>::max ())
91 return std::numeric_limits<int>::max ();
92 else if (x < std::numeric_limits<int>::min ())
93 return std::numeric_limits<int>::min ();
94 else
95 return static_cast<int> ((x > 0) ? (x + 0.5) : (x - 0.5));
96 }
97
76 double 98 double
77 D_NINT (double x) 99 D_NINT (double x)
100 {
101 if (xisinf (x) || xisnan (x))
102 return x;
103 else
104 return floor (x + 0.5);
105 }
106
107 float
108 F_NINT (float x)
78 { 109 {
79 if (xisinf (x) || xisnan (x)) 110 if (xisinf (x) || xisnan (x))
80 return x; 111 return x;
81 else 112 else
82 return floor (x + 0.5); 113 return floor (x + 0.5);
377 os << ","; 408 os << ",";
378 octave_write_double (os, imag (c)); 409 octave_write_double (os, imag (c));
379 os << ")"; 410 os << ")";
380 } 411 }
381 412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434 static inline float
435 read_float_inf_nan_na (std::istream& is, char c, char sign = '+')
436 {
437 float d = 0.0;
438
439 switch (c)
440 {
441 case 'i': case 'I':
442 {
443 c = is.get ();
444 if (c == 'n' || c == 'N')
445 {
446 c = is.get ();
447 if (c == 'f' || c == 'F')
448 d = sign == '-' ? -octave_Inf : octave_Inf;
449 else
450 is.putback (c);
451 }
452 else
453 is.putback (c);
454 }
455 break;
456
457 case 'n': case 'N':
458 {
459 c = is.get ();
460 if (c == 'a' || c == 'A')
461 {
462 c = is.get ();
463 if (c == 'n' || c == 'N')
464 d = octave_NaN;
465 else
466 {
467 is.putback (c);
468 d = octave_NA;
469 }
470 }
471 else
472 is.putback (c);
473 }
474 break;
475
476 default:
477 abort ();
478 }
479
480 return d;
481 }
482
483 float
484 octave_read_float (std::istream& is)
485 {
486 float d = 0.0;
487
488 char c1 = ' ';
489
490 while (isspace (c1))
491 c1 = is.get ();
492
493 switch (c1)
494 {
495 case '-':
496 {
497 char c2 = 0;
498 c2 = is.get ();
499 if (c2 == 'i' || c2 == 'I')
500 d = read_float_inf_nan_na (is, c2, c1);
501 else
502 {
503 is.putback (c2);
504 is.putback (c1);
505 is >> d;
506 }
507 }
508 break;
509
510 case '+':
511 {
512 char c2 = 0;
513 c2 = is.get ();
514 if (c2 == 'i' || c2 == 'I')
515 d = read_float_inf_nan_na (is, c2, c1);
516 else
517 {
518 is.putback (c2);
519 is.putback (c1);
520 is >> d;
521 }
522 }
523 break;
524
525 case 'i': case 'I':
526 case 'n': case 'N':
527 d = read_float_inf_nan_na (is, c1);
528 break;
529
530 default:
531 is.putback (c1);
532 is >> d;
533 }
534
535 return d;
536 }
537
538 FloatComplex
539 octave_read_float_complex (std::istream& is)
540 {
541 float re = 0.0, im = 0.0;
542
543 FloatComplex cx = 0.0;
544
545 char ch = ' ';
546
547 while (isspace (ch))
548 ch = is.get ();
549
550 if (ch == '(')
551 {
552 re = octave_read_float (is);
553 ch = is.get ();
554
555 if (ch == ',')
556 {
557 im = octave_read_float (is);
558 ch = is.get ();
559
560 if (ch == ')')
561 cx = FloatComplex (re, im);
562 else
563 is.setstate (std::ios::failbit);
564 }
565 else if (ch == ')')
566 cx = re;
567 else
568 is.setstate (std::ios::failbit);
569 }
570 else
571 {
572 is.putback (ch);
573 cx = octave_read_float (is);
574 }
575
576 return cx;
577
578 }
579
580 void
581 octave_write_float (std::ostream& os, float d)
582 {
583 if (lo_ieee_is_NA (d))
584 os << "NA";
585 else if (lo_ieee_isnan (d))
586 os << "NaN";
587 else if (lo_ieee_isinf (d))
588 os << (d < 0 ? "-Inf" : "Inf");
589 else
590 os << d;
591 }
592
593 void
594 octave_write_float_complex (std::ostream& os, const FloatComplex& c)
595 {
596 os << "(";
597 octave_write_float (os, real (c));
598 os << ",";
599 octave_write_float (os, imag (c));
600 os << ")";
601 }
602
382 /* 603 /*
383 ;;; Local Variables: *** 604 ;;; Local Variables: ***
384 ;;; mode: C++ *** 605 ;;; mode: C++ ***
385 ;;; End: *** 606 ;;; End: ***
386 */ 607 */