Skip to content

Commit

Permalink
feat: add vector addition
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorWalther committed Jul 31, 2024
1 parent 1bd9198 commit 2aaee8a
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 4 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/hw1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# TODO
#jobs:
# test:
# vector_addition:
# runs-on: ${{ matrix.os }}
# strategy:
# fail-fast: false
Expand All @@ -21,6 +21,6 @@
# - name: dependencies
# working-directory: ./homeworks/hw1
# run: go mod tidy
# - name: test
# - name: vector_addition
# working-directory: ./homeworks/hw1
# run: go test -v ./...
# run: go vector_addition -v ./...
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func main() {
}

if testEnv != "TEST_ENV_DATA" {
panic("test inh mismatch")
panic("vector_addition inh mismatch")
}

reader := bufio.NewReader(os.Stdin)
Expand Down
32 changes: 32 additions & 0 deletions open_lessons/asm/simd/vector_addition/vector_addition_arm64.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "textflag.h"

TEXT ·vectorAddition(SB), NOSPLIT, $0
LDP first_base+0(FP), (R0, R1)
LDP second_base+24(FP), (R2, R3)
LDP dst_base+48(FP), (R4, R5)

MOVD $0, R7
MOVD R4, R11
MOVD R0, R12
MOVD R2, R13

loop:
CMP R5, R7
BGE done

VLD1 (R12), [V1.S4]
VLD1 (R13), [V2.S4]
VADD V1.S4, V2.S4, V3.S4

VST1 [V3.S4], (R11)

ADD $4, R7

ADD $16, R11
ADD $16, R12
ADD $16, R13

B loop

done:
RET
55 changes: 55 additions & 0 deletions open_lessons/asm/simd/vector_addition/vector_addition_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"math/rand/v2"
"testing"
)

func vectorAddition(first, second, dst []uint32)

func vectorAdditionV0(first, second, dst []uint32) {
for i := 0; i < len(first); i++ {
dst[i] = first[i] + second[i]
}
}

func BenchmarkAdd(b *testing.B) {
b.Run("SIMD vector addition", func(b *testing.B) {
b.StopTimer()
f, s, dst := getData()
b.StartTimer()

for i := 0; i < b.N; i++ {
vectorAddition(f, s, dst)
}
})

b.Run("simple vector addition", func(b *testing.B) {
b.StopTimer()
f, s, dst := getData()
b.StartTimer()

for i := 0; i < b.N; i++ {
vectorAdditionV0(f, s, dst)
}
})
}

// assume alignment
func getData() ([]uint32, []uint32, []uint32) {
first := make([]uint32, 1_000_000)

for i := 0; i < len(first); i++ {
first[i] = rand.N[uint32](5)
}

second := make([]uint32, 1_000_000)

for i := 0; i < len(second); i++ {
second[i] = rand.N[uint32](5)
}

dst := make([]uint32, 1_000_000)

return first, second, dst
}

0 comments on commit 2aaee8a

Please sign in to comment.