protocol-oriented integers (take 2)

Vectors, matrices, etc can't conform to this protocol anyway because of the * and / requirements. And while it is true that it's not uncommon to reference an all-zero matrix or vector as "0", that doesn't work for any other number... for them I think the spelling should be "additiveIdentity". It'll be easy enough to just say
protocol Addable {
    static var additiveIdentity: Self {get}
    ...
}
extension Addable where Self: Arithmetic {
    static var additiveIdentity: Self {return Self.zero}
}

IMHO, anyway.

- Dave Sweeris

···

On Jan 25, 2017, at 07:59, Anton Mironov via swift-evolution <swift-evolution@swift.org> wrote:

Hi everyone,

I want to suggest a tiny extension to an Arithmetic protocol. It would be nice to have an additive identity and a multiplicative identity constants. Basically zero and one.

protocol Arithmetic {
  /* ... */
  static var zero: Self { get }  // additive identity: (value + .zero) == value
  static var one: Self { get }   // multiplicative identity: (value * .one) == value
}

These constants will ease implementation of math structures: vectors, matrices and etc.
I’m sorry if I’m duplicating someone’s suggestion. It is really hard to search for something in a long thread.

1. Square matrices of fixed size can absolutely conform to this protocol (except for ‘/‘, as discussed earlier in the thread).

2. It’s pretty common to call the multiplicative identity matrix 1 (though “I” is more common). The rest of the integers map to square matrices in the obvious way n —> n*1 (i.e. the matrix with n on the main diagonal, and zeros elsewhere).

– Steve

···

On Jan 25, 2017, at 1:58 PM, David Sweeris via swift-evolution <swift-evolution@swift.org> wrote:

On Jan 25, 2017, at 07:59, Anton Mironov via swift-evolution <swift-evolution@swift.org> wrote:

Hi everyone,

I want to suggest a tiny extension to an Arithmetic protocol. It would be nice to have an additive identity and a multiplicative identity constants. Basically zero and one.

protocol Arithmetic {
 /* ... */
 static var zero: Self { get }  // additive identity: (value + .zero) == value
 static var one: Self { get }   // multiplicative identity: (value * .one) == value
}

These constants will ease implementation of math structures: vectors, matrices and etc.
I’m sorry if I’m duplicating someone’s suggestion. It is really hard to search for something in a long thread.

Vectors, matrices, etc can't conform to this protocol anyway because of the * and / requirements. And while it is true that it's not uncommon to reference an all-zero matrix or vector as "0", that doesn't work for any other number... for them I think the spelling should be "additiveIdentity". It'll be easy enough to just say
protocol Addable {
   static var additiveIdentity: Self {get}
   ...
}
extension Addable where Self: Arithmetic {
   static var additiveIdentity: Self {return Self.zero}
}

IMHO, anyway.

Stephen Canon wrote that Arithmetic should refine
ExpressibleByIntegerLiteral because of certain mathematical properties of
rings. In that case, 0 and 1 would just be spelled in that way. Otherwise,
+1.

···

On Wed, Jan 25, 2017 at 11:38 Anton Mironov via swift-evolution < swift-evolution@swift.org> wrote:

Hi everyone,

I want to suggest a tiny extension to an Arithmetic protocol. It would be
nice to have an additive identity and a multiplicative identity constants.
Basically zero and one.

protocol Arithmetic {
  /* ... */
  static var zero: Self { get }  // additive identity: (value + .zero) ==
value
  static var one: Self { get }   // multiplicative identity: (value *
.one) == value
}

These constants will ease implementation of math structures: vectors,
matrices and etc.
I’m sorry if I’m duplicating someone’s suggestion. It is really hard to
search for something in a long thread.

Thanks,
Anton Mironov

On Jan 13, 2017, at 10:47 PM, Max Moiseev via swift-evolution < > swift-evolution@swift.org> wrote:

Hi everyone,

Back in June 2016 we discussed the new design of the integer types for the
standard library. It even resulted in acceptance of SE-0104
<https://github.com/apple/swift-evolution/blob/master/proposals/0104-improved-integers.md&gt; for
Swift 3. Unfortunately we were not able to implement it in time for the
release.

But it was not forgotten, although, as time went by, a few changes needed
to be made in order to reflect the current state of the language.
Without further introduction, please welcome the refined proposal to make
integers in Swift more suitable for generic programming.

Available in this gist
Protocol-oriented integers (take 2) · GitHub and also
inlined below.

Max

Protocol-oriented integers (take 2)

   - Proposal: SE-NNNN
   <https://gist.github.com/moiseev/0000-even-more-improved-integers.md&gt;
   - Authors: Dave Abrahams <https://github.com/dabrahams&gt;, Maxim Moiseev
   <https://github.com/moiseev&gt;
   - Review Manager: TBD
   - Status: Awaiting review
   - Bug: SR-3196 <Issues · apple/swift-issues · GitHub;
   - Previous Proposal: SE-0104
   <https://gist.github.com/moiseev/0104-improved-integers.md&gt;

<Protocol-oriented integers (take 2) · GitHub;
Introduction

This proposal is an evolution of SE-0104
<https://gist.github.com/moiseev/0104-improved-integers.md&gt;\. The goal is
still to clean up Swifts integer APIs and make them more useful for generic
programming.

The language has evolved in ways that affect integers APIs since the time
the original proposal was approved for Swift 3. We also attempted to
implement the proposed model in the standard library and found that some
essential APIs were missing, whereas others could be safely removed.

Major changes to the APIs introduced by this proposal (as compared to
SE-0104 <https://gist.github.com/moiseev/0104-improved-integers.md&gt;\) are
listed in a dedicated section
<Protocol-oriented integers (take 2) · GitHub;
.

<Protocol-oriented integers (take 2) · GitHub;
Motivation

Swift's integer protocols don't currently provide a suitable basis for
generic programming. See this blog post
<http://blog.krzyzanowskim.com/2015/03/01/swift_madness_of_generic_integer/&gt; for
an example of an attempt to implement a generic algorithm over integers.

The way the Arithmetic protocol is defined, it does not generalize to
floating point numbers and also slows down compilation by requiring every
concrete type to provide an implementation of arithmetic operators, thus
polluting the overload set.

Converting from one integer type to another is performed using the concept
of the 'maximum width integer' (see MaxInt), which is an artificial
limitation. The very existence of MaxInt makes it unclear what to do
should someone implement Int256, for example.

Another annoying problem is the inability to use integers of different
types in comparison and bit-shift operations. For example, the following
snippets won't compile:

var x: Int8 = 42let y = 1let z = 0

x <<= y // error: binary operator '<<=' cannot be applied to operands of type 'Int8' and 'Int'if x > z { ... } // error: binary operator '>' cannot be applied to operands of type 'Int8' and 'Int'

Currently, bit-shifting a negative number of (or too many) bits causes a
trap on some platforms, which makes low-level bit manipulations needlessly
dangerous and unpredictable.

Finally, the current design predates many of the improvements that came
since Swift 1, and hasn't been revised since then.

<Protocol-oriented integers (take 2) · GitHub
solution

We propose a new model that does not have above mentioned problems and is
more easily extensible.

                +--------------+ +-------------+
        +------>+ Arithmetic | | Comparable |
        > > (+,-,*,/) | | (==,<,>,...)|
        > +-------------++ +---+---------+
        > ^ ^
+-------+------------+ | |
> SignedArithmetic | +-+-------+-----------+
> (unary -) | | BinaryInteger |
+------+-------------+ |(words,%,bitwise,...)|
       ^ ++---+-----+----------+
       > +-----------^ ^ ^---------------+
       > > > >
+------+---------++ +---------+---------------+ +--+----------------+
> SignedInteger | | FixedWidthInteger | | UnsignedInteger |
> > >(endianness,overflow,...)| | |
+---------------+-+ +-+--------------------+--+ +-+-----------------+
                ^ ^ ^ ^
                > > > >
                > > > >
               ++--------+-+ +-+-------+-+
               >Int family |-+ |UInt family|-+
               +-----------+ | +-----------+ |
                 +-----------+ +-----------+

There are several benefits provided by this model over the old one:

   -

   It allows mixing integer types in generic functions.

   The possibility to initialize instances of any concrete integer type
   with values of any other concrete integer type enables writing functions
   that operate on more than one type conforming to BinaryInteger, such
   as heterogeneous comparisons or bit shifts, described later.
   -

   It removes the overload resolution overhead.

   Arithmetic and bitwise operations can now be defined as generic
   operators on protocols. This approach significantly reduces the number of
   overloads for those operations, which used to be defined for every single
   concrete integer type.
   -

   It enables protocol sharing between integer and floating point types.

   Note the exclusion of the % operation from Arithmetic. Its behavior
   for floating point numbers is sufficiently different from the one for
   integers that using it in generic context would lead to confusion. The
   FloatingPoint protocol introduced by SE-0067
   <https://gist.github.com/moiseev/0067-floating-point-protocols.md&gt; should
   now refine SignedArithmetic.
   -

   It makes future extensions possible.

   The proposed model eliminates the 'largest integer type' concept
   previously used to interoperate between integer types (see toIntMax in
   the current model) and instead provides access to machine words. It also
   introduces thedoubleWidthMultiply, doubleWidthDivide, and
   quotientAndRemainder methods. Together these changes can be used to
   provide an efficient implementation of bignums that would be hard to
   achieve otherwise.

The implementation of proposed model in the standard library is available in
the new-integer-protocols branch
<https://github.com/apple/swift/tree/new-integer-protocols&gt;\.

<Protocol-oriented integers (take 2) · GitHub
note on bit shifts

This proposal introduces the concepts of *smart shifts* and *masking
shifts*.

The semantics of shift operations are often undefined
<LLVM Language Reference Manual — LLVM 18.0.0git documentation; in under-
or over-shift cases. *Smart shifts*, implemented by >> and <<, are
designed to address this problem and always behave in a well defined way,
as shown in the examples below:

   -

   x << -2 is equivalent to x >> 2
   -

   (1 as UInt8) >> 42) will evaluate to 0
   -

   (-128 as Int8) >> 42) will evaluate to 0xff or -1

In most scenarios, the right hand operand is a literal constant, and
branches for handling under- and over-shift cases can be optimized away.
For other cases, this proposal provides *masking shifts*, implemented by
&>> and &<<. A masking shift logically preprocesses the right hand
operand by masking its bits to produce a value in the range 0...(x-1)
where x is the number of bits in the left hand operand. On most
architectures this masking is already performed by the CPU's shift
instructions and has no cost. Both kinds of shift avoid undefined behavior
and produce uniform semantics across architectures.

<Protocol-oriented integers (take 2) · GitHub
design
<https://gist.github.com/moiseev/62ffe3c91b66866fdebf6f3fcc7cad8c#whats-new-since-se-0104&gt;What&#39;s
new since SE-0104

   -

   SE-0091
   <https://gist.github.com/moiseev/0091-improving-operators-in-protocols.md&gt; removed
   the necessity to dispatch generic operators through special methods.

   All operators are now declared by protocols as static funcs.
   -

   Standard Library no longer provides + and - operators for Strideable
    types.

   They were problematic, as one could have written mixed-type code like let
   x: Int64 = 42; x += (1 as Int), which would compile, but shouldn't.
   Besides, since the Stride of an unsigned type is signed, Standard
   Library had to implement a hack to make code like let x: UInt = 42; x
   += (1 as Int) ambiguous. These operators were only necessary because
   they made advancing collection indices convenient, which is no longer the
   case since the introduction of the new indexing model
   <https://gist.github.com/moiseev/0065-collections-move-indices.md&gt; in
   Swift 3.
   -

   Shifts and other bitwise operations were moved from FixedWidthInteger
    to BinaryInteger.

   Left shift operation on an unbounded integer should infinitely extend
   the number, and never drop set bits when they reach the most significant
   position in the underlying representation.
   -

   BitwiseOperations protocol was deprecated.

   We believe there are no useful entities that support bitwise
   operations, but at the same time are not binary integers.
   -

   minimumSignedRepresentationBitWidth property was removed.
   -

   trailingZeros property was added to the BinaryInteger protocol.

   leadingZeros and popcount properties are still defined by the
   FixedWidthInteger protocol.
   -

   Endian-converting initializers and properties were added to the
   FixedWidthInteger protocol.
   -

   Standard library introduces the new type DoubleWidth<T>.

   See this section
   <Protocol-oriented integers (take 2) · GitHub; for
   more details.

<Protocol-oriented integers (take 2) · GitHub;
Protocols
<Protocol-oriented integers (take 2) · GitHub;
Arithmetic

The Arithmetic protocol declares binary arithmetic operators – such as +,
-, and * — and their mutating counterparts.

It provides a suitable basis for arithmetic on scalars such as integers
and floating point numbers.

Both mutating and non-mutating operations are declared in the protocol,
however only the mutating ones are required, as default implementations of
the non-mutating ones are provided by a protocol extension.

The Magnitude associated type is able to hold the absolute value of any
possible value of Self. Concrete types do not have to provide a type
alias for it, as it can be inferred from the magnitude property. This
property can be useful in operations that are simpler to implement in terms
of unsigned values, for example, printing a value of an integer, which is
just printing a '-' character in front of an absolute value.

Please note that for ordinary work, the magnitude property should not be
preferred to the abs(_) function, whose return value is of the same type
as its argument.

public protocol Arithmetic : Equatable, ExpressibleByIntegerLiteral {
  /// Creates a new instance from the given integer, if it can be represented /// exactly. /// /// If the value passed as `source` is not representable exactly, the result /// is `nil`. In the following example, the constant `x` is successfully /// created from a value of `100`, while the attempt to initialize the /// constant `y` from `1_000` fails because the `Int8` type can represent /// `127` at maximum: /// /// let x = Int8(exactly: 100) /// // x == Optional(100) /// let y = Int8(exactly: 1_000) /// // y == nil /// /// - Parameter source: A floating-point value to convert to an integer. init?<T : BinaryInteger>(exactly source: T)

  /// A type that can represent the absolute value of any possible value of the /// conforming type. associatedtype Magnitude : Equatable, ExpressibleByIntegerLiteral

  /// The magnitude of this value. /// /// For any numeric value `x`, `x.magnitude` is the absolute value of `x`. /// You can use the `magnitude` property in operations that are simpler to /// implement in terms of unsigned values, such as printing the value of an /// integer, which is just printing a '-' character in front of an absolute /// value. /// /// let x = -200 /// // x.magnitude == 200 /// /// The global `abs(_:)` function provides more familiar syntax when you need /// to find an absolute value. In addition, because `abs(_:)` always returns /// a value of the same type, even in a generic context, using the function /// instead of the `magnitude` property is encouraged. /// /// - SeeAlso: `abs(_:)` var magnitude: Magnitude { get }

  /// Returns the sum of the two given values. /// /// The sum of `lhs` and `rhs` must be representable in the same type. In the /// following example, the result of `100 + 200` is greater than the maximum /// representable `Int8` value: /// /// let x: Int8 = 10 + 21 /// // x == 31 /// let y: Int8 = 100 + 121 /// // Overflow error static func +(_ lhs: Self, _ rhs: Self) -> Self

  /// Adds the given value to this value in place. /// /// For example: /// /// var x = 15 /// y += 7 /// // y == 22 static func +=(_ lhs: inout Self, rhs: Self)

  /// Returns the difference of the two given values. /// /// The difference of `lhs` and `rhs` must be representable in the same type. /// In the following example, the result of `10 - 21` is less than zero, the /// minimum representable `UInt` value: /// /// let x: UInt = 21 - 10 /// // x == 11 /// let y: UInt = 10 - 21 /// // Overflow error static func -(_ lhs: Self, _ rhs: Self) -> Self

  /// Subtracts the given value from this value in place. /// /// For example: /// /// var x = 15 /// y -= 7 /// // y == 8 static func -=(_ lhs: inout Self, rhs: Self)

  /// Returns the product of the two given values. /// /// The product of `lhs` and `rhs` must be representable in the same type. In /// the following example, the result of `10 * 50` is greater than the /// maximum representable `Int8` value. /// /// let x: Int8 = 10 * 5 /// // x == 50 /// let y: Int8 = 10 * 50 /// // Overflow error static func *(_ lhs: Self, _ rhs: Self) -> Self

  /// Multiples this value by the given value in place. /// /// For example: /// /// var x = 15 /// y *= 7 /// // y == 105 static func *=(_ lhs: inout Self, rhs: Self)

  /// Returns the quotient of dividing the first value by the second. /// /// For integer types, any remainder of the division is discarded. /// /// let x = 21 / 5 /// // x == 4 static func /(_ lhs: Self, _ rhs: Self) -> Self

  /// Divides this value by the given value in place. /// /// For example: /// /// var x = 15 /// y /= 7 /// // y == 2 static func /=(_ lhs: inout Self, rhs: Self)
}
extension Arithmetic {
  public init() { self = 0 }

  public static prefix func + (x: Self) -> Self {
    return x
  }
}

<Protocol-oriented integers (take 2) · GitHub;
SignedArithmetic

The SignedArithmetic protocol is for numbers that can be negated.

public protocol SignedArithmetic : Arithmetic {
  /// Returns the additive inverse of this value. /// /// let x = 21 /// let y = -x /// // y == -21 /// /// - Returns: The additive inverse of this value. /// /// - SeeAlso: `negate()` static prefix func - (_ operand: Self

I agree that 0 and 1 literals look more expressive in the case of numbers.
Maybe “Arithmetic” name confused me for a bit. I suspected Arithmetic protocol to be more general rather than specific to numbers.

···

On Jan 25, 2017, at 20:00, Max Moiseev <moiseev@apple.com> wrote:

Hi Anton,

Arithmetic refines RepresentableByIntegerLiteral, so it is quite possible to use `0` and `1` when you need them.

Max

On Jan 25, 2017, at 7:59 AM, Anton Mironov <antonvmironov@gmail.com <mailto:antonvmironov@gmail.com>> wrote:

Hi everyone,

I want to suggest a tiny extension to an Arithmetic protocol. It would be nice to have an additive identity and a multiplicative identity constants. Basically zero and one.

protocol Arithmetic {
  /* ... */
  static var zero: Self { get }  // additive identity: (value + .zero) == value
  static var one: Self { get }   // multiplicative identity: (value * .one) == value
}

These constants will ease implementation of math structures: vectors, matrices and etc.
I’m sorry if I’m duplicating someone’s suggestion. It is really hard to search for something in a long thread.

Thanks,
Anton Mironov

On Jan 13, 2017, at 10:47 PM, Max Moiseev via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Hi everyone,

Back in June 2016 we discussed the new design of the integer types for the standard library. It even resulted in acceptance of SE-0104 <https://github.com/apple/swift-evolution/blob/master/proposals/0104-improved-integers.md&gt; for Swift 3. Unfortunately we were not able to implement it in time for the release.

But it was not forgotten, although, as time went by, a few changes needed to be made in order to reflect the current state of the language.
Without further introduction, please welcome the refined proposal to make integers in Swift more suitable for generic programming.

Available in this gist Protocol-oriented integers (take 2) · GitHub and also inlined below.

Max

Protocol-oriented integers (take 2)

Proposal: SE-NNNN <https://gist.github.com/moiseev/0000-even-more-improved-integers.md&gt;
Authors: Dave Abrahams <https://github.com/dabrahams&gt;, Maxim Moiseev <https://github.com/moiseev&gt;
Review Manager: TBD
Status: Awaiting review
Bug: SR-3196 <Issues · apple/swift-issues · GitHub;
Previous Proposal: SE-0104 <https://gist.github.com/moiseev/0104-improved-integers.md&gt;
<Protocol-oriented integers (take 2) · GitHub

This proposal is an evolution of SE-0104 <https://gist.github.com/moiseev/0104-improved-integers.md&gt;\. The goal is still to clean up Swifts integer APIs and make them more useful for generic programming.

The language has evolved in ways that affect integers APIs since the time the original proposal was approved for Swift 3. We also attempted to implement the proposed model in the standard library and found that some essential APIs were missing, whereas others could be safely removed.

Major changes to the APIs introduced by this proposal (as compared to SE-0104 <https://gist.github.com/moiseev/0104-improved-integers.md&gt;\) are listed in a dedicated section <Protocol-oriented integers (take 2) · GitHub.

<Protocol-oriented integers (take 2) · GitHub

Swift's integer protocols don't currently provide a suitable basis for generic programming. See this blog post <http://blog.krzyzanowskim.com/2015/03/01/swift_madness_of_generic_integer/&gt; for an example of an attempt to implement a generic algorithm over integers.

The way the Arithmetic protocol is defined, it does not generalize to floating point numbers and also slows down compilation by requiring every concrete type to provide an implementation of arithmetic operators, thus polluting the overload set.

Converting from one integer type to another is performed using the concept of the 'maximum width integer' (see MaxInt), which is an artificial limitation. The very existence of MaxInt makes it unclear what to do should someone implement Int256, for example.

Another annoying problem is the inability to use integers of different types in comparison and bit-shift operations. For example, the following snippets won't compile:

var x: Int8 = 42
let y = 1
let z = 0

x <<= y // error: binary operator '<<=' cannot be applied to operands of type 'Int8' and 'Int'
if x > z { ... } // error: binary operator '>' cannot be applied to operands of type 'Int8' and 'Int'
Currently, bit-shifting a negative number of (or too many) bits causes a trap on some platforms, which makes low-level bit manipulations needlessly dangerous and unpredictable.

Finally, the current design predates many of the improvements that came since Swift 1, and hasn't been revised since then.

<Protocol-oriented integers (take 2) · GitHub solution

We propose a new model that does not have above mentioned problems and is more easily extensible.

                +--------------+ +-------------+
        +------>+ Arithmetic | | Comparable |
        > > (+,-,*,/) | | (==,<,>,...)|
        > +-------------++ +---+---------+
        > ^ ^
+-------+------------+ | |
> SignedArithmetic | +-+-------+-----------+
> (unary -) | | BinaryInteger |
+------+-------------+ |(words,%,bitwise,...)|
       ^ ++---+-----+----------+
       > +-----------^ ^ ^---------------+
       > > > >
+------+---------++ +---------+---------------+ +--+----------------+
> SignedInteger | | FixedWidthInteger | | UnsignedInteger |
> > >(endianness,overflow,...)| | |
+---------------+-+ +-+--------------------+--+ +-+-----------------+
                ^ ^ ^ ^
                > > > >
                > > > >
               ++--------+-+ +-+-------+-+
               >Int family |-+ |UInt family|-+
               +-----------+ | +-----------+ |
                 +-----------+ +-----------+
There are several benefits provided by this model over the old one:

It allows mixing integer types in generic functions.

The possibility to initialize instances of any concrete integer type with values of any other concrete integer type enables writing functions that operate on more than one type conforming to BinaryInteger, such as heterogeneous comparisons or bit shifts, described later.

It removes the overload resolution overhead.

Arithmetic and bitwise operations can now be defined as generic operators on protocols. This approach significantly reduces the number of overloads for those operations, which used to be defined for every single concrete integer type.

It enables protocol sharing between integer and floating point types.

Note the exclusion of the % operation from Arithmetic. Its behavior for floating point numbers is sufficiently different from the one for integers that using it in generic context would lead to confusion. The FloatingPoint protocol introduced by SE-0067 <https://gist.github.com/moiseev/0067-floating-point-protocols.md&gt; should now refine SignedArithmetic.

It makes future extensions possible.

The proposed model eliminates the 'largest integer type' concept previously used to interoperate between integer types (see toIntMax in the current model) and instead provides access to machine words. It also introduces thedoubleWidthMultiply, doubleWidthDivide, and quotientAndRemainder methods. Together these changes can be used to provide an efficient implementation of bignums that would be hard to achieve otherwise.

The implementation of proposed model in the standard library is available in the new-integer-protocols branch <https://github.com/apple/swift/tree/new-integer-protocols&gt;\.

<Protocol-oriented integers (take 2) · GitHub note on bit shifts

This proposal introduces the concepts of smart shifts and masking shifts.

The semantics of shift operations are often undefined <LLVM Language Reference Manual — LLVM 18.0.0git documentation; in under- or over-shift cases. Smart shifts, implemented by >> and <<, are designed to address this problem and always behave in a well defined way, as shown in the examples below:

x << -2 is equivalent to x >> 2

(1 as UInt8) >> 42) will evaluate to 0

(-128 as Int8) >> 42) will evaluate to 0xff or -1

