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

Commit a79708e

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 d2d2cac commit a79708e

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
@@ -193,9 +193,15 @@ typedef basic_ostream<char> ostream;
193193
Vector data;
194194

195195
__HOST_DEVICE__
196-
operator T() const noexcept { return data[idx]; }
196+
constexpr operator const T&() const noexcept {
197+
return reinterpret_cast<
198+
const T (&)[sizeof(Vector) / sizeof(T)]>(data)[idx];
199+
}
197200
__HOST_DEVICE__
198-
operator T() const volatile noexcept { return data[idx]; }
201+
constexpr operator const volatile T&() const volatile noexcept {
202+
return reinterpret_cast<
203+
const volatile T (&)[sizeof(Vector) / sizeof(T)]>(data)[idx];
204+
}
199205

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

0 commit comments

Comments
 (0)