From 903b44695404990c56304c7b0d829514ef9a41c2 Mon Sep 17 00:00:00 2001 From: Ben Cohen Date: Thu, 31 Oct 2024 11:16:32 -0700 Subject: [PATCH] Adopt experimental version of fixed-size Vector type --- Examples/SwiftBreak/Sources/FixedArray.swift | 71 ++++++++++++------- Examples/SwiftBreak/Sources/Game.swift | 7 +- Examples/SwiftBreak/Sources/Sprite+Game.swift | 2 +- Examples/SwiftBreak/Sources/Vector.swift | 10 +-- Examples/swift.mk | 4 ++ 5 files changed, 57 insertions(+), 37 deletions(-) diff --git a/Examples/SwiftBreak/Sources/FixedArray.swift b/Examples/SwiftBreak/Sources/FixedArray.swift index 18dba0d..ab7e982 100644 --- a/Examples/SwiftBreak/Sources/FixedArray.swift +++ b/Examples/SwiftBreak/Sources/FixedArray.swift @@ -1,35 +1,52 @@ -struct FixedArray: ~Copyable { - let count: Int - private let _buffer: UnsafeMutablePointer +import Builtin - init(count: Int, first: consuming Element, rest: (borrowing Element) -> Element) { - precondition(count > 0) - self.count = count - _buffer = .allocate(capacity: count) - for i in 1..: ~Copyable { + private var storage: Builtin.FixedArray +} - deinit { - _buffer.deinitialize(count: count) - _buffer.deallocate() - } +extension Vector where Element: ~Copyable { + init(first: @autoclosure ()->Element, rest valueForIndex: (borrowing Element) -> Element) { + storage = Builtin.emplace { rawPointer in + let first = first() + let base = UnsafeMutablePointer(rawPointer) + for i in 1.. Void) { - for i in 0.. Element { + _read { + assert(i >= 0 && i < Count) + let rawPointer = Builtin.addressOfBorrow(self) + let base = UnsafePointer(rawPointer) + yield ((base + i).pointee) + } + + _modify { + assert(i >= 0 && i < Count) + let rawPointer = Builtin.addressof(&self) + let base = UnsafeMutablePointer(rawPointer) + yield (&(base + i).pointee) + } + } + + func forEach(_ body: (borrowing Element) -> Void) { + for i in 0.. Void) { - var i = 0 - self.forEach { - body(i, $0) - i += 1 + func enumerated(_ body: (Int, borrowing Element) -> Void) { + for i in 0.. + var bricks: Vector<40,Sprite> } struct ActiveGame { var score: Int - var ballVelocity: Vector + var ballVelocity: Vec2 var bricksRemaining: Int } @@ -36,7 +36,6 @@ struct Game: ~Copyable { Sprite.setupWalls() // Start in loading state with 4 x 10 bricks. - let brick = Sprite.brick() let splash = Sprite.splash() splash.addSprite() self.sprites = @@ -46,7 +45,7 @@ struct Game: ~Copyable { ball: .ball(), paddle: .paddle(), gameOver: .gameOver(), - bricks: .init(count: 40, first: brick) { $0.copy() }) + bricks: .init(first: Sprite.brick()) { $0.copy() }) Sprite.drawSprites() self.state = .loading } diff --git a/Examples/SwiftBreak/Sources/Sprite+Game.swift b/Examples/SwiftBreak/Sources/Sprite+Game.swift index ce48e6e..83bffaa 100644 --- a/Examples/SwiftBreak/Sources/Sprite+Game.swift +++ b/Examples/SwiftBreak/Sources/Sprite+Game.swift @@ -83,7 +83,7 @@ extension Sprite { activeGame.bricksRemaining -= 1 } - var normal = Vector(collision.normal) + var normal = Vec2(collision.normal) if otherSprite.tag == .paddle { // compute placement of ball on paddle in domain -1 to 1. diff --git a/Examples/SwiftBreak/Sources/Vector.swift b/Examples/SwiftBreak/Sources/Vector.swift index a6684c8..db065fd 100644 --- a/Examples/SwiftBreak/Sources/Vector.swift +++ b/Examples/SwiftBreak/Sources/Vector.swift @@ -1,8 +1,8 @@ import Playdate -typealias Vector = SIMD2 +typealias Vec2 = SIMD2 -extension Vector { +extension Vec2 { init(_ collisionVector: CollisionVector) { self = .init(Float32(collisionVector.x), Float32(collisionVector.y)) } @@ -12,7 +12,7 @@ extension Vector { } } -extension Vector { +extension Vec2 { func reflected(along normal: Self) -> Self { self - (2 * (self • normal)) * normal } @@ -22,7 +22,7 @@ extension Vector { } } -extension Vector { +extension Vec2 { func rotated(by theta: Float32) -> Self { .init( x: self.x * cosf(theta) + self.y * -sinf(theta), @@ -35,7 +35,7 @@ extension Vector { } infix operator • : MultiplicationPrecedence -extension Vector { +extension Vec2 { static func • (lhs: Self, rhs: Self) -> Float { (lhs.x * rhs.x) + (lhs.y * rhs.y) } diff --git a/Examples/swift.mk b/Examples/swift.mk index d3fc471..9c0c69d 100644 --- a/Examples/swift.mk +++ b/Examples/swift.mk @@ -42,8 +42,12 @@ SWIFT_FLAGS := \ -O \ -wmo -enable-experimental-feature Embedded \ -enable-experimental-feature NoncopyableGenerics \ + -enable-experimental-feature ValueGenerics \ + -enable-experimental-feature BuiltinModule \ -Xfrontend -disable-stack-protector \ -Xfrontend -function-sections \ + -Xfrontend -disable-availability-checking \ + -disable-experimental-parser-round-trip \ -swift-version 6 \ -Xcc -DTARGET_EXTENSION \ -module-cache-path build/module-cache \