Skip to content

Commit dd369c7

Browse files
authored
[compiler-rt] Fix tests of __aeabi_(idivmod|uidivmod|uldivmod) to support big endian (#126277)
This patch makes these functions' tests work in big endian mode: - `__aeabi_idivmod`. - `__aeabi_uidivmod`. - `__aeabi_uldivmod`. The three functions return a struct containing two fields, quotient and remainder, via *value in regs* calling convention. They differ in the integer type of each field. In the tests of the first two, a 64-bit integer is used as the return type of the call. And as consequence of the ABI rules for structs (Composite Types), the quotient resides in `r0` and the remainder in `r1` regardless of endianness. So, in order to access each component from the 64-bit integer in the caller code, care must be taken to access the correct bits as they do depend on endianness in this case. In the test of the third one, the caller code has inline assembly to access the components. This assembly code assumed little endian, so it had to be made flexible for big endian as well. `_YUGA_BIG_ENDIAN` is defined in `int_endianness.h`. It's a macro internal to compiler-rt that's in theory compatible with more toolchains than gcc and clang.
1 parent 67f59a6 commit dd369c7

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

compiler-rt/test/builtins/Unit/arm/aeabi_idivmod_test.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,19 @@ int test__aeabi_idivmod(si_int a, si_int b,
1414
{
1515
si_int rem;
1616
du_int ret = __aeabi_idivmod(a, b);
17+
// __aeabi_idivmod actually returns a struct { quotient; remainder; } using
18+
// value_in_regs calling convention. Due to the ABI rules, struct fields
19+
// come in the same order regardless of endianness. However since the
20+
// result is received here as a 64-bit integer, in which endianness does
21+
// matter, the position of each component (quotient and remainder) varies
22+
// depending on endianness.
23+
# if _YUGA_BIG_ENDIAN
24+
rem = ret & 0xFFFFFFFF;
25+
si_int result = ret >> 32;
26+
# else
1727
rem = ret >> 32;
1828
si_int result = ret & 0xFFFFFFFF;
29+
# endif
1930
if (result != expected_result) {
2031
printf("error in __aeabi_idivmod: %d / %d = %d, expected %d\n",
2132
a, b, result, expected_result);

compiler-rt/test/builtins/Unit/arm/aeabi_uidivmod_test.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,19 @@ int test__aeabi_uidivmod(su_int a, su_int b,
1313
su_int expected_result, su_int expected_rem)
1414
{
1515
du_int ret = __aeabi_uidivmod(a, b);
16+
// __aeabi_uidivmod actually returns a struct { quotient; remainder; }
17+
// using value_in_regs calling convention. Due to the ABI rules, struct
18+
// fields come in the same order regardless of endianness. However since
19+
// the result is received here as a 64-bit integer, in which endianness
20+
// does matter, the position of each component (quotient and remainder)
21+
// varies depending on endianness.
22+
# if _YUGA_BIG_ENDIAN
23+
su_int rem = ret & 0xFFFFFFFF;
24+
si_int result = ret >> 32;
25+
# else
1626
su_int rem = ret >> 32;
1727
si_int result = ret & 0xFFFFFFFF;
28+
# endif
1829

1930
if (result != expected_result) {
2031
printf("error in __aeabi_uidivmod: %u / %u = %u, expected %u\n",

compiler-rt/test/builtins/Unit/arm/aeabi_uldivmod_test.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,37 @@ COMPILER_RT_ABI void /* __value_in_regs */ __aeabi_uldivmod(du_int a, du_int b);
1212
int test_aeabi_uldivmod(du_int a, du_int b, du_int expected_q, du_int expected_r)
1313
{
1414
du_int q, r;
15+
// __aeabi_uldivmod returns a struct { quotient; remainder; } using
16+
// value_in_regs calling convention. Each field is a 64-bit integer, so the
17+
// quotient resides in r0 and r1, while the remainder in r2 and r3. The
18+
// byte order however depends on the endianness.
1519
__asm__(
20+
# if _YUGA_BIG_ENDIAN
21+
"movs r1, %Q[a] \n"
22+
"movs r0, %R[a] \n"
23+
"movs r3, %Q[b] \n"
24+
"movs r2, %R[b] \n"
25+
# else
1626
"movs r0, %Q[a] \n"
1727
"movs r1, %R[a] \n"
1828
"movs r2, %Q[b] \n"
1929
"movs r3, %R[b] \n"
30+
# endif
2031
"bl __aeabi_uldivmod \n"
32+
# if _YUGA_BIG_ENDIAN
33+
"movs %Q[q], r1\n"
34+
"movs %R[q], r0\n"
35+
"movs %Q[r], r3\n"
36+
"movs %R[r], r2\n"
37+
# else
2138
"movs %Q[q], r0\n"
2239
"movs %R[q], r1\n"
2340
"movs %Q[r], r2\n"
2441
"movs %R[r], r3\n"
25-
: [q] "=r" (q), [r] "=r"(r)
42+
# endif
43+
: [q] "=r"(q), [r] "=r"(r)
2644
: [a] "r"(a), [b] "r"(b)
27-
: "lr", "r0", "r1", "r2", "r3"
28-
);
45+
: "lr", "r0", "r1", "r2", "r3");
2946
if (q != expected_q || r != expected_r)
3047
printf("error in aeabi_uldivmod: %llX / %llX = %llX, R = %llX, expected %llX, %llX\n",
3148
a, b, q, r, expected_q, expected_r);

0 commit comments

Comments
 (0)