From e19ee2b50e5bb55fc2a16a7b6bf3131181302b04 Mon Sep 17 00:00:00 2001 From: Sergey Fedorov Date: Wed, 29 Jul 2026 07:40:59 +0000 Subject: [PATCH 03/13] folly/DiscriminatedPtr: support 32-bit architectures (ppc32) The tag was stored in bits 48-63 of a uintptr_t. On 32-bit targets uintptr_t is 32 bits wide, so the shifts were out of range and the tag was lost; the header refused 32-bit builds via #error, which broke building the test suite on Darwin/ppc32. Store data_ as uint64_t instead: the pointer still occupies the low bits (all 32 of them on 32-bit targets) and the tag bits 48-63, so the class now works unchanged on 32-bit. On 64-bit targets uintptr_t == uint64_t, so this is a no-op. Allow FOLLY_PPC in the arch check. Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_013G8CDpEyDCCgV4jaMM9WME --- folly/DiscriminatedPtr.h | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/folly/DiscriminatedPtr.h b/folly/DiscriminatedPtr.h index b2125f5a5..8e99e4ff1 100644 --- a/folly/DiscriminatedPtr.h +++ b/folly/DiscriminatedPtr.h @@ -33,8 +33,9 @@ #include #include -#if !FOLLY_X64 && !FOLLY_AARCH64 && !FOLLY_PPC64 && !FOLLY_RISCV64 -#error "DiscriminatedPtr is x64, arm64, ppc64 and riscv64 specific code." +#if !FOLLY_X64 && !FOLLY_AARCH64 && !FOLLY_PPC && !FOLLY_PPC64 && \ + !FOLLY_RISCV64 +#error "DiscriminatedPtr is x64, arm64, ppc, ppc64 and riscv64 specific code." #endif namespace folly { @@ -201,13 +202,14 @@ class DiscriminatedPtr { return uint16_t(idx + 1); } void* ptr() const { - return reinterpret_cast(data_ & ((1ULL << 48) - 1)); + return reinterpret_cast( + static_cast(data_ & ((1ULL << 48) - 1))); } void set(void* p, uint16_t v) { - uintptr_t ip = reinterpret_cast(p); + uint64_t ip = reinterpret_cast(p); CHECK(!(ip >> 48)); - ip |= static_cast(v) << 48; + ip |= static_cast(v) << 48; data_ = ip; } @@ -215,9 +217,11 @@ class DiscriminatedPtr { * We store a pointer in the least significant 48 bits of data_, and a type * index (0 = empty, or 1-based index in Types) in the most significant 16 * bits. We rely on the fact that pointers have their most significant 16 - * bits clear on x86_64. + * bits clear on x86_64. data_ is a uint64_t rather than uintptr_t so that + * the same layout works on 32-bit architectures, where pointers occupy only + * the low 32 bits. */ - uintptr_t data_; + uint64_t data_; }; template -- 2.43.0