-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[string] Fix string implementation for big endian platforms #21077
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
milseman
merged 2 commits into
swiftlang:master
from
linux-on-ibm-z:s390x-smallstring-v2
Feb 5, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,19 @@ | |
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// The code units in _SmallString are always stored in memory in the same order | ||
// that they would be stored in an array. This means that on big-endian | ||
// platforms the order of the bytes in storage is reversed compared to | ||
// _StringObject whereas on little-endian platforms the order is the same. | ||
// | ||
// Memory layout: | ||
// | ||
// |0 1 2 3 4 5 6 7 8 9 A B C D E F| ← hexadecimal offset in bytes | ||
// | _storage.0 | _storage.1 | ← raw bits | ||
// | code units | | ← encoded layout | ||
// ↑ ↑ | ||
// first (leftmost) code unit discriminator (incl. count) | ||
// | ||
@_fixed_layout @usableFromInline | ||
internal struct _SmallString { | ||
@usableFromInline | ||
|
@@ -50,16 +63,18 @@ internal struct _SmallString { | |
@inlinable @inline(__always) | ||
internal init(_ object: _StringObject) { | ||
_internalInvariant(object.isSmall) | ||
self.init(raw: object.rawBits) | ||
// On big-endian platforms the byte order is the reverse of _StringObject. | ||
let leading = object.rawBits.0.littleEndian | ||
let trailing = object.rawBits.1.littleEndian | ||
self.init(raw: (leading, trailing)) | ||
} | ||
|
||
@inlinable @inline(__always) | ||
internal init() { | ||
self.init(raw: _StringObject(empty:()).rawBits) | ||
self.init(_StringObject(empty:())) | ||
} | ||
} | ||
|
||
// TODO | ||
extension _SmallString { | ||
@inlinable | ||
internal static var capacity: Int { | ||
|
@@ -72,9 +87,12 @@ extension _SmallString { | |
} | ||
} | ||
|
||
// Get an integer equivalent to the _StringObject.discriminatedObjectRawBits | ||
// computed property. | ||
@inlinable @inline(__always) | ||
internal var rawDiscriminatedObject: UInt64 { | ||
return _storage.1 | ||
// Reverse the bytes on big-endian systems. | ||
return _storage.1.littleEndian | ||
} | ||
|
||
@inlinable | ||
|
@@ -107,7 +125,7 @@ extension _SmallString { | |
// usage: it always clears the discriminator and count (in case it's full) | ||
@inlinable @inline(__always) | ||
internal var zeroTerminatedRawCodeUnits: RawBitPattern { | ||
let smallStringCodeUnitMask: UInt64 = 0x00FF_FFFF_FFFF_FFFF | ||
let smallStringCodeUnitMask = ~UInt64(0xFF).bigEndian // zero last byte | ||
return (self._storage.0, self._storage.1 & smallStringCodeUnitMask) | ||
} | ||
|
||
|
@@ -231,11 +249,12 @@ extension _SmallString { | |
_internalInvariant(count <= _SmallString.capacity) | ||
|
||
let isASCII = (leading | trailing) & 0x8080_8080_8080_8080 == 0 | ||
let countAndDiscriminator = UInt64(truncatingIfNeeded: count) &<< 56 | ||
| _StringObject.Nibbles.small(isASCII: isASCII) | ||
_internalInvariant(trailing & countAndDiscriminator == 0) | ||
let discriminator = _StringObject.Nibbles | ||
.small(withCount: count, isASCII: isASCII) | ||
.littleEndian // reversed byte order on big-endian platforms | ||
_internalInvariant(trailing & discriminator == 0) | ||
|
||
self.init(raw: (leading, trailing | countAndDiscriminator)) | ||
self.init(raw: (leading, trailing | discriminator)) | ||
_internalInvariant(self.count == count) | ||
} | ||
|
||
|
@@ -295,23 +314,31 @@ extension _SmallString { | |
#endif | ||
|
||
extension UInt64 { | ||
// Fetches the `i`th byte, from least-significant to most-significant | ||
// | ||
// TODO: endianess awareness day | ||
// Fetches the `i`th byte in memory order. On little-endian systems the byte | ||
// at i=0 is the least significant byte (LSB) while on big-endian systems the | ||
// byte at i=7 is the LSB. | ||
@inlinable @inline(__always) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does "left to right" mean, memory-order/host-endianess? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clarified the comment (memory-order). |
||
internal func _uncheckedGetByte(at i: Int) -> UInt8 { | ||
_internalInvariant(i >= 0 && i < MemoryLayout<UInt64>.stride) | ||
#if _endian(big) | ||
let shift = (7 - UInt64(truncatingIfNeeded: i)) &* 8 | ||
#else | ||
let shift = UInt64(truncatingIfNeeded: i) &* 8 | ||
#endif | ||
return UInt8(truncatingIfNeeded: (self &>> shift)) | ||
} | ||
|
||
// Sets the `i`th byte, from least-significant to most-significant | ||
// | ||
// TODO: endianess awareness day | ||
// Sets the `i`th byte in memory order. On little-endian systems the byte | ||
// at i=0 is the least significant byte (LSB) while on big-endian systems the | ||
// byte at i=7 is the LSB. | ||
@inlinable @inline(__always) | ||
internal mutating func _uncheckedSetByte(at i: Int, to value: UInt8) { | ||
_internalInvariant(i >= 0 && i < MemoryLayout<UInt64>.stride) | ||
#if _endian(big) | ||
let shift = (7 - UInt64(truncatingIfNeeded: i)) &* 8 | ||
#else | ||
let shift = UInt64(truncatingIfNeeded: i) &* 8 | ||
#endif | ||
let valueMask: UInt64 = 0xFF &<< shift | ||
self = (self & ~valueMask) | (UInt64(truncatingIfNeeded: value) &<< shift) | ||
} | ||
|
@@ -331,5 +358,6 @@ internal func _bytesToUInt64( | |
r = r | (UInt64(input[idx]) &<< shift) | ||
shift = shift &+ 8 | ||
} | ||
return r | ||
// Convert from little-endian to host byte order. | ||
return r.littleEndian | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is worth a comment stating it's back to
_StringObject
's notion of a raw discriminated object. I can add that in a followup PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a brief comment. Happy to drop this additional commit if you'd rather do a comment follow up yourself.