Stephen Canon | 6bbe0bb | 2011-03-18 16:35:02 +0000 | [diff] [blame] | 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 | *===----------------------------------------------------------------------===*/ |
Nick Kledzik | dada275 | 2010-07-27 06:24:32 +0000 | [diff] [blame] | 14 | |
| 15 | #include "../assembly.h" |
| 16 | |
Stephen Canon | 6bbe0bb | 2011-03-18 16:35:02 +0000 | [diff] [blame] | 17 | #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 | |
| 23 | .syntax unified |
| 24 | .align 3 |
Nick Kledzik | dada275 | 2010-07-27 06:24:32 +0000 | [diff] [blame] | 25 | DEFINE_COMPILERRT_FUNCTION(__modsi3) |
Nick Kledzik | a09d09d | 2013-05-24 19:38:11 +0000 | [diff] [blame] | 26 | #if __ARM_ARCH_7S__ |
| 27 | tst r1, r1 |
| 28 | beq LOCAL_LABEL(divzero) |
| 29 | sdiv r2, r0, r1 |
| 30 | mls r0, r2, r1, r0 |
| 31 | bx lr |
| 32 | LOCAL_LABEL(divzero): |
| 33 | mov r0, #0 |
| 34 | bx lr |
| 35 | #else |
Stephen Canon | 6bbe0bb | 2011-03-18 16:35:02 +0000 | [diff] [blame] | 36 | ESTABLISH_FRAME |
| 37 | // Set aside the sign of the dividend. |
| 38 | mov r4, r0 |
| 39 | // Take absolute value of a and b via abs(x) = (x^(x >> 31)) - (x >> 31). |
| 40 | eor r2, r0, r0, asr #31 |
| 41 | eor r3, r1, r1, asr #31 |
| 42 | sub r0, r2, r0, asr #31 |
| 43 | sub r1, r3, r1, asr #31 |
| 44 | // abs(a) % abs(b) |
Anton Korobeynikov | 647fc73 | 2011-04-19 17:50:09 +0000 | [diff] [blame] | 45 | bl SYMBOL_NAME(__umodsi3) |
Stephen Canon | 6bbe0bb | 2011-03-18 16:35:02 +0000 | [diff] [blame] | 46 | // Apply sign of dividend to result and return. |
| 47 | eor r0, r0, r4, asr #31 |
| 48 | sub r0, r0, r4, asr #31 |
| 49 | CLEAR_FRAME_AND_RETURN |
Nick Kledzik | a09d09d | 2013-05-24 19:38:11 +0000 | [diff] [blame] | 50 | #endif |