Skip to content

Commit

Permalink
[crt] Add 48-bit add/sub routines, update tests for 48-bit compiler f…
Browse files Browse the repository at this point in the history
…ixes
  • Loading branch information
calc84maniac committed Nov 19, 2024
1 parent c79fe8d commit 5ee00b4
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 13 deletions.
4 changes: 2 additions & 2 deletions examples/standalone_examples/math_test/autotest.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"start": "vram_start",
"size": "vram_16_size",
"expected_CRCs": [
"B9510AD8"
"D4024EF7"
]
},
"neg": {
Expand Down Expand Up @@ -94,7 +94,7 @@
"start": "vram_start",
"size": "vram_16_size",
"expected_CRCs": [
"C9C416BB"
"42B53B74"
]
},
"popcnt": {
Expand Down
13 changes: 2 additions & 11 deletions examples/standalone_examples/math_test/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,10 @@ static int48_t __builtin_bitreverse48(int48_t x)
// return __builtin_bitreverse64(x) >> 16;
return __builtin_bitreverse32(x >> 16) | (int48_t)__builtin_bitreverse16(x) << 32;
}
static uint24_t __builtin_bswap24(uint24_t x)
{
return __builtin_bswap32(x) >> 8;
// return __builtin_bswap16(x >> 8) | (uint8_t)x << 16;
}
static uint48_t __builtin_bswap48(uint48_t x)
{
return __builtin_bswap64(x) >> 16;
// return __builtin_bswap32(x >> 16) | (uint48_t)(uint16_t)x << 32;
// return __builtin_bswap32(x >> 16) | (uint48_t)__builtin_bswap16(x) << 32;
}
static int __builtin_popcounti48(uint48_t x)
{
Expand Down Expand Up @@ -300,11 +295,8 @@ static const bool _0IsUnsignedu = 1;
static const u##UnOp u##unop_##name = {0, _0IsUnsigned##u, #name, b##name##_, s##name##_, i##name##_, l##name##_, i48##name##_, ll##name##_};
#define DEFINE_UNOP_STRUCT_B_TO_LL_EXCEPT_I48(u, name) \
static const u##UnOp u##unop_##name = {0, _0IsUnsigned##u, #name, b##name##_, s##name##_, i##name##_, l##name##_, NULL, ll##name##_};
// clang version 15.0.0 (https://github.com/CE-Programming/llvm-project
// 23b78267b5d376b232475d0805a937e54b61e0d0): unable to legalize instruction:
// %5:_(s48) = G_BSWAP %0:_ (in function: i48bswap_)
#define DEFINE_UNOP_STRUCT_BSWAP(u, name) \
static const u##UnOp u##unop_##name = {0, _0IsUnsigned##u, #name, NULL, s##name##_, i##name##_, l##name##_, NULL, ll##name##_};
static const u##UnOp u##unop_##name = {0, _0IsUnsigned##u, #name, NULL, s##name##_, NULL, l##name##_, i48##name##_, ll##name##_};

#define DEFINE_BINOP_STRUCT_B(u, name) \
static const u##BinOp u##binop_##name = {1, _0IsUnsigned##u, #name, b##name##_};
Expand Down Expand Up @@ -439,7 +431,6 @@ DEFINE_UNOP_PREFIX_FUNC_LL( , bitrev, __builtin_bitreverse64)
DEFINE_UNOP_STRUCT_B_TO_LL(, bitrev)

DEFINE_UNOP_PREFIX_FUNC_S(u, bswap, __builtin_bswap16)
DEFINE_UNOP_PREFIX_FUNC_I(u, bswap, __builtin_bswap24)
DEFINE_UNOP_PREFIX_FUNC_L(u, bswap, __builtin_bswap32)
DEFINE_UNOP_PREFIX_FUNC_I48(u, bswap, __builtin_bswap48)
DEFINE_UNOP_PREFIX_FUNC_LL(u, bswap, __builtin_bswap64)
Expand Down
18 changes: 18 additions & 0 deletions src/crt/i48add.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
; Performs 48-bit addition
;
; Returns:
; ude:uhl = ude:uhl + uiy:ubc

assume adl=1

section .text
public __i48add
__i48add:
add hl, bc
ex de, hl
push bc
lea bc, iy
adc hl, bc
pop bc
ex de, hl
ret
19 changes: 19 additions & 0 deletions src/crt/i48sub.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
; Performs 48-bit subtraction
;
; Returns:
; ude:uhl = ude:uhl - uiy:ubc

assume adl=1

section .text
public __i48sub
__i48sub:
or a, a
sbc hl, bc
ex de, hl
push bc
lea bc, iy
sbc hl, bc
pop bc
ex de, hl
ret
14 changes: 14 additions & 0 deletions test/regression/i48routines/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ void test_##ROUTINE(regs_t *regs) \
: "cc", "a", "c", "e", "l"); \
}

TESTFUNC(i48add)
TESTFUNC(i48and)
TESTFUNC(i48bitrev)
TESTFUNC(i48bswap)
Expand All @@ -89,8 +90,14 @@ TESTFUNC(i48remu)
TESTFUNC(i48shl)
TESTFUNC(i48shrs)
TESTFUNC(i48shru)
TESTFUNC(i48sub)
TESTFUNC(i48xor)

void ref_i48add(regs_t *regs)
{
regs->dehl.z_ext += regs->iybc.z_ext;
}

void ref_i48and(regs_t *regs)
{
regs->dehl.z_ext &= regs->iybc.z_ext;
Expand Down Expand Up @@ -195,6 +202,11 @@ void ref_i48shru(regs_t *regs)
regs->dehl.z_ext >>= regs->iybc.low & 0xFF;
}

void ref_i48sub(regs_t *regs)
{
regs->dehl.z_ext -= regs->iybc.z_ext;
}

void ref_i48xor(regs_t *regs)
{
regs->dehl.z_ext ^= regs->iybc.z_ext;
Expand Down Expand Up @@ -324,6 +336,7 @@ int main(void)

do
{
TEST(unsigned, i48add, 0)
TEST(unsigned, i48and, 0)
TEST(unsigned, i48bitrev, 0)
TEST(unsigned, i48bswap, 0)
Expand All @@ -349,6 +362,7 @@ int main(void)
TEST(unsigned_shift, i48shl, 0)
TEST(signed_shift, i48shrs, 0)
TEST(unsigned_shift, i48shru, 0)
TEST(unsigned, i48sub, 0)
TEST(unsigned, i48xor, 0)

printf("All tests passed");
Expand Down

0 comments on commit 5ee00b4

Please sign in to comment.