Commit Graph

515 Commits

Author SHA1 Message Date
Ben Rimmington
b65d0c1d11 Consolidate _stdlib_random functions (#2)
* Use the `__has_include` and `GRND_RANDOM` macros

* Use `getentropy` instead of `getrandom`

* Use `std::min` from the <algorithm> header

* Move `#if` out of the `_stdlib_random` function

* Use `getrandom` with "/dev/urandom" fallback

* Use `#pragma comment` to import "Bcrypt.lib"

* <https://docs.microsoft.com/en-us/cpp/preprocessor/comment-c-cpp>
* <https://clang.llvm.org/docs/UsersManual.html#microsoft-extensions>

* Use "/dev/urandom" instead of `SecRandomCopyBytes`

* Use `swift::StaticMutex` for shared "/dev/urandom"

* Add `getrandom_available`; use `O_CLOEXEC` flag

Add platform impl docs

Update copyrights

Fix docs

Add _stdlib_random test

Update _stdlib_random test

Add missing &

Notice about _stdlib_random

Fix docs

Guard on upperBound = 0

Test full range of 8 bit integers

Remove some gyb

Clean up integerRangeTest

Remove FixedWidthInteger constraint

Use arc4random universally

Fix randomElement

Constrain shuffle to RandomAccessCollection

warning instead of error

Move Apple's implementation

Fix failing test on 32 bit systems
2018-05-09 00:21:44 -05:00
Ben Rimmington
a5df0ef83d Add _stdlib_random for more platforms (#1)
* Remove refs to Countable ranges

* Add `_stdlib_random` for more platforms

* Use `getrandom` (if available) for Android, Cygwin

* Reorder the `_stdlib_random` functions

* Also include <features.h> on Linux

* Add `#error TODO` in `_stdlib_random` for Windows

* Colon after Fatal Error

Performance improvement for Random

gybify ranges

Fix typo in 'basic random numbers'
Add _stdlib_random as a testable method

Switch to generic constraints

Hopefully link against bcrypt

Fix some implementation details

1. Uniform distribution is now uniform
2. Apply Jens' method for uniform floats

Fix a lineable attribute
2018-05-04 21:03:38 -05:00
Azoy
d23d219e95 Add shims for stdlib random
Initial random api

Use C syscall for I/O

1. Fixed an issue where integers would would result in an infinite loop if they were unsigned, or signed integers always returning negative numbers.
2. Fixed an issue with Bool initialization

Add shuffle functions

Add documentation to Random API

Fix a few typos within the documentation

Fixes more typos

Also states that the range for floating points is from 0 to 1 inclusive

Update API to reflect mailing list discussions

Remove unnecessary import

Make sure not to return upperBound on Range

Use SecRandomCopyBytes on older macOS

Update API to match mailing list discussion, add tests

Added pick(_:) to collection
Added random(in:using:) to Randomizable
Added tests

Fix typo in Randomizable documentation

Rename pick to sampling

Move sampling below random

Update docs

Use new Libc naming

Fix Random.swift with new Libc naming

Remove sampling

gybify signed integer creation

Make FloatingPoint.random exclusive

Refactor {Closed}Range.random

Fix FloatingPoint initialization

Precondition getting a random number from range

Fix some doc typos

Make .random a function

Update API to reflect discussion

Make .random a function
Remove .random() in favor of .random(in:) for all numeric types

Fix compile errors

Clean up _stdlib_random

Cleanup around API

Remove `.random()` requirement from `Collection`

Use generators

Optimize shuffle()

Thread safety for /dev/urandom

Remove {Closed}Range<BinaryFloatingPoint>.random()

Add Collection random requirement

Refactor _stdlib_random

Remove whitespace changes

Clean linux shim

Add shuffle and more tests

Provide finishing tests and suggestions

Remove refs to Countable ranges

Revert to checking if T is > UInt64
2018-05-04 21:02:57 -05:00
Saleem Abdulrasool
ad26c68d53 Merge pull request #15942 from compnerd/windbg
stdlib: add additional compiler rt routines
2018-04-18 08:27:49 -07:00
Saleem Abdulrasool
d55d348016 stdlib: add additional compiler rt routines
The quad precision support in visual studio does not support
the operations which clang will sometimes lower.  These currently
generate code using GNU extensions.  Add additional stubs for math
routines on Windows.  This allows the standard library to build in
debug mode.
2018-04-15 17:01:30 -07:00
Saleem Abdulrasool
302ce638eb stubs: silence unused function warning
When building on Linux, the definition of `swift_snprintf_l` would cause
an unused function warning.  Expand the scope of the preprocessor guard
to encompass the function for the single use.  This avoids the unused
function warning.
2018-04-13 18:07:07 -07:00
Saleem Abdulrasool
a34ac086d4 stubs: clean up android support routines
Some of the previously used stubs are no longer needed in newer releases
of the Android API.  Android L and Android O provide the functions in
their associated versions of bionic.  This is needed to build against a
newer version of the SDK.
2018-04-13 18:06:50 -07:00
Saleem Abdulrasool
dfa8a3113f Merge pull request #15912 from compnerd/compiler-rt
stubs: split up compiler-rt routines
2018-04-13 15:15:39 -07:00
Saleem Abdulrasool
9f045add2c stdlib: silence truncation warning on Windows
The returned type `std::streamoff` on Windows x64 is a `long long`
rather than `int`.  This results in a 64-to-32 bit shortening of the
value.  Use the appropriate type to avoid the truncation.

`strlen` returns a unsigned value, but `std::streamoff` is an signed
value.  Explicitly cast the value to avoid the warning about the
implicit signed conversion.
2018-04-13 10:28:17 -07:00
Saleem Abdulrasool
0a1352efff stubs: split up compiler-rt routines
Move the duplicated compiler-rt support routines into its own source
file.  This will need to be expanded for Windows.  As on Linux, there
are certain builtin routines which are not available from the standard
runtime and need to be augmented for now.
2018-04-13 09:44:42 -07:00
Saleem Abdulrasool
b432249a17 stubs: silence pedantic warning (NFC)
C requires that there is at least one declaration in a translation unit.
Because this file is ObjC and not ObjC++, this restriction applies.  Add
a declaration to silence the warning in the case that ObjC interop is
disabled.
2018-04-06 09:29:05 -07:00
Michael Gottesman
9accf4a82f Merge pull request #15686 from gottesmm/pr-2aa38499b4210d8c66240446893c4f340fc62351
[+0-all-args] Remove autoreleasepool on non-x86_64 platforms from Str…
2018-04-02 13:29:57 -07:00
Michael Gottesman
bc48a66c77 [+0-all-args] Remove autoreleasepool on non-x86_64 platforms from String stubs.
This is only necessary on x86-64. On arm/arm64 we are already ok since the
assembly marker that we use there does not suffer from this issue.

For x86-64 we need to solve the problem upstream by not tailing calling
objc_retainAutoreleasedReturnValue.

rdar://38675842
2018-04-02 10:50:08 -07:00
tbkka
97a934c412 SR-106: New floating-point description implementation (#15474)
* SR-106: New floating-point `description` implementation

This replaces the current implementation of `description` and
`debugDescription` for the standard floating-point types with a new
formatting routine based on a variation of Florian Loitsch' Grisu2
algorithm with changes suggested by Andrysco, Jhala, and Lerner's 2016
paper describing Errol3.

Unlike the earlier code based on `sprintf` with a fixed number of
digits, this version always chooses the optimal number of digits.  As
such, we can now use the exact same output for both `description` and
`debugDescription` (except of course that `debugDescription` provides
full detail for NaNs).

The implementation has been extensively commented; people familiar with
Grisu-style algorithms should find the code easy to understand.

This implementation is:

* Fast.  It uses only fixed-width integer arithmetic and has constant
  memory and time requirements.

* Simple. It is only a little more complex than Loitsch' original
  implementation of Grisu2.  The digit decomposition logic for double is
  less than 300 lines of standard C (half of which is common arithmetic
  support routines).

* Always Accurate. Converting the decimal form back to binary (using an
  accurate algorithm such as Clinger's) will always yield exactly the
  original binary value.  For the IEEE 754 formats, the round-trip will
  produce exactly the same bit pattern in memory.  This is an essential
  requirement for JSON serialization, debugging, and logging.

* Always Short.  This always selects an accurate result with the minimum
  number of decimal digits.  (So that `1.0 / 10.0` will always print
  `0.1`.)

* Always Close.  Among all accurate, short results, this always chooses
  the result that is closest to the exact floating-point value. (In case
  of an exact tie, it rounds the last digit even.)

This resolves SR-106 and related issues that have complained
about the floating-point `description` properties being inexact.

* Remove duplicate infinity handling

* Use defined(__SIZEOF_INT128__) to detect uint128_t support

* Separate `extracting` the integer part from `clearing` the integer part

The previous code was unnecessarily obfuscated by the attempt to combine
these two operations.

* Use `UINT32_MAX` to mask off 32 bits of a larger integer

* Correct the expected NaN results for 32-bit i386

* Make the C++ exceptions here consistent

Adding a C source file somehow exposed an issue in an unrelated C++ file.
Thanks to Joe Groff for the fix.

* Rename SwiftDtoa to ".cpp"

Having a C file in stdlib/public/runtime causes strange
build failures on Linux in unrelated C++ files.

As a workaround, rename SwiftDtoa.c to .cpp to see
if that avoids the problems.

* Revert "Make the C++ exceptions here consistent"

This reverts commit 6cd5c20566.
2018-04-01 16:52:48 -07:00
Tony Allevato
fb9f7ecca1 Merge branch 'master' into unicode-properties 2018-03-31 09:54:18 -07:00
swift-ci
19d3095cc0 Merge pull request #15564 from lanza/split-archs 2018-03-30 14:09:37 -07:00
Nathan Lanza
e06c52584f Change various uses of SWIFT_SDK_${SDK}_PATH to its architecture specific vairant
Cross-compilation for multiple architectures & sdks require various
variables to be split to specify the arch/adk variant being focused on.
This change modifies various uses of the `SWIFT_SDK_${SDK}_PATH` to
`SWIFT_SDK_${SDK}_ARCH_${ARCH}`
2018-03-30 17:50:59 +00:00
Saleem Abdulrasool
15a25a2126 Merge pull request #15568 from lanza/normalizeunicodeto59
change icu version check in UnicodeNormalization to 59
2018-03-29 12:01:10 -04:00
Tony Allevato
354f2ad66e [stdlib] Add "numeric{Type,Value}" to Unicode.Scalar.Properties 2018-03-27 21:07:27 -07:00
Tony Allevato
e7fa49984a [stdlib] Add "{lower,title,upper}caseMapping" to Unicode.Scalar.Properties 2018-03-26 06:38:55 -07:00
Tony Allevato
9858d4edfb [stdlib] Add "name", "nameAlias" to Unicode.Scalar.Properties 2018-03-26 06:38:55 -07:00
Tony Allevato
af798fa972 [stdlib] Add "generalCategory" to Unicode.Scalar.Properties 2018-03-26 06:38:55 -07:00
Tony Allevato
d6ee54f4b5 [stdlib] Add "age" to Unicode.Scalar.Properties 2018-03-26 06:38:54 -07:00
Michael Gottesman
e567bc9028 [+0-all-args] Enable +0 normal arguments.
rdar://34222540
2018-03-19 20:25:31 -07:00
Lance Parker
cbf157f924 [stdlib]Unify String hashing implementation (#14921)
* Add partial range subscripts to _UnmanagedOpaqueString

* Use SipHash13+_NormalizedCodeUnitIterator for String hashes on all platforms

* Remove unecessary collation algorithm shims

* Pass the buffer to the SipHasher for ASCII

* Hash the ascii parts of UTF16 strings the same way we hash pure ascii strings

* De-dupe some code that can be shared between _UnmanagedOpaqueString and _UnmanagedString<UInt16>

* ASCII strings now hash consistently for in hashASCII() and hashUTF16()

* Fix zalgo comparison regression

* Use hasher

* Fix crash when appending to an empty _FixedArray

* Compact ASCII characters into a single UInt64 for hashing

* String: Switch to _hash(into:)-based hashing

This should speed up String hashing quite a bit, as doing it through hashValue involves two rounds of SipHash nested in each other.

* Remove obsolete workaround for ARC traffic

* Ditch _FixedArray<UInt8> in favor of _UIntBuffer<UInt64, UInt8>

* Bad rebase remnants

* Fix failing benchmarks

* michael's feedback

* clarify the comment about nul-terminated string hashes
2018-03-17 22:13:37 -07:00
Stephen Canon
ddf70fa199 Cleanup tgmath wrappers. (#15304)
* Cleanup tgmath wrappers.

- Remove special-case gyb logic for lgamma on Darwin; the symbols we need are always present, even if not visible in the headers, so we only need a prototype.
- Add some deprecations for symbols that have direct stdlib analogues.
- Make some operations generic on [Binary]FloatingPoint, where they can map to the protocols instead of calling libm.
- Mark ldexp(Float/Double) renamed to scalbn; for binary formats these are identical functions, and we don't really want to use these names for hypothetical future Decimal support, as they're not Swifty.
2018-03-17 10:32:13 -04:00
Karoy Lorentey
2cf0e4daa1 [runtime] Collapse hashing parameters into a single struct
Having a single initializer function lets us not set a randomized seed in deterministic mode, slightly simplifying the stdlib.

Set related stdlib properties to be always inlined.
2018-03-14 17:44:13 +00:00
Michael Ilseman
b572bfe2f4 [stubs] Add CFString*Bytes stubs, drop some dead ones.
NFC, just stubs and plumbing.
2018-03-13 15:32:20 -07:00
Karoy Lorentey
b0108336f7 Merge pull request #15207 from lorentey/seedless-hashes
[runtime] Add better control over the random hashing seed
2018-03-13 19:46:51 +00:00
Karoy Lorentey
879e4f9ce5 [runtime][stdlib] Better support for random hash seeds
- Hash seed randomization can now be disabled by defining the SWIFT_DETERMINISTIC_HASHING environment value with a value of "1".
- The random hash seed is now generated using arc4random, where available. On platforms where it isn't, don't construct std::random_device twice.
- _Hasher._secretKey is renamed _Hashing._seed, with no setter.
- _Hasher._isDeterministic is a new property exposing whether we're running with non-random hashes. (Set/Dictionary will need this information to decide if they're allowed to use per-instance seeding.)
2018-03-13 16:15:09 +00:00
Ben Cohen
f9b3e14137 [stdlib] Use Swift-native Character iteration for hasPrefix/Suffix (#14390)
* Use Swift-native Character iteration for hasPrefix/Suffix

* Remove old tests for removed C shims
2018-03-12 17:41:55 -07:00
Arnold Schwaighofer
e106161d8f Speculative fix for linux bot linker failure 2018-03-09 17:55:39 -08:00
Arnold Schwaighofer
b9602754cd Untangle the dependency on swiftStdlibStubs from swiftRuntime that I introduced
rdar://35525730
2018-03-07 11:17:19 -08:00
Nathan Lanza
d00223e687 change icu version check in UnicodeNormalization to 59 2018-02-26 14:38:16 -08:00
Joe Groff
be19535802 Merge pull request #14831 from jckarter/protected-runtime-visibility
Visibility fixes for ELF.
2018-02-26 13:26:14 -08:00
swift-ci
ac54feca49 Merge pull request #14734 from compnerd/icu-signature 2018-02-26 12:36:01 -08:00
Joe Groff
8656807b90 Visibility fixes for ELF.
- `_swiftEmpty{Array,Dictionary,Set}Storage` should be marked with `SWIFT_RUNTIME_STDLIB_INTERFACE` so that they can be linked from the standard library implementation.
- Runtime export symbols ought to have protected visibility.
2018-02-26 10:39:10 -08:00
Saleem Abdulrasool
73c04d1dd9 stubs: match ICU signature for unorm2_normalize (NFC)
Adjust the signature to match the ICU declaration for
`unorm2_normalize`.  This was adjusted to allow building against ICU
59.1.  The shim type definition for the UChar ensures that the signature
is correct on all the targets.  NFC.
2018-02-25 22:48:27 -08:00
Chris Lattner
ba7ed9afee Mark the runtime's reimplementation of
llvm::hashing::detail::fixed_seed_override as weak in case it is
staticly linked into an app that includes a real definition from
LLVM.
2018-02-22 14:57:09 -08:00
Lance Parker
0661de22a2 [stdlib]Un-revert string comparison (#14694)
Restore (un-revert) sting comparison, with fixes

More exhaustive testing of opaque strings, which consistently reproduces prior sporadic failure. Shims fixups. Some test tweaking.
2018-02-18 10:50:33 -08:00
Saleem Abdulrasool
cd58f5c1c9 Shims: adjust the ICU interface
The default ICU build will change the underlying type of the UChar type,
with C++ using the builtin `char16_t` rather than `unsigned short`.
This adjusts the interface to account for that.  I've verified across
Apple's implementation that they always use the `unsigned short` as the
type for `UChar`.  Since we cannot guarantee that the ICU interfaces are
built the same way on all targets, especially when using the underlying
system's ICU.

Adjust the stubs implementation declaration to match the ICU header's
declaration.
2018-02-15 21:11:06 -08:00
Lance Parker
abe6a6d177 Revert string comparison (#14657) 2018-02-15 14:37:43 -08:00
Lance Parker
db2de58dd6 Remove unecessary unicode collation algorithm shims 2018-02-14 15:44:11 -08:00
Lance Parker
32d21c489f Add unicode normalization shims 2018-02-14 15:44:11 -08:00
Thomas Roughton
7422973c70 [stdlib] Fix bug in Windows FloatingPoint parsing
On `istringstream`, `tellg()` returns -1 if the stream is at the end of the file. This indicates success in this circumstance, so we should update `pos` to reflect that the whole string has been read.

NFC on platforms other than Windows, Cygwin, and Haiku.
2018-02-13 18:50:15 +13:00
Michael Gottesman
ce0c40e40b [+0-all] Update more runtime files for +0 normal arguments.
This is NFC when --enable-guaranteed-normal-arguments isn't passed into
build-script.

rdar://34222540
2018-01-29 12:13:45 -08:00
Michael Gottesman
18bcc1e143 [+0-args] Change SWIFT_CC(Swift) functions to use SWIFT_NS_RELEASES_ARGUMENT instead of NS_RELEASES_ARGUMENT.
rdar://34222540
2018-01-23 16:01:27 -08:00
Thomas Roughton
b4391f35de Fix comment on #endif for defined(_Win32) 2017-12-04 11:39:22 +13:00
troughton
a1518031ec Fix Windows errno() and _stdlib_open 2017-12-04 11:12:11 +13:00
Greg Parker
da14cd79a6 [runtime] Clean up symbol exports in libc functions. (#13202) 2017-12-01 17:49:11 -08:00