From 04fce28ae3a42e20ce86386d5808880f7dd3b15f Mon Sep 17 00:00:00 2001 From: Kenneth Moreland Date: Thu, 30 Jul 2015 16:43:19 -0600 Subject: [PATCH] Deal with small integer promotions C and C++ has a funny feature where operations on small integers (char and short) actually promote the result to a 32 bit integer. Most often in our code the result is pushed back to the same type, and picky compilers can then give a warning about an implicit type conversion (that we inevitably don't care about). Here are a lot of changes to suppress the warnings. --- vtkm/Matrix.h | 6 +-- vtkm/Types.h | 20 ++++---- vtkm/cont/ArrayHandleCounting.h | 3 +- vtkm/cont/testing/TestingArrayHandles.h | 2 +- vtkm/cont/testing/TestingFancyArrayHandles.h | 4 +- .../UnitTestArrayHandleCompositeVector.cxx | 4 +- .../testing/UnitTestFetchArrayDirectIn.cxx | 2 +- vtkm/testing/Testing.h | 2 +- vtkm/testing/UnitTestMatrix.cxx | 3 +- vtkm/testing/UnitTestTypes.cxx | 48 ++++++++++++------- vtkm/testing/VecTraitsTests.h | 9 ++-- 11 files changed, 61 insertions(+), 42 deletions(-) diff --git a/vtkm/Matrix.h b/vtkm/Matrix.h index 1ab58abc0..1f924645f 100644 --- a/vtkm/Matrix.h +++ b/vtkm/Matrix.h @@ -161,13 +161,13 @@ vtkm::Matrix MatrixMultiply( { for (vtkm::IdComponent colIndex = 0; colIndex < NumCol; colIndex++) { - T sum = leftFactor(rowIndex, 0) * rightFactor(0, colIndex); + T sum = T(leftFactor(rowIndex, 0) * rightFactor(0, colIndex)); for (vtkm::IdComponent internalIndex = 1; internalIndex < NumInternal; internalIndex++) { - sum += leftFactor(rowIndex, internalIndex) - * rightFactor(internalIndex, colIndex); + sum = T(sum + (leftFactor(rowIndex, internalIndex) + * rightFactor(internalIndex, colIndex))); } result(rowIndex, colIndex) = sum; } diff --git a/vtkm/Types.h b/vtkm/Types.h index 501d61f15..88a2eb510 100644 --- a/vtkm/Types.h +++ b/vtkm/Types.h @@ -677,7 +677,7 @@ struct Add template VTKM_EXEC_CONT_EXPORT T operator()(const T &a, const T &b) const { - return a + b; + return T(a + b); } }; @@ -686,7 +686,7 @@ struct Subtract template VTKM_EXEC_CONT_EXPORT T operator()(const T &a, const T &b) const { - return a - b; + return T(a - b); } }; @@ -695,7 +695,7 @@ struct Multiply template VTKM_EXEC_CONT_EXPORT T operator()(const T &a, const T &b) const { - return a * b; + return T(a * b); } }; @@ -704,7 +704,7 @@ struct Divide template VTKM_EXEC_CONT_EXPORT T operator()(const T &a, const T &b) const { - return a / b; + return T(a / b); } }; @@ -713,7 +713,7 @@ struct Negate template VTKM_EXEC_CONT_EXPORT T operator()(const T &x) const { - return -x; + return T(-x); } }; @@ -1055,10 +1055,10 @@ template VTKM_EXEC_CONT_EXPORT T dot(const vtkm::Vec &a, const vtkm::Vec &b) { - T result = a[0]*b[0]; + T result = T(a[0]*b[0]); for (vtkm::IdComponent componentIndex = 1; componentIndex < Size; componentIndex++) { - result += a[componentIndex]*b[componentIndex]; + result = T(result + a[componentIndex]*b[componentIndex]); } return result; } @@ -1067,21 +1067,21 @@ template VTKM_EXEC_CONT_EXPORT T dot(const vtkm::Vec &a, const vtkm::Vec &b) { - return (a[0]*b[0]) + (a[1]*b[1]); + return T((a[0]*b[0]) + (a[1]*b[1])); } template VTKM_EXEC_CONT_EXPORT T dot(const vtkm::Vec &a, const vtkm::Vec &b) { - return (a[0]*b[0]) + (a[1]*b[1]) + (a[2]*b[2]); + return T((a[0]*b[0]) + (a[1]*b[1]) + (a[2]*b[2])); } template VTKM_EXEC_CONT_EXPORT T dot(const vtkm::Vec &a, const vtkm::Vec &b) { - return (a[0]*b[0]) + (a[1]*b[1]) + (a[2]*b[2]) + (a[3]*b[3]); + return T((a[0]*b[0]) + (a[1]*b[1]) + (a[2]*b[2]) + (a[3]*b[3])); } //Integer types of a width less than an integer get implicitly casted to diff --git a/vtkm/cont/ArrayHandleCounting.h b/vtkm/cont/ArrayHandleCounting.h index 18a673231..3e233d192 100644 --- a/vtkm/cont/ArrayHandleCounting.h +++ b/vtkm/cont/ArrayHandleCounting.h @@ -74,7 +74,8 @@ public: VTKM_EXEC_CONT_EXPORT ValueType Get(vtkm::Id index) const { - return StartingValue + ValueType(static_cast(index)); + return ValueType(StartingValue + + ValueType(static_cast(index))); } private: diff --git a/vtkm/cont/testing/TestingArrayHandles.h b/vtkm/cont/testing/TestingArrayHandles.h index 174f377a0..b1858e842 100644 --- a/vtkm/cont/testing/TestingArrayHandles.h +++ b/vtkm/cont/testing/TestingArrayHandles.h @@ -87,7 +87,7 @@ struct TestingArrayHandles VTKM_EXEC_EXPORT void operator()(vtkm::Id index) const { - this->Portal.Set(index, this->Portal.Get(index)+ T(1)); + this->Portal.Set(index, T(this->Portal.Get(index) + T(1))); } }; diff --git a/vtkm/cont/testing/TestingFancyArrayHandles.h b/vtkm/cont/testing/TestingFancyArrayHandles.h index 17dcea556..5f8c9e0da 100644 --- a/vtkm/cont/testing/TestingFancyArrayHandles.h +++ b/vtkm/cont/testing/TestingFancyArrayHandles.h @@ -262,7 +262,7 @@ private: "Counting Handle Failed"); VTKM_TEST_ASSERT(test_equal(result_v, control_value), "Counting Handle Control Failed"); - component_value = component_value + ComponentType(1); + component_value = ComponentType(component_value + ComponentType(1)); } } }; @@ -447,7 +447,7 @@ private: "Transform Counting Handle Failed"); VTKM_TEST_ASSERT(test_equal(result_v, control_value), "Transform Counting Handle Control Failed"); - component_value = component_value + ComponentType(1); + component_value = ComponentType(component_value + ComponentType(1)); } } }; diff --git a/vtkm/cont/testing/UnitTestArrayHandleCompositeVector.cxx b/vtkm/cont/testing/UnitTestArrayHandleCompositeVector.cxx index 03b8f06da..2407aa9f2 100644 --- a/vtkm/cont/testing/UnitTestArrayHandleCompositeVector.cxx +++ b/vtkm/cont/testing/UnitTestArrayHandleCompositeVector.cxx @@ -44,7 +44,9 @@ vtkm::FloatDefault TestValue3Ids(vtkm::Id index, vtkm::IdComponent inComponentIndex, int inArrayId) { - return index + vtkm::FloatDefault(0.1)*inComponentIndex + vtkm::FloatDefault(0.01)*inArrayId; + return (vtkm::FloatDefault(index) + + 0.1f*vtkm::FloatDefault(inComponentIndex) + + 0.01f*vtkm::FloatDefault(inArrayId)); } template diff --git a/vtkm/exec/arg/testing/UnitTestFetchArrayDirectIn.cxx b/vtkm/exec/arg/testing/UnitTestFetchArrayDirectIn.cxx index 31c19275b..daca6b499 100644 --- a/vtkm/exec/arg/testing/UnitTestFetchArrayDirectIn.cxx +++ b/vtkm/exec/arg/testing/UnitTestFetchArrayDirectIn.cxx @@ -68,7 +68,7 @@ struct FetchArrayDirectInTests VTKM_TEST_ASSERT(test_equal(value, TestValue(index, T())), "Got invalid value from Load."); - value = T(2)*value; + value = T(T(2)*value); // This should be a no-op, but we should be able to call it. fetch.Store(index, invocation, value); diff --git a/vtkm/testing/Testing.h b/vtkm/testing/Testing.h index 13f8f9ec9..d247b3f98 100644 --- a/vtkm/testing/Testing.h +++ b/vtkm/testing/Testing.h @@ -397,7 +397,7 @@ vtkm::Vec TestValue(vtkm::Id index, vtkm::Vec) { vtkm::Vec value; for (vtkm::IdComponent i = 0; i < N; i++) { - value[i] = TestValue(index, T()) + T(i + 1); + value[i] = T(TestValue(index, T()) + T(i + 1)); } return value; } diff --git a/vtkm/testing/UnitTestMatrix.cxx b/vtkm/testing/UnitTestMatrix.cxx index 6e3dfabd3..0aba75134 100644 --- a/vtkm/testing/UnitTestMatrix.cxx +++ b/vtkm/testing/UnitTestMatrix.cxx @@ -55,6 +55,7 @@ struct MatrixTest static const vtkm::IdComponent NUM_ROWS = NumRow; static const vtkm::IdComponent NUM_COLS = NumCol; typedef vtkm::Matrix MatrixType; + typedef typename MatrixType::ComponentType ComponentType; static void BasicCreation() { @@ -74,7 +75,7 @@ struct MatrixTest MatrixType value = TestValue(0, MatrixType()); FOR_ROW_COL(matrix) { - matrix[row][col] = value(row,col)*2; + matrix[row][col] = ComponentType(value(row,col)*2); } FOR_ROW_COL(matrix) { diff --git a/vtkm/testing/UnitTestTypes.cxx b/vtkm/testing/UnitTestTypes.cxx index 614f009e9..373019be8 100644 --- a/vtkm/testing/UnitTestTypes.cxx +++ b/vtkm/testing/UnitTestTypes.cxx @@ -130,16 +130,16 @@ template void GeneralVecTypeTest(const vtkm::Vec &) { typedef vtkm::Vec T; - + //grab the number of elements of T T a, b, c; - typename T::ComponentType s(5); + ComponentType s(5); for(vtkm::IdComponent i=0; i < T::NUM_COMPONENTS; ++i) { - a[i]=typename T::ComponentType((i+1)*2); - b[i]=typename T::ComponentType(i+1); - c[i]=typename T::ComponentType((i+1)*2); + a[i]=ComponentType((i+1)*2); + b[i]=ComponentType(i+1); + c[i]=ComponentType((i+1)*2); } //verify prefix and postfix increment and decrement @@ -149,36 +149,46 @@ void GeneralVecTypeTest(const vtkm::Vec &) c[T::NUM_COMPONENTS-1]--; //make c nearly like a to verify == and != are correct. - c[T::NUM_COMPONENTS-1]=(c[T::NUM_COMPONENTS-1]-1); + c[T::NUM_COMPONENTS-1]=ComponentType(c[T::NUM_COMPONENTS-1]-1); T plus = a + b; T correct_plus; for(vtkm::IdComponent i=0; i < T::NUM_COMPONENTS; ++i) - { correct_plus[i] = a[i] + b[i]; } + { + correct_plus[i] = ComponentType(a[i] + b[i]); + } VTKM_TEST_ASSERT(test_equal(plus, correct_plus),"Tuples not added correctly."); T minus = a - b; T correct_minus; for(vtkm::IdComponent i=0; i < T::NUM_COMPONENTS; ++i) - { correct_minus[i] = a[i] - b[i]; } + { + correct_minus[i] = ComponentType(a[i] - b[i]); + } VTKM_TEST_ASSERT(test_equal(minus, correct_minus),"Tuples not subtracted correctly."); T mult = a * b; T correct_mult; for(vtkm::IdComponent i=0; i < T::NUM_COMPONENTS; ++i) - { correct_mult[i] = a[i] * b[i]; } + { + correct_mult[i] = ComponentType(a[i] * b[i]); + } VTKM_TEST_ASSERT(test_equal(mult, correct_mult),"Tuples not multiplied correctly."); T div = a / b; T correct_div; for(vtkm::IdComponent i=0; i < T::NUM_COMPONENTS; ++i) - { correct_div[i] = a[i] / b[i]; } + { + correct_div[i] = ComponentType(a[i] / b[i]); + } VTKM_TEST_ASSERT(test_equal(div,correct_div),"Tuples not divided correctly."); mult = s * a; for(vtkm::IdComponent i=0; i < T::NUM_COMPONENTS; ++i) - { correct_mult[i] = s * a[i]; } + { + correct_mult[i] = ComponentType(s * a[i]); + } VTKM_TEST_ASSERT(test_equal(mult, correct_mult), "Scalar and Tuple did not multiply correctly."); @@ -186,10 +196,12 @@ void GeneralVecTypeTest(const vtkm::Vec &) VTKM_TEST_ASSERT(test_equal(mult, correct_mult), "Tuple and Scalar to not multiply correctly."); - typename T::ComponentType d = vtkm::dot(a, b); - typename T::ComponentType correct_d = 0; + ComponentType d = vtkm::dot(a, b); + ComponentType correct_d = 0; for(vtkm::IdComponent i=0; i < T::NUM_COMPONENTS; ++i) - {correct_d += a[i] * b[i]; } + { + correct_d = ComponentType(correct_d + a[i] * b[i]); + } VTKM_TEST_ASSERT(test_equal(d, correct_d), "dot(Tuple) wrong"); VTKM_TEST_ASSERT(!(a < b), "operator< wrong"); @@ -411,25 +423,25 @@ void TypeTest(Scalar) Scalar a = 4; Scalar b = 2; - Scalar plus = a + b; + Scalar plus = Scalar(a + b); if (plus != 6) { VTKM_TEST_FAIL("Scalars do not add correctly."); } - Scalar minus = a - b; + Scalar minus = Scalar(a - b); if (minus != 2) { VTKM_TEST_FAIL("Scalars to not subtract correctly."); } - Scalar mult = a * b; + Scalar mult = Scalar(a * b); if (mult != 8) { VTKM_TEST_FAIL("Scalars to not multiply correctly."); } - Scalar div = a / b; + Scalar div = Scalar(a / b); if (div != 2) { VTKM_TEST_FAIL("Scalars to not divide correctly."); diff --git a/vtkm/testing/VecTraitsTests.h b/vtkm/testing/VecTraitsTests.h index 110e304f8..deeaac724 100644 --- a/vtkm/testing/VecTraitsTests.h +++ b/vtkm/testing/VecTraitsTests.h @@ -62,7 +62,10 @@ static void TestVecTypeImpl( const ComponentType multiplier = 4; for (vtkm::IdComponent i = 0; i < NUM_COMPONENTS; i++) { - Traits::SetComponent(result, i, multiplier*Traits::GetComponent(vector, i)); + Traits::SetComponent(result, + i, + ComponentType( + multiplier*Traits::GetComponent(vector, i))); } VTKM_TEST_ASSERT(test_equal(Traits::ToVec(result), multiplier*Traits::ToVec(vector)), @@ -75,7 +78,7 @@ static void TestVecTypeImpl( for (vtkm::IdComponent i = 0; i < NUM_COMPONENTS; i++) { Traits::GetComponent(result, i) - = multiplier * Traits::GetComponent(vector, i); + = ComponentType(multiplier * Traits::GetComponent(vector, i)); } VTKM_TEST_ASSERT(test_equal(Traits::ToVec(result), multiplier*Traits::ToVec(vector)), @@ -88,7 +91,7 @@ static void TestVecTypeImpl( { ComponentType component = Traits::GetComponent(vector, i); - result += component * component; + result = ComponentType(result + (component * component)); } VTKM_TEST_ASSERT( test_equal(result,