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

ShConcreteCTypeOp.cpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright 2003-2005 Serious Hack Inc.
00004 // 
00005 // This software is provided 'as-is', without any express or implied
00006 // warranty. In no event will the authors be held liable for any damages
00007 // arising from the use of this software.
00008 // 
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it
00011 // freely, subject to the following restrictions:
00012 // 
00013 // 1. The origin of this software must not be misrepresented; you must
00014 // not claim that you wrote the original software. If you use this
00015 // software in a product, an acknowledgment in the product documentation
00016 // would be appreciated but is not required.
00017 // 
00018 // 2. Altered source versions must be plainly marked as such, and must
00019 // not be misrepresented as being the original software.
00020 // 
00021 // 3. This notice may not be removed or altered from any source
00022 // distribution.
00024 #include <cmath>
00025 #include "ShMath.hpp"
00026 #include <numeric>
00027 #include "ShOperation.hpp"
00028 #include "ShEval.hpp"
00029 
00030 namespace SH {
00031 
00032   // TODO some of these specializations don't need to be in the cpp
00033   // since they work for most things...
00034 
00035 /**************************************************************
00036  *** Use Specialization to define particular ops 
00037  **************************************************************/
00038 #define SHCCTO_UNARY_OP_SPEC_TMPL(T, op, opsrc)\
00039 template<>\
00040 SHCCTO_UNARY_OP_SPEC(T, op, opsrc)
00041 
00042 #define SHCCTO_UNARY_OP_SPEC(T, op, opsrc)\
00043 void ShConcreteCTypeOp<op, T>::doop(DataPtr dest, DataCPtr a, DataCPtr b, DataCPtr c) \
00044 {\
00045   int ao = a->size() > 1;\
00046 \
00047   Variant::iterator D = dest->begin();\
00048   Variant::const_iterator A = a->begin();\
00049   for(; D != dest->end(); A += ao, ++D) {\
00050     (*D) = opsrc;\
00051   }\
00052 }\
00053 
00054 #define SHCCTO_BINARY_OP_SPEC(T, op, opsrc)\
00055 void ShConcreteCTypeOp<op, T>::doop(DataPtr dest, DataCPtr a, DataCPtr b, DataCPtr c) \
00056 {\
00057   int ao = a->size() > 1;\
00058   int bo = b->size() > 1;\
00059 \
00060   Variant::iterator D = dest->begin();\
00061   Variant::const_iterator A, B;\
00062   A = a->begin();\
00063   B = b->begin();\
00064   for(; D != dest->end(); A += ao, B += bo, ++D) {\
00065     (*D) = opsrc;\
00066   }\
00067 }
00068 
00069 #define SHCCTO_TERNARY_OP_SPEC(T, op, opsrc)\
00070 void ShConcreteCTypeOp<op, T>::doop(DataPtr dest, DataCPtr a, DataCPtr b, DataCPtr c) \
00071 {\
00072   int ao = a->size() > 1;\
00073   int bo = b->size() > 1;\
00074   int co = c->size() > 1;\
00075 \
00076   Variant::iterator D = dest->begin();\
00077   Variant::const_iterator A, B, C;\
00078   A = a->begin();\
00079   B = b->begin();\
00080   C = c->begin();\
00081   for(; D != dest->end(); A += ao, B += bo, C += co, ++D) {\
00082     (*D) = opsrc;\
00083   }\
00084 }   
00085 
00086 /* Specializations for unary ops */
00087 SHCCTO_UNARY_OP_SPEC_TMPL(double, SH_OP_ABS, std::fabs(*A));
00088 SHCCTO_UNARY_OP_SPEC_TMPL(float,  SH_OP_ABS, fabsf(*A));
00089 SHCCTO_UNARY_OP_SPEC(double, SH_OP_ACOS, std::acos(*A));
00090 SHCCTO_UNARY_OP_SPEC(float,  SH_OP_ACOS, acosf(*A));
00091 SHCCTO_UNARY_OP_SPEC(double, SH_OP_ASIN, std::asin(*A));
00092 SHCCTO_UNARY_OP_SPEC(float,  SH_OP_ASIN, asinf(*A));
00093 SHCCTO_UNARY_OP_SPEC(double, SH_OP_ATAN, std::atan(*A));
00094 SHCCTO_UNARY_OP_SPEC(float,  SH_OP_ATAN, atanf(*A));
00095 SHCCTO_UNARY_OP_SPEC(double, SH_OP_CBRT, std::pow(*A, 1.0 / 3.0));
00096 SHCCTO_UNARY_OP_SPEC(float,  SH_OP_CBRT, powf(*A, 1.0f / 3.0f));
00097 SHCCTO_UNARY_OP_SPEC(double, SH_OP_CEIL, std::ceil(*A));
00098 SHCCTO_UNARY_OP_SPEC(float,  SH_OP_CEIL, ceilf(*A));
00099 SHCCTO_UNARY_OP_SPEC(double, SH_OP_COS, std::cos(*A));
00100 SHCCTO_UNARY_OP_SPEC(float,  SH_OP_COS, cosf(*A));
00101 SHCCTO_UNARY_OP_SPEC(double, SH_OP_EXP, std::exp(*A));
00102 SHCCTO_UNARY_OP_SPEC(float,  SH_OP_EXP, expf(*A));
00103 SHCCTO_UNARY_OP_SPEC(double, SH_OP_EXP2, std::pow(2.0, *A));
00104 SHCCTO_UNARY_OP_SPEC(float,  SH_OP_EXP2, powf(2.0f, *A));
00105 SHCCTO_UNARY_OP_SPEC(double, SH_OP_EXP10, std::pow(10.0, *A));
00106 SHCCTO_UNARY_OP_SPEC(float,  SH_OP_EXP10, powf(10.0f, *A));
00107 SHCCTO_UNARY_OP_SPEC(double, SH_OP_FLR, std::floor(*A));
00108 SHCCTO_UNARY_OP_SPEC(float,  SH_OP_FLR, floorf(*A));
00109 SHCCTO_UNARY_OP_SPEC(double, SH_OP_FRAC, (*A) - std::floor(*A));
00110 SHCCTO_UNARY_OP_SPEC(float,  SH_OP_FRAC, (*A) - floorf(*A));
00111 SHCCTO_UNARY_OP_SPEC(double, SH_OP_LOG, std::log(*A)); 
00112 SHCCTO_UNARY_OP_SPEC(float,  SH_OP_LOG, logf(*A)); 
00113 SHCCTO_UNARY_OP_SPEC(double, SH_OP_LOG2, std::log(*A) / std::log(2.0)); 
00114 SHCCTO_UNARY_OP_SPEC(float,  SH_OP_LOG2, log2f(*A) / log2f(2.0f)); 
00115 SHCCTO_UNARY_OP_SPEC(double, SH_OP_LOG10, std::log(*A) / std::log(10.0)); 
00116 SHCCTO_UNARY_OP_SPEC(float,  SH_OP_LOG10, logf(*A) / logf(10.0f)); 
00117 
00118 
00119 void ShConcreteCTypeOp<SH_OP_NORM, double>::doop(DataPtr dest, DataCPtr a, DataCPtr b, DataCPtr c) 
00120 {
00121   // assume dest.size == a->size 
00122   double m = std::sqrt(std::inner_product(a->begin(), a->end(), a->begin(), 0.0));
00123 
00124   Variant::const_iterator A = a->begin();
00125   Variant::iterator D = dest->begin();
00126   for(; A != a->end(); ++D, ++A) (*D) = (*A) / m; 
00127 }
00128 
00129 
00130 void ShConcreteCTypeOp<SH_OP_NORM, float>::doop(DataPtr dest, DataCPtr a, DataCPtr b, DataCPtr c) 
00131 {
00132   // assume dest.size == a->size 
00133   float m = std::sqrt(std::inner_product(a->begin(), a->end(), a->begin(), 0.0f));
00134 
00135   Variant::const_iterator A = a->begin();
00136   Variant::iterator D = dest->begin();
00137   for(; A != a->end(); ++D, ++A) (*D) = (*A) / m; 
00138 }
00139 
00140 SHCCTO_UNARY_OP_SPEC_TMPL(double, SH_OP_RCP, 1.0 / (*A)); 
00141 SHCCTO_UNARY_OP_SPEC_TMPL(float,  SH_OP_RCP, 1.0f / (*A)); 
00142 SHCCTO_UNARY_OP_SPEC(double, SH_OP_RND, std::floor(*A + 0.5)); 
00143 SHCCTO_UNARY_OP_SPEC(float,  SH_OP_RND, floorf(*A + 0.5f)); 
00144 SHCCTO_UNARY_OP_SPEC(double, SH_OP_RSQ, 1.0 / std::sqrt(*A)); 
00145 SHCCTO_UNARY_OP_SPEC(float,  SH_OP_RSQ, 1.0f / sqrtf(*A)); 
00146 SHCCTO_UNARY_OP_SPEC_TMPL(double, SH_OP_SGN, ((*A) < 0 ? -1 : (*A) > 0 ? 1 : 0)); 
00147 SHCCTO_UNARY_OP_SPEC_TMPL(float , SH_OP_SGN, ((*A) < 0 ? -1 : (*A) > 0 ? 1 : 0)); 
00148 SHCCTO_UNARY_OP_SPEC(double, SH_OP_SIN, std::sin(*A)); 
00149 SHCCTO_UNARY_OP_SPEC(float,  SH_OP_SIN, sinf(*A)); 
00150 SHCCTO_UNARY_OP_SPEC(double, SH_OP_SQRT, std::sqrt(*A)); 
00151 SHCCTO_UNARY_OP_SPEC(float,  SH_OP_SQRT, sqrtf(*A)); 
00152 SHCCTO_UNARY_OP_SPEC(double, SH_OP_TAN, std::tan(*A)); 
00153 SHCCTO_UNARY_OP_SPEC(float,  SH_OP_TAN, tanf(*A)); 
00154 
00155 /* Specializations for binary ops */
00156 SHCCTO_BINARY_OP_SPEC(double, SH_OP_ATAN2, std::atan2((*A), (*B)));
00157 SHCCTO_BINARY_OP_SPEC(float,  SH_OP_ATAN2, atan2f((*A), (*B)));
00158 
00159 
00160 // Binary Ops
00161 SHCCTO_BINARY_OP_SPEC(double, SH_OP_MOD, ((*A) - (*B) * std::floor((*A) / (*B)))); 
00162 SHCCTO_BINARY_OP_SPEC(float,  SH_OP_MOD, ((*A) - (*B) * floorf((*A) / (*B)))); 
00163 SHCCTO_BINARY_OP_SPEC(double, SH_OP_POW, std::pow((*A), (*B))); 
00164 SHCCTO_BINARY_OP_SPEC(float,  SH_OP_POW, powf((*A), (*B))); 
00165 
00166 }

Generated on Thu Apr 21 17:32:46 2005 for Sh by  doxygen 1.4.2