Skip to content

Commit

Permalink
Add vavg
Browse files Browse the repository at this point in the history
  • Loading branch information
jiegec committed Dec 12, 2023
1 parent b576aeb commit 0661534
Show file tree
Hide file tree
Showing 21 changed files with 166 additions and 10 deletions.
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,6 @@ Vector Half Add Unsigned

Vector Half Sub Unsigned

### vavg.b/h/w/d

Vector Average

### vavg.bu/hu/wu/du

Vector Average Unsigned

### vavgr.b/h/w/d
### vavgr.bu/hu/wu/du

Expand Down
1 change: 1 addition & 0 deletions code/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
!*.h
!*.cpp
!*.sh
!*.py
32 changes: 32 additions & 0 deletions code/gen_tb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
widths_all = ["b", "bu", "h", "hu", "w", "wu", "d", "du"]

tb = {
"vavg": (widths_all, "v128 a, v128 b")
}

for name in tb:
t = tb[name]
widths = t[0]
args = t[1]

for width in widths:
inst_name = name + "_" + width

fuzz_args = 0
for arg in args.split(', '):
if 'v128' in arg:
fuzz_args += 1

print(f'Saving {inst_name}.cpp')
with open(f'{inst_name}.cpp', 'w') as f:
print('#include "common.h"', file=f)
print('', file=f)
print(f'v128 {inst_name}({args}) {{', file=f)
print(' v128 dst;', file=f)
print(f'#include "{inst_name}.h"', file=f)
print(' return dst;', file=f)
print('}', file=f)
print('', file=f)
print('void test() {', file=f)
print(f' FUZZ{fuzz_args}({inst_name});', file=f)
print('}', file=f)
11 changes: 11 additions & 0 deletions code/vavg_b.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "common.h"

v128 vavg_b(v128 a, v128 b) {
v128 dst;
#include "vavg_b.h"
return dst;
}

void test() {
FUZZ2(vavg_b);
}
3 changes: 3 additions & 0 deletions code/vavg_b.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
for (int i = 0; i < 16; i++) {
dst.byte[i] = ((s8)a.byte[i] >> 1) + ((s8)b.byte[i] >> 1) + (a.byte[i] & b.byte[i] & 1);
}
11 changes: 11 additions & 0 deletions code/vavg_bu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "common.h"

v128 vavg_bu(v128 a, v128 b) {
v128 dst;
#include "vavg_bu.h"
return dst;
}

void test() {
FUZZ2(vavg_bu);
}
3 changes: 3 additions & 0 deletions code/vavg_bu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
for (int i = 0; i < 16; i++) {
dst.byte[i] = ((u8)a.byte[i] >> 1) + ((u8)b.byte[i] >> 1) + (a.byte[i] & b.byte[i] & 1);
}
11 changes: 11 additions & 0 deletions code/vavg_d.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "common.h"

v128 vavg_d(v128 a, v128 b) {
v128 dst;
#include "vavg_d.h"
return dst;
}

void test() {
FUZZ2(vavg_d);
}
3 changes: 3 additions & 0 deletions code/vavg_d.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
for (int i = 0; i < 2; i++) {
dst.dword[i] = ((s64)a.dword[i] >> 1) + ((s64)b.dword[i] >> 1) + (a.dword[i] & b.dword[i] & 1);
}
11 changes: 11 additions & 0 deletions code/vavg_du.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "common.h"

v128 vavg_du(v128 a, v128 b) {
v128 dst;
#include "vavg_du.h"
return dst;
}

void test() {
FUZZ2(vavg_du);
}
3 changes: 3 additions & 0 deletions code/vavg_du.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
for (int i = 0; i < 2; i++) {
dst.dword[i] = ((u64)a.dword[i] >> 1) + ((u64)b.dword[i] >> 1) + (a.dword[i] & b.dword[i] & 1);
}
11 changes: 11 additions & 0 deletions code/vavg_h.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "common.h"

v128 vavg_h(v128 a, v128 b) {
v128 dst;
#include "vavg_h.h"
return dst;
}

void test() {
FUZZ2(vavg_h);
}
3 changes: 3 additions & 0 deletions code/vavg_h.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
for (int i = 0; i < 8; i++) {
dst.half[i] = ((s16)a.half[i] >> 1) + ((s16)b.half[i] >> 1) + (a.half[i] & b.half[i] & 1);
}
11 changes: 11 additions & 0 deletions code/vavg_hu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "common.h"

v128 vavg_hu(v128 a, v128 b) {
v128 dst;
#include "vavg_hu.h"
return dst;
}

void test() {
FUZZ2(vavg_hu);
}
3 changes: 3 additions & 0 deletions code/vavg_hu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
for (int i = 0; i < 8; i++) {
dst.half[i] = ((u16)a.half[i] >> 1) + ((u16)b.half[i] >> 1) + (a.half[i] & b.half[i] & 1);
}
11 changes: 11 additions & 0 deletions code/vavg_w.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "common.h"

v128 vavg_w(v128 a, v128 b) {
v128 dst;
#include "vavg_w.h"
return dst;
}

void test() {
FUZZ2(vavg_w);
}
3 changes: 3 additions & 0 deletions code/vavg_w.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
for (int i = 0; i < 4; i++) {
dst.word[i] = ((s32)a.word[i] >> 1) + ((s32)b.word[i] >> 1) + (a.word[i] & b.word[i] & 1);
}
11 changes: 11 additions & 0 deletions code/vavg_wu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "common.h"

v128 vavg_wu(v128 a, v128 b) {
v128 dst;
#include "vavg_wu.h"
return dst;
}

void test() {
FUZZ2(vavg_wu);
}
3 changes: 3 additions & 0 deletions code/vavg_wu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
for (int i = 0; i < 4; i++) {
dst.word[i] = ((u32)a.word[i] >> 1) + ((u32)b.word[i] >> 1) + (a.word[i] & b.word[i] & 1);
}
13 changes: 11 additions & 2 deletions docs/lsx_integer/vaddsub.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Add/Subtract/Absolute
# Add, Subtract, Absolute and Average

{{ vadd('b') }}
{{ vadd('h') }}
Expand Down Expand Up @@ -36,4 +36,13 @@
{{ vaddwev('d', 'wu', 'w') }}
{{ vaddwev('q', 'd') }}
{{ vaddwev('q', 'du') }}
{{ vaddwev('q', 'du', 'd') }}
{{ vaddwev('q', 'du', 'd') }}

{{ vavg('b') }}
{{ vavg('bu') }}
{{ vavg('h') }}
{{ vavg('hu') }}
{{ vavg('w') }}
{{ vavg('wu') }}
{{ vavg('d') }}
{{ vavg('du') }}
10 changes: 10 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ def vaddwev(wide, narrow, narrow2=None):
desc=f"Add even-positioned {signedness} {narrow_width}-bit elements in `a` and {signedness2} elements in `b`, save the {wide_width}-bit result in `dst`.",
)

@env.macro
def vavg(name):
width = widths[name]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m128i __lsx_vavg_{name} (__m128i a, __m128i b)",
instr=f"vavg.{name} vr, vr, vr",
desc=f"Compute the average of {signedness} {width}-bit elements in `a` and `b`, save the result in `dst`.",
)

@env.macro
def vshuf_hwd(name):
width = widths[name]
Expand Down

0 comments on commit 0661534

Please sign in to comment.