Skip to content

Commit 7cc5989

Browse files
authored
[libc] Some MSVC compatibility fixes in src/__support. (#159428)
1 parent 5f105fe commit 7cc5989

File tree

5 files changed

+29
-15
lines changed

5 files changed

+29
-15
lines changed

libc/src/__support/CPP/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ add_header_library(
2020
libc.hdr.stdint_proxy
2121
libc.src.__support.macros.attributes
2222
libc.src.__support.macros.sanitizer
23+
libc.src.__support.macros.properties.compiler
2324
)
2425

2526
add_header_library(
@@ -209,6 +210,7 @@ add_object_library(
209210
libc.hdr.func.malloc
210211
libc.hdr.func.aligned_alloc
211212
libc.src.__support.common
213+
libc.src.__support.macros.properties.compiler
212214
libc.src.__support.macros.properties.os
213215
)
214216

libc/src/__support/CPP/bit.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "src/__support/CPP/type_traits.h"
1717
#include "src/__support/macros/attributes.h"
1818
#include "src/__support/macros/config.h"
19+
#include "src/__support/macros/properties/compiler.h"
1920
#include "src/__support/macros/sanitizer.h"
2021

2122
namespace LIBC_NAMESPACE_DECL {
@@ -36,7 +37,7 @@ LIBC_INLINE constexpr cpp::enable_if_t<
3637
To>
3738
bit_cast(const From &from) {
3839
MSAN_UNPOISON(&from, sizeof(From));
39-
#if __has_builtin(__builtin_bit_cast)
40+
#if __has_builtin(__builtin_bit_cast) || defined(LIBC_COMPILER_IS_MSVC)
4041
return __builtin_bit_cast(To, from);
4142
#else
4243
To to{};

libc/src/__support/CPP/new.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "hdr/func/malloc.h"
1515
#include "src/__support/common.h"
1616
#include "src/__support/macros/config.h"
17+
#include "src/__support/macros/properties/compiler.h"
1718
#include "src/__support/macros/properties/os.h"
1819

1920
#include <stddef.h> // For size_t
@@ -109,8 +110,12 @@ LIBC_INLINE void *operator new[](size_t, void *p) { return p; }
109110
// header file in all libc source files where operator delete is called ensures
110111
// that only libc call sites use these replacement operator delete functions.
111112

113+
#ifndef LIBC_COMPILER_IS_MSVC
112114
#define DELETE_NAME(name) \
113115
__asm__(LIBC_MACRO_TO_STRING(LIBC_NAMESPACE) "_" LIBC_MACRO_TO_STRING(name))
116+
#else
117+
#define DELETE_NAME(name)
118+
#endif // LIBC_COMPILER_IS_MSVC
114119

115120
void operator delete(void *) noexcept DELETE_NAME(delete);
116121
void operator delete(void *, std::align_val_t) noexcept

libc/src/__support/big_int.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ LIBC_INLINE constexpr DoubleWide<word> mul2(word a, word b) {
9595
#endif
9696
else {
9797
using half_word = half_width_t<word>;
98-
const auto shiftl = [](word value) -> word {
98+
constexpr auto shiftl = [](word value) -> word {
9999
return value << cpp::numeric_limits<half_word>::digits;
100100
};
101-
const auto shiftr = [](word value) -> word {
101+
constexpr auto shiftr = [](word value) -> word {
102102
return value >> cpp::numeric_limits<half_word>::digits;
103103
};
104104
// Here we do a one digit multiplication where 'a' and 'b' are of type
@@ -111,19 +111,19 @@ LIBC_INLINE constexpr DoubleWide<word> mul2(word a, word b) {
111111
// c result
112112
// We convert 'lo' and 'hi' from 'half_word' to 'word' so multiplication
113113
// doesn't overflow.
114-
const word a_lo = lo(a);
115-
const word b_lo = lo(b);
116-
const word a_hi = hi(a);
117-
const word b_hi = hi(b);
118-
const word step1 = b_lo * a_lo; // no overflow;
119-
const word step2 = b_lo * a_hi; // no overflow;
120-
const word step3 = b_hi * a_lo; // no overflow;
121-
const word step4 = b_hi * a_hi; // no overflow;
114+
word a_lo = lo(a);
115+
word b_lo = lo(b);
116+
word a_hi = hi(a);
117+
word b_hi = hi(b);
118+
word step1 = b_lo * a_lo; // no overflow;
119+
word step2 = b_lo * a_hi; // no overflow;
120+
word step3 = b_hi * a_lo; // no overflow;
121+
word step4 = b_hi * a_hi; // no overflow;
122122
word lo_digit = step1;
123123
word hi_digit = step4;
124-
const word no_carry = 0;
125-
word carry;
126-
word _; // unused carry variable.
124+
word no_carry = 0;
125+
word carry = 0;
126+
[[maybe_unused]] word _ = 0; // unused carry variable.
127127
lo_digit = add_with_carry<word>(lo_digit, shiftl(step2), no_carry, carry);
128128
hi_digit = add_with_carry<word>(hi_digit, shiftr(step2), carry, _);
129129
lo_digit = add_with_carry<word>(lo_digit, shiftl(step3), no_carry, carry);

libc/src/__support/math_extras.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T>
2525
mask_trailing_ones() {
2626
constexpr unsigned T_BITS = CHAR_BIT * sizeof(T);
2727
static_assert(count <= T_BITS && "Invalid bit index");
28-
return count == 0 ? 0 : (T(-1) >> (T_BITS - count));
28+
// MSVC complains about out of range shifts.
29+
if constexpr (count == 0)
30+
return 0;
31+
else if constexpr (count >= T_BITS)
32+
return T(-1);
33+
else
34+
return T(-1) >> (T_BITS - count);
2935
}
3036

3137
// Create a bitmask with the count left-most bits set to 1, and all other bits

0 commit comments

Comments
 (0)