Skip to content

Commit 5ee00b4

Browse files
committed
[crt] Add 48-bit add/sub routines, update tests for 48-bit compiler fixes
1 parent c79fe8d commit 5ee00b4

File tree

5 files changed

+55
-13
lines changed

5 files changed

+55
-13
lines changed

examples/standalone_examples/math_test/autotest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"start": "vram_start",
6363
"size": "vram_16_size",
6464
"expected_CRCs": [
65-
"B9510AD8"
65+
"D4024EF7"
6666
]
6767
},
6868
"neg": {
@@ -94,7 +94,7 @@
9494
"start": "vram_start",
9595
"size": "vram_16_size",
9696
"expected_CRCs": [
97-
"C9C416BB"
97+
"42B53B74"
9898
]
9999
},
100100
"popcnt": {

examples/standalone_examples/math_test/src/main.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,10 @@ static int48_t __builtin_bitreverse48(int48_t x)
8282
// return __builtin_bitreverse64(x) >> 16;
8383
return __builtin_bitreverse32(x >> 16) | (int48_t)__builtin_bitreverse16(x) << 32;
8484
}
85-
static uint24_t __builtin_bswap24(uint24_t x)
86-
{
87-
return __builtin_bswap32(x) >> 8;
88-
// return __builtin_bswap16(x >> 8) | (uint8_t)x << 16;
89-
}
9085
static uint48_t __builtin_bswap48(uint48_t x)
9186
{
9287
return __builtin_bswap64(x) >> 16;
93-
// return __builtin_bswap32(x >> 16) | (uint48_t)(uint16_t)x << 32;
88+
// return __builtin_bswap32(x >> 16) | (uint48_t)__builtin_bswap16(x) << 32;
9489
}
9590
static int __builtin_popcounti48(uint48_t x)
9691
{
@@ -300,11 +295,8 @@ static const bool _0IsUnsignedu = 1;
300295
static const u##UnOp u##unop_##name = {0, _0IsUnsigned##u, #name, b##name##_, s##name##_, i##name##_, l##name##_, i48##name##_, ll##name##_};
301296
#define DEFINE_UNOP_STRUCT_B_TO_LL_EXCEPT_I48(u, name) \
302297
static const u##UnOp u##unop_##name = {0, _0IsUnsigned##u, #name, b##name##_, s##name##_, i##name##_, l##name##_, NULL, ll##name##_};
303-
// clang version 15.0.0 (https://github.com/CE-Programming/llvm-project
304-
// 23b78267b5d376b232475d0805a937e54b61e0d0): unable to legalize instruction:
305-
// %5:_(s48) = G_BSWAP %0:_ (in function: i48bswap_)
306298
#define DEFINE_UNOP_STRUCT_BSWAP(u, name) \
307-
static const u##UnOp u##unop_##name = {0, _0IsUnsigned##u, #name, NULL, s##name##_, i##name##_, l##name##_, NULL, ll##name##_};
299+
static const u##UnOp u##unop_##name = {0, _0IsUnsigned##u, #name, NULL, s##name##_, NULL, l##name##_, i48##name##_, ll##name##_};
308300

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

441433
DEFINE_UNOP_PREFIX_FUNC_S(u, bswap, __builtin_bswap16)
442-
DEFINE_UNOP_PREFIX_FUNC_I(u, bswap, __builtin_bswap24)
443434
DEFINE_UNOP_PREFIX_FUNC_L(u, bswap, __builtin_bswap32)
444435
DEFINE_UNOP_PREFIX_FUNC_I48(u, bswap, __builtin_bswap48)
445436
DEFINE_UNOP_PREFIX_FUNC_LL(u, bswap, __builtin_bswap64)

src/crt/i48add.src

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; Performs 48-bit addition
2+
;
3+
; Returns:
4+
; ude:uhl = ude:uhl + uiy:ubc
5+
6+
assume adl=1
7+
8+
section .text
9+
public __i48add
10+
__i48add:
11+
add hl, bc
12+
ex de, hl
13+
push bc
14+
lea bc, iy
15+
adc hl, bc
16+
pop bc
17+
ex de, hl
18+
ret

src/crt/i48sub.src

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; Performs 48-bit subtraction
2+
;
3+
; Returns:
4+
; ude:uhl = ude:uhl - uiy:ubc
5+
6+
assume adl=1
7+
8+
section .text
9+
public __i48sub
10+
__i48sub:
11+
or a, a
12+
sbc hl, bc
13+
ex de, hl
14+
push bc
15+
lea bc, iy
16+
sbc hl, bc
17+
pop bc
18+
ex de, hl
19+
ret

test/regression/i48routines/src/main.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ void test_##ROUTINE(regs_t *regs) \
6969
: "cc", "a", "c", "e", "l"); \
7070
}
7171

72+
TESTFUNC(i48add)
7273
TESTFUNC(i48and)
7374
TESTFUNC(i48bitrev)
7475
TESTFUNC(i48bswap)
@@ -89,8 +90,14 @@ TESTFUNC(i48remu)
8990
TESTFUNC(i48shl)
9091
TESTFUNC(i48shrs)
9192
TESTFUNC(i48shru)
93+
TESTFUNC(i48sub)
9294
TESTFUNC(i48xor)
9395

96+
void ref_i48add(regs_t *regs)
97+
{
98+
regs->dehl.z_ext += regs->iybc.z_ext;
99+
}
100+
94101
void ref_i48and(regs_t *regs)
95102
{
96103
regs->dehl.z_ext &= regs->iybc.z_ext;
@@ -195,6 +202,11 @@ void ref_i48shru(regs_t *regs)
195202
regs->dehl.z_ext >>= regs->iybc.low & 0xFF;
196203
}
197204

205+
void ref_i48sub(regs_t *regs)
206+
{
207+
regs->dehl.z_ext -= regs->iybc.z_ext;
208+
}
209+
198210
void ref_i48xor(regs_t *regs)
199211
{
200212
regs->dehl.z_ext ^= regs->iybc.z_ext;
@@ -324,6 +336,7 @@ int main(void)
324336

325337
do
326338
{
339+
TEST(unsigned, i48add, 0)
327340
TEST(unsigned, i48and, 0)
328341
TEST(unsigned, i48bitrev, 0)
329342
TEST(unsigned, i48bswap, 0)
@@ -349,6 +362,7 @@ int main(void)
349362
TEST(unsigned_shift, i48shl, 0)
350363
TEST(signed_shift, i48shrs, 0)
351364
TEST(unsigned_shift, i48shru, 0)
365+
TEST(unsigned, i48sub, 0)
352366
TEST(unsigned, i48xor, 0)
353367

354368
printf("All tests passed");

0 commit comments

Comments
 (0)