Skip to content
This repository was archived by the owner on Jan 26, 2024. It is now read-only.

Commit a99abc6

Browse files
committed
Vector component const conversion operators should return references
This is necessary for consistency between the representation of vector types between host and device, as well as for consistency with the way vector types behave when defined as structures (as is the case for code ported from CUDA). Without this patch, this simple test case: ```` const float& pick(float4 const& v) { return v.x; } const volatile float& pick(float4 const volatile& v) { return v.x; } int main() { const float4 c = make_float4(0, 1, 2, 3); volatile float4 v = make_float4(1, 2, 3, 0); std::cout << pick(c) << std::endl; std::cout << pick(v) << std::endl; } ```` will fail to compile when built with the host compiler. Closes hipamd issue #3
1 parent 3ec1ccd commit a99abc6

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

include/hip/amd_detail/amd_hip_vector_types.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,15 @@ template <typename __T> struct is_scalar : public integral_constant<bool, __is_s
209209
Vector data;
210210

211211
__HOST_DEVICE__
212-
operator T() const noexcept { return data[idx]; }
212+
constexpr operator const T&() const noexcept {
213+
return reinterpret_cast<
214+
const T (&)[sizeof(Vector) / sizeof(T)]>(data)[idx];
215+
}
213216
__HOST_DEVICE__
214-
operator T() const volatile noexcept { return data[idx]; }
217+
constexpr operator const volatile T&() const volatile noexcept {
218+
return reinterpret_cast<
219+
const volatile T (&)[sizeof(Vector) / sizeof(T)]>(data)[idx];
220+
}
215221

216222
#ifdef __HIP_ENABLE_VECTOR_SCALAR_ACCESSORY_ENUM_CONVERSION__
217223
// The conversions to enum are fairly ghastly, but unfortunately used in

0 commit comments

Comments
 (0)