Skip to content

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

[Core/Ref] Reduce comparison instances of integral types for equal and not_equal ref impl (#30461)

Tomasz Jankowskifc514d10de7d
diff --git a/src/core/reference/include/openvino/reference/equal.hpp b/src/core/reference/include/openvino/reference/equal.hpp
index 38fc86003c6..cf8ded58a75 100644
--- a/src/core/reference/include/openvino/reference/equal.hpp
+++ b/src/core/reference/include/openvino/reference/equal.hpp
@@ -10,8 +10,7 @@
#include "openvino/reference/autobroadcast_binop.hpp"
#include "openvino/reference/utils/type_util.hpp"
-namespace ov {
-namespace reference {
+namespace ov::reference {
namespace func {
template <class T>
bool equal(const T lhs, const T rhs) {
@@ -27,30 +26,6 @@ void equal(const T* arg0, const T* arg1, char* out, size_t count) {
/**
* @brief Reference implementation of binary elementwise Equal operator.
*
- * Used for integral types with custom `equal` function (reduce binary size).
- *
- * @param arg0 Pointer to input 0 data.
- * @param arg1 Pointer to input 1 data.
- * @param out Pointer to output data.
- * @param arg_shape0 Input 0 shape.
- * @param arg_shape1 Input 1 shape.
- * @param broadcast_spec Broadcast specification mode.
- */
-template <typename T, typename U, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
-void equal(const T* arg0,
- const T* arg1,
- U* out,
- const Shape& arg0_shape,
- const Shape& arg1_shape,
- const op::AutoBroadcastSpec& broadcast_spec) {
- autobroadcast_binop(arg0, arg1, out, arg0_shape, arg1_shape, broadcast_spec, func::equal<T>);
-}
-
-/**
- * @brief Reference implementation of binary elementwise Equal operator.
- *
- * Used for floating-point types to (avoid warning compare floating point with `==`).
- *
* @param arg0 Pointer to input 0 data.
* @param arg1 Pointer to input 1 data.
* @param out Pointer to output data.
@@ -58,14 +33,20 @@ void equal(const T* arg0,
* @param arg_shape1 Input 1 shape.
* @param broadcast_spec Broadcast specification mode.
*/
-template <typename T, typename U, typename std::enable_if<ov::is_floating_point<T>()>::type* = nullptr>
+template <typename T, typename U>
void equal(const T* arg0,
const T* arg1,
U* out,
const Shape& arg0_shape,
const Shape& arg1_shape,
const op::AutoBroadcastSpec& broadcast_spec) {
- autobroadcast_binop(arg0, arg1, out, arg0_shape, arg1_shape, broadcast_spec, std::equal_to<T>());
+ if constexpr (std::is_integral_v<T>) {
+ using S = std::make_signed_t<T>;
+ const auto sig0 = reinterpret_cast<const S*>(arg0);
+ const auto sig1 = reinterpret_cast<const S*>(arg1);
+ autobroadcast_binop(sig0, sig1, out, arg0_shape, arg1_shape, broadcast_spec, func::equal<S>);
+ } else {
+ autobroadcast_binop(arg0, arg1, out, arg0_shape, arg1_shape, broadcast_spec, std::equal_to<T>());
+ }
}
-} // namespace reference
-} // namespace ov
+} // namespace ov::reference
diff --git a/src/core/reference/include/openvino/reference/not_equal.hpp b/src/core/reference/include/openvino/reference/not_equal.hpp
index 091d81a31ed..892024545a3 100644
--- a/src/core/reference/include/openvino/reference/not_equal.hpp
+++ b/src/core/reference/include/openvino/reference/not_equal.hpp
@@ -9,8 +9,7 @@
#include "openvino/reference/autobroadcast_binop.hpp"
-namespace ov {
-namespace reference {
+namespace ov::reference {
// Use custom implementation as function instead std::not_equal_to functor, gives smaller binary size.
// If removed or replace check impact on library binary size.
namespace func {
@@ -27,7 +26,13 @@ void not_equal(const T* arg0,
const Shape& arg0_shape,
const Shape& arg1_shape,
const op::AutoBroadcastSpec& broadcast_spec) {
- autobroadcast_binop(arg0, arg1, out, arg0_shape, arg1_shape, broadcast_spec, func::not_equal<T>);
+ if constexpr (std::is_integral_v<T>) {
+ using S = std::make_signed_t<T>;
+ const auto sig0 = reinterpret_cast<const S*>(arg0);
+ const auto sig1 = reinterpret_cast<const S*>(arg1);
+ autobroadcast_binop(sig0, sig1, out, arg0_shape, arg1_shape, broadcast_spec, func::not_equal<S>);
+ } else {
+ autobroadcast_binop(arg0, arg1, out, arg0_shape, arg1_shape, broadcast_spec, func::not_equal<T>);
+ }
}
-} // namespace reference
-} // namespace ov
+} // namespace ov::reference