-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1bd9198
commit 2aaee8a
Showing
4 changed files
with
91 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
32 changes: 32 additions & 0 deletions
32
open_lessons/asm/simd/vector_addition/vector_addition_arm64.s
This file contains 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 |
---|---|---|
@@ -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
55
open_lessons/asm/simd/vector_addition/vector_addition_test.go
This file contains 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 |
---|---|---|
@@ -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 | ||
} |