Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement optimized lbswap and llbswap #386

Merged
merged 1 commit into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/standalone/math_test/autotest.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"key|enter",
"hashWait|bitrev",
"key|enter",
"hashWait|bswap",
"key|enter",
"hashWait|popcnt",
"key|enter",
"hashWait|and",
Expand Down Expand Up @@ -87,6 +89,14 @@
"BBADF82E"
]
},
"bswap": {
"description": "bswap",
"start": "vram_start",
"size": "vram_16_size",
"expected_CRCs": [
"7709B5AE"
]
},
"popcnt": {
"description": "popcnt",
"start": "vram_start",
Expand Down
9 changes: 9 additions & 0 deletions examples/standalone/math_test/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ DEFINE_BINOP_TYPE(u)
static const u##UnOp unop_##name = {#name, b##name##_, s##name##_, i##name##_, l##name##_};
#define DEFINE_UNOP_STRUCT_B_TO_LL(u, name) \
static const u##UnOp unop_##name = {#name, b##name##_, s##name##_, i##name##_, l##name##_, ll##name##_};
#define DEFINE_UNOP_STRUCT_S_L_LL(u, name) \
static const u##UnOp unop_##name = {#name, 0, s##name##_, 0, l##name##_, ll##name##_};

#define DEFINE_BINOP_STRUCT_B(u, name) \
static const u##BinOp binop_##name = {#name, b##name##_};
Expand Down Expand Up @@ -316,6 +318,12 @@ DEFINE_UNOP_PREFIX_FUNC_L( , bitrev, __builtin_bitreverse32)
DEFINE_UNOP_PREFIX_FUNC_LL( , bitrev, __builtin_bitreverse64)
DEFINE_UNOP_STRUCT_B_TO_LL( , bitrev)

// Needs to be unsigned to avoid extra bits from sign extension
DEFINE_UNOP_PREFIX_FUNC_S(u, bswap, __builtin_bswap16)
DEFINE_UNOP_PREFIX_FUNC_L(u, bswap, __builtin_bswap32)
DEFINE_UNOP_PREFIX_FUNC_LL(u, bswap, __builtin_bswap64)
DEFINE_UNOP_STRUCT_S_L_LL(u, bswap)

// Needs to be unsigned to avoid extra bits from sign extension
DEFINE_UNOP_PREFIX_FUNC_B_TO_I(u, popcnt, __builtin_popcount)
DEFINE_UNOP_PREFIX_FUNC_L(u, popcnt, __builtin_popcountl)
Expand Down Expand Up @@ -349,6 +357,7 @@ static const UnOp *unops[] = {
&unop_neg,
&unop_abs,
&unop_bitrev,
(const UnOp *)&unop_bswap,
(const UnOp *)&unop_popcnt,
};

Expand Down
15 changes: 15 additions & 0 deletions src/crt/lbswap.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
assume adl=1

section .text
public __lbswap
__lbswap:
push hl ; ...[hlu]hl <- (sp)
inc sp ; ...[hlu]h <- (sp)
ld h, e ; uhl = [hlu]el
push hl ; ...[hlu]h[hlu]el <- (sp)
inc sp ; ...[hlu]h[hlu]e <- (sp)
ld e, l
pop hl ; euhl = lh[hlu]e
; ...[hlu] <- (sp)
inc sp ; ... <- (sp)
ret
16 changes: 16 additions & 0 deletions src/crt/llbswap.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
assume adl=1

section .text
public __llbswap
__llbswap:
push af
ld a, b ; a = b
ld b, l ; bcudeuhl = lc[deu]de[hlu]hl
ld l, d ; bcudeuhl = lc[deu]de[hlu]hd
ld d, c ; bcudeuhl = lc[deu]ce[hlu]hd
ld c, h ; bcudeuhl = lh[deu]ce[hlu]hd
ld h, e ; bcudeuhl = lh[deu]ce[hlu]ed
ld e, a ; bcudeuhl = lh[deu]cb[hlu]ed
ex de, hl ; bcudeuhl = lh[hlu]ed[deu]cb
pop af
ret