Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ShFraction.hpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright (c) 2003 University of Waterloo Computer Graphics Laboratory
00004 // Project administrator: Michael D. McCool
00005 // Authors: Zheng Qin, Stefanus Du Toit, Kevin Moule, Tiberiu S. Popa,
00006 //          Michael D. McCool
00007 // 
00008 // This software is provided 'as-is', without any express or implied
00009 // warranty. In no event will the authors be held liable for any damages
00010 // arising from the use of this software.
00011 // 
00012 // Permission is granted to anyone to use this software for any purpose,
00013 // including commercial applications, and to alter it and redistribute it
00014 // freely, subject to the following restrictions:
00015 // 
00016 // 1. The origin of this software must not be misrepresented; you must
00017 // not claim that you wrote the original software. If you use this
00018 // software in a product, an acknowledgment in the product documentation
00019 // would be appreciated but is not required.
00020 // 
00021 // 2. Altered source versions must be plainly marked as such, and must
00022 // not be misrepresented as being the original software.
00023 // 
00024 // 3. This notice may not be removed or altered from any source
00025 // distribution.
00027 #ifndef SHFRACTION_HPP
00028 #define SHFRACTION_HPP
00029 
00030 #include <limits>
00031 #include "ShUtility.hpp"
00032 
00033 namespace SH {
00034 
00038 template<typename T> struct ShFractionLongType { typedef T type; };
00039 
00040 // @todo range - making certain assumptions about sizes of type 
00041 // Make sure these are valid under all circumstances
00042 // May want to use int instead of short in some cases if hardware needs
00043 // to convert shorts.
00044 template<> struct ShFractionLongType<int> { typedef long long type; };
00045 template<> struct ShFractionLongType<short> { typedef int type; };
00046 template<> struct ShFractionLongType<char> { typedef short type; };
00047 template<> struct ShFractionLongType<unsigned int> { typedef unsigned long long type; };
00048 template<> struct ShFractionLongType<unsigned short> { typedef unsigned int type; };
00049 template<> struct ShFractionLongType<unsigned char> { typedef unsigned short type; };
00050 
00051 template<typename T> struct ShFractionSignedLongType { typedef T type; };
00052 
00053 // @todo range - making certain assumptions about sizes of type; 
00054 // Make sure these are valid under all circumstances
00055 template<> struct ShFractionSignedLongType<int> { typedef long long type; };
00056 template<> struct ShFractionSignedLongType<short> { typedef int type; };
00057 template<> struct ShFractionSignedLongType<char> { typedef short type; };
00058 template<> struct ShFractionSignedLongType<unsigned int> { typedef long long type; };
00059 template<> struct ShFractionSignedLongType<unsigned short> { typedef int type; };
00060 template<> struct ShFractionSignedLongType<unsigned char> { typedef short type; };
00061 
00062 /*** Sh class for fraction types represented in integers.  
00063  *
00064  * Inspired by NuFixed.hh and NuFixed.cc from the MetaMedia project.
00065  *
00066  * @param T integer type to use
00067  */
00068 
00085 template<typename T /* @todo clamp , bool Clamp=true */>
00086 struct ShFraction {
00087 
00088   // Type to use for operations that require temps with twice the bits
00089   // (and when clamping is on)
00090   typedef typename ShFractionLongType<T>::type LongType; 
00091   typedef typename ShFractionSignedLongType<T>::type SignedLongType; 
00092 
00093   // The usual type used in computation
00094   // @todo clamp typedef typename SelectType<Clamp, LongType, T>::type CompType; 
00095   typedef LongType CompType; 
00096 
00097   // Some information about the type and constant values representable by
00098   // this fraction type
00099   static const bool is_signed = std::numeric_limits<T>::is_signed;
00100   static const int BITS = sizeof(T) * 8; 
00101   static const T ONE; 
00102   static const T MAX; 
00103   static const T MIN;  
00104 
00105   T m_val;
00106 
00108   ShFraction();
00109 
00111   ShFraction(double value);
00112 
00114   static ShFraction make_fraction(CompType value);
00115 
00117   static ShFraction make_fraction_signed(SignedLongType value);
00118 
00119   template<typename T2>
00120   ShFraction(const ShFraction<T2> &other);
00121 
00123   operator double() const;
00124   T& val();
00125   T val() const;
00126 
00127 
00129   ShFraction& operator=(double value);
00130   ShFraction& operator=(const ShFraction& other);
00131   ShFraction& operator+=(double value);
00132   ShFraction& operator+=(const ShFraction& other);
00133   ShFraction& operator-=(double value);
00134   ShFraction& operator-=(const ShFraction& other);
00135   ShFraction& operator*=(double value);
00136   ShFraction& operator*=(const ShFraction& other);
00137   ShFraction& operator/=(double value);
00138   ShFraction& operator/=(const ShFraction& other);
00139 
00142   ShFraction& operator%=(double value);
00143   ShFraction& operator%=(const ShFraction& other);
00144   // @}
00145 
00149   ShFraction operator-() const;
00150 
00152   template<typename TT>
00153   friend std::ostream& operator<<(std::ostream& out, const ShFraction<TT> &value);
00154 
00155 
00157   template<typename TT>
00158   friend std::istream& operator>>(std::istream& out, ShFraction<TT> &value);
00159 
00160   private:
00161     // convert value to double
00162     double get_double() const;
00163 
00164     // convert double to value (may clamp)
00165     static T clamp_val(double value);
00166 
00167     // convert temporary computation type to value (may clamp) 
00168     // this works just fine for the non-clamp case as well as well
00169     static T clamp_val(CompType temp);
00170 
00171     // convert temporary computation type to value (may clamp) 
00172     static T clamp_val_signed(SignedLongType temp);
00173 };
00174 
00175 template<typename T/* @todo clamp , bool Clamp */>
00176 const T ShFraction<T>::ONE = std::numeric_limits<T>::max(); 
00177 
00178 template<typename T/* @todo clamp , bool Clamp */>
00179 const T ShFraction<T>::MAX = ShFraction<T>::ONE; 
00180 
00181 template<typename T/* @todo clamp , bool Clamp */>
00182 const T ShFraction<T>::MIN = is_signed ? -ShFraction<T>::ONE : 0;  
00183 
00185 template<typename T/* @todo clamp , bool Clamp */>
00186 ShFraction<T> operator+(const ShFraction<T> &a, const ShFraction<T> &b);
00187 
00188 template<typename T/* @todo clamp , bool Clamp */>
00189 ShFraction<T> operator-(const ShFraction<T> &a, const ShFraction<T> &b);
00190 
00191 template<typename T/* @todo clamp , bool Clamp */>
00192 ShFraction<T> operator*(const ShFraction<T> &a, const ShFraction<T> &b);
00193 
00194 template<typename T/* @todo clamp , bool Clamp */>
00195 ShFraction<T> operator/(const ShFraction<T> &a, const ShFraction<T> &b);
00196 
00197 template<typename T/* @todo clamp , bool Clamp */>
00198 ShFraction<T> operator%(const ShFraction<T> &a, const ShFraction<T> &b);
00199 
00200 template<typename T/* @todo clamp , bool Clamp */>
00201 ShFraction<T> cbrt(const ShFraction<T> &a);
00202 
00203 template<typename T/* @todo clamp , bool Clamp */>
00204 ShFraction<T> exp(const ShFraction<T> &a);
00205 
00206 template<typename T/* @todo clamp , bool Clamp */>
00207 ShFraction<T> exp2(const ShFraction<T> &a);
00208 
00209 template<typename T/* @todo clamp , bool Clamp */>
00210 ShFraction<T> exp10(const ShFraction<T> &a);
00211 
00212 template<typename T/* @todo clamp , bool Clamp */>
00213 ShFraction<T> log(const ShFraction<T> &a);
00214 
00215 template<typename T/* @todo clamp , bool Clamp */>
00216 ShFraction<T> log2(const ShFraction<T> &a);
00217 
00218 template<typename T/* @todo clamp , bool Clamp */>
00219 ShFraction<T> log10(const ShFraction<T> &a);
00220 
00221 template<typename T/* @todo clamp , bool Clamp */>
00222 ShFraction<T> frac(const ShFraction<T> &a);
00223 
00224 template<typename T/* @todo clamp , bool Clamp */>
00225 ShFraction<T> fmod(const ShFraction<T> &a, const ShFraction<T> &b);
00226 
00227 template<typename T/* @todo clamp , bool Clamp */>
00228 ShFraction<T> pow(const ShFraction<T> &a, const ShFraction<T> &b);
00229 
00230 template<typename T/* @todo clamp , bool Clamp */>
00231 ShFraction<T> rcp(const ShFraction<T> &a);
00232 
00233 template<typename T/* @todo clamp , bool Clamp */>
00234 ShFraction<T> rsq(const ShFraction<T> &a);
00235 
00236 template<typename T/* @todo clamp , bool Clamp */>
00237 ShFraction<T> sgn(const ShFraction<T> &a);
00238 
00239 template<typename T/* @todo clamp , bool Clamp */>
00240 ShFraction<T> sqrt(const ShFraction<T> &a);
00241 
00243 template<typename T/* @todo clamp , bool Clamp */>
00244 ShFraction<T> acos(const ShFraction<T> &a);
00245 
00246 template<typename T/* @todo clamp , bool Clamp */>
00247 ShFraction<T> asin(const ShFraction<T> &a);
00248 
00249 template<typename T/* @todo clamp , bool Clamp */>
00250 ShFraction<T> atan(const ShFraction<T> &a);
00251 
00252 template<typename T/* @todo clamp , bool Clamp */>
00253 ShFraction<T> atan2(const ShFraction<T> &a, const ShFraction<T> &b);
00254 
00255 template<typename T/* @todo clamp , bool Clamp */>
00256 ShFraction<T> cos(const ShFraction<T> &a); 
00257 template<typename T/* @todo clamp , bool Clamp */>
00258 ShFraction<T> sin(const ShFraction<T> &a);
00259 
00260 template<typename T/* @todo clamp , bool Clamp */>
00261 ShFraction<T> tan(const ShFraction<T> &a);
00262 
00264 template<typename T/* @todo clamp , bool Clamp */>
00265 bool operator<(const ShFraction<T> &a, const ShFraction<T> &b);
00266 
00267 template<typename T/* @todo clamp , bool Clamp */>
00268 bool operator<=(const ShFraction<T> &a, const ShFraction<T> &b);
00269 
00270 template<typename T/* @todo clamp , bool Clamp */>
00271 bool operator>(const ShFraction<T> &a, const ShFraction<T> &b);
00272 
00273 template<typename T/* @todo clamp , bool Clamp */>
00274 bool operator>=(const ShFraction<T> &a, const ShFraction<T> &b);
00275 
00276 template<typename T/* @todo clamp , bool Clamp */>
00277 bool  operator==(const ShFraction<T> &a, const ShFraction<T> &b);
00278 
00279 template<typename T/* @todo clamp , bool Clamp */>
00280 bool operator!=(const ShFraction<T> &a, const ShFraction<T> &b);
00281 
00283 template<typename T/* @todo clamp , bool Clamp */>
00284 ShFraction<T> min(const ShFraction<T> &a, const ShFraction<T> &b);
00285 
00286 template<typename T/* @todo clamp , bool Clamp */>
00287 ShFraction<T> max(const ShFraction<T> &a, const ShFraction<T> &b);
00288 
00289 template<typename T/* @todo clamp , bool Clamp */>
00290 ShFraction<T> floor(const ShFraction<T> &a);
00291 
00292 template<typename T/* @todo clamp , bool Clamp */>
00293 ShFraction<T> ceil(const ShFraction<T> &a);
00294 
00295 template<typename T/* @todo clamp , bool Clamp */>
00296 ShFraction<T> rnd(const ShFraction<T> &a);
00297 
00298 template<typename T/* @todo clamp , bool Clamp */>
00299 ShFraction<T> abs(const ShFraction<T> &a);
00300 
00302 template<typename T/* @todo clamp , bool Clamp */>
00303 ShFraction<T> cond(const ShFraction<T> &a, const ShFraction<T> &b, const ShFraction<T> &c);
00304 
00305 template<typename T/* @todo clamp , bool Clamp */>
00306 ShFraction<T> lerp(const ShFraction<T> &a, const ShFraction<T> &b, const ShFraction<T> &c);
00307 
00308 typedef ShFraction<int> ShFracInt;
00309 typedef ShFraction<short> ShFracShort;
00310 typedef ShFraction<char> ShFracByte;
00311 
00312 typedef ShFraction<unsigned int> ShFracUInt;
00313 typedef ShFraction<unsigned short> ShFracUShort;
00314 typedef ShFraction<unsigned char> ShFracUByte;
00315 
00316 }
00317 
00318 
00319 #include "ShFractionImpl.hpp"
00320   
00321 #endif

Generated on Mon Jan 24 18:36:31 2005 for Sh by  doxygen 1.4.1