-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathIntegerArithmetic.swift.gyb
158 lines (138 loc) · 5.17 KB
/
IntegerArithmetic.swift.gyb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//===--- IntegerArithmetic.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 IntegerArithmetic.swift.gyb. Do Not Edit
// Directly!
%{
integerBinaryOps = [
(x[:-1], x[-1], x[:-1].capitalize(), 'a result')
for x in 'add+ subtract- multiply* divide/'.split()
] + [ ('remainder', '%', 'Divide', 'the remainder') ]
}%
/// 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,_,Action,result in integerBinaryOps:
/// ${Action} `lhs` and `rhs`, returning ${result} and a `Bool` that is
/// true iff the operation caused an arithmetic overflow.
static 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,Action,result in integerBinaryOps:
/// ${Action} `lhs` and `rhs`, returning ${result} and trapping in case of
/// arithmetic overflow (except in -Ounchecked builds).
@warn_unused_result
func ${op} (lhs: Self, rhs: Self) -> Self
% end
/// Explicitly convert to `IntMax`, trapping on overflow (except in
/// -Ounchecked builds).
@warn_unused_result
func toIntMax() -> IntMax
}
% for name,op,Action,result in integerBinaryOps:
/// ${Action} `lhs` and `rhs`, returning ${result} and trapping in case of
/// arithmetic overflow (except in -Ounchecked builds).
@_transparent
@warn_unused_result
public func ${op} <T : _IntegerArithmeticType>(lhs: T, rhs: T) -> T {
return _overflowChecked(T.${name}WithOverflow(lhs, rhs))
}
% if (op != '/') and (op != '%'):
/// ${name} `lhs` and `rhs`, silently discarding any overflow.
@_transparent
@warn_unused_result
public func &${op} <T : _IntegerArithmeticType>(lhs: T, rhs: T) -> T {
return T.${name}WithOverflow(lhs, rhs).0
}
% end
/// ${name} `lhs` and `rhs` and store the result in `lhs`, trapping in
/// case of arithmetic overflow (except in -Ounchecked builds).
@_transparent
public func ${op}= <T : _IntegerArithmeticType>(inout lhs: T, rhs: T) {
lhs = lhs ${op} rhs
}
% end
//===--- SignedNumberType -------------------------------------------------===//
// A numeric type that supports abs(x), +x and -x
//===----------------------------------------------------------------------===//
// SignedNumberType itself contains only operator requirements having
// default implementations on the base protocol.
/// Instances of conforming types can be subtracted, arithmetically
/// negated, and initialized from `0`.
///
/// Axioms:
///
/// - `x - 0 == x`
/// - `-x == 0 - x`
/// - `-(-x) == x`
public protocol SignedNumberType : Comparable, IntegerLiteralConvertible {
/// Return the result of negating `x`.
@warn_unused_result
prefix func - (x: Self) -> Self
/// Return the difference between `lhs` and `rhs`.
@warn_unused_result
func - (lhs: Self, rhs: 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 - <T : SignedNumberType>(x: T) -> T {
return 0 - x
}
// Unary +
@_transparent
public prefix func + <T : SignedNumberType>(x: T) -> T {
return x
}
//===--- abs(x) -----------------------------------------------------------===//
public struct _Abs {}
internal func _abs<Args>(args: Args) -> (_Abs, Args) {
return (_Abs(), args)
}
// Do not use this operator directly; call abs(x) instead
@_transparent
public func ~> <T : SignedNumberType>(x:T,_:(_Abs, ())) -> T {
return x < 0 ? -x : x
}
// FIXME: should this be folded into SignedNumberType?
/// A type that supports an "absolute value" function.
public protocol AbsoluteValuable : SignedNumberType {
/// Returns the absolute value of `x`.
@warn_unused_result
static func abs(x: Self) -> Self
}
// Do not use this operator directly; call abs(x) instead
@_transparent
public func ~> <T : AbsoluteValuable>(x:T,_:(_Abs, ())) -> T {
return T.abs(x)
}
/// Return the absolute value of `x`.
///
/// Concrete instances of `SignedNumberType` can specialize this
/// function by conforming to `AbsoluteValuable`.
@_transparent
public func abs<T : SignedNumberType>(x: T) -> T {
return x~>_abs()
}
// ${'Local Variables'}:
// eval: (read-only-mode 1)
// End: