Skip to content

Commit 4e629dd

Browse files
runer112mateoconlechuga
authored andcommitted
Implement optimized lbswap and llbswap
1 parent 33bddaa commit 4e629dd

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

examples/standalone/math_test/autotest.json

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
"key|enter",
2121
"hashWait|bitrev",
2222
"key|enter",
23+
"hashWait|bswap",
24+
"key|enter",
2325
"hashWait|popcnt",
2426
"key|enter",
2527
"hashWait|and",
@@ -87,6 +89,14 @@
8789
"BBADF82E"
8890
]
8991
},
92+
"bswap": {
93+
"description": "bswap",
94+
"start": "vram_start",
95+
"size": "vram_16_size",
96+
"expected_CRCs": [
97+
"7709B5AE"
98+
]
99+
},
90100
"popcnt": {
91101
"description": "popcnt",
92102
"start": "vram_start",

examples/standalone/math_test/src/main.c

+9
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ DEFINE_BINOP_TYPE(u)
206206
static const u##UnOp unop_##name = {#name, b##name##_, s##name##_, i##name##_, l##name##_};
207207
#define DEFINE_UNOP_STRUCT_B_TO_LL(u, name) \
208208
static const u##UnOp unop_##name = {#name, b##name##_, s##name##_, i##name##_, l##name##_, ll##name##_};
209+
#define DEFINE_UNOP_STRUCT_S_L_LL(u, name) \
210+
static const u##UnOp unop_##name = {#name, 0, s##name##_, 0, l##name##_, ll##name##_};
209211

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

321+
// Needs to be unsigned to avoid extra bits from sign extension
322+
DEFINE_UNOP_PREFIX_FUNC_S(u, bswap, __builtin_bswap16)
323+
DEFINE_UNOP_PREFIX_FUNC_L(u, bswap, __builtin_bswap32)
324+
DEFINE_UNOP_PREFIX_FUNC_LL(u, bswap, __builtin_bswap64)
325+
DEFINE_UNOP_STRUCT_S_L_LL(u, bswap)
326+
319327
// Needs to be unsigned to avoid extra bits from sign extension
320328
DEFINE_UNOP_PREFIX_FUNC_B_TO_I(u, popcnt, __builtin_popcount)
321329
DEFINE_UNOP_PREFIX_FUNC_L(u, popcnt, __builtin_popcountl)
@@ -349,6 +357,7 @@ static const UnOp *unops[] = {
349357
&unop_neg,
350358
&unop_abs,
351359
&unop_bitrev,
360+
(const UnOp *)&unop_bswap,
352361
(const UnOp *)&unop_popcnt,
353362
};
354363

src/crt/lbswap.src

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
assume adl=1
2+
3+
section .text
4+
public __lbswap
5+
__lbswap:
6+
push hl ; ...[hlu]hl <- (sp)
7+
inc sp ; ...[hlu]h <- (sp)
8+
ld h, e ; uhl = [hlu]el
9+
push hl ; ...[hlu]h[hlu]el <- (sp)
10+
inc sp ; ...[hlu]h[hlu]e <- (sp)
11+
ld e, l
12+
pop hl ; euhl = lh[hlu]e
13+
; ...[hlu] <- (sp)
14+
inc sp ; ... <- (sp)
15+
ret

src/crt/llbswap.src

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
assume adl=1
2+
3+
section .text
4+
public __llbswap
5+
__llbswap:
6+
push af
7+
ld a, b ; a = b
8+
ld b, l ; bcudeuhl = lc[deu]de[hlu]hl
9+
ld l, d ; bcudeuhl = lc[deu]de[hlu]hd
10+
ld d, c ; bcudeuhl = lc[deu]ce[hlu]hd
11+
ld c, h ; bcudeuhl = lh[deu]ce[hlu]hd
12+
ld h, e ; bcudeuhl = lh[deu]ce[hlu]ed
13+
ld e, a ; bcudeuhl = lh[deu]cb[hlu]ed
14+
ex de, hl ; bcudeuhl = lh[hlu]ed[deu]cb
15+
pop af
16+
ret

0 commit comments

Comments
 (0)