diff --git a/Application/src/commons/common/commons.pc.h b/Application/src/commons/common/commons.pc.h
index 38bcb34e3a762f951829425ad9d42ba8adc52632..fa3e178822bbdc87a93ed4a31c87e3658c65f136 100644
--- a/Application/src/commons/common/commons.pc.h
+++ b/Application/src/commons/common/commons.pc.h
@@ -58,24 +58,6 @@
 #ifdef WIN32
 #define _USE_MATH_DEFINES
 #include <cmath>
-
-#if (_MSC_VER <= 1916)
-    // visual studio 2017 does not have __builtin_clzl
-    #include <intrin.h>
-
-    static inline int __builtin_clz(unsigned x) {
-        return (int)__lzcnt(x);
-    }
-
-    static inline int __builtin_clzll(unsigned long long x) {
-        return (int)__lzcnt64(x);
-    }
-
-    static inline int __builtin_clzl(unsigned long x) {
-        return sizeof(x) == 8 ? __builtin_clzll(x) : __builtin_clz((uint32_t)x);
-    }
-#endif
-
 #endif
 
 typedef int32_t long_t;
diff --git a/Application/src/commons/common/gui/GLImpl.cpp b/Application/src/commons/common/gui/GLImpl.cpp
index 903df07ca5178a39190c18a51f4241fcb3869acb..7781ee1b96bbfb1ba3499021137255becda4987f 100644
--- a/Application/src/commons/common/gui/GLImpl.cpp
+++ b/Application/src/commons/common/gui/GLImpl.cpp
@@ -398,7 +398,7 @@ TexturePtr GLImpl::texture(const Image * ptr) {
         }
     }
     
-    auto width = next_pow2(ptr->cols), height = next_pow2(ptr->rows);
+    auto width = next_pow2(sign_cast<uint64_t>(ptr->cols)), height = next_pow2(sign_cast<uint64_t>(ptr->rows));
     auto capacity = size_t(ptr->dims) * size_t(width) * size_t(height);
     if (empty.size() < capacity)
         empty.resize(capacity, 0);
