Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/src/__support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ add_header_library(
libc.src.__support.CPP.type_traits
libc.src.__support.macros.attributes
libc.src.__support.macros.config
libc.src.__support.macros.properties.compiler
)

add_header_library(
Expand Down
2 changes: 2 additions & 0 deletions libc/src/__support/CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ add_header_library(
libc.hdr.stdint_proxy
libc.src.__support.macros.attributes
libc.src.__support.macros.sanitizer
libc.src.__support.macros.properties.compiler
)

add_header_library(
Expand Down Expand Up @@ -209,6 +210,7 @@ add_object_library(
libc.hdr.func.malloc
libc.hdr.func.aligned_alloc
libc.src.__support.common
libc.src.__support.macros.properties.compiler
libc.src.__support.macros.properties.os
)

Expand Down
3 changes: 2 additions & 1 deletion libc/src/__support/CPP/bit.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "src/__support/CPP/type_traits.h"
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/compiler.h"
#include "src/__support/macros/sanitizer.h"

namespace LIBC_NAMESPACE_DECL {
Expand All @@ -36,7 +37,7 @@ LIBC_INLINE constexpr cpp::enable_if_t<
To>
bit_cast(const From &from) {
MSAN_UNPOISON(&from, sizeof(From));
#if __has_builtin(__builtin_bit_cast)
#if __has_builtin(__builtin_bit_cast) || defined(LIBC_COMPILER_IS_MSVC)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we not just

#if defined(LIBC_COMPILER_IS_MSVC)
#define __has_builtin(x) 0
#endif

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already define __has_builtin(x) 0 for MSVC https://github.com/llvm/llvm-project/blob/main/libc/src/__support/macros/config.h#L26

The problem here is that MSVC does have __builtin_bit_cast.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What a pain

return __builtin_bit_cast(To, from);
#else
To to{};
Expand Down
5 changes: 5 additions & 0 deletions libc/src/__support/CPP/new.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "hdr/func/malloc.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/compiler.h"
#include "src/__support/macros/properties/os.h"

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

#ifndef LIBC_COMPILER_IS_MSVC
#define DELETE_NAME(name) \
__asm__(LIBC_MACRO_TO_STRING(LIBC_NAMESPACE) "_" LIBC_MACRO_TO_STRING(name))
#else
#define DELETE_NAME(name)
#endif // LIBC_COMPILER_IS_MSVC

void operator delete(void *) noexcept DELETE_NAME(delete);
void operator delete(void *, std::align_val_t) noexcept
Expand Down
26 changes: 13 additions & 13 deletions libc/src/__support/big_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ LIBC_INLINE constexpr DoubleWide<word> mul2(word a, word b) {
#endif
else {
using half_word = half_width_t<word>;
const auto shiftl = [](word value) -> word {
constexpr auto shiftl = [](word value) -> word {
return value << cpp::numeric_limits<half_word>::digits;
};
const auto shiftr = [](word value) -> word {
constexpr auto shiftr = [](word value) -> word {
return value >> cpp::numeric_limits<half_word>::digits;
};
// Here we do a one digit multiplication where 'a' and 'b' are of type
Expand All @@ -111,19 +111,19 @@ LIBC_INLINE constexpr DoubleWide<word> mul2(word a, word b) {
// c result
// We convert 'lo' and 'hi' from 'half_word' to 'word' so multiplication
// doesn't overflow.
const word a_lo = lo(a);
const word b_lo = lo(b);
const word a_hi = hi(a);
const word b_hi = hi(b);
const word step1 = b_lo * a_lo; // no overflow;
const word step2 = b_lo * a_hi; // no overflow;
const word step3 = b_hi * a_lo; // no overflow;
const word step4 = b_hi * a_hi; // no overflow;
word a_lo = lo(a);
word b_lo = lo(b);
word a_hi = hi(a);
word b_hi = hi(b);
word step1 = b_lo * a_lo; // no overflow;
word step2 = b_lo * a_hi; // no overflow;
word step3 = b_hi * a_lo; // no overflow;
word step4 = b_hi * a_hi; // no overflow;
word lo_digit = step1;
word hi_digit = step4;
const word no_carry = 0;
word carry;
word _; // unused carry variable.
word no_carry = 0;
word carry = 0;
[[maybe_unused]] word _ = 0; // unused carry variable.
lo_digit = add_with_carry<word>(lo_digit, shiftl(step2), no_carry, carry);
hi_digit = add_with_carry<word>(hi_digit, shiftr(step2), carry, _);
lo_digit = add_with_carry<word>(lo_digit, shiftl(step3), no_carry, carry);
Expand Down
11 changes: 11 additions & 0 deletions libc/src/__support/math_extras.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "src/__support/CPP/type_traits.h" // is_unsigned_v, is_constant_evaluated
#include "src/__support/macros/attributes.h" // LIBC_INLINE
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/compiler.h"

namespace LIBC_NAMESPACE_DECL {

Expand All @@ -25,7 +26,17 @@ LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T>
mask_trailing_ones() {
constexpr unsigned T_BITS = CHAR_BIT * sizeof(T);
static_assert(count <= T_BITS && "Invalid bit index");
#ifndef LIBC_COMPILER_IS_MSVC
return count == 0 ? 0 : (T(-1) >> (T_BITS - count));
#else
// MSVC complains about out of range shifts.
if constexpr (count == 0)
return 0;
else if constexpr (count >= T_BITS)
return T(-1);
else
return T(-1) >> (T_BITS - count);
#endif // !LIBC_COMPILER_IS_MSVC
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we need to keep the non-MSVC version? It seems like the new version should work on other platforms as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess not? Probably just remove the static_assert and keep the new version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm, we can actually keep the static assert.

}

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