Skip to content

Commit

Permalink
added drop and keep mutating SIMD functions, and...
Browse files Browse the repository at this point in the history
- moved SIMD tests to own file
- optimized `readSIMDBuffer` in `Socket`
  • Loading branch information
RandomHashTags committed Nov 8, 2024
1 parent 38d4252 commit b126248
Show file tree
Hide file tree
Showing 4 changed files with 456 additions and 124 deletions.
13 changes: 5 additions & 8 deletions Sources/Destiny/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ public extension Socket {

/// Reads `scalarCount` characters and loads them into the target SIMD.
@inlinable
func readLineSIMD2<T : SIMD>() throws -> (T, Int) where T.Scalar == UInt8 { // read just the method, path & http version
func readLineSIMD2<T : SIMD>(length: Int) throws -> (T, Int) where T.Scalar == UInt8 { // read just the method, path & http version
var string:T = T()
let read:Int = try withUnsafeMutableBytes(of: &string) { p in
return try readSIMDBuffer(into: p.baseAddress!, length: T.scalarCount)
return try readSIMDBuffer(into: p.baseAddress!, length: length)
}
return (string, read)
}
Expand All @@ -76,7 +76,7 @@ public extension Socket {
var head_count:Int = 0
var token:DestinyRoutePathType = .init()
while true {
let (line, read):(SIMD64<UInt8>, Int) = try readLineSIMD2()
let (line, read):(SIMD64<UInt8>, Int) = try readLineSIMD2(length: 64)
if head_count == 0 {
token = line.lowHalf
}
Expand Down Expand Up @@ -160,15 +160,12 @@ public extension Socket {
/// Reads multiple bytes and writes them into a buffer
@inlinable
func readSIMDBuffer(into baseAddress: UnsafeMutableRawPointer, length: Int) throws -> Int {
var bytes_read:Int = 0
if Task.isCancelled { return 0 }
let to_read:Int = min(Self.bufferLength, length - bytes_read)
let read_bytes:Int = recv(fileDescriptor, baseAddress + bytes_read, to_read, 0)
let read_bytes:Int = recv(fileDescriptor, baseAddress, length, 0)
if read_bytes < 0 { // error
throw SocketError.readBufferFailed()
}
bytes_read += read_bytes
return bytes_read
return read_bytes
}
}

Expand Down
292 changes: 291 additions & 1 deletion Sources/DestinyUtilities/StackStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -589,4 +589,294 @@ public extension SIMD64 where Scalar : BinaryInteger {
public extension SIMD2 where Scalar : BinaryInteger {
@inlinable func hasSuffix(_ simd: SIMD2<Scalar>) -> Bool {
}
}*/
}*/

// MARK: drop O(1)
// implementation should never change
public extension SIMD2 where Scalar : BinaryInteger {
/// Sets the trailing scalars to zero.
///
/// - Parameters:
/// - length: The number of trailing scalars to set to zero.
/// - Complexity: O(1)
@inlinable
mutating func drop(_ length: Int) {
switch length {
case _ where length <= 0:
break
case 1:
self[1] = 0
break
default:
self = .init()
break
}
}
}
public extension SIMD4 where Scalar : BinaryInteger {
/// Sets the trailing scalars to zero.
///
/// - Parameters:
/// - length: The number of trailing scalars to set to zero.
/// - Complexity: O(1)
@inlinable
mutating func drop(_ length: Int) {
switch length {
case _ where length <= 0:
break
case 1:
highHalf[1] = 0
break
case 2:
highHalf = .init()
break
case 3:
highHalf = .init()
lowHalf[1] = 0
break
default:
self = .init()
break
}
}
}
public extension SIMD8 where Scalar : BinaryInteger {
/// Sets the trailing scalars to zero.
///
/// - Parameters:
/// - length: The number of trailing scalars to set to zero.
/// - Complexity: O(1)
@inlinable
mutating func drop(_ length: Int) {
switch length {
case _ where length <= 0:
break
case 1...4:
highHalf.drop(length)
break
case 5...7:
highHalf = .init()
lowHalf.drop(length - 4)
break
default:
self = .init()
break
}
}
}
public extension SIMD16 where Scalar : BinaryInteger {
/// Sets the trailing scalars to zero.
///
/// - Parameters:
/// - length: The number of trailing scalars to set to zero.
/// - Complexity: O(1)
mutating func drop(_ length: Int) {
switch length {
case _ where length <= 0:
break
case 1...8:
highHalf.drop(length)
break
case 9...15:
highHalf = .init()
lowHalf.drop(length - 8)
break
default:
self = .init()
break
}
}
}
public extension SIMD32 where Scalar : BinaryInteger {
/// Sets the trailing scalars to zero.
///
/// - Parameters:
/// - length: The number of trailing scalars to set to zero.
/// - Complexity: O(1)
mutating func drop(_ length: Int) {
switch length {
case _ where length <= 0:
break
case 1...16:
highHalf.drop(length)
break
case 17...31:
highHalf = .init()
lowHalf.drop(length - 16)
break
default:
self = .init()
break
}
}
}
public extension SIMD64 where Scalar : BinaryInteger {
/// Sets the trailing scalars to zero.
///
/// - Parameters:
/// - length: The number of trailing scalars to set to zero.
/// - Complexity: O(1)
mutating func drop(_ length: Int) {
switch length {
case _ where length <= 0:
break
case 1...32:
highHalf.drop(length)
break
case 33...63:
highHalf = .init()
lowHalf.drop(length - 32)
break
default:
self = .init()
break
}
}
}

// MARK: keep O(1)
// implementation should never change
public extension SIMD2 where Scalar : BinaryInteger {
/// Keeps the leading scalar values and sets everything else to zero.
///
/// - Complexity: O(1)
/// - Parameters:
/// - length: The number of leading scalars to keep.
@inlinable
mutating func keep(_ length: Int) {
switch length {
case _ where length <= 0:
self = .init()
break
case 1:
y = 0
break
default:
break
}
}
}
public extension SIMD4 where Scalar : BinaryInteger {
/// Keeps the leading scalar values and sets everything else to zero.
///
/// - Complexity: O(1)
/// - Parameters:
/// - length: The number of leading scalars to keep.
@inlinable
mutating func keep(_ length: Int) {
switch length {
case _ where length <= 0:
self = .init()
break
case 1:
y = 0
z = 0
w = 0
break
case 2:
highHalf = .init()
break
case 3:
w = 0
break
default:
break
}
}
}
public extension SIMD8 where Scalar : BinaryInteger {
/// Keeps the leading scalar values and sets everything else to zero.
///
/// - Complexity: O(1)
/// - Parameters:
/// - length: The number of leading scalars to keep.
@inlinable
mutating func keep(_ length: Int) {
switch length {
case _ where length <= 0:
self = .init()
break
case 1...4:
lowHalf.keep(length)
highHalf = .init()
break
case 5...7:
highHalf.keep(length - 4)
break
default:
break
}
}
}
public extension SIMD16 where Scalar : BinaryInteger {
/// Keeps the leading scalar values and sets everything else to zero.
///
/// - Complexity: O(1)
/// - Parameters:
/// - length: The number of leading scalars to keep.
@inlinable
mutating func keep(_ length: Int) {
switch length {
case _ where length <= 0:
self = .init()
break
case 1...8:
lowHalf.keep(length)
highHalf = .init()
break
case 9...15:
highHalf.keep(length - 8)
break
default:
break
}
}
}
public extension SIMD32 where Scalar : BinaryInteger {
/// Keeps the leading scalar values and sets everything else to zero.
///
/// - Complexity: O(1)
/// - Parameters:
/// - length: The number of leading scalars to keep.
@inlinable
mutating func keep(_ length: Int) {
switch length {
case _ where length <= 0:
self = .init()
break
case 1...16:
lowHalf.keep(length)
highHalf = .init()
break
case 17...31:
highHalf.keep(length - 16)
break
default:
break
}
}
}
public extension SIMD64 where Scalar : BinaryInteger {
/// Keeps the leading scalar values and sets everything else to zero.
///
/// - Complexity: O(1)
/// - Parameters:
/// - length: The number of leading scalars to keep.
@inlinable
mutating func keep(_ length: Int) {
switch length {
case _ where length <= 0:
self = .init()
break
case 1...32:
lowHalf.keep(length)
highHalf = .init()
break
case 33...63:
highHalf.keep(length - 32)
break
default:
break
}
}
}
Loading

0 comments on commit b126248

Please sign in to comment.