Skip to content

Commit 4d0e89e

Browse files
[SYCL][NFCI] Fix -Wconversion in half implementation (#19469)
This is a patch in a series of fixing warnings emitted by `-Wconversion` flag which is necessary according to our SDL processes.
1 parent fd89ae4 commit 4d0e89e

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

sycl/include/sycl/half_type.hpp

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ inline __SYCL_CONSTEXPR_HALF uint16_t float2Half(const float &Val) {
7070
Exp16 = 0x1f;
7171
} else if (__builtin_expect(Exp32Diff > -14, 0)) {
7272
// normal range for half type
73-
Exp16 = Exp32Diff + 15;
73+
Exp16 = static_cast<uint16_t>(Exp32Diff) + 15;
7474
// convert 23-bit mantissa to 10-bit mantissa.
75-
Frac16 = Frac32 >> 13;
75+
Frac16 = static_cast<uint16_t>(Frac32 >> 13);
7676
// Round the mantissa as given in OpenCL spec section : 6.1.1.1 The half
7777
// data type.
7878
// Round to nearest.
@@ -85,7 +85,8 @@ inline __SYCL_CONSTEXPR_HALF uint16_t float2Half(const float &Val) {
8585
Frac16 += Frac16 & 1;
8686
} else if (__builtin_expect(Exp32Diff > -25, 0)) {
8787
// subnormals
88-
Frac16 = (Frac32 | (uint32_t(1) << 23)) >> (-Exp32Diff - 1);
88+
Frac16 = static_cast<uint16_t>((Frac32 | (uint32_t(1) << 23)) >>
89+
(-Exp32Diff - 1));
8990
}
9091

9192
if (__builtin_expect(Exp32 == 0xff && Frac32 != 0, 0)) {
@@ -133,7 +134,7 @@ inline __SYCL_CONSTEXPR_HALF float half2Float(const uint16_t &Val) {
133134
Exp32 = 113 - OffSet;
134135
}
135136

136-
uint32_t Frac32 = Frac16 << 13;
137+
uint32_t Frac32 = static_cast<uint32_t>(Frac16 << 13);
137138

138139
uint32_t Bits = 0;
139140
Bits |= Sign;
@@ -312,72 +313,72 @@ class [[__sycl_detail__::__uses_aspects__(aspect::fp16)]] half {
312313
__SYCL_CONSTEXPR_HALF friend half operator op(const half lhs, \
313314
const int rhs) { \
314315
half rtn = lhs; \
315-
rtn op_eq rhs; \
316+
rtn op_eq half(static_cast<float>(rhs)); \
316317
return rtn; \
317318
} \
318319
__SYCL_CONSTEXPR_HALF friend half operator op(const int lhs, \
319320
const half rhs) { \
320-
half rtn = lhs; \
321+
half rtn(static_cast<float>(lhs)); \
321322
rtn op_eq rhs; \
322323
return rtn; \
323324
} \
324325
__SYCL_CONSTEXPR_HALF friend half operator op(const half lhs, \
325326
const long rhs) { \
326327
half rtn = lhs; \
327-
rtn op_eq rhs; \
328+
rtn op_eq half(static_cast<float>(rhs)); \
328329
return rtn; \
329330
} \
330331
__SYCL_CONSTEXPR_HALF friend half operator op(const long lhs, \
331332
const half rhs) { \
332-
half rtn = lhs; \
333+
half rtn(static_cast<float>(lhs)); \
333334
rtn op_eq rhs; \
334335
return rtn; \
335336
} \
336337
__SYCL_CONSTEXPR_HALF friend half operator op(const half lhs, \
337338
const long long rhs) { \
338339
half rtn = lhs; \
339-
rtn op_eq rhs; \
340+
rtn op_eq half(static_cast<float>(rhs)); \
340341
return rtn; \
341342
} \
342343
__SYCL_CONSTEXPR_HALF friend half operator op(const long long lhs, \
343344
const half rhs) { \
344-
half rtn = lhs; \
345+
half rtn(static_cast<float>(lhs)); \
345346
rtn op_eq rhs; \
346347
return rtn; \
347348
} \
348349
__SYCL_CONSTEXPR_HALF friend half operator op(const half &lhs, \
349350
const unsigned int &rhs) { \
350351
half rtn = lhs; \
351-
rtn op_eq rhs; \
352+
rtn op_eq half(static_cast<float>(rhs)); \
352353
return rtn; \
353354
} \
354355
__SYCL_CONSTEXPR_HALF friend half operator op(const unsigned int &lhs, \
355356
const half &rhs) { \
356-
half rtn = lhs; \
357+
half rtn(static_cast<float>(lhs)); \
357358
rtn op_eq rhs; \
358359
return rtn; \
359360
} \
360361
__SYCL_CONSTEXPR_HALF friend half operator op(const half &lhs, \
361362
const unsigned long &rhs) { \
362363
half rtn = lhs; \
363-
rtn op_eq rhs; \
364+
rtn op_eq half(static_cast<float>(rhs)); \
364365
return rtn; \
365366
} \
366367
__SYCL_CONSTEXPR_HALF friend half operator op(const unsigned long &lhs, \
367368
const half &rhs) { \
368-
half rtn = lhs; \
369+
half rtn(static_cast<float>(lhs)); \
369370
rtn op_eq rhs; \
370371
return rtn; \
371372
} \
372373
__SYCL_CONSTEXPR_HALF friend half operator op( \
373374
const half &lhs, const unsigned long long &rhs) { \
374375
half rtn = lhs; \
375-
rtn op_eq rhs; \
376+
rtn op_eq half(static_cast<float>(rhs)); \
376377
return rtn; \
377378
} \
378379
__SYCL_CONSTEXPR_HALF friend half operator op(const unsigned long long &lhs, \
379380
const half &rhs) { \
380-
half rtn = lhs; \
381+
half rtn(static_cast<float>(lhs)); \
381382
rtn op_eq rhs; \
382383
return rtn; \
383384
}
@@ -412,51 +413,51 @@ class [[__sycl_detail__::__uses_aspects__(aspect::fp16)]] half {
412413
} \
413414
__SYCL_CONSTEXPR_HALF friend bool operator op(const half &lhs, \
414415
const int &rhs) { \
415-
return lhs.getFPRep() op rhs; \
416+
return lhs.getFPRep() op static_cast<float>(rhs); \
416417
} \
417418
__SYCL_CONSTEXPR_HALF friend bool operator op(const int &lhs, \
418419
const half &rhs) { \
419-
return lhs op rhs.getFPRep(); \
420+
return static_cast<float>(lhs) op rhs.getFPRep(); \
420421
} \
421422
__SYCL_CONSTEXPR_HALF friend bool operator op(const half &lhs, \
422423
const long &rhs) { \
423-
return lhs.getFPRep() op rhs; \
424+
return lhs.getFPRep() op static_cast<float>(rhs); \
424425
} \
425426
__SYCL_CONSTEXPR_HALF friend bool operator op(const long &lhs, \
426427
const half &rhs) { \
427-
return lhs op rhs.getFPRep(); \
428+
return static_cast<float>(lhs) op rhs.getFPRep(); \
428429
} \
429430
__SYCL_CONSTEXPR_HALF friend bool operator op(const half &lhs, \
430431
const long long &rhs) { \
431-
return lhs.getFPRep() op rhs; \
432+
return lhs.getFPRep() op static_cast<float>(rhs); \
432433
} \
433434
__SYCL_CONSTEXPR_HALF friend bool operator op(const long long &lhs, \
434435
const half &rhs) { \
435-
return lhs op rhs.getFPRep(); \
436+
return static_cast<float>(lhs) op rhs.getFPRep(); \
436437
} \
437438
__SYCL_CONSTEXPR_HALF friend bool operator op(const half &lhs, \
438439
const unsigned int &rhs) { \
439-
return lhs.getFPRep() op rhs; \
440+
return lhs.getFPRep() op static_cast<float>(rhs); \
440441
} \
441442
__SYCL_CONSTEXPR_HALF friend bool operator op(const unsigned int &lhs, \
442443
const half &rhs) { \
443-
return lhs op rhs.getFPRep(); \
444+
return static_cast<float>(lhs) op rhs.getFPRep(); \
444445
} \
445446
__SYCL_CONSTEXPR_HALF friend bool operator op(const half &lhs, \
446447
const unsigned long &rhs) { \
447-
return lhs.getFPRep() op rhs; \
448+
return lhs.getFPRep() op static_cast<float>(rhs); \
448449
} \
449450
__SYCL_CONSTEXPR_HALF friend bool operator op(const unsigned long &lhs, \
450451
const half &rhs) { \
451-
return lhs op rhs.getFPRep(); \
452+
return static_cast<float>(lhs) op rhs.getFPRep(); \
452453
} \
453454
__SYCL_CONSTEXPR_HALF friend bool operator op( \
454455
const half &lhs, const unsigned long long &rhs) { \
455-
return lhs.getFPRep() op rhs; \
456+
return lhs.getFPRep() op static_cast<float>(rhs); \
456457
} \
457458
__SYCL_CONSTEXPR_HALF friend bool operator op(const unsigned long long &lhs, \
458459
const half &rhs) { \
459-
return lhs op rhs.getFPRep(); \
460+
return static_cast<float>(lhs) op rhs.getFPRep(); \
460461
}
461462
OP(==)
462463
OP(!=)

0 commit comments

Comments
 (0)