Skip to content

Commit

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

Vector Half Sub Unsigned

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

### vmax.b/h/w/d

Vector Maximum
Expand Down
4 changes: 4 additions & 0 deletions code/gen_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@
with open(f"vavg_{width}.h", 'w') as f:
print(f'for (int i = 0;i < {128 // w};i++) {{', file=f)
print(f' dst.{m}[i] = (({sign}{w})a.{m}[i] >> 1) + (({sign}{w})b.{m}[i] >> 1) + (a.{m}[i] & b.{m}[i] & 1);', file=f)
print(f'}}', file=f)
with open(f"vavgr_{width}.h", 'w') as f:
print(f'for (int i = 0;i < {128 // w};i++) {{', file=f)
print(f' dst.{m}[i] = (({sign}{w})a.{m}[i] >> 1) + (({sign}{w})b.{m}[i] >> 1) + ((a.{m}[i] | b.{m}[i]) & 1);', file=f)
print(f'}}', file=f)
3 changes: 2 additions & 1 deletion code/gen_tb.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
widths_all = ["b", "bu", "h", "hu", "w", "wu", "d", "du"]

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

for name in tb:
Expand Down
11 changes: 11 additions & 0 deletions code/vavgr_b.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "common.h"

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

void test() {
FUZZ2(vavgr_b);
}
3 changes: 3 additions & 0 deletions code/vavgr_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/vavgr_bu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "common.h"

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

void test() {
FUZZ2(vavgr_bu);
}
3 changes: 3 additions & 0 deletions code/vavgr_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/vavgr_d.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "common.h"

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

void test() {
FUZZ2(vavgr_d);
}
3 changes: 3 additions & 0 deletions code/vavgr_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/vavgr_du.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "common.h"

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

void test() {
FUZZ2(vavgr_du);
}
3 changes: 3 additions & 0 deletions code/vavgr_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/vavgr_h.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "common.h"

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

void test() {
FUZZ2(vavgr_h);
}
3 changes: 3 additions & 0 deletions code/vavgr_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/vavgr_hu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "common.h"

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

void test() {
FUZZ2(vavgr_hu);
}
3 changes: 3 additions & 0 deletions code/vavgr_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/vavgr_w.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "common.h"

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

void test() {
FUZZ2(vavgr_w);
}
3 changes: 3 additions & 0 deletions code/vavgr_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/vavgr_wu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "common.h"

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

void test() {
FUZZ2(vavgr_wu);
}
3 changes: 3 additions & 0 deletions code/vavgr_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);
}
9 changes: 9 additions & 0 deletions docs/lsx_integer/vaddsub.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,12 @@
{{ vavg('wu') }}
{{ vavg('d') }}
{{ vavg('du') }}

{{ vavgr('b') }}
{{ vavgr('bu') }}
{{ vavgr('h') }}
{{ vavgr('hu') }}
{{ vavgr('w') }}
{{ vavgr('wu') }}
{{ vavgr('d') }}
{{ vavgr('du') }}
12 changes: 11 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,17 @@ def vavg(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`.",
desc=f"Compute the average (rounded towards negative infinity) of {signedness} {width}-bit elements in `a` and `b`, save the result in `dst`.",
)

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

@env.macro
Expand Down

0 comments on commit c4db29b

Please sign in to comment.