diff --git a/Application/src/commons/common/gui/IMGUIBase.cpp b/Application/src/commons/common/gui/IMGUIBase.cpp
index dd117157ad2b8f4f951f4ab765dd80931c326f30..fb3fc2aaef1ee692396faee7000caf195fdb3ca8 100644
--- a/Application/src/commons/common/gui/IMGUIBase.cpp
+++ b/Application/src/commons/common/gui/IMGUIBase.cpp
@@ -152,8 +152,8 @@ void clear_cache() {
         static Size2 gpu_size_of(const ExternalImage* image) {
             if(!image || !image->source())
                 return Size2();
-            return Size2(next_pow2((uint64_t)image->source()->bounds().width),
-                         next_pow2((uint64_t)image->source()->bounds().height));
+            return Size2(next_pow2(sign_cast<uint64_t>(image->source()->bounds().width)),
+                         next_pow2(sign_cast<uint64_t>(image->source()->bounds().height)));
         }
         
         void update_with(const ExternalImage* image) {
diff --git a/Application/src/commons/common/gui/MetalImpl.mm b/Application/src/commons/common/gui/MetalImpl.mm
index e24bd469ed8d4a7591f6986aa26cbccd1f44c941..a8f2c78be48089195c873726fda4739962a54d1a 100644
--- a/Application/src/commons/common/gui/MetalImpl.mm
+++ b/Application/src/commons/common/gui/MetalImpl.mm
@@ -2,6 +2,7 @@
 
 #include <types.h>
 #include <misc/metastring.h>
+#include <misc/checked_casts.h>
 #include "MetalImpl.h"
 
 #include <imgui/imgui.h>
@@ -354,8 +355,8 @@ bool MetalImpl::open_files(const std::vector<file::Path> &paths) {
     TexturePtr MetalImpl::texture(const Image * ptr) {
         GLIMPL_CHECK_THREAD_ID();
         
-        uint width = next_pow2(ptr->cols);
-        uint height = next_pow2(ptr->rows);
+        uint width = next_pow2(sign_cast<uint64_t>(ptr->cols));
+        uint height = next_pow2(sign_cast<uint64_t>(ptr->rows));
         
         auto input_format = MTLPixelFormatRGBA8Unorm;
         if(ptr->dims == 1) {
diff --git a/Application/src/commons/common/misc/checked_casts.h b/Application/src/commons/common/misc/checked_casts.h
index b81197a72929d19217e12ac274e0d23f0d6a1573..b6dfcfbfe540391e2c549d68c58814cc5dfe16d7 100644
--- a/Application/src/commons/common/misc/checked_casts.h
+++ b/Application/src/commons/common/misc/checked_casts.h
@@ -54,8 +54,7 @@ constexpr To sign_cast(From&& value) {
     using FromType = typename remove_cvref<From>::type;
     using ToType = typename remove_cvref<To>::type;
     
-    if constexpr(!std::is_floating_point<ToType>::value
-                 && std::is_integral<ToType>::value)
+    if constexpr(std::is_integral<ToType>::value)
     {
         if constexpr(std::is_signed<ToType>::value) {
             if (value > std::numeric_limits<ToType>::max())
diff --git a/Application/src/commons/common/misc/math.h b/Application/src/commons/common/misc/math.h
index fda8c982a5703de8ba14574ada16e5ad8ae2672b..f6cba881af386e58c1e0508748cef7888ee8db08 100644
--- a/Application/src/commons/common/misc/math.h
+++ b/Application/src/commons/common/misc/math.h
@@ -1,5 +1,28 @@
 #pragma once
 
+#ifdef WIN32
+#define _USE_MATH_DEFINES
+#include <cmath>
+
+#if (_MSC_VER <= 1916)
+    // visual studio 2017 does not have __builtin_clzl
+    #include <intrin.h>
+
+    static inline int __builtin_clz(unsigned x) {
+        return (int)__lzcnt(x);
+    }
+
+    static inline int __builtin_clzll(unsigned long long x) {
+        return (int)__lzcnt64(x);
+    }
+
+    static inline int __builtin_clzl(unsigned long x) {
+        return sizeof(x) == 8 ? __builtin_clzll(x) : __builtin_clz((uint32_t)x);
+    }
+#endif
+
+#endif
+
 namespace cmn {
     template<typename T = double>
     inline T cos(const T& s) {
diff --git a/Application/src/tracker/gui/IdentityHeatmap.cpp b/Application/src/tracker/gui/IdentityHeatmap.cpp
index cee90edc041c7c4a7525d99cd88f274f8d3583d6..5f20a7dee3821fd335e76818ddc8d94487771dc8 100644
--- a/Application/src/tracker/gui/IdentityHeatmap.cpp
+++ b/Application/src/tracker/gui/IdentityHeatmap.cpp
@@ -804,7 +804,7 @@ void Leaf::clear() {
 }
 
 void Grid::create(const Size2 &image_dimensions) {
-    uint32_t dim = (uint32_t)image_dimensions.max();
+    auto dim = sign_cast<uint32_t>(image_dimensions.max());
     dim = (uint32_t)next_pow2(dim); // ensure that it is always divisible by two
     Debug("Creating a grid of size %ux%u (for image of size %.0fx%.0f)", dim, dim, image_dimensions.width, image_dimensions.height);
     
diff --git a/Application/src/tracker/tracking/Accumulation.cpp b/Application/src/tracker/tracking/Accumulation.cpp
index 5365840e681a461069cdb0408f64d907c7347280..a578cdd2c5be83ac0008420b254d86281d4f00b2 100644
--- a/Application/src/tracker/tracking/Accumulation.cpp
+++ b/Application/src/tracker/tracking/Accumulation.cpp
@@ -906,7 +906,7 @@ bool Accumulation::start() {
                     if(distance < min_distance) min_distance = distance;
                     //distance = roundf((1 - SQR(average)) * 10) * 10;
                     
-                    range_distance = narrow_cast<int64_t>(next_pow2(range_distance));
+                    range_distance = narrow_cast<int64_t>(next_pow2(sign_cast<uint64_t>(range_distance)));
                     
                     copied_sorted.insert({distance, range_distance, q, cached, range, extended_range, samples});
                 } else {