[CollectionUtilities] Silence a warning on 32 bit platforms

It turns out `UInt.bitWidth` is getting too eagerly inlined, so conditions based on it trigger spurious “code will never be executed” warnings. Silence those by switching to looking at `MemoryLayout<UInt>`, which doesn’t get turned into a constant *that* early.
This commit is contained in:
Karoy Lorentey committed 2023-05-18 18:32:41 -07:00
1 parent 992e0f3446
commit 0c002200bb
2 files changed
+12 -3

No files matched your search

@@ -41,13 +41,16 @@ extension UInt {
var shift: Self = 0
var n = Self(truncatingIfNeeded: n)
if Self.bitWidth == 64 {
if MemoryLayout<UInt>.size == 8 {
let c32 = (self & 0xFFFFFFFF)._nonzeroBitCount
if n >= c32 {
shift &+= 32
n &-= c32
}
} else {
assert(MemoryLayout<Self>.size == 4, "Unknown platform")
}
let c16 = ((self &>> shift) & 0xFFFF)._nonzeroBitCount
if n >= c16 {
shift &+= 16
@@ -47,13 +47,16 @@ extension UInt {
var shift: Self = 0
var n = Self(truncatingIfNeeded: n)
if Self.bitWidth == 64 {
if MemoryLayout<UInt>.size == 8 {
let c32 = (self & 0xFFFFFFFF)._nonzeroBitCount
if n >= c32 {
shift &+= 32
n &-= c32
}
} else {
assert(MemoryLayout<Self>.size == 4, "Unknown platform")
}
let c16 = ((self &>> shift) & 0xFFFF)._nonzeroBitCount
if n >= c16 {
shift &+= 16
@@ -184,13 +187,16 @@ extension UInt {
var shift: Self = 0
var n = Self(truncatingIfNeeded: n)
if Self.bitWidth == 64 {
if MemoryLayout<UInt>.size == 8 {
let c32 = (self & 0xFFFFFFFF)._nonzeroBitCount
if n >= c32 {
shift &+= 32
n &-= c32
}
} else {
assert(MemoryLayout<Self>.size == 4, "Unknown platform")
}
let c16 = ((self &>> shift) & 0xFFFF)._nonzeroBitCount
if n >= c16 {
shift &+= 16