diff mbox series

[v2,kvm-unit-tests,2/2] arm: add eabi version of 64-bit division functions

Message ID 20210512105440.748153-3-pbonzini@redhat.com (mailing list archive)
State New, archived
Headers show
Series fix long division routines for ARM eabi | expand

Commit Message

Paolo Bonzini May 12, 2021, 10:54 a.m. UTC
eabi prescribes different entry points for 64-bit division on
32-bit platforms.  Implement a wrapper for the GCC-style __divmoddi4
and __udivmoddi4 functions.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arm/Makefile.arm  |  1 +
 lib/arm/ldivmod.S | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+)
 create mode 100644 lib/arm/ldivmod.S

Comments

Alexandru Elisei May 12, 2021, 1:44 p.m. UTC | #1
Hi Paolo,

On 5/12/21 11:54 AM, Paolo Bonzini wrote:
> eabi prescribes different entry points for 64-bit division on
> 32-bit platforms.  Implement a wrapper for the GCC-style __divmoddi4
> and __udivmoddi4 functions.
>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  arm/Makefile.arm  |  1 +
>  lib/arm/ldivmod.S | 32 ++++++++++++++++++++++++++++++++
>  2 files changed, 33 insertions(+)
>  create mode 100644 lib/arm/ldivmod.S
>
> diff --git a/arm/Makefile.arm b/arm/Makefile.arm
> index 687a8ed..3a4cc6b 100644
> --- a/arm/Makefile.arm
> +++ b/arm/Makefile.arm
> @@ -24,6 +24,7 @@ cflatobjs += lib/arm/spinlock.o
>  cflatobjs += lib/arm/processor.o
>  cflatobjs += lib/arm/stack.o
>  cflatobjs += lib/ldiv32.o
> +cflatobjs += lib/arm/ldivmod.o
>  
>  # arm specific tests
>  tests =
> diff --git a/lib/arm/ldivmod.S b/lib/arm/ldivmod.S
> new file mode 100644
> index 0000000..de11ac9
> --- /dev/null
> +++ b/lib/arm/ldivmod.S
> @@ -0,0 +1,32 @@
> +// EABI ldivmod and uldivmod implementation based on libcompiler-rt
> +//
> +// This file is dual licensed under the MIT and the University of Illinois Open
> +// Source Licenses.

At first I was confused about the prototype for these functions, but I suppose
they are the functions defined by [1] and [2], and they take a two int64
arguments, the numerator and the denominator.

[1]
https://android.googlesource.com/toolchain/compiler-rt/+/refs/heads/master/lib/builtins/arm/aeabi_uldivmod.S
[2]
https://android.googlesource.com/toolchain/compiler-rt/+/refs/heads/master/lib/builtins/arm/aeabi_ldivmod.S
> +
> +	.syntax unified
> +	.align 2
> +	.globl __aeabi_uldivmod
> +	.type __aeabi_uldivmod, %function
> +__aeabi_uldivmod:
> +	push	{r11, lr}
> +	sub	sp, sp, #16
> +	add	r12, sp, #8
> +	str	r12, [sp]                // third argument to __udivmoddi4

The way we call __udivmoddi4 looks correct to me. We make room on the stack to
store the remainder, and push that address at the top of the stack so it can be
used by the function as the third argument

