Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef GNASH_NUMERIC_H
00022 #define GNASH_NUMERIC_H
00023
00024 #ifdef HAVE_CONFIG_H
00025 # include "gnashconfig.h"
00026 #endif
00027
00028 #ifdef SOLARIS_HOST
00029 # include <ieeefp.h>
00030 #endif
00031
00032 #include <cmath>
00033 #include <algorithm>
00034 #include <boost/cstdint.hpp>
00035 #include <limits>
00036
00037 namespace gnash {
00038
00039
00040
00041
00042 static const double PI = 3.14159265358979323846;
00043
00044 inline bool
00045 isFinite(double d)
00046 {
00047 #if defined(HAVE_FINITE) && !defined(HAVE_ISFINITE)
00048 return (finite(d));
00049 #else
00050
00051
00052 using namespace std;
00053 return (isfinite(d));
00054 #endif
00055 }
00056
00057 inline double
00058 infinite_to_zero(double x)
00059 {
00060 return isFinite(x) ? x : 0.0;
00061 }
00062
00063 template <typename T>
00064 inline T
00065 clamp(T i, T min, T max)
00066 {
00067 assert(min <= max);
00068 return std::max<T>(min, std::min<T>(i, max));
00069 }
00070
00071 template<typename T>
00072 inline T
00073 lerp(T a, T b, T f)
00074 {
00075 return (b - a) * f + a;
00076 }
00077
00078 inline int
00079 frnd(float f)
00080 {
00081 return static_cast<int>(f + 0.5f);
00082 }
00083
00084 inline double
00085 twipsToPixels(int i)
00086 {
00087 return static_cast<double>(i / 20.0);
00088 }
00089
00090 template<size_t Factor>
00091 boost::int32_t
00092 truncateWithFactor(double a)
00093 {
00094
00095 const double factor = static_cast<double>(Factor);
00096
00097
00098
00099
00100
00101
00102
00103
00104 static const double upperUnsignedLimit =
00105 std::numeric_limits<boost::uint32_t>::max() + 1.0;
00106 static const double upperSignedLimit =
00107 std::numeric_limits<boost::int32_t>::max() / factor;
00108 static const double lowerSignedLimit =
00109 std::numeric_limits<boost::int32_t>::min() / factor;
00110
00111 if (a >= lowerSignedLimit && a <= upperSignedLimit) {
00112 return static_cast<boost::int32_t>(a * factor);
00113 }
00114
00115
00116 return a >= 0 ?
00117 static_cast<boost::uint32_t>(
00118 std::fmod(a * factor, upperUnsignedLimit))
00119 :
00120 -static_cast<boost::uint32_t>(
00121 std::fmod(-a * factor, upperUnsignedLimit));
00122 }
00123
00124
00125 inline boost::int32_t
00126 pixelsToTwips(double a)
00127 {
00128 return truncateWithFactor<20>(a);
00129 }
00130
00131 }
00132
00133 #endif