diff --git a/cmake/covfie-compiler-options-cpp.cmake b/cmake/covfie-compiler-options-cpp.cmake index 4210a9e..1d03a79 100644 --- a/cmake/covfie-compiler-options-cpp.cmake +++ b/cmake/covfie-compiler-options-cpp.cmake @@ -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 ) diff --git a/cmake/covfie-compiler-options-cuda.cmake b/cmake/covfie-compiler-options-cuda.cmake index 5af4ca4..2d5bf3b 100644 --- a/cmake/covfie-compiler-options-cuda.cmake +++ b/cmake/covfie-compiler-options-cuda.cmake @@ -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 diff --git a/examples/common/bitmap/bitmap.cpp b/examples/common/bitmap/bitmap.cpp index 5f2f673..2b862e0 100644 --- a/examples/common/bitmap/bitmap.cpp +++ b/examples/common/bitmap/bitmap.cpp @@ -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(i) / 256.0f) * 240.0f; float c = v * s; float hp = hue / 60.0f; diff --git a/examples/core/convert_bfield.cpp b/examples/core/convert_bfield.cpp index 756c6c9..c257709 100644 --- a/examples/core/convert_bfield.cpp +++ b/examples/core/convert_bfield.cpp @@ -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::lround((maxx - minx) / 100.0)) + 1; + std::size_t sy = + static_cast(std::lround((maxy - miny) / 100.0)) + 1; + std::size_t sz = + static_cast(std::lround((maxz - minz) / 100.0)) + 1; BOOST_LOG_TRIVIAL(info) << "Magnetic field size is " << sx << "x" << sy << "x" << sz; @@ -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(sx - 1) / (maxx - minx), + static_cast(sy - 1) / (maxy - miny), + static_cast(sz - 1) / (maxz - minz) ); field_t field(covfie::make_parameter_pack( diff --git a/examples/core/convert_bfield_csv.cpp b/examples/core/convert_bfield_csv.cpp index 452c5a0..4e98ee9 100644 --- a/examples/core/convert_bfield_csv.cpp +++ b/examples/core/convert_bfield_csv.cpp @@ -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::lround((maxx - minx) / 100.0)) + 1; + std::size_t sy = + static_cast(std::lround((maxy - miny) / 100.0)) + 1; + std::size_t sz = + static_cast(std::lround((maxz - minz) / 100.0)) + 1; BOOST_LOG_TRIVIAL(info) << "Magnetic field size is " << sx << "x" << sy << "x" << sz; @@ -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(sx - 1) / (maxx - minx), + static_cast(sy - 1) / (maxy - miny), + static_cast(sz - 1) / (maxz - minz) ); field_t field(covfie::make_parameter_pack( diff --git a/examples/core/scaleup_bfield.cpp b/examples/core/scaleup_bfield.cpp index 6579287..0e95689 100644 --- a/examples/core/scaleup_bfield.cpp +++ b/examples/core/scaleup_bfield.cpp @@ -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(x) * 33.333333333f, + -10000.f + static_cast(y) * 33.333333333f, + -15000.f + static_cast(z) * 33.333333333f ) = iv.at( - -10000.f + x * 33.333333333f, - -10000.f + y * 33.333333333f, - -15000.f + z * 33.333333333f + -10000.f + static_cast(x) * 33.333333333f, + -10000.f + static_cast(y) * 33.333333333f, + -15000.f + static_cast(z) * 33.333333333f ); } } diff --git a/examples/cpu/render_slice.cpp b/examples/cpu/render_slice.cpp index 9cbae5f..215de3e 100644 --- a/examples/cpu/render_slice.cpp +++ b/examples/cpu/render_slice.cpp @@ -128,8 +128,10 @@ int main(int argc, char ** argv) for (unsigned int x = 0; x < vm["width"].as(); ++x) { for (unsigned int y = 0; y < vm["height"].as(); ++y) { - float fx = x / static_cast(vm["width"].as()); - float fy = y / static_cast(vm["height"].as()); + float fx = static_cast(x) / + static_cast(vm["width"].as()); + float fy = static_cast(y) / + static_cast(vm["height"].as()); decltype(fv)::output_t p; diff --git a/examples/cuda/render_slice.cu b/examples/cuda/render_slice.cu index 1b53654..cdd3e66 100644 --- a/examples/cuda/render_slice.cu +++ b/examples/cuda/render_slice.cu @@ -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(width); - float fy = y / static_cast(height); + float fx = static_cast(x) / static_cast(width); + float fy = static_cast(y) / static_cast(height); typename field_t::output_t p = vf.at(fx * 20000.f - 10000.f, fy * 20000.f - 10000.f, z); diff --git a/lib/core/covfie/core/backend/transformer/nearest_neighbour.hpp b/lib/core/covfie/core/backend/transformer/nearest_neighbour.hpp index d52425f..f35b09f 100644 --- a/lib/core/covfie/core/backend/transformer/nearest_neighbour.hpp +++ b/lib/core/covfie/core/backend/transformer/nearest_neighbour.hpp @@ -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( + std::lrintf(c[i]) + ); } return m_backend.at(nc); diff --git a/lib/core/covfie/core/backend/transformer/strided.hpp b/lib/core/covfie/core/backend/transformer/strided.hpp index c24dec2..bc6b656 100644 --- a/lib/core/covfie/core/backend/transformer/strided.hpp +++ b/lib/core/covfie/core/backend/transformer/strided.hpp @@ -61,7 +61,7 @@ struct strided { std::accumulate( std::begin(sizes), std::end(sizes), - 1, + 1ul, std::multiplies() ) ); @@ -136,7 +136,7 @@ struct strided { std::accumulate( std::begin(m_sizes), std::end(m_sizes), - 1, + 1ul, std::multiplies() ), make_strided_copy(o) diff --git a/lib/cuda/covfie/cuda/backend/primitive/cuda_texture.hpp b/lib/cuda/covfie/cuda/backend/primitive/cuda_texture.hpp index 221a120..812e711 100644 --- a/lib/cuda/covfie/cuda/backend/primitive/cuda_texture.hpp +++ b/lib/cuda/covfie/cuda/backend/primitive/cuda_texture.hpp @@ -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]; @@ -123,21 +123,26 @@ struct cuda_texture { idx += tmp; } + std::size_t idx2 = static_cast(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(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(v[0]); + stage[idx2].y = static_cast(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(v[0]); + stage[idx2].y = static_cast(v[1]); + stage[idx2].z = static_cast(v[2]); + stage[idx2].w = static_cast(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(v[0]); + stage[idx2].y = static_cast(v[1]); + stage[idx2].z = static_cast(v[2]); + stage[idx2].w = static_cast(v[3]); } }), std::tuple_cat(srcSize) diff --git a/tests/cpu/test_cpu_array_backend.cpp b/tests/cpu/test_cpu_array_backend.cpp index 49c3441..f3346ba 100644 --- a/tests/cpu/test_cpu_array_backend.cpp +++ b/tests/cpu/test_cpu_array_backend.cpp @@ -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(x) + 1.f * static_cast(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(x) + 1.f * static_cast(j) + ); } } } @@ -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(x) + 1.f * static_cast(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(x) + 1.f * static_cast(j) + ); } } } @@ -79,7 +87,9 @@ 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(x) + + 100.f * static_cast(y) + + 1.f * static_cast(j); } } } @@ -87,7 +97,12 @@ 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) { - 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(x) + + 100.f * static_cast(y) + + 1.f * static_cast(j) + ); } } } @@ -106,7 +121,9 @@ 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(x) + + 100.f * static_cast(y) + + 1.f * static_cast(j); } } } @@ -114,7 +131,12 @@ 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) { - 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(x) + + 100.f * static_cast(y) + + 1.f * static_cast(j) + ); } } } @@ -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(x) + + 100.f * static_cast(y) + + 10.f * static_cast(z) + + 1.f * static_cast(j); } } } @@ -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(x) + + 100.f * static_cast(y) + + 10.f * static_cast(z) + + 1.f * static_cast(j) ); } } @@ -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(x) + + 100.f * static_cast(y) + + 10.f * static_cast(z) + + 1.f * static_cast(j); } } } @@ -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(x) + + 100.f * static_cast(y) + + 10.f * static_cast(z) + + 1.f * static_cast(j) ); } }