@@ -45,14 +45,21 @@ class struct(object):
45
45
def __repr__( self ) :
46
46
return 'struct ( % r) ' % self . __dict__
47
47
48
- binaryArithmetic = [
49
- struct ( operator= '+ ', name= 'adding', mutatingName= 'add', firstArg= '_', llvmName= 'add', kind= '+ ') ,
50
- struct ( operator= '- ', name= 'subtracting', mutatingName= 'subtract', firstArg= '_', llvmName= 'sub', kind= '- ') ,
51
- struct ( operator= '* ', name= 'multiplied', mutatingName= 'multiply', firstArg= 'by', llvmName= 'mul', kind= '* ') ,
52
- struct ( operator= '/', name='divided', mutatingName='divide', firstArg='by', llvmName='div', kind='/ ') ,
48
+ binaryArithmetic = {
49
+ 'Arithmetic' : [
50
+ struct ( operator= '+ ', name= 'adding', mutatingName= 'add', firstArg= '_', llvmName= 'add', kind= '+ ') ,
51
+ struct ( operator= '- ', name= 'subtracting', mutatingName= 'subtract', firstArg= '_', llvmName= 'sub', kind= '- ') ,
52
+ struct ( operator= '* ', name= 'multiplied', mutatingName= 'multiply', firstArg= 'by', llvmName= 'mul', kind= '* ') ,
53
+ struct ( operator= '/', name='divided', mutatingName='divide', firstArg='by', llvmName='div', kind='/ ') ,
54
+ ] ,
55
+ 'Integer' : [
53
56
struct ( operator= '% ', name= 'remainder', mutatingName= 'formRemainder', firstArg= 'dividingBy', llvmName= 'rem', kind= '/ ') ,
54
- ]
57
+ ] ,
58
+ }
55
59
60
+ # We need the same order of operations, so simple itertools. chain ( d. values ( ) )
61
+ # won't work
62
+ allBinaryArithmetic = binaryArithmetic [ 'Arithmetic'] + binaryArithmetic[ 'Integer']
56
63
57
64
binaryBitwise = [
58
65
struct ( operator= '& ', name= 'and') ,
@@ -136,11 +143,11 @@ public func _log(@autoclosure _ message: ()->String) {
136
143
/// non-mutating implementations, they will be provided by a protocol extension.
137
144
/// Implementation in that case will copy `self`, perform a mutating operation
138
145
/// on it and return the resulting value.
139
- public protocol Arithmetic {
146
+ public protocol Arithmetic : Equatable , IntegerLiteralConvertible {
140
147
/// Initialize to zero
141
148
init ( )
142
149
143
- % for x in binaryArithmetic:
150
+ % for x in binaryArithmetic [ 'Arithmetic' ] :
144
151
// defaulted using an in-place counterpart, but can be used as an
145
152
// optimization hook
146
153
@warn_unused_result
@@ -151,16 +158,18 @@ public protocol Arithmetic {
151
158
% end
152
159
}
153
160
154
- extension Arithmetic {
155
- % for x in binaryArithmetic:
156
- % callLabel = x. firstArg + ': ' if not x. firstArg == '_' else ''
161
+ % for Protocol in binaryArithmetic:
162
+ extension ${ Protocol} {
163
+ % for x in binaryArithmetic [ Protocol] :
164
+ % callLabel = x. firstArg + ': ' if not x. firstArg == '_' else ''
157
165
public func ${ x. name} ( ${ x. firstArg} rhs: Self) - > Self {
158
166
var lhs = self
159
167
lhs . ${ x. mutatingName} ( ${ callLabel} rhs)
160
168
return lhs
161
169
}
162
- % end
170
+ % end
163
171
}
172
+ % end
164
173
165
174
/// SignedArithmetic protocol will only be conformed to by signed numbers,
166
175
/// otherwise it would be possible to negate an unsigned value.
@@ -177,18 +186,20 @@ extension SignedArithmetic {
177
186
}
178
187
}
179
188
180
- % for x in binaryArithmetic:
181
- % callLabel = x. firstArg + ': ' if not x. firstArg == '_' else ''
189
+ % for Protocol in binaryArithmetic:
190
+ % for x in binaryArithmetic [ Protocol] :
191
+ % callLabel = x. firstArg + ': ' if not x. firstArg == '_' else ''
182
192
@_transparent
183
193
@warn_unused_result
184
- public func ${ x. operator} < T: Arithmetic > ( lhs: T, rhs: T) - > T {
194
+ public func ${ x. operator} < T: $ { Protocol } > ( lhs: T, rhs: T) - > T {
185
195
return lhs . ${ x. name} ( ${ callLabel} rhs)
186
196
}
187
197
188
198
@_transparent
189
- public func ${ x. operator} = < T: Arithmetic > ( lhs: inout T, rhs: T) {
199
+ public func ${ x. operator} = < T: $ { Protocol } > ( lhs: inout T, rhs: T) {
190
200
lhs . ${ x. mutatingName} ( ${ callLabel} rhs)
191
201
}
202
+ % end
192
203
% end
193
204
194
205
@_transparent
@@ -274,6 +285,16 @@ public protocol Integer : ${IntegerBase} {
274
285
/// Has the value -1 if `self` is 0.
275
286
/// Has the value equal to `bitWidth - 1` for fixed width integers.
276
287
var signBitIndex : Word { get }
288
+
289
+ % for x in binaryArithmetic [ 'Integer'] :
290
+ // defaulted using an in-place counterpart, but can be used as an
291
+ // optimization hook
292
+ @warn_unused_result
293
+ func ${ x. name } ( ${ x. firstArg} rhs: Self) - > Self
294
+
295
+ // implementation hook
296
+ mutating func ${ x. mutatingName} ( ${ x. firstArg} rhs: Self)
297
+ % end
277
298
}
278
299
279
300
extension Integer {
@@ -419,7 +440,7 @@ public protocol FixedWidthInteger : Integer {
419
440
static var max : Self { get }
420
441
static var min : Self { get }
421
442
422
- % for x in binaryArithmetic :
443
+ % for x in allBinaryArithmetic :
423
444
% {
424
445
comment = '''
425
446
/// Return a pair consisting of `self` {} `rhs`,
@@ -551,7 +572,7 @@ extension FixedWidthInteger {
551
572
else { self = Self ( extendingOrTruncating: source) }
552
573
}
553
574
554
- % for x in binaryArithmetic :
575
+ % for x in allBinaryArithmetic :
555
576
% callLabel = x. firstArg + ': ' if not x. firstArg == '_' else ''
556
577
@_transparent
557
578
public mutating func ${ x. mutating Name} ( ${ x. firstArg} rhs: Self) {
@@ -616,7 +637,7 @@ extension FixedWidthInteger {
616
637
}
617
638
}
618
639
619
- % for x in binaryArithmetic :
640
+ % for x in allBinaryArithmetic :
620
641
% callLabel = x. firstArg + ': ' if not x. firstArg == '_' else ''
621
642
% if x. kind != '/ ':
622
643
@warn_unused_result
@@ -763,7 +784,7 @@ public struct ${Self}
763
784
return Bool ( Builtin . cmp_ ${ u} lt_Int${ bits} ( _storage, rhs. _storage) )
764
785
}
765
786
766
- % for x in binaryArithmetic :
787
+ % for x in allBinaryArithmetic :
767
788
% callLabel = x. firstArg + ': ' if not x. firstArg == '_' else ''
768
789
/// Return a pair consisting of `self` ${x.operator} `rhs`,
769
790
/// truncated to fit if necessary, and a flag indicating whether an
@@ -1008,7 +1029,7 @@ public struct DoubleWidth<
1008
1029
1009
1030
public static var bitWidth : Word { return 2 * T. bitWidth }
1010
1031
1011
- % for x in binaryArithmetic [ : 2 ] :
1032
+ % for x in allBinaryArithmetic [ : 2 ] :
1012
1033
% highAffectedByLowOverflow = 'T. max' if x. operator == '+ ' else 'T. min'
1013
1034
public func ${ x. name} WithOverflow( _ rhs: DoubleWidth < T > )
1014
1035
- > ( partialValue: DoubleWidth < T > , overflow: ArithmeticOverflow) {
@@ -1089,7 +1110,7 @@ public struct DoubleWidth<
1089
1110
return mkResult ( carry > 0 )
1090
1111
}
1091
1112
1092
- % for x in binaryArithmetic [ 3 : ] :
1113
+ % for x in allBinaryArithmetic [ 3 : ] :
1093
1114
@warn_unused_result
1094
1115
public func ${ x. name} WithOverflow(
1095
1116
${ x. firstArg} rhs: DoubleWidth< T>
@@ -1165,7 +1186,7 @@ public struct ${Self}
1165
1186
return _storage. isLess ( than: rhs. _storage)
1166
1187
}
1167
1188
1168
- % for x in binaryArithmetic :
1189
+ % for x in allBinaryArithmetic :
1169
1190
% callLabel = x. firstArg + ': ' if not x. firstArg == '_' else ''
1170
1191
/// Return a pair consisting of `self` ${x.operator} `rhs`,
1171
1192
/// truncated to fit if necessary, and a flag indicating whether an
0 commit comments