00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00027 #ifndef SHLIBMISCIMPL_HPP
00028 #define SHLIBMISCIMPL_HPP
00029
00030 #include "ShLibMisc.hpp"
00031 #include "ShInstructions.hpp"
00032 #include "ShProgram.hpp"
00033
00034 namespace SH {
00035
00036 template<int M, int N, typename T>
00037 inline
00038 ShGeneric<M, T> cast(const ShGeneric<N, T>& a)
00039 {
00040 int copySize = std::min(M, N);
00041 ShAttrib<M, SH_TEMP, T> result;
00042
00043 int* indices = new int[copySize];
00044 for(int i = 0; i < copySize; ++i) indices[i] = i;
00045 if(M < N) {
00046 result = a.template swiz<M>(indices);
00047 } else if( M > N ) {
00048 result.template swiz<N>(indices) = a;
00049 } else {
00050 shASN(result, a);
00051 }
00052 delete [] indices;
00053 return result;
00054 }
00055
00056 template<int M>
00057 inline
00058 ShGeneric<M, double> cast(double a)
00059 {
00060 return cast<M>(ShAttrib<1, SH_CONST, double>(a));
00061 }
00062
00063 template<int M, int N, typename T>
00064 inline
00065 ShGeneric<M, T> fillcast(const ShGeneric<N, T>& a)
00066 {
00067 if( M <= N ) return cast<M>(a);
00068 int indices[M];
00069 for(int i = 0; i < M; ++i) indices[i] = i >= N ? N - 1 : i;
00070 return a.template swiz<M>(indices);
00071 }
00072
00073 template<int M>
00074 inline
00075 ShGeneric<M, double> fillcast(double a)
00076 {
00077 return fillcast<M>(ShAttrib<1, SH_CONST, double>(a));
00078 }
00079
00080 template<int M, int N, typename T1, typename T2>
00081 inline
00082 ShGeneric<M+N, CT1T2> join(const ShGeneric<M, T1>& a, const ShGeneric<N, T2>& b)
00083 {
00084 int indices[M+N];
00085 for(int i = 0; i < M+N; ++i) indices[i] = i;
00086 ShAttrib<M+N, SH_TEMP, CT1T2> result;
00087 result.template swiz<M>(indices) = a;
00088 result.template swiz<N>(indices + M) = b;
00089 return result;
00090 }
00091
00092 template<int N, typename T>
00093 inline
00094 void discard(const ShGeneric<N, T>& c)
00095 {
00096 shKIL(c);
00097 }
00098
00099 template<int N, typename T>
00100 inline
00101 void kill(const ShGeneric<N, T>& c)
00102 {
00103 discard(c);
00104 }
00105
00106 template<typename T>
00107 ShProgram freeze(const ShProgram& p,
00108 const T& uniform)
00109 {
00110 return (p >> uniform) << (T::ConstType)(uniform);
00111 }
00112
00113 }
00114
00115 #endif