Skip to content

Commit

Permalink
Merge pull request #51 from li3zhen1/simd-fast-math
Browse files Browse the repository at this point in the history
[SIMD] Use any(mask) and simd_fast_length
  • Loading branch information
li3zhen1 authored Mar 20, 2024
2 parents 165626b + d0bc34d commit ac3570e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
24 changes: 14 additions & 10 deletions Sources/ForceSimulation/KDTree/KDBox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import simd
/// A box in N-dimensional space.
///
/// - Note: `p0` is the minimum point of the box, `p1` is the maximum point of the box.
public struct KDBox<V>: Equatable where V: SIMD, V.Scalar: FloatingPoint & ExpressibleByFloatLiteral {
public struct KDBox<V>: Equatable
where V: SIMD, V.Scalar: FloatingPoint & ExpressibleByFloatLiteral {
/// the minimum anchor of the box
public var p0: V

Expand Down Expand Up @@ -92,6 +93,7 @@ public struct KDBox<V>: Equatable where V: SIMD, V.Scalar: FloatingPoint & Expre

extension KDBox {
@inlinable
@inline(__always)
var diagnalVector: V {
return p1 - p0
}
Expand All @@ -101,16 +103,20 @@ extension KDBox {
return Self(uncheckedP0: .zero, uncheckedP1: .zero)
}

@inlinable var center: V { (p1 + p0) / 2.0 }
@inlinable
@inline(__always)
var center: V { (p1 + p0) / 2.0 }

/// Test if the box contains a point.
/// - Parameter point: N dimensional point
/// - Returns: `true` if the box contains the point, `false` otherwise.
/// The boundary test is similar to ..< operator.
@inlinable func contains(_ point: V) -> Bool {
let mask = (p0 .> point) .| (point .>= p1)

return mask == .init(repeating: false)
@inlinable
@inline(__always)
func contains(_ point: V) -> Bool {
// let mask =
return !any((p0 .> point) .| (point .>= p1))
//mask == .init(repeating: false)

// equivalent to:
// for i in point.indices {
Expand Down Expand Up @@ -189,7 +195,6 @@ extension KDBox {

@inlinable public static func cover(of points: UnsafeArray<V>) -> Self {


var _p0 = points[0]
var _p1 = points[0]

Expand All @@ -208,16 +213,15 @@ extension KDBox {
// }
// }
}


#if DEBUG
let testBox = Self(_p0, _p1+1)
let testBox = Self(_p0, _p1 + 1)
for i in points.range {
assert(testBox.contains(points[i]))
}
#endif

return Self(_p0, _p1+1)
return Self(_p0, _p1 + 1)
}

}
4 changes: 2 additions & 2 deletions Sources/ForceSimulation/Utils/SimulatableVector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ extension SIMD2: L2NormCalculatable where Scalar == Double {

@inlinable
public func length() -> Scalar {
return simd_length(self)
return simd_fast_length(self)
}
}

Expand All @@ -129,7 +129,7 @@ extension SIMD3: L2NormCalculatable where Scalar == Float {

@inlinable
public func length() -> Scalar {
return simd_length(self)
return simd_fast_length(self)
}
}

Expand Down

0 comments on commit ac3570e

Please sign in to comment.