diff --git a/sdrbase/CMakeLists.txt b/sdrbase/CMakeLists.txt index 438101ce1..da3469fc7 100644 --- a/sdrbase/CMakeLists.txt +++ b/sdrbase/CMakeLists.txt @@ -62,6 +62,7 @@ set(sdrbase_SOURCES util/CRC64.cpp util/db.cpp + util/fixedtraits.cpp util/message.cpp util/messagequeue.cpp util/prettyprint.cpp @@ -168,6 +169,7 @@ set(sdrbase_HEADERS util/db.h util/doublebuffer.h util/export.h + util/fixedtraits.h util/message.h util/messagequeue.h util/movingaverage.h diff --git a/sdrbase/util/fixed.h b/sdrbase/util/fixed.h new file mode 100644 index 000000000..582c5e05d --- /dev/null +++ b/sdrbase/util/fixed.h @@ -0,0 +1,1983 @@ +/////////////////////////////////////////////////////////////////////////////////// +// (C) Copyright 2007 Anthony Williams // +// // +// Distributed under the Boost Software License, Version 1.0. // +// See: http://www.boost.org/LICENSE_1_0.txt) // +// // +// Copyright (C) 2018 Edouard Griffiths, F4EXB // +// // +// Modified as fully templatized class with variable size and type internal // +// representation // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#ifndef SDRBASE_UTIL_FIXED_H_ +#define SDRBASE_UTIL_FIXED_H_ + +#include +#include +#include + +#include "fixedtraits.h" + +// Internally 1 is 2^28. 28 is the highest power of two that can represent 9.99999... safely on 64 bits internally + +unsigned const fixed_resolution_shift=28; +int64_t const fixed_resolution=1LL< +class Fixed +{ +private: + IntType m_nVal; + +public: + + struct internal + {}; + + Fixed(): + m_nVal(0) + {} + + Fixed(internal, IntType nVal): + m_nVal(nVal) + {} + Fixed(int64_t nVal): + m_nVal(nVal << FixedTraits::fixed_resolution_shift) + {} + + Fixed(int nVal): + m_nVal(int64_t(nVal) << FixedTraits::fixed_resolution_shift) + {} + + Fixed(short nVal): + m_nVal(int64_t(nVal) << FixedTraits::fixed_resolution_shift) + {} + + Fixed(uint64_t nVal): + m_nVal(nVal << FixedTraits::fixed_resolution_shift) + {} + + Fixed(unsigned int nVal): + m_nVal(int64_t(nVal) << FixedTraits::fixed_resolution_shift) + {} + Fixed(unsigned short nVal): + m_nVal(int64_t(nVal) << FixedTraits::fixed_resolution_shift) + {} + Fixed(double nVal): + m_nVal(static_cast(nVal*static_cast(FixedTraits::fixed_resolution))) + {} + Fixed(float nVal): + m_nVal(static_cast(nVal*static_cast(FixedTraits::fixed_resolution))) + {} + + template + Fixed& operator=(T other) + { + m_nVal = Fixed(other).m_nVal; + return *this; + } + + Fixed& operator=(Fixed const& other) + { + m_nVal = other.m_nVal; + return *this; + } + + friend bool operator==(Fixed const& lhs,Fixed const& rhs) + { + return lhs.m_nVal == rhs.m_nVal; + } + + friend bool operator!=(Fixed const& lhs,Fixed const& rhs) + { + return lhs.m_nVal != rhs.m_nVal; + } + + friend bool operator<(Fixed const& lhs,Fixed const& rhs) + { + return lhs.m_nVal < rhs.m_nVal; + } + + friend bool operator>(Fixed const& lhs,Fixed const& rhs) + { + return lhs.m_nVal > rhs.m_nVal; + } + + friend bool operator<=(Fixed const& lhs,Fixed const& rhs) + { + return lhs.m_nVal <= rhs.m_nVal; + } + + friend bool operator>=(Fixed const& lhs,Fixed const& rhs) + { + return lhs.m_nVal >= rhs.m_nVal; + } + + operator bool() const + { + return m_nVal?true:false; + } + + inline operator double() const + { + return as_double(); + } + + int64_t as_internal() const + { + return m_nVal; + } + + float as_float() const + { + return m_nVal/(float)fixed_resolution; + } + + double as_double() const + { + return m_nVal/(double)fixed_resolution; + } + + int64_t as_long() const + { + return (int64_t)(m_nVal/fixed_resolution); + } + int64_t as_int64() const + { + return m_nVal/fixed_resolution; + } + + int as_int() const + { + return (int)(m_nVal/fixed_resolution); + } + + uint64_t as_unsigned_long() const + { + return (uint64_t)(m_nVal/fixed_resolution); + } + uint64_t as_unsigned_int64() const + { + return (uint64_t)m_nVal/fixed_resolution; + } + + unsigned int as_unsigned_int() const + { + return (unsigned int)(m_nVal/fixed_resolution); + } + + short as_short() const + { + return (short)(m_nVal/fixed_resolution); + } + + unsigned short as_unsigned_short() const + { + return (unsigned short)(m_nVal/fixed_resolution); + } + + Fixed operator++() + { + m_nVal += fixed_resolution; + return *this; + } + + Fixed operator--() + { + m_nVal -= fixed_resolution; + return *this; + } + + Fixed floor() const; + Fixed ceil() const; + Fixed sqrt() const; + Fixed exp() const; + Fixed log() const; + Fixed& operator%=(Fixed const& other); + Fixed& operator*=(Fixed const& val); + Fixed& operator/=(Fixed const& val); + + Fixed& operator-=(Fixed const& val) + { + m_nVal -= val.m_nVal; + return *this; + } + + Fixed& operator+=(Fixed const& val) + { + m_nVal += val.m_nVal; + return *this; + } + + Fixed& operator*=(double val) + { + return (*this)*=Fixed(val); + } + + Fixed& operator*=(float val) + { + return (*this)*=Fixed(val); + } + + Fixed& operator*=(int64_t val) + { + m_nVal*=val; + return *this; + } + + Fixed& operator*=(int val) + { + m_nVal*=val; + return *this; + } + + Fixed& operator*=(short val) + { + m_nVal*=val; + return *this; + } + + Fixed& operator*=(char val) + { + m_nVal*=val; + return *this; + } + + Fixed& operator*=(uint64_t val) + { + m_nVal*=val; + return *this; + } + + Fixed& operator*=(unsigned int val) + { + m_nVal*=val; + return *this; + } + + Fixed& operator*=(unsigned short val) + { + m_nVal*=val; + return *this; + } + + Fixed& operator*=(unsigned char val) + { + m_nVal*=val; + return *this; + } + + Fixed& operator/=(double val) + { + return (*this)/=Fixed(val); + } + + Fixed& operator/=(float val) + { + return (*this)/=Fixed(val); + } + + Fixed& operator/=(int64_t val) + { + m_nVal/=val; + return *this; + } + + Fixed& operator/=(int val) + { + m_nVal/=val; + return *this; + } + + Fixed& operator/=(short val) + { + m_nVal/=val; + return *this; + } + + Fixed& operator/=(char val) + { + m_nVal/=val; + return *this; + } + + Fixed& operator/=(uint64_t val) + { + m_nVal/=val; + return *this; + } + + Fixed& operator/=(unsigned int val) + { + m_nVal/=val; + return *this; + } + + Fixed& operator/=(unsigned short val) + { + m_nVal/=val; + return *this; + } + + Fixed& operator/=(unsigned char val) + { + m_nVal/=val; + return *this; + } + + bool operator!() const + { + return m_nVal==0; + } + + Fixed modf(Fixed* integral_part) const; + Fixed atan() const; + + static void sin_cos(Fixed const& theta,Fixed* s,Fixed*c); + static void to_polar(Fixed const& x,Fixed const& y,Fixed* r,Fixed*theta); + + Fixed sin() const; + Fixed cos() const; + Fixed tan() const; + Fixed operator-() const; + Fixed abs() const; +}; + +/* why always as double ? +inline std::ostream& operator<<(std::ostream& os,fixed const& value) +{ + return os< +inline Fixed operator-(double a, Fixed const& b) +{ + Fixed temp(a); + return temp -= b; +} + + +template +inline Fixed operator-(float a, Fixed const& b) +{ + Fixed temp(a); + return temp -= b; +} + +template +inline Fixed operator-(uint64_t a, Fixed const& b) +{ + Fixed temp(a); + return temp -= b; +} + +template +inline Fixed operator-(int64_t a, Fixed const& b) +{ + Fixed temp(a); + return temp -= b; +} + +template +inline Fixed operator-(unsigned a, Fixed const& b) +{ + Fixed temp(a); + return temp -= b; +} + +template +inline Fixed operator-(int a, Fixed const& b) +{ + Fixed temp(a); + return temp -= b; +} + +template +inline Fixed operator-(unsigned short a, Fixed const& b) +{ + Fixed temp(a); + return temp -= b; +} + +template +inline Fixed operator-(short a, Fixed const& b) +{ + Fixed temp(a); + return temp -= b; +} + +template +inline Fixed operator-(unsigned char a, Fixed const& b) +{ + Fixed temp(a); + return temp -= b; +} + +template +inline Fixed operator-(char a, Fixed const& b) +{ + Fixed temp(a); + return temp -= b; +} + +template +inline Fixed operator-(Fixed const& a, double b) +{ + Fixed temp(a); + return temp -= b; +} + +template +inline Fixed operator-(Fixed const& a, float b) +{ + Fixed temp(a); + return temp -= b; +} + +template +inline Fixed operator-(Fixed const& a,uint64_t b) +{ + Fixed temp(a); + return temp -= b; +} + +template +inline Fixed operator-(Fixed const& a, int64_t b) +{ + Fixed temp(a); + return temp -= b; +} + +template +inline Fixed operator-(Fixed const& a, unsigned b) +{ + Fixed temp(a); + return temp -= b; +} + +template +inline Fixed operator-(Fixed const& a, int b) +{ + Fixed temp(a); + return temp -= b; +} + +template +inline Fixed operator-(Fixed const& a, unsigned short b) +{ + Fixed temp(a); + return temp -= b; +} + +template +inline Fixed operator-(Fixed const& a, short b) +{ + Fixed temp(a); + return temp -= b; +} + +template +inline Fixed operator-(Fixed const& a, unsigned char b) +{ + Fixed temp(a); + return temp -= b; +} + +template +inline Fixed operator-(Fixed const& a, char b) +{ + Fixed temp(a); + return temp -= b; +} + +template +inline Fixed operator-(Fixed const& a, Fixed const& b) +{ + Fixed temp(a); + return temp -= b; +} + +template +inline Fixed operator%(double a, Fixed const& b) +{ + Fixed temp(a); + return temp %= b; +} + +template +inline Fixed operator%(float a, Fixed const& b) +{ + Fixed temp(a); + return temp %= b; +} + +template +inline Fixed operator%(uint64_t a, Fixed const& b) +{ + Fixed temp(a); + return temp %= b; +} + +template +inline Fixed operator%(int64_t a, Fixed const& b) +{ + Fixed temp(a); + return temp %= b; +} + +template +inline Fixed operator%(unsigned a, Fixed const& b) +{ + Fixed temp(a); + return temp %= b; +} + +template +inline Fixed operator%(int a, Fixed const& b) +{ + Fixed temp(a); + return temp %= b; +} + +template +inline Fixed operator%(unsigned short a, Fixed const& b) +{ + Fixed temp(a); + return temp %= b; +} + +template +inline Fixed operator%(short a, Fixed const& b) +{ + Fixed temp(a); + return temp %= b; +} + +template +inline Fixed operator%(unsigned char a, Fixed const& b) +{ + Fixed temp(a); + return temp %= b; +} + +template +inline Fixed operator%(char a, Fixed const& b) +{ + Fixed temp(a); + return temp %= b; +} + +template +inline Fixed operator%(Fixed const& a,double b) +{ + Fixed temp(a); + return temp %= b; +} + +template +inline Fixed operator%(Fixed const& a, float b) +{ + Fixed temp(a); + return temp %= b; +} + +template +inline Fixed operator%(Fixed const& a, uint64_t b) +{ + Fixed temp(a); + return temp %= b; +} + +template +inline Fixed operator%(Fixed const& a, int64_t b) +{ + Fixed temp(a); + return temp %= b; +} + +template +inline Fixed operator%(Fixed const& a, unsigned b) +{ + Fixed temp(a); + return temp %= b; +} + +template +inline Fixed operator%(Fixed const& a, int b) +{ + Fixed temp(a); + return temp %= b; +} + +template +inline Fixed operator%(Fixed const& a, unsigned short b) +{ + Fixed temp(a); + return temp %= b; +} + +template +inline Fixed operator%(Fixed const& a, short b) +{ + Fixed temp(a); + return temp %= b; +} + +template +inline Fixed operator%(Fixed const& a, unsigned char b) +{ + Fixed temp(a); + return temp %= b; +} + +template +inline Fixed operator%(Fixed const& a, char b) +{ + Fixed temp(a); + return temp %= b; +} + +template +inline Fixed operator%(Fixed const& a, Fixed const& b) +{ + Fixed temp(a); + return temp %= b; +} + +template +inline Fixed operator+(double a, Fixed const& b) +{ + Fixed temp(a); + return temp += b; +} + +template +inline Fixed operator+(float a, Fixed const& b) +{ + Fixed temp(a); + return temp += b; +} + +template +inline Fixed operator+(uint64_t a, Fixed const& b) +{ + Fixed temp(a); + return temp += b; +} + +template +inline Fixed operator+(int64_t a, Fixed const& b) +{ + Fixed temp(a); + return temp += b; +} + +template +inline Fixed operator+(unsigned a, Fixed const& b) +{ + Fixed temp(a); + return temp += b; +} + +template +inline Fixed operator+(int a, Fixed const& b) +{ + Fixed temp(a); + return temp += b; +} + +template +inline Fixed operator+(unsigned short a, Fixed const& b) +{ + Fixed temp(a); + return temp += b; +} + +template +inline Fixed operator+(short a, Fixed const& b) +{ + Fixed temp(a); + return temp += b; +} + +template +inline Fixed operator+(unsigned char a, Fixed const& b) +{ + Fixed temp(a); + return temp += b; +} + +template +inline Fixed operator+(char a, Fixed const& b) +{ + Fixed temp(a); + return temp += b; +} + +template +inline Fixed operator+(Fixed const& a, double b) +{ + Fixed temp(a); + return temp += b; +} + +template +inline Fixed operator+(Fixed const& a, float b) +{ + Fixed temp(a); + return temp += b; +} + +template +inline Fixed operator+(Fixed const& a, uint64_t b) +{ + Fixed temp(a); + return temp += b; +} + +template +inline Fixed operator+(Fixed const& a, int64_t b) +{ + Fixed temp(a); + return temp += b; +} + +template +inline Fixed operator+(Fixed const& a, unsigned b) +{ + Fixed temp(a); + return temp += b; +} + +template +inline Fixed operator+(Fixed const& a, int b) +{ + Fixed temp(a); + return temp += b; +} + +template +inline Fixed operator+(Fixed const& a, unsigned short b) +{ + Fixed temp(a); + return temp += b; +} + +template +inline Fixed operator+(Fixed const& a, short b) +{ + Fixed temp(a); + return temp += b; +} + +template +inline Fixed operator+(Fixed const& a, unsigned char b) +{ + Fixed temp(a); + return temp += b; +} + +template +inline Fixed operator+(Fixed const& a, char b) +{ + Fixed temp(a); + return temp += b; +} + +template +inline Fixed operator+(Fixed const& a, Fixed const& b) +{ + Fixed temp(a); + return temp += b; +} + +template +inline Fixed operator*(double a, Fixed const& b) +{ + Fixed temp(b); + return temp *= a; +} + +template +inline Fixed operator*(float a, Fixed const& b) +{ + Fixed temp(b); + return temp *= a; +} + +template +inline Fixed operator*(uint64_t a, Fixed const& b) +{ + Fixed temp(b); + return temp *= a; +} + +template +inline Fixed operator*(int64_t a, Fixed const& b) +{ + Fixed temp(b); + return temp *= a; +} + +template +inline Fixed operator*(unsigned a, Fixed const& b) +{ + Fixed temp(b); + return temp *= a; +} + +template +inline Fixed operator*(int a, Fixed const& b) +{ + Fixed temp(b); + return temp *= a; +} + +template +inline Fixed operator*(unsigned short a, Fixed const& b) +{ + Fixed temp(b); + return temp *= a; +} + +template +inline Fixed operator*(short a, Fixed const& b) +{ + Fixed temp(b); + return temp *= a; +} + +template +inline Fixed operator*(unsigned char a, Fixed const& b) +{ + Fixed temp(b); + return temp *= a; +} + +template +inline Fixed operator*(char a, Fixed const& b) +{ + Fixed temp(b); + return temp *= a; +} + +template +inline Fixed operator*(Fixed const& a, double b) +{ + Fixed temp(a); + return temp *= b; +} + +template +inline Fixed operator*(Fixed const& a, float b) +{ + Fixed temp(a); + return temp *= b; +} + +template +inline Fixed operator*(Fixed const& a, uint64_t b) +{ + Fixed temp(a); + return temp *= b; +} + +template +inline Fixed operator*(Fixed const& a, int64_t b) +{ + Fixed temp(a); + return temp *= b; +} + +template +inline Fixed operator*(Fixed const& a, unsigned b) +{ + Fixed temp(a); + return temp *= b; +} + +template +inline Fixed operator*(Fixed const& a, int b) +{ + Fixed temp(a); + return temp *= b; +} + +template +inline Fixed operator*(Fixed const& a, unsigned short b) +{ + Fixed temp(a); + return temp *= b; +} + +template +inline Fixed operator*(Fixed const& a, short b) +{ + Fixed temp(a); + return temp *= b; +} + +template +inline Fixed operator*(Fixed const& a, unsigned char b) +{ + Fixed temp(a); + return temp *= b; +} + +template +inline Fixed operator*(Fixed const& a, char b) +{ + Fixed temp(a); + return temp *= b; +} + +template +inline Fixed operator*(Fixed const& a, Fixed const& b) +{ + Fixed temp(a); + return temp *= b; +} + +template +inline Fixed operator/(double a, Fixed const& b) +{ + Fixed temp(a); + return temp /= b; +} + +template +inline Fixed operator/(float a, Fixed const& b) +{ + Fixed temp(a); + return temp /= b; +} + +template +inline Fixed operator/(uint64_t a, Fixed const& b) +{ + Fixed temp(a); + return temp /= b; +} + +template +inline Fixed operator/(int64_t a, Fixed const& b) +{ + Fixed temp(a); + return temp /= b; +} + +template +inline Fixed operator/(unsigned a, Fixed const& b) +{ + Fixed temp(a); + return temp /= b; +} + +template +inline Fixed operator/(int a, Fixed const& b) +{ + Fixed temp(a); + return temp /= b; +} + +template +inline Fixed operator/(unsigned short a, Fixed const& b) +{ + Fixed temp(a); + return temp /= b; +} + +template +inline Fixed operator/(short a, Fixed const& b) +{ + Fixed temp(a); + return temp /= b; +} + +template +inline Fixed operator/(unsigned char a, Fixed const& b) +{ + Fixed temp(a); + return temp /= b; +} + +template +inline Fixed operator/(char a, Fixed const& b) +{ + Fixed temp(a); + return temp /= b; +} + +template +inline Fixed operator/(Fixed const& a, double b) +{ + Fixed temp(a); + return temp /= b; +} + +template +inline Fixed operator/(Fixed const& a, float b) +{ + Fixed temp(a); + return temp /= b; +} + +template +inline Fixed operator/(Fixed const& a, uint64_t b) +{ + Fixed temp(a); + return temp /= b; +} + +template +inline Fixed operator/(Fixed const& a, int64_t b) +{ + Fixed temp(a); + return temp /= b; +} + +template +inline Fixed operator/(Fixed const& a, unsigned b) +{ + Fixed temp(a); + return temp /= b; +} + +template +inline Fixed operator/(Fixed const& a, int b) +{ + Fixed temp(a); + return temp /= b; +} + +template +inline Fixed operator/(Fixed const& a, unsigned short b) +{ + Fixed temp(a); + return temp /= b; +} + +template +inline Fixed operator/(Fixed const& a, short b) +{ + Fixed temp(a); + return temp /= b; +} + +template +inline Fixed operator/(Fixed const& a, unsigned char b) +{ + Fixed temp(a); + return temp /= b; +} + +template +inline Fixed operator/(Fixed const& a, char b) +{ + Fixed temp(a); + return temp /= b; +} + +template +inline Fixed operator/(Fixed const& a, Fixed const& b) +{ + Fixed temp(a); + return temp /= b; +} + +template +inline bool operator==(double a, Fixed const& b) +{ + return Fixed(a) == b; +} + +template +inline bool operator==(float a, Fixed const& b) +{ + return Fixed(a) == b; +} + +template +inline bool operator==(uint64_t a, Fixed const& b) +{ + return Fixed(a) == b; +} + +template +inline bool operator==(int64_t a, Fixed const& b) +{ + return Fixed(a) == b; +} + +template +inline bool operator==(unsigned a, Fixed const& b) +{ + return Fixed(a) == b; +} + +template +inline bool operator==(int a, Fixed const& b) +{ + return Fixed(a) == b; +} + +template +inline bool operator==(unsigned short a, Fixed const& b) +{ + return Fixed(a) == b; +} + +template +inline bool operator==(short a, Fixed const& b) +{ + return Fixed(a) == b; +} + +template +inline bool operator==(unsigned char a, Fixed const& b) +{ + return Fixed(a) == b; +} + +template +inline bool operator==(char a, Fixed const& b) +{ + return Fixed(a) == b; +} + +template +inline bool operator==(Fixed const& a, double b) +{ + return a == Fixed(b); +} + +template +inline bool operator==(Fixed const& a, float b) +{ + return a == Fixed(b); +} + +template +inline bool operator==(Fixed const& a, uint64_t b) +{ + return a == Fixed(b); +} + +template +inline bool operator==(Fixed const& a, int64_t b) +{ + return a == Fixed(b); +} + +template +inline bool operator==(Fixed const& a, unsigned b) +{ + return a == Fixed(b); +} + +template +inline bool operator==(Fixed const& a, int b) +{ + return a == Fixed(b); +} + +template +inline bool operator==(Fixed const& a, unsigned short b) +{ + return a == Fixed(b); +} + +template +inline bool operator==(Fixed const& a, short b) +{ + return a == Fixed(b); +} + +template +inline bool operator==(Fixed const& a, unsigned char b) +{ + return a == Fixed(b); +} + +template +inline bool operator==(Fixed const& a, char b) +{ + return a == Fixed(b); +} + +template +inline bool operator!=(double a, Fixed const& b) +{ + return Fixed(a) != b; +} + +template +inline bool operator!=(float a, Fixed const& b) +{ + return Fixed(a) != b; +} + +template +inline bool operator!=(uint64_t a, Fixed const& b) +{ + return Fixed(a) != b; +} + +template +inline bool operator!=(int64_t a, Fixed const& b) +{ + return Fixed(a) != b; +} + +template +inline bool operator!=(unsigned a, Fixed const& b) +{ + return Fixed(a) != b; +} + +template +inline bool operator!=(int a, Fixed const& b) +{ + return Fixed(a) != b; +} + +template +inline bool operator!=(unsigned short a, Fixed const& b) +{ + return Fixed(a) != b; +} + +template +inline bool operator!=(short a, Fixed const& b) +{ + return Fixed(a) != b; +} + +template +inline bool operator!=(unsigned char a, Fixed const& b) +{ + return Fixed(a) != b; +} + +template +inline bool operator!=(char a, Fixed const& b) +{ + return Fixed(a) != b; +} + +template +inline bool operator!=(Fixed const& a, double b) +{ + return a != Fixed(b); +} + +template +inline bool operator!=(Fixed const& a, float b) +{ + return a != Fixed(b); +} + +template +inline bool operator!=(Fixed const& a, uint64_t b) +{ + return a != Fixed(b); +} + +template +inline bool operator!=(Fixed const& a, int64_t b) +{ + return a != Fixed(b); +} + +template +inline bool operator!=(Fixed const& a, unsigned b) +{ + return a != Fixed(b); +} + +template +inline bool operator!=(Fixed const& a, int b) +{ + return a != Fixed(b); +} + +template +inline bool operator!=(Fixed const& a, unsigned short b) +{ + return a != Fixed(b); +} + +template +inline bool operator!=(Fixed const& a, short b) +{ + return a != Fixed(b); +} + +template +inline bool operator!=(Fixed const& a, unsigned char b) +{ + return a != Fixed(b); +} + +template +inline bool operator!=(Fixed const& a, char b) +{ + return a != Fixed(b); +} + +template +inline bool operator<(double a, Fixed const& b) +{ + return Fixed(a) < b; +} + +template +inline bool operator<(float a, Fixed const& b) +{ + return Fixed(a) < b; +} + +template +inline bool operator<(uint64_t a, Fixed const& b) +{ + return Fixed(a) < b; +} + +template +inline bool operator<(int64_t a, Fixed const& b) +{ + return Fixed(a) < b; +} + +template +inline bool operator<(unsigned a, Fixed const& b) +{ + return Fixed(a) < b; +} + +template +inline bool operator<(int a, Fixed const& b) +{ + return Fixed(a) < b; +} + +template +inline bool operator<(unsigned short a, Fixed const& b) +{ + return Fixed(a) < b; +} + +template +inline bool operator<(short a, Fixed const& b) +{ + return Fixed(a) +inline bool operator<(unsigned char a, Fixed const& b) +{ + return Fixed(a) < b; +} + +template +inline bool operator<(char a, Fixed const& b) +{ + return Fixed(a) < b; +} + +template +inline bool operator<(Fixed const& a, double b) +{ + return a < Fixed(b); +} + +template +inline bool operator<(Fixed const& a, float b) +{ + return a < Fixed(b); +} + +template +inline bool operator<(Fixed const& a, uint64_t b) +{ + return a < Fixed(b); +} + +template +inline bool operator<(Fixed const& a, int64_t b) +{ + return a < Fixed(b); +} + +template +inline bool operator<(Fixed const& a, unsigned b) +{ + return a < Fixed(b); +} + +template +inline bool operator<(Fixed const& a, int b) +{ + return a < Fixed(b); +} + +template +inline bool operator<(Fixed const& a, unsigned short b) +{ + return a < Fixed(b); +} + +template +inline bool operator<(Fixed const& a, short b) +{ + return a < Fixed(b); +} + +template +inline bool operator<(Fixed const& a, unsigned char b) +{ + return a < Fixed(b); +} + +template +inline bool operator<(Fixed const& a, char b) +{ + return a < Fixed(b); +} + +template +inline bool operator>(double a, Fixed const& b) +{ + return Fixed(a) > b; +} + +template +inline bool operator>(float a, Fixed const& b) +{ + return Fixed(a) > b; +} + +template +inline bool operator>(uint64_t a, Fixed const& b) +{ + return Fixed(a) > b; +} + +template +inline bool operator>(int64_t a, Fixed const& b) +{ + return Fixed(a) > b; +} + +template +inline bool operator>(unsigned a, Fixed const& b) +{ + return Fixed(a) > b; +} + +template +inline bool operator>(int a, Fixed const& b) +{ + return Fixed(a) > b; +} + +template +inline bool operator>(unsigned short a, Fixed const& b) +{ + return Fixed(a) > b; +} + +template +inline bool operator>(short a, Fixed const& b) +{ + return Fixed(a) > b; +} + +template +inline bool operator>(unsigned char a, Fixed const& b) +{ + return Fixed(a) > b; +} + +template +inline bool operator>(char a, Fixed const& b) +{ + return Fixed(a) > b; +} + +template +inline bool operator>(Fixed const& a, double b) +{ + return a > Fixed(b); +} + +template +inline bool operator>(Fixed const& a, float b) +{ + return a > Fixed(b); +} + +template +inline bool operator>(Fixed const& a, uint64_t b) +{ + return a > Fixed(b); +} + +template +inline bool operator>(Fixed const& a, int64_t b) +{ + return a > Fixed(b); +} + +template +inline bool operator>(Fixed const& a, unsigned b) +{ + return a > Fixed(b); +} + +template +inline bool operator>(Fixed const& a, int b) +{ + return a > Fixed(b); +} + +template +inline bool operator>(Fixed const& a, unsigned short b) +{ + return a > Fixed(b); +} + +template +inline bool operator>(Fixed const& a, short b) +{ + return a > Fixed(b); +} + +template +inline bool operator>(Fixed const& a, unsigned char b) +{ + return a > Fixed(b); +} + +template +inline bool operator>(Fixed const& a, char b) +{ + return a > Fixed(b); +} + +template +inline bool operator<=(double a, Fixed const& b) +{ + return Fixed(a) <= b; +} + +template +inline bool operator<=(float a, Fixed const& b) +{ + return Fixed(a) <= b; +} + +template +inline bool operator<=(uint64_t a, Fixed const& b) +{ + return Fixed(a) <= b; +} + +template +inline bool operator<=(int64_t a, Fixed const& b) +{ + return Fixed(a) <= b; +} + +template +inline bool operator<=(unsigned a, Fixed const& b) +{ + return Fixed(a) <= b; +} + +template +inline bool operator<=(int a, Fixed const& b) +{ + return Fixed(a) <= b; +} + +template +inline bool operator<=(unsigned short a, Fixed const& b) +{ + return Fixed(a) <= b; +} + +template +inline bool operator<=(short a, Fixed const& b) +{ + return Fixed(a) <= b; +} + +template +inline bool operator<=(unsigned char a, Fixed const& b) +{ + return Fixed(a) <= b; +} + +template +inline bool operator<=(char a, Fixed const& b) +{ + return Fixed(a) <= b; +} + +template +inline bool operator<=(Fixed const& a, double b) +{ + return a <= Fixed(b); +} + +template +inline bool operator<=(Fixed const& a, float b) +{ + return a <= Fixed(b); +} + +template +inline bool operator<=(Fixed const& a, uint64_t b) +{ + return a <= Fixed(b); +} + +template +inline bool operator<=(Fixed const& a, int64_t b) +{ + return a <= Fixed(b); +} + +template +inline bool operator<=(Fixed const& a, unsigned b) +{ + return a <= Fixed(b); +} + +template +inline bool operator<=(Fixed const& a, int b) +{ + return a <= Fixed(b); +} + +template +inline bool operator<=(Fixed const& a, unsigned short b) +{ + return a <= Fixed(b); +} + +template +inline bool operator<=(Fixed const& a, short b) +{ + return a <= Fixed(b); +} + +template +inline bool operator<=(Fixed const& a,unsigned char b) +{ + return a <= Fixed(b); +} + +template +inline bool operator<=(Fixed const& a,char b) +{ + return a <= Fixed(b); +} + +template +inline bool operator>=(double a, Fixed const& b) +{ + return Fixed(a) >= b; +} + +template +inline bool operator>=(float a, Fixed const& b) +{ + return Fixed(a) >= b; +} + +template +inline bool operator>=(uint64_t a, Fixed const& b) +{ + return Fixed(a) >= b; +} + +template +inline bool operator>=(int64_t a, Fixed const& b) +{ + return Fixed(a) >= b; +} + +template +inline bool operator>=(unsigned a, Fixed const& b) +{ + return Fixed(a) >= b; +} + +template +inline bool operator>=(int a, Fixed const& b) +{ + return Fixed(a) >= b; +} + +template +inline bool operator>=(unsigned short a, Fixed const& b) +{ + return Fixed(a) >= b; +} + +template +inline bool operator>=(short a, Fixed const& b) +{ + return Fixed(a)>=b; +} + +template +inline bool operator>=(unsigned char a, Fixed const& b) +{ + return Fixed(a) >= b; +} + +template +inline bool operator>=(char a, Fixed const& b) +{ + return Fixed(a) >= b; +} + +template +inline bool operator>=(Fixed const& a, double b) +{ + return a >= Fixed(b); +} + +template +inline bool operator>=(Fixed const& a,float b) +{ + return a >= Fixed(b); +} + +template +inline bool operator>=(Fixed const& a, uint64_t b) +{ + return a >= Fixed(b); +} + +template +inline bool operator>=(Fixed const& a, int64_t b) +{ + return a >= Fixed(b); +} + +template +inline bool operator>=(Fixed const& a, unsigned b) +{ + return a >= Fixed(b); +} + +template +inline bool operator>=(Fixed const& a, int b) +{ + return a >= Fixed(b); +} + +template +inline bool operator>=(Fixed const& a, unsigned short b) +{ + return a >= Fixed(b); +} + +template +inline bool operator>=(Fixed const& a, short b) +{ + return a >= Fixed(b); +} + +template +inline bool operator>=(Fixed const& a, unsigned char b) +{ + return a >= Fixed(b); +} + +template +inline bool operator>=(Fixed const& a, char b) +{ + return a >= Fixed(b); +} + +template +inline Fixed sin(Fixed const& x) +{ + return x.sin(); +} + +template +inline Fixed cos(Fixed const& x) +{ + return x.cos(); +} + +template +inline Fixed tan(Fixed const& x) +{ + return x.tan(); +} + +template +inline Fixed sqrt(Fixed const& x) +{ + return x.sqrt(); +} + +template +inline Fixed exp(Fixed const& x) +{ + return x.exp(); +} + +template +inline Fixed log(Fixed const& x) +{ + return x.log(); +} + +template +inline Fixed floor(Fixed const& x) +{ + return x.floor(); +} + +template +inline Fixed ceil(Fixed const& x) +{ + return x.ceil(); +} + +template +inline Fixed abs(Fixed const& x) +{ + return x.abs(); +} + +template +inline Fixed modf(Fixed const& x, Fixed*integral_part) +{ + return x.modf(integral_part); +} + +template +inline Fixed Fixed::ceil() const +{ + if(m_nVal%fixed_resolution) + { + return floor()+1; + } + else + { + return *this; + } +} + +template +inline Fixed Fixed::floor() const +{ + Fixed res(*this); + int64_t const remainder=m_nVal%fixed_resolution; + if(remainder) + { + res.m_nVal-=remainder; + if(m_nVal<0) + { + res-=1; + } + } + return res; +} + +template +inline Fixed Fixed::sin() const +{ + Fixed res; + sin_cos(*this,&res,0); + return res; +} + +template +inline Fixed Fixed::cos() const +{ + Fixed res; + sin_cos(*this,0,&res); + return res; +} + +template +inline Fixed Fixed::tan() const +{ + Fixed s,c; + sin_cos(*this,&s,&c); + return s/c; +} + +template +inline Fixed Fixed::operator-() const +{ + return Fixed(internal(),-m_nVal); +} + +template +inline Fixed Fixed::abs() const +{ + return Fixed(internal(),m_nVal<0?-m_nVal:m_nVal); +} + +template +inline Fixed Fixed::modf(Fixed*integral_part) const +{ + int64_t fractional_part=m_nVal%fixed_resolution; + if(m_nVal<0 && fractional_part>0) + { + fractional_part-=fixed_resolution; + } + integral_part->m_nVal=m_nVal-fractional_part; + return Fixed(internal(),fractional_part); +} + +template +inline Fixed arg(const std::complex>& val) +{ + Fixed r,theta; + Fixed::to_polar(val.real(),val.imag(),&r,&theta); + return theta; +} + +template +inline std::complex> polar(Fixed const& rho, Fixed const& theta) +{ + Fixed s,c; + Fixed::sin_cos(theta,&s,&c); + return std::complex>(rho * c, rho * s); +} + +Fixed const fixed_max(Fixed::internal(),0x7fffffffffffffffLL); +Fixed const fixed_one(Fixed::internal(),1LL<<(fixed_resolution_shift)); +Fixed const fixed_zero(Fixed::internal(),0); +Fixed const fixed_half(Fixed::internal(),1LL<<(fixed_resolution_shift-1)); +extern Fixed const fixed_pi; +extern Fixed const fixed_two_pi; +extern Fixed const fixed_half_pi; +extern Fixed const fixed_quarter_pi; + +#endif /* SDRBASE_UTIL_FIXED_H_ */ diff --git a/sdrbase/util/fixedtraits.cpp b/sdrbase/util/fixedtraits.cpp new file mode 100644 index 000000000..168b260c0 --- /dev/null +++ b/sdrbase/util/fixedtraits.cpp @@ -0,0 +1,46 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2018 F4EXB // +// written by Edouard Griffiths // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#include "fixedtraits.h" + +const int64_t FixedTraits<28>::log_two_power_n_reversed[35] = { + 0x18429946ELL,0x1791272EFLL,0x16DFB516FLL,0x162E42FF0LL,0x157CD0E70LL,0x14CB5ECF1LL,0x1419ECB71LL,0x13687A9F2LL, + 0x12B708872LL,0x1205966F3LL,0x115424573LL,0x10A2B23F4LL,0xFF140274LL,0xF3FCE0F5LL,0xE8E5BF75LL,0xDDCE9DF6LL, + 0xD2B77C76LL,0xC7A05AF7LL,0xBC893977LL,0xB17217F8LL,0xA65AF679LL,0x9B43D4F9LL,0x902CB379LL,0x851591FaLL, + 0x79FE707bLL,0x6EE74EFbLL,0x63D02D7BLL,0x58B90BFcLL,0x4DA1EA7CLL,0x428AC8FdLL,0x3773A77DLL,0x2C5C85FeLL, + 0x2145647ELL,0x162E42FfLL,0xB17217FLL +}; + +const int64_t FixedTraits<28>::log_one_plus_two_power_minus_n[28] = { + 0x67CC8FBLL,0x391FEF9LL,0x1E27077LL,0xF85186LL, + 0x7E0A6CLL,0x3F8151LL,0x1FE02ALL,0xFF805LL,0x7FE01LL,0x3FF80LL,0x1FFE0LL,0xFFF8LL, + 0x7FFELL,0x4000LL,0x2000LL,0x1000LL,0x800LL,0x400LL,0x200LL,0x100LL, + 0x80LL,0x40LL,0x20LL,0x10LL,0x8LL,0x4LL,0x2LL,0x1LL +}; + +const int64_t FixedTraits<28>::log_one_over_one_minus_two_power_minus_n[28] = { + 0xB172180LL,0x49A5884LL,0x222F1D0LL,0x108598BLL, + 0x820AECLL,0x408159LL,0x20202BLL,0x100805LL,0x80201LL,0x40080LL,0x20020LL,0x10008LL, + 0x8002LL,0x4001LL,0x2000LL,0x1000LL,0x800LL,0x400LL,0x200LL,0x100LL, + 0x80LL,0x40LL,0x20LL,0x10LL,0x8LL,0x4LL,0x2LL,0x1LL +}; + +const int64_t FixedTraits<28>::arctantab[32] = { + 297197971, 210828714, 124459457, 65760959, 33381290, 16755422, 8385879, + 4193963, 2097109, 1048571, 524287, 262144, 131072, 65536, 32768, 16384, + 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1, 0, 0, +}; diff --git a/sdrbase/util/fixedtraits.h b/sdrbase/util/fixedtraits.h new file mode 100644 index 000000000..3af812573 --- /dev/null +++ b/sdrbase/util/fixedtraits.h @@ -0,0 +1,43 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2018 F4EXB // +// written by Edouard Griffiths // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#ifndef SDRBASE_UTIL_FIXEDTRAITS_H_ +#define SDRBASE_UTIL_FIXEDTRAITS_H_ + +#include + +template +class FixedTraits +{ +}; + +template<> +class FixedTraits<28> +{ + static const uint32_t fixed_resolution_shift = 28; //!< 1.0 representation. 28 is the highest power of two that can represent 9.99999... safely on 64 bits internally + static const int64_t fixed_resolution = 1LL << 28; + static const int64_t internal_pi = 0x3243f6a8; + static const int64_t internal_two_pi = 0x6487ed51; + static const int64_t internal_half_pi = 0x1921fb54; + static const int64_t internal_quarter_pi = 0xc90fdaa; + static const int64_t log_two_power_n_reversed[35]; // 35 = 63 - 28 + static const int64_t log_one_plus_two_power_minus_n[28]; + static const int64_t log_one_over_one_minus_two_power_minus_n[28]; + static const int64_t arctantab[32]; +}; + +#endif /* SDRBASE_UTIL_FIXEDTRAITS_H_ */