Skip to content

Commit 13b4127

Browse files
r-barnesfacebook-github-bot
authored andcommitted
Fix implicit conversion (pytorch#46833)
Summary: Pull Request resolved: pytorch#46833 Implicit integer conversions are causing compiler warnings. Since in this case the logs make it pretty clear that the `unsigned` types won't overflow despite 64-bit inputs, we fix the issue by making the downconversion explicit. Test Plan: Standard test rig. Reviewed By: malfet Differential Revision: D24481377 fbshipit-source-id: 4422538286d8ed2beb65065544016fd430394ff8
1 parent ecdbea7 commit 13b4127

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

c10/util/llvmMathExtras.h

+7-5
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
#define LLVM_SUPPORT_MATHEXTRAS_H
1515

1616
#include <algorithm>
17-
#include <cmath>
1817
#include <cassert>
1918
#include <climits>
19+
#include <cmath>
20+
#include <cstdint>
2021
#include <cstring>
2122
#include <limits>
2223
#include <type_traits>
@@ -547,26 +548,26 @@
547548
/// (32 bit edition.)
548549
/// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2
549550
inline unsigned Log2_32(uint32_t Value) {
550-
return 31 - countLeadingZeros(Value);
551+
return static_cast<unsigned>(31 - countLeadingZeros(Value));
551552
}
552553

553554
/// Return the floor log base 2 of the specified value, -1 if the value is zero.
554555
/// (64 bit edition.)
555556
inline unsigned Log2_64(uint64_t Value) {
556-
return 63 - countLeadingZeros(Value);
557+
return static_cast<unsigned>(63 - countLeadingZeros(Value));
557558
}
558559

559560
/// Return the ceil log base 2 of the specified value, 32 if the value is zero.
560561
/// (32 bit edition).
561562
/// Ex. Log2_32_Ceil(32) == 5, Log2_32_Ceil(1) == 0, Log2_32_Ceil(6) == 3
562563
inline unsigned Log2_32_Ceil(uint32_t Value) {
563-
return 32 - countLeadingZeros(Value - 1);
564+
return static_cast<unsigned>(32 - countLeadingZeros(Value - 1));
564565
}
565566

566567
/// Return the ceil log base 2 of the specified value, 64 if the value is zero.
567568
/// (64 bit edition.)
568569
inline unsigned Log2_64_Ceil(uint64_t Value) {
569-
return 64 - countLeadingZeros(Value - 1);
570+
return static_cast<unsigned>(64 - countLeadingZeros(Value - 1));
570571
}
571572

572573
/// Return the greatest common divisor of the values using Euclid's algorithm.
@@ -589,6 +590,7 @@
589590

590591
/// This function takes a 32-bit integer and returns the bit equivalent float.
591592
inline float BitsToFloat(uint32_t Bits) {
593+
//TODO: Use bit_cast once C++20 becomes available.
592594
float F;
593595
static_assert(sizeof(uint32_t) == sizeof(float), "Unexpected type sizes");
594596
memcpy(&F, &Bits, sizeof(Bits));

0 commit comments

Comments
 (0)