Skip to content

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

[CPU] Replace union usage with bit_cast where applicable (#31057)

Arseniy Obolenskiyee32de07af5b
diff --git a/src/plugins/intel_cpu/src/nodes/common/fp16_utils.h b/src/plugins/intel_cpu/src/nodes/common/fp16_utils.h
index 371d86da9a7..f212a6f306d 100644
--- a/src/plugins/intel_cpu/src/nodes/common/fp16_utils.h
+++ b/src/plugins/intel_cpu/src/nodes/common/fp16_utils.h
@@ -6,6 +6,8 @@
#include <cstdint>
+#include "utils/cpp/bit_cast.hpp"
+
namespace ov {
namespace intel_cpu {
@@ -17,18 +19,6 @@ typedef short ie_fp16;
#define EXP_MASK_F32 0x7F800000U
#define EXP_MASK_F16 0x7C00U
-// small helper function to represent uint32_t value as float32
-inline float asfloat(uint32_t v) {
- // Both type-punning casts and unions are UB per C++ spec
- // But compilers usually only break code with casts
- union {
- float f;
- uint32_t i;
- } u;
- u.i = v;
- return u.f;
-}
-
// Function to convert F32 into F16
inline float f16tof32(ie_fp16 x) {
// this is storage for output result
@@ -82,7 +72,7 @@ inline float f16tof32(ie_fp16 x) {
}
// finaly represent result as float and return
- return asfloat(u);
+ return ov::intel_cpu::bit_cast<float>(u);
}
} // namespace intel_cpu
diff --git a/src/plugins/intel_cpu/src/nodes/eye.cpp b/src/plugins/intel_cpu/src/nodes/eye.cpp
index b0055e86ba5..d693e7720b8 100644
--- a/src/plugins/intel_cpu/src/nodes/eye.cpp
+++ b/src/plugins/intel_cpu/src/nodes/eye.cpp
@@ -132,7 +132,7 @@ void Eye::executeSpecified() {
size_t start = 0;
size_t end = 0;
splitter(elementsCount, nthr, ithr, start, end);
- memset(dst + start, 0, (end - start) * sizeof(T));
+ std::fill(dst + start, dst + end, static_cast<T>(0));
});
if (onesPerBatchNum == 0) {
return;
@@ -152,7 +152,7 @@ void Eye::executeSpecified() {
size_t start = 0;
size_t end = 0;
splitter(batchVolume, nthr, ithr, start, end);
- memset(dst + start * spatialCount, 0, (end - start) * spatialSize);
+ std::fill(dst + start * spatialCount, dst + end * spatialCount, static_cast<T>(0));
if (onesPerBatchNum == 0) {
return;
}
diff --git a/src/plugins/intel_cpu/src/nodes/kernels/x64/random_uniform.cpp b/src/plugins/intel_cpu/src/nodes/kernels/x64/random_uniform.cpp
index fc7a7bae80e..31cc2f846c0 100644
--- a/src/plugins/intel_cpu/src/nodes/kernels/x64/random_uniform.cpp
+++ b/src/plugins/intel_cpu/src/nodes/kernels/x64/random_uniform.cpp
@@ -16,6 +16,7 @@
#include "nodes/kernels/x64/registers_pool.hpp"
#include "openvino/core/except.hpp"
#include "openvino/core/type/element_type.hpp"
+#include "utils/cpp/bit_cast.hpp"
using namespace dnnl::impl::cpu;
@@ -42,11 +43,6 @@ namespace ov::intel_cpu::kernel::random_uniform {
mov(R, reinterpret_cast<uintptr_t>(A##_aligned)); \
}
-union FloatAsBits {
- float f;
- uint32_t u;
-};
-
////////////// PHILOX GENERATOR /////////////////////////
template <x64::cpu_isa_t isa>
@@ -756,26 +752,23 @@ void MersenneTwisterGenerator<x64::avx512_core>::initVectors() {
// Initialize constants based on the requested data type
if (m_jcp.out_data_type == element::f32) {
- FloatAsBits val{};
- val.f = 1.0F / (1 << 24);
- BROADCAST_CONSTANT(vpbroadcastd, v_divisor, r32_aux, val.u);
+ auto val = ov::intel_cpu::bit_cast<uint32_t>(1.0F / (1 << 24));
+ BROADCAST_CONSTANT(vpbroadcastd, v_divisor, r32_aux, val);
BROADCAST_CONSTANT(vpbroadcastd, v_mask, r32_aux, static_cast<uint32_t>((1 << 24) - 1))
BROADCAST_PARAM(vpbroadcastd, v_range, r64_aux, GET_MERSENNE_OFFSET(range_ptr))
BROADCAST_PARAM(vpbroadcastd, v_min, r64_aux, GET_MERSENNE_OFFSET(min_ptr))
} else if (m_jcp.out_data_type == element::f16 && x64::mayiuse(x64::avx512_core_fp16)) {
- FloatAsBits val{};
- val.f = 1.0F / (1 << 11);
- BROADCAST_CONSTANT(vpbroadcastd, v_divisor, r32_aux, val.u);
+ auto val = ov::intel_cpu::bit_cast<uint32_t>(1.0F / (1 << 11));
+ BROADCAST_CONSTANT(vpbroadcastd, v_divisor, r32_aux, val);
BROADCAST_CONSTANT(vpbroadcastd, v_mask, r32_aux, static_cast<uint32_t>((1 << 11) - 1))
// Note: two times too many values in Zmm
BROADCAST_PARAM(vpbroadcastw, v_range, r64_aux, GET_MERSENNE_OFFSET(range_ptr))
BROADCAST_PARAM(vpbroadcastw, v_min, r64_aux, GET_MERSENNE_OFFSET(min_ptr))
} else if (m_jcp.out_data_type == element::bf16 && x64::mayiuse(x64::avx512_core_bf16)) {
- FloatAsBits val{};
- val.f = 1.0F / (1 << 8);
- BROADCAST_CONSTANT(vpbroadcastd, v_divisor, r32_aux, val.u);
+ auto val = ov::intel_cpu::bit_cast<uint32_t>(1.0F / (1 << 8));
+ BROADCAST_CONSTANT(vpbroadcastd, v_divisor, r32_aux, val);
BROADCAST_CONSTANT(vpbroadcastd, v_mask, r32_aux, static_cast<uint32_t>((1 << 8) - 1))
// Note: two times too many values in Zmm
diff --git a/src/plugins/intel_cpu/src/nodes/psroi_pooling.cpp b/src/plugins/intel_cpu/src/nodes/psroi_pooling.cpp
index f391473515c..f74beb20189 100644
--- a/src/plugins/intel_cpu/src/nodes/psroi_pooling.cpp
+++ b/src/plugins/intel_cpu/src/nodes/psroi_pooling.cpp
@@ -633,7 +633,7 @@ void PSROIPooling::executeSpecified() {
}
});
- memset(dstData + realRois * nc * nh * nw, 0, (nn - realRois) * nc * nh * nw * sizeof(outputType));
+ std::fill(dstData + realRois * nc * nh * nw, dstData + nn * nc * nh * nw, static_cast<outputType>(0));
}
namespace {
diff --git a/src/plugins/intel_cpu/src/nodes/random_uniform.cpp b/src/plugins/intel_cpu/src/nodes/random_uniform.cpp
index ff05a982baf..c9a1393ccad 100644
--- a/src/plugins/intel_cpu/src/nodes/random_uniform.cpp
+++ b/src/plugins/intel_cpu/src/nodes/random_uniform.cpp
@@ -37,6 +37,7 @@
#include "openvino/op/random_uniform.hpp"
#include "openvino/op/util/attr_types.hpp"
#include "shape_inference/shape_inference_cpu.hpp"
+#include "utils/cpp/bit_cast.hpp"
#include "utils/general_utils.h"
namespace ov::intel_cpu::node {
@@ -503,21 +504,19 @@ inline void runPhilox(uint64_t key, uint64_t counter, uint64_t n, uint32_t* res)
}
inline void convertToOutputTypePhilox(const uint32_t* in, float min, float range, float* out, size_t el_to_copy) {
- RandomUniform::OutputType out_val{};
-
for (size_t i = 0LU; i < el_to_copy; i++) {
- out_val.u32 = 0x3f800000 | (in[i] & 0x7fffffU);
- out[i] = (out_val.f32 - 1.F) * range + min;
+ uint32_t bits = 0x3f800000 | (in[i] & 0x7fffffU);
+ float f = ov::intel_cpu::bit_cast<float>(bits);
+ out[i] = (f - 1.F) * range + min;
}
}
inline void convertToOutputTypePhilox(const uint32_t* in, float16 min, float16 range, float16* out, size_t el_to_copy) {
- RandomUniform::OutputType out_val{};
-
for (size_t i = 0LU; i < el_to_copy; i++) {
auto x_uint16 = static_cast<uint16_t>(in[i]);
- out_val.u16 = 0x3c00 | (x_uint16 & 0x03ffU);
- out[i] = (out_val.f16 - static_cast<float16>(1)) * range + min;
+ uint16_t bits = 0x3c00 | (x_uint16 & 0x03ffU);
+ float16 f = ov::intel_cpu::bit_cast<float16>(bits);
+ out[i] = (f - static_cast<float16>(1)) * range + min;
}
}
@@ -526,12 +525,11 @@ inline void convertToOutputTypePhilox(const uint32_t* in,
bfloat16 range,
bfloat16* out,
size_t el_to_copy) {
- RandomUniform::OutputType out_val{};
-
for (size_t i = 0LU; i < el_to_copy; i++) {
auto x_uint16 = static_cast<uint16_t>(in[i]);
- out_val.u16 = 0x3f80 | (x_uint16 & 0x7fU);
- out[i] = (out_val.bf16 - static_cast<bfloat16>(1)) * range + min;
+ uint16_t bits = 0x3f80 | (x_uint16 & 0x7fU);
+ bfloat16 f = ov::intel_cpu::bit_cast<bfloat16>(bits);
+ out[i] = (f - static_cast<bfloat16>(1)) * range + min;
}
}
diff --git a/src/plugins/intel_cpu/src/nodes/region_yolo.cpp b/src/plugins/intel_cpu/src/nodes/region_yolo.cpp
index 1c9e8031a58..e7c36ac0f92 100644
--- a/src/plugins/intel_cpu/src/nodes/region_yolo.cpp
+++ b/src/plugins/intel_cpu/src/nodes/region_yolo.cpp
@@ -37,6 +37,7 @@
#include "openvino/op/region_yolo.hpp"
#include "shape_inference/shape_inference_cpu.hpp"
#include "utils/bfloat16.hpp"
+#include "utils/cpp/bit_cast.hpp"
#include "utils/general_utils.h"
using namespace dnnl::impl;
@@ -367,9 +368,7 @@ void RegionYolo::createPrimitive() {
}
inline float RegionYolo::logistic_scalar(float src) {
- U aux2{};
- aux2.as_float_value = src;
- int sign = aux2.as_int_value >> 31;
+ int sign = ov::intel_cpu::bit_cast<int>(src) >> 31;
if (sign == 0) {
src *= -1;
}
diff --git a/src/plugins/intel_cpu/src/nodes/region_yolo.h b/src/plugins/intel_cpu/src/nodes/region_yolo.h
index e35e0be4c5b..ea10ee2fc11 100644
--- a/src/plugins/intel_cpu/src/nodes/region_yolo.h
+++ b/src/plugins/intel_cpu/src/nodes/region_yolo.h
@@ -76,11 +76,6 @@ private:
std::shared_ptr<jit_uni_logistic_kernel> logistic_kernel = nullptr;
std::shared_ptr<SoftmaxGeneric> softmax_kernel;
- union U {
- float as_float_value;
- int as_int_value;
- };
-
static inline float logistic_scalar(float src);
inline void calculate_logistic(size_t start_index, int count, uint8_t* dst_data);
};
diff --git a/src/plugins/intel_cpu/src/utils/bfloat16.hpp b/src/plugins/intel_cpu/src/utils/bfloat16.hpp
index 3626da44462..a2dc7806946 100644
--- a/src/plugins/intel_cpu/src/utils/bfloat16.hpp
+++ b/src/plugins/intel_cpu/src/utils/bfloat16.hpp
@@ -8,6 +8,8 @@
#include <cstdint>
#include <limits>
+#include "utils/cpp/bit_cast.hpp"
+
/**
* The bfloat16_t class can be used as an arithmetic type. All arithmetic operations goes through conversion to the
* float data type.
@@ -35,7 +37,8 @@ public:
{}
operator float() const {
- return F32{static_cast<uint32_t>(m_value) << 16}.vfloat;
+ auto bits = static_cast<uint32_t>(m_value) << 16;
+ return ov::intel_cpu::bit_cast<float>(bits);
}
static constexpr bfloat16_t from_bits(uint16_t bits) {
return {bits, true};
@@ -45,27 +48,23 @@ public:
}
static uint16_t round_to_nearest_even(float x) {
- return static_cast<uint16_t>((F32(x).vint + ((F32(x).vint & 0x00010000) >> 1)) >> 16);
+ uint32_t bits = ov::intel_cpu::bit_cast<uint32_t>(x);
+ return static_cast<uint16_t>((bits + ((bits & 0x00010000u) >> 1)) >> 16);
}
static uint16_t round_to_nearest(float x) {
- return static_cast<uint16_t>((F32(x).vint + 0x8000) >> 16);
+ uint32_t bits = ov::intel_cpu::bit_cast<uint32_t>(x);
+ return static_cast<uint16_t>((bits + 0x8000u) >> 16);
}
static uint16_t truncate(float x) {
- return static_cast<uint16_t>((F32(x).vint) >> 16);
+ uint32_t bits = ov::intel_cpu::bit_cast<uint32_t>(x);
+ return static_cast<uint16_t>(bits >> 16);
}
private:
constexpr bfloat16_t(uint16_t x, [[maybe_unused]] bool flag) : m_value{x} {}
- union alignas(16) F32 {
- F32(float val) : vfloat{val} {}
-
- F32(uint32_t val) : vint{val} {}
- float vfloat;
- uint32_t vint;
- };
- uint16_t m_value;
+ uint16_t m_value{};
};
} // namespace ov::intel_cpu
diff --git a/src/plugins/intel_cpu/src/utils/cpp/bit_cast.hpp b/src/plugins/intel_cpu/src/utils/cpp/bit_cast.hpp
new file mode 100644
index 00000000000..99c5063c8fe
--- /dev/null
+++ b/src/plugins/intel_cpu/src/utils/cpp/bit_cast.hpp
@@ -0,0 +1,37 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <cstring>
+#include <type_traits>
+#if defined(OPENVINO_CPP_VER_AT_LEAST_20)
+# include <bit>
+#endif
+
+namespace ov::intel_cpu {
+
+#if defined(OPENVINO_CPP_VER_AT_LEAST_20)
+using std::bit_cast;
+#else
+template <typename To, typename From>
+inline std::enable_if_t<sizeof(To) == sizeof(From) && std::is_trivially_copyable_v<From> &&
+ std::is_trivially_copyable_v<To>,
+ To>
+bit_cast(const From& src) noexcept {
+ static_assert(std::is_trivially_constructible_v<To>, "Destination type must be trivially constructible");
+ To dst{};
+# if defined(__GNUC__) && !defined(__clang__)
+# pragma GCC diagnostic push
+# pragma GCC diagnostic ignored "-Wclass-memaccess"
+# endif
+ std::memcpy(&dst, &src, sizeof(To));
+# if defined(__GNUC__) && !defined(__clang__)
+# pragma GCC diagnostic pop
+# endif
+ return dst;
+}
+#endif
+
+} // namespace ov::intel_cpu