//===--- IntegerArithmeticType.swift.gyb ---------------------*- swift -*--===// // // This source file is part of the Swift.org open source project // // Copyright (c) 2014 - 2015 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 // //===----------------------------------------------------------------------===// %# Ignore the following admonition; it applies to the resulting .swift file only // Automatically Generated From IntegerArithmeticType.swift.gyb. Do Not // Edit Directly! % integerBinaryOps = [ % (x[:-1], x[-1]) for x in % 'add+ subtract- multiply* divide/ remainder%'.split()] /// This protocol is an implementation detail of `IntegerArithmeticType`; do /// not use it directly. /// /// Its requirements are inherited by `IntegerArithmeticType` and thus must /// be satisfied by types conforming to that protocol. public protocol _IntegerArithmeticType { % for name,_ in integerBinaryOps: /// ${name} `lhs` and `rhs`, returning a result and a `Bool` that is /// true iff the operation caused an arithmetic overflow. class func ${name}WithOverflow(lhs: Self, _ rhs: Self) -> (Self, overflow: Bool) % end } /// The common requirements for types that support integer arithmetic. public protocol IntegerArithmeticType : _IntegerArithmeticType, Comparable { // Checked arithmetic functions. Specific implementations in // FixedPoint.swift.gyb support static checking for integer types. % for name,op in integerBinaryOps: /// ${name} `lhs` and `rhs`, trapping in case of arithmetic overflow /// (except in -Ounchecked builds). func ${op} (lhs: Self, rhs: Self) -> Self % end /// Explicitly convert to `IntMax`, trapping on overflow (except in /// -Ounchecked builds). func toIntMax() -> IntMax } % for name,op in integerBinaryOps: /// ${name} `lhs` and `rhs`, trapping in case of arithmetic overflow /// (except in -Ounchecked builds). @transparent public func ${op} (lhs: T, rhs: T) -> T { return _overflowChecked(T.${name}WithOverflow(lhs, rhs)) } /// ${name} `lhs` and `rhs`, silently discarding any overflow. @transparent public func &${op} (lhs: T, rhs: T) -> T { return T.${name}WithOverflow(lhs, rhs).0 } /// ${name} `lhs` and `rhs` and store the result in `lhs`, trapping in /// case of arithmetic overflow (except in -Ounchecked builds). @transparent public func ${op}= (inout lhs: T, rhs: T) { lhs = lhs ${op} rhs } % end //===--- SignedNumberType -------------------------------------------------===// // A numeric type that supports abs(x), +x and -x //===----------------------------------------------------------------------===// /// This protocol is an implementation detail of `SignedNumberType`; do /// not use it directly. /// /// Its requirements are inherited by `SignedNumberType` and thus must /// be satisfied by types conforming to that protocol. public protocol _SignedNumberType : Comparable, IntegerLiteralConvertible { // Subtraction is a requirement for SignedNumberType func - (lhs: Self, rhs: Self) -> Self } // SignedNumberType itself contains only operator requirements having // default implementations on the base protocol. public protocol SignedNumberType : _SignedNumberType { prefix func - (x: Self) -> Self // Do not use this operator directly; call abs(x) instead func ~> (_:Self,_:(_Abs, ())) -> Self } // Unary negation in terms of subtraction. This is a default // implementation; models of SignedNumberType can provide their own // implementations. @transparent public prefix func - (x: T) -> T { return 0 - x } // Unary + @transparent public prefix func + (x: T) -> T { return x } //===--- abs(x) -----------------------------------------------------------===// public struct _Abs {} internal func _abs(args: Args) -> (_Abs, Args) { return (_Abs(), args) } // Do not use this operator directly; call abs(x) instead @transparent public func ~> (x:T,_:(_Abs, ())) -> T { return x < 0 ? -x : x } /// A type that supports an "absolute value" function. public protocol AbsoluteValuable : SignedNumberType { class func abs(_:Self) -> Self } // Do not use this operator directly; call abs(x) instead @transparent public func ~> (x:T,_:(_Abs, ())) -> T { return T.abs(x) } // Absolute value. Concrete instances of SignedNumberType can specialize // this function by conforming to AbsoluteValuable. @transparent public func abs(x: T) -> T { return x~>_abs() } // ${'Local Variables'}: // eval: (read-only-mode 1) // End: