Skip to content

Commit

Permalink
Addd vaddwod
Browse files Browse the repository at this point in the history
  • Loading branch information
jiegec committed Dec 12, 2023
1 parent c4db29b commit 4e4404f
Show file tree
Hide file tree
Showing 96 changed files with 396 additions and 162 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,14 @@ Vector Subtract

Vector Subtract with Width Extension

### vaddwod.h.b/w.h/d.w/q.d
### vsubwod.h.b/w.h/d.w/q.d

### vsubwev.h.bu/w.hu/d.wu/q.du

Vector Subtract with Width Extension Unsigned

### vaddwod.h.bu/w.hu/d.wu/q.du
### vsubwod.h.bu/w.hu/d.wu/q.du

### vaddwod.h.bu.b/w.hu.h/q.wu.w/q.du.d

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

Vector Saturated Add
Expand Down
78 changes: 68 additions & 10 deletions code/gen_impl.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
widths_all = ["b", "bu", "h", "hu", "w", "wu", "d", "du"]
import os

widths = {
"b": 8,
Expand All @@ -10,6 +10,18 @@
"d": 64,
"du": 64,
"q": 128,
"qu": 128,
}

double_widths = {
"b": "h",
"bu": "h",
"h": "w",
"hu": "w",
"w": "d",
"wu": "d",
"d": "q",
"du": "q",
}

members = {
Expand All @@ -21,20 +33,66 @@
"wu": "word",
"d": "dword",
"du": "dword",
"q": "qword",
"qu": "qword",
}

for width in widths_all:
for width in ["b", "bu", "h", "hu", "w", "wu", "d", "du"]:
w = widths[width]
m = members[width]
if "u" in width:
sign = "u"
else:
sign = "s"
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)

for width in ["b", "bu", "h", "hu", "w", "wu", "d", "du"]:
double_width = double_widths[width]
w = widths[width]
m = members[width]
double_w = widths[double_width]
double_m = members[double_width]
if "u" in width:
sign = "u"
else:
sign = "s"
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)
for signed2 in [False, True]:
if "u" not in width and not signed2:
continue
if "u" in width and signed2:
suffix = f"_{width[:-1]}"
else:
suffix = ""
if signed2:
sign2 = "s"
else:
sign2 = "u"
with open(f"vaddwev_{double_width}_{width}{suffix}.h", "w") as f:
print(f"for (int i = 0;i < {128 // double_w};i++) {{", file=f)
print(
f" dst.{double_m}[i] = ({sign}{double_w})({sign}{w})a.{m}[2 * i] + ({sign2}{double_w})({sign2}{w})b.{m}[2 * i];",
file=f,
)
print(f"}}", file=f)
with open(f"vaddwod_{double_width}_{width}{suffix}.h", "w") as f:
print(f"for (int i = 0;i < {128 // double_w};i++) {{", file=f)
print(
f" dst.{double_m}[i] = ({sign}{double_w})({sign}{w})a.{m}[2 * i + 1] + ({sign2}{double_w})({sign2}{w})b.{m}[2 * i + 1];",
file=f,
)
print(f"}}", file=f)

os.system("clang-format -i *.cpp *.h")
48 changes: 34 additions & 14 deletions code/gen_tb.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
import os

widths_all = ["b", "bu", "h", "hu", "w", "wu", "d", "du"]
widths_vaddw = [
"h_b",
"h_bu",
"h_bu_b",
"w_h",
"w_hu",
"w_hu_h",
"d_w",
"d_wu",
"d_wu_w",
"q_d",
"q_du",
"q_du_d",
]

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

for name in tb:
Expand All @@ -14,20 +32,22 @@
inst_name = name + "_" + width

fuzz_args = 0
for arg in args.split(', '):
if 'v128' in arg:
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(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("", 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)
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)

os.system("clang-format -i *.cpp *.h")
3 changes: 2 additions & 1 deletion code/vabsd_b.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
for (int i = 0; i < 16; i++) {
dst.byte[i] = ((s8)a.byte[i] > (s8)b.byte[i]) ? (a.byte[i] - b.byte[i]) : (b.byte[i] - a.byte[i]);
dst.byte[i] = ((s8)a.byte[i] > (s8)b.byte[i]) ? (a.byte[i] - b.byte[i])
: (b.byte[i] - a.byte[i]);
}
3 changes: 2 additions & 1 deletion code/vabsd_bu.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
for (int i = 0; i < 16; i++) {
dst.byte[i] = ((u8)a.byte[i] > (u8)b.byte[i]) ? (a.byte[i] - b.byte[i]) : (b.byte[i] - a.byte[i]);
dst.byte[i] = ((u8)a.byte[i] > (u8)b.byte[i]) ? (a.byte[i] - b.byte[i])
: (b.byte[i] - a.byte[i]);
}
4 changes: 3 additions & 1 deletion code/vabsd_d.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
for (int i = 0; i < 2; i++) {
dst.dword[i] = ((s64)a.dword[i] > (s64)b.dword[i]) ? (a.dword[i] - b.dword[i]) : (b.dword[i] - a.dword[i]);
dst.dword[i] = ((s64)a.dword[i] > (s64)b.dword[i])
? (a.dword[i] - b.dword[i])
: (b.dword[i] - a.dword[i]);
}
4 changes: 3 additions & 1 deletion code/vabsd_du.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
for (int i = 0; i < 2; i++) {
dst.dword[i] = ((u64)a.dword[i] > (u64)b.dword[i]) ? (a.dword[i] - b.dword[i]) : (b.dword[i] - a.dword[i]);
dst.dword[i] = ((u64)a.dword[i] > (u64)b.dword[i])
? (a.dword[i] - b.dword[i])
: (b.dword[i] - a.dword[i]);
}
3 changes: 2 additions & 1 deletion code/vabsd_h.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
for (int i = 0; i < 8; i++) {
dst.half[i] = ((s16)a.half[i] > (s16)b.half[i]) ? (a.half[i] - b.half[i]) : (b.half[i] - a.half[i]);
dst.half[i] = ((s16)a.half[i] > (s16)b.half[i]) ? (a.half[i] - b.half[i])
: (b.half[i] - a.half[i]);
}
3 changes: 2 additions & 1 deletion code/vabsd_hu.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
for (int i = 0; i < 8; i++) {
dst.half[i] = ((u16)a.half[i] > (u16)b.half[i]) ? (a.half[i] - b.half[i]) : (b.half[i] - a.half[i]);
dst.half[i] = ((u16)a.half[i] > (u16)b.half[i]) ? (a.half[i] - b.half[i])
: (b.half[i] - a.half[i]);
}
3 changes: 2 additions & 1 deletion code/vabsd_w.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
for (int i = 0; i < 4; i++) {
dst.word[i] = ((s32)a.word[i] > (s32)b.word[i]) ? (a.word[i] - b.word[i]) : (b.word[i] - a.word[i]);
dst.word[i] = ((s32)a.word[i] > (s32)b.word[i]) ? (a.word[i] - b.word[i])
: (b.word[i] - a.word[i]);
}
3 changes: 2 additions & 1 deletion code/vabsd_wu.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
for (int i = 0; i < 4; i++) {
dst.word[i] = ((u32)a.word[i] > (u32)b.word[i]) ? (a.word[i] - b.word[i]) : (b.word[i] - a.word[i]);
dst.word[i] = ((u32)a.word[i] > (u32)b.word[i]) ? (a.word[i] - b.word[i])
: (b.word[i] - a.word[i]);
}
1 change: 0 additions & 1 deletion code/vadda_d.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
for (int i = 0; i < 2; i++) {
dst.dword[i] = abs((s64)a.dword[i]) + abs((s64)b.dword[i]);
}

1 change: 0 additions & 1 deletion code/vadda_h.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ v128 vadda_h(v128 a, v128 b) {
}

void test() { FUZZ2(vadda_h); }

1 change: 0 additions & 1 deletion code/vadda_h.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
for (int i = 0; i < 8; i++) {
dst.half[i] = abs((s16)a.half[i]) + abs((s16)b.half[i]);
}

1 change: 0 additions & 1 deletion code/vadda_w.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ v128 vadda_w(v128 a, v128 b) {
}

void test() { FUZZ2(vadda_w); }

2 changes: 1 addition & 1 deletion code/vaddwev_d_w.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
for (int i = 0;i < 2;i++) {
for (int i = 0; i < 2; i++) {
dst.dword[i] = (s64)(s32)a.word[2 * i] + (s64)(s32)b.word[2 * i];
}
2 changes: 1 addition & 1 deletion code/vaddwev_d_wu.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
for (int i = 0;i < 2;i++) {
for (int i = 0; i < 2; i++) {
dst.dword[i] = (u64)(u32)a.word[2 * i] + (u64)(u32)b.word[2 * i];
}
2 changes: 1 addition & 1 deletion code/vaddwev_d_wu_w.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
for (int i = 0;i < 2;i++) {
for (int i = 0; i < 2; i++) {
dst.dword[i] = (u64)(u32)a.word[2 * i] + (s64)(s32)b.word[2 * i];
}
4 changes: 2 additions & 2 deletions code/vaddwev_h_b.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
for (int i = 0;i < 8;i++) {
for (int i = 0; i < 8; i++) {
dst.half[i] = (s16)(s8)a.byte[2 * i] + (s16)(s8)b.byte[2 * i];
}
}
4 changes: 2 additions & 2 deletions code/vaddwev_h_bu.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
for (int i = 0;i < 8;i++) {
for (int i = 0; i < 8; i++) {
dst.half[i] = (u16)(u8)a.byte[2 * i] + (u16)(u8)b.byte[2 * i];
}
}
2 changes: 1 addition & 1 deletion code/vaddwev_h_bu_b.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
for (int i = 0;i < 8;i++) {
for (int i = 0; i < 8; i++) {
dst.half[i] = (u16)(u8)a.byte[2 * i] + (s16)(s8)b.byte[2 * i];
}
4 changes: 3 additions & 1 deletion code/vaddwev_q_d.h
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
dst.qword[0] = (s128)(s64)a.dword[0] + (s128)(s64)b.dword[0];
for (int i = 0; i < 1; i++) {
dst.qword[i] = (s128)(s64)a.dword[2 * i] + (s128)(s64)b.dword[2 * i];
}
4 changes: 3 additions & 1 deletion code/vaddwev_q_du.h
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
dst.qword[0] = (u128)(u64)a.dword[0] + (u128)(u64)b.dword[0];
for (int i = 0; i < 1; i++) {
dst.qword[i] = (u128)(u64)a.dword[2 * i] + (u128)(u64)b.dword[2 * i];
}
4 changes: 3 additions & 1 deletion code/vaddwev_q_du_d.h
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
dst.qword[0] = (u128)(u64)a.dword[0] + (s128)(s64)b.dword[0];
for (int i = 0; i < 1; i++) {
dst.qword[i] = (u128)(u64)a.dword[2 * i] + (s128)(s64)b.dword[2 * i];
}
2 changes: 1 addition & 1 deletion code/vaddwev_w_h.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
for (int i = 0;i < 4;i++) {
for (int i = 0; i < 4; i++) {
dst.word[i] = (s32)(s16)a.half[2 * i] + (s32)(s16)b.half[2 * i];
}
2 changes: 1 addition & 1 deletion code/vaddwev_w_hu.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
for (int i = 0;i < 4;i++) {
for (int i = 0; i < 4; i++) {
dst.word[i] = (u32)(u16)a.half[2 * i] + (u32)(u16)b.half[2 * i];
}
2 changes: 1 addition & 1 deletion code/vaddwev_w_hu_h.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
for (int i = 0;i < 4;i++) {
for (int i = 0; i < 4; i++) {
dst.word[i] = (u32)(u16)a.half[2 * i] + (s32)(s16)b.half[2 * i];
}
9 changes: 9 additions & 0 deletions code/vaddwod_d_w.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "common.h"

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

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

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

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

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

void test() { FUZZ2(vaddwod_d_wu_w); }
3 changes: 3 additions & 0 deletions code/vaddwod_d_wu_w.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)(u32)a.word[2 * i + 1] + (s64)(s32)b.word[2 * i + 1];
}
9 changes: 9 additions & 0 deletions code/vaddwod_h_b.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "common.h"

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

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

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

void test() { FUZZ2(vaddwod_h_bu); }
3 changes: 3 additions & 0 deletions code/vaddwod_h_bu.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)(u8)a.byte[2 * i + 1] + (u16)(u8)b.byte[2 * i + 1];
}
Loading

0 comments on commit 4e4404f

Please sign in to comment.