In most scenarios, the right hand operand is a literal constant, and branches for handling under- and over-shift cases can be optimized away. For other cases, this proposal provides masking shifts, implemented by &>> and &<<. A masking shift logically preprocesses the right hand operand by masking its bits to produce a value in the range 0...(x-1) where x is the number of bits in the left hand operand. On most architectures this masking is already performed by the CPU's shift instructions and has no cost. Both kinds of shift avoid undefined behavior and produce uniform semantics across architectures.

<Protocol-oriented integers (take 2) · GitHub design

<https://gist.github.com/moiseev/62ffe3c91b66866fdebf6f3fcc7cad8c#whats-new-since-se-0104&gt;What&#39;s new since SE-0104

SE-0091 <https://gist.github.com/moiseev/0091-improving-operators-in-protocols.md&gt; removed the necessity to dispatch generic operators through special methods.

All operators are now declared by protocols as static funcs.

Standard Library no longer provides + and - operators for Strideable types.

They were problematic, as one could have written mixed-type code like let x: Int64 = 42; x += (1 as Int), which would compile, but shouldn't. Besides, since the Stride of an unsigned type is signed, Standard Library had to implement a hack to make code like let x: UInt = 42; x += (1 as Int) ambiguous. These operators were only necessary because they made advancing collection indices convenient, which is no longer the case since the introduction of the new indexing model <https://gist.github.com/moiseev/0065-collections-move-indices.md&gt; in Swift 3.

Shifts and other bitwise operations were moved from FixedWidthInteger to BinaryInteger.

Left shift operation on an unbounded integer should infinitely extend the number, and never drop set bits when they reach the most significant position in the underlying representation.

BitwiseOperations protocol was deprecated.

We believe there are no useful entities that support bitwise operations, but at the same time are not binary integers.

minimumSignedRepresentationBitWidth property was removed.

trailingZeros property was added to the BinaryInteger protocol.

leadingZeros and popcount properties are still defined by the FixedWidthInteger protocol.

Endian-converting initializers and properties were added to the FixedWidthInteger protocol.

Standard library introduces the new type DoubleWidth<T>.

See this section <Protocol-oriented integers (take 2) · GitHub; for more details.

<Protocol-oriented integers (take 2) · GitHub

<Protocol-oriented integers (take 2) · GitHub

The Arithmetic protocol declares binary arithmetic operators – such as +, -, and * — and their mutating counterparts.

It provides a suitable basis for arithmetic on scalars such as integers and floating point numbers.

Both mutating and non-mutating operations are declared in the protocol, however only the mutating ones are required, as default implementations of the non-mutating ones are provided by a protocol extension.

The Magnitude associated type is able to hold the absolute value of any possible value of Self. Concrete types do not have to provide a type alias for it, as it can be inferred from the magnitude property. This property can be useful in operations that are simpler to implement in terms of unsigned values, for example, printing a value of an integer, which is just printing a '-' character in front of an absolute value.

Please note that for ordinary work, the magnitude property should not be preferred to the abs(_) function, whose return value is of the same type as its argument.

public protocol Arithmetic : Equatable, ExpressibleByIntegerLiteral {
  /// Creates a new instance from the given integer, if it can be represented
  /// exactly.
  ///
  /// If the value passed as `source` is not representable exactly, the result
  /// is `nil`. In the following example, the constant `x` is successfully
  /// created from a value of `100`, while the attempt to initialize the
  /// constant `y` from `1_000` fails because the `Int8` type can represent
  /// `127` at maximum:
  ///
  /// let x = Int8(exactly: 100)
  /// // x == Optional(100)
  /// let y = Int8(exactly: 1_000)
  /// // y == nil
  ///
  /// - Parameter source: A floating-point value to convert to an integer.
  init?<T : BinaryInteger>(exactly source: T)

  /// A type that can represent the absolute value of any possible value of the
  /// conforming type.
  associatedtype Magnitude : Equatable, ExpressibleByIntegerLiteral

  /// The magnitude of this value.
  ///
  /// For any numeric value `x`, `x.magnitude` is the absolute value of `x`.
  /// You can use the `magnitude` property in operations that are simpler to
  /// implement in terms of unsigned values, such as printing the value of an
  /// integer, which is just printing a '-' character in front of an absolute
  /// value.
  ///
  /// let x = -200
  /// // x.magnitude == 200
  ///
  /// The global `abs(_:)` function provides more familiar syntax when you need
  /// to find an absolute value. In addition, because `abs(_:)` always returns
  /// a value of the same type, even in a generic context, using the function
  /// instead of the `magnitude` property is encouraged.
  ///
  /// - SeeAlso: `abs(_:)`
  var magnitude: Magnitude { get }

  /// Returns the sum of the two given values.
  ///
  /// The sum of `lhs` and `rhs` must be representable in the same type. In the
  /// following example, the result of `100 + 200` is greater than the maximum
  /// representable `Int8` value:
  ///
  /// let x: Int8 = 10 + 21
  /// // x == 31
  /// let y: Int8 = 100 + 121
  /// // Overflow error
  static func +(_ lhs: Self, _ rhs: Self) -> Self

  /// Adds the given value to this value in place.
  ///
  /// For example:
  ///
  /// var x = 15
  /// y += 7
  /// // y == 22
  static func +=(_ lhs: inout Self, rhs: Self)

  /// Returns the difference of the two given values.
  ///
  /// The difference of `lhs` and `rhs` must be representable in the same type.
  /// In the following example, the result of `10 - 21` is less than zero, the
  /// minimum representable `UInt` value:
  ///
  /// let x: UInt = 21 - 10
  /// // x == 11
  /// let y: UInt = 10 - 21
  /// // Overflow error
  static func -(_ lhs: Self, _ rhs: Self) -> Self

  /// Subtracts the given value from this value in place.
  ///
  /// For example:
  ///
  /// var x = 15
  /// y -= 7
  /// // y == 8
  static func -=(_ lhs: inout Self, rhs: Self)

  /// Returns the product of the two given values.
  ///
  /// The product of `lhs` and `rhs` must be representable in the same type. In
  /// the following example, the result of `10 * 50` is greater than the
  /// maximum representable `Int8` value.
  ///
  /// let x: Int8 = 10 * 5
  /// // x == 50
  /// let y: Int8 = 10 * 50
  /// // Overflow error
  static func *(_ lhs: Self, _ rhs: Self) -> Self

  /// Multiples this value by the given value in place.
  ///
  /// For example:
  ///
  /// var x = 15
  /// y *= 7
  /// // y == 105
  static func *=(_ lhs: inout Self, rhs: Self)

  /// Returns the quotient of dividing the first value by the second.
  ///
  /// For integer types, any remainder of the division is discarded.
  ///
  /// let x = 21 / 5
  /// // x == 4
  static func /(_ lhs: Self, _ rhs: Self) -> Self

  /// Divides this value by the given value in place.
  ///
  /// For example:
  ///
  /// var x = 15
  /// y /= 7
  /// // y == 2
  static func /=(_ lhs: inout Self, rhs: Self)
}

extension Arithmetic {
  public init() { self = 0 }

  public static prefix func + (x: Self) -> Self {
    return x
  }
}
<Protocol-oriented integers (take 2) · GitHub

The SignedArithmetic protocol is for numbers that can be negated.

public protocol SignedArithmetic : Arithmetic {
  /// Returns the additive inverse of this value.
  ///
  /// let x = 21
  /// let y = -x
  /// // y == -21
  ///
  /// - Returns: The additive inverse of this value.
  ///
  /// - SeeAlso: `negate()`
  static prefix func - (_ operand: Self) -> Self

  /// Replaces this value with its additive inverse.
  ///
  /// The following example uses the `negate()` method to negate the value of
  /// an integer `x`:
  ///
  /// var x = 21
  /// x.negate()
  /// // x == -21
  ///
  /// - SeeAlso: The unary minus operator (`-`).
  mutating func negate()
}

extension SignedArithmetic {
  public static prefix func - (_ operand: Self) -> Self {
    var result = operand
    result.negate()
    return result
  }

  public mutating func negate() {
    self = Self() - self
  }
}
<Protocol-oriented integers (take 2) · GitHub

The BinaryInteger protocol is the basis for all the integer types provided by the standard library.

This protocol adds a few new initializers. Two of them allow to create integers from floating point numbers, others support construction from instances of any type conforming to BinaryInteger, using different strategies:

Initialize Self with the value, provided that the value is representable. The precondition should be satisfied by the caller.

Extend or truncate the value to fit into Self

Clamp the value to the representable range of Self

BinaryInteger also declares bitwise and shift operators.

public protocol BinaryInteger :
  Comparable, Hashable, Arithmetic, CustomStringConvertible, Strideable {

  /// A Boolean value indicating whether this type is a signed integer type.
  ///
  /// *Signed* integer types can represent both positive and negative values.
  /// *Unsigned* integer types can represent only nonnegative values.
  static var isSigned: Bool { get }

  /// Creates an integer from the given floating-point value, if it can be
  /// represented exactly.
  ///
  /// If the value passed as `source` is not representable exactly, the result
  /// is `nil`. In the following example, the constant `x` is successfully
  /// created from a value of `21.0`, while the attempt to initialize the
  /// constant `y` from `21.5` fails:
  ///
  /// let x = Int(exactly: 21.0)
  /// // x == Optional(21)
  /// let y = Int(exactly: 21.5)
  /// // y == nil
  ///
  /// - Parameter source: A floating-point value to convert to an integer.
  init?<T : FloatingPoint>(exactly source: T)

  /// Creates an integer from the given floating-point value, truncating any
  /// fractional part.
  ///
  /// Truncating the fractional part of `source` is equivalent to rounding
  /// toward zero.
  ///
  /// let x = Int(21.5)
  /// // x == 21
  /// let y = Int(-21.5)
  /// // y == -21
  ///
  /// If `source` is outside the bounds of this type after truncation, a
  /// runtime error may occur.
  ///
  /// let z = UInt(-21.5)
  /// // Error: ...the result would be less than UInt.min
  ///
  /// - Parameter source: A floating-point value to convert to an integer.
  /// `source` must be representable in this type after truncation.
  init<T : FloatingPoint>(_ source: T)

  /// Creates an new instance from the given integer.
  ///
  /// If the value passed as `source` is not representable in this type, a
  /// runtime error may occur.
  ///
  /// let x = -500 as Int
  /// let y = Int32(x)
  /// // y == -500
  ///
  /// // -500 is not representable as a 'UInt32' instance
  /// let z = UInt32(x)
  /// // Error
  ///
  /// - Parameter source: An integer to convert. `source` must be representable
  /// in this type.
  init<T : BinaryInteger>(_ source: T)

  /// Creates a new instance from the bit pattern of the given instance by
  /// sign-extending or truncating to fit this type.
  ///
  /// When the bit width of `T` (the type of `source`) is equal to or greater
  /// than this type's bit width, the result is the truncated
  /// least-significant bits of `source`. For example, when converting a
  /// 16-bit value to an 8-bit type, only the lower 8 bits of `source` are
  /// used.
  ///
  /// let p: Int16 = -500
  /// // 'p' has a binary representation of 11111110_00001100
  /// let q = Int8(extendingOrTruncating: p)
  /// // q == 12
  /// // 'q' has a binary representation of 00001100
  ///
  /// When the bit width of `T` is less than this type's bit width, the result
  /// is *sign-extended* to fill the remaining bits. That is, if `source` is
  /// negative, the result is padded with ones; otherwise, the result is
  /// padded with zeros.
  ///
  /// let u: Int8 = 21
  /// // 'u' has a binary representation of 00010101
  /// let v = Int16(extendingOrTruncating: u)
  /// // v == 21
  /// // 'v' has a binary representation of 00000000_00010101
  ///
  /// let w: Int8 = -21
  /// // 'w' has a binary representation of 11101011
  /// let x = Int16(extendingOrTruncating: w)
  /// // x == -21
  /// // 'x' has a binary representation of 11111111_11101011
  /// let y = UInt16(extendingOrTruncating: w)
  /// // y == 65515
  /// // 'y' has a binary representation of 11111111_11101011
  ///
  /// - Parameter source: An integer to convert to this type.
  init<T : BinaryInteger>(extendingOrTruncating source: T)

  /// Creates a new instance with the representable value that's closest to the
  /// given integer.
  ///
  /// If the value passed as `source` is greater than the maximum representable
  /// value in this type, the result is the type's `max` value. If `source` is
  /// less than the smallest representable value in this type, the result is
  /// the type's `min` value.
  ///
  /// In this example, `x` is initialized as an `Int8` instance by clamping
  /// `500` to the range `-128...127`, and `y` is initialized as a `UInt`
  /// instance by clamping `-500` to the range `0...UInt.max`.
  ///
  /// let x = Int8(clamping: 500)
  /// // x == 127
  /// // x == Int8.max
  ///
  /// let y = UInt(clamping: -500)
  /// // y == 0
  ///
  /// - Parameter source: An integer to convert to this type.
  init<T : BinaryInteger>(clamping source: T)

  /// Returns the n-th word, counting from the least significant to most
  /// significant, of this value's binary representation.
  ///
  /// The `word(at:)` method returns negative values in two's complement
  /// representation, regardless of a type's underlying implementation. If `n`
  /// is greater than the number of words in this value's current
  /// representation, the result is `0` for positive numbers and `~0` for
  /// negative numbers.
  ///
  /// - Parameter n: The word to return, counting from the least significant to
  /// most significant. `n` must be greater than or equal to zero.
  /// - Returns: An word-sized, unsigned integer with the bit pattern of the
  /// n-th word of this value.
  func word(at n: Int) -> UInt

  /// The number of bits in the current binary representation of this value.
  ///
  /// This property is a constant for instances of fixed-width integer
  /// types.
  var bitWidth : Int { get }

  /// The number of trailing zeros in this value's binary representation.
  ///
  /// For example, in a fixed-width integer type with a `bitWidth` value of 8,
  /// the number -8 has three trailing zeros.
  ///
  /// let x = Int8(bitPattern: 0b1111_1000)
  /// // x == -8
  /// // x.trailingZeros == 3
  var trailingZeros: Int { get }

  /// Returns the remainder of dividing the first value by the second.
  ///
  /// The result has the same sign as `lhs` and is less than `rhs.magnitude`.
  ///
  /// let x = 22 % 5
  /// // x == 2
  /// let y = 22 % -5
  /// // y == 2
  /// let z = -22 % -5
  /// // z == -2
  ///
  /// - Parameters:
  /// - lhs: The value to divide.
  /// - rhs: The value to divide `lhs` by. `rhs` must not be zero.
  static func %(_ lhs: Self, _ rhs: Self) -> Self

  /// Replaces this value with the remainder of itself divided by the given
  /// value. For example:
  ///
  /// var x = 15
  /// x %= 7
  /// // x == 1
  ///
  /// - Parameter rhs: The value to divide this value by. `rhs` must not be
  /// zero.
  ///
  /// - SeeAlso: `remainder(dividingBy:)`
  static func %=(_ lhs: inout Self, _ rhs: Self)

  /// Returns the inverse of the bits set in the argument.
  ///
  /// The bitwise NOT operator (`~`) is a prefix operator that returns a value
  /// in which all the bits of its argument are flipped: Bits that are `1` in
  /// the argument are `0` in the result, and bits that are `0` in the argument
  /// are `1` in the result. This is equivalent to the inverse of a set. For
  /// example:
  ///
  /// let x: UInt8 = 5 // 0b00000101
  /// let notX = ~x // 0b11111010
  ///
  /// Performing a bitwise NOT operation on 0 returns a value with every bit
  /// set to `1`.
  ///
  /// let allOnes = ~UInt8.min // 0b11111111
  ///
  /// - Complexity: O(1).
  static prefix func ~ (_ x: Self) -> Self

  /// Returns the result of performing a bitwise AND operation on this value
  /// and the given value.
  ///
  /// A bitwise AND operation results in a value that has each bit set to `1`
  /// where *both* of its arguments have that bit set to `1`. For example:
  ///
  /// let x: UInt8 = 5 // 0b00000101
  /// let y: UInt8 = 14 // 0b00001110
  /// let z = x & y // 0b00000100
  static func &(_ lhs: Self, _ rhs: Self) -> Self
  static func &=(_ lhs: inout Self, _ rhs: Self)

  /// Returns the result of performing a bitwise OR operation on this value and
  /// the given value.
  ///
  /// A bitwise OR operation results in a value that has each bit set to `1`
  /// where *one or both* of its arguments have that bit set to `1`. For
  /// example:
  ///
  /// let x: UInt8 = 5 // 0b00000101
  /// let y: UInt8 = 14 // 0b00001110
  /// let z = x | y // 0b00001111
  static func |(_ lhs: Self, _ rhs: Self) -> Self
  static func |=(_ lhs: inout Self, _ rhs: Self)

  /// Returns the result of performing a bitwise XOR operation on this value
  /// and the given value.
  ///
  /// A bitwise XOR operation, also known as an exclusive OR operation, results
  /// in a value that has each bit set to `1` where *one or the other but not
  /// both* of its arguments had that bit set to `1`. For example:
  ///
  /// let x: UInt8 = 5 // 0b00000101
  /// let y: UInt8 = 14 // 0b00001110
  /// let z = x ^ y // 0b00001011
  static func ^(_ lhs: Self, _ rhs: Self) -> Self
  static func ^=(_ lhs: inout Self, _ rhs: Self)

  /// Returns the result of shifting this value's binary representation the
  /// specified number of digits to the right.
  ///
  /// In a *masking shift*, the bit pattern of the value passed as `rhs` is
  /// masked to produce a value between zero and the bit width of `lhs`. The
  /// shift is performed using this masked value. Masking shifts require more
  /// care to use correctly than a traditional bit shift, but are likely to be
  /// more efficient when used with shift amounts that are not compile-time
  /// constants. On most architectures, a masking shift compiles down to a
  /// single instruction.
  ///
  /// For example, if you shift an 8-bit, unsigned integer by 2, the shift
  /// amount requires no masking.
  ///
  /// let x: UInt8 = 30 // 0b00011110
  /// let y = x &>> 2
  /// // y == 7 // 0b00000111
  ///
  /// However, if you shift it by 11, it first bitmasks `rhs` to `3`, and then
  /// uses that masked value as the number of bits to shift `x`.
  ///
  /// let z = x &>> 11
  /// // z == 3 // 0b00000011
  ///
  /// Relationship to the Right Shift Operator
  /// ----------------------------------------
  ///
  /// The masking right shift operator handles attempted overshifts and
  /// undershifts differently from the right shift operator (`>>`). When the
  /// value passed as `rhs` in a masking shift is within the range
  /// `0...<bitWidth`, the operation is equivalent to using the right shift
  /// operator.
  ///
  /// let x: UInt8 = 30 // 0b00011110
  /// let y1 = x &>> 2
  /// // y1 == 7 // 0b00000111
  /// let y2 = x >> 2
  /// // y2 == 7 // 0b00000111
  ///
  /// The right shift operator does not mask its right-hand-side argument, so
  /// passing `11` as `rhs` shifts all the bits of `x` to zero.
  ///
  /// let z1 = x &>> 11
  /// // z1 == 240 // 0b00000011
  /// let z2 = x >> 11
  /// // z2 == 0 // 0b00000000
  ///
  /// - Parameter rhs: The number of bits to shift this value to the right. If
  /// `rhs` is outside the range `0..<bitWidth`, it is masked to produce a
  /// value within that range.
  /// - Returns: The result of shifting this value by the masked `rhs` to the
  /// right.
  ///
  /// - SeeAlso: `&<<`, `>>`
  static func &>>(_ lhs: Self, _ rhs: Self) -> Self
  static func &>>=(_ lhs: inout Self, _ rhs: Self)

  /// Returns the result of shifting this value's binary representation the
  /// specified number of digits to the left.
  ///
  /// In a *masking shift*, the bit pattern of the value passed as `rhs` is
  /// masked to produce a value between zero and the bit width of `lhs`. The
  /// shift is performed using this masked value. Masking shifts require more
  /// care to use correctly than a traditional bit shift, but are likely to be
  /// more efficient when used with shift amounts that are not compile-time
  /// constants. On most architectures, a masking shift compiles down to a
  /// single instruction.
  ///
  /// For example, if you shift an 8-bit, unsigned integer by 2, the shift
  /// amount requires no masking.
  ///
  /// let x: UInt8 = 30 // 0b00011110
  /// let y = x &>> 2
  /// // y == 120 // 0b01111000
  ///
  /// However, if you shift it by 11, it first bitmasks `rhs` to `3`, and then
  /// uses that masked value as the number of bits to shift `x`.
  ///
  /// let z = x &<< 11
  /// // z == 240 // 0b11110000
  ///
  /// Relationship to the Left Shift Operator
  /// ---------------------------------------
  ///
  /// The masking left shift operator handles attempted overshifts and
  /// undershifts differently from the left shift operator (`<<`). When the
  /// value passed as `rhs` in a masking shift is within the range
  /// `0...<bitWidth`, the operation is equivalent to using the left shift
  /// operator.
  ///
  /// let x: UInt8 = 30 // 0b00011110
  /// let y1 = x &<< 2
  /// // y1 == 120 // 0b01111000
  /// let y2 = x << 2
  /// // y2 == 120 // 0b01111000
  ///
  /// The left shift operator does not mask its right-hand-side argument, so
  /// passing `11` as `rhs` shifts all the bits of `x` to zero.
  ///
  /// let z1 = x &<< 11
  /// // z1 == 240 // 0b11110000
  /// let z2 = x << 11
  /// // z2 == 0 // 0b00000000
  ///
  /// - Parameter rhs: The number of bits to shift this value to the left. If
  /// `rhs` is outside the range `0..<bitWidth`, it is masked to produce a
  /// value within that range.
  /// - Returns: The result of shifting this value by the masked `rhs` to the
  /// left.
  ///
  /// - SeeAlso: `&>>`, `<<`
  static func &<<(_ lhs: Self, _ rhs: Self) -> Self
  static func &<<=(_ lhs: inout Self, _ rhs: Self)

  /// Returns the quotient and remainder of this value divided by the given
  /// value.
  ///
  /// Use this method to calculate the quotient and remainder of a division at
  /// the same time.
  ///
  /// let x = 1_000_000
  /// let (q, r) = x.quotientAndRemainder(dividingBy: 933)
  /// // q == 1071
  /// // r == 757
  ///
  /// - Parameter rhs: The value to divide this value by.
  /// - Returns: A tuple containing the quotient and remainder of this value
  /// divided by `rhs`.
  func quotientAndRemainder(dividingBy rhs: Self)
    -> (quotient: Self, remainder: Self)

  /// Returns `-1` if this value is negative and `1` if it's positive;
  /// otherwise, `0`.
  ///
  /// - Returns: The sign of this number, expressed as an integer of the same
  /// type.
  func signum() -> Self
}
<Protocol-oriented integers (take 2) · GitHub

