blob: bba73b429203deb119af0beabf660c8ea351532f [file] [log] [blame]
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
@_fixed_layout
public struct CGFloat {
#if arch(i386) || arch(arm)
/// The native type used to store the CGFloat, which is Float on
/// 32-bit architectures and Double on 64-bit architectures.
public typealias NativeType = Float
#elseif arch(x86_64) || arch(arm64) || arch(s390x)
/// The native type used to store the CGFloat, which is Float on
/// 32-bit architectures and Double on 64-bit architectures.
public typealias NativeType = Double
#endif
@_transparent public init() {
self.native = 0.0
}
@_transparent public init(_ value: Float) {
self.native = NativeType(value)
}
@_transparent public init(_ value: Double) {
self.native = NativeType(value)
}
#if !os(Windows) && (arch(i386) || arch(x86_64))
@_transparent public init(_ value: Float80) {
self.native = NativeType(value)
}
#endif
@_transparent public init(_ value: CGFloat) {
self.native = value.native
}
/// Creates a new value, rounded to the closest possible representatation.
///
/// If two representable values are equally close, the result is the value
/// with more trailing zeros in its significand bit pattern.
///
/// - Parameter value: The integer to convert to a floating-point value.
public init(_ value: UInt8) {
self.native = NativeType(value)
}
/// Creates a new value, rounded to the closest possible representatation.
///
/// If two representable values are equally close, the result is the value
/// with more trailing zeros in its significand bit pattern.
///
/// - Parameter value: The integer to convert to a floating-point value.
public init(_ value: Int8) {
self.native = NativeType(value)
}
/// Creates a new value, rounded to the closest possible representatation.
///
/// If two representable values are equally close, the result is the value
/// with more trailing zeros in its significand bit pattern.
///
/// - Parameter value: The integer to convert to a floating-point value.
public init(_ value: UInt16) {
self.native = NativeType(value)
}
/// Creates a new value, rounded to the closest possible representatation.
///
/// If two representable values are equally close, the result is the value
/// with more trailing zeros in its significand bit pattern.
///
/// - Parameter value: The integer to convert to a floating-point value.
public init(_ value: Int16) {
self.native = NativeType(value)
}
/// Creates a new value, rounded to the closest possible representatation.
///
/// If two representable values are equally close, the result is the value
/// with more trailing zeros in its significand bit pattern.
///
/// - Parameter value: The integer to convert to a floating-point value.
public init(_ value: UInt32) {
self.native = NativeType(value)
}
/// Creates a new value, rounded to the closest possible representatation.
///
/// If two representable values are equally close, the result is the value
/// with more trailing zeros in its significand bit pattern.
///
/// - Parameter value: The integer to convert to a floating-point value.
public init(_ value: Int32) {
self.native = NativeType(value)
}
/// Creates a new value, rounded to the closest possible representatation.
///
/// If two representable values are equally close, the result is the value
/// with more trailing zeros in its significand bit pattern.
///
/// - Parameter value: The integer to convert to a floating-point value.
public init(_ value: UInt64) {
self.native = NativeType(value)
}
/// Creates a new value, rounded to the closest possible representatation.
///
/// If two representable values are equally close, the result is the value
/// with more trailing zeros in its significand bit pattern.
///
/// - Parameter value: The integer to convert to a floating-point value.
public init(_ value: Int64) {
self.native = NativeType(value)
}
/// Creates a new value, rounded to the closest possible representatation.
///
/// If two representable values are equally close, the result is the value
/// with more trailing zeros in its significand bit pattern.
///
/// - Parameter value: The integer to convert to a floating-point value.
public init(_ value: UInt) {
self.native = NativeType(value)
}
/// Creates a new value, rounded to the closest possible representatation.
///
/// If two representable values are equally close, the result is the value
/// with more trailing zeros in its significand bit pattern.
///
/// - Parameter value: The integer to convert to a floating-point value.
public init(_ value: Int) {
self.native = NativeType(value)
}
/// The native value.
public var native: NativeType
}
@_transparent extension CGFloat : BinaryFloatingPoint {
public typealias RawSignificand = UInt
public typealias Exponent = Int
public static var exponentBitCount: Int {
return NativeType.exponentBitCount
}
public static var significandBitCount: Int {
return NativeType.significandBitCount
}
// Conversions to/from integer encoding. These are not part of the
// BinaryFloatingPoint prototype because there's no guarantee that an
// integer type of the same size actually exists (e.g. Float80).
public var bitPattern: UInt {
return UInt(native.bitPattern)
}
public init(bitPattern: UInt) {
#if arch(i386) || arch(arm)
native = NativeType(bitPattern: UInt32(bitPattern))
#elseif arch(x86_64) || arch(arm64) || arch(s390x)
native = NativeType(bitPattern: UInt64(bitPattern))
#endif
}
public var sign: FloatingPointSign {
return native.sign
}
public var exponentBitPattern: UInt {
return native.exponentBitPattern
}
public var significandBitPattern: UInt {
return UInt(native.significandBitPattern)
}
public init(sign: FloatingPointSign,
exponentBitPattern: UInt,
significandBitPattern: UInt) {
native = NativeType(sign: sign,
exponentBitPattern: exponentBitPattern,
significandBitPattern: NativeType.RawSignificand(significandBitPattern))
}
public init(nan payload: RawSignificand, signaling: Bool) {
native = NativeType(nan: NativeType.RawSignificand(payload),
signaling: signaling)
}
public static var infinity: CGFloat {
return CGFloat(NativeType.infinity)
}
public static var nan: CGFloat {
return CGFloat(NativeType.nan)
}
public static var signalingNaN: CGFloat {
return CGFloat(NativeType.signalingNaN)
}
@available(*, unavailable, renamed: "nan")
public static var quietNaN: CGFloat {
fatalError("unavailable")
}
public static var greatestFiniteMagnitude: CGFloat {
return CGFloat(NativeType.greatestFiniteMagnitude)
}
public static var pi: CGFloat {
return CGFloat(NativeType.pi)
}
public var ulp: CGFloat {
return CGFloat(native.ulp)
}
public static var leastNormalMagnitude: CGFloat {
return CGFloat(NativeType.leastNormalMagnitude)
}
public static var leastNonzeroMagnitude: CGFloat {
return CGFloat(NativeType.leastNonzeroMagnitude)
}
public var exponent: Int {
return native.exponent
}
public var significand: CGFloat {
return CGFloat(native.significand)
}
public init(sign: FloatingPointSign, exponent: Int, significand: CGFloat) {
native = NativeType(sign: sign,
exponent: exponent, significand: significand.native)
}
public mutating func round(_ rule: FloatingPointRoundingRule) {
native.round(rule)
}
public var nextUp: CGFloat {
return CGFloat(native.nextUp)
}
public var magnitude: CGFloat {
return CGFloat(Swift.abs(native))
}
public mutating func negate() {
native.negate()
}
public mutating func add(_ other: CGFloat) {
native.add(other.native)
}
public mutating func subtract(_ other: CGFloat) {
native.subtract(other.native)
}
public mutating func multiply(by other: CGFloat) {
native.multiply(by: other.native)
}
public mutating func divide(by other: CGFloat) {
native.divide(by: other.native)
}
public mutating func formTruncatingRemainder(dividingBy other: CGFloat) {
native.formTruncatingRemainder(dividingBy: other.native)
}
public mutating func formRemainder(dividingBy other: CGFloat) {
native.formRemainder(dividingBy: other.native)
}
public mutating func formSquareRoot( ) {
native.formSquareRoot( )
}
public mutating func addProduct(_ lhs: CGFloat, _ rhs: CGFloat) {
native.addProduct(lhs.native, rhs.native)
}
public func isEqual(to other: CGFloat) -> Bool {
return self.native.isEqual(to: other.native)
}
public func isLess(than other: CGFloat) -> Bool {
return self.native.isLess(than: other.native)
}
public func isLessThanOrEqualTo(_ other: CGFloat) -> Bool {
return self.native.isLessThanOrEqualTo(other.native)
}
public var isNormal: Bool {
return native.isNormal
}
public var isFinite: Bool {
return native.isFinite
}
public var isZero: Bool {
return native.isZero
}
public var isSubnormal: Bool {
return native.isSubnormal
}
public var isInfinite: Bool {
return native.isInfinite
}
public var isNaN: Bool {
return native.isNaN
}
public var isSignalingNaN: Bool {
return native.isSignalingNaN
}
@available(*, unavailable, renamed: "isSignalingNaN")
public var isSignaling: Bool {
fatalError("unavailable")
}
public var isCanonical: Bool {
return true
}
public var floatingPointClass: FloatingPointClassification {
return native.floatingPointClass
}
public var binade: CGFloat {
return CGFloat(native.binade)
}
public var significandWidth: Int {
return native.significandWidth
}
/// Create an instance initialized to `value`.
public init(floatLiteral value: NativeType) {
native = value
}
/// Create an instance initialized to `value`.
public init(integerLiteral value: Int) {
native = NativeType(value)
}
}
extension CGFloat {
@available(*, unavailable, renamed: "leastNormalMagnitude")
public static var min: CGFloat {
fatalError("unavailable")
}
@available(*, unavailable, renamed: "greatestFiniteMagnitude")
public static var max: CGFloat {
fatalError("unavailable")
}
@available(*, unavailable, message: "Please use the `abs(_:)` free function")
public static func abs(_ x: CGFloat) -> CGFloat {
fatalError("unavailable")
}
}
@available(*, unavailable, renamed: "CGFloat.leastNormalMagnitude")
public var CGFLOAT_MIN: CGFloat {
fatalError("unavailable")
}
@available(*, unavailable, renamed: "CGFloat.greatestFiniteMagnitude")
public var CGFLOAT_MAX: CGFloat {
fatalError("unavailable")
}
extension CGFloat : CustomReflectable {
/// Returns a mirror that reflects `self`.
public var customMirror: Mirror {
return Mirror(reflecting: native)
}
}
@_transparent extension CGFloat : CustomStringConvertible {
/// A textual representation of `self`.
public var description: String {
return native.description
}
}
@_transparent extension CGFloat : Hashable {
/// The hash value.
///
/// **Axiom:** `x == y` implies `x.hashValue == y.hashValue`
///
/// - Note: the hash value is not guaranteed to be stable across
/// different invocations of the same program. Do not persist the
/// hash value across program runs.
public var hashValue: Int {
return native.hashValue
}
}
@_transparent extension UInt8 {
public init(_ value: CGFloat) {
self = UInt8(value.native)
}
}
@_transparent extension Int8 {
public init(_ value: CGFloat) {
self = Int8(value.native)
}
}
@_transparent extension UInt16 {
public init(_ value: CGFloat) {
self = UInt16(value.native)
}
}
@_transparent extension Int16 {
public init(_ value: CGFloat) {
self = Int16(value.native)
}
}
@_transparent extension UInt32 {
public init(_ value: CGFloat) {
self = UInt32(value.native)
}
}
@_transparent extension Int32 {
public init(_ value: CGFloat) {
self = Int32(value.native)
}
}
@_transparent extension UInt64 {
public init(_ value: CGFloat) {
self = UInt64(value.native)
}
}
@_transparent extension Int64 {
public init(_ value: CGFloat) {
self = Int64(value.native)
}
}
@_transparent extension UInt {
public init(_ value: CGFloat) {
self = UInt(value.native)
}
}
@_transparent extension Int {
public init(_ value: CGFloat) {
self = Int(value.native)
}
}
@_transparent extension Double {
public init(_ value: CGFloat) {
self = Double(value.native)
}
}
@_transparent extension Float {
public init(_ value: CGFloat) {
self = Float(value.native)
}
}
//===----------------------------------------------------------------------===//
// Standard Operator Table
//===----------------------------------------------------------------------===//
// TODO: These should not be necessary, since they're already provided by
// <T: FloatingPoint>, but in practice they are currently needed to
// disambiguate overloads. We should find a way to remove them, either by
// tweaking the overload resolution rules, or by removing the other
// definitions in the standard lib, or both.
@_transparent
public func +(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
return lhs.adding(rhs)
}
@_transparent
public func -(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
return lhs.subtracting(rhs)
}
@_transparent
public func *(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
return lhs.multiplied(by: rhs)
}
@_transparent
public func /(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
return lhs.divided(by: rhs)
}
@_transparent
public func +=(lhs: inout CGFloat, rhs: CGFloat) {
lhs.add(rhs)
}
@_transparent
public func -=(lhs: inout CGFloat, rhs: CGFloat) {
lhs.subtract(rhs)
}
@_transparent
public func *=(lhs: inout CGFloat, rhs: CGFloat) {
lhs.multiply(by: rhs)
}
@_transparent
public func /=(lhs: inout CGFloat, rhs: CGFloat) {
lhs.divide(by: rhs)
}
//===----------------------------------------------------------------------===//
// Strideable Conformance
//===----------------------------------------------------------------------===//
@_transparent extension CGFloat : Strideable {
/// Returns a stride `x` such that `self.advanced(by: x)` approximates
/// `other`.
///
/// - Complexity: O(1).
@_transparent
public func distance(to other: CGFloat) -> CGFloat {
return CGFloat(other.native - self.native)
}
/// Returns a `Self` `x` such that `self.distance(to: x)` approximates
/// `n`.
///
/// - Complexity: O(1).
@_transparent
public func advanced(by amount: CGFloat) -> CGFloat {
return CGFloat(self.native + amount.native)
}
}
//===----------------------------------------------------------------------===//
// Deprecated operators
//===----------------------------------------------------------------------===//
@_transparent
@available(*, unavailable, message: "use += 1")
@discardableResult
public prefix func ++(rhs: inout CGFloat) -> CGFloat {
fatalError("++ is not available")
}
@_transparent
@available(*, unavailable, message: "use -= 1")
@discardableResult
public prefix func --(rhs: inout CGFloat) -> CGFloat {
fatalError("-- is not available")
}
@_transparent
@available(*, unavailable, message: "use += 1")
@discardableResult
public postfix func ++(lhs: inout CGFloat) -> CGFloat {
fatalError("++ is not available")
}
@_transparent
@available(*, unavailable, message: "use -= 1")
@discardableResult
public postfix func --(lhs: inout CGFloat) -> CGFloat {
fatalError("-- is not available")
}
@_transparent
@available(*, unavailable, message: "Use truncatingRemainder instead")
public func %(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
fatalError("% is not available.")
}
@_transparent
@available(*, unavailable, message: "Use formTruncatingRemainder instead")
public func %=(lhs: inout CGFloat, rhs: CGFloat) {
fatalError("%= is not available.")
}
//===----------------------------------------------------------------------===//
// tgmath
//===----------------------------------------------------------------------===//
@_transparent
public func acos(_ x: CGFloat) -> CGFloat {
return CGFloat(acos(x.native))
}
@_transparent
public func cos(_ x: CGFloat) -> CGFloat {
return CGFloat(cos(x.native))
}
@_transparent
public func sin(_ x: CGFloat) -> CGFloat {
return CGFloat(sin(x.native))
}
@_transparent
public func asin(_ x: CGFloat) -> CGFloat {
return CGFloat(asin(x.native))
}
@_transparent
public func atan(_ x: CGFloat) -> CGFloat {
return CGFloat(atan(x.native))
}
@_transparent
public func tan(_ x: CGFloat) -> CGFloat {
return CGFloat(tan(x.native))
}
@_transparent
public func acosh(_ x: CGFloat) -> CGFloat {
return CGFloat(acosh(x.native))
}
@_transparent
public func asinh(_ x: CGFloat) -> CGFloat {
return CGFloat(asinh(x.native))
}
@_transparent
public func atanh(_ x: CGFloat) -> CGFloat {
return CGFloat(atanh(x.native))
}
@_transparent
public func cosh(_ x: CGFloat) -> CGFloat {
return CGFloat(cosh(x.native))
}
@_transparent
public func sinh(_ x: CGFloat) -> CGFloat {
return CGFloat(sinh(x.native))
}
@_transparent
public func tanh(_ x: CGFloat) -> CGFloat {
return CGFloat(tanh(x.native))
}
@_transparent
public func exp(_ x: CGFloat) -> CGFloat {
return CGFloat(exp(x.native))
}
@_transparent
public func exp2(_ x: CGFloat) -> CGFloat {
return CGFloat(exp2(x.native))
}
@_transparent
public func expm1(_ x: CGFloat) -> CGFloat {
return CGFloat(expm1(x.native))
}
@_transparent
public func log(_ x: CGFloat) -> CGFloat {
return CGFloat(log(x.native))
}
@_transparent
public func log10(_ x: CGFloat) -> CGFloat {
return CGFloat(log10(x.native))
}
@_transparent
public func log2(_ x: CGFloat) -> CGFloat {
return CGFloat(log2(x.native))
}
@_transparent
public func log1p(_ x: CGFloat) -> CGFloat {
return CGFloat(log1p(x.native))
}
@_transparent
public func logb(_ x: CGFloat) -> CGFloat {
return CGFloat(logb(x.native))
}
@_transparent
public func cbrt(_ x: CGFloat) -> CGFloat {
return CGFloat(cbrt(x.native))
}
@_transparent
public func erf(_ x: CGFloat) -> CGFloat {
return CGFloat(erf(x.native))
}
@_transparent
public func erfc(_ x: CGFloat) -> CGFloat {
return CGFloat(erfc(x.native))
}
@_transparent
public func tgamma(_ x: CGFloat) -> CGFloat {
return CGFloat(tgamma(x.native))
}
@_transparent
public func nearbyint(_ x: CGFloat) -> CGFloat {
return CGFloat(nearbyint(x.native))
}
@_transparent
public func rint(_ x: CGFloat) -> CGFloat {
return CGFloat(rint(x.native))
}
@_transparent
public func atan2(_ lhs: CGFloat, _ rhs: CGFloat) -> CGFloat {
return CGFloat(atan2(lhs.native, rhs.native))
}
@_transparent
public func hypot(_ lhs: CGFloat, _ rhs: CGFloat) -> CGFloat {
return CGFloat(hypot(lhs.native, rhs.native))
}
@_transparent
public func pow(_ lhs: CGFloat, _ rhs: CGFloat) -> CGFloat {
return CGFloat(pow(lhs.native, rhs.native))
}
@_transparent
public func copysign(_ lhs: CGFloat, _ rhs: CGFloat) -> CGFloat {
return CGFloat(copysign(lhs.native, rhs.native))
}
@_transparent
public func nextafter(_ lhs: CGFloat, _ rhs: CGFloat) -> CGFloat {
return CGFloat(nextafter(lhs.native, rhs.native))
}
@_transparent
public func fdim(_ lhs: CGFloat, _ rhs: CGFloat) -> CGFloat {
return CGFloat(fdim(lhs.native, rhs.native))
}
@_transparent
public func fmax(_ lhs: CGFloat, _ rhs: CGFloat) -> CGFloat {
return CGFloat(fmax(lhs.native, rhs.native))
}
@_transparent
public func fmin(_ lhs: CGFloat, _ rhs: CGFloat) -> CGFloat {
return CGFloat(fmin(lhs.native, rhs.native))
}
@_transparent
@available(*, unavailable, message: "use the floatingPointClass property.")
public func fpclassify(_ x: CGFloat) -> Int {
fatalError("unavailable")
}
@available(*, unavailable, message: "use the isNormal property.")
public func isnormal(_ value: CGFloat) -> Bool { return value.isNormal }
@available(*, unavailable, message: "use the isFinite property.")
public func isfinite(_ value: CGFloat) -> Bool { return value.isFinite }
@available(*, unavailable, message: "use the isInfinite property.")
public func isinf(_ value: CGFloat) -> Bool { return value.isInfinite }
@available(*, unavailable, message: "use the isNaN property.")
public func isnan(_ value: CGFloat) -> Bool { return value.isNaN }
@available(*, unavailable, message: "use the sign property.")
public func signbit(_ value: CGFloat) -> Int { return value.sign.rawValue }
@_transparent
public func modf(_ x: CGFloat) -> (CGFloat, CGFloat) {
let (ipart, fpart) = modf(x.native)
return (CGFloat(ipart), CGFloat(fpart))
}
@_transparent
public func ldexp(_ x: CGFloat, _ n: Int) -> CGFloat {
return CGFloat(ldexp(x.native, n))
}
@_transparent
public func frexp(_ x: CGFloat) -> (CGFloat, Int) {
let (frac, exp) = frexp(x.native)
return (CGFloat(frac), exp)
}
@_transparent
public func ilogb(_ x: CGFloat) -> Int {
return ilogb(x.native)
}
@_transparent
public func scalbn(_ x: CGFloat, _ n: Int) -> CGFloat {
return CGFloat(scalbn(x.native, n))
}
@_transparent
public func lgamma(_ x: CGFloat) -> (CGFloat, Int) {
let (value, sign) = lgamma(x.native)
return (CGFloat(value), sign)
}
@_transparent
public func remquo(_ x: CGFloat, _ y: CGFloat) -> (CGFloat, Int) {
let (rem, quo) = remquo(x.native, y.native)
return (CGFloat(rem), quo)
}
@_transparent
public func nan(_ tag: String) -> CGFloat {
return CGFloat(nan(tag) as CGFloat.NativeType)
}
@_transparent
public func j0(_ x: CGFloat) -> CGFloat {
return CGFloat(j0(Double(x.native)))
}
@_transparent
public func j1(_ x: CGFloat) -> CGFloat {
return CGFloat(j1(Double(x.native)))
}
@_transparent
public func jn(_ n: Int, _ x: CGFloat) -> CGFloat {
return CGFloat(jn(n, Double(x.native)))
}
@_transparent
public func y0(_ x: CGFloat) -> CGFloat {
return CGFloat(y0(Double(x.native)))
}
@_transparent
public func y1(_ x: CGFloat) -> CGFloat {
return CGFloat(y1(Double(x.native)))
}
@_transparent
public func yn(_ n: Int, _ x: CGFloat) -> CGFloat {
return CGFloat(yn(n, Double(x.native)))
}
@_transparent
extension CGFloat : _CVarArgPassedAsDouble, _CVarArgAligned {
/// Transform `self` into a series of machine words that can be
/// appropriately interpreted by C varargs
public var _cVarArgEncoding: [Int] {
return native._cVarArgEncoding
}
/// Return the required alignment in bytes of
/// the value returned by `_cVarArgEncoding`.
public var _cVarArgAlignment: Int {
return native._cVarArgAlignment
}
}