Commit Graph

44 Commits

Author SHA1 Message Date
Stephen Canon
a3ba517837 Mark legacy overloads of &+ and &- unavailable. (#38723)
These were defined for both FixedWidthInteger and FixedWidthInteger & SignedInteger for source compatibility with Swift 3; the latter set probably should have been removed when we stabilized the ABI, but were not. We can't easily remove them entirely (because we need them for ABI stability now), but we can mark them unavailable so that the typechecker doesn't have to consider them..
2021-08-05 21:18:48 -04:00
Stephen Canon
ce122dbb81 Clarify the documentation of SignedNumeric.
Previously we said that a type conforming to SignedNumeric allows positive and negative values, but that's nonsense. A type conforms to signed numeric if it has a negate() operation; it doesn't even have to have a notion of positive or negative values (for examples, see complex numbers, quaternions, integers mod n, etc).
2021-08-04 23:03:33 -04:00
Alex Martini
1c5c111aab Merge pull request #36709 from amartini51/main
Doc comment corrections
2021-04-05 15:46:31 -07:00
Meghana Gupta
dbea482085 Refactor FixedWidthInteger init (#36485) 2021-03-18 13:47:12 -07:00
Alex Martini
0b9ae84d66 Remove misleading comment about .zero.
There's nothing useful to say about the type of .zero -- it's
constrained by the protocol to Self, so you don't have any choice what
it is when conforming to AdditiveArithmetic.

Fixes <rdar://problem/72592923> (SR-13983).
2021-02-17 15:09:49 -08:00
Patrick Pijnappel
8ecd884b62 [stdlib] Expand integer conversion comment 2020-10-19 17:55:50 +11:00
Patrick Pijnappel
87c177c4b6 [stdlib] Change misleading comment 2020-10-19 10:51:18 +11:00
Stephen Canon
294bde951b Documentation fix: remainder matches sign of lhs, not rhs. 2020-08-31 08:46:24 -04:00
Nate Cook
2938466ecb Update docs for numericCast (#31464)
Drops outdated random number generation example and simplifies a bit.

Fixes <rdar://problem/57888487>
2020-05-01 15:14:33 -05:00
Ben Rimmington
682c1672b6 [stdlib] Update file headers of de-gybbed files 2020-04-01 22:14:03 +01:00
Alex Martini
c0e2f08818 Add missing backtick.
Fixes rdar://problem/58855960
2020-03-05 11:17:50 -08:00
NevinBR
a7f28785c9 Fixes for FixedWidthInteger.random(in: ClosedRange<Self>, using:) (#29633)
Allow the closed-range version of `FixedWidthInteger.random(in:using:)` to work for types larger than 64 bits when the entire valid range (`.min ... .max`) is passed in.

Also, closed ranges are never empty, so the unnecessary `!isEmpty` precondition has been removed.
2020-02-05 17:17:20 +01:00
Ravi Kandhadai
c198c1a2aa [Constant Evaluator] Add support for BinaryInteger.description which
converts an integer to a string. This patch adds a @_semantics
annotation to the BinaryInteger.description function.
2020-01-27 21:13:50 -08:00
Luciano Almeida
392baefc47 [stdlib][Qol] SR-11295 Removing stdlib unnecessary coercions (#27165)
* Removing unnecessary casts from stdlib

* Minor adjustments

* [stdlib][qol] Clean up error variable assign NSError

* Removing unnecessary coercions after removing DeclRefExpr restriction.
2019-12-11 07:30:25 -08:00
Paul Hudson
06f82a53b5 Replaced the majority of ' : ' with ': '. 2019-07-18 20:46:07 +01:00
Richard Wei
259571a7f7 Add default implementations for AdditiveArithmetic.{+=, -=}. (#25321)
This patch defines a default implementation for `+=` and `-=` in `extension AdditiveArithmetic` implemented in terms of `+` and `-`.
2019-07-04 22:57:27 -07:00
Stephen Canon
166d0fa961 FloatingPoint.radix should be transparent. (#25789)
* FloatingPoint.radix should be inlinable.

* Switch transparent to inlinable+inline(__always).
2019-06-26 18:20:27 -04:00
t.ae
f96cbb8a36 Make AdditiveArithmetic.zero @_transparent (#25658) 2019-06-26 09:07:30 -04:00
Stephen Canon
2df36527d3 Provide a default implementation of multipliedFullWidth (#25346)
* Provide a default implementation of multipliedFullWidth

Previously, [U]Int64 fatalErrored on 32b platforms, which is obviously undesirable. This PR provides a default implementation on FixedWidthInteger, which is not ideally efficient for all types, but is correct, and gives the optimizer all the information that it needs to generate good code in the important case of Int64 arithmetic on 32b platforms. There's still some minor room for improvement, but we'll call that an optimizer bug now.

* Clarify comments somewhat, remove `merge` nested function

I was only using `merge` in one place, so making it a function seems unnecessary. Also got rid of some trucatingIfNeeded inits where the compiler is able to reason that no checks are needed anyway.

* Add some basic test coverage specifically for multipliedFullWidth

* Fix typo, further clarify bounds comments.

* Make new defaulted implementation @_aEIC so we don't need availability.
2019-06-11 22:02:48 -07:00
Xiaodi Wu
da9f67c781 [docs] Update trailingZeroBitCount documentation (#24713)
* [docs] Update `trailingZeroBitCount` documentation

Document the required behavior for `trailingZeroBitCount` when the value is zero. Namely, in that scenario, `trailingZeroBitCount` should be equal to `bitWidth`.

* [docs] Address reviewer comments on `trailingZeroBitCount` docs
2019-05-13 08:53:28 -04:00
USAMI Kosuke
aa9a0d4b16 Fix document : Numeric -> AdditiveArithmetic 2019-03-11 21:58:43 +09:00
Maxim Moiseev
02cc2256e9 [stdlib] Remove a bunch of declarations marked as obsoleted in 4 2019-01-29 11:13:47 -08:00
Nate Cook
f19aca6cb0 [WIP] Revise documentation for Swift 5 stdlib additions (#21333)
* Revise the Unicode scalar/Character properties
* Minor revisions to `compactMapValues` docs.
* Add documentation for AdditiveArithmetic, revise Numeric
* Apply minor style updates to count(where:).
* Revise string interpolation docs.

- Convert table of interpolation examples to a list of examples. Tables
aren't supported by Swift markup, so this wouldn't render properly in
Xcode or on the web.
- Add a description of what a user must implement in a custom
string interpolation type to get the behavior they want.

* Revise isMultiple(of:) docs.

- In particular, add emphasis to mathematical symbols and equations to
match how we document such things elsewhere.
- I'm using asterisks for single symbols, and underscores for equations
because it's easier to read in-source when you don't have to escape
multiplication within emphasis.

* Add some abstracts to the SIMD vector types.

- Adds a dictionary of spelled out numbers. Only numbers < 10
  should be spelled out according to editorial.
- Adds abstracts to some of the basic members.
- Includes parameter descriptions for the xyzw properties and inits,
but not for the unlabeled initializers. Combined with the protocol
extension method abstracts, this should complete coverage of the concrete
types.
2019-01-03 18:19:20 -06:00
Valeriy Van
0e5d18654e Fixes comment for binaryString example snippet in FixedWidthInteger 2018-12-10 17:12:31 +01:00
Doug Gregor
ab75577079 [stdlib] Teach abs(_:) to use magnitude when it can.
Within the (single) implementation of abs(_:), dynamically check whether
the numeric type and its `Magnitude` are of the same type and, if so,
return the result of `magnitude`. This ensures that we do the right thing
with respect to (e.g.) floating point values like -0.0, without resorting
to overloading of abs(_:).
2018-12-05 21:05:52 -08:00
Doug Gregor
85d488d461 [stdlib] Remove magnitude-based overload of abs(_:).
The standard library has two versions of the `abs(_:)` function:

```
func abs<T : SignedNumeric>(_ x: T) -> T where T.Magnitude == T
func abs<T : SignedNumeric & Comparable>(_ x: T) -> T
```

The first is more specialized than the second because `T.Magnitude` is
known to conform to `Comparable`. Indeed, it’s a more specialized
implementation that returns `magnitude`.

However, this overload behaves oddly: in the expression `abs(-8)`, the type
checker will pick the first overload because it is more specialized. That’s
a general guiding principle for overloading: pick the most specialized
overload that works.

However, to select that overload, it needs to pick a type for the literal
“8” for which that overload works, and it chooses `Double`. The “obvious”
answer, `Int`, doesn’t work because `Int.Magnitude == UInt`.

There is a conflict between the two rules, here: we prefer more-specialized
overloads (but we’ll fall back to less-specialized if those don’t work) and we prefer to use `Int` for integer literals (but we’ll fall back to `Double` if it doesn’t work). We have a few options from a type-checker
perspective:

1. Consider the more-specialized-function rule to be more important
2. Consider the integer-literals-prefer-`Int` rule to be more important
3. Call the result ambiguous and make the user annotate it

The type checker currently does #1, although at some point in the past it
did #2. Moving forward, #1 is a better choice because it prunes the number
of overloads that need to be considered: if the more-specialized overload
succeeds its type-check, the others need not be considered. It’s also
easier to reason about than the literal-scoring approach, because there can
be a direct definition for “more specialized than” that can be reasoned
about.

I think we should dodge the issue by removing the more-specialized version
of `abs(_:)`. Its use of `magnitude` seems unlikely to provide a
significant performance benefit, and the presence of overloading either
forces us to consider both overloads always (which is bad for type checker
performance) or accept the regression that `abs(-8)` is `Double`. Better
to eliminate the overloading and, if needed in the future, find a better
way to introduce the more-specialized implementation without it being a
separate signature.

Fixes rdar://problem/42345366.
2018-12-04 23:10:04 -08:00
Maxim Moiseev
cbf83ac04f [NFC][stdlib] Add FIXME markers to simplify audit 2018-11-14 11:58:42 -08:00
Slava Pestov
f6c2caf64b stdlib: Add @inlinable to @inline(__always) declarations
These should be audited since some might not actually need to be
@inlinable, but for now:

- Anything public and @inline(__always) is now also @inlinable
- Anything @usableFromInline and @inline(__always) is now @inlinable
2018-11-13 15:15:07 -05:00
Richard Wei
57facc8daa Update comments 2018-11-08 10:18:31 -08:00
Richard Wei
44c2f7761a Implement SE-0233 AdditiveArithmetic 2018-11-07 19:43:13 -08:00
Maxim Moiseev
e107fab25b [stdlib] Internalize _Buffer32 and _Buffer72 2018-11-07 11:25:31 -08:00
Maxim Moiseev
d6501e2e90 Remove sil-serialize-all FIXMEs from integers 2018-10-16 13:27:29 -07:00
Stephen Canon
c88d4e407f Remove unsafe arithmetic operations deprecated in 4.2 (#19426) 2018-09-24 11:00:28 -04:00
Maxim Moiseev
224098b12d [stdlib] Audit @_transparent/@inlinable in Integers.swift 2018-09-13 17:20:20 -07:00
Doug Gregor
cffe3869a6 Merge pull request #19034 from DougGregor/protocol-override
Introduce overrides of protocol members and drop them from witness tables
2018-09-05 20:57:21 -07:00
Doug Gregor
0a8058ca6a [Integer protocols] Make BinaryInteger.Words conform to RandomAccessCollection.
Require that BinaryInteger.Words conform to RandomAccessCollection with
an Index type of Int, simplifying clients.

Fixes rdar://problem/36410936.
2018-09-05 16:22:27 -07:00
Doug Gregor
f8e53d9129 [Standard library] Audit protocol member overrides in protocols.
Add the `-warn-implicit-overrides` flag when building the standard library
and overlays, so that each protocol member that overrides a member of an
inherited protocol will produce a warning unless annotated with either
‘override’ or ‘@_nonoverride’.

An annotation of `override` will mean that the overriding requirement will be treated identically to the overridden declaration. If for some reason a concrete type’s conformance to the inheriting protocol provides a different witness for the overriding requirement than the conformance to the inherited protocol’s witness for the overridden requirement, the witness for the inheriting (more-specialized) protocol will be ignored. A protocol requirement marked ‘override’ only makes sense when the declaration is needed to help associated type inference, which is why the ‘override’ annotations correlate so closely with ABI FIXMEs.

An annotation of `@_nonoverride` means that the two protocol requirements will be treated independently, and may be bound to different witnesses. Use `@_nonoverride` when we might need different witnesses, e.g., because the semantics of the potentially-overriding declaration differ from that of the potentially-overridden declaration. `BidirectionalCollection.index(_:offsetBy:)` is the most obvious example, because the `BidirectionalCollection` ’s version of `index(_:offsetBy:)` allows negative indices. `RandomAccessCollection` ’s version is also marked `@_nonoverride` because it is required to be asymptotically faster than the `Collection` or `BidirectionalCollection` versions.
2018-09-05 13:51:26 -07:00
Stephen Canon
190c7d6b9f Default implementation of isMultiple(of:) on BinaryInteger
In order to provide source compatibility with existing user types conforming to BinaryInteger, we want to have a default implementation available. It's somewhat difficult to provide a good default implementation that correctly handles arbitrary non-symmetrical ranges in the face of negative divisors, so fall back on testing divisibility of the magnitudes, which avoids the problem.

On the plus side, this default implementation works fine for types conforming to UnsignedInteger, which lets us move the FixedWidthInteger implementation down to FixedWidthInteger & SignedInteger, and simplify it in the process.
2018-09-04 16:32:14 -04:00
swift-ci
161819b31c Merge pull request #18634 from natecook1000/nc-random-highlevel-notes 2018-09-04 08:14:43 -07:00
Stephen Canon
c1f25e0673 [SE-0225] Implementation of isMultiple for BinaryInteger. (#18689)
* Implement SE-0225 (BinaryInteger.isMultiple(of:))

A default implementation is provided for FixedWidthInteger, with very basic test coverage included.
2018-09-04 10:31:05 -04:00
Nate Cook
908ac9f43b Revise note about future random algorithm changes 2018-09-03 22:16:23 -05:00
Nate Cook
8174511a3f Add notes about future changes to high-level random algorithms 2018-09-03 22:16:23 -05:00
Maxim Moiseev
91143a2630 [NFC][stdlib] Unify usage of argument labels in operators 2018-08-31 13:03:18 -07:00
Maxim Moiseev
dca1ad87af [stdlib] Partially ungyb Integers
The new file straucture is similar to that of floating point types,
where protocols live in one file, and concrete types – in another.
2018-08-29 13:46:10 -07:00