Skip to content

[stdlib] operators on pointer types to static funcs #6100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 6, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 51 additions & 52 deletions stdlib/public/core/UnsafePointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -486,66 +486,65 @@ extension ${Self} : CustomPlaygroundQuickLookable {
}
}

// - Note: Strideable's implementation is potentially less efficient and cannot
// handle misaligned pointers.
@_transparent
public func == <Pointee>(
lhs: ${Self}<Pointee>, rhs: ${Self}<Pointee>
) -> Bool {
return Bool(Builtin.cmp_eq_RawPointer(lhs._rawValue, rhs._rawValue))
}
extension ${Self} {
// - Note: Strideable's implementation is potentially less efficient and cannot
// handle misaligned pointers.
@_transparent
public static func == (lhs: ${Self}<Pointee>, rhs: ${Self}<Pointee>) -> Bool {
return Bool(Builtin.cmp_eq_RawPointer(lhs._rawValue, rhs._rawValue))
}

// - Note: Strideable's implementation is potentially less efficient and cannot
// handle misaligned pointers.
//
// - Note: This is an unsigned comparison unlike Strideable's implementation.
@_transparent
public func < <Pointee>(lhs: ${Self}<Pointee>, rhs: ${Self}<Pointee>) -> Bool {
return Bool(Builtin.cmp_ult_RawPointer(lhs._rawValue, rhs._rawValue))
}
// - Note: Strideable's implementation is potentially less efficient and
// cannot handle misaligned pointers.
//
// - Note: This is an unsigned comparison unlike Strideable's implementation.
@_transparent
public static func < (lhs: ${Self}<Pointee>, rhs: ${Self}<Pointee>) -> Bool {
return Bool(Builtin.cmp_ult_RawPointer(lhs._rawValue, rhs._rawValue))
}

// - Note: The following family of operator overloads are redundant
// with Strideable. However, optimizer improvements are needed
// before they can be removed without affecting performance.
// - Note: The following family of operator overloads are redundant
// with Strideable. However, optimizer improvements are needed
// before they can be removed without affecting performance.

/// - Precondition: The result is within bounds of the same allocation.
@_transparent
public func + <Pointee>(lhs: ${Self}<Pointee>, rhs: Int) -> ${Self}<Pointee> {
return ${Self}(Builtin.gep_Word(
lhs._rawValue, rhs._builtinWordValue, Pointee.self))
}
/// - Precondition: The result is within bounds of the same allocation.
@_transparent
public static func + (lhs: ${Self}<Pointee>, rhs: Int) -> ${Self}<Pointee> {
return ${Self}(Builtin.gep_Word(
lhs._rawValue, rhs._builtinWordValue, Pointee.self))
}

/// - Precondition: The result is within the bounds of the same allocation.
@_transparent
public func + <Pointee>(lhs: Int,
rhs: ${Self}<Pointee>) -> ${Self}<Pointee> {
return rhs + lhs
}
/// - Precondition: The result is within the bounds of the same allocation.
@_transparent
public static func + (lhs: Int, rhs: ${Self}<Pointee>) -> ${Self}<Pointee> {
return rhs + lhs
}

/// - Precondition: The result is within the bounds of the same allocation.
@_transparent
public func - <Pointee>(lhs: ${Self}<Pointee>, rhs: Int) -> ${Self}<Pointee> {
return lhs + -rhs
}
/// - Precondition: The result is within the bounds of the same allocation.
@_transparent
public static func - (lhs: ${Self}<Pointee>, rhs: Int) -> ${Self}<Pointee> {
return lhs + -rhs
}

@_transparent
public func - <Pointee>(lhs: ${Self}<Pointee>, rhs: ${Self}<Pointee>) -> Int {
return
Int(Builtin.sub_Word(Builtin.ptrtoint_Word(lhs._rawValue),
Builtin.ptrtoint_Word(rhs._rawValue)))
/ MemoryLayout<Pointee>.stride
}
@_transparent
public static func - (lhs: ${Self}<Pointee>, rhs: ${Self}<Pointee>) -> Int {
return
Int(Builtin.sub_Word(Builtin.ptrtoint_Word(lhs._rawValue),
Builtin.ptrtoint_Word(rhs._rawValue)))
/ MemoryLayout<Pointee>.stride
}

/// - Precondition: The result is within the bounds of the same allocation.
@_transparent
public func += <Pointee>(lhs: inout ${Self}<Pointee>, rhs: Int) {
lhs = lhs + rhs
}
/// - Precondition: The result is within the bounds of the same allocation.
@_transparent
public static func += (lhs: inout ${Self}<Pointee>, rhs: Int) {
lhs = lhs + rhs
}

/// - Precondition: The result is within the bounds of the same allocation.
@_transparent
public func -= <Pointee>(lhs: inout ${Self}<Pointee>, rhs: Int) {
lhs = lhs - rhs
/// - Precondition: The result is within the bounds of the same allocation.
@_transparent
public static func -= (lhs: inout ${Self}<Pointee>, rhs: Int) {
lhs = lhs - rhs
}
}

extension ${Self} {
Expand Down