The FixedWidthInteger protocol adds the notion of endianness as well as static properties for type bounds and bit width.

The WithOverflow family of methods is used in default implementations of mutating arithmetic methods (see the Arithmetic protocol). Having these methods allows the library to provide both bounds-checked and masking implementations of arithmetic operations, without duplicating code.

The doubleWidthMultiply and doubleWidthDivide methods are necessary building blocks to implement support for integer types of a greater width such as arbitrary-precision integers.

public protocol FixedWidthInteger : BinaryInteger {
  /// The number of bits used for the underlying binary representation of
  /// values of this type.
  ///
  /// An unsigned, fixed-width integer type can represent values from 0 through
  /// `(2 ** bitWidth) - 1`, where `**` is exponentiation. A signed,
  /// fixed-width integer type can represent values from
  /// `-(2 ** bitWidth - 1)` through `(2 ** bitWidth - 1) - 1`. For example,
  /// the `Int8` type has a `bitWidth` value of 8 and can store any integer in
  /// the range `-128...127`.
  static var bitWidth : Int { get }

  /// The maximum representable integer in this type.
  ///
  /// For unsigned integer types, this value is `(2 ** bitWidth) - 1`, where
  /// `**` is exponentiation. For signed integer types, this value is
  /// `(2 ** bitWidth - 1) - 1`.
  static var max: Self { get }

  /// The minimum representable value.
  ///
  /// For unsigned integer types, this value is always `0`. For signed integer
  /// types, this value is `-(2 ** bitWidth - 1)`, where `**` is
  /// exponentiation.
  static var min: Self { get }

  /// Returns the sum of this value and the given value along with a flag
  /// indicating whether overflow occurred in the operation.
  ///
  /// - Parameter other: The value to add to this value.
  /// - Returns: A tuple containing the result of the addition along with a
  /// flag indicating whether overflow occurred. If the `overflow` component
  /// is `.none`, the `partialValue` component contains the entire sum. If
  /// the `overflow` component is `.overflow`, an overflow occurred and the
  /// `partialValue` component contains the truncated sum of this value and
  /// `other`.
  ///
  /// - SeeAlso: `+`
  func addingWithOverflow(_ other: Self)
    -> (partialValue: Self, overflow: ArithmeticOverflow)

  /// Returns the difference of this value and the given value along with a
  /// flag indicating whether overflow occurred in the operation.
  ///
  /// - Parameter other: The value to subtract from this value.
  /// - Returns: A tuple containing the result of the subtraction along with a
  /// flag indicating whether overflow occurred. If the `overflow` component
  /// is `.none`, the `partialValue` component contains the entire
  /// difference. If the `overflow` component is `.overflow`, an overflow
  /// occurred and the `partialValue` component contains the truncated
  /// result of `other` subtracted from this value.
  ///
  /// - SeeAlso: `-`
  func subtractingWithOverflow(_ other: Self)
    -> (partialValue: Self, overflow: ArithmeticOverflow)

  /// Returns the product of this value and the given value along with a flag
  /// indicating whether overflow occurred in the operation.
  ///
  /// - Parameter other: The value to multiply by this value.
  /// - Returns: A tuple containing the result of the multiplication along with
  /// a flag indicating whether overflow occurred. If the `overflow`
  /// component is `.none`, the `partialValue` component contains the entire
  /// product. If the `overflow` component is `.overflow`, an overflow
  /// occurred and the `partialValue` component contains the truncated
  /// product of this value and `other`.
  ///
  /// - SeeAlso: `*`, `doubleWidthMultiply(_:_:)`
  func multipliedWithOverflow(by other: Self)
    -> (partialValue: Self, overflow: ArithmeticOverflow)

  /// Returns the quotient of dividing this value by the given value along with
  /// a flag indicating whether overflow occurred in the operation.
  ///
  /// For a value `x`, if zero is passed as `other`, the result is
  /// `(x, .overflow)`.
  ///
  /// - Parameter other: The value to divide this value by.
  /// - Returns: A tuple containing the result of the division along with a
  /// flag indicating whether overflow occurred. If the `overflow` component
  /// is `.none`, the `partialValue` component contains the entire quotient.
  /// If the `overflow` component is `.overflow`, an overflow occurred and
  /// the `partialValue` component contains the truncated quotient.
  ///
  /// - SeeAlso: `/`, `doubleWidthDivide(_:_:)`
  func dividedWithOverflow(by other: Self)
    -> (partialValue: Self, overflow: ArithmeticOverflow)

  /// Returns a tuple containing the high and low parts of the result of
  /// multiplying its arguments.
  ///
  /// Use this method to calculate the full result of a product that would
  /// otherwise overflow. Unlike traditional truncating multiplication, the
  /// `doubleWidthMultiply(_:_:)` method returns both the `high` and `low`
  /// parts of the product of `lhs` and `rhs`. The following example uses this
  /// method to multiply two `UInt8` values that normally overflow when
  /// multiplied:
  ///
  /// let x: UInt8 = 100
  /// let y: UInt8 = 20
  /// let result = UInt8.doubleWidthMultiply(100, 20)
  /// // result.high == 0b00000111
  /// // result.low == 0b11010000
  ///
  /// The product of `x` and `y` is 2000, which is too large to represent in a
  /// `UInt8` instance. The `high` and `low` components of the `result` tuple
  /// represent 2000 when concatenated to form a double-width integer; that
  /// is, using `result.high` as the high byte and `result.low` as the low byte
  /// of a `UInt16` instance.
  ///
  /// let z = UInt16(result.high) << 8 | UInt16(result.low)
  /// // z == 2000
  ///
  /// - Parameters:
  /// - lhs: A value to multiply.
  /// - rhs: Another value to multiply.
  /// - Returns: A tuple containing the high and low parts of the result of
  /// multiplying `lhs` and `rhs`.
  ///
  /// - SeeAlso: `multipliedWithOverflow(by:)`
  static func doubleWidthMultiply(_ lhs: Self, _ rhs: Self)
    -> (high: Self, low: Magnitude)

  /// Returns a tuple containing the quotient and remainder of dividing the
  /// first argument by the second.
  ///
  /// The resulting quotient must be representable within the bounds of the
  /// type. If the quotient of dividing `lhs` by `rhs` is too large to
  /// represent in the type, a runtime error may occur.
  ///
  /// - Parameters:
  /// - lhs: A tuple containing the high and low parts of a double-width
  /// integer. The `high` component of the tuple carries the sign, if the
  /// type is signed.
  /// - rhs: The integer to divide into `lhs`.
  /// - Returns: A tuple containing the quotient and remainder of `lhs` divided
  /// by `rhs`.
  static func doubleWidthDivide(
    _ lhs: (high: Self, low: Magnitude), _ rhs: Self)
    -> (quotient: Self, remainder: Self)

  /// The number of bits equal to 1 in this value's binary representation.
  ///
  /// For example, in a fixed-width integer type with a `bitWidth` value of 8,
  /// the number 31 has five bits equal to 1.
  ///
  /// let x: Int8 = 0b0001_1111
  /// // x == 31
  /// // x.popcount == 5
  var popcount: Int { get }

  /// The number of leading zeros in this value's binary representation.
  ///
  /// For example, in a fixed-width integer type with a `bitWidth` value of 8,
  /// the number 31 has three leading zeros.
  ///
  /// let x: Int8 = 0b0001_1111
  /// // x == 31
  /// // x.leadingZeros == 3
  /// - SeeAlso: `BinaryInteger.trailingZeros`
  var leadingZeros: Int { get }

  /// Creates an integer from its big-endian representation, changing the
  /// byte order if necessary.
  init(bigEndian value: Self)

  /// Creates an integer from its little-endian representation, changing the
  /// byte order if necessary.
  init(littleEndian value: Self)

  /// The big-endian representation of this integer.
  ///
  /// If necessary, the byte order of this value is reversed from the typical
  /// byte order of this integer type. On a big-endian platform, for any
  /// integer `x`, `x == x.bigEndian`.
  ///
  /// - SeeAlso: `littleEndian`
  var bigEndian: Self { get }

  /// The little-endian representation of this integer.
  ///
  /// If necessary, the byte order of this value is reversed from the typical
  /// byte order of this integer type. On a little-endian platform, for any
  /// integer `x`, `x == x.littleEndian`.
  ///
  /// - SeeAlso: `bigEndian`
  var littleEndian: Self { get }

  /// A representation of this integer with the byte order swapped.
  var byteSwapped: Self { get }
}
<Protocol-oriented integers (take 2) · GitHub protocols

public protocol UnsignedInteger : BinaryInteger {
  associatedtype Magnitude : BinaryInteger
}
public protocol SignedInteger : BinaryInteger, SignedArithmetic {
  associatedtype Magnitude : BinaryInteger
}
<Protocol-oriented integers (take 2) · GitHub

The DoubleWidth<T> type allows to create wider fixed-width integer types from the ones available in the standard library.

Standard library currently provides fixed-width integer types of up to 64 bits. A value of DoubleWidth<Int64> will double the range of the underlying type and implement all the FixedWidthInteger requirements. Please note though that the implementation will not necessarily be the most efficient one, so it would not be a good idea to use DoubleWidth<Int32>instead of a built-in Int64.

<Protocol-oriented integers (take 2) · GitHub operators

In addition to the operators described in the protocols section <Protocol-oriented integers (take 2) · GitHub, we also provide a few extensions that are not protocol requirements:

<Protocol-oriented integers (take 2) · GitHub shifts

extension BinaryInteger {
  // Masking shifts
  static func &>> <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Self
  static func &>>= <Other : BinaryInteger>(lhs: inout Self, rhs: Other)
  static func &<< <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Self
  static func &<<= <Other : BinaryInteger>(lhs: inout Self, rhs: Other)

  // 'Smart' shifts
  static func >> <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Self
  static func >>= <Other : BinaryInteger>(lhs: inout Self, rhs: Other)
  static func << <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Self
  static func <<= <Other : BinaryInteger>(lhs: inout Self, rhs: Other)
}
<Protocol-oriented integers (take 2) · GitHub equality and comparison

extension BinaryInteger {
  // Equality
  static func == <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Bool
  static func != <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Bool

  // Comparison
  static func < <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Bool
  static func <= <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Bool
  static func > <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Bool
  static func >= <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Bool
}
<Protocol-oriented integers (take 2) · GitHub arithmetic

public func &* <T: FixedWidthInteger>(lhs: T, rhs: T) -> T
public func &- <T: FixedWidthInteger>(lhs: T, rhs: T) -> T
public func &+ <T: FixedWidthInteger>(lhs: T, rhs: T) -> T
<Protocol-oriented integers (take 2) · GitHub

This proposal:

DOES NOT solve the integer promotion problem, which would allow mixed-type arithmetic. However, we believe that it is an important step in the right direction.

DOES NOT include the implementation of a BigInt type, but allows it to be implemented in the future.

<Protocol-oriented integers (take 2) · GitHub compatibility

The proposed change is designed to be as non-breaking as possible, and it has been proven that it does not break code on concrete integer types. However, there are still a few API breaking changes in the realm of generic code:

Integer protocols in Swift up to and including version 3 were not particularly useful for generic programming, but were rather a means of sharing implementation between conforming types. Therefore we believe the amount of code that relied on these protocols is relatively small. The breakage can be further reduced by introducing proper aliases for the removed protocols with deprecation warnings.

Deprecation of the BitwiseOperations protocol. We find it hard to imagine a type that conforms to this protocol, but is not a binary integer type.

Addition of 'smart' shifts will change the behavior of existing code. It will still compile, but will be potentially less performant due to extra logic involved. In a case, where this becomes a problem, newly introduced masking shift operators can be used instead. Unfortunately, performance characteristics of the code cannot be statically checked, and thus migration cannot be provided.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

I wish I knew how to express those requirements in the type system.
This is a good way to implement additiveIdentity but I assume that Arithmetic protocol implies behavior of Addable.

···

On Jan 25, 2017, at 20:58, David Sweeris <davesweeris@mac.com> wrote:

On Jan 25, 2017, at 07:59, Anton Mironov via swift-evolution <swift-evolution@swift.org> wrote:

Hi everyone,

I want to suggest a tiny extension to an Arithmetic protocol. It would be nice to have an additive identity and a multiplicative identity constants. Basically zero and one.

protocol Arithmetic {
 /* ... */
 static var zero: Self { get }  // additive identity: (value + .zero) == value
 static var one: Self { get }   // multiplicative identity: (value * .one) == value
}

These constants will ease implementation of math structures: vectors, matrices and etc.
I’m sorry if I’m duplicating someone’s suggestion. It is really hard to search for something in a long thread.

Vectors, matrices, etc can't conform to this protocol anyway because of the * and / requirements. And while it is true that it's not uncommon to reference an all-zero matrix or vector as "0", that doesn't work for any other number... for them I think the spelling should be "additiveIdentity". It'll be easy enough to just say
protocol Addable {
   static var additiveIdentity: Self {get}
   ...
}
extension Addable where Self: Arithmetic {
   static var additiveIdentity: Self {return Self.zero}
}

IMHO, anyway.

- Dave Sweeris

>
> Protocol-oriented integers (take 2)
>
> • Proposal: SE-NNNN
> • Authors: Dave Abrahams, Maxim Moiseev
> • Review Manager: TBD
> • Status: Awaiting review
> • Bug: SR-3196
> • Previous Proposal: SE-0104

Definitely liking what I'm seeing here.

