Skip to content

Commit

Permalink
Reduce duplication by using macros
Browse files Browse the repository at this point in the history
  • Loading branch information
jiegec committed Dec 12, 2023
1 parent c32d315 commit 257039c
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 272 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
286 changes: 14 additions & 272 deletions docs/lsx_integer/vaddsub.md
Original file line number Diff line number Diff line change
@@ -1,274 +1,16 @@
# Add/Subtract/Absolute Difference

## __m128i __lsx_vadd_b (__m128i a, __m128i b)

### Synopsis

```c++
__m128i __lsx_vadd_b (__m128i a, __m128i b)
#include <lsxintrin.h>
Instruction: vadd.b vr, vr, vr
CPU Flags: LSX
```
### Description
Add 8-bit elements in `a` and `b`, save the result in `dst`.
### Operation
```c++
{% include 'vadd_b.h' %}
```

## __m128i __lsx_vadd_h (__m128i a, __m128i b)

### Synopsis

```c++
__m128i __lsx_vadd_h (__m128i a, __m128i b)
#include <lsxintrin.h>
Instruction: vadd.h vr, vr, vr
CPU Flags: LSX
```
### Description
Add 16-bit elements in `a` and `b`, save the result in `dst`.
### Operation
```c++
{% include 'vadd_h.h' %}
```

## __m128i __lsx_vadd_w (__m128i a, __m128i b)

### Synopsis

```c++
__m128i __lsx_vadd_w (__m128i a, __m128i b)
#include <lsxintrin.h>
Instruction: vadd.w vr, vr, vr
CPU Flags: LSX
```
### Description
Add 32-bit elements in `a` and `b`, save the result in `dst`.
### Operation
```c++
{% include 'vadd_w.h' %}
```

## __m128i __lsx_vadd_d (__m128i a, __m128i b)

### Synopsis

```c++
__m128i __lsx_vadd_d (__m128i a, __m128i b)
#include <lsxintrin.h>
Instruction: vadd.d vr, vr, vr
CPU Flags: LSX
```
### Description
Add 64-bit elements in `a` and `b`, save the result in `dst`.
### Operation
```c++
{% include 'vadd_d.h' %}
```

## __m128i __lsx_vadd_q (__m128i a, __m128i b)

### Synopsis

```c++
__m128i __lsx_vadd_q (__m128i a, __m128i b)
#include <lsxintrin.h>
Instruction: vadd.q vr, vr, vr
CPU Flags: LSX
```
### Description
Add 128-bit elements in `a` and `b`, save the result in `dst`.
### Operation
```c++
{% include 'vadd_q.h' %}
```

## __m128i __lsx_vabsd_b (__m128i a, __m128i b)

### Synopsis

```c++
__m128i __lsx_vabsd_b (__m128i a, __m128i b)
#include <lsxintrin.h>
Instruction: vabsd.b vr, vr, vr
CPU Flags: LSX
```
### Description
Compute absolute difference of signed 8-bit elements in `a` and `b`, save the result in `dst`.
### Operation
```c++
{% include 'vabsd_b.h' %}
```

## __m128i __lsx_vabsd_bu (__m128i a, __m128i b)

### Synopsis

```c++
__m128i __lsx_vabsd_bu (__m128i a, __m128i b)
#include <lsxintrin.h>
Instruction: vabsd.bu vr, vr, vr
CPU Flags: LSX
```
### Description
Compute absolute difference of unsigned 8-bit elements in `a` and `b`, save the result in `dst`.
### Operation
```c++
{% include 'vabsd_bu.h' %}
```

## __m128i __lsx_vabsd_h (__m128i a, __m128i b)

### Synopsis

```c++
__m128i __lsx_vabsd_h (__m128i a, __m128i b)
#include <lsxintrin.h>
Instruction: vabsd.h vr, vr, vr
CPU Flags: LSX
```
### Description
Compute absolute difference of signed 16-bit elements in `a` and `b`, save the result in `dst`.
### Operation
```c++
{% include 'vabsd_h.h' %}
```

