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.
This commit is contained in:
Kenneth Moreland 2015-07-30 16:43:19 -06:00
parent 21b3b318ba
commit 04fce28ae3
11 changed files with 61 additions and 42 deletions

@ -161,13 +161,13 @@ vtkm::Matrix<T,NumRow,NumCol> 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;
}

@ -677,7 +677,7 @@ struct Add
template<typename T>
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<typename T>
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<typename T>
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<typename T>
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<typename T>
VTKM_EXEC_CONT_EXPORT T operator()(const T &x) const
{
return -x;
return T(-x);
}
};
@ -1055,10 +1055,10 @@ template<typename T, vtkm::IdComponent Size>
VTKM_EXEC_CONT_EXPORT
T dot(const vtkm::Vec<T,Size> &a, const vtkm::Vec<T,Size> &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<typename T>
VTKM_EXEC_CONT_EXPORT
T dot(const vtkm::Vec<T,2> &a, const vtkm::Vec<T,2> &b)
{
return (a[0]*b[0]) + (a[1]*b[1]);
return T((a[0]*b[0]) + (a[1]*b[1]));
}
template<typename T>
VTKM_EXEC_CONT_EXPORT
T dot(const vtkm::Vec<T,3> &a, const vtkm::Vec<T,3> &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<typename T>
VTKM_EXEC_CONT_EXPORT
T dot(const vtkm::Vec<T,4> &a, const vtkm::Vec<T,4> &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

@ -74,7 +74,8 @@ public:
VTKM_EXEC_CONT_EXPORT
ValueType Get(vtkm::Id index) const {
return StartingValue + ValueType(static_cast<ComponentType>(index));
return ValueType(StartingValue +
ValueType(static_cast<ComponentType>(index)));
}
private:

@ -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)));
}
};

@ -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));
}
}
};

@ -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<typename ValueType>

@ -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);

@ -397,7 +397,7 @@ vtkm::Vec<T,N> TestValue(vtkm::Id index, vtkm::Vec<T,N>) {
vtkm::Vec<T,N> 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;
}

@ -55,6 +55,7 @@ struct MatrixTest
static const vtkm::IdComponent NUM_ROWS = NumRow;
static const vtkm::IdComponent NUM_COLS = NumCol;
typedef vtkm::Matrix<T,NUM_ROWS,NUM_COLS> 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)
{

@ -130,16 +130,16 @@ template<typename ComponentType, vtkm::IdComponent Size>
void GeneralVecTypeTest(const vtkm::Vec<ComponentType,Size> &)
{
typedef vtkm::Vec<ComponentType,Size> 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<ComponentType,Size> &)
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<ComponentType,Size> &)
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.");

@ -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,