Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[coverity] Fix control flow issues in core (#30328)

Pawel Raasz4e4b51b55994
diff --git a/src/core/reference/include/openvino/reference/cum_sum.hpp b/src/core/reference/include/openvino/reference/cum_sum.hpp
index 863c745f587..14eb9a886e8 100644
--- a/src/core/reference/include/openvino/reference/cum_sum.hpp
+++ b/src/core/reference/include/openvino/reference/cum_sum.hpp
@@ -6,17 +6,24 @@
#include <cmath>
+#include "openvino/core/shape.hpp"
+
namespace ov {
namespace reference {
template <typename T, typename P>
void cumsum(const T* arg, const P axis, T* out, const Shape& tensor_shape, const bool exclusive, const bool reverse) {
- const auto rank = tensor_shape.size();
- const auto normalized_axis = axis >= 0 ? axis : rank + axis;
+ const auto normalized_axis = [axis, rank = tensor_shape.size()] {
+ if constexpr (std::is_unsigned_v<decltype(axis)>) {
+ return axis;
+ } else {
+ return axis < 0 ? axis + rank : axis;
+ }
+ }();
const auto axis_dim = tensor_shape[normalized_axis];
const auto size_before_axis = shape_size(tensor_shape.begin(), tensor_shape.begin() + normalized_axis);
- const auto size_after_axis = shape_size(tensor_shape.begin() + normalized_axis + 1, tensor_shape.end());
+ const auto size_after_axis = shape_size(tensor_shape.begin() + (normalized_axis + 1), tensor_shape.end());
const auto reverse_shift = reverse ? -1 : 1;
const auto element_shift = exclusive ? size_after_axis * reverse_shift : 0;
diff --git a/src/core/reference/include/openvino/reference/divide.hpp b/src/core/reference/include/openvino/reference/divide.hpp
index e0b63b1c119..fa407ce559c 100644
--- a/src/core/reference/include/openvino/reference/divide.hpp
+++ b/src/core/reference/include/openvino/reference/divide.hpp
@@ -40,18 +40,22 @@ T try_python_div(const T x, const T y) {
}
T quot = div(x, y);
- return (((x < 0) != (y < 0)) && (x % y != 0)) ? quot - T{1} : quot;
+ if constexpr (std::is_unsigned_v<T>) {
+ return quot;
+ } else {
+ return (((x < 0) != (y < 0)) && (x % y != 0)) ? quot - T{1} : quot;
+ }
}
} // namespace func
template <typename T>
-typename std::enable_if<std::is_integral<T>::value>::type divide(const T* arg0,
- const T* arg1,
- T* out,
- const Shape& arg0_shape,
- const Shape& arg1_shape,
- const op::AutoBroadcastSpec& broadcast_spec,
- bool pythondiv) {
+std::enable_if_t<std::is_integral_v<T>> divide(const T* arg0,
+ const T* arg1,
+ T* out,
+ const Shape& arg0_shape,
+ const Shape& arg1_shape,
+ const op::AutoBroadcastSpec& broadcast_spec,
+ bool pythondiv) {
auto div = pythondiv ? func::try_python_div<T> : func::try_div<T>;
autobroadcast_binop(arg0, arg1, out, arg0_shape, arg1_shape, broadcast_spec, div);
}
@@ -59,8 +63,7 @@ typename std::enable_if<std::is_integral<T>::value>::type divide(const T* arg0,
// In English: return type is void and T must be a standard floating point type, or
// bfloat16, or float16.
template <typename T>
-typename std::enable_if<std::is_floating_point<T>::value || std::is_same<T, bfloat16>::value ||
- std::is_same<T, float16>::value>::type
+std::enable_if_t<std::is_floating_point_v<T> || std::is_same_v<T, bfloat16> || std::is_same_v<T, float16>>
divide(const T* arg0, const T* arg1, T* out, size_t count, bool) {
// TODO: Here we do not check for div by zero, so we'll get +-inf here
// if arg1[i] == 0. Is that the right thing to do? Jury's still out.
@@ -68,15 +71,14 @@ divide(const T* arg0, const T* arg1, T* out, size_t count, bool) {
}
template <typename T>
-typename std::enable_if<std::is_floating_point<T>::value || std::is_same<T, bfloat16>::value ||
- std::is_same<T, float16>::value>::type
-divide(const T* arg0,
- const T* arg1,
- T* out,
- const Shape& arg0_shape,
- const Shape& arg1_shape,
- const op::AutoBroadcastSpec& broadcast_spec,
- bool) {
+std::enable_if_t<std::is_floating_point_v<T> || std::is_same_v<T, bfloat16> || std::is_same_v<T, float16>> divide(
+ const T* arg0,
+ const T* arg1,
+ T* out,
+ const Shape& arg0_shape,
+ const Shape& arg1_shape,
+ const op::AutoBroadcastSpec& broadcast_spec,
+ bool) {
autobroadcast_binop(arg0, arg1, out, arg0_shape, arg1_shape, broadcast_spec, func::div<T>);
}
} // namespace reference
diff --git a/src/core/src/type/float4_e2m1.cpp b/src/core/src/type/float4_e2m1.cpp
index 125d4a3558e..ee5316aa515 100644
--- a/src/core/src/type/float4_e2m1.cpp
+++ b/src/core/src/type/float4_e2m1.cpp
@@ -85,10 +85,10 @@ uint8_t f32_to_f4e2m1_bits(const float value) {
// Restore the hidden 1 in target mantissa for subnormal calculation
fractional = f_m_hidden_one_mask | (input & f32_m_mask) << (f32_e_size - f4e2m1_e_size - 4U);
// Will any bits be shifted off?
- int32_t shift = target_f_biased_exp < -(f4e2m1_e_max) ? 0 : (1U << (1 - target_f_biased_exp));
+ int32_t shift = (1U << (1 - target_f_biased_exp));
uint32_t sticky = (fractional & (shift - 1)) ? 1 : 0;
- fractional = ((1 + target_f_biased_exp) > f4e2m1_e_max - 1) ? 0 : fractional >> (1 - target_f_biased_exp);
+ fractional = fractional >> (1 - target_f_biased_exp);
fractional |= sticky;
// apply rounding
if (((fractional & round_half) == round_odd) || ((fractional & round_norm) != 0)) {
diff --git a/src/plugins/template/tests/functional/op_reference/cum_sum.cpp b/src/plugins/template/tests/functional/op_reference/cum_sum.cpp
index 251a47bc93e..c0f5bc29820 100644
--- a/src/plugins/template/tests/functional/op_reference/cum_sum.cpp
+++ b/src/plugins/template/tests/functional/op_reference/cum_sum.cpp
@@ -162,7 +162,7 @@ std::vector<CumSumParams> generateCumSumParams(const element::Type& type) {
false,
true,
element::i64,
- int64_t(0),
+ int64_t(-1),
Shape{}),
CumSumParams(Shape{6},
type,
@@ -190,7 +190,7 @@ std::vector<CumSumParams> generateCumSumParams(const element::Type& type) {
false,
false,
element::i32,
- int32_t(0),
+ int32_t(-2),
Shape{}),
CumSumParams(Shape{2, 4},
type,
@@ -274,7 +274,7 @@ std::vector<CumSumParams> generateCumSumParams(const element::Type& type) {
false,
false,
element::i32,
- int32_t(1),
+ int32_t(-2),
Shape{}),
CumSumParams(
Shape{3, 2, 4},