## __m128i __lsx_vabsd_hu (__m128i a, __m128i b)

### Synopsis

```c++
__m128i __lsx_vabsd_hu (__m128i a, __m128i b)
#include <lsxintrin.h>
Instruction: vabsd.hu vr, vr, vr
CPU Flags: LSX
```
### Description
Compute absolute difference of unsigned 16-bit elements in `a` and `b`, save the result in `dst`.
### Operation
```c++
{% include 'vabsd_hu.h' %}
```

## __m128i __lsx_vabsd_w (__m128i a, __m128i b)

### Synopsis

```c++
__m128i __lsx_vabsd_w (__m128i a, __m128i b)
#include <lsxintrin.h>
Instruction: vabsd.w vr, vr, vr
CPU Flags: LSX
```
### Description
Compute absolute difference of signed 32-bit elements in `a` and `b`, save the result in `dst`.
### Operation
```c++
{% include 'vabsd_w.h' %}
```

## __m128i __lsx_vabsd_wu (__m128i a, __m128i b)

### Synopsis

```c++
__m128i __lsx_vabsd_wu (__m128i a, __m128i b)
#include <lsxintrin.h>
Instruction: vabsd.wu vr, vr, vr
CPU Flags: LSX
```
### Description
Compute absolute difference of unsigned 32-bit elements in `a` and `b`, save the result in `dst`.
### Operation
```c++
{% include 'vabsd_wu.h' %}
```

## __m128i __lsx_vabsd_d (__m128i a, __m128i b)

### Synopsis

```c++
__m128i __lsx_vabsd_d (__m128i a, __m128i b)
#include <lsxintrin.h>
Instruction: vabsd.d vr, vr, vr
CPU Flags: LSX
```
### Description
Compute absolute difference of signed 64-bit elements in `a` and `b`, save the result in `dst`.
### Operation
```c++
{% include 'vabsd_d.h' %}
```

## __m128i __lsx_vabsd_du (__m128i a, __m128i b)

### Synopsis

```c++
__m128i __lsx_vabsd_du (__m128i a, __m128i b)
#include <lsxintrin.h>
Instruction: vabsd.du vr, vr, vr
CPU Flags: LSX
```
### Description
Compute absolute difference of unsigned 64-bit elements in `a` and `b`, save the result in `dst`.
### Operation
```c++
{% include 'vabsd_du.h' %}
```
{{ vadd('b') }}
{{ vadd('h') }}
{{ vadd('w') }}
{{ vadd('d') }}
{{ vadd('q') }}

{{ vabsd('b') }}
{{ vabsd('bu') }}
{{ vabsd('h') }}
{{ vabsd('hu') }}
{{ vabsd('w') }}
{{ vabsd('wu') }}
{{ vabsd('d') }}
{{ vabsd('du') }}
71 changes: 71 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
def define_env(env):
widths = {
"b": 8,
"bu": 8,
"h": 16,
"hu": 16,
"w": 32,
"wu": 32,
"d": 64,
"du": 64,
"q": 128,
}

signednesses = {
"b": "signed",
"bu": "unsigned",
"h": "signed",
"hu": "unsigned",
"w": "signed",
"wu": "unsigned",
"d": "signed",
"du": "unsigned",
"q": "signed",
}

def include(file):
return open(f"code/{file}").read().strip()

def instruction(intrinsic, instr, desc):
file_name = instr.split(" ")[0].replace(".", "_")
return f"""
## {intrinsic}
### Synopsis
```c++
{intrinsic}
#include <lsxintrin.h>
Instruction: {instr}
CPU Flags: LSX
```
### Description
{desc}
### Operation
```c++
{include(f'{file_name}.h')}
```
"""

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

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

0 comments on commit 257039c

Please sign in to comment.