Skip to content

Commit

Permalink
Add vbitrev
Browse files Browse the repository at this point in the history
  • Loading branch information
jiegec committed Dec 12, 2023
1 parent 3d4bd38 commit 73780f3
Show file tree
Hide file tree
Showing 21 changed files with 158 additions and 9 deletions.
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,6 @@ Vector Multiplication High

### vssrarn.bu.h/hu.w/wu.d

### vbitset.b/h/w/d

### vbitrev.b/h/w/d

### vpackev.b/h/w/d

### vpackod.b/h/w/d
Expand Down Expand Up @@ -364,10 +360,6 @@ Vector Multiplication High

### vextl.qu.du

### vbitseti.b/h/w/d

### vbitrevi.b/h/w/d

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

### vslli.b/h/w/d
Expand Down
14 changes: 14 additions & 0 deletions code/gen_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,19 @@
file=f,
)
print(f"}}", file=f)
with open(f"vbitrev_{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] ^ ((u{w})1 << (b.{m}[i] % {w}));",
file=f,
)
print(f"}}", file=f)
with open(f"vbitrevi_{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] ^ ((u{w})1 << imm);",
file=f,
)
print(f"}}", file=f)

os.system("clang-format -i *.cpp *.h")
2 changes: 2 additions & 0 deletions code/gen_tb.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"vbitclri": (widths_signed, "v128 a, int imm", [0, 3, 7]),
"vbitset": (widths_signed, "v128 a, v128 b"),
"vbitseti": (widths_signed, "v128 a, int imm", [0, 3, 7]),
"vbitrev": (widths_signed, "v128 a, v128 b"),
"vbitrevi": (widths_signed, "v128 a, int imm", [0, 3, 7]),
}

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

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

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

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

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

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

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

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

void test() { FUZZ2(vbitrev_w); }
3 changes: 3 additions & 0 deletions code/vbitrev_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] ^ ((u32)1 << (b.word[i] % 32));
}
13 changes: 13 additions & 0 deletions code/vbitrevi_b.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "common.h"

v128 vbitrevi_b(v128 a, int imm) {
v128 dst;
#include "vbitrevi_b.h"
return dst;
}

void test() {
FUZZ1(vbitrevi_b, 0);
FUZZ1(vbitrevi_b, 3);
FUZZ1(vbitrevi_b, 7);
}
3 changes: 3 additions & 0 deletions code/vbitrevi_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] ^ ((u8)1 << imm);
}
13 changes: 13 additions & 0 deletions code/vbitrevi_d.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "common.h"

v128 vbitrevi_d(v128 a, int imm) {
v128 dst;
#include "vbitrevi_d.h"
return dst;
}

void test() {
FUZZ1(vbitrevi_d, 0);
FUZZ1(vbitrevi_d, 3);
FUZZ1(vbitrevi_d, 7);
}
3 changes: 3 additions & 0 deletions code/vbitrevi_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] ^ ((u64)1 << imm);
}
13 changes: 13 additions & 0 deletions code/vbitrevi_h.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "common.h"

v128 vbitrevi_h(v128 a, int imm) {
v128 dst;
#include "vbitrevi_h.h"
return dst;
}

void test() {
FUZZ1(vbitrevi_h, 0);
FUZZ1(vbitrevi_h, 3);
FUZZ1(vbitrevi_h, 7);
}
3 changes: 3 additions & 0 deletions code/vbitrevi_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] ^ ((u16)1 << imm);
}
13 changes: 13 additions & 0 deletions code/vbitrevi_w.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "common.h"

v128 vbitrevi_w(v128 a, int imm) {
v128 dst;
#include "vbitrevi_w.h"
return dst;
}

void test() {
FUZZ1(vbitrevi_w, 0);
FUZZ1(vbitrevi_w, 3);
FUZZ1(vbitrevi_w, 7);
}
3 changes: 3 additions & 0 deletions code/vbitrevi_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] ^ ((u32)1 << imm);
}
12 changes: 11 additions & 1 deletion docs/lsx_bitops/vbitwise.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,14 @@ Compute bitwise selection: for each bit position, if the bit in `c` equals to on
{{ vbitseti('b') }}
{{ vbitseti('h') }}
{{ vbitseti('w') }}
{{ vbitseti('d') }}
{{ vbitseti('d') }}

{{ vbitrev('b') }}
{{ vbitrev('h') }}
{{ vbitrev('w') }}
{{ vbitrev('d') }}

{{ vbitrevi('b') }}
{{ vbitrevi('h') }}
{{ vbitrevi('w') }}
{{ vbitrevi('d') }}
19 changes: 19 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,25 @@ def vbitseti(name):
desc=f"Set the bit specified by `imm` from {width}-bit elements in `a`, save the result in `dst`.",
)

@env.macro
def vbitrev(name):
width = widths[name]
return instruction(
intrinsic=f"__m128i __lsx_vbitrev_{name} (__m128i a, __m128i b)",
instr=f"vbitrev.{name} vr, vr, vr",
desc=f"Toggle the bit specified by elements in `b` from {width}-bit elements in `a`, save the result in `dst`.",
)

@env.macro
def vbitrevi(name):
width = widths[name]
imm_upper = width - 1
return instruction(
intrinsic=f"__m128i __lsx_vbitrevi_{name} (__m128i a, imm0_{imm_upper} imm)",
instr=f"vbitrevi.{name} vr, vr, imm",
desc=f"Toggle the bit specified by `imm` from {width}-bit elements in `a`, save the result in `dst`.",
)

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

0 comments on commit 73780f3

Please sign in to comment.