123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505 |
- #pragma once
- #ifndef softfloat_h
- #define softfloat_h 1
- #include "cvdef.h"
- #if defined(_MSC_VER) && _MSC_VER < 1600
- namespace cv {
- typedef signed int int32_t;
- typedef unsigned int uint32_t;
- }
- #elif defined(_MSC_VER) || __cplusplus >= 201103L
- #include <cstdint>
- #else
- #include <stdint.h>
- #endif
- namespace cv
- {
- struct softfloat;
- struct softdouble;
- struct CV_EXPORTS softfloat
- {
- public:
-
- softfloat() { v = 0; }
-
- softfloat( const softfloat& c) { v = c.v; }
-
- softfloat& operator=( const softfloat& c )
- {
- if(&c != this) v = c.v;
- return *this;
- }
-
- static const softfloat fromRaw( const uint32_t a ) { softfloat x; x.v = a; return x; }
-
- explicit softfloat( const uint32_t );
- explicit softfloat( const uint64_t );
- explicit softfloat( const int32_t );
- explicit softfloat( const int64_t );
-
- explicit softfloat( const float a ) { Cv32suf s; s.f = a; v = s.u; }
-
- operator softdouble() const;
- operator float() const { Cv32suf s; s.u = v; return s.f; }
-
- softfloat operator + (const softfloat&) const;
- softfloat operator - (const softfloat&) const;
- softfloat operator * (const softfloat&) const;
- softfloat operator / (const softfloat&) const;
- softfloat operator - () const { softfloat x; x.v = v ^ (1U << 31); return x; }
-
- softfloat operator % (const softfloat&) const;
- softfloat& operator += (const softfloat& a) { *this = *this + a; return *this; }
- softfloat& operator -= (const softfloat& a) { *this = *this - a; return *this; }
- softfloat& operator *= (const softfloat& a) { *this = *this * a; return *this; }
- softfloat& operator /= (const softfloat& a) { *this = *this / a; return *this; }
- softfloat& operator %= (const softfloat& a) { *this = *this % a; return *this; }
-
- bool operator == ( const softfloat& ) const;
- bool operator != ( const softfloat& ) const;
- bool operator > ( const softfloat& ) const;
- bool operator >= ( const softfloat& ) const;
- bool operator < ( const softfloat& ) const;
- bool operator <= ( const softfloat& ) const;
-
- inline bool isNaN() const { return (v & 0x7fffffff) > 0x7f800000; }
-
- inline bool isInf() const { return (v & 0x7fffffff) == 0x7f800000; }
-
- inline bool isSubnormal() const { return ((v >> 23) & 0xFF) == 0; }
-
- inline bool getSign() const { return (v >> 31) != 0; }
-
- inline softfloat setSign(bool sign) const { softfloat x; x.v = (v & ((1U << 31) - 1)) | ((uint32_t)sign << 31); return x; }
-
- inline int getExp() const { return ((v >> 23) & 0xFF) - 127; }
-
- inline softfloat setExp(int e) const { softfloat x; x.v = (v & 0x807fffff) | (((e + 127) & 0xFF) << 23 ); return x; }
-
- inline softfloat getFrac() const
- {
- uint_fast32_t vv = (v & 0x007fffff) | (127 << 23);
- return softfloat::fromRaw(vv);
- }
-
- inline softfloat setFrac(const softfloat& s) const
- {
- softfloat x;
- x.v = (v & 0xff800000) | (s.v & 0x007fffff);
- return x;
- }
-
- static softfloat zero() { return softfloat::fromRaw( 0 ); }
-
- static softfloat inf() { return softfloat::fromRaw( 0xFF << 23 ); }
-
- static softfloat nan() { return softfloat::fromRaw( 0x7fffffff ); }
-
- static softfloat one() { return softfloat::fromRaw( 127 << 23 ); }
-
- static softfloat min() { return softfloat::fromRaw( 0x01 << 23 ); }
-
- static softfloat eps() { return softfloat::fromRaw( (127 - 23) << 23 ); }
-
- static softfloat max() { return softfloat::fromRaw( (0xFF << 23) - 1 ); }
-
- static softfloat pi() { return softfloat::fromRaw( 0x40490fdb ); }
- uint32_t v;
- };
- struct CV_EXPORTS softdouble
- {
- public:
-
- softdouble() : v(0) { }
-
- softdouble( const softdouble& c) { v = c.v; }
-
- softdouble& operator=( const softdouble& c )
- {
- if(&c != this) v = c.v;
- return *this;
- }
-
- static softdouble fromRaw( const uint64_t a ) { softdouble x; x.v = a; return x; }
-
- explicit softdouble( const uint32_t );
- explicit softdouble( const uint64_t );
- explicit softdouble( const int32_t );
- explicit softdouble( const int64_t );
-
- explicit softdouble( const double a ) { Cv64suf s; s.f = a; v = s.u; }
-
- operator softfloat() const;
- operator double() const { Cv64suf s; s.u = v; return s.f; }
-
- softdouble operator + (const softdouble&) const;
- softdouble operator - (const softdouble&) const;
- softdouble operator * (const softdouble&) const;
- softdouble operator / (const softdouble&) const;
- softdouble operator - () const { softdouble x; x.v = v ^ (1ULL << 63); return x; }
-
- softdouble operator % (const softdouble&) const;
- softdouble& operator += (const softdouble& a) { *this = *this + a; return *this; }
- softdouble& operator -= (const softdouble& a) { *this = *this - a; return *this; }
- softdouble& operator *= (const softdouble& a) { *this = *this * a; return *this; }
- softdouble& operator /= (const softdouble& a) { *this = *this / a; return *this; }
- softdouble& operator %= (const softdouble& a) { *this = *this % a; return *this; }
-
- bool operator == ( const softdouble& ) const;
- bool operator != ( const softdouble& ) const;
- bool operator > ( const softdouble& ) const;
- bool operator >= ( const softdouble& ) const;
- bool operator < ( const softdouble& ) const;
- bool operator <= ( const softdouble& ) const;
-
- inline bool isNaN() const { return (v & 0x7fffffffffffffff) > 0x7ff0000000000000; }
-
- inline bool isInf() const { return (v & 0x7fffffffffffffff) == 0x7ff0000000000000; }
-
- inline bool isSubnormal() const { return ((v >> 52) & 0x7FF) == 0; }
-
- inline bool getSign() const { return (v >> 63) != 0; }
-
- softdouble setSign(bool sign) const { softdouble x; x.v = (v & ((1ULL << 63) - 1)) | ((uint_fast64_t)(sign) << 63); return x; }
-
- inline int getExp() const { return ((v >> 52) & 0x7FF) - 1023; }
-
- inline softdouble setExp(int e) const
- {
- softdouble x;
- x.v = (v & 0x800FFFFFFFFFFFFF) | ((uint_fast64_t)((e + 1023) & 0x7FF) << 52);
- return x;
- }
-
- inline softdouble getFrac() const
- {
- uint_fast64_t vv = (v & 0x000FFFFFFFFFFFFF) | ((uint_fast64_t)(1023) << 52);
- return softdouble::fromRaw(vv);
- }
-
- inline softdouble setFrac(const softdouble& s) const
- {
- softdouble x;
- x.v = (v & 0xFFF0000000000000) | (s.v & 0x000FFFFFFFFFFFFF);
- return x;
- }
-
- static softdouble zero() { return softdouble::fromRaw( 0 ); }
-
- static softdouble inf() { return softdouble::fromRaw( (uint_fast64_t)(0x7FF) << 52 ); }
-
- static softdouble nan() { return softdouble::fromRaw( CV_BIG_INT(0x7FFFFFFFFFFFFFFF) ); }
-
- static softdouble one() { return softdouble::fromRaw( (uint_fast64_t)( 1023) << 52 ); }
-
- static softdouble min() { return softdouble::fromRaw( (uint_fast64_t)( 0x01) << 52 ); }
-
- static softdouble eps() { return softdouble::fromRaw( (uint_fast64_t)( 1023 - 52 ) << 52 ); }
-
- static softdouble max() { return softdouble::fromRaw( ((uint_fast64_t)(0x7FF) << 52) - 1 ); }
-
- static softdouble pi() { return softdouble::fromRaw( CV_BIG_INT(0x400921FB54442D18) ); }
- uint64_t v;
- };
- CV_EXPORTS softfloat mulAdd( const softfloat& a, const softfloat& b, const softfloat & c);
- CV_EXPORTS softdouble mulAdd( const softdouble& a, const softdouble& b, const softdouble& c);
- CV_EXPORTS softfloat sqrt( const softfloat& a );
- CV_EXPORTS softdouble sqrt( const softdouble& a );
- }
- CV_EXPORTS int cvTrunc(const cv::softfloat& a);
- CV_EXPORTS int cvTrunc(const cv::softdouble& a);
- CV_EXPORTS int cvRound(const cv::softfloat& a);
- CV_EXPORTS int cvRound(const cv::softdouble& a);
- CV_EXPORTS int cvFloor(const cv::softfloat& a);
- CV_EXPORTS int cvFloor(const cv::softdouble& a);
- CV_EXPORTS int cvCeil(const cv::softfloat& a);
- CV_EXPORTS int cvCeil(const cv::softdouble& a);
- namespace cv
- {
- template<typename _Tp> static inline _Tp saturate_cast(softfloat a) { return _Tp(a); }
- template<typename _Tp> static inline _Tp saturate_cast(softdouble a) { return _Tp(a); }
- template<> inline uchar saturate_cast<uchar>(softfloat a) { return (uchar)std::max(std::min(cvRound(a), (int)UCHAR_MAX), 0); }
- template<> inline uchar saturate_cast<uchar>(softdouble a) { return (uchar)std::max(std::min(cvRound(a), (int)UCHAR_MAX), 0); }
- template<> inline schar saturate_cast<schar>(softfloat a) { return (schar)std::min(std::max(cvRound(a), (int)SCHAR_MIN), (int)SCHAR_MAX); }
- template<> inline schar saturate_cast<schar>(softdouble a) { return (schar)std::min(std::max(cvRound(a), (int)SCHAR_MIN), (int)SCHAR_MAX); }
- template<> inline ushort saturate_cast<ushort>(softfloat a) { return (ushort)std::max(std::min(cvRound(a), (int)USHRT_MAX), 0); }
- template<> inline ushort saturate_cast<ushort>(softdouble a) { return (ushort)std::max(std::min(cvRound(a), (int)USHRT_MAX), 0); }
- template<> inline short saturate_cast<short>(softfloat a) { return (short)std::min(std::max(cvRound(a), (int)SHRT_MIN), (int)SHRT_MAX); }
- template<> inline short saturate_cast<short>(softdouble a) { return (short)std::min(std::max(cvRound(a), (int)SHRT_MIN), (int)SHRT_MAX); }
- template<> inline int saturate_cast<int>(softfloat a) { return cvRound(a); }
- template<> inline int saturate_cast<int>(softdouble a) { return cvRound(a); }
- template<> inline unsigned saturate_cast<unsigned>(softfloat a) { return cvRound(a); }
- template<> inline unsigned saturate_cast<unsigned>(softdouble a) { return cvRound(a); }
- inline softfloat min(const softfloat& a, const softfloat& b) { return (a > b) ? b : a; }
- inline softdouble min(const softdouble& a, const softdouble& b) { return (a > b) ? b : a; }
- inline softfloat max(const softfloat& a, const softfloat& b) { return (a > b) ? a : b; }
- inline softdouble max(const softdouble& a, const softdouble& b) { return (a > b) ? a : b; }
- inline softfloat abs( softfloat a) { softfloat x; x.v = a.v & ((1U << 31) - 1); return x; }
- inline softdouble abs( softdouble a) { softdouble x; x.v = a.v & ((1ULL << 63) - 1); return x; }
- CV_EXPORTS softfloat exp( const softfloat& a);
- CV_EXPORTS softdouble exp( const softdouble& a);
- CV_EXPORTS softfloat log( const softfloat& a );
- CV_EXPORTS softdouble log( const softdouble& a );
- CV_EXPORTS softfloat pow( const softfloat& a, const softfloat& b);
- CV_EXPORTS softdouble pow( const softdouble& a, const softdouble& b);
- CV_EXPORTS softfloat cbrt( const softfloat& a );
- CV_EXPORTS softdouble sin( const softdouble& a );
- CV_EXPORTS softdouble cos( const softdouble& a );
- }
- #endif
|