Skip to content

Commit a96a964

Browse files
authored
Merge #1903 Fix the strict alias issue
This PR fixes the strict alias issue by using memcpy Related PR: #1903
2 parents 2111234 + 289220b commit a96a964

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

core/base/extended_float.hpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
1+
// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
22
//
33
// SPDX-License-Identifier: BSD-3-Clause
44

55
#ifndef GKO_CORE_BASE_EXTENDED_FLOAT_HPP_
66
#define GKO_CORE_BASE_EXTENDED_FLOAT_HPP_
77

88

9+
#include <cstring>
910
#include <limits>
1011
#include <type_traits>
1112

@@ -91,16 +92,27 @@ class truncated {
9192

9293
GKO_ATTRIBUTES explicit truncated(const float_type& val) noexcept
9394
{
94-
const auto& bits = reinterpret_cast<const full_bits_type&>(val);
95+
// hip does not allow std::memcpy in __host__ __device__
96+
#ifndef HIP_VERSION
97+
using std::memcpy;
98+
#endif
99+
full_bits_type bits;
100+
memcpy(&bits, &val, sizeof(full_bits_type));
95101
data_ = static_cast<bits_type>((bits & component_mask) >>
96102
component_position);
97103
}
98104

99105
GKO_ATTRIBUTES operator float_type() const noexcept
100106
{
107+
// hip does not allow std::memcpy in __host__ __device__
108+
#ifndef HIP_VERSION
109+
using std::memcpy;
110+
#endif
101111
const auto bits = static_cast<full_bits_type>(data_)
102112
<< component_position;
103-
return reinterpret_cast<const float_type&>(bits);
113+
float_type ans;
114+
memcpy(&ans, &bits, sizeof(float_type));
115+
return ans;
104116
}
105117

106118
GKO_ATTRIBUTES truncated operator-() const noexcept

0 commit comments

Comments
 (0)