xref: /netbsd-src/sys/external/bsd/compiler_rt/dist/lib/builtins/arm/modsi3.S (revision f6e43b3179e19718c2af5950ddbd1384060c23e7)
1/*===-- modsi3.S - 32-bit signed integer modulus --------------------------===//
2 *
3 *                     The LLVM Compiler Infrastructure
4 *
5 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
7 *
8 *===----------------------------------------------------------------------===//
9 *
10 * This file implements the __modsi3 (32-bit signed integer modulus) function
11 * for the ARM architecture as a wrapper around the unsigned routine.
12 *
13 *===----------------------------------------------------------------------===*/
14
15#include "../assembly.h"
16
17#define ESTABLISH_FRAME \
18    push   {r4, lr}
19#define CLEAR_FRAME_AND_RETURN \
20    pop    {r4, pc}
21
22	.syntax unified
23	.text
24#if __ARM_ARCH_ISA_THUMB == 2
25	.thumb
26#endif
27
28@ int __modsi3(int divident, int divisor)
29@   Calculate and return the remainder of the (signed) division.
30
31	.p2align 3
32#if __ARM_ARCH_ISA_THUMB == 2
33DEFINE_COMPILERRT_THUMB_FUNCTION(__modsi3)
34#else
35DEFINE_COMPILERRT_FUNCTION(__modsi3)
36#endif
37#if __ARM_ARCH_EXT_IDIV__
38	tst     r1, r1
39	beq     LOCAL_LABEL(divzero)
40	sdiv	r2, r0, r1
41	mls 	r0, r2, r1, r0
42	bx      lr
43LOCAL_LABEL(divzero):
44	mov     r0, #0
45	bx      lr
46#else
47    ESTABLISH_FRAME
48    //  Set aside the sign of the dividend.
49    mov     r4,     r0
50    //  Take absolute value of a and b via abs(x) = (x^(x >> 31)) - (x >> 31).
51    eor     r2,     r0, r0, asr #31
52    eor     r3,     r1, r1, asr #31
53    sub     r0,     r2, r0, asr #31
54    sub     r1,     r3, r1, asr #31
55    //  abs(a) % abs(b)
56    bl     SYMBOL_NAME(__umodsi3)
57    //  Apply sign of dividend to result and return.
58    eor     r0,     r0, r4, asr #31
59    sub     r0,     r0, r4, asr #31
60    CLEAR_FRAME_AND_RETURN
61#endif
62END_COMPILERRT_FUNCTION(__modsi3)
63