Skip to content

Commit

Permalink
Add vsub
Browse files Browse the repository at this point in the history
  • Loading branch information
jiegec committed Dec 12, 2023
1 parent 88b42e5 commit 8a226b6
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 7 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ Vector Store with Register Offset

Vector Set Equal/Less than or Equal/Less Than

### vsub.b/h/w/d

Vector Subtract

### vsadd.b/h/w/d

Vector Saturated Add
Expand Down Expand Up @@ -124,8 +120,6 @@ Vector Multiplication High

### vfrstp.b/h

### vsub.q

### vsignconv.b/h/w/d

### vfsub.s/d
Expand Down
8 changes: 8 additions & 0 deletions code/gen_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@
for width in ["b", "h", "w", "d"]:
w = widths[width]
m = members[width]
for name, op in [("add", "+"), ("sub", "-")]:
with open(f"v{name}_{width}.h", "w") as f:
print(f"for (int i = 0;i < {128 // w};i++) {{", file=f)
print(
f" dst.{m}[i] = a.{m}[i] {op} b.{m}[i];",
file=f,
)
print(f"}}", file=f)
with open(f"vbitclr_{width}.h", "w") as f:
print(f"for (int i = 0;i < {128 // w};i++) {{", file=f)
print(
Expand Down
2 changes: 2 additions & 0 deletions code/gen_tb.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

tb = {
# widths, args, extra args for imm
"vadd": (widths_signed, "v128 a, v128 b"),
"vavg": (widths_all, "v128 a, v128 b"),
"vavgr": (widths_all, "v128 a, v128 b"),
"vaddwev": (widths_vaddw, "v128 a, v128 b"),
Expand Down Expand Up @@ -66,6 +67,7 @@
"vmini": (widths_all, "v128 a, int imm", [0, 3, 15]),
"vmulwev": (widths_vaddw, "v128 a, v128 b"),
"vmulwod": (widths_vaddw, "v128 a, v128 b"),
"vsub": (widths_signed, "v128 a, v128 b"),
"vsubwev": (widths_vsubw, "v128 a, v128 b"),
"vsubwod": (widths_vsubw, "v128 a, v128 b"),
}
Expand Down
2 changes: 1 addition & 1 deletion code/vadd_b.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
for (int i = 0; i < 16; i++) {
dst.byte[i] = a.byte[i] + b.byte[i];
}
}
9 changes: 9 additions & 0 deletions code/vsub_b.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "common.h"

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

void test() { FUZZ2(vsub_b); }
3 changes: 3 additions & 0 deletions code/vsub_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] = a.byte[i] - b.byte[i];
}
9 changes: 9 additions & 0 deletions code/vsub_d.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "common.h"

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

void test() { FUZZ2(vsub_d); }
3 changes: 3 additions & 0 deletions code/vsub_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] = a.dword[i] - b.dword[i];
}
9 changes: 9 additions & 0 deletions code/vsub_h.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "common.h"

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

void test() { FUZZ2(vsub_h); }
3 changes: 3 additions & 0 deletions code/vsub_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] = a.half[i] - b.half[i];
}
9 changes: 9 additions & 0 deletions code/vsub_q.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "common.h"

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

void test() { FUZZ2(vsub_q); }
1 change: 1 addition & 0 deletions code/vsub_q.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dst.qword[0] = a.qword[0] - b.qword[0];
9 changes: 9 additions & 0 deletions code/vsub_w.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "common.h"

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

void test() { FUZZ2(vsub_w); }
3 changes: 3 additions & 0 deletions code/vsub_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] = a.word[i] - b.word[i];
}
7 changes: 7 additions & 0 deletions docs/lsx/integer_computation.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@
{{ vmulwev('q', 'du') }}
{{ vmulwev('q', 'du', 'd') }}

{{ vsub('b') }}
{{ vsub('h') }}
{{ vsub('w') }}
{{ vsub('d') }}
{{ vsub('q') }}


{{ vsubwev('h', 'b') }}
{{ vsubwev('h', 'bu') }}
{{ vsubwev('w', 'h') }}
Expand Down
8 changes: 8 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,11 @@ def vldrepl(name):
desc=f"Read {width}-bit data from memory address `addr + (offset << {shift})`, replicate the data to all vector lanes and save into `dst`.",
)

@env.macro
def vsub(name):
width = widths[name]
return instruction(
intrinsic=f"__m128i __lsx_vsub_{name} (__m128i a, __m128i b)",
instr=f"vsub.{name} vr, vr, vr",
desc=f"Subtract {width}-bit elements in `a` and `b`, save the result in `dst`.",
)

0 comments on commit 8a226b6

Please sign in to comment.