> public protocol Arithmetic : Equatable, ExpressibleByIntegerLiteral
> {

Is there a reason `Arithmetic` is not `Hashable`? (I think `Comparable`
isn't here because complex numbers can't be compared, but correct me if I'm
wrong about that.)

> /// A type that can represent the absolute value of any possible value
of the
> /// conforming type.
> associatedtype Magnitude : Equatable, ExpressibleByIntegerLiteral

Is there a reason not to have this be `Arithmetic`? Maybe just the
circularity problem?

> /// Returns the n-th word, counting from the least significant to most
> /// significant, of this value's binary representation.
> ///
> /// The `word(at:)` method returns negative values in two's complement
> /// representation, regardless of a type's underlying implementation.
If `n`
> /// is greater than the number of words in this value's current
> /// representation, the result is `0` for positive numbers and `~0` for
> /// negative numbers.
> ///
> /// - Parameter n: The word to return, counting from the least
significant to
> /// most significant. `n` must be greater than or equal to zero.
> /// - Returns: An word-sized, unsigned integer with the bit pattern of
the
> /// n-th word of this value.
> func word(at n: Int) -> UInt

How does the client know how many words there are? Are they supposed to
calculate that from `bitWidth`?

Oh, I see:

> good catch; countRepresentedWords is in the prototype
> (https://github.com/apple/swift/blob/new-integer-
protocols/stdlib/public/core/Integers.swift.gyb#L1521),
> and it should be in the proposal.

That looks fine to me.

> /// The number of bits in the current binary representation of this
value.
> ///
> /// This property is a constant for instances of fixed-width integer
> /// types.
> var bitWidth : Int { get }

So, um, I'm a little confused about this one. Is this the physical number
of bits in the value, or is it the number of bits you need to get from
`word(at:)` in order to get all bits representing the value?

For instance, when you call `UInt32.bitWidth`, does it return `32`, the
physical number of bits in the value, or `33`, the number of bits including
the (always zero) sign bit?

Could you comment on what use cases would make `33` the relevant answer?
I'd always thought that "the number of bits in the current binary
representation" admits of no alternative interpretation than `32` in your
example.

> static func doubleWidthMultiply(_ lhs: Self, _ rhs: Self) -> (high:
Self, low: Magnitude)
> static func doubleWidthDivide(_ lhs: (high: Self, low: Magnitude), _
rhs: Self) -> (quotient: Self, remainder: Self)

Could these take/return a single `DoubleWidth<Self>` value instead of two
separate `Self` and `Magnitude` values? Or would that present circularity
problems?

Having mulled the idea of implementing an IEEE Decimal type, I'd be sad to
see these return DoubleWidth<Self>. Double-width multiply as it is here is
useful when you want to get the result and immediately discard either the
high or the low bits, for instance. If you'd want a result of type
`DoubleWidth<Foo>` instead, you could always just write
`DoubleWidth<Foo>(a) * DoubleWidth<Foo>(b)`.

> /// The number of bits equal to 1 in this value's binary representation.
> ///
> /// For example, in a fixed-width integer type with a `bitWidth` value
of 8,
> /// the number 31 has five bits equal to 1.
> ///
> /// let x: Int8 = 0b0001_1111
> /// // x == 31
> /// // x.popcount == 5
> var popcount: Int { get }

I'm not super-fond of this name; I assume it's a term of art, but it's a
pretty obscure one. Maybe `numberOfOnes`? `onesWithin`?

Far less obscure than `ulp` in `FloatingPoint`!

I think `popcount` is by far the most appropriate name here. It uses
`count`, which is rather consistent with existing Swift APIs, and it is a
visually recognizable term of art (note the absence of capitalization on
"count"). I think a useful barometer for the appropriateness of a term of
art is three-pronged:

* People who know of this concept (the population count of a binary
integer) will almost invariably know it as `popcount`. (Really, if you're
working with binary integers as a collection of bits, you will likely
encounter the term if you look for useful algorithms).

* Those who do not can immediately get all the information they need by
typing this term into Google or Wikipedia, because it needs no
disambiguation.

* It is hard to express the concept more accurately using an alternative
name. `hammingWeight`, `populationCount` are move verbose; `hammingWeight`
would be the more general term, but loses the word "count" and doesn't
_specifically_ communicate the fact that property has to do with the
integer's _binary_ representation. `populationCount` is needlessly wordy
and still a term of art. `countOfOnes` is unfamiliar to people who know
what a popcount is, and no more enlightening to someone who doesn't know
what a popcount is, as it fails to communicate the binary
representation-ness directly.

···

On Sun, Jan 29, 2017 at 6:02 AM, Brent Royal-Gordon via swift-evolution < swift-evolution@swift.org> wrote:

> On Jan 13, 2017, at 12:47 PM, Max Moiseev via swift-evolution < > swift-evolution@swift.org> wrote:

> DoubleWidth
>
> The DoubleWidth<T> type allows to create wider fixed-width integer types
from the ones available in the standard library.

I'm glad you're planning to include `DoubleWidth` this time.

> • Deprecation of the BitwiseOperations protocol. We find it hard
to imagine a type that conforms to this protocol, but is not a binary
integer type.

While I'm sure any such type *could* be a binary integer type, I'm not
convinced they necessary *should* be. For instance, suppose I have a "bit
vector"; I know I never want to perform arithmetic on it, but I *do* want
to manipulate bits separately, so I make it look like a
`RandomAccessCollection` of `Bool`s. It might make a great deal of sense to
support bitwise operations on this type, even though I don't want to
clutter it up with arithmetic.

--
Brent Royal-Gordon
Architechies

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Hmm, interesting. I might be tempted to use a 40-bit type for large arrays,
but the performance hit for any useful computation over a large array would
probably tilt heavily in favor of plain 64-bit integers. What's your use
case for such a 40-bit type? And is it common enough to justify such a
facility in the stdlib vs. providing the tools to build it yourself?

···

On Sun, Jan 29, 2017 at 12:29 David Sweeris via swift-evolution < swift-evolution@swift.org> wrote:

On Jan 13, 2017, at 12:47, Max Moiseev via swift-evolution < > swift-evolution@swift.org> wrote:

DoubleWidth

The DoubleWidth<T> type allows to create wider fixed-width integer types
from the ones available in the standard library.

Standard library currently provides fixed-width integer types of up to 64
bits. A value of DoubleWidth<Int64> will double the range of the
underlying type and implement all the FixedWidthInteger requirements. *Please
note* though that the implementation will not necessarily be the most
efficient one, so it would not be a good idea to use DoubleWidth<Int32>instead
of a built-in Int64.

What about having a "CompoundBinaryInteger<T,U>" type, and then do
"typealias DoubleWidth<T> = CompoundBinaryInteger<T,T>"? That would make it
easier to have, say, 40-bit ints, "CompoundBinaryInteger<Int8,Int32>",
while still having the convenience of "DoubleWidth<T>".

- Dave Sweeris
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Protocol-oriented integers (take 2)

  • Proposal: SE-NNNN
  • Authors: Dave Abrahams, Maxim Moiseev
  • Review Manager: TBD
  • Status: Awaiting review
  • Bug: SR-3196
  • Previous Proposal: SE-0104

Definitely liking what I'm seeing here.

public protocol Arithmetic : Equatable, ExpressibleByIntegerLiteral
{

Is there a reason `Arithmetic` is not `Hashable`?

Just that it's an orthogonal concern. I think it would make a lot of
sense to make all BinaryIntegers hashable, since we can provide a
default implementation that simply hash-combines words.

(I think `Comparable` isn't here because complex numbers can't be
compared, but correct me if I'm wrong about that.)

That's right.

/// A type that can represent the absolute value of any possible value of the
  /// conforming type.
  associatedtype Magnitude : Equatable, ExpressibleByIntegerLiteral

Is there a reason not to have this be `Arithmetic`? Maybe just the
circularity problem?

Yes, that, at least.

  /// Returns the n-th word, counting from the least significant to most
  /// significant, of this value's binary representation.
  ///
  /// The `word(at:)` method returns negative values in two's complement
  /// representation, regardless of a type's underlying implementation. If `n`
  /// is greater than the number of words in this value's current
  /// representation, the result is `0` for positive numbers and `~0` for
  /// negative numbers.
  ///
  /// - Parameter n: The word to return, counting from the least significant to
  /// most significant. `n` must be greater than or equal to zero.
  /// - Returns: An word-sized, unsigned integer with the bit pattern of the
  /// n-th word of this value.
  func word(at n: Int) -> UInt

How does the client know how many words there are? Are they supposed to calculate that from
`bitWidth`?

Oh, I see:

good catch; countRepresentedWords is in the prototype

(https://github.com/apple/swift/blob/new-integer-protocols/stdlib/public/core/Integers.swift.gyb#L1521\),

and it should be in the proposal.

That looks fine to me.

/// The number of bits in the current binary representation of this value.
  ///
  /// This property is a constant for instances of fixed-width integer
  /// types.
  var bitWidth : Int { get }

So, um, I'm a little confused about this one. Is this the physical
number of bits in the value, or is it the number of bits you need to
get from `word(at:)` in order to get all bits representing the value?

For instance, when you call `UInt32.bitWidth`, does it return `32`,
the physical number of bits in the value, or `33`, the number of bits
including the (always zero) sign bit?

I think we recently sorted this out with Nate Cook, who was going to
write some new text for it. If that's not the case, Nate, please
correct me.

  static func doubleWidthMultiply(_ lhs: Self, _ rhs: Self) -> (high: Self, low: Magnitude)
  static func doubleWidthDivide(_ lhs: (high: Self, low: Magnitude),
_ rhs: Self) -> (quotient: Self, remainder: Self)

Could these take/return a single `DoubleWidth<Self>` value instead of
two separate `Self` and `Magnitude` values? Or would that present
circularity problems?

It might work; we should investigate. When this was originally
designed, we weren't proposing to include DoubleWidth.

  /// The number of bits equal to 1 in this value's binary representation.
  ///
  /// For example, in a fixed-width integer type with a `bitWidth` value of 8,
  /// the number 31 has five bits equal to 1.
  ///
  /// let x: Int8 = 0b0001_1111
  /// // x == 31
  /// // x.popcount == 5
  var popcount: Int { get }

I'm not super-fond of this name; I assume it's a term of art, but it's
a pretty obscure one. Maybe `numberOfOnes`? `onesWithin`?

Yes, the rationale is that it's a term of art. I think if you were
going to call it something else, you'd need to mention "one _bits_" in
the name. If we can come up with a name that's obviously better, that's
great, but lacking a clear winner we should go with the already-accepted
term.

DoubleWidth

The DoubleWidth<T> type allows to create wider fixed-width integer
types from the ones available in the standard library.

I'm glad you're planning to include `DoubleWidth` this time.

  • Deprecation of the BitwiseOperations protocol. We find it
hard to imagine a type that conforms to this protocol, but is not a
binary integer type.

While I'm sure any such type *could* be a binary integer type, I'm not
convinced they necessary *should* be. For instance, suppose I have a
"bit vector"; I know I never want to perform arithmetic on it, but I
*do* want to manipulate bits separately, so I make it look like a
`RandomAccessCollection` of `Bool`s.

OK, so that's a collection of Bools. It doesn't matter that it's
*implemented* with one bit per Bool.

It might make a great deal of sense to support bitwise operations on
this type,

I think that's a model of SetAlgebra, then, isn't it?

even though I don't want to clutter it up with arithmetic.

The question is, what algorithm wants to operate generically on a model
of BitwiseOperations? If we can't imagine one, it doesn't deserve its
own protocol.

Personally I don't think the strict separation of SetAlgebra and things
that do bitwise operations makes sense, but I know some people feel
strongly that it would be confusing for users to expose set operations
with bitwise operator names. IMO using | for union and & for
intersection would be beautiful. But that's a story for another day...

···

on Sun Jan 29 2017, Brent Royal-Gordon <swift-evolution@swift.org> wrote:

On Jan 13, 2017, at 12:47 PM, Max Moiseev via swift-evolution <swift-evolution@swift.org> wrote:

--
-Dave

Hi Ben,

The reason to include init?<T : BinaryInteger>(exactly:T) as a requirement for the Arithmetic (now Number) protocol was that it already refines ExpressibleByIntegerLiteral, so it would be quite weird to *not* allow explicit initialization.

Generic conversion to floating point types is not covered by this proposal, but is somewhat related:
https://github.com/apple/swift/blob/master/stdlib/public/core/FloatingPoint.swift.gyb#L257
https://github.com/apple/swift/blob/master/stdlib/public/core/FloatingPoint.swift.gyb#L1588
There are a few unimplemented (but planned) initializers on FloatingPoint and BinaryFloatingPoint that would make this happen.
If and when those are implemented, you would be able to write the version of numericCast<I : BinaryInteger, F : BinaryFloatingPoint>() using those.

Hope this answers your question.

Max

···

On Jan 17, 2017, at 9:43 AM, Ben Rimmington <me@benrimmington.com> wrote:

<https://gist.github.com/moiseev/62ffe3c91b66866fdebf6f3fcc7cad8c&gt;

  public protocol Arithmetic {
    init?<T : BinaryInteger>(exactly source: T)
  }

  public protocol BinaryInteger : Arithmetic {
    init?<T : FloatingPoint>(exactly source: T)
  }

Should the `init?(exactly:)` initializers belong to the same protocol?
Would this allow generic conversions between integer and floating-point types?
For example, a failable `numericCast` function.

-- Ben

Thanks, that's very helpful. Yes, my question was more directed to those
situations that can't be optimized away. It's good to know that the maximum
total cost is an and and a shift, which is what it sounded like but wasn't
made explicit.

···

On Fri, Jan 13, 2017 at 17:25 Stephen Canon <scanon@apple.com> wrote:

> On Jan 13, 2017, at 5:14 PM, Xiaodi Wu via swift-evolution < > swift-evolution@swift.org> wrote:
>
> [Resending to list with original message removed for length.]
>
> This is fantastic. Glad to see it take shape. Very neat and insightful
to have trailingZeros on BinaryInteger but leadingZeros on
FixedWidthInteger. A couple of questions on smart shifts: What is the
performance penalty as compared to the existing operators? Beyond
performance, what are the implications for migrating existing bit twiddling
algorithms written in Swift 3?

Hi Xiaodi —

I don’t want to speak for Max and Dave, but I think I can provide some
insight for your questions about shifts.

First, the overwhelming majority of shifts have a compile-time-constant
RHS. For these cases, there’s no performance change (except that the smart
shifts may be able to optimize away the entire expression if the shift is
overlarge).

For smart shifts with non-constant right-hand sides, the compiler will
frequently still be able to prove that the shift count is always positive
or negative and less than word size (this handles a lot of the most common
cases like normalizing an integer, reading from a bitstream, or shifting
words of a bignum); again there’s no performance penalty.

In the remaining cases where the compiler cannot bound the right-hand
side, there will be some branches present; there may be a few regressions
from these cases, but I expect most to be small (and the code
simplifications are well worth it). Users can always use the masking
shifts, which lower to single instructions for 32b and 64b integers types
on Intel and arm64, and which are at worst an and and a shift (two very
cheap instructions) on other architectures.

Basically, I expect there to be no perf impact for almost all code, and
that the perf impact is relatively easily mitigated when it does occur.
This is an easy tradeoff for fully-defined and sensible operator behavior.

– Steve

Responding to both Jacob and Xiaodi here; thanks very much for your
feedback!

Great work, all. I'm not sure I ever commented on SE-0104, so I've read
through this one more carefully. Here are some things that came to mind:

*## Arithmetic*

Why are ExpressibleByIntegerLiteral and init?<T:BinaryInteger>(exactly:)
required? I could understand needing access to 0, but this could be
provided by a static property or nullary initializer. It doesn't seem like
all types supporting arithmetic operations would necessarily be convertible
from an arbitrary binary integer.

I tried to evaluate the Arithmetic protocol by considering what it means
for higher-dimensional types such as CGPoint and CGVector. My use case was
a linear interpolation function:

    func lerp<T: Arithmetic>(from a: T, to b: T, by ratio: T) -> T {
        return a + (b - a) * ratio
    }

If I've read the proposal correctly, this definition works for integer and
floating-point values. But we can't make it work properly for CGVector (or,
perhaps less mathematically correct but more familiar, CGPoint). It's okay
to define +(CGVector, CGVector) and -(CGVector, CGVector), but *(CGVector,
CGVector) and /(CGVector, CGVector) don't make much sense. What we really
want is *(CGVector, *CGFloat*) and /(CGVector, *CGFloat*).

After consulting a mathematician, I believe what the lerp function really
wants is for its generic param to be an affine space
<https://en.wikipedia.org/wiki/Affine_space&gt;\. I explored this a bit here:
https://gist.github.com/jtbandes/93eeb7d5eee8e1a7245387c660d53b
03#file-affine-swift-L16-L18

This approach is less restrictive and more composable than the proposed
Arithmetic protocol, which can be viewed as a special case with the
following definitions:

    extension Arithmetic: AffineSpace, VectorSpace { // not actually
allowed in Swift
        typealias Displacement = Self
        typealias Scalar = Self
    }

It'd be great to be able to define a lerp() which works for all
floating-point and integer numbers, as well as points and vectors (assuming
a glorious future where CGPoint and CGVector have built-in arithmetic
operations). But maybe the complexity of these extra protocols isn't worth
it for the stdlib...

I think, in the end, it's the _name_ that could use improvement here. As
the doc comments say, `Arithmetic` is supposed to provide a "suitable basis
for arithmetic on scalars"--perhaps `ScalarArithmetic` might be more
appropriate? It would make it clear that `CGVector` is not meant to be a
conforming type.

We want Arithmetic to be able to handle complex numbers. Whether Scalar
would be appropriate in that case sort of depends on what the implied
field is, right?

It's true that CGPoint and CGVector have no obvious sensible
interpretation of "42", and that's unfortunate. The problem with
protocols for algebraic structures is that there's an incredibly
complicated lattice (see figures 3.1, 3.2 in
ftp://jcmc.indiana.edu/pub/techreports/TR638.pdf) and we don't want to
shove all of those protocols into the standard library (especially not
prematurely) but each requirement you add to a more-coarsely aggregated
protocol like Arithmetic will make it ineligible for representing some
important type.

That said, the ability to interpret integer literals as an arbitrary
Arithmetic isn't used anywhere in the standard library, so I'd like to
consider undoing

and moving ExpressibleByIntegerLiteral down the protocol hierarchy to
BinaryInteger.

*## BinaryInteger*

I'm a little confused by the presence of init(extendingOrTruncating:) for
*all* binary integers. Why does it make sense to be able to write
UInt16(extendingOrTruncating: (-21 as Int8)) ? In my mind, sign-extension
only has meaning when the source and destination types are both signed.

Here (just IMHO), I disagree. Since any binary integer can be truncated and
any can be right-shifted, it makes sense for `init(extendingOrTruncating:)`
to be available for all of them. If I understand the proposal correctly,
`Int(extendingOrTruncating: (-1 as Int8))` would give you a different
result than `Int(extendingOrTruncating: (255 as UInt8)`.

Yes it would. But the real justification for this operation is generic
programming with integers. It isn't possible, today, to define:

  init<T:BinaryInteger>(extending:T) where T.isNarrowerThan(Self)
  init<T:BinaryInteger>(truncating:T) where T.isWiderThan(Self)

Although I would not be against a clamp() function in the standard library,
"init(clamping:)" sounds strange to me. What about calling it
"init(nearestTo:)"? One could also define a FloatingPoint version of
init(nearestTo:), i.e. lround(). For maximum annoyance and explicitness,
you could even rename the existing init(_:) to init(truncating:) to make it
clear that truncation, not regular rounding, occurs when converting from
floating-point.

I would disagree with that as well; the existing `init(_:)` truncates the
fractional part but errors if that value is outside the representable
range, while the word "truncating" makes it sound like something is done to
make any possible source value give you a destination value (a la
"extendingOrTruncating" for integers).

Meanwhile, "nearest to" is problematic for me because either 127 and 129 is
"nearest to" 128, and neither integer is "nearest to" 500, yet
`Int8(clamping: 128)` and `Int8(clamping: 500)` both give you 127. This
operation is very different from lround() for floating point, which in
Swift is `rounded(.toNearestOrAwayFromZero)` (or just `rounded()`).

In both these cases, I think it's important to use spellings that
distinguish doing things to the fractional part of floating point values
and doing things to the binary representation of integer values. I think
there's great value in using the term "clamp", which is very different from
"nearest"; and in using an unlabeled `init(_:)` for initializing from FP
values, which is most similar to the unlabeled `init(_:)` for initializing
from integer values, as opposed to your suggested `init(truncating:)` which
connotes some similarity to `init(extendingOrTruncating:)` for integer
values.

+1

*... masking shifts*

The example claims that "(30 as UInt8) &>> 11" produces 3, because it
right-shifts 30 by 3. But isn't the bitWidth of UInt8 always 8 since it's a
fixed-width type?

Yes.

Why should 11 get masked to 3 before the shift?

Because those are the semantics of masking shift?

You can think of masking shift as an optimization akin to using &+ to
add signed numbers when you know it can't overflow. It's an expert's
tool. If you want semantics that always make sense, use regular shift.

(Also, it might be a good idea to choose examples with numbers whose
base-ten representations don't look like valid binary. :wink:)

Good point.

What use cases are there for masking shifts? I was under the
impression that "smart shifts" were pretty much how the usual shift
instructions already behaved.

No, sadly not! The way the usual shift instructions behave is that if
you shift by a negative amount or you overshift, you get undefined
behavior, which gets expressed in various fun ways at runtime!

(Minor: were the smart shift operators supposed to be included as
BinaryInteger protocol requirements? I only see them in the "heterogeneous
shifts" extension.)

They don't need to be requirements, as they're defined entirely in terms
of other (required) operations. I'd be interested in any arguments you
might have for making them requirements, though.

*... init<T: BinaryInteger>(_ source: T)*

Now a thought experiment: suppose you wanted to write an
arbitrary-precision BigInt, or any binary integer such as Int256. The
BinaryInteger protocol requires you to provide init<T:BinaryInteger>(_
source: T). Given the source of type T, how do you access its bits? Is
repeated use of word(at:) the recommended way?

Yes.

If so, it might be nice to include a "wordCount" returning the number
of available words; otherwise I suppose the user has to use something
like bitWidth/(8*MemoryLayout<Int>.size), which is pretty ugly.

good catch; countRepresentedWords is in the prototype
(https://github.com/apple/swift/blob/new-integer-protocols/stdlib/public/core/Integers.swift.gyb#L1521\),
and it should be in the proposal.

*## FixedWidthInteger*

Why is popcount restricted to FixedWidthInteger? It seems like it could
theoretically apply to any UnsignedInteger.

You can perfectly legitimately get a popcount for a signed integer. It's
just looking at the binary representation and counting the ones. But then
with two's complement, it'd have to be restricted to FixedWidthInteger and
not BinaryInteger, because the same negative value would have a different
popcount depending on the type's bitwidth.

Right, or to put it differently, the popcount of a negative BigInt would
always be inifinite.

I'd disagree strongly with removing popcount from signed binary
integers. However, I suppose the same requirement could be applied to
both FixedWidthInteger and UnsignedInteger.

Given that there's little point in ever creating an unsigned BigInt
type, I think that wouldn't make much sense.

*## Heterogenous shifts, equality, and comparison*

These look great. How will the default implementations be provided?

You can look at the prototype to see how it's done:
https://github.com/apple/swift/blob/new-integer-protocols/stdlib/public/core/Integers.swift.gyb

(Equivalent question: how would a user define their own heterogeneous
operators?) I suppose this works:

    static func &>> <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Self
{
        // delegate to the protocol requirement &>>(Self, Self)
        return self &>> Self(extendingOrTruncating: rhs)
    }

But for operations you can't delegate so easily... I'm imagining trying to
implement heterogeneous comparison (i.e. < ) and the best I can come up
with uses signum() and word(at:)...

It's already implemented; you don't need to do anything.
https://github.com/apple/swift/blob/new-integer-protocols/stdlib/public/core/Integers.swift.gyb#L1687

Also, should these be protocol requirements so user-defined types can
specialize them?

That's a good question; I don't have an opinion yet. Anyone else?

*## Masking arithmetic*

Do &* and &+ and &- need their operands to have the same type, or could
these be heterogeneous too (at least &+ and &-)?

I don't know what semantics you're imagining for heterogeneous &+. What
type would it return? What mask would it use?

Note that the language doesn't let us express a generic heterogeneous
operation where the result type is the wider of the two argument types.

···

on Sat Jan 14 2017, Xiaodi Wu <swift-evolution@swift.org> wrote:

On Sat, Jan 14, 2017 at 1:32 AM, Jacob Bandes-Storch via swift-evolution < > swift-evolution@swift.org> wrote:

Jacob

On Fri, Jan 13, 2017 at 12:47 PM, Max Moiseev via swift-evolution < >> swift-evolution@swift.org> wrote:

Hi everyone,

Back in June 2016 we discussed the new design of the integer types for
the standard library. It even resulted in acceptance of SE-0104
<https://github.com/apple/swift-evolution/blob/master/proposals/0104-improved-integers.md&gt; for
Swift 3. Unfortunately we were not able to implement it in time for the
release.

But it was not forgotten, although, as time went by, a few changes needed
to be made in order to reflect the current state of the language.
Without further introduction, please welcome the refined proposal to make
integers in Swift more suitable for generic programming.

Available in this gist m’s gists · GitHub
oiseev/62ffe3c91b66866fdebf6f3fcc7cad8c and also inlined below.

Max

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
-Dave

Excited to see this getting closer!

I notice the BinaryInteger's func word(at n: Int) -> UInt function
expects a 2's complement representation. Do you expect that "BigInt"
will be implemented with 2's complement?

Ah, this is an excellent question, excellently phrased!

We designed it so BigInt could use 2's complement (see
DoubleWidth<Int64>, which works that way), which should be faster than
sign-magnitude...

BUT: I hadn't really considered that if existing bignum libraries use
sign-magnitude, we'll want to be able to efficiently use their
representations, so we need to make sure the protocols don't force those
representations to be inefficient.

As a nonmutating function, I would think any implementation from a 1's
complement big int would result in O(n^2) unless the BigInt were
designed with scratch space where it could store the intermediate
values of that representation.

Oh... you mean that word(at:) itself would be linear, and thus
algorithms that iterate the words linearly would be O(N^2)... yuck.

So far, the only algorithm we have using word(at:) is this one, and
https://github.com/apple/swift/blob/new-integer-protocols/stdlib/public/core/Integers.swift.gyb#L2191
presumably we could adapt it to sign-magnitude by expanding the
BinaryInteger protocol with a static var that indicates which
representation is used.

enum Representation {
   twosComplement, signMagnitude, /*onesComplement?*/
}
static var representation: Representation { get }

Now, the other algorithms that I can anticipate would use word(at:) are
basically... the ones you'd have to implement to build BigInt, so I
guess this change would be OK.

Supporting sign-magnitude complicates generic programming with these
protocols, though, so I want to be really sure we need to do this before
we take the plunge.

I only ask because just last week I was designing an arbitrary-width
float type in 1's complement, well, really I kept the sign bit in a
separate Bool.

Sounds like sign-magnitude rather than 1's complement if I remember my
complements correctly.

Of course, I recognize the value of being able to write integer init
code from a 2's complement representation, I'm just curious if there's
a way to allow this function to be more efficient for alternate
representations.

I love that there's a DoubleWidth type, having coded some arbitrary
precision arithmetic, that's always a downer going from generic code
to having to pick a specific "big enough" result type.

I'm glad the % operator got moved. It really did stand out as not
part of floating points.

Trailing zeros looks like a sensible optimization.

I agree with moving the shift operators, I like that you've called out
the notion of an infinite shift.

I don't think we ever meant to say anything about infinite shifts!

I can't say I fully understood the use of the separate mask vs. smart
shift operators, but I'll take another read in the next few days and
see if I understand why they exist.

You're not alone it appears. We need to clarify that in the text,
obviously. The smart shift operators are there to give you sensible
semantics without undefined behavior or traps in the cases that have
logically computable answers. The masking shifts are there for
performance in the rare cases where smart shifting with a non-literal
shift amount introduces non-negligible overheads (testing and branching
to handle overshifts and negative shift amounts).

Obviously you have to know that the masking behavior is benign in your
use case when using masking shifts in an operation. I made the mistake
of using a masking shift to optimize some code the other day where the
shift amount could be equal to the full width of the value being
shifted. I ended up widening the LHS before doing the shift and then
truncating back to the width, but that's only because I was writing
performance-critical code. The smart shift would have “just worked.”

I know this PR does not address the BitInt implementation, but do you
have one?

I know someone that implemented one... using sign-magnitude, actually,
and his word(at:) implementation has the linear cost problem. I'll ask
him to post a link.

Link: https://github.com/natecook1000/swift/blob/nc-bigint/test/Prototypes/BigInt.swift

This is just a proof of concept BigInt implementation—there's not much in the way of optimization going on, and it's a little out of date. It does use sign-magnitude, so the word(at:) implementation is O(n) on each call due to this bit:

https://github.com/natecook1000/swift/blob/nc-bigint/test/Prototypes/BigInt.swift#L571-L576

The only other question I have from looking at this again is whether perhaps BinaryInteger should have a toString(radix:uppercase:) method, since arbitrary precision types may be able to optimize significantly for different bases. The prototype at the link doesn't do this, but a hex string (or any factor of 2 base?) should be far cheaper to generate than the general case that uses repeated division. Without that, a BigInt type doesn't have any way to customize the generic String<T>(_:radix:uppercase:) initializer.

-Nate

···

On Jan 14, 2017, at 7:41 PM, Dave Abrahams via swift-evolution <swift-evolution@swift.org> wrote:
on Sat Jan 14 2017, Benjamin Spratling <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

I'm glad to see this moving forward.

-Ben Spratling

On Jan 14, 2017, at 2:00 AM, Rien via swift-evolution <swift-evolution@swift.org> wrote:

+1

Any change of including “ranged integers”?
I.e. an integer with a value that must fit in a predefined range?

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: Swiftrien (Rien) · GitHub
Project: http://swiftfire.nl

On 13 Jan 2017, at 21:47, Max Moiseev via swift-evolution <swift-evolution@swift.org> wrote:

Hi everyone,

Back in June 2016 we discussed the new design of the integer types for the standard library. It even resulted in acceptance of SE-0104 for Swift 3. Unfortunately we were not able to implement it in time for the release.

But it was not forgotten, although, as time went by, a few changes needed to be made in order to reflect the current state of the language.
Without further introduction, please welcome the refined proposal to make integers in Swift more suitable for generic programming.

Available in this gist Protocol-oriented integers (take 2) · GitHub and also inlined below.

Max

Protocol-oriented integers (take 2)

  • Proposal: SE-NNNN
  • Authors: Dave Abrahams, Maxim Moiseev
  • Review Manager: TBD
  • Status: Awaiting review
  • Bug: SR-3196
  • Previous Proposal: SE-0104
Introduction

This proposal is an evolution of SE-0104. The goal is still to clean up Swifts integer APIs and make them more useful for generic programming.

The language has evolved in ways that affect integers APIs since the time the original proposal was approved for Swift 3. We also attempted to implement the proposed model in the standard library and found that some essential APIs were missing, whereas others could be safely removed.

Major changes to the APIs introduced by this proposal (as compared to SE-0104) are listed in a dedicated section.

Motivation

Swift's integer protocols don't currently provide a suitable basis for generic programming. See this blog post for an example of an attempt to implement a generic algorithm over integers.

The way the Arithmetic protocol is defined, it does not generalize to floating point numbers and also slows down compilation by requiring every concrete type to provide an implementation of arithmetic operators, thus polluting the overload set.

Converting from one integer type to another is performed using the concept of the 'maximum width integer' (see MaxInt), which is an artificial limitation. The very existence of MaxInt makes it unclear what to do should someone implement Int256, for example.

Another annoying problem is the inability to use integers of different types in comparison and bit-shift operations. For example, the following snippets won't compile:

var x: Int8 = 42
let y = 1
let z = 0

x
<<= y // error: binary operator '<<=' cannot be applied to operands of type 'Int8' and 'Int'
if x > z { ... } // error: binary operator '>' cannot be applied to operands of type 'Int8' and 'Int'
Currently, bit-shifting a negative number of (or too many) bits causes a trap on some platforms, which makes low-level bit manipulations needlessly dangerous and unpredictable.

Finally, the current design predates many of the improvements that came since Swift 1, and hasn't been revised since then.

Proposed solution

We propose a new model that does not have above mentioned problems and is more easily extensible.

              +--------------+ +-------------+
      +------>+ Arithmetic | | Comparable |
      > > (+,-,*,/) | | (==,<,>,...)|
      > +-------------++ +---+---------+
      > ^ ^
+-------+------------+ | |
> SignedArithmetic | +-+-------+-----------+
> (unary -) | | BinaryInteger |
+------+-------------+ |(words,%,bitwise,...)|
     ^ ++---+-----+----------+
     > +-----------^ ^ ^---------------+
     > > > >
+------+---------++ +---------+---------------+ +--+----------------+
> SignedInteger | | FixedWidthInteger | | UnsignedInteger |
> > >(endianness,overflow,...)| | |
+---------------+-+ +-+--------------------+--+ +-+-----------------+
              ^ ^ ^ ^
              > > > >
              > > > >
             ++--------+-+ +-+-------+-+
             >Int family |-+ |UInt family|-+
             +-----------+ | +-----------+ |
               +-----------+ +-----------+

There are several benefits provided by this model over the old one:

  • It allows mixing integer types in generic functions.

The possibility to initialize instances of any concrete integer type with values of any other concrete integer type enables writing functions that operate on more than one type conforming to BinaryInteger, such as heterogeneous comparisons or bit shifts, described later.

  • It removes the overload resolution overhead.

Arithmetic and bitwise operations can now be defined as generic operators on protocols. This approach significantly reduces the number of overloads for those operations, which used to be defined for every single concrete integer type.

  • It enables protocol sharing between integer and floating point types.

Note the exclusion of the % operation from Arithmetic. Its behavior for floating point numbers is sufficiently different from the one for integers that using it in generic context would lead to confusion. The FloatingPoint protocol introduced by SE-0067 should now refine SignedArithmetic.

  • It makes future extensions possible.

The proposed model eliminates the 'largest integer type' concept previously used to interoperate between integer types (see toIntMax in the current model) and instead provides access to machine words. It also introduces thedoubleWidthMultiply, doubleWidthDivide, and quotientAndRemainder methods. Together these changes can be used to provide an efficient implementation of bignums that would be hard to achieve otherwise.

The implementation of proposed model in the standard library is available in the new-integer-protocols branch.

A note on bit shifts

This proposal introduces the concepts of smart shifts and masking shifts.

The semantics of shift operations are often undefined in under- or over-shift cases. Smart shifts, implemented by >> and <<, are designed to address this problem and always behave in a well defined way, as shown in the examples below:

  • x << -2 is equivalent to x >> 2

  • (1 as UInt8) >> 42) will evaluate to 0

  • (-128 as Int8) >> 42) will evaluate to 0xff or -1

In most scenarios, the right hand operand is a literal constant, and branches for handling under- and over-shift cases can be optimized away. For other cases, this proposal provides masking shifts, implemented by &>> and &<<. A masking shift logically preprocesses the right hand operand by masking its bits to produce a value in the range 0...(x-1) where x is the number of bits in the left hand operand. On most architectures this masking is already performed by the CPU's shift instructions and has no cost. Both kinds of shift avoid undefined behavior and produce uniform semantics across architectures.

Detailed design

What's new since SE-0104

  • SE-0091 removed the necessity to dispatch generic operators through special methods.

All operators are now declared by protocols as static funcs.

  • Standard Library no longer provides + and - operators for Strideable types.

They were problematic, as one could have written mixed-type code like let x: Int64 = 42; x += (1 as Int), which would compile, but shouldn't. Besides, since the Stride of an unsigned type is signed, Standard Library had to implement a hack to make code like let x: UInt = 42; x += (1 as Int) ambiguous. These operators were only necessary because they made advancing collection indices convenient, which is no longer the case since the introduction of the new indexing model in Swift 3.

  • Shifts and other bitwise operations were moved from FixedWidthInteger to BinaryInteger.

Left shift operation on an unbounded integer should infinitely extend the number, and never drop set bits when they reach the most significant position in the underlying representation.

  • BitwiseOperations protocol was deprecated.

We believe there are no useful entities that support bitwise operations, but at the same time are not binary integers.

  • minimumSignedRepresentationBitWidth property was removed.

  • trailingZeros property was added to the BinaryInteger protocol.

leadingZeros and popcount properties are still defined by the FixedWidthInteger protocol.

  • Endian-converting initializers and properties were added to the FixedWidthInteger protocol.

  • Standard library introduces the new type DoubleWidth<T>.

See this section for more details.

Protocols

Arithmetic

The Arithmetic protocol declares binary arithmetic operators – such as +, -, and * — and their mutating counterparts.

It provides a suitable basis for arithmetic on scalars such as integers and floating point numbers.

Both mutating and non-mutating operations are declared in the protocol, however only the mutating ones are required, as default implementations of the non-mutating ones are provided by a protocol extension.

The Magnitude associated type is able to hold the absolute value of any possible value of Self. Concrete types do not have to provide a type alias for it, as it can be inferred from the magnitude property. This property can be useful in operations that are simpler to implement in terms of unsigned values, for example, printing a value of an integer, which is just printing a '-' character in front of an absolute value.

Please note that for ordinary work, the magnitude property should not be preferred to the abs(_) function, whose return value is of the same type as its argument.

public protocol Arithmetic : Equatable, ExpressibleByIntegerLiteral
{

/// Creates a new instance from the given integer, if it can be represented
/// exactly.
///
/// If the value passed as `source` is not representable exactly, the result
/// is `nil`. In the following example, the constant `x` is successfully
/// created from a value of `100`, while the attempt to initialize the
/// constant `y` from `1_000` fails because the `Int8` type can represent
/// `127` at maximum:
///
/// let x = Int8(exactly: 100)
/// // x == Optional(100)
/// let y = Int8(exactly: 1_000)
/// // y == nil
///
/// - Parameter source: A floating-point value to convert to an integer.
init?<T : BinaryInteger>(exactly source
: T)

/// A type that can represent the absolute value of any possible value of the
/// conforming type.
associatedtype Magnitude : Equatable, ExpressibleByIntegerLiteral

/// The magnitude of this value.
///
/// For any numeric value `x`, `x.magnitude` is the absolute value of `x`.
/// You can use the `magnitude` property in operations that are simpler to
/// implement in terms of unsigned values, such as printing the value of an
/// integer, which is just printing a '-' character in front of an absolute
/// value.
///
/// let x = -200
/// // x.magnitude == 200
///
/// The global `abs(_:)` function provides more familiar syntax when you need
/// to find an absolute value. In addition, because `abs(_:)` always returns
/// a value of the same type, even in a generic context, using the function
/// instead of the `magnitude` property is encouraged.
///
/// - SeeAlso: `abs(_:)`
var magnitude: Magnitude { get
}

/// Returns the sum of the two given values.
///
/// The sum of `lhs` and `rhs` must be representable in the same type. In the
/// following example, the result of `100 + 200` is greater than the maximum
/// representable `Int8` value:
///
/// let x: Int8 = 10 + 21
/// // x == 31
/// let y: Int8 = 100 + 121
/// // Overflow error
static func +(_ lhs: Self, _ rhs: Self) -> Self

/// Adds the given value to this value in place.
///
/// For example:
///
/// var x = 15
/// y += 7
/// // y == 22
static func +=(_ lhs: inout Self, rhs: Self
)

/// Returns the difference of the two given values.
///
/// The difference of `lhs` and `rhs` must be representable in the same type.
/// In the following example, the result of `10 - 21` is less than zero, the
/// minimum representable `UInt` value:
///
/// let x: UInt = 21 - 10
/// // x == 11
/// let y: UInt = 10 - 21
/// // Overflow error
static func -(_ lhs: Self, _ rhs: Self) -> Self

/// Subtracts the given value from this value in place.
///
/// For example:
///
/// var x = 15
/// y -= 7
/// // y == 8
static func -=(_ lhs: inout Self, rhs: Self
)

/// Returns the product of the two given values.
///
/// The product of `lhs` and `rhs` must be representable in the same type. In
/// the following example, the result of `10 * 50` is greater than the
/// maximum representable `Int8` value.
///
/// let x: Int8 = 10 * 5
/// // x == 50
/// let y: Int8 = 10 * 50
/// // Overflow error
static func *(_ lhs: Self, _ rhs: Self) -> Self

/// Multiples this value by the given value in place.
///
/// For example:
///
/// var x = 15
/// y *= 7
/// // y == 105
static func *=(_ lhs: inout Self, rhs: Self
)

/// Returns the quotient of dividing the first value by the second.
///
/// For integer types, any remainder of the division is discarded.
///
/// let x = 21 / 5
/// // x == 4
static func /(_ lhs: Self, _ rhs: Self) -> Self

/// Divides this value by the given value in place.
///
/// For example:
///
/// var x = 15
/// y /= 7
/// // y == 2
static func /=(_ lhs: inout Self, rhs: Self
)
}

extension Arithmetic
{

public init() { self = 0
}

public static prefix func + (x: Self) -> Self
{

return
x
}
}

SignedArithmetic

The SignedArithmetic protocol is for numbers that can be negated.

public protocol SignedArithmetic : Arithmetic
{

/// Returns the additive inverse of this value.
///
/// let x = 21
/// let y = -x
/// // y == -21
///
/// - Returns: The additive inverse of this value.
///
/// - SeeAlso: `negate()`
static prefix func - (_ operand: Self) -> Self

/// Replaces this value with its additive inverse.
///
/// The following example uses the `negate()` method to negate the value of
/// an integer `x`:
///
/// var x = 21
/// x.negate()
/// // x == -21
///
/// - SeeAlso: The unary minus operator (`-`).
mutating func negate
()
}

extension SignedArithmetic
{

public static prefix func - (_ operand: Self) -> Self
{

var result =
operand
  result.
negate
()

return
result
}

public mutating func negate
() {

self = Self() - self

}
}

BinaryInteger

The BinaryInteger protocol is the basis for all the integer types provided by the standard library.

This protocol adds a few new initializers. Two of them allow to create integers from floating point numbers, others support construction from instances of any type conforming to BinaryInteger, using different strategies:

  • Initialize Self with the value, provided that the value is representable. The precondition should be satisfied by the caller.

  • Extend or truncate the value to fit into Self

  • Clamp the value to the representable range of Self

BinaryInteger also declares bitwise and shift operators.

public protocol BinaryInteger :
Comparable, Hashable, Arithmetic, CustomStringConvertible, Strideable
{

/// A Boolean value indicating whether this type is a signed integer type.
///
/// *Signed* integer types can represent both positive and negative values.
/// *Unsigned* integer types can represent only nonnegative values.
static var isSigned: Bool { get
}

/// Creates an integer from the given floating-point value, if it can be
/// represented exactly.
///
/// If the value passed as `source` is not representable exactly, the result
/// is `nil`. In the following example, the constant `x` is successfully
/// created from a value of `21.0`, while the attempt to initialize the
/// constant `y` from `21.5` fails:
///
/// let x = Int(exactly: 21.0)
/// // x == Optional(21)
/// let y = Int(exactly: 21.5)
/// // y == nil
///
/// - Parameter source: A floating-point value to convert to an integer.
init?<T : FloatingPoint>(exactly source
: T)

/// Creates an integer from the given floating-point value, truncating any
/// fractional part.
///
/// Truncating the fractional part of `source` is equivalent to rounding
/// toward zero.
///
/// let x = Int(21.5)
/// // x == 21
/// let y = Int(-21.5)
/// // y == -21
///
/// If `source` is outside the bounds of this type after truncation, a
/// runtime error may occur.
///
/// let z = UInt(-21.5)
/// // Error: ...the result would be less than UInt.min
///
/// - Parameter source: A floating-point value to convert to an integer.
/// `source` must be representable in this type after truncation.
init<T : FloatingPoint>(_ source
: T)

/// Creates an new instance from the given integer.
///
/// If the value passed as `source` is not representable in this type, a
/// runtime error may occur.
///
/// let x = -500 as Int
/// let y = Int32(x)
/// // y == -500
///
/// // -500 is not representable as a 'UInt32' instance
/// let z = UInt32(x)
/// // Error
///
/// - Parameter source: An integer to convert. `source` must be representable
/// in this type.
init<T : BinaryInteger>(_ source
: T)

/// Creates a new instance from the bit pattern of the given instance by
/// sign-extending or truncating to fit this type.
///
/// When the bit width of `T` (the type of `source`) is equal to or greater
/// than this type's bit width, the result is the truncated
/// least-significant bits of `source`. For example, when converting a
/// 16-bit value to an 8-bit type, only the lower 8 bits of `source` are
/// used.
///
/// let p: Int16 = -500
/// // 'p' has a binary representation of 11111110_00001100
/// let q = Int8(extendingOrTruncating: p)
/// // q == 12
/// // 'q' has a binary representation of 00001100
///
/// When the bit width of `T` is less than this type's bit width, the result
/// is *sign-extended* to fill the remaining bits. That is, if `source` is
/// negative, the result is padded with ones; otherwise, the result is
/// padded with zeros.
///
/// let u: Int8 = 21
/// // 'u' has a binary representation of 00010101
/// let v = Int16(extendingOrTruncating: u)
/// // v == 21
/// // 'v' has a binary representation of 00000000_00010101
///
/// let w: Int8 = -21
/// // 'w' has a binary representation of 11101011
/// let x = Int16(extendingOrTruncating: w)
/// // x == -21
/// // 'x' has a binary representation of 11111111_11101011
/// let y = UInt16(extendingOrTruncating: w)
/// // y == 65515
/// // 'y' has a binary representation of 11111111_11101011
///
/// - Parameter source: An integer to convert to this type.
init<T : BinaryInteger>(extendingOrTruncating source
: T)

/// Creates a new instance with the representable value that's closest to the
/// given integer.
///
/// If the value passed as `source` is greater than the maximum representable
/// value in this type, the result is the type's `max` value. If `source` is
/// less than the smallest representable value in this type, the result is
/// the type's `min` value.
///
/// In this example, `x` is initialized as an `Int8` instance by clamping
/// `500` to the range `-128...127`, and `y` is initialized as a `UInt`
/// instance by clamping `-500` to the range `0...UInt.max`.
///
/// let x = Int8(clamping: 500)
/// // x == 127
/// // x == Int8.max
///
/// let y = UInt(clamping: -500)
/// // y == 0
///
/// - Parameter source: An integer to convert to this type.
init<T : BinaryInteger>(clamping source
: T)

/// Returns the n-th word, counting from the least significant to most
/// significant, of this value's binary representation.
///
/// The `word(at:)` method returns negative values in two's complement
/// representation, regardless of a type's underlying implementation. If `n`
/// is greater than the number of words in this value's current
/// representation, the result is `0` for positive numbers and `~0` for
/// negative numbers.
///
/// - Parameter n: The word to return, counting from the least significant to
/// most significant. `n` must be greater than or equal to zero.
/// - Returns: An word-sized, unsigned integer with the bit pattern of the
/// n-th word of this value.
func word(at n: Int) -> UInt

/// The number of bits in the current binary representation of this value.
///
/// This property is a constant for instances of fixed-width integer
/// types.
var bitWidth : Int { get
}

/// The number of trailing zeros in this value's binary representation.
///
/// For example, in a fixed-width integer type with a `bitWidth` value of 8,
/// the number -8 has three trailing zeros.
///
/// let x = Int8(bitPattern: 0b1111_1000)
/// // x == -8
/// // x.trailingZeros == 3
var trailingZeros: Int { get
}

/// Returns the remainder of dividing the first value by the second.
///
/// The result has the same sign as `lhs` and is less than `rhs.magnitude`.
///
/// let x = 22 % 5
/// // x == 2
/// let y = 22 % -5
/// // y == 2
/// let z = -22 % -5
/// // z == -2
///
/// - Parameters:
/// - lhs: The value to divide.
/// - rhs: The value to divide `lhs` by. `rhs` must not be zero.
static func %(_ lhs: Self, _ rhs: Self) -> Self

/// Replaces this value with the remainder of itself divided by the given
/// value. For example:
///
/// var x = 15
/// x %= 7
/// // x == 1
///
/// - Parameter rhs: The value to divide this value by. `rhs` must not be
/// zero.
///
/// - SeeAlso: `remainder(dividingBy:)`
static func %=(_ lhs: inout Self, _ rhs: Self
)

/// Returns the inverse of the bits set in the argument.
///
/// The bitwise NOT operator (`~`) is a prefix operator that returns a value
/// in which all the bits of its argument are flipped: Bits that are `1` in
/// the argument are `0` in the result, and bits that are `0` in the argument
/// are `1` in the result. This is equivalent to the inverse of a set. For
/// example:
///
/// let x: UInt8 = 5 // 0b00000101
/// let notX = ~x // 0b11111010
///
/// Performing a bitwise NOT operation on 0 returns a value with every bit
/// set to `1`.
///
/// let allOnes = ~UInt8.min // 0b11111111
///
/// - Complexity: O(1).
static prefix func ~ (_ x: Self) -> Self

/// Returns the result of performing a bitwise AND operation on this value
/// and the given value.
///
/// A bitwise AND operation results in a value that has each bit set to `1`
/// where *both* of its arguments have that bit set to `1`. For example:
///
/// let x: UInt8 = 5 // 0b00000101
/// let y: UInt8 = 14 // 0b00001110
/// let z = x & y // 0b00000100
static func &(_ lhs: Self, _ rhs: Self) -> Self

static func &=(_ lhs: inout Self, _ rhs: Self
)

/// Returns the result of performing a bitwise OR operation on this value and
/// the given value.
///
/// A bitwise OR operation results in a value that has each bit set to `1`
/// where *one or both* of its arguments have that bit set to `1`. For
/// example:
///
/// let x: UInt8 = 5 // 0b00000101
/// let y: UInt8 = 14 // 0b00001110
/// let z = x | y // 0b00001111
static func |(_ lhs: Self, _ rhs: Self) -> Self

static func |=(_ lhs: inout Self, _ rhs: Self
)

/// Returns the result of performing a bitwise XOR operation on this value
/// and the given value.
///
/// A bitwise XOR operation, also known as an exclusive OR operation, results
/// in a value that has each bit set to `1` where *one or the other but not
/// both* of its arguments had that bit set to `1`. For example:
///
/// let x: UInt8 = 5 // 0b00000101
/// let y: UInt8 = 14 // 0b00001110
/// let z = x ^ y // 0b00001011
static func ^(_ lhs: Self, _ rhs: Self) -> Self

static func ^=(_ lhs: inout Self, _ rhs: Self
)

/// Returns the result of shifting this value's binary representation the
/// specified number of digits to the right.
///
/// In a *masking shift*, the bit pattern of the value passed as `rhs` is
/// masked to produce a value between zero and the bit width of `lhs`. The
/// shift is performed using this masked value. Masking shifts require more
/// care to use correctly than a traditional bit shift, but are likely to be
/// more efficient when used with shift amounts that are not compile-time
/// constants. On most architectures, a masking shift compiles down to a
/// single instruction.
///
/// For example, if you shift an 8-bit, unsigned integer by 2, the shift
/// amount requires no masking.
///
/// let x: UInt8 = 30 // 0b00011110
/// let y = x &>> 2
/// // y == 7 // 0b00000111
///
/// However, if you shift it by 11, it first bitmasks `rhs` to `3`, and then
/// uses that masked value as the number of bits to shift `x`.
///
/// let z = x &>> 11
/// // z == 3 // 0b00000011
///
/// Relationship to the Right Shift Operator
/// ----------------------------------------
///
/// The masking right shift operator handles attempted overshifts and
/// undershifts differently from the right shift operator (`>>`). When the
/// value passed as `rhs` in a masking shift is within the range
/// `0...<bitWidth`, the operation is equivalent to using the right shift
/// operator.
///
/// let x: UInt8 = 30 // 0b00011110
/// let y1 = x &>> 2
/// // y1 == 7 // 0b00000111
/// let y2 = x >> 2
/// // y2 == 7 // 0b00000111
///
/// The right shift operator does not mask its right-hand-side argument, so
/// passing `11` as `rhs` shifts all the bits of `x` to zero.
///
/// let z1 = x &>> 11
/// // z1 == 240 // 0b00000011
/// let z2 = x >> 11
/// // z2 == 0 // 0b00000000
///
/// - Parameter rhs: The number of bits to shift this value to the right. If
/// `rhs` is outside the range `0..<bitWidth`, it is masked to produce a
/// value within that range.
/// - Returns: The result of shifting this value by the masked `rhs` to the
/// right.
///
/// - SeeAlso: `&<<`, `>>`
static func &>>(_ lhs: Self, _ rhs: Self) -> Self

static func &>>=(_ lhs: inout Self, _ rhs: Self
)

/// Returns the result of shifting this value's binary representation the
/// specified number of digits to the left.
///
/// In a *masking shift*, the bit pattern of the value passed as `rhs` is
/// masked to produce a value between zero and the bit width of `lhs`. The
/// shift is performed using this masked value. Masking shifts require more
/// care to use correctly than a traditional bit shift, but are likely to be
/// more efficient when used with shift amounts that are not compile-time
/// constants. On most architectures, a masking shift compiles down to a
/// single instruction.
///
/// For example, if you shift an 8-bit, unsigned integer by 2, the shift
/// amount requires no masking.
///
/// let x: UInt8 = 30 // 0b00011110
/// let y = x &>> 2
/// // y == 120 // 0b01111000
///
/// However, if you shift it by 11, it first bitmasks `rhs` to `3`, and then
/// uses that masked value as the number of bits to shift `x`.
///
/// let z = x &<< 11
/// // z == 240 // 0b11110000
///
/// Relationship to the Left Shift Operator
/// ---------------------------------------
///
/// The masking left shift operator handles attempted overshifts and
/// undershifts differently from the left shift operator (`<<`). When the
/// value passed as `rhs` in a masking shift is within the range
/// `0...<bitWidth`, the operation is equivalent to using the left shift
/// operator.
///
/// let x: UInt8 = 30 // 0b00011110
/// let y1 = x &<< 2
/// // y1 == 120 // 0b01111000
/// let y2 = x << 2
/// // y2 == 120 // 0b01111000
///
/// The left shift operator does not mask its right-hand-side argument, so
/// passing `11` as `rhs` shifts all the bits of `x` to zero.
///
/// let z1 = x &<< 11
/// // z1 == 240 // 0b11110000
/// let z2 = x << 11
/// // z2 == 0 // 0b00000000
///
/// - Parameter rhs: The number of bits to shift this value to the left. If
/// `rhs` is outside the range `0..<bitWidth`, it is masked to produce a
/// value within that range.
/// - Returns: The result of shifting this value by the masked `rhs` to the
/// left.
///
/// - SeeAlso: `&>>`, `<<`
static func &<<(_ lhs: Self, _ rhs: Self) -> Self

static func &<<=(_ lhs: inout Self, _ rhs: Self
)

/// Returns the quotient and remainder of this value divided by the given
/// value.
///
/// Use this method to calculate the quotient and remainder of a division at
/// the same time.
///
/// let x = 1_000_000
/// let (q, r) = x.quotientAndRemainder(dividingBy: 933)
/// // q == 1071
/// // r == 757
///
/// - Parameter rhs: The value to divide this value by.
/// - Returns: A tuple containing the quotient and remainder of this value
/// divided by `rhs`.
func quotientAndRemainder(dividingBy rhs: Self
)

-> (quotient: Self, remainder: Self
)

/// Returns `-1` if this value is negative and `1` if it's positive;
/// otherwise, `0`.
///
/// - Returns: The sign of this number, expressed as an integer of the same
/// type.
func signum() -> Self

}

FixedWidthInteger

The FixedWidthInteger protocol adds the notion of endianness as well as static properties for type bounds and bit width.

The WithOverflow family of methods is used in default implementations of mutating arithmetic methods (see the Arithmetic protocol). Having these methods allows the library to provide both bounds-checked and masking implementations of arithmetic operations, without duplicating code.

The doubleWidthMultiply and doubleWidthDivide methods are necessary building blocks to implement support for integer types of a greater width such as arbitrary-precision integers.

public protocol FixedWidthInteger : BinaryInteger
{

/// The number of bits used for the underlying binary representation of
/// values of this type.
///
/// An unsigned, fixed-width integer type can represent values from 0 through
/// `(2 ** bitWidth) - 1`, where `**` is exponentiation. A signed,
/// fixed-width integer type can represent values from
/// `-(2 ** bitWidth - 1)` through `(2 ** bitWidth - 1) - 1`. For example,
/// the `Int8` type has a `bitWidth` value of 8 and can store any integer in
/// the range `-128...127`.
static var bitWidth : Int { get
}

/// The maximum representable integer in this type.
///
/// For unsigned integer types, this value is `(2 ** bitWidth) - 1`, where
/// `**` is exponentiation. For signed integer types, this value is
/// `(2 ** bitWidth - 1) - 1`.
static var max: Self { get
}

/// The minimum representable value.
///
/// For unsigned integer types, this value is always `0`. For signed integer
/// types, this value is `-(2 ** bitWidth - 1)`, where `**` is
/// exponentiation.
static var min: Self { get
}

/// Returns the sum of this value and the given value along with a flag
/// indicating whether overflow occurred in the operation.
///
/// - Parameter other: The value to add to this value.
/// - Returns: A tuple containing the result of the addition along with a
/// flag indicating whether overflow occurred. If the `overflow` component
/// is `.none`, the `partialValue` component contains the entire sum. If
/// the `overflow` component is `.overflow`, an overflow occurred and the
/// `partialValue` component contains the truncated sum of this value and
/// `other`.
///
/// - SeeAlso: `+`
func addingWithOverflow(_ other: Self
)

-> (partialValue: Self, overflow
: ArithmeticOverflow)

/// Returns the difference of this value and the given value along with a
/// flag indicating whether overflow occurred in the operation.
///
/// - Parameter other: The value to subtract from this value.
/// - Returns: A tuple containing the result of the subtraction along with a
/// flag indicating whether overflow occurred. If the `overflow` component
/// is `.none`, the `partialValue` component contains the entire
/// difference. If the `overflow` component is `.overflow`, an overflow
/// occurred and the `partialValue` component contains the truncated
/// result of `other` subtracted from this value.
///
/// - SeeAlso: `-`
func subtractingWithOverflow(_ other: Self
)

-> (partialValue: Self, overflow
: ArithmeticOverflow)

/// Returns the product of this value and the given value along with a flag
/// indicating whether overflow occurred in the operation.
///
/// - Parameter other: The value to multiply by this value.
/// - Returns: A tuple containing the result of the multiplication along with
/// a flag indicating whether overflow occurred. If the `overflow`
/// component is `.none`, the `partialValue` component contains the entire
/// product. If the `overflow` component is `.overflow`, an overflow
/// occurred and the `partialValue` component contains the truncated
/// product of this value and `other`.
///
/// - SeeAlso: `*`, `doubleWidthMultiply(_:_:)`
func multipliedWithOverflow(by other: Self
)

-> (partialValue: Self, overflow
: ArithmeticOverflow)

/// Returns the quotient of dividing this value by the given value along with
/// a flag indicating whether overflow occurred in the operation.
///
/// For a value `x`, if zero is passed as `other`, the result is
/// `(x, .overflow)`.
///
/// - Parameter other: The value to divide this value by.
/// - Returns: A tuple containing the result of the division along with a
/// flag indicating whether overflow occurred. If the `overflow` component
/// is `.none`, the `partialValue` component contains the entire quotient.
/// If the `overflow` component is `.overflow`, an overflow occurred and
/// the `partialValue` component contains the truncated quotient.
///
/// - SeeAlso: `/`, `doubleWidthDivide(_:_:)`
func dividedWithOverflow(by other: Self
)

-> (partialValue: Self, overflow
: ArithmeticOverflow)

/// Returns a tuple containing the high and low parts of the result of
/// multiplying its arguments.
///
/// Use this method to calculate the full result of a product that would
/// otherwise overflow. Unlike traditional truncating multiplication, the
/// `doubleWidthMultiply(_:_:)` method returns both the `high` and `low`
/// parts of the product of `lhs` and `rhs`. The following example uses this
/// method to multiply two `UInt8` values that normally overflow when
/// multiplied:
///
/// let x: UInt8 = 100
/// let y: UInt8 = 20
/// let result = UInt8.doubleWidthMultiply(100, 20)
/// // result.high == 0b00000111
/// // result.low == 0b11010000
///
/// The product of `x` and `y` is 2000, which is too large to represent in a
/// `UInt8` instance. The `high` and `low` components of the `result` tuple
/// represent 2000 when concatenated to form a double-width integer; that
/// is, using `result.high` as the high byte and `result.low` as the low byte
/// of a `UInt16` instance.
///
/// let z = UInt16(result.high) << 8 | UInt16(result.low)
/// // z == 2000
///
/// - Parameters:
/// - lhs: A value to multiply.
/// - rhs: Another value to multiply.
/// - Returns: A tuple containing the high and low parts of the result of
/// multiplying `lhs` and `rhs`.
///
/// - SeeAlso: `multipliedWithOverflow(by:)`
static func doubleWidthMultiply(_ lhs: Self, _ rhs: Self
)

-> (high: Self, low
: Magnitude)

/// Returns a tuple containing the quotient and remainder of dividing the
/// first argument by the second.
///
/// The resulting quotient must be representable within the bounds of the
/// type. If the quotient of dividing `lhs` by `rhs` is too large to
/// represent in the type, a runtime error may occur.
///
/// - Parameters:
/// - lhs: A tuple containing the high and low parts of a double-width
/// integer. The `high` component of the tuple carries the sign, if the
/// type is signed.
/// - rhs: The integer to divide into `lhs`.
/// - Returns: A tuple containing the quotient and remainder of `lhs` divided
/// by `rhs`.
static func doubleWidthDivide
(

_ lhs: (high: Self, low: Magnitude), _ rhs: Self
)

-> (quotient: Self, remainder: Self
)

/// The number of bits equal to 1 in this value's binary representation.
///
/// For example, in a fixed-width integer type with a `bitWidth` value of 8,
/// the number 31 has five bits equal to 1.
///
/// let x: Int8 = 0b0001_1111
/// // x == 31
/// // x.popcount == 5
var popcount: Int { get
}

/// The number of leading zeros in this value's binary representation.
///
/// For example, in a fixed-width integer type with a `bitWidth` value of 8,
/// the number 31 has three leading zeros.
///
/// let x: Int8 = 0b0001_1111
/// // x == 31
/// // x.leadingZeros == 3
/// - SeeAlso: `BinaryInteger.trailingZeros`
var leadingZeros: Int { get
}

/// Creates an integer from its big-endian representation, changing the
/// byte order if necessary.
init(bigEndian value: Self
)

/// Creates an integer from its little-endian representation, changing the
/// byte order if necessary.
init(littleEndian value: Self
)

/// The big-endian representation of this integer.
///
/// If necessary, the byte order of this value is reversed from the typical
/// byte order of this integer type. On a big-endian platform, for any
/// integer `x`, `x == x.bigEndian`.
///
/// - SeeAlso: `littleEndian`
var bigEndian: Self { get
}

/// The little-endian representation of this integer.
///
/// If necessary, the byte order of this value is reversed from the typical
/// byte order of this integer type. On a little-endian platform, for any
/// integer `x`, `x == x.littleEndian`.
///
/// - SeeAlso: `bigEndian`
var littleEndian: Self { get
}

/// A representation of this integer with the byte order swapped.
var byteSwapped: Self { get
}
}

Auxiliary protocols

public protocol UnsignedInteger : BinaryInteger
{

associatedtype Magnitude : BinaryInteger

}

public protocol SignedInteger : BinaryInteger, SignedArithmetic
{

associatedtype Magnitude : BinaryInteger

}

DoubleWidth

The DoubleWidth<T> type allows to create wider fixed-width integer types from the ones available in the standard library.

Standard library currently provides fixed-width integer types of up to 64 bits. A value of DoubleWidth<Int64> will double the range of the underlying type and implement all the FixedWidthInteger requirements. Please note though that the implementation will not necessarily be the most efficient one, so it would not be a good idea to use DoubleWidth<Int32>instead of a built-in Int64.

Extra operators

In addition to the operators described in the protocols section, we also provide a few extensions that are not protocol requirements:

Heterogeneous shifts

extension BinaryInteger
{

// Masking shifts
static func &>> <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Self

static func &>>= <Other : BinaryInteger>(lhs: inout Self, rhs
: Other)

static func &<< <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Self

static func &<<= <Other : BinaryInteger>(lhs: inout Self, rhs
: Other)

// 'Smart' shifts
static func >> <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Self

static func >>= <Other : BinaryInteger>(lhs: inout Self, rhs
: Other)

static func << <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Self

static func <<= <Other : BinaryInteger>(lhs: inout Self, rhs
: Other)
}

Heterogeneous equality and comparison

extension BinaryInteger
{

// Equality
static func == <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Bool

static func != <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Bool

// Comparison
static func < <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Bool

static func <= <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Bool

static func > <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Bool

static func >= <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Bool

}

Masking arithmetic

public func &* <T: FixedWidthInteger>(lhs: T, rhs: T) ->
T

public func &- <T: FixedWidthInteger>(lhs: T, rhs: T) ->
T

public func &+ <T: FixedWidthInteger>(lhs: T, rhs: T) -> T
Non-goals

This proposal:

  • DOES NOT solve the integer promotion problem, which would allow mixed-type arithmetic. However, we believe that it is an important step in the right direction.

  • DOES NOT include the implementation of a BigInt type, but allows it to be implemented in the future.

Source compatibility

The proposed change is designed to be as non-breaking as possible, and it has been proven that it does not break code on concrete integer types. However, there are still a few API breaking changes in the realm of generic code:

  • Integer protocols in Swift up to and including version 3 were not particularly useful for generic programming, but were rather a means of sharing implementation between conforming types. Therefore we believe the amount of code that relied on these protocols is relatively small. The breakage can be further reduced by introducing proper aliases for the removed protocols with deprecation warnings.

  • Deprecation of the BitwiseOperations protocol. We find it hard to imagine a type that conforms to this protocol, but is not a binary integer type.

  • Addition of 'smart' shifts will change the behavior of existing code. It will still compile, but will be potentially less performant due to extra logic involved. In a case, where this becomes a problem, newly introduced masking shift operators can be used instead. Unfortunately, performance characteristics of the code cannot be statically checked, and thus migration cannot be provided.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
-Dave

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

Couldn't you fix this by replacing `func word(at n: Int) -> UInt` with `var
words: Sequence<UInt>`, producing the words from least to most significant?

···

On Sat, Jan 14, 2017 at 7:41 PM, Dave Abrahams via swift-evolution < swift-evolution@swift.org> wrote:

Oh... you mean that word(at:) itself would be linear, and thus
algorithms that iterate the words linearly would be O(N^2)... yuck.

Nobody's proposing to remove Strideable.

···

on Mon Jan 16 2017, Xiaodi Wu <swift-evolution@swift.org> wrote:

Strideable? I didn't think Strideable itself was going away. I'd be sad if
it did; for one, all the Unsafe*Pointers are Strideable and that has its
uses.

--
-Dave

Really glad to see this coming back :)

I have a couple of questions:

1) How does Strideable relate to Arithmetic?

My understanding is that Strideable allows non-numeric types to express
different-type addition and subtraction, and that Arithmetic is for numeric
types only (derives from ExpressibleByIntegerLiteral) and allows then to
express same-type operations. Is that correct?

My understanding was that heterogeneous addition and subtraction using
operators are not supported not merely due to compiler limitations but as a
fundamental philosophy, because the type of the intended result can be
unclear to human readers and unintentional promotion is to be avoided? In
that vein, my understanding was that having those operators available for
Strideable was inconsistent and overdue for removal, and `advanced(by:)` is
the intended spelling for advancing a value by a distance of a different
type.

If so, it would be nice to add it to the proposal so people know when
they should conform (or set their generic constraints) to one or the other.

2) Can SignedNumber get some operators now?

Strideable.Stride is bound to this weird protocol ‘SignedNumber’. You can
create instances of it out of thin air via an integer literal, but you
can’t accumulate Strides to find the overall distance between some adjacent
Strideables.

I understood these new protocols to mean that SignedNumber is headed for
deprecation?

It would be nice if the proposal said that, then (and mentioned the
corresponding change to Strideable.Stride). I assume it will be replaced
with SignedArithmetic, then.

Also, #1 is why I don’t like the Strideable operator deprecation being
part of the proposal (and therefore part of the language specification). It
doesn’t solve the underlying issue, which is that our integer literal
protocols aren’t really good enough - they’re not useful enough outside the
standard library (see: https://bugs.swift.org/browse/SR-920\), and
evidently the compiler isn’t able to use them to infer types correctly.
Perhaps fixing it is a low priority, and that’s fine, but what about if
somebody else defines this operator in their library? They start of break
lots of valid code because the compiler can’t do the right thing. Improved
literal protocols may be the better way to resolve that problem.

I agree totally that literals are aching for improvements as outlined in
SR-920. Not sure how it relates to integer protocols though. What are some
examples of valid code that get broken today because of compiler
limitations?

The PR that implemented this fixed a bug with integer literals -
basically, the thrust of it (as I understand) is that the compiler can’t
figure out which is the preferred overload. It doesn’t know to prefer
same-type arithmetic when resolving literals involved in operators.

Yes, that's a compiler shortcoming and a practical reason to remove + for
the moment. However, there's also the other reason to remove +, which is
that Swift doesn't have a promotion model for integer types, and until it
does heterogeneous operations shouldn't be spelled using + whether the
compiler knows how to pick a preferred overload or not...

My problem with just fixing the problem by removing that operator is that

anybody could add it back in their own code, and that should be perfectly
allowable, but if they did so basic expressions would fail to compile.

This would only break their own code unless it's also exported in a
library. For the former, if we fix something because it's broken, the fact
that someone can put it back into their own code to break it isn't a reason
for us not to fix it :) As to the latter scenario, clashes between
libraries that export different implementations of the same operators and
the inability for end users to choose not the import them with the rest of
the library _is_ actually an annoying issue that needs improvement. This is
getting away from the topic of integers and distinct from the topic of
literal protocols, but tackling this topic is sorely needed IMO.

To me it sounds preferable to leave this as a implementation artefact
until we get a chance to make the literal protocols better.

Strideable isn’t some esoteric protocol just for collection indexes, you
know. It’s very useful for any quasi-numeric type whose absolute value is
semantically different from a difference. For example, I have
strongly-typed wrappers for a ByteOffset within a binary file and a
LineNumber inside a text file. These wrappers allow me to write extensions
(for example to clamp to the bounds of a particular file), and ensure that
I don’t accidentally mix semantically different values. They are both
Strideable by Int, which is a better semantic fit than Arithmetic
(LineNumbers are absolute, should you be able to add them? What does
LineNumber * LineNumber mean?).

`advanced(by:)` is still there, yes? It's not as terse, but neither is
`truncatingRemainder(dividingBy:)` that replaced `%` for floating point
types. I think the motivation is similar in both cases (i.e. to distinguish
two subtly different operations from each other).

So my point is basically that it’s an important protocol (maybe not so
much in the standard library, but certainly beyond).

Strideable? I didn't think Strideable itself was going away. I'd be sad if
it did; for one, all the Unsafe*Pointers are Strideable and that has its
uses.

···

On Mon, Jan 16, 2017 at 3:32 PM, Karl Wagner <razielim@gmail.com> wrote:

On 16 Jan 2017, at 20:53, Xiaodi Wu <xiaodi.wu@gmail.com> wrote:
On Mon, Jan 16, 2017 at 11:57 AM, Karl Wagner via swift-evolution < > swift-evolution@swift.org> wrote:

- Karl

I barely understand any of this thread, so these might be silly questions,
or based on a total misunderstanding the goals here, but it seems to be
about: a) clever/advanced mathsy things; b) naming things; and c) the
standard library... my questions are:

Do specialised clever advanced mathsy libraries all work exactly the same,
or do they have slightly different behaviours or conventions or semantics
in different fields of study?

Is the the goal here to define a common/shared sub-set of specialised maths
libraries, but in the standard library? Would that be useful by itself?

Could it be possible that "naming things" in the standard library would
trample all over possible uses in external libraries, making it harder
rather than easier for specialised maths libraries to construct their
interfaces in a non-confusing way?

···

On Tue, 17 Jan 2017 at 01:39 Dave Abrahams via swift-evolution < swift-evolution@swift.org> wrote:

on Mon Jan 16 2017, Xiaodi Wu <swift-evolution@swift.org> wrote:

> Strideable? I didn't think Strideable itself was going away. I'd be sad
if
> it did; for one, all the Unsafe*Pointers are Strideable and that has its
> uses.

Nobody's proposing to remove Strideable.

--
-Dave

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

There is some merit in having them be declared as static properties... If it's a non-trivial process to initialize a type, it might be worth it to have a static property that either provides the storage directly or just returns a fileprivate value declared outside the type.

- Dave Sweeris

···

On Jan 25, 2017, at 10:03, Xiaodi Wu via swift-evolution <swift-evolution@swift.org> wrote:

Stephen Canon wrote that Arithmetic should refine ExpressibleByIntegerLiteral because of certain mathematical properties of rings. In that case, 0 and 1 would just be spelled in that way. Otherwise, +1.

On Wed, Jan 25, 2017 at 11:38 Anton Mironov via swift-evolution <swift-evolution@swift.org> wrote:
Hi everyone,

I want to suggest a tiny extension to an Arithmetic protocol. It would be nice to have an additive identity and a multiplicative identity constants. Basically zero and one.

protocol Arithmetic {
  /* ... */
  static var zero: Self { get }  // additive identity: (value + .zero) == value
  static var one: Self { get }   // multiplicative identity: (value * .one) == value
}

These constants will ease implementation of math structures: vectors, matrices and etc.
I’m sorry if I’m duplicating someone’s suggestion. It is really hard to search for something in a long thread.

Stephen Canon wrote that Arithmetic should refine ExpressibleByIntegerLiteral because of certain mathematical properties of rings. In that case, 0 and 1 would just be spelled in that way. Otherwise, +1.

Hi everyone,

I want to suggest a tiny extension to an Arithmetic protocol. It would be nice to have an additive identity and a multiplicative identity constants. Basically zero and one.

protocol Arithmetic {
  /* ... */
  static var zero: Self { get }  // additive identity: (value + .zero) == value
  static var one: Self { get }   // multiplicative identity: (value * .one) == value
}

These constants will ease implementation of math structures: vectors, matrices and etc.
I’m sorry if I’m duplicating someone’s suggestion. It is really hard to search for something in a long thread.

There is some merit in having them be declared as static properties... If it's a non-trivial process to initialize a type, it might be worth it to have a static property that either provides the storage directly or just returns a fileprivate value declared outside the type.

A type for which this was necessary can just special-case handling 0 and 1 literals.

···

Sent from my iPhone

On Jan 25, 2017, at 1:45 PM, David Sweeris via swift-evolution <swift-evolution@swift.org> wrote:

On Jan 25, 2017, at 10:03, Xiaodi Wu via swift-evolution <swift-evolution@swift.org> wrote:

On Wed, Jan 25, 2017 at 11:38 Anton Mironov via swift-evolution <swift-evolution@swift.org> wrote:

It should already be possible to handle special cases like that since you
can reässign self in an initializer. Here's a contrived example; would it
allay your performance concerns?

struct Foo: ExpressibleByIntegerLiteral {
    private let value: String
    private static let zero = Foo("a really big thing")

    init(integerLiteral value: Int) {
        if value == 0 {
            self = .zero
        } else {
            self = Foo(String(value))
        }
    }

    private init(_ value: String) {
        self.value = value
    }
}

let x: Foo = 5   // value = "5"
let y: Foo = 0   // value = "a really big thing"

Given that, I definitely prefer the more natural literal expressibility to
the words "zero" and "one".

···

On Wed, Jan 25, 2017 at 10:45 AM David Sweeris via swift-evolution < swift-evolution@swift.org> wrote:

On Jan 25, 2017, at 10:03, Xiaodi Wu via swift-evolution < > swift-evolution@swift.org> wrote:

Stephen Canon wrote that Arithmetic should refine
ExpressibleByIntegerLiteral because of certain mathematical properties of
rings. In that case, 0 and 1 would just be spelled in that way. Otherwise,
+1.
On Wed, Jan 25, 2017 at 11:38 Anton Mironov via swift-evolution < > swift-evolution@swift.org> wrote:

Hi everyone,

I want to suggest a tiny extension to an Arithmetic protocol. It would be
nice to have an additive identity and a multiplicative identity constants.
Basically zero and one.

protocol Arithmetic {
  /* ... */
  static var zero: Self { get }  // additive identity: (value + .zero) ==
value
  static var one: Self { get }   // multiplicative identity: (value *
.one) == value
}

These constants will ease implementation of math structures: vectors,
matrices and etc.
I’m sorry if I’m duplicating someone’s suggestion. It is really hard to
search for something in a long thread.

There is some merit in having them be declared as static properties... If
it's a non-trivial process to initialize a type, it might be worth it to
have a static property that either provides the storage directly or just
returns a fileprivate value declared outside the type.

- Dave Sweeris
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Oh I see. Thank you for the explanation.
I agree that this name will generate less confusion than a purely mathematical one.

···

On Jan 25, 2017, at 21:24, Max Moiseev <moiseev@apple.com> wrote:

On Jan 25, 2017, at 11:18 AM, Anton Mironov <antonvmironov@gmail.com <mailto:antonvmironov@gmail.com>> wrote:

I agree that 0 and 1 literals look more expressive in the case of numbers.
Maybe “Arithmetic” name confused me for a bit. I suspected Arithmetic protocol to be more general rather than specific to numbers.

It *is* more general. As Steve pointed out in one of his emails, any type that can make sense of 0 and 1, should also be able to hold something that corresponds to 2, or 3, etc. If the type has 1 and +, then it should have some notion for `1+1`.

And the protocol name is intentionally non-scientific, as it does not represent any mathematical concept, but rather is a collection of things people used to refer to when they think about simple arithmetics.

Max

On Jan 25, 2017, at 20:00, Max Moiseev <moiseev@apple.com <mailto:moiseev@apple.com>> wrote:

Hi Anton,

Arithmetic refines RepresentableByIntegerLiteral, so it is quite possible to use `0` and `1` when you need them.

Max

On Jan 25, 2017, at 7:59 AM, Anton Mironov <antonvmironov@gmail.com <mailto:antonvmironov@gmail.com>> wrote:

Hi everyone,

I want to suggest a tiny extension to an Arithmetic protocol. It would be nice to have an additive identity and a multiplicative identity constants. Basically zero and one.

protocol Arithmetic {
  /* ... */
  static var zero: Self { get }  // additive identity: (value + .zero) == value
  static var one: Self { get }   // multiplicative identity: (value * .one) == value
}

These constants will ease implementation of math structures: vectors, matrices and etc.
I’m sorry if I’m duplicating someone’s suggestion. It is really hard to search for something in a long thread.

Thanks,
Anton Mironov

On Jan 13, 2017, at 10:47 PM, Max Moiseev via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Hi everyone,

Back in June 2016 we discussed the new design of the integer types for the standard library. It even resulted in acceptance of SE-0104 <https://github.com/apple/swift-evolution/blob/master/proposals/0104-improved-integers.md&gt; for Swift 3. Unfortunately we were not able to implement it in time for the release.

But it was not forgotten, although, as time went by, a few changes needed to be made in order to reflect the current state of the language.
Without further introduction, please welcome the refined proposal to make integers in Swift more suitable for generic programming.

Available in this gist Protocol-oriented integers (take 2) · GitHub and also inlined below.

Max

Protocol-oriented integers (take 2)

Proposal: SE-NNNN <https://gist.github.com/moiseev/0000-even-more-improved-integers.md&gt;
Authors: Dave Abrahams <https://github.com/dabrahams&gt;, Maxim Moiseev <https://github.com/moiseev&gt;
Review Manager: TBD
Status: Awaiting review
Bug: SR-3196 <Issues · apple/swift-issues · GitHub;
Previous Proposal: SE-0104 <https://gist.github.com/moiseev/0104-improved-integers.md&gt;
<Protocol-oriented integers (take 2) · GitHub

This proposal is an evolution of SE-0104 <https://gist.github.com/moiseev/0104-improved-integers.md&gt;\. The goal is still to clean up Swifts integer APIs and make them more useful for generic programming.

The language has evolved in ways that affect integers APIs since the time the original proposal was approved for Swift 3. We also attempted to implement the proposed model in the standard library and found that some essential APIs were missing, whereas others could be safely removed.

Major changes to the APIs introduced by this proposal (as compared to SE-0104 <https://gist.github.com/moiseev/0104-improved-integers.md&gt;\) are listed in a dedicated section <Protocol-oriented integers (take 2) · GitHub.

<Protocol-oriented integers (take 2) · GitHub

Swift's integer protocols don't currently provide a suitable basis for generic programming. See this blog post <http://blog.krzyzanowskim.com/2015/03/01/swift_madness_of_generic_integer/&gt; for an example of an attempt to implement a generic algorithm over integers.

The way the Arithmetic protocol is defined, it does not generalize to floating point numbers and also slows down compilation by requiring every concrete type to provide an implementation of arithmetic operators, thus polluting the overload set.

Converting from one integer type to another is performed using the concept of the 'maximum width integer' (see MaxInt), which is an artificial limitation. The very existence of MaxInt makes it unclear what to do should someone implement Int256, for example.

Another annoying problem is the inability to use integers of different types in comparison and bit-shift operations. For example, the following snippets won't compile:

var x: Int8 = 42
let y = 1
let z = 0

x <<= y // error: binary operator '<<=' cannot be applied to operands of type 'Int8' and 'Int'
if x > z { ... } // error: binary operator '>' cannot be applied to operands of type 'Int8' and 'Int'
Currently, bit-shifting a negative number of (or too many) bits causes a trap on some platforms, which makes low-level bit manipulations needlessly dangerous and unpredictable.

Finally, the current design predates many of the improvements that came since Swift 1, and hasn't been revised since then.

<Protocol-oriented integers (take 2) · GitHub solution

We propose a new model that does not have above mentioned problems and is more easily extensible.

                +--------------+ +-------------+
        +------>+ Arithmetic | | Comparable |
        > > (+,-,*,/) | | (==,<,>,...)|
        > +-------------++ +---+---------+
        > ^ ^
+-------+------------+ | |
> SignedArithmetic | +-+-------+-----------+
> (unary -) | | BinaryInteger |
+------+-------------+ |(words,%,bitwise,...)|
       ^ ++---+-----+----------+
       > +-----------^ ^ ^---------------+
       > > > >
+------+---------++ +---------+---------------+ +--+----------------+
> SignedInteger | | FixedWidthInteger | | UnsignedInteger |
> > >(endianness,overflow,...)| | |
+---------------+-+ +-+--------------------+--+ +-+-----------------+
                ^ ^ ^ ^
                > > > >
                > > > >
               ++--------+-+ +-+-------+-+
               >Int family |-+ |UInt family|-+
               +-----------+ | +-----------+ |
                 +-----------+ +-----------+
There are several benefits provided by this model over the old one:

It allows mixing integer types in generic functions.

The possibility to initialize instances of any concrete integer type with values of any other concrete integer type enables writing functions that operate on more than one type conforming to BinaryInteger, such as heterogeneous comparisons or bit shifts, described later.

It removes the overload resolution overhead.

Arithmetic and bitwise operations can now be defined as generic operators on protocols. This approach significantly reduces the number of overloads for those operations, which used to be defined for every single concrete integer type.

It enables protocol sharing between integer and floating point types.

Note the exclusion of the % operation from Arithmetic. Its behavior for floating point numbers is sufficiently different from the one for integers that using it in generic context would lead to confusion. The FloatingPoint protocol introduced by SE-0067 <https://gist.github.com/moiseev/0067-floating-point-protocols.md&gt; should now refine SignedArithmetic.

It makes future extensions possible.

The proposed model eliminates the 'largest integer type' concept previously used to interoperate between integer types (see toIntMax in the current model) and instead provides access to machine words. It also introduces thedoubleWidthMultiply, doubleWidthDivide, and quotientAndRemainder methods. Together these changes can be used to provide an efficient implementation of bignums that would be hard to achieve otherwise.

The implementation of proposed model in the standard library is available in the new-integer-protocols branch <https://github.com/apple/swift/tree/new-integer-protocols&gt;\.

<Protocol-oriented integers (take 2) · GitHub note on bit shifts

This proposal introduces the concepts of smart shifts and masking shifts.

The semantics of shift operations are often undefined <LLVM Language Reference Manual — LLVM 18.0.0git documentation; in under- or over-shift cases. Smart shifts, implemented by >> and <<, are designed to address this problem and always behave in a well defined way, as shown in the examples below:

x << -2 is equivalent to x >> 2

(1 as UInt8) >> 42) will evaluate to 0

(-128 as Int8) >> 42) will evaluate to 0xff or -1

In most scenarios, the right hand operand is a literal constant, and branches for handling under- and over-shift cases can be optimized away. For other cases, this proposal provides masking shifts, implemented by &>> and &<<. A masking shift logically preprocesses the right hand operand by masking its bits to produce a value in the range 0...(x-1) where x is the number of bits in the left hand operand. On most architectures this masking is already performed by the CPU's shift instructions and has no cost. Both kinds of shift avoid undefined behavior and produce uniform semantics across architectures.

<Protocol-oriented integers (take 2) · GitHub design

<https://gist.github.com/moiseev/62ffe3c91b66866fdebf6f3fcc7cad8c#whats-new-since-se-0104&gt;What&#39;s new since SE-0104

SE-0091 <https://gist.github.com/moiseev/0091-improving-operators-in-protocols.md&gt; removed the necessity to dispatch generic operators through special methods.

All operators are now declared by protocols as static funcs.

Standard Library no longer provides + and - operators for Strideable types.

They were problematic, as one could have written mixed-type code like let x: Int64 = 42; x += (1 as Int), which would compile, but shouldn't. Besides, since the Stride of an unsigned type is signed, Standard Library had to implement a hack to make code like let x: UInt = 42; x += (1 as Int) ambiguous. These operators were only necessary because they made advancing collection indices convenient, which is no longer the case since the introduction of the new indexing model <https://gist.github.com/moiseev/0065-collections-move-indices.md&gt; in Swift 3.

Shifts and other bitwise operations were moved from FixedWidthInteger to BinaryInteger.

Left shift operation on an unbounded integer should infinitely extend the number, and never drop set bits when they reach the most significant position in the underlying representation.

BitwiseOperations protocol was deprecated.

We believe there are no useful entities that support bitwise operations, but at the same time are not binary integers.

minimumSignedRepresentationBitWidth property was removed.

trailingZeros property was added to the BinaryInteger protocol.

leadingZeros and popcount properties are still defined by the FixedWidthInteger protocol.

Endian-converting initializers and properties were added to the FixedWidthInteger protocol.

Standard library introduces the new type DoubleWidth<T>.

See this section <Protocol-oriented integers (take 2) · GitHub; for more details.

<Protocol-oriented integers (take 2) · GitHub

<Protocol-oriented integers (take 2) · GitHub

The Arithmetic protocol declares binary arithmetic operators – such as +, -, and * — and their mutating counterparts.

It provides a suitable basis for arithmetic on scalars such as integers and floating point numbers.

Both mutating and non-mutating operations are declared in the protocol, however only the mutating ones are required, as default implementations of the non-mutating ones are provided by a protocol extension.

The Magnitude associated type is able to hold the absolute value of any possible value of Self. Concrete types do not have to provide a type alias for it, as it can be inferred from the magnitude property. This property can be useful in operations that are simpler to implement in terms of unsigned values, for example, printing a value of an integer, which is just printing a '-' character in front of an absolute value.

Please note that for ordinary work, the magnitude property should not be preferred to the abs(_) function, whose return value is of the same type as its argument.

public protocol Arithmetic : Equatable, ExpressibleByIntegerLiteral {
  /// Creates a new instance from the given integer, if it can be represented
  /// exactly.
  ///
  /// If the value passed as `source` is not representable exactly, the result
  /// is `nil`. In the following example, the constant `x` is successfully
  /// created from a value of `100`, while the attempt to initialize the
  /// constant `y` from `1_000` fails because the `Int8` type can represent
  /// `127` at maximum:
  ///
  /// let x = Int8(exactly: 100)
  /// // x == Optional(100)
  /// let y = Int8(exactly: 1_000)
  /// // y == nil
  ///
  /// - Parameter source: A floating-point value to convert to an integer.
  init?<T : BinaryInteger>(exactly source: T)

  /// A type that can represent the absolute value of any possible value of the
  /// conforming type.
  associatedtype Magnitude : Equatable, ExpressibleByIntegerLiteral

  /// The magnitude of this value.
  ///
  /// For any numeric value `x`, `x.magnitude` is the absolute value of `x`.
  /// You can use the `magnitude` property in operations that are simpler to
  /// implement in terms of unsigned values, such as printing the value of an
  /// integer, which is just printing a '-' character in front of an absolute
  /// value.
  ///
  /// let x = -200
  /// // x.magnitude == 200
  ///
  /// The global `abs(_:)` function provides more familiar syntax when you need
  /// to find an absolute value. In addition, because `abs(_:)` always returns
  /// a value of the same type, even in a generic context, using the function
  /// instead of the `magnitude` property is encouraged.
  ///
  /// - SeeAlso: `abs(_:)`
  var magnitude: Magnitude { get }

  /// Returns the sum of the two given values.
  ///
  /// The sum of `lhs` and `rhs` must be representable in the same type. In the
  /// following example, the result of `100 + 200` is greater than the maximum
  /// representable `Int8` value:
  ///
  /// let x: Int8 = 10 + 21
  /// // x == 31
  /// let y: Int8 = 100 + 121
  /// // Overflow error
  static func +(_ lhs: Self, _ rhs: Self) -> Self

  /// Adds the given value to this value in place.
  ///
  /// For example:
  ///
  /// var x = 15
  /// y += 7
  /// // y == 22
  static func +=(_ lhs: inout Self, rhs: Self)

  /// Returns the difference of the two given values.
  ///
  /// The difference of `lhs` and `rhs` must be representable in the same type.
  /// In the following example, the result of `10 - 21` is less than zero, the
  /// minimum representable `UInt` value:
  ///
  /// let x: UInt = 21 - 10
  /// // x == 11
  /// let y: UInt = 10 - 21
  /// // Overflow error
  static func -(_ lhs: Self, _ rhs: Self) -> Self

  /// Subtracts the given value from this value in place.
  ///
  /// For example:
  ///
  /// var x = 15
  /// y -= 7
  /// // y == 8
  static func -=(_ lhs: inout Self, rhs: Self)

  /// Returns the product of the two given values.
  ///
  /// The product of `lhs` and `rhs` must be representable in the same type. In
  /// the following example, the result of `10 * 50` is greater than the
  /// maximum representable `Int8` value.
  ///
  /// let x: Int8 = 10 * 5
  /// // x == 50
  /// let y: Int8 = 10 * 50
  /// // Overflow error
  static func *(_ lhs: Self, _ rhs: Self) -> Self

  /// Multiples this value by the given value in place.
  ///
  /// For example:
  ///
  /// var x = 15
  /// y *= 7
  /// // y == 105
  static func *=(_ lhs: inout Self, rhs: Self)

  /// Returns the quotient of dividing the first value by the second.
  ///
  /// For integer types, any remainder of the division is discarded.
  ///
  /// let x = 21 / 5
  /// // x == 4
  static func /(_ lhs: Self, _ rhs: Self) -> Self

  /// Divides this value by the given value in place.
  ///
  /// For example:
  ///
  /// var x = 15
  /// y /= 7
  /// // y == 2
  static func /=(_ lhs: inout Self, rhs: Self)
}

extension Arithmetic {
  public init() { self = 0 }

  public static prefix func + (x: Self) -> Self {
    return x
  }
}
<Protocol-oriented integers (take 2) · GitHub

The SignedArithmetic protocol is for numbers that can be negated.

public protocol SignedArithmetic : Arithmetic {
  /// Returns the additive inverse of this value.
  ///
  /// let x = 21
  /// let y = -x
  /// // y == -21
  ///
  /// - Returns: The additive inverse of this value.
  ///
  /// - SeeAlso: `negate()`
  static prefix func - (_ operand: Self) -> Self

  /// Replaces this value with its additive inverse.
  ///
  /// The following example uses the `negate()` method to negate the value of
  /// an integer `x`:
  ///
  /// var x = 21
  /// x.negate()
  /// // x == -21
  ///
  /// - SeeAlso: The unary minus operator (`-`).
  mutating func negate()
}

extension SignedArithmetic {
  public static prefix func - (_ operand: Self) -> Self {
    var result = operand
    result.negate()
    return result
  }

  public mutating func negate() {
    self = Self() - self
  }
}
<Protocol-oriented integers (take 2) · GitHub

The BinaryInteger protocol is the basis for all the integer types provided by the standard library.

This protocol adds a few new initializers. Two of them allow to create integers from floating point numbers, others support construction from instances of any type conforming to BinaryInteger, using different strategies:

Initialize Self with the value, provided that the value is representable. The precondition should be satisfied by the caller.

Extend or truncate the value to fit into Self

Clamp the value to the representable range of Self

BinaryInteger also declares bitwise and shift operators.

public protocol BinaryInteger :
  Comparable, Hashable, Arithmetic, CustomStringConvertible, Strideable {

  /// A Boolean value indicating whether this type is a signed integer type.
  ///
  /// *Signed* integer types can represent both positive and negative values.
  /// *Unsigned* integer types can represent only nonnegative values.
  static var isSigned: Bool { get }

  /// Creates an integer from the given floating-point value, if it can be
  /// represented exactly.
  ///
  /// If the value passed as `source` is not representable exactly, the result
  /// is `nil`. In the following example, the constant `x` is successfully
  /// created from a value of `21.0`, while the attempt to initialize the
  /// constant `y` from `21.5` fails:
  ///
  /// let x = Int(exactly: 21.0)
  /// // x == Optional(21)
  /// let y = Int(exactly: 21.5)
  /// // y == nil
  ///
  /// - Parameter source: A floating-point value to convert to an integer.
  init?<T : FloatingPoint>(exactly source: T)

  /// Creates an integer from the given floating-point value, truncating any
  /// fractional part.
  ///
  /// Truncating the fractional part of `source` is equivalent to rounding
  /// toward zero.
  ///
  /// let x = Int(21.5)
  /// // x == 21
  /// let y = Int(-21.5)
  /// // y == -21
  ///
  /// If `source` is outside the bounds of this type after truncation, a
  /// runtime error may occur.
  ///
  /// let z = UInt(-21.5)
  /// // Error: ...the result would be less than UInt.min
  ///
  /// - Parameter source: A floating-point value to convert to an integer.
  /// `source` must be representable in this type after truncation.
  init<T : FloatingPoint>(_ source: T)

  /// Creates an new instance from the given integer.
  ///
  /// If the value passed as `source` is not representable in this type, a
  /// runtime error may occur.
  ///
  /// let x = -500 as Int
  /// let y = Int32(x)
  /// // y == -500
  ///
  /// // -500 is not representable as a 'UInt32' instance
  /// let z = UInt32(x)
  /// // Error
  ///
  /// - Parameter source: An integer to convert. `source` must be representable
  /// in this type.
  init<T : BinaryInteger>(_ source: T)

  /// Creates a new instance from the bit pattern of the given instance by
  /// sign-extending or truncating to fit this type.
  ///
  /// When the bit width of `T` (the type of `source`) is equal to or greater
  /// than this type's bit width, the result is the truncated
  /// least-significant bits of `source`. For example, when converting a
  /// 16-bit value to an 8-bit type, only the lower 8 bits of `source` are
  /// used.
  ///
  /// let p: Int16 = -500
  /// // 'p' has a binary representation of 11111110_00001100
  /// let q = Int8(extendingOrTruncating: p)
  /// // q == 12
  /// // 'q' has a binary representation of 00001100
  ///
  /// When the bit width of `T` is less than this type's bit width, the result
  /// is *sign-extended* to fill the remaining bits. That is, if `source` is
  /// negative, the result is padded with ones; otherwise, the result is
  /// padded with zeros.
  ///
  /// let u: Int8 = 21
  /// // 'u' has a binary representation of 00010101
  /// let v = Int16(extendingOrTruncating: u)
  /// // v == 21
  /// // 'v' has a binary representation of 00000000_00010101
  ///
  /// let w: Int8 = -21
  /// // 'w' has a binary representation of 11101011
  /// let x = Int16(extendingOrTruncating: w)
  /// // x == -21
  /// // 'x' has a binary representation of 11111111_11101011
  /// let y = UInt16(extendingOrTruncating: w)
  /// // y == 65515
  /// // 'y' has a binary representation of 11111111_11101011
  ///
  /// - Parameter source: An integer to convert to this type.
  init<T : BinaryInteger>(extendingOrTruncating source: T)

  /// Creates a new instance with the representable value that's closest to the
  /// given integer.
  ///
  /// If the value passed as `source` is greater than the maximum representable
  /// value in this type, the result is the type's `max` value. If `source` is
  /// less than the smallest representable value in this type, the result is
  /// the type's `min` value.
  ///
  /// In this example, `x` is initialized as an `Int8` instance by clamping
  /// `500` to the range `-128...127`, and `y` is initialized as a `UInt`
  /// instance by clamping `-500` to the range `0...UInt.max`.
  ///
  /// let x = Int8(clamping: 500)
  /// // x == 127
  /// // x == Int8.max
  ///
  /// let y = UInt(clamping: -500)
  /// // y == 0
  ///
  /// - Parameter source: An integer to convert to this type.
  init<T : BinaryInteger>(clamping source: T)

  /// Returns the n-th word, counting from the least significant to most
  /// significant, of this value's binary representation.
  ///
  /// The `word(at:)` method returns negative values in two's complement
  /// representation, regardless of a type's underlying implementation. If `n`
  /// is greater than the number of words in this value's current
  /// representation, the result is `0` for positive numbers and `~0` for
  /// negative numbers.
  ///
  /// - Parameter n: The word to return, counting from the least significant to
  /// most significant. `n` must be greater than or equal to zero.
  /// - Returns: An word-sized, unsigned integer with the bit pattern of the
  /// n-th word of this value.
  func word(at n: Int) -> UInt

  /// The number of bits in the current binary representation of this value.
  ///
  /// This property is a constant for instances of fixed-width integer
  /// types.
  var bitWidth : Int { get }

  /// The number of trailing zeros in this value's binary representation.
  ///
  /// For example, in a fixed-width integer type with a `bitWidth` value of 8,
  /// the number -8 has three trailing zeros.
  ///
  /// let x = Int8(bitPattern: 0b1111_1000)
  /// // x == -8
  /// // x.trailingZeros == 3
  var trailingZeros: Int { get }

  /// Returns the remainder of dividing the first value by the second.
  ///
  /// The result has the same sign as `lhs` and is less than `rhs.magnitude`.
  ///
  /// let x = 22 % 5
  /// // x == 2
  /// let y = 22 % -5
  /// // y == 2
  /// let z = -22 % -5
  /// // z == -2
  ///
  /// - Parameters:
  /// - lhs: The value to divide.
  /// - rhs: The value to divide `lhs` by. `rhs` must not be zero.
  static func %(_ lhs: Self, _ rhs: Self) -> Self

  /// Replaces this value with the remainder of itself divided by the given
  /// value. For example:
  ///
  /// var x = 15
  /// x %= 7
  /// // x == 1
  ///
  /// - Parameter rhs: The value to divide this value by. `rhs` must not be
  /// zero.
  ///
  /// - SeeAlso: `remainder(dividingBy:)`
  static func %=(_ lhs: inout Self, _ rhs: Self)

  /// Returns the inverse of the bits set in the argument.
  ///
  /// The bitwise NOT operator (`~`) is a prefix operator that returns a value
  /// in which all the bits of its argument are flipped: Bits that are `1` in
  /// the argument are `0` in the result, and bits that are `0` in the argument
  /// are `1` in the result. This is equivalent to the inverse of a set. For
  /// example:
  ///
  /// let x: UInt8 = 5 // 0b00000101
  /// let notX = ~x // 0b11111010
  ///
  /// Performing a bitwise NOT operation on 0 returns a value with every bit
  /// set to `1`.
  ///
  /// let allOnes = ~UInt8.min // 0b11111111
  ///
  /// - Complexity: O(1).
  static prefix func ~ (_ x: Self) -> Self

  /// Returns the result of performing a bitwise AND operation on this value
  /// and the given value.
  ///
  /// A bitwise AND operation results in a value that has each bit set to `1`
  /// where *both* of its arguments have that bit set to `1`. For example:
  ///
  /// let x: UInt8 = 5 // 0b00000101
  /// let y: UInt8 = 14 // 0b00001110
  /// let z = x & y // 0b00000100
  static func &(_ lhs: Self, _ rhs: Self) -> Self
  static func &=(_ lhs: inout Self, _ rhs: Self)

  /// Returns the result of performing a bitwise OR operation on this value and
  /// the given value.
  ///
  /// A bitwise OR operation results in a value that has each bit set to `1`
  /// where *one or both* of its arguments have that bit set to `1`. For
  /// example:
  ///
  /// let x: UInt8 = 5 // 0b00000101
  /// let y: UInt8 = 14 // 0b00001110
  /// let z = x | y // 0b00001111
  static func |(_ lhs: Self, _ rhs: Self) -> Self
  static func |=(_ lhs: inout Self, _ rhs: Self)

  /// Returns the result of performing a bitwise XOR operation on this value
  /// and the given value.
  ///
  /// A bitwise XOR operation, also known as an exclusive OR operation, results
  /// in a value that has each bit set to `1` where *one or the other but not
  /// both* of its arguments had that bit set to `1`. For example:
  ///
  /// let x: UInt8 = 5 // 0b00000101
  /// let y: UInt8 = 14 // 0b00001110
  /// let z = x ^ y // 0b00001011
  static func ^(_ lhs: Self, _ rhs: Self) -> Self
  static func ^=(_ lhs: inout Self, _ rhs: Self)

  /// Returns the result of shifting this value's binary representation the
  /// specified number of digits to the right.
  ///
  /// In a *masking shift*, the bit pattern of the value passed as `rhs` is
  /// masked to produce a value between zero and the bit width of `lhs`. The
  /// shift is performed using this masked value. Masking shifts require more
  /// care to use correctly than a traditional bit shift, but are likely to be
  /// more efficient when used with shift amounts that are not compile-time
  /// constants. On most architectures, a masking shift compiles down to a
  /// single instruction.
  ///
  /// For example, if you shift an 8-bit, unsigned integer by 2, the shift
  /// amount requires no masking.
  ///
  /// let x: UInt8 = 30 // 0b00011110
  /// let y = x &>> 2
  /// // y == 7 // 0b00000111
  ///
  /// However, if you shift it by 11, it first bitmasks `rhs` to `3`, and then
  /// uses that masked value as the number of bits to shift `x`.
  ///
  /// let z = x &>> 11
  /// // z == 3 // 0b00000011
  ///
  /// Relationship to the Right Shift Operator
  /// ----------------------------------------
  ///
  /// The masking right shift operator handles attempted overshifts and
  /// undershifts differently from the right shift operator (`>>`). When the
  /// value passed as `rhs` in a masking shift is within the range
  /// `0...<bitWidth`, the operation is equivalent to using the right shift
  /// operator.
  ///
  /// let x: UInt8 = 30 // 0b00011110
  /// let y1 = x &>> 2
  /// // y1 == 7 // 0b00000111
  /// let y2 = x >> 2
  /// // y2 == 7 // 0b00000111
  ///
  /// The right shift operator does not mask its right-hand-side argument, so
  /// passing `11` as `rhs` shifts all the bits of `x` to zero.
  ///
  /// let z1 = x &>> 11
  /// // z1 == 240 // 0b00000011
  /// let z2 = x >> 11
  /// // z2 == 0 // 0b00000000
  ///
  /// - Parameter rhs: The number of bits to shift this value to the right. If
  /// `rhs` is outside the range `0..<bitWidth`, it is masked to produce a
  /// value within that range.
  /// - Returns: The result of shifting this value by the masked `rhs` to the
  /// right.
  ///
  /// - SeeAlso: `&<<`, `>>`
  static func &>>(_ lhs: Self, _ rhs: Self) -> Self
  static func &>>=(_ lhs: inout Self, _ rhs: Self)

  /// Returns the result of shifting this value's binary representation the
  /// specified number of digits to the left.
  ///
  /// In a *masking shift*, the bit pattern of the value passed as `rhs` is
  /// masked to produce a value between zero and the bit width of `lhs`. The
  /// shift is performed using this masked value. Masking shifts require more
  /// care to use correctly than a traditional bit shift, but are likely to be
  /// more efficient when used with shift amounts that are not compile-time
  /// constants. On most architectures, a masking shift compiles down to a
  /// single instruction.
  ///
  /// For example, if you shift an 8-bit, unsigned integer by 2, the shift
  /// amount requires no masking.
  ///
  /// let x: UInt8 = 30 // 0b00011110
  /// let y = x &>> 2
  /// // y == 120 // 0b01111000
  ///
  /// However, if you shift it by 11, it first bitmasks `rhs` to `3`, and then
  /// uses that masked value as the number of bits to shift `x`.
  ///
  /// let z = x &<< 11
  /// // z == 240 // 0b11110000
  ///
  /// Relationship to the Left Shift Operator
  /// ---------------------------------------
  ///
  /// The masking left shift operator handles attempted overshifts and
  /// undershifts differently from the left shift operator (`<<`). When the
  /// value passed as `rhs` in a masking shift is within the range
  /// `0...<bitWidth`, the operation is equivalent to using the left shift
  /// operator.
  ///
  /// let x: UInt8 = 30 // 0b00011110
  /// let y1 = x &<< 2
  /// // y1 == 120 // 0b01111000
  /// let y2 = x << 2
  /// // y2 == 120 // 0b01111000
  ///
  /// The left shift operator does not mask its right-hand-side argument, so
  /// passing `11` as `rhs` shifts all the bits of `x` to zero.
  ///
  /// let z1 = x &<< 11
  /// // z1 == 240 // 0b11110000
  /// let z2 = x << 11
  /// // z2 == 0 // 0b00000000
  ///
  /// - Parameter rhs: The number of bits to shift this value to the left. If
  /// `rhs` is outside the range `0..<bitWidth`, it is masked to produce a
  /// value within that range.
  /// - Returns: The result of shifting this value by the masked `rhs` to the
  /// left.
  ///
  /// - SeeAlso: `&>>`, `<<`
  static func &<<(_ lhs: Self, _ rhs: Self) -> Self
  static func &<<=(_ lhs: inout Self, _ rhs: Self)

  /// Returns the quotient and remainder of this value divided by the given
  /// value.
  ///
  /// Use this method to calculate the quotient and remainder of a division at
  /// the same time.
  ///
  /// let x = 1_000_000
  /// let (q, r) = x.quotientAndRemainder(dividingBy: 933)
  /// // q == 1071
  /// // r == 757
  ///
  /// - Parameter rhs: The value to divide this value by.
  /// - Returns: A tuple containing the quotient and remainder of this value
  /// divided by `rhs`.
  func quotientAndRemainder(dividingBy rhs: Self)
    -> (quotient: Self, remainder: Self)

  /// Returns `-1` if this value is negative and `1` if it's positive;
  /// otherwise, `0`.
  ///
  /// - Returns: The sign of this number, expressed as an integer of the same
  /// type.
  func signum() -> Self
}
<Protocol-oriented integers (take 2) · GitHub

The FixedWidthInteger protocol adds the notion of endianness as well as static properties for type bounds and bit width.

The WithOverflow family of methods is used in default implementations of mutating arithmetic methods (see the Arithmetic protocol). Having these methods allows the library to provide both bounds-checked and masking implementations of arithmetic operations, without duplicating code.

The doubleWidthMultiply and doubleWidthDivide methods are necessary building blocks to implement support for integer types of a greater width such as arbitrary-precision integers.

public protocol FixedWidthInteger : BinaryInteger {
  /// The number of bits used for the underlying binary representation of
  /// values of this type.
  ///
  /// An unsigned, fixed-width integer type can represent values from 0 through
  /// `(2 ** bitWidth) - 1`, where `**` is exponentiation. A signed,
  /// fixed-width integer type can represent values from
  /// `-(2 ** bitWidth - 1)` through `(2 ** bitWidth - 1) - 1`. For example,
  /// the `Int8` type has a `bitWidth` value of 8 and can store any integer in
  /// the range `-128...127`.
  static var bitWidth : Int { get }

  /// The maximum representable integer in this type.
  ///
  /// For unsigned integer types, this value is `(2 ** bitWidth) - 1`, where
  /// `**` is exponentiation. For signed integer types, this value is
  /// `(2 ** bitWidth - 1) - 1`.
  static var max: Self { get }

  /// The minimum representable value.
  ///
  /// For unsigned integer types, this value is always `0`. For signed integer
  /// types, this value is `-(2 ** bitWidth - 1)`, where `**` is
  /// exponentiation.
  static var min: Self { get }

  /// Returns the sum of this value and the given value along with a flag
  /// indicating whether overflow occurred in the operation.
  ///
  /// - Parameter other: The value to add to this value.
  /// - Returns: A tuple containing the result of the addition along with a
  /// flag indicating whether overflow occurred. If the `overflow` component
  /// is `.none`, the `partialValue` component contains the entire sum. If
  /// the `overflow` component is `.overflow`, an overflow occurred and the
  /// `partialValue` component contains the truncated sum of this value and
  /// `other`.
  ///
  /// - SeeAlso: `+`
  func addingWithOverflow(_ other: Self)
    -> (partialValue: Self, overflow: ArithmeticOverflow)

  /// Returns the difference of this value and the given value along with a
  /// flag indicating whether overflow occurred in the operation.
  ///
  /// - Parameter other: The value to subtract from this value.
  /// - Returns: A tuple containing the result of the subtraction along with a
  /// flag indicating whether overflow occurred. If the `overflow` component
  /// is `.none`, the `partialValue` component contains the entire
  /// difference. If the `overflow` component is `.overflow`, an overflow
  /// occurred and the `partialValue` component contains the truncated
  /// result of `other` subtracted from this value.
  ///
  /// - SeeAlso: `-`
  func subtractingWithOverflow(_ other: Self)
    -> (partialValue: Self, overflow: ArithmeticOverflow)

  /// Returns the product of this value and the given value along with a flag
  /// indicating whether overflow occurred in the operation.
  ///
  /// - Parameter other: The value to multiply by this value.
  /// - Returns: A tuple containing the result of the multiplication along with
  /// a flag indicating whether overflow occurred. If the `overflow`
  /// component is `.none`, the `partialValue` component contains the entire
  /// product. If the `overflow` component is `.overflow`, an overflow
  /// occurred and the `partialValue` component contains the truncated
  /// product of this value and `other`.
  ///
  /// - SeeAlso: `*`, `doubleWidthMultiply(_:_:)`
  func multipliedWithOverflow(by other: Self)
    -> (partialValue: Self, overflow: ArithmeticOverflow)

  /// Returns the quotient of dividing this value by the given value along with
  /// a flag indicating whether overflow occurred in the operation.
  ///
  /// For a value `x`, if zero is passed as `other`, the result is
  /// `(x, .overflow)`.
  ///
  /// - Parameter other: The value to divide this value by.
  /// - Returns: A tuple containing the result of the division along with a
  /// flag indicating whether overflow occurred. If the `overflow` component
  /// is `.none`, the `partialValue` component contains the entire quotient.
  /// If the `overflow` component is `.overflow`, an overflow occurred and
  /// the `partialValue` component contains the truncated quotient.
  ///
  /// - SeeAlso: `/`, `doubleWidthDivide(_:_:)`
  func dividedWithOverflow(by other: Self)
    -> (partialValue: Self, overflow: ArithmeticOverflow)

  /// Returns a tuple containing the high and low parts of the result of
  /// multiplying its arguments.
  ///
  /// Use this method to calculate the full result of a product that would
  /// otherwise overflow. Unlike traditional truncating multiplication, the
  /// `doubleWidthMultiply(_:_:)` method returns both the `high` and `low`
  /// parts of the product of `lhs` and `rhs`. The following example uses this
  /// method to multiply two `UInt8` values that normally overflow when
  /// multiplied:
  ///
  /// let x: UInt8 = 100
  /// let y: UInt8 = 20
  /// let result = UInt8.doubleWidthMultiply(100, 20)
  /// // result.high == 0b00000111
  /// // result.low == 0b11010000
  ///
  /// The product of `x` and `y` is 2000, which is too large to represent in a
  /// `UInt8` instance. The `high` and `low` components of the `result` tuple
  /// represent 2000 when concatenated to form a double-width integer; that
  /// is, using `result.high` as the high byte and `result.low` as the low byte
  /// of a `UInt16` instance.
  ///
  /// let z = UInt16(result.high) << 8 | UInt16(result.low)
  /// // z == 2000
  ///
  /// - Parameters:
  /// - lhs: A value to multiply.
  /// - rhs: Another value to multiply.
  /// - Returns: A tuple containing the high and low parts of the result of
  /// multiplying `lhs` and `rhs`.
  ///
  /// - SeeAlso: `multipliedWithOverflow(by:)`
  static func doubleWidthMultiply(_ lhs: Self, _ rhs: Self)
    -> (high: Self, low: Magnitude)

  /// Returns a tuple containing the quotient and remainder of dividing the
  /// first argument by the second.
  ///
  /// The resulting quotient must be representable within the bounds of the
  /// type. If the quotient of dividing `lhs` by `rhs` is too large to
  /// represent in the type, a runtime error may occur.
  ///
  /// - Parameters:
  /// - lhs: A tuple containing the high and low parts of a double-width
  /// integer. The `high` component of the tuple carries the sign, if the
  /// type is signed.
  /// - rhs: The integer to divide into `lhs`.
  /// - Returns: A tuple containing the quotient and remainder of `lhs` divided
  /// by `rhs`.
  static func doubleWidthDivide(
    _ lhs: (high: Self, low: Magnitude), _ rhs: Self)
    -> (quotient: Self, remainder: Self)

  /// The number of bits equal to 1 in this value's binary representation.
  ///
  /// For example, in a fixed-width integer type with a `bitWidth` value of 8,
  /// the number 31 has five bits equal to 1.
  ///
  /// let x: Int8 = 0b0001_1111
  /// // x == 31
  /// // x.popcount == 5
  var popcount: Int { get }

  /// The number of leading zeros in this value's binary representation.
  ///
  /// For example, in a fixed-width integer type with a `bitWidth` value of 8,
  /// the number 31 has three leading zeros.
  ///
  /// let x: Int8 = 0b0001_1111
  /// // x == 31
  /// // x.leadingZeros == 3
  /// - SeeAlso: `BinaryInteger.trailingZeros`
  var leadingZeros: Int { get }

  /// Creates an integer from its big-endian representation, changing the
  /// byte order if necessary.
  init(bigEndian value: Self)

  /// Creates an integer from its little-endian representation, changing the
  /// byte order if necessary.
  init(littleEndian value: Self)

  /// The big-endian representation of this integer.
  ///
  /// If necessary, the byte order of this value is reversed from the typical
  /// byte order of this integer type. On a big-endian platform, for any
  /// integer `x`, `x == x.bigEndian`.
  ///
  /// - SeeAlso: `littleEndian`
  var bigEndian: Self { get }

  /// The little-endian representation of this integer.
  ///
  /// If necessary, the byte order of this value is reversed from the typical
  /// byte order of this integer type. On a little-endian platform, for any
  /// integer `x`, `x == x.littleEndian`.
  ///
  /// - SeeAlso: `bigEndian`
  var littleEndian: Self { get }

  /// A representation of this integer with the byte order swapped.
  var byteSwapped: Self { get }
}
<Protocol-oriented integers (take 2) · GitHub protocols

public protocol UnsignedInteger : BinaryInteger {
  associatedtype Magnitude : BinaryInteger
}
public protocol SignedInteger : BinaryInteger, SignedArithmetic {
  associatedtype Magnitude : BinaryInteger
}
<Protocol-oriented integers (take 2) · GitHub

The DoubleWidth<T> type allows to create wider fixed-width integer types from the ones available in the standard library.

Standard library currently provides fixed-width integer types of up to 64 bits. A value of DoubleWidth<Int64> will double the range of the underlying type and implement all the FixedWidthInteger requirements. Please note though that the implementation will not necessarily be the most efficient one, so it would not be a good idea to use DoubleWidth<Int32>instead of a built-in Int64.

<Protocol-oriented integers (take 2) · GitHub operators

In addition to the operators described in the protocols section <Protocol-oriented integers (take 2) · GitHub, we also provide a few extensions that are not protocol requirements:

<Protocol-oriented integers (take 2) · GitHub shifts

extension BinaryInteger {
  // Masking shifts
  static func &>> <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Self
  static func &>>= <Other : BinaryInteger>(lhs: inout Self, rhs: Other)
  static func &<< <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Self
  static func &<<= <Other : BinaryInteger>(lhs: inout Self, rhs: Other)

  // 'Smart' shifts
  static func >> <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Self
  static func >>= <Other : BinaryInteger>(lhs: inout Self, rhs: Other)
  static func << <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Self
  static func <<= <Other : BinaryInteger>(lhs: inout Self, rhs: Other)
}
<Protocol-oriented integers (take 2) · GitHub equality and comparison

extension BinaryInteger {
  // Equality
  static func == <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Bool
  static func != <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Bool

  // Comparison
  static func < <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Bool
  static func <= <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Bool
  static func > <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Bool
  static func >= <Other : BinaryInteger>(lhs: Self, rhs: Other) -> Bool
}
<Protocol-oriented integers (take 2) · GitHub arithmetic

public func &* <T: FixedWidthInteger>(lhs: T, rhs: T) -> T
public func &- <T: FixedWidthInteger>(lhs: T, rhs: T) -> T
public func &+ <T: FixedWidthInteger>(lhs: T, rhs: T) -> T
<Protocol-oriented integers (take 2) · GitHub

This proposal:

DOES NOT solve the integer promotion problem, which would allow mixed-type arithmetic. However, we believe that it is an important step in the right direction.

DOES NOT include the implementation of a BigInt type, but allows it to be implemented in the future.

<Protocol-oriented integers (take 2) · GitHub compatibility

The proposed change is designed to be as non-breaking as possible, and it has been proven that it does not break code on concrete integer types. However, there are still a few API breaking changes in the realm of generic code:

Integer protocols in Swift up to and including version 3 were not particularly useful for generic programming, but were rather a means of sharing implementation between conforming types. Therefore we believe the amount of code that relied on these protocols is relatively small. The breakage can be further reduced by introducing proper aliases for the removed protocols with deprecation warnings.

Deprecation of the BitwiseOperations protocol. We find it hard to imagine a type that conforms to this protocol, but is not a binary integer type.

Addition of 'smart' shifts will change the behavior of existing code. It will still compile, but will be potentially less performant due to extra logic involved. In a case, where this becomes a problem, newly introduced masking shift operators can be used instead. Unfortunately, performance characteristics of the code cannot be statically checked, and thus migration cannot be provided.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution