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

ShLibClampImpl.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 SHLIBCLAMPIMPL_HPP
00028 #define SHLIBCLAMPIMPL_HPP
00029 
00030 #include "ShLibClamp.hpp"
00031 #include "ShInstructions.hpp"
00032 #include "ShAttrib.hpp"
00033 #include "ShLibMiscImpl.hpp"
00034 
00035 namespace SH {
00036 
00037 template<int N, typename T>
00038 inline
00039 ShGeneric<N, T> abs(const ShGeneric<N, T>& var)
00040 {
00041   ShAttrib<N, SH_TEMP, T> t;
00042   shABS(t, var);
00043   return t;
00044 }
00045 
00046 template<int N, typename T>
00047 inline
00048 ShGeneric<N, T> ceil(const ShGeneric<N, T>& var)
00049 {
00050   ShAttrib<N, SH_TEMP, T> t;
00051   shCEIL(t, var);
00052   return t;
00053 }
00054 
00055 template<int N, typename T>
00056 inline
00057 ShGeneric<N, T> floor(const ShGeneric<N, T>& var)
00058 {
00059   ShAttrib<N, SH_TEMP, T> t;
00060   shFLR(t, var);
00061   return t;
00062 }
00063 
00064 template<int N, typename T>
00065 inline
00066 ShGeneric<N, T> round(const ShGeneric<N, T>& var)
00067 {
00068   ShAttrib<N, SH_TEMP, T> t;
00069   shRND(t, var);
00070   return t;
00071 }
00072 
00073 template<int N, typename T1, typename T2>
00074 inline
00075 ShGeneric<N, CT1T2> mod(const ShGeneric<N, T1>& left, const ShGeneric<N, T2>& right)
00076 {
00077   ShAttrib<N, SH_TEMP, CT1T2> t;
00078   shMOD(t, left, right);
00079   return t;
00080 }
00081 template<int N, typename T1, typename T2>
00082 inline
00083 ShGeneric<N, CT1T2> mod(const ShGeneric<N, T1>& left, const ShGeneric<1, T2>& right)
00084 {
00085   ShAttrib<N, SH_TEMP, CT1T2> t;
00086   shMOD(t, left, right);
00087   return t;
00088 }
00089 template<typename T1, typename T2>
00090 inline
00091 ShGeneric<1, CT1T2> mod(const ShGeneric<1, T1>& left, const ShGeneric<1, T2>& right)
00092 {
00093   ShAttrib<1, SH_TEMP, CT1T2> t;
00094   shMOD(t, left, right);
00095   return t;
00096 }
00097 
00098 template<int N, typename T1, typename T2>
00099 inline
00100 ShGeneric<N, CT1T2> operator%(const ShGeneric<N, T1>& left, const ShGeneric<N, T2>& right)
00101 {
00102   return mod(left, right);
00103 }
00104 template<int N, typename T1, typename T2>
00105 inline
00106 ShGeneric<N, CT1T2> operator%(const ShGeneric<N, T1>& left, const ShGeneric<1, T2>& right)
00107 {
00108   return mod(left, right);
00109 }
00110 template<typename T1, typename T2>
00111 inline
00112 ShGeneric<1, CT1T2> operator%(const ShGeneric<1, T1>& left, const ShGeneric<1, T2>& right)
00113 {
00114   return mod(left, right);
00115 }
00116 SH_SHLIB_CONST_SCALAR_OP(mod);
00117 SH_SHLIB_CONST_N_OP_LEFT(mod);
00118 SH_SHLIB_CONST_SCALAR_OP(operator%);
00119 SH_SHLIB_CONST_N_OP_LEFT(operator%);
00120 
00121 template<int N, typename T>
00122 inline
00123 ShGeneric<N, T> frac(const ShGeneric<N, T>& var)
00124 {
00125   ShAttrib<N, SH_TEMP, T> t;
00126   shFRAC(t, var);
00127   return t;
00128 }
00129 
00130 template<int N, typename T>
00131 inline
00132 ShGeneric<N, T> pos(const ShGeneric<N, T>& var)
00133 {
00134   return max(var, fillcast<N>(0.0f));
00135 }
00136 
00137 template<int N, typename T1, typename T2>
00138 inline
00139 ShGeneric<N,  CT1T2> max(const ShGeneric<N, T1>& left, const ShGeneric<N, T2>& right)
00140 {
00141   ShAttrib<N, SH_TEMP, CT1T2> t;
00142   shMAX(t, left, right);
00143   return t;
00144 }
00145 SH_SHLIB_CONST_SCALAR_OP(max);
00146 
00147 template<int N, typename T1, typename T2>
00148 inline
00149 ShGeneric<N,  CT1T2> min(const ShGeneric<N, T1>& left, const ShGeneric<N, T2>& right)
00150 {
00151   ShAttrib<N, SH_TEMP, CT1T2> t;
00152   shMIN(t, left, right);
00153   return t;
00154 }
00155 SH_SHLIB_CONST_SCALAR_OP(min);
00156 
00157 template<int N, typename T>
00158 ShGeneric<1, T> max(const ShGeneric<N, T>& a)
00159 {
00160   int lhswz[N/2 + N%2];
00161   for (int i = 0; i < N/2 + N%2; i++) {
00162     lhswz[i] = i;
00163   }
00164   int rhswz[N/2];
00165   for (int i = 0; i < N/2; i++) {
00166     rhswz[i] = i + N/2 + N%2;
00167   }
00168 
00169   return max(max(a.template swiz<N/2 + N%2>(lhswz)), max(a.template swiz<N/2>(rhswz)));
00170 }
00171 
00172 template<typename T>
00173 ShGeneric<1, T> max(const ShGeneric<1, T>& a)
00174 {
00175   return a;
00176 }
00177 
00178 template<int N, typename T>
00179 ShGeneric<1, T> min(const ShGeneric<N, T>& a)
00180 {
00181   int lhswz[N/2 + N%2];
00182   for (int i = 0; i < N/2 + N%2; i++) {
00183     lhswz[i] = i;
00184   }
00185   int rhswz[N/2];
00186   for (int i = 0; i < N/2; i++) {
00187     rhswz[i] = i + N/2 + N%2;
00188   }
00189 
00190   return min(min(a.template swiz<N/2 + N%2>(lhswz)), min(a.template swiz<N/2>(rhswz)));
00191 }
00192 
00193 template<typename T>
00194 ShGeneric<1, T> min(const ShGeneric<1, T>& a)
00195 {
00196   return a;
00197 }
00198 
00199 template<int N, typename T1, typename T2, typename T3>
00200 inline
00201 ShGeneric<N, CT1T2T3> clamp(const ShGeneric<N, T1>& a,
00202                       const ShGeneric<N, T2>& b, const ShGeneric<N, T3>& c)
00203 {
00204   return min(max(a, b), c);
00205 }
00206 template<int N, typename T1, typename T2, typename T3>
00207 inline
00208 ShGeneric<N, CT1T2T3> clamp(const ShGeneric<N, T1>& a,
00209                       const ShGeneric<1, T2>& b, const ShGeneric<1, T3>& c)
00210 {
00211   return min(max(a, fillcast<N>(b)), fillcast<N>(c));
00212 }
00213 
00214 template<typename T1, typename T2, typename T3>
00215 inline
00216 ShGeneric<1, CT1T2T3> clamp(const ShGeneric<1, T1>& a,
00217                       const ShGeneric<1, T2>& b, const ShGeneric<1, T3>& c)
00218 {
00219   return min(max(a, b), c);
00220 }
00221 SH_SHLIB_CONST_TRINARY_OP_011(clamp);
00222 
00223 template<int N, typename T>
00224 inline
00225 ShGeneric<N, T> sat(const ShGeneric<N, T>& var)
00226 {
00227   return min(var, fillcast<N>(ShConstAttrib1f(1.0)));
00228 }
00229 
00230 template<int N, typename T>
00231 inline
00232 ShGeneric<N, T> sign(const ShGeneric<N, T>& var)
00233 {
00234   ShAttrib<N, SH_TEMP, T> t;
00235   shSGN(t, var);
00236   return t;
00237 }
00238 
00239 }
00240 
00241 #endif

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