xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/arm/modsi3.S (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric//===-- modsi3.S - 32-bit signed integer modulus --------------------------===//
2*0b57cec5SDimitry Andric//
3*0b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric//
7*0b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric//
9*0b57cec5SDimitry Andric// This file implements the __modsi3 (32-bit signed integer modulus) function
10*0b57cec5SDimitry Andric// for the ARM architecture as a wrapper around the unsigned routine.
11*0b57cec5SDimitry Andric//
12*0b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
13*0b57cec5SDimitry Andric
14*0b57cec5SDimitry Andric#include "../assembly.h"
15*0b57cec5SDimitry Andric
16*0b57cec5SDimitry Andric#define ESTABLISH_FRAME \
17*0b57cec5SDimitry Andric    push   {r4, r7, lr}    ;\
18*0b57cec5SDimitry Andric    add     r7,     sp, #4
19*0b57cec5SDimitry Andric#define CLEAR_FRAME_AND_RETURN \
20*0b57cec5SDimitry Andric    pop    {r4, r7, pc}
21*0b57cec5SDimitry Andric
22*0b57cec5SDimitry Andric	.syntax unified
23*0b57cec5SDimitry Andric	.text
24*0b57cec5SDimitry Andric	DEFINE_CODE_STATE
25*0b57cec5SDimitry Andric
26*0b57cec5SDimitry Andric@ int __modsi3(int divident, int divisor)
27*0b57cec5SDimitry Andric@   Calculate and return the remainder of the (signed) division.
28*0b57cec5SDimitry Andric
29*0b57cec5SDimitry Andric	.p2align 3
30*0b57cec5SDimitry AndricDEFINE_COMPILERRT_FUNCTION(__modsi3)
31*0b57cec5SDimitry Andric#if __ARM_ARCH_EXT_IDIV__
32*0b57cec5SDimitry Andric	tst     r1, r1
33*0b57cec5SDimitry Andric	beq     LOCAL_LABEL(divzero)
34*0b57cec5SDimitry Andric	sdiv	r2, r0, r1
35*0b57cec5SDimitry Andric	mls 	r0, r2, r1, r0
36*0b57cec5SDimitry Andric	bx      lr
37*0b57cec5SDimitry AndricLOCAL_LABEL(divzero):
38*0b57cec5SDimitry Andric	mov     r0, #0
39*0b57cec5SDimitry Andric	bx      lr
40*0b57cec5SDimitry Andric#else
41*0b57cec5SDimitry Andric    ESTABLISH_FRAME
42*0b57cec5SDimitry Andric    //  Set aside the sign of the dividend.
43*0b57cec5SDimitry Andric    mov     r4,     r0
44*0b57cec5SDimitry Andric    //  Take absolute value of a and b via abs(x) = (x^(x >> 31)) - (x >> 31).
45*0b57cec5SDimitry Andric    eor     r2,     r0, r0, asr #31
46*0b57cec5SDimitry Andric    eor     r3,     r1, r1, asr #31
47*0b57cec5SDimitry Andric    sub     r0,     r2, r0, asr #31
48*0b57cec5SDimitry Andric    sub     r1,     r3, r1, asr #31
49*0b57cec5SDimitry Andric    //  abs(a) % abs(b)
50*0b57cec5SDimitry Andric    bl     SYMBOL_NAME(__umodsi3)
51*0b57cec5SDimitry Andric    //  Apply sign of dividend to result and return.
52*0b57cec5SDimitry Andric    eor     r0,     r0, r4, asr #31
53*0b57cec5SDimitry Andric    sub     r0,     r0, r4, asr #31
54*0b57cec5SDimitry Andric    CLEAR_FRAME_AND_RETURN
55*0b57cec5SDimitry Andric#endif
56*0b57cec5SDimitry AndricEND_COMPILERRT_FUNCTION(__modsi3)
57*0b57cec5SDimitry Andric
58*0b57cec5SDimitry AndricNO_EXEC_STACK_DIRECTIVE
59*0b57cec5SDimitry Andric
60