> +	bl	__udivmoddi4
> +	ldr	r2, [sp, #8]             // remainder returned in r2-r3
> +	ldr	r3, [sp, #12]
> +	add	sp, sp, #16
> +	pop	{r11, pc}

I'm not sure what is going on here. Is the function returning 2 64bit arguments as
an 128bit vector? Or is the function being called from assembly and this is a
convention between it and the caller? I did a grep in the compiler-rt repo for
__aeabi_uldivmod and couldn't find any uses.

Other than my confusion about the return value, both functions match the
compiler-rt definitions.

Thanks,

Alex

> +
> +	.globl __aeabi_ldivmod
> +	.type __aeabi_ldivmod, %function
> +__aeabi_ldivmod:
> +	push	{r11, lr}
> +	sub	sp, sp, #16
> +	add	r12, sp, #8
> +	str	r12, [sp]                // third argument to __divmoddi4
> +	bl	__divmoddi4
> +	ldr	r2, [sp, #8]             // remainder returned in r2-r3
> +	ldr	r3, [sp, #12]
> +	add	sp, sp, #16
> +	pop	{r11, pc}
Paolo Bonzini May 12, 2021, 1:51 p.m. UTC | #2
On 12/05/21 15:44, Alexandru Elisei wrote:
>> +	bl	__udivmoddi4
>> +	ldr	r2, [sp, #8]             // remainder returned in r2-r3
>> +	ldr	r3, [sp, #12]
>> +	add	sp, sp, #16
>> +	pop	{r11, pc}
> 
> I'm not sure what is going on here. Is the function returning 2 64bit arguments as
> an 128bit vector? Or is the function being called from assembly and this is a
> convention between it and the caller?

It's an eABI convention that spans the runtime and the compiler.

https://developer.arm.com/documentation/ihi0043/e/?lang=en#standardized-compiler-helper-functions 
says it returns a "pair of (unsigned) long longs is returned in {{r0, 
r1}, {r2, r3}}, the quotient in {r0, r1}, and the remainder in {r2, r3}."

Paolo
Alexandru Elisei May 12, 2021, 2:04 p.m. UTC | #3
Hi Paolo,

On 5/12/21 2:51 PM, Paolo Bonzini wrote:
> On 12/05/21 15:44, Alexandru Elisei wrote:
>>> +    bl    __udivmoddi4
>>> +    ldr    r2, [sp, #8]             // remainder returned in r2-r3
>>> +    ldr    r3, [sp, #12]
>>> +    add    sp, sp, #16
>>> +    pop    {r11, pc}
>>
>> I'm not sure what is going on here. Is the function returning 2 64bit arguments as
>> an 128bit vector? Or is the function being called from assembly and this is a
>> convention between it and the caller?
>
> It's an eABI convention that spans the runtime and the compiler.
>
> https://developer.arm.com/documentation/ihi0043/e/?lang=en#standardized-compiler-helper-functions
> says it returns a "pair of (unsigned) long longs is returned in {{r0, r1}, {r2,
> r3}}, the quotient in {r0, r1}, and the remainder in {r2, r3}."

Thanks for the link, the functions are indeed returning the quotient in {r0, r1}
and remainder in {r2, r3} according to the convention:

Reviewed-by: Alexandru Elisei <alexandru.elisei@arm.com>

Thanks,

Alex
diff mbox series

Patch

diff --git a/arm/Makefile.arm b/arm/Makefile.arm
index 687a8ed..3a4cc6b 100644
--- a/arm/Makefile.arm
+++ b/arm/Makefile.arm
@@ -24,6 +24,7 @@  cflatobjs += lib/arm/spinlock.o
 cflatobjs += lib/arm/processor.o
 cflatobjs += lib/arm/stack.o
 cflatobjs += lib/ldiv32.o
+cflatobjs += lib/arm/ldivmod.o
 
 # arm specific tests
 tests =
diff --git a/lib/arm/ldivmod.S b/lib/arm/ldivmod.S
new file mode 100644
index 0000000..de11ac9
--- /dev/null
+++ b/lib/arm/ldivmod.S
@@ -0,0 +1,32 @@ 
+// EABI ldivmod and uldivmod implementation based on libcompiler-rt
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses.
+
+	.syntax unified
+	.align 2
+	.globl __aeabi_uldivmod
+	.type __aeabi_uldivmod, %function
+__aeabi_uldivmod:
+	push	{r11, lr}
+	sub	sp, sp, #16
+	add	r12, sp, #8
+	str	r12, [sp]                // third argument to __udivmoddi4
+	bl	__udivmoddi4
+	ldr	r2, [sp, #8]             // remainder returned in r2-r3
+	ldr	r3, [sp, #12]
+	add	sp, sp, #16
+	pop	{r11, pc}
+
+	.globl __aeabi_ldivmod
+	.type __aeabi_ldivmod, %function
+__aeabi_ldivmod:
+	push	{r11, lr}
+	sub	sp, sp, #16
+	add	r12, sp, #8
+	str	r12, [sp]                // third argument to __divmoddi4
+	bl	__divmoddi4
+	ldr	r2, [sp, #8]             // remainder returned in r2-r3
+	ldr	r3, [sp, #12]
+	add	sp, sp, #16
+	pop	{r11, pc}