blob: 1d302edc67bda1172e4da44be3a69325f142805f [file] [log] [blame]
Stephen Canon5abb5c12011-03-18 16:35:02 +00001/*===-- 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 *===----------------------------------------------------------------------===*/
Nick Kledzikcceb1f22010-07-27 06:24:32 +000014
15#include "../assembly.h"
16
Stephen Canon5abb5c12011-03-18 16:35:02 +000017#define ESTABLISH_FRAME \
18 push {r4, r7, lr} ;\
19 add r7, sp, #4
20#define CLEAR_FRAME_AND_RETURN \
21 pop {r4, r7, pc}
22
Saleem Abdulrasool57aa97f2014-06-01 04:07:03 +000023 .syntax unified
24 .text
Saleem Abdulrasool1b8f1a42014-06-16 16:05:24 +000025#if __ARM_ARCH_ISA_THUMB == 2
26 .thumb
27#endif
Saleem Abdulrasool57aa97f2014-06-01 04:07:03 +000028
Saleem Abdulrasoola0d65972014-08-09 20:17:43 +000029@ int __modsi3(int divident, int divisor)
30@ Calculate and return the remainder of the (signed) division.
31
Saleem Abdulrasool57aa97f2014-06-01 04:07:03 +000032 .p2align 3
Saleem Abdulrasool48d4e4d2014-10-07 02:39:13 +000033#if __ARM_ARCH_ISA_THUMB == 2
Steven Wu84610ba2014-10-04 00:18:59 +000034DEFINE_COMPILERRT_THUMB_FUNCTION(__modsi3)
Saleem Abdulrasool48d4e4d2014-10-07 02:39:13 +000035#else
36DEFINE_COMPILERRT_FUNCTION(__modsi3)
37#endif
Stephen Hines7633afc2013-10-25 06:26:44 +000038#if __ARM_ARCH_EXT_IDIV__
Nick Kledzik23bbd612013-05-24 19:38:11 +000039 tst r1, r1
40 beq LOCAL_LABEL(divzero)
41 sdiv r2, r0, r1
42 mls r0, r2, r1, r0
43 bx lr
44LOCAL_LABEL(divzero):
45 mov r0, #0
46 bx lr
47#else
Stephen Canon5abb5c12011-03-18 16:35:02 +000048 ESTABLISH_FRAME
49 // Set aside the sign of the dividend.
50 mov r4, r0
51 // Take absolute value of a and b via abs(x) = (x^(x >> 31)) - (x >> 31).
52 eor r2, r0, r0, asr #31
53 eor r3, r1, r1, asr #31
54 sub r0, r2, r0, asr #31
55 sub r1, r3, r1, asr #31
56 // abs(a) % abs(b)
Anton Korobeynikov16536102011-04-19 17:50:09 +000057 bl SYMBOL_NAME(__umodsi3)
Stephen Canon5abb5c12011-03-18 16:35:02 +000058 // Apply sign of dividend to result and return.
59 eor r0, r0, r4, asr #31
60 sub r0, r0, r4, asr #31
61 CLEAR_FRAME_AND_RETURN
Nick Kledzik23bbd612013-05-24 19:38:11 +000062#endif
Joerg Sonnenberger171e9cf2014-01-24 14:33:42 +000063END_COMPILERRT_FUNCTION(__modsi3)
Saleem Abdulrasool590e85b2016-06-22 22:09:42 +000064
65NO_EXEC_STACK_DIRECTIVE
66