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 #include <cmath>
00028 #include "ShMath.hpp"
00029 #include <numeric>
00030 #include "ShOperation.hpp"
00031 #include "ShEval.hpp"
00032
00033 namespace SH {
00034
00035
00036
00037
00038
00039
00040
00041 #define SHCCTO_UNARY_OP_SPEC(T, op, opsrc)\
00042 void ShConcreteCTypeOp<op, T>::doop(DataPtr dest, DataCPtr a, DataCPtr b, DataCPtr c) \
00043 {\
00044 int ao = a->size() > 1;\
00045 \
00046 Variant::iterator D = dest->begin();\
00047 Variant::const_iterator A = a->begin();\
00048 for(; D != dest->end(); A += ao, ++D) {\
00049 (*D) = opsrc;\
00050 }\
00051 }\
00052
00053 #define SHCCTO_BINARY_OP_SPEC(T, op, opsrc)\
00054 void ShConcreteCTypeOp<op, T>::doop(DataPtr dest, DataCPtr a, DataCPtr b, DataCPtr c) \
00055 {\
00056 int ao = a->size() > 1;\
00057 int bo = b->size() > 1;\
00058 \
00059 Variant::iterator D = dest->begin();\
00060 Variant::const_iterator A, B;\
00061 A = a->begin();\
00062 B = b->begin();\
00063 for(; D != dest->end(); A += ao, B += bo, ++D) {\
00064 (*D) = opsrc;\
00065 }\
00066 }
00067
00068 #define SHCCTO_TERNARY_OP_SPEC(T, op, opsrc)\
00069 void ShConcreteCTypeOp<op, T>::doop(DataPtr dest, DataCPtr a, DataCPtr b, DataCPtr c) \
00070 {\
00071 int ao = a->size() > 1;\
00072 int bo = b->size() > 1;\
00073 int co = c->size() > 1;\
00074 \
00075 Variant::iterator D = dest->begin();\
00076 Variant::const_iterator A, B, C;\
00077 A = a->begin();\
00078 B = b->begin();\
00079 C = c->begin();\
00080 for(; D != dest->end(); A += ao, B += bo, C += co, ++D) {\
00081 (*D) = opsrc;\
00082 }\
00083 }
00084
00085
00086 SHCCTO_UNARY_OP_SPEC(double, SH_OP_ABS, std::fabs(*A));
00087 SHCCTO_UNARY_OP_SPEC(float, SH_OP_ABS, fabsf(*A));
00088 SHCCTO_UNARY_OP_SPEC(double, SH_OP_ACOS, std::acos(*A));
00089 SHCCTO_UNARY_OP_SPEC(float, SH_OP_ACOS, acosf(*A));
00090 SHCCTO_UNARY_OP_SPEC(double, SH_OP_ASIN, std::asin(*A));
00091 SHCCTO_UNARY_OP_SPEC(float, SH_OP_ASIN, asinf(*A));
00092 SHCCTO_UNARY_OP_SPEC(double, SH_OP_ATAN, std::atan(*A));
00093 SHCCTO_UNARY_OP_SPEC(float, SH_OP_ATAN, atanf(*A));
00094 SHCCTO_UNARY_OP_SPEC(double, SH_OP_CBRT, std::pow(*A, 1.0 / 3.0));
00095 SHCCTO_UNARY_OP_SPEC(float, SH_OP_CBRT, powf(*A, 1.0f / 3.0f));
00096 SHCCTO_UNARY_OP_SPEC(double, SH_OP_CEIL, std::ceil(*A));
00097 SHCCTO_UNARY_OP_SPEC(float, SH_OP_CEIL, ceilf(*A));
00098 SHCCTO_UNARY_OP_SPEC(double, SH_OP_COS, std::cos(*A));
00099 SHCCTO_UNARY_OP_SPEC(float, SH_OP_COS, cosf(*A));
00100 SHCCTO_UNARY_OP_SPEC(double, SH_OP_EXP, std::exp(*A));
00101 SHCCTO_UNARY_OP_SPEC(float, SH_OP_EXP, expf(*A));
00102 SHCCTO_UNARY_OP_SPEC(double, SH_OP_EXP2, std::pow(2.0, *A));
00103 SHCCTO_UNARY_OP_SPEC(float, SH_OP_EXP2, powf(2.0f, *A));
00104 SHCCTO_UNARY_OP_SPEC(double, SH_OP_EXP10, std::pow(10.0, *A));
00105 SHCCTO_UNARY_OP_SPEC(float, SH_OP_EXP10, powf(10.0f, *A));
00106 SHCCTO_UNARY_OP_SPEC(double, SH_OP_FLR, std::floor(*A));
00107 SHCCTO_UNARY_OP_SPEC(float, SH_OP_FLR, floorf(*A));
00108 SHCCTO_UNARY_OP_SPEC(double, SH_OP_FRAC, (*A) - std::floor(*A));
00109 SHCCTO_UNARY_OP_SPEC(float, SH_OP_FRAC, (*A) - floorf(*A));
00110 SHCCTO_UNARY_OP_SPEC(double, SH_OP_LOG, std::log(*A));
00111 SHCCTO_UNARY_OP_SPEC(float, SH_OP_LOG, logf(*A));
00112 SHCCTO_UNARY_OP_SPEC(double, SH_OP_LOG2, std::log(*A) / std::log(2.0));
00113 SHCCTO_UNARY_OP_SPEC(float, SH_OP_LOG2, log2f(*A) / log2f(2.0f));
00114 SHCCTO_UNARY_OP_SPEC(double, SH_OP_LOG10, std::log(*A) / std::log(10.0));
00115 SHCCTO_UNARY_OP_SPEC(float, SH_OP_LOG10, logf(*A) / logf(10.0f));
00116
00117
00118 void ShConcreteCTypeOp<SH_OP_NORM, double>::doop(DataPtr dest, DataCPtr a, DataCPtr b, DataCPtr c)
00119 {
00120
00121 double m = std::sqrt(std::inner_product(a->begin(), a->end(), a->begin(), 0.0));
00122
00123 Variant::const_iterator A = a->begin();
00124 Variant::iterator D = dest->begin();
00125 for(; A != a->end(); ++D, ++A) (*D) = (*A) / m;
00126 }
00127
00128
00129 void ShConcreteCTypeOp<SH_OP_NORM, float>::doop(DataPtr dest, DataCPtr a, DataCPtr b, DataCPtr c)
00130 {
00131
00132 float m = std::sqrt(std::inner_product(a->begin(), a->end(), a->begin(), 0.0f));
00133
00134 Variant::const_iterator A = a->begin();
00135 Variant::iterator D = dest->begin();
00136 for(; A != a->end(); ++D, ++A) (*D) = (*A) / m;
00137 }
00138
00139 SHCCTO_UNARY_OP_SPEC(double, SH_OP_RCP, 1.0 / (*A));
00140 SHCCTO_UNARY_OP_SPEC(float, SH_OP_RCP, 1.0f / (*A));
00141 SHCCTO_UNARY_OP_SPEC(double, SH_OP_RND, std::floor(*A + 0.5));
00142 SHCCTO_UNARY_OP_SPEC(float, SH_OP_RND, floorf(*A + 0.5f));
00143 SHCCTO_UNARY_OP_SPEC(double, SH_OP_RSQ, 1.0 / std::sqrt(*A));
00144 SHCCTO_UNARY_OP_SPEC(float, SH_OP_RSQ, 1.0f / sqrtf(*A));
00145 SHCCTO_UNARY_OP_SPEC(double, SH_OP_SGN, ((*A) < 0 ? -1 : (*A) > 0 ? 1 : 0));
00146 SHCCTO_UNARY_OP_SPEC(float , SH_OP_SGN, ((*A) < 0 ? -1 : (*A) > 0 ? 1 : 0));
00147 SHCCTO_UNARY_OP_SPEC(double, SH_OP_SIN, std::sin(*A));
00148 SHCCTO_UNARY_OP_SPEC(float, SH_OP_SIN, sinf(*A));
00149 SHCCTO_UNARY_OP_SPEC(double, SH_OP_SQRT, std::sqrt(*A));
00150 SHCCTO_UNARY_OP_SPEC(float, SH_OP_SQRT, sqrtf(*A));
00151 SHCCTO_UNARY_OP_SPEC(double, SH_OP_TAN, std::tan(*A));
00152 SHCCTO_UNARY_OP_SPEC(float, SH_OP_TAN, tanf(*A));
00153
00154
00155 SHCCTO_BINARY_OP_SPEC(double, SH_OP_ATAN2, std::atan2((*A), (*B)));
00156 SHCCTO_BINARY_OP_SPEC(float, SH_OP_ATAN2, atan2f((*A), (*B)));
00157
00158
00159
00160 SHCCTO_BINARY_OP_SPEC(double, SH_OP_MOD, std::fmod((*A), (*B)));
00161 SHCCTO_BINARY_OP_SPEC(float, SH_OP_MOD, fmodf((*A), (*B)));
00162 SHCCTO_BINARY_OP_SPEC(double, SH_OP_POW, std::pow((*A), (*B)));
00163 SHCCTO_BINARY_OP_SPEC(float, SH_OP_POW, powf((*A), (*B)));
00164
00165 }