Skip to content

Commit 392221c

Browse files
committed
Fix Alignment.log2
Guard against 0 and correct 64-bit log2 for 32-bit input
1 parent b9e9c92 commit 392221c

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

Sources/LLVM/Units.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@ public struct Alignment: Comparable, Hashable {
2727
///
2828
/// An n-byte alignment contains log-base-two-many least-significant zeros.
2929
public func log2() -> UInt32 {
30+
guard !isZero else { return 0 }
3031
return 31 - UInt32(self.rawValue.leadingZeroBitCount)
3132
}
3233

3334
/// Returns the log-base-two value of this alignment as a 64-bit integer.
3435
///
3536
/// An n-byte alignment contains log-base-two-many least-significant zeros.
3637
public func log2() -> UInt64 {
37-
return 63 - UInt64(self.rawValue.leadingZeroBitCount)
38+
guard !isZero else { return 0 }
39+
// rawValue is only 32 bits
40+
return 31 - UInt64(self.rawValue.leadingZeroBitCount)
3841
}
3942

4043
/// Returns the alignment of a pointer which points to the given number of

0 commit comments

Comments
 (0)