Skip to content

Commit

Permalink
Fix several conversion warnings
Browse files Browse the repository at this point in the history
These are bugs reported by floating point conversion flags.
  • Loading branch information
stephenswat committed Jul 30, 2024
1 parent b647dee commit cf786ee
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 53 deletions.
2 changes: 2 additions & 0 deletions cmake/covfie-compiler-options-cpp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ if( ( "${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU" ) OR
covfie_add_flag( CMAKE_CXX_FLAGS "-Wshadow" )
covfie_add_flag( CMAKE_CXX_FLAGS "-Wunused-local-typedefs" )
covfie_add_flag( CMAKE_CXX_FLAGS "-pedantic" )
covfie_add_flag( CMAKE_CXX_FLAGS "-Wfloat-conversion" )
covfie_add_flag( CMAKE_CXX_FLAGS "-Wconversion" )

# Fail on warnings, if asked for that behaviour.
if( COVFIE_FAIL_ON_WARNINGS )
Expand Down
3 changes: 3 additions & 0 deletions cmake/covfie-compiler-options-cuda.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ if( "${CMAKE_CUDA_COMPILER_ID}" MATCHES "NVIDIA" )
covfie_add_flag( CMAKE_CUDA_FLAGS "--expt-relaxed-constexpr" )
endif()

covfie_add_flag( CMAKE_CUDA_FLAGS "-Wfloat-conversion" )
covfie_add_flag( CMAKE_CUDA_FLAGS "-Wconversion" )

# Fail on warnings, if asked for that behaviour.
if( COVFIE_FAIL_ON_WARNINGS )
if( ( "${CUDAToolkit_VERSION}" VERSION_GREATER_EQUAL "10.2" ) AND
Expand Down
2 changes: 1 addition & 1 deletion examples/common/bitmap/bitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void render_bitmap(
for (unsigned int i = 0; i < 256; ++i) {
float s = 0.7f;
float v = 1.0f;
float hue = (i / 256.0f) * 240.0f;
float hue = (static_cast<float>(i) / 256.0f) * 240.0f;
float c = v * s;
float hp = hue / 60.0f;

Expand Down
15 changes: 9 additions & 6 deletions examples/core/convert_bfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,12 @@ field_t read_bfield(const std::string & fn)
* Now that we have the limits of our field, compute the size in each
* dimension.
*/
std::size_t sx = std::lround((maxx - minx) / 100.0) + 1;
std::size_t sy = std::lround((maxy - miny) / 100.0) + 1;
std::size_t sz = std::lround((maxz - minz) / 100.0) + 1;
std::size_t sx =
static_cast<std::size_t>(std::lround((maxx - minx) / 100.0)) + 1;
std::size_t sy =
static_cast<std::size_t>(std::lround((maxy - miny) / 100.0)) + 1;
std::size_t sz =
static_cast<std::size_t>(std::lround((maxz - minz) / 100.0)) + 1;

BOOST_LOG_TRIVIAL(info)
<< "Magnetic field size is " << sx << "x" << sy << "x" << sz;
Expand All @@ -151,9 +154,9 @@ field_t read_bfield(const std::string & fn)
covfie::algebra::affine<3> translation =
covfie::algebra::affine<3>::translation(-minx, -miny, -minz);
covfie::algebra::affine<3> scaling = covfie::algebra::affine<3>::scaling(
(sx - 1) / (maxx - minx),
(sy - 1) / (maxy - miny),
(sz - 1) / (maxz - minz)
static_cast<float>(sx - 1) / (maxx - minx),
static_cast<float>(sy - 1) / (maxy - miny),
static_cast<float>(sz - 1) / (maxz - minz)
);

field_t field(covfie::make_parameter_pack(
Expand Down
15 changes: 9 additions & 6 deletions examples/core/convert_bfield_csv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,12 @@ field_t read_bfield(const std::string & fn)
* Now that we have the limits of our field, compute the size in each
* dimension.
*/
std::size_t sx = std::lround((maxx - minx) / 100.0) + 1;
std::size_t sy = std::lround((maxy - miny) / 100.0) + 1;
std::size_t sz = std::lround((maxz - minz) / 100.0) + 1;
std::size_t sx =
static_cast<std::size_t>(std::lround((maxx - minx) / 100.0)) + 1;
std::size_t sy =
static_cast<std::size_t>(std::lround((maxy - miny) / 100.0)) + 1;
std::size_t sz =
static_cast<std::size_t>(std::lround((maxz - minz) / 100.0)) + 1;

BOOST_LOG_TRIVIAL(info)
<< "Magnetic field size is " << sx << "x" << sy << "x" << sz;
Expand All @@ -167,9 +170,9 @@ field_t read_bfield(const std::string & fn)
covfie::algebra::affine<3> translation =
covfie::algebra::affine<3>::translation(-minx, -miny, -minz);
covfie::algebra::affine<3> scaling = covfie::algebra::affine<3>::scaling(
(sx - 1) / (maxx - minx),
(sy - 1) / (maxy - miny),
(sz - 1) / (maxz - minz)
static_cast<float>(sx - 1) / (maxx - minx),
static_cast<float>(sy - 1) / (maxy - miny),
static_cast<float>(sz - 1) / (maxz - minz)
);

field_t field(covfie::make_parameter_pack(
Expand Down
12 changes: 6 additions & 6 deletions examples/core/scaleup_bfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ int main(int argc, char ** argv)
for (std::size_t y = 0; y < 601; ++y) {
for (std::size_t z = 0; z < 901; ++z) {
ov.at(
-10000.f + x * 33.333333333f,
-10000.f + y * 33.333333333f,
-15000.f + z * 33.333333333f
-10000.f + static_cast<float>(x) * 33.333333333f,
-10000.f + static_cast<float>(y) * 33.333333333f,
-15000.f + static_cast<float>(z) * 33.333333333f
) =
iv.at(
-10000.f + x * 33.333333333f,
-10000.f + y * 33.333333333f,
-15000.f + z * 33.333333333f
-10000.f + static_cast<float>(x) * 33.333333333f,
-10000.f + static_cast<float>(y) * 33.333333333f,
-15000.f + static_cast<float>(z) * 33.333333333f
);
}
}
Expand Down
6 changes: 4 additions & 2 deletions examples/cpu/render_slice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@ int main(int argc, char ** argv)

for (unsigned int x = 0; x < vm["width"].as<unsigned int>(); ++x) {
for (unsigned int y = 0; y < vm["height"].as<unsigned int>(); ++y) {
float fx = x / static_cast<float>(vm["width"].as<unsigned int>());
float fy = y / static_cast<float>(vm["height"].as<unsigned int>());
float fx = static_cast<float>(x) /
static_cast<float>(vm["width"].as<unsigned int>());
float fy = static_cast<float>(y) /
static_cast<float>(vm["height"].as<unsigned int>());

decltype(fv)::output_t p;

Expand Down
4 changes: 2 additions & 2 deletions examples/cuda/render_slice.cu
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ __global__ void render(
int y = blockDim.y * blockIdx.y + threadIdx.y;

if (x < width && y < height) {
float fx = x / static_cast<float>(width);
float fy = y / static_cast<float>(height);
float fx = static_cast<float>(x) / static_cast<float>(width);
float fy = static_cast<float>(y) / static_cast<float>(height);

typename field_t::output_t p =
vf.at(fx * 20000.f - 10000.f, fy * 20000.f - 10000.f, z);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ struct nearest_neighbour {

for (std::size_t i = 0; i < contravariant_output_t::dimensions; ++i)
{
nc[i] = std::lrintf(c[i]);
nc[i] = static_cast<typename contravariant_output_t::scalar_t>(
std::lrintf(c[i])
);
}

return m_backend.at(nc);
Expand Down
4 changes: 2 additions & 2 deletions lib/core/covfie/core/backend/transformer/strided.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct strided {
std::accumulate(
std::begin(sizes),
std::end(sizes),
1,
1ul,
std::multiplies<std::size_t>()
)
);
Expand Down Expand Up @@ -136,7 +136,7 @@ struct strided {
std::accumulate(
std::begin(m_sizes),
std::end(m_sizes),
1,
1ul,
std::multiplies<std::size_t>()
),
make_strided_copy(o)
Expand Down
31 changes: 18 additions & 13 deletions lib/cuda/covfie/cuda/backend/primitive/cuda_texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ struct cuda_texture {
typename T::parent_t::covariant_output_t::vector_t v =
no.at(ia);

typename contravariant_input_t::scalar_t idx = 0;
std::size_t idx = 0;

for (std::size_t k = contravariant_input_t::dimensions - 1;
k <= contravariant_input_t::dimensions;
--k)
{
typename contravariant_input_t::scalar_t tmp = ia[k];
std::size_t tmp = ia[k];

for (std::size_t l = k - 1; l < k; --l) {
tmp *= srcSize[l];
Expand All @@ -123,21 +123,26 @@ struct cuda_texture {
idx += tmp;
}

std::size_t idx2 = static_cast<std::size_t>(idx);

using stage_scalar_t =
typename covariant_output_t::scalar_t;

if constexpr (covariant_output_t::dimensions == 1) {
stage[idx] = v[0];
stage[idx2] = static_cast<stage_scalar_t>(v[0]);
} else if constexpr (covariant_output_t::dimensions == 2) {
stage[idx].x = v[0];
stage[idx].y = v[1];
stage[idx2].x = static_cast<stage_scalar_t>(v[0]);
stage[idx2].y = static_cast<stage_scalar_t>(v[1]);
} else if constexpr (covariant_output_t::dimensions == 3) {
stage[idx].x = v[0];
stage[idx].y = v[1];
stage[idx].z = v[2];
stage[idx].w = 0.f;
stage[idx2].x = static_cast<stage_scalar_t>(v[0]);
stage[idx2].y = static_cast<stage_scalar_t>(v[1]);
stage[idx2].z = static_cast<stage_scalar_t>(v[2]);
stage[idx2].w = static_cast<stage_scalar_t>(0.f);
} else if constexpr (covariant_output_t::dimensions == 4) {
stage[idx].x = v[0];
stage[idx].y = v[1];
stage[idx].z = v[2];
stage[idx].w = v[3];
stage[idx2].x = static_cast<stage_scalar_t>(v[0]);
stage[idx2].y = static_cast<stage_scalar_t>(v[1]);
stage[idx2].z = static_cast<stage_scalar_t>(v[2]);
stage[idx2].w = static_cast<stage_scalar_t>(v[3]);
}
}),
std::tuple_cat(srcSize)
Expand Down
60 changes: 46 additions & 14 deletions tests/cpu/test_cpu_array_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ TEST(TestFieldViewCPUArrayBackend, WriteRead1DSingleFloat)

for (std::size_t x = 0; x < 5; ++x) {
for (std::size_t j = 0; j < 1; ++j) {
fv.at(x)[j] = 1000.f * x + 1.f * j;
fv.at(x)[j] =
1000.f * static_cast<float>(x) + 1.f * static_cast<float>(j);
}
}

for (std::size_t x = 0; x < 5; ++x) {
for (std::size_t j = 0; j < 1; ++j) {
EXPECT_EQ(fv.at(x)[j], 1000.f * x + 1.f * j);
EXPECT_EQ(
fv.at(x)[j],
1000.f * static_cast<float>(x) + 1.f * static_cast<float>(j)
);
}
}
}
Expand All @@ -55,13 +59,17 @@ TEST(TestFieldViewCPUArrayBackend, WriteRead1DArrayFloat)

for (std::size_t x = 0; x < 5; ++x) {
for (std::size_t j = 0; j < 3; ++j) {
fv.at(x)[j] = 1000.f * x + 1.f * j;
fv.at(x)[j] =
1000.f * static_cast<float>(x) + 1.f * static_cast<float>(j);
}
}

for (std::size_t x = 0; x < 5; ++x) {
for (std::size_t j = 0; j < 3; ++j) {
EXPECT_EQ(fv.at(x)[j], 1000.f * x + 1.f * j);
EXPECT_EQ(
fv.at(x)[j],
1000.f * static_cast<float>(x) + 1.f * static_cast<float>(j)
);
}
}
}
Expand All @@ -79,15 +87,22 @@ TEST(TestFieldViewCPUArrayBackend, WriteRead2DSingleFloat)
for (std::size_t x = 0; x < 5; ++x) {
for (std::size_t y = 0; y < 7; ++y) {
for (std::size_t j = 0; j < 1; ++j) {
fv.at(x, y)[j] = 1000.f * x + 100.f * y + 1.f * j;
fv.at(x, y)[j] = 1000.f * static_cast<float>(x) +
100.f * static_cast<float>(y) +
1.f * static_cast<float>(j);
}
}
}

for (std::size_t x = 0; x < 5; ++x) {
for (std::size_t y = 0; y < 7; ++y) {
for (std::size_t j = 0; j < 1; ++j) {
EXPECT_EQ(fv.at(x, y)[j], 1000.f * x + 100.f * y + 1.f * j);
EXPECT_EQ(
fv.at(x, y)[j],
1000.f * static_cast<float>(x) +
100.f * static_cast<float>(y) +
1.f * static_cast<float>(j)
);
}
}
}
Expand All @@ -106,15 +121,22 @@ TEST(TestFieldViewCPUArrayBackend, WriteRead2DArrayFloat)
for (std::size_t x = 0; x < 5; ++x) {
for (std::size_t y = 0; y < 7; ++y) {
for (std::size_t j = 0; j < 3; ++j) {
fv.at(x, y)[j] = 1000.f * x + 100.f * y + 1.f * j;
fv.at(x, y)[j] = 1000.f * static_cast<float>(x) +
100.f * static_cast<float>(y) +
1.f * static_cast<float>(j);
}
}
}

for (std::size_t x = 0; x < 5; ++x) {
for (std::size_t y = 0; y < 7; ++y) {
for (std::size_t j = 0; j < 3; ++j) {
EXPECT_EQ(fv.at(x, y)[j], 1000.f * x + 100.f * y + 1.f * j);
EXPECT_EQ(
fv.at(x, y)[j],
1000.f * static_cast<float>(x) +
100.f * static_cast<float>(y) +
1.f * static_cast<float>(j)
);
}
}
}
Expand All @@ -134,8 +156,10 @@ TEST(TestFieldViewCPUArrayBackend, WriteRead3DSingleFloat)
for (std::size_t y = 0; y < 7; ++y) {
for (std::size_t z = 0; z < 2; ++z) {
for (std::size_t j = 0; j < 1; ++j) {
fv.at(x, y, z)[j] =
1000.f * x + 100.f * y + 10.f * z + 1.f * j;
fv.at(x, y, z)[j] = 1000.f * static_cast<float>(x) +
100.f * static_cast<float>(y) +
10.f * static_cast<float>(z) +
1.f * static_cast<float>(j);
}
}
}
Expand All @@ -147,7 +171,10 @@ TEST(TestFieldViewCPUArrayBackend, WriteRead3DSingleFloat)
for (std::size_t j = 0; j < 1; ++j) {
EXPECT_EQ(
fv.at(x, y, z)[j],
1000.f * x + 100.f * y + 10.f * z + 1.f * j
1000.f * static_cast<float>(x) +
100.f * static_cast<float>(y) +
10.f * static_cast<float>(z) +
1.f * static_cast<float>(j)
);
}
}
Expand All @@ -169,8 +196,10 @@ TEST(TestFieldViewCPUArrayBackend, WriteRead3DArrayFloat)
for (std::size_t y = 0; y < 7; ++y) {
for (std::size_t z = 0; z < 2; ++z) {
for (std::size_t j = 0; j < 3; ++j) {
fv.at(x, y, z)[j] =
1000.f * x + 100.f * y + 10.f * z + 1.f * j;
fv.at(x, y, z)[j] = 1000.f * static_cast<float>(x) +
100.f * static_cast<float>(y) +
10.f * static_cast<float>(z) +
1.f * static_cast<float>(j);
}
}
}
Expand All @@ -182,7 +211,10 @@ TEST(TestFieldViewCPUArrayBackend, WriteRead3DArrayFloat)
for (std::size_t j = 0; j < 3; ++j) {
EXPECT_EQ(
fv.at(x, y, z)[j],
1000.f * x + 100.f * y + 10.f * z + 1.f * j
1000.f * static_cast<float>(x) +
100.f * static_cast<float>(y) +
10.f * static_cast<float>(z) +
1.f * static_cast<float>(j)
);
}
}
Expand Down

0 comments on commit cf786ee

Please sign in to comment.