Skip to content

Commit

Permalink
fix: avoid warnings on MSVC
Browse files Browse the repository at this point in the history
Warnings in header files can be a problem for consumers that enable
`/WX` (or `-Werror`).  In this case, using `... & -align` produces a
warning (C4146) with MSVC. The fix is to use equivalent expression
`...  & ~(align - 1)`, which was already used in the same file.
  • Loading branch information
coryan committed May 6, 2023
1 parent da2c4a6 commit 835f3b4
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/google/protobuf/arena_align.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ struct ArenaAlignDefault {
}

static inline PROTOBUF_ALWAYS_INLINE constexpr size_t Ceil(size_t n) {
return (n + align - 1) & -align;
return (n + align - 1) & ~(align - 1);
}
static inline PROTOBUF_ALWAYS_INLINE constexpr size_t Floor(size_t n) {
return (n & ~(align - 1));
Expand All @@ -113,7 +113,7 @@ struct ArenaAlignDefault {
template <typename T>
static inline PROTOBUF_ALWAYS_INLINE T* Ceil(T* ptr) {
uintptr_t intptr = reinterpret_cast<uintptr_t>(ptr);
return reinterpret_cast<T*>((intptr + align - 1) & -align);
return reinterpret_cast<T*>((intptr + align - 1) & ~(align - 1));
}

template <typename T>
Expand Down Expand Up @@ -142,7 +142,7 @@ struct ArenaAlign {
return (reinterpret_cast<uintptr_t>(ptr) & (align - 1)) == 0U;
}

constexpr size_t Ceil(size_t n) const { return (n + align - 1) & -align; }
constexpr size_t Ceil(size_t n) const { return (n + align - 1) & ~(align - 1); }
constexpr size_t Floor(size_t n) const { return (n & ~(align - 1)); }

constexpr size_t Padded(size_t n) const {
Expand All @@ -156,7 +156,7 @@ struct ArenaAlign {
template <typename T>
T* Ceil(T* ptr) const {
uintptr_t intptr = reinterpret_cast<uintptr_t>(ptr);
return reinterpret_cast<T*>((intptr + align - 1) & -align);
return reinterpret_cast<T*>((intptr + align - 1) & ~(align - 1));
}

template <typename T>
Expand Down

0 comments on commit 835f3b4

Please sign in to comment.