Mercurial > hg > octave-lyh
comparison liboctave/lo-mappers.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 | 39930366b709 |
children | 76142609e8d2 |
comparison
equal
deleted
inserted
replaced
7788:45f5faba05a2 | 7789:82be108cc558 |
---|---|
394 xmax (const Complex& x, const Complex& y) | 394 xmax (const Complex& x, const Complex& y) |
395 { | 395 { |
396 return abs (x) >= abs (y) ? x : (xisnan (x) ? x : y); | 396 return abs (x) >= abs (y) ? x : (xisnan (x) ? x : y); |
397 } | 397 } |
398 | 398 |
399 | |
400 // float -> float mappers. | |
401 | |
402 float | |
403 arg (float x) | |
404 { | |
405 return atan2 (0.0, x); | |
406 } | |
407 | |
408 float | |
409 conj (float x) | |
410 { | |
411 return x; | |
412 } | |
413 | |
414 float | |
415 fix (float x) | |
416 { | |
417 return x > 0 ? floor (x) : ceil (x); | |
418 } | |
419 | |
420 float | |
421 imag (float) | |
422 { | |
423 return 0.0; | |
424 } | |
425 | |
426 float | |
427 real (float x) | |
428 { | |
429 return x; | |
430 } | |
431 | |
432 float | |
433 xround (float x) | |
434 { | |
435 #if defined (HAVE_ROUND) | |
436 return round (x); | |
437 #else | |
438 if (x >= 0) | |
439 { | |
440 float y = floor (x); | |
441 | |
442 if ((x - y) >= 0.5) | |
443 y += 1.0; | |
444 | |
445 return y; | |
446 } | |
447 else | |
448 { | |
449 float y = ceil (x); | |
450 | |
451 if ((y - x) >= 0.5) | |
452 y -= 1.0; | |
453 | |
454 return y; | |
455 } | |
456 #endif | |
457 } | |
458 | |
459 float | |
460 xtrunc (float x) | |
461 { | |
462 #if defined (HAVE_TRUNC) | |
463 return trunc (x); | |
464 #else | |
465 return x > 0 ? floor (x) : ceil (x); | |
466 #endif | |
467 } | |
468 | |
469 float | |
470 xroundb (float x) | |
471 { | |
472 float t = xround (x); | |
473 | |
474 if (fabs (x - t) == 0.5) | |
475 t = 2 * xtrunc (0.5 * t); | |
476 | |
477 return t; | |
478 } | |
479 | |
480 float | |
481 signum (float x) | |
482 { | |
483 float tmp = 0.0; | |
484 | |
485 if (x < 0.0) | |
486 tmp = -1.0; | |
487 else if (x > 0.0) | |
488 tmp = 1.0; | |
489 | |
490 return xisnan (x) ? octave_Float_NaN : tmp; | |
491 } | |
492 | |
493 float | |
494 xlog2 (float x) | |
495 { | |
496 #if defined (HAVE_LOG2) | |
497 return log2 (x); | |
498 #else | |
499 #if defined (M_LN2) | |
500 static float ln2 = M_LN2; | |
501 #else | |
502 static float ln2 = log2 (2); | |
503 #endif | |
504 | |
505 return log (x) / ln2; | |
506 #endif | |
507 } | |
508 | |
509 FloatComplex | |
510 xlog2 (const FloatComplex& x) | |
511 { | |
512 #if defined (M_LN2) | |
513 static float ln2 = M_LN2; | |
514 #else | |
515 static float ln2 = log (2); | |
516 #endif | |
517 | |
518 return std::log (x) / ln2; | |
519 } | |
520 | |
521 float | |
522 xexp2 (float x) | |
523 { | |
524 #if defined (HAVE_EXP2) | |
525 return exp2 (x); | |
526 #else | |
527 #if defined (M_LN2) | |
528 static float ln2 = M_LN2; | |
529 #else | |
530 static float ln2 = log2 (2); | |
531 #endif | |
532 | |
533 return exp (x * ln2); | |
534 #endif | |
535 } | |
536 | |
537 float | |
538 xlog2 (float x, int& exp) | |
539 { | |
540 return frexpf (x, &exp); | |
541 } | |
542 | |
543 FloatComplex | |
544 xlog2 (const FloatComplex& x, int& exp) | |
545 { | |
546 float ax = std::abs (x); | |
547 float lax = xlog2 (ax, exp); | |
548 return (exp == 0) ? x : (x / ax) * lax; | |
549 } | |
550 | |
551 // float -> bool mappers. | |
552 | |
553 bool | |
554 xisnan (float x) | |
555 { | |
556 return lo_ieee_isnan (x); | |
557 } | |
558 | |
559 bool | |
560 xfinite (float x) | |
561 { | |
562 return lo_ieee_finite (x); | |
563 } | |
564 | |
565 bool | |
566 xisinf (float x) | |
567 { | |
568 return lo_ieee_isinf (x); | |
569 } | |
570 | |
571 bool | |
572 octave_is_NA (float x) | |
573 { | |
574 return lo_ieee_is_NA (x); | |
575 } | |
576 | |
577 bool | |
578 octave_is_NaN_or_NA (float x) | |
579 { | |
580 return lo_ieee_isnan (x); | |
581 } | |
582 | |
583 // (float, float) -> float mappers. | |
584 | |
585 // FIXME -- need to handle NA too? | |
586 | |
587 float | |
588 xmin (float x, float y) | |
589 { | |
590 if (x < y) | |
591 return x; | |
592 | |
593 if (y <= x) | |
594 return y; | |
595 | |
596 if (xisnan (x) && ! xisnan (y)) | |
597 return y; | |
598 else if (xisnan (y) && ! xisnan (x)) | |
599 return x; | |
600 else if (octave_is_NA (x) || octave_is_NA (y)) | |
601 return octave_Float_NA; | |
602 else | |
603 return octave_Float_NaN; | |
604 } | |
605 | |
606 float | |
607 xmax (float x, float y) | |
608 { | |
609 if (x > y) | |
610 return x; | |
611 | |
612 if (y >= x) | |
613 return y; | |
614 | |
615 if (xisnan (x) && ! xisnan (y)) | |
616 return y; | |
617 else if (xisnan (y) && ! xisnan (x)) | |
618 return x; | |
619 else if (octave_is_NA (x) || octave_is_NA (y)) | |
620 return octave_Float_NA; | |
621 else | |
622 return octave_Float_NaN; | |
623 } | |
624 | |
625 // complex -> complex mappers. | |
626 | |
627 FloatComplex | |
628 acos (const FloatComplex& x) | |
629 { | |
630 static FloatComplex i (0, 1); | |
631 | |
632 return -i * (log (x + i * (sqrt (static_cast<float>(1.0) - x*x)))); | |
633 } | |
634 | |
635 FloatComplex | |
636 acosh (const FloatComplex& x) | |
637 { | |
638 return log (x + sqrt (x*x - static_cast<float>(1.0))); | |
639 } | |
640 | |
641 FloatComplex | |
642 asin (const FloatComplex& x) | |
643 { | |
644 static FloatComplex i (0, 1); | |
645 | |
646 return -i * log (i*x + sqrt (static_cast<float>(1.0) - x*x)); | |
647 } | |
648 | |
649 FloatComplex | |
650 asinh (const FloatComplex& x) | |
651 { | |
652 return log (x + sqrt (x*x + static_cast<float>(1.0))); | |
653 } | |
654 | |
655 FloatComplex | |
656 atan (const FloatComplex& x) | |
657 { | |
658 static FloatComplex i (0, 1); | |
659 | |
660 return i * log ((i + x) / (i - x)) / static_cast<float>(2.0); | |
661 } | |
662 | |
663 FloatComplex | |
664 atanh (const FloatComplex& x) | |
665 { | |
666 return log ((static_cast<float>(1.0) + x) / (static_cast<float>(1.0) - x)) / static_cast<float>(2.0); | |
667 } | |
668 | |
669 FloatComplex | |
670 ceil (const FloatComplex& x) | |
671 { | |
672 return FloatComplex (ceil (real (x)), ceil (imag (x))); | |
673 } | |
674 | |
675 FloatComplex | |
676 fix (const FloatComplex& x) | |
677 { | |
678 return FloatComplex (fix (real (x)), fix (imag (x))); | |
679 } | |
680 | |
681 FloatComplex | |
682 floor (const FloatComplex& x) | |
683 { | |
684 return FloatComplex (floor (real (x)), floor (imag (x))); | |
685 } | |
686 | |
687 FloatComplex | |
688 xround (const FloatComplex& x) | |
689 { | |
690 return FloatComplex (xround (real (x)), xround (imag (x))); | |
691 } | |
692 | |
693 FloatComplex | |
694 xroundb (const FloatComplex& x) | |
695 { | |
696 return FloatComplex (xroundb (real (x)), xroundb (imag (x))); | |
697 } | |
698 | |
699 FloatComplex | |
700 signum (const FloatComplex& x) | |
701 { | |
702 float tmp = abs (x); | |
703 | |
704 return tmp == 0 ? 0.0 : x / tmp; | |
705 } | |
706 | |
707 // complex -> bool mappers. | |
708 | |
709 bool | |
710 xisnan (const FloatComplex& x) | |
711 { | |
712 return (xisnan (real (x)) || xisnan (imag (x))); | |
713 } | |
714 | |
715 bool | |
716 xfinite (const FloatComplex& x) | |
717 { | |
718 float rx = real (x); | |
719 float ix = imag (x); | |
720 | |
721 return (xfinite (rx) && ! xisnan (rx) | |
722 && xfinite (ix) && ! xisnan (ix)); | |
723 } | |
724 | |
725 bool | |
726 xisinf (const FloatComplex& x) | |
727 { | |
728 return (xisinf (real (x)) || xisinf (imag (x))); | |
729 } | |
730 | |
731 bool | |
732 octave_is_NA (const FloatComplex& x) | |
733 { | |
734 return (octave_is_NA (real (x)) || octave_is_NA (imag (x))); | |
735 } | |
736 | |
737 bool | |
738 octave_is_NaN_or_NA (const FloatComplex& x) | |
739 { | |
740 return (xisnan (real (x)) || xisnan (imag (x))); | |
741 } | |
742 | |
743 // (complex, complex) -> complex mappers. | |
744 | |
745 // FIXME -- need to handle NA too? | |
746 | |
747 FloatComplex | |
748 xmin (const FloatComplex& x, const FloatComplex& y) | |
749 { | |
750 return abs (x) <= abs (y) ? x : (xisnan (x) ? x : y); | |
751 } | |
752 | |
753 FloatComplex | |
754 xmax (const FloatComplex& x, const FloatComplex& y) | |
755 { | |
756 return abs (x) >= abs (y) ? x : (xisnan (x) ? x : y); | |
757 } | |
758 | |
399 /* | 759 /* |
400 ;;; Local Variables: *** | 760 ;;; Local Variables: *** |
401 ;;; mode: C++ *** | 761 ;;; mode: C++ *** |
402 ;;; End: *** | 762 ;;; End: *** |
403 */ | 763 */ |