Adopt package access modifier and get rid of gybbing

This commit is contained in:
Karoy Lorentey committed 2025-09-29 11:56:14 -07:00
1 parent 7b847a3b70
commit bb8d566833
42 files changed
+261 -5325

No files matched your search

@@ -2,29 +2,20 @@
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors
// Copyright (c) 2022 - 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
%{
from gyb_utils import *
}%
${autogenerated_warning()}
% for modifier in visibility_levels:
${visibility_boilerplate(modifier)}
extension FixedWidthInteger {
/// Round up `self` to the nearest power of two, assuming it's representable.
/// Returns 0 if `self` isn't positive.
@inlinable
${modifier} func _roundUpToPowerOfTwo() -> Self {
package func _roundUpToPowerOfTwo() -> Self {
guard self > 0 else { return 0 }
let l = Self.bitWidth - (self &- 1).leadingZeroBitCount
return 1 << l
}
}
% end
${visibility_boilerplate("end")}
@@ -9,13 +9,6 @@
//
//===----------------------------------------------------------------------===//
%{
from gyb_utils import *
}%
${autogenerated_warning()}
% for modifier in visibility_levels:
${visibility_boilerplate(modifier)}
extension FixedWidthInteger {
@inlinable @inline(__always)
internal var _nonzeroBitCount: Self {
@@ -23,7 +16,7 @@ extension FixedWidthInteger {
}
@inlinable @inline(__always)
${modifier} func _rank(ofBit bit: UInt) -> Int {
package func _rank(ofBit bit: UInt) -> Int {
assert(bit < Self.bitWidth)
let mask: Self = (1 &<< bit) &- 1
return (self & mask).nonzeroBitCount
@@ -32,8 +25,8 @@ extension FixedWidthInteger {
extension UInt {
@_effects(releasenone)
${"@usableFromInline" if modifier != "public" else "//@usableFromInline" }
${modifier} func _bit(ranked n: Int) -> UInt? {
@usableFromInline
package func _bit(ranked n: Int) -> UInt? {
// FIXME: Use bit deposit instruction when available (PDEP on Intel).
assert(UInt.bitWidth == 64 || UInt.bitWidth == 32,
"Unsupported UInt bitWidth")
@@ -85,8 +78,8 @@ extension UInt32 {
// Returns the position of the `n`th set bit in `self`, i.e., the bit with
// rank `n`.
@_effects(releasenone)
${"@usableFromInline" if modifier != "public" else "//@usableFromInline" }
${modifier} func _bit(ranked n: Int) -> UInt? {
@usableFromInline
package func _bit(ranked n: Int) -> UInt? {
// FIXME: Use bit deposit instruction when available (PDEP on Intel).
assert(n >= 0 && n < Self.bitWidth)
var shift: Self = 0
@@ -125,8 +118,8 @@ extension UInt16 {
// Returns the position of the `n`th set bit in `self`, i.e., the bit with
// rank `n`.
@_effects(releasenone)
${"@usableFromInline" if modifier != "public" else "//@usableFromInline" }
${modifier} func _bit(ranked n: Int) -> UInt? {
@usableFromInline
package func _bit(ranked n: Int) -> UInt? {
// FIXME: Use bit deposit instruction when available (PDEP on Intel).
assert(n >= 0 && n < Self.bitWidth)
var shift: Self = 0
@@ -155,5 +148,3 @@ extension UInt16 {
return UInt(truncatingIfNeeded: shift)
}
}
% end
${visibility_boilerplate("end")}
@@ -2,33 +2,24 @@
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors
// Copyright (c) 2022 - 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
%{
from gyb_utils import *
}%
${autogenerated_warning()}
% for modifier in visibility_levels:
${visibility_boilerplate(modifier)}
extension UInt {
@inlinable @inline(__always)
${modifier} var _firstSetBit: UInt? {
package var _firstSetBit: UInt? {
guard self != 0 else { return nil }
let v = UInt.bitWidth &- 1 &- self.leadingZeroBitCount
return UInt(truncatingIfNeeded: v)
}
@inlinable @inline(__always)
${modifier} var _lastSetBit: UInt? {
package var _lastSetBit: UInt? {
guard self != 0 else { return nil }
return UInt(truncatingIfNeeded: self.trailingZeroBitCount)
}
}
% end
${visibility_boilerplate("end")}
@@ -2,23 +2,16 @@
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors
// Copyright (c) 2022 - 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
%{
from gyb_utils import *
}%
${autogenerated_warning()}
% for modifier in visibility_levels:
${visibility_boilerplate(modifier)}
extension UInt {
@inlinable
${modifier} var _reversed: UInt {
package var _reversed: UInt {
// https://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel
var shift: UInt = UInt(UInt.bitWidth)
var mask: UInt = ~0;
@@ -32,5 +25,3 @@ extension UInt {
return result
}
}
% end
${visibility_boilerplate("end")}
@@ -1,46 +0,0 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
// #############################################################################
// # #
// # DO NOT EDIT THIS FILE; IT IS AUTOGENERATED. #
// # #
// #############################################################################
// In single module mode, we need these declarations to be internal,
// but in regular builds we want them to be public. Unfortunately
// the current best way to do this is to duplicate all definitions.
#if COLLECTIONS_SINGLE_MODULE
extension FixedWidthInteger {
/// Round up `self` to the nearest power of two, assuming it's representable.
/// Returns 0 if `self` isn't positive.
@inlinable
internal func _roundUpToPowerOfTwo() -> Self {
guard self > 0 else { return 0 }
let l = Self.bitWidth - (self &- 1).leadingZeroBitCount
return 1 << l
}
}
#else // !COLLECTIONS_SINGLE_MODULE
extension FixedWidthInteger {
/// Round up `self` to the nearest power of two, assuming it's representable.
/// Returns 0 if `self` isn't positive.
@inlinable
public func _roundUpToPowerOfTwo() -> Self {
guard self > 0 else { return 0 }
let l = Self.bitWidth - (self &- 1).leadingZeroBitCount
return 1 << l
}
}
#endif // COLLECTIONS_SINGLE_MODULE
@@ -1,304 +0,0 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
// #############################################################################
// # #
// # DO NOT EDIT THIS FILE; IT IS AUTOGENERATED. #
// # #
// #############################################################################
// In single module mode, we need these declarations to be internal,
// but in regular builds we want them to be public. Unfortunately
// the current best way to do this is to duplicate all definitions.
#if COLLECTIONS_SINGLE_MODULE
extension FixedWidthInteger {
@inlinable @inline(__always)
internal var _nonzeroBitCount: Self {
Self(truncatingIfNeeded: nonzeroBitCount)
}
@inlinable @inline(__always)
internal func _rank(ofBit bit: UInt) -> Int {
assert(bit < Self.bitWidth)
let mask: Self = (1 &<< bit) &- 1
return (self & mask).nonzeroBitCount
}
}
extension UInt {
@_effects(releasenone)
@usableFromInline
internal func _bit(ranked n: Int) -> UInt? {
// FIXME: Use bit deposit instruction when available (PDEP on Intel).
assert(UInt.bitWidth == 64 || UInt.bitWidth == 32,
"Unsupported UInt bitWidth")
var shift: Self = 0
var n = Self(truncatingIfNeeded: n)
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
n &-= c16
}
let c8 = ((self &>> shift) & 0xFF)._nonzeroBitCount
if n >= c8 {
shift &+= 8
n &-= c8
}
let c4 = ((self &>> shift) & 0xF)._nonzeroBitCount
if n >= c4 {
shift &+= 4
n &-= c4
}
let c2 = ((self &>> shift) & 0x3)._nonzeroBitCount
if n >= c2 {
shift &+= 2
n &-= c2
}
let c1 = (self &>> shift) & 0x1
if n >= c1 {
shift &+= 1
n &-= c1
}
guard n == 0 && (self &>> shift) & 0x1 == 1 else { return nil }
return shift
}
}
extension UInt32 {
// Returns the position of the `n`th set bit in `self`, i.e., the bit with
// rank `n`.
@_effects(releasenone)
@usableFromInline
internal func _bit(ranked n: Int) -> UInt? {
// FIXME: Use bit deposit instruction when available (PDEP on Intel).
assert(n >= 0 && n < Self.bitWidth)
var shift: Self = 0
var n = Self(truncatingIfNeeded: n)
let c16 = (self & 0xFFFF)._nonzeroBitCount
if n >= c16 {
shift = 16
n -= c16
}
let c8 = ((self &>> shift) & 0xFF)._nonzeroBitCount
if n >= c8 {
shift &+= 8
n -= c8
}
let c4 = ((self &>> shift) & 0xF)._nonzeroBitCount
if n >= c4 {
shift &+= 4
n -= c4
}
let c2 = ((self &>> shift) & 0x3)._nonzeroBitCount
if n >= c2 {
shift &+= 2
n -= c2
}
let c1 = (self &>> shift) & 0x1
if n >= c1 {
shift &+= 1
n -= c1
}
guard n == 0, (self &>> shift) & 0x1 == 1 else { return nil }
return UInt(truncatingIfNeeded: shift)
}
}
extension UInt16 {
// Returns the position of the `n`th set bit in `self`, i.e., the bit with
// rank `n`.
@_effects(releasenone)
@usableFromInline
internal func _bit(ranked n: Int) -> UInt? {
// FIXME: Use bit deposit instruction when available (PDEP on Intel).
assert(n >= 0 && n < Self.bitWidth)
var shift: Self = 0
var n = Self(truncatingIfNeeded: n)
let c8 = ((self &>> shift) & 0xFF)._nonzeroBitCount
if n >= c8 {
shift &+= 8
n -= c8
}
let c4 = ((self &>> shift) & 0xF)._nonzeroBitCount
if n >= c4 {
shift &+= 4
n -= c4
}
let c2 = ((self &>> shift) & 0x3)._nonzeroBitCount
if n >= c2 {
shift &+= 2
n -= c2
}
let c1 = (self &>> shift) & 0x1
if n >= c1 {
shift &+= 1
n -= c1
}
guard n == 0, (self &>> shift) & 0x1 == 1 else { return nil }
return UInt(truncatingIfNeeded: shift)
}
}
#else // !COLLECTIONS_SINGLE_MODULE
extension FixedWidthInteger {
@inlinable @inline(__always)
internal var _nonzeroBitCount: Self {
Self(truncatingIfNeeded: nonzeroBitCount)
}
@inlinable @inline(__always)
public func _rank(ofBit bit: UInt) -> Int {
assert(bit < Self.bitWidth)
let mask: Self = (1 &<< bit) &- 1
return (self & mask).nonzeroBitCount
}
}
extension UInt {
@_effects(releasenone)
//@usableFromInline
public func _bit(ranked n: Int) -> UInt? {
// FIXME: Use bit deposit instruction when available (PDEP on Intel).
assert(UInt.bitWidth == 64 || UInt.bitWidth == 32,
"Unsupported UInt bitWidth")
var shift: Self = 0
var n = Self(truncatingIfNeeded: n)
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
n &-= c16
}
let c8 = ((self &>> shift) & 0xFF)._nonzeroBitCount
if n >= c8 {
shift &+= 8
n &-= c8
}
let c4 = ((self &>> shift) & 0xF)._nonzeroBitCount
if n >= c4 {
shift &+= 4
n &-= c4
}
let c2 = ((self &>> shift) & 0x3)._nonzeroBitCount
if n >= c2 {
shift &+= 2
n &-= c2
}
let c1 = (self &>> shift) & 0x1
if n >= c1 {
shift &+= 1
n &-= c1
}
guard n == 0 && (self &>> shift) & 0x1 == 1 else { return nil }
return shift
}
}
extension UInt32 {
// Returns the position of the `n`th set bit in `self`, i.e., the bit with
// rank `n`.
@_effects(releasenone)
//@usableFromInline
public func _bit(ranked n: Int) -> UInt? {
// FIXME: Use bit deposit instruction when available (PDEP on Intel).
assert(n >= 0 && n < Self.bitWidth)
var shift: Self = 0
var n = Self(truncatingIfNeeded: n)
let c16 = (self & 0xFFFF)._nonzeroBitCount
if n >= c16 {
shift = 16
n -= c16
}
let c8 = ((self &>> shift) & 0xFF)._nonzeroBitCount
if n >= c8 {
shift &+= 8
n -= c8
}
let c4 = ((self &>> shift) & 0xF)._nonzeroBitCount
if n >= c4 {
shift &+= 4
n -= c4
}
let c2 = ((self &>> shift) & 0x3)._nonzeroBitCount
if n >= c2 {
shift &+= 2
n -= c2
}
let c1 = (self &>> shift) & 0x1
if n >= c1 {
shift &+= 1
n -= c1
}
guard n == 0, (self &>> shift) & 0x1 == 1 else { return nil }
return UInt(truncatingIfNeeded: shift)
}
}
extension UInt16 {
// Returns the position of the `n`th set bit in `self`, i.e., the bit with
// rank `n`.
@_effects(releasenone)
//@usableFromInline
public func _bit(ranked n: Int) -> UInt? {
// FIXME: Use bit deposit instruction when available (PDEP on Intel).
assert(n >= 0 && n < Self.bitWidth)
var shift: Self = 0
var n = Self(truncatingIfNeeded: n)
let c8 = ((self &>> shift) & 0xFF)._nonzeroBitCount
if n >= c8 {
shift &+= 8
n -= c8
}
let c4 = ((self &>> shift) & 0xF)._nonzeroBitCount
if n >= c4 {
shift &+= 4
n -= c4
}
let c2 = ((self &>> shift) & 0x3)._nonzeroBitCount
if n >= c2 {
shift &+= 2
n -= c2
}
let c1 = (self &>> shift) & 0x1
if n >= c1 {
shift &+= 1
n -= c1
}
guard n == 0, (self &>> shift) & 0x1 == 1 else { return nil }
return UInt(truncatingIfNeeded: shift)
}
}
#endif // COLLECTIONS_SINGLE_MODULE
@@ -1,54 +0,0 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
// #############################################################################
// # #
// # DO NOT EDIT THIS FILE; IT IS AUTOGENERATED. #
// # #
// #############################################################################
// In single module mode, we need these declarations to be internal,
// but in regular builds we want them to be public. Unfortunately
// the current best way to do this is to duplicate all definitions.
#if COLLECTIONS_SINGLE_MODULE
extension UInt {
@inlinable @inline(__always)
internal var _firstSetBit: UInt? {
guard self != 0 else { return nil }
let v = UInt.bitWidth &- 1 &- self.leadingZeroBitCount
return UInt(truncatingIfNeeded: v)
}
@inlinable @inline(__always)
internal var _lastSetBit: UInt? {
guard self != 0 else { return nil }
return UInt(truncatingIfNeeded: self.trailingZeroBitCount)
}
}
#else // !COLLECTIONS_SINGLE_MODULE
extension UInt {
@inlinable @inline(__always)
public var _firstSetBit: UInt? {
guard self != 0 else { return nil }
let v = UInt.bitWidth &- 1 &- self.leadingZeroBitCount
return UInt(truncatingIfNeeded: v)
}
@inlinable @inline(__always)
public var _lastSetBit: UInt? {
guard self != 0 else { return nil }
return UInt(truncatingIfNeeded: self.trailingZeroBitCount)
}
}
#endif // COLLECTIONS_SINGLE_MODULE
@@ -1,58 +0,0 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
// #############################################################################
// # #
// # DO NOT EDIT THIS FILE; IT IS AUTOGENERATED. #
// # #
// #############################################################################
// In single module mode, we need these declarations to be internal,
// but in regular builds we want them to be public. Unfortunately
// the current best way to do this is to duplicate all definitions.
#if COLLECTIONS_SINGLE_MODULE
extension UInt {
@inlinable
internal var _reversed: UInt {
// https://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel
var shift: UInt = UInt(UInt.bitWidth)
var mask: UInt = ~0;
var result = self
while true {
shift &>>= 1
guard shift > 0 else { break }
mask ^= mask &<< shift
result = ((result &>> shift) & mask) | ((result &<< shift) & ~mask)
}
return result
}
}
#else // !COLLECTIONS_SINGLE_MODULE
extension UInt {
@inlinable
public var _reversed: UInt {
// https://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel
var shift: UInt = UInt(UInt.bitWidth)
var mask: UInt = ~0;
var result = self
while true {
shift &>>= 1
guard shift > 0 else { break }
mask ^= mask &<< shift
result = ((result &>> shift) & mask) | ((result &<< shift) & ~mask)
}
return result
}
}
#endif // COLLECTIONS_SINGLE_MODULE