Skip to content

Commit

Permalink
Add tests (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenghaoz committed Jul 15, 2023
1 parent ed569e7 commit 77e86ab
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: test

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
example:
name: example
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v4
- name: Install clang
uses: ConorMacBride/install-package@v1
with:
apt: clang libc6-dev-i386
- name: Install GOAT
run: go install .
- name: Run GOAT
run: goat src/avx_mul_to.c -O3 -mavx -mfma
working-directory: example
- name: Run tests
run: go test -v ./...
working-directory: example
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ It help to utilize optimization from C compiler in Go projects. For example, gen

## Install

1. Install prerequisites

```bash
sudo apt update
sudo apt install clang libc6-dev-i386
```

2. Install GoAT

```bash
go install github.com/gorse-io/goat@latest
```

## Usage

Expand Down
24 changes: 24 additions & 0 deletions example/avx_mul_to_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package example

import (
"reflect"
"testing"
"unsafe"
)

func AVXMulTo(a, b, c []float32) {
if len(a) != len(b) || len(a) != len(c) {
panic("floats: slice lengths do not match")
}
avx_mul_to(unsafe.Pointer(&a[0]), unsafe.Pointer(&b[0]), unsafe.Pointer(&c[0]), unsafe.Pointer(uintptr(len(a))))
}

func TestAVXMulTo(t *testing.T) {
a := []float32{1, 2, 3, 4}
b := []float32{5, 6, 7, 8}
c := make([]float32, len(a))
AVXMulTo(a, b, c)
if !reflect.DeepEqual(c, []float32{5, 12, 21, 32}) {
t.Errorf("AVXMulTo(%v, %v, %v) = %v, want %v", a, b, c, c, []float32{5, 12, 21, 32})
}
}
22 changes: 22 additions & 0 deletions example/src/avx_mul_to.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <immintrin.h>
#include <stdint.h>

void avx_mul_to(float *a, float *b, float *c, int64_t n)
{
int epoch = n / 8;
int remain = n % 8;
for (int i = 0; i < epoch; i++)
{
__m256 v1 = _mm256_loadu_ps(a);
__m256 v2 = _mm256_loadu_ps(b);
__m256 v = _mm256_mul_ps(v1, v2);
_mm256_storeu_ps(c, v);
a += 8;
b += 8;
c += 8;
}
for (int i = 0; i < remain; i++)
{
c[i] = a[i] * b[i];
}
}

0 comments on commit 77e86ab

Please sign in to comment.