Let the compiler choose what precision we want to do the vector-scalar multiplication/division in.

This commit is contained in:
Pavel Krajcevski 2014-02-19 19:36:28 -05:00
parent 366a7cdfe4
commit 2d7ee21fb7
2 changed files with 16 additions and 6 deletions

View file

@ -206,7 +206,7 @@ namespace FasTC {
static inline VectorType ScalarMultiply(const VectorType &v, const ScalarType &s) { static inline VectorType ScalarMultiply(const VectorType &v, const ScalarType &s) {
VectorType a(v); VectorType a(v);
for(int i = 0; i < VectorType::Size; i++) for(int i = 0; i < VectorType::Size; i++)
a(i) *= static_cast<typename VectorType::ScalarType>(s); a(i) = static_cast<typename VectorType::ScalarType>(a(i) * s);
return a; return a;
} }
@ -223,7 +223,7 @@ namespace FasTC {
static inline VectorType ScalarDivide(const VectorType &v, const ScalarType &s) { static inline VectorType ScalarDivide(const VectorType &v, const ScalarType &s) {
VectorType a(v); VectorType a(v);
for(int i = 0; i < VectorType::Size; i++) for(int i = 0; i < VectorType::Size; i++)
a(i) /= static_cast<typename VectorType::ScalarType>(s); a(i) = static_cast<typename VectorType::ScalarType>(a(i) / s);
return a; return a;
} }

View file

@ -190,16 +190,26 @@ TEST(VectorBase, Scaling) {
v2fd = v2f / 3; v2fd = v2f / 3;
EXPECT_NEAR(v2fd[0], 1.0f / 3.0f, kEpsilon); EXPECT_NEAR(v2fd[0], 1.0f / 3.0f, kEpsilon);
EXPECT_NEAR(v2fd[1], 1.0f, kEpsilon); EXPECT_NEAR(v2fd[1], 1.0f, kEpsilon);
unsigned uv[2] = {1, 3};
FasTC::VectorBase<unsigned, 2> v2u (uv);
FasTC::VectorBase<unsigned, 2> v2ud = v2u * 0.5;
EXPECT_EQ(v2ud[0], 0);
EXPECT_EQ(v2ud[1], 1);
v2ud = v2u / 0.5f;
EXPECT_EQ(v2ud[0], 2);
EXPECT_EQ(v2ud[1], 6);
} }
TEST(VectorBase, Addition) { TEST(VectorBase, Addition) {
float fv[2] = {1.1f, 3.2f}; float fv[2] = {1.1f, 3.2f};
FasTC::VectorBase<float, 2> v2f (fv); FasTC::VectorBase<float, 2> v2f (fv);
unsigned uv[2] = {5, 2}; int uv[2] = {5, 2};
FasTC::VectorBase<unsigned, 2> v2u (uv); FasTC::VectorBase<int, 2> v2u (uv);
FasTC::VectorBase<unsigned, 2> au = v2u + v2f; FasTC::VectorBase<int, 2> au = v2u + v2f;
EXPECT_EQ(au[0], 6); EXPECT_EQ(au[0], 6);
EXPECT_EQ(au[1], 5); EXPECT_EQ(au[1], 5);
@ -209,7 +219,7 @@ TEST(VectorBase, Addition) {
au = v2u - v2f; au = v2u - v2f;
EXPECT_EQ(au[0], 3); EXPECT_EQ(au[0], 3);
EXPECT_EQ(au[1], 0); EXPECT_EQ(au[1], -1);
af = v2f - v2u; af = v2f - v2u;
EXPECT_NEAR(af[0], -3.9f, kEpsilon); EXPECT_NEAR(af[0], -3.9f, kEpsilon);