Commit Graph

68 Commits

Author SHA1 Message Date
Doug Gregor
ad626f9c0e [Stdlib performance] Make integer conversion operations transparent
The integer conversion operations were inlinable, but aren't getting
inlined in debug builds, which results in unreasonably poor
performance. Mark them as transparent so we don't end up with
unspecialized generic code in the hot path.

Fixes https://github.com/swiftlang/swift/issues/78501
2025-03-12 07:40:49 -07:00
Doug Gregor
22eecacc35 Adopt unsafe annotations throughout the standard library 2025-02-26 14:28:01 -08:00
Ben Rimmington
3c855da45d [stdlib] Fix coding style of AdditiveArithmetic (#75461)
Use explicit access modifiers instead of `public extension` inheritance.
2024-07-25 11:30:14 -04:00
Ben Rimmington
fc8cc33777 [stdlib] Update documentation of integer types (#74973)
* [stdlib] Remove docs from default implementations

Customized docs were not removed:

  public static var isSigned: Bool
  public static var max: Self
  public static var min: Self
  public var description: String
  public var magnitude: Self
  public init<T: BinaryInteger>(_ source: T)
  public func distance(to other: Self) -> Int
  public func advanced(by n: Int) -> Self

* [stdlib] Remove unused docs for "unsafe" methods

* [stdlib] Remove inherited "ReportingOverflow" docs

* [stdlib] Remove inherited docs for operators

Customized docs were not removed.
(`+`, `-`, `*`, `+=`, `-=`, `*=`)

Non-inherited docs were not removed.
(`&+`, `&-`, `&+=`, `&-=`, `&*=`)

* [stdlib] Reattach doc comments to their APIs

Use `rstrip()` to remove the trailing newline.
<https://docs.python.org/3/library/stdtypes.html#str.rstrip>

* [stdlib] Remove inherited docs from integer types

* [stdlib] Remove FIXME(ABI) comments

* [stdlib] Remove unused docs for operators

* [stdlib] Update example code for `-=` and `*=`

* [stdlib] Update internal gyb comments

* [stdlib] Move docs to BinaryInteger overrides

* [stdlib] Remove unused gyb code

* [stdlib] Fix `&-` and `&*` examples
2024-07-24 17:31:15 -05:00
Alexander Cyon
c21b1e68fd [stdlib] Fix typos 2024-07-06 13:09:57 +02:00
Kuba Mracek
7ae20b7039 [embedded] Port Swift.String to embedded Swift 2024-05-08 11:11:37 -07:00
Stephen Canon
a381589524 SE-0425 implementation (#72139) 2024-04-02 16:24:41 -04:00
Nate Chandler
73231b185b [BitwiseCopyable] Don't apply to FixedWidthInteger.
Per LWG feedback.

This reverts commit c8119e3c01.
2024-03-06 08:12:54 -08:00
Oscar Byström Ericsson
d7b3d3e319 Minor advanced(by:) enhancement. 2024-02-03 15:18:02 +01:00
Oscar Byström Ericsson
17187e38f5 This patch fixes two unnecessary traps in BinaryInteger's advanced(to:) method.
- `Int8.min.advanced(by: 128)`, etc.
- `UInt.max.advanced(by: Int.min)`, etc.
2024-02-03 13:56:33 +01:00
Nate Chandler
c8119e3c01 [stdlib] FixedWidthInteger is BitwiseCopyable. 2024-01-30 14:38:07 -08:00
Kuba Mracek
bd5913a176 [embedded] Make RNG APIs available on embedded Swift 2024-01-17 14:53:24 -08:00
Stephen Canon
7a0c4b5e4a Add default implementations for FixedWidthInteger.dividingFullWidth (#70823)
These are provided for FixedWidthInteger & UnsignedInteger (the base implementation, following Knuth's Algorithm D) and SignedInteger (converting to magnitudes and calling the former). Previously no default implementations were available, requiring every type to implement these operations.

These defaults will not be optimal for large fixed-width integers, so types vending Int512 or similar integers should still provide their own implementations, but they are unconditionally available as a fallback, which simplifies the process of writing such types, and work well enough as a fallback for modest fixed-width integer types like Int64 or 32b or smaller platforms or Int128 on 64b platforms.

Additionally rework the concrete implementations to guarantee that we always trap when the quotient is not representable, and to improve performance for 64b integers on arm64_32, and added some new test coverage for these operations.
2024-01-12 15:25:01 -05:00
Philippe Hausler
7a2c21d312 [Embedded] enable random APIs if a RandomNumberGenerator is provided, but leave system random number generators unavailable (#69645) 2023-11-03 21:12:22 -07:00
Kuba Mracek
3646c32b45 [embedded] Move _CustomStringConvertibleOrNone/_LosslessStringConvertibleOrNone to avoid detaching doccomments from their declarations 2023-10-13 15:31:30 -07:00
Kuba Mracek
169ed58bee [embedded] Revert change on FixedWidthInteger protocol requirements, use an empty protocol instead 2023-09-17 17:23:08 -07:00
Kuba Mracek
92b075b518 [embedded] Drop several #ifs and fatalError()s by adding an unavailable ExpressibleByStringInterpolation String conformance 2023-09-16 13:40:25 -07:00
Kuba Mracek
cba7cdfad6 [embedded] Drop an unnecessary change in Integers.swift 2023-09-16 13:25:21 -07:00
Kuba Mracek
ae2e903574 [embedded] Build an initial embedded Swift standard library
This isn't a "complete" port of the standard library for embedded Swift, but
something that should serve as a starting point for further iterations on the
stdlib.

- General CMake logic for building a library as ".swiftmodule only" (ONLY_SWIFTMODULE).
- CMake logic in stdlib/public/core/CMakeLists.txt to start building the embedded stdlib for a handful of hardcoded target triples.
- Lots of annotations throughout the standard library to make types, functions, protocols unavailable in embedded Swift (@_unavailableInEmbedded).
- Mainly this is about stdlib functionality that relies on existentials, type erasure, metatypes, reflection, string interpolations.
- We rely on function body removal of unavailable functions to eliminate the actual problematic SIL code (existentials).
- Many .swift files are not included in the compilation of embedded stdlib at all, to simplify the scope of the annotations.
- EmbeddedStubs.swift is used to stub out (as unavailable and fatalError'd) the missing functionality.
2023-09-16 12:38:46 -07:00
Xiaodi Wu
df6a56ecbc [stdlib] Make a stylistic change per reviewer feedback. 2023-01-17 19:39:59 -05:00
Xiaodi Wu
f671e50265 [stdlib] Further optimize heterogeneous binary integer comparison 2023-01-15 13:32:24 -05:00
Xiaodi Wu
d6ac4e3476 [stdlib] Improve performance of generic binary integer == and < 2023-01-15 13:32:24 -05:00
Alex Martini
950b7160c2 Correct the expected output in an example. 2022-05-12 10:35:55 -07:00
Josh Soref
a0d2cabda6 Spelling stdlib/public/core (#42441)
* spelling: available

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: components

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: conjunction

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: conversion

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: enforce

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: guarantee

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: interchangeable

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: satisfied

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: superfluous

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

Co-authored-by: Josh Soref <jsoref@users.noreply.github.com>
2022-04-19 14:02:24 -07:00
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