Skip to content

Commit 2f73e13

Browse files
authored
SWDEV-525933 - add constexpr operators for fp16/bf16 (#199)
1 parent a320a3f commit 2f73e13

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

hipamd/include/hip/amd_detail/amd_hip_bf16.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@
123123
#if defined(__HIPCC_RTC__)
124124
#define __BF16_HOST_DEVICE__ __BF16_DEVICE__
125125
#else
126-
#include <algorithm>
127126
#include <climits>
128127
#include <cmath>
129128
#define __BF16_HOST_DEVICE__ __host__ __BF16_DEVICE__
@@ -215,7 +214,7 @@ struct __attribute__((aligned(2))) __hip_bfloat16 {
215214
__BF16_HOST_DEVICE__ __hip_bfloat16(const float val) : __x_bf16(static_cast<__bf16>(val)) {}
216215

217216
/*! \brief create __hip_bfloat16 from a __hip_bfloat16_raw */
218-
__BF16_HOST_DEVICE__ __hip_bfloat16(const __hip_bfloat16_raw& val) : __x(val.x) {}
217+
__BF16_HOST_DEVICE__ constexpr __hip_bfloat16(const __hip_bfloat16_raw& val) : __x(val.x) {}
219218

220219
/*! \brief create __hip_bfloat16 from __bf16 */
221220
__BF16_HOST_DEVICE__ __hip_bfloat16(const __bf16 val) : __x_bf16(val) {}
@@ -232,7 +231,7 @@ struct __attribute__((aligned(2))) __hip_bfloat16 {
232231
}
233232

234233
/*! \brief return false if bfloat value is +0.0 or -0.0, returns true otherwise */
235-
__BF16_HOST_DEVICE__ operator bool() const { return __x_bf16 != 0.0f; }
234+
__BF16_HOST_DEVICE__ constexpr operator bool() const { return __x_bf16 != 0.0f; }
236235

237236
/*! \brief return a casted char from underlying float val */
238237
__BF16_HOST_DEVICE__ operator char() const { return static_cast<char>(__x_bf16); }
@@ -370,7 +369,7 @@ struct __attribute__((aligned(4))) __hip_bfloat162 {
370369
__BF16_HOST_DEVICE__ __hip_bfloat162(const __hip_bfloat162& val) : x(val.x), y(val.y) {}
371370

372371
/*! \brief create __hip_bfloat162 from two __hip_bfloat16 */
373-
__BF16_HOST_DEVICE__ __hip_bfloat162(const __hip_bfloat16& a, const __hip_bfloat16& b)
372+
__BF16_HOST_DEVICE__ constexpr __hip_bfloat162(const __hip_bfloat16& a, const __hip_bfloat16& b)
374373
: x(a), y(b) {}
375374

376375
/*! \brief create __hip_bfloat162 from vector of __bf16_2 */

hipamd/include/hip/amd_detail/amd_hip_fp16.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ THE SOFTWARE.
9999
// CREATORS
100100
__HOST_DEVICE__
101101
__half() = default;
102-
__HOST_DEVICE__
102+
__HOST_DEVICE__ constexpr
103103
__half(const __half_raw& x) : data{x.data} {}
104104
#if !defined(__HIP_NO_HALF_CONVERSIONS__)
105105
__HOST_DEVICE__
@@ -363,12 +363,9 @@ THE SOFTWARE.
363363
__half2(const __half2_raw& xx) : data{xx.data} {}
364364
__HOST_DEVICE__
365365
__half2(decltype(data) xx) : data{xx} {}
366-
__HOST_DEVICE__
366+
__HOST_DEVICE__ constexpr
367367
__half2(const __half& xx, const __half& yy)
368-
:
369-
data{static_cast<__half_raw>(xx).data,
370-
static_cast<__half_raw>(yy).data}
371-
{}
368+
: x(xx), y(yy) {}
372369
__HOST_DEVICE__
373370
__half2(const __half2&) = default;
374371
__HOST_DEVICE__

0 commit comments

Comments
 (0)