1214152Sed/* ===-- divsi3.c - Implement __divsi3 -------------------------------------===
2214152Sed *
3214152Sed *                     The LLVM Compiler Infrastructure
4214152Sed *
5222656Sed * This file is dual licensed under the MIT and the University of Illinois Open
6222656Sed * Source Licenses. See LICENSE.TXT for details.
7214152Sed *
8214152Sed * ===----------------------------------------------------------------------===
9214152Sed *
10214152Sed * This file implements __divsi3 for the compiler_rt library.
11214152Sed *
12214152Sed * ===----------------------------------------------------------------------===
13214152Sed */
14214152Sed
15214152Sed#include "int_lib.h"
16214152Sed
17222656Sedsu_int COMPILER_RT_ABI __udivsi3(su_int n, su_int d);
18214152Sed
19214152Sed/* Returns: a / b */
20214152Sed
21239138SandrewARM_EABI_FNALIAS(idiv, divsi3)
22222656Sed
23222656SedCOMPILER_RT_ABI si_int
24214152Sed__divsi3(si_int a, si_int b)
25214152Sed{
26214152Sed    const int bits_in_word_m1 = (int)(sizeof(si_int) * CHAR_BIT) - 1;
27214152Sed    si_int s_a = a >> bits_in_word_m1;           /* s_a = a < 0 ? -1 : 0 */
28214152Sed    si_int s_b = b >> bits_in_word_m1;           /* s_b = b < 0 ? -1 : 0 */
29214152Sed    a = (a ^ s_a) - s_a;                         /* negate if s_a == -1 */
30214152Sed    b = (b ^ s_b) - s_b;                         /* negate if s_b == -1 */
31214152Sed    s_a ^= s_b;                                  /* sign of quotient */
32239138Sandrew    /*
33239138Sandrew     * On CPUs without unsigned hardware division support,
34239138Sandrew     *  this calls __udivsi3 (notice the cast to su_int).
35239138Sandrew     * On CPUs with unsigned hardware division support,
36239138Sandrew     *  this uses the unsigned division instruction.
37239138Sandrew     */
38239138Sandrew    return ((su_int)a/(su_int)b ^ s_a) - s_a;    /* negate if s_a == -1 */
39214152Sed}
40