diff --git a/src/dawn/common/Numeric.h b/src/dawn/common/Numeric.h index f8e559599e..a9a4521fc9 100644 --- a/src/dawn/common/Numeric.h +++ b/src/dawn/common/Numeric.h @@ -16,6 +16,9 @@ #define SRC_DAWN_COMMON_NUMERIC_H_ #include +#include + +#include "dawn/common/Assert.h" namespace detail { @@ -39,4 +42,12 @@ inline constexpr uint32_t u32_sizeof = detail::u32_sizeof(); template inline constexpr uint32_t u32_alignof = detail::u32_alignof(); +// Only defined for unsigned integers because that is all that is +// needed at the time of writing. +template >> +inline Dst checked_cast(const Src& value) { + ASSERT(value <= std::numeric_limits::max()); + return static_cast(value); +} + #endif // SRC_DAWN_COMMON_NUMERIC_H_ diff --git a/src/dawn/utils/WGPUHelpers.cpp b/src/dawn/utils/WGPUHelpers.cpp index 7c36d81428..52e070af3b 100644 --- a/src/dawn/utils/WGPUHelpers.cpp +++ b/src/dawn/utils/WGPUHelpers.cpp @@ -22,6 +22,7 @@ #include "dawn/common/Constants.h" #include "dawn/common/Log.h" +#include "dawn/common/Numeric.h" #include "spirv-tools/optimizer.hpp" @@ -382,7 +383,7 @@ namespace utils { wgpu::BindGroupDescriptor descriptor; descriptor.layout = layout; - descriptor.entryCount = entries.size(); + descriptor.entryCount = checked_cast(entries.size()); descriptor.entries = entries.data(); return device.CreateBindGroup(&descriptor);