1276789Sdim/* ===-- divsi3.c - Implement __divsi3 -------------------------------------===
2276789Sdim *
3276789Sdim *                     The LLVM Compiler Infrastructure
4276789Sdim *
5276789Sdim * This file is dual licensed under the MIT and the University of Illinois Open
6276789Sdim * Source Licenses. See LICENSE.TXT for details.
7276789Sdim *
8276789Sdim * ===----------------------------------------------------------------------===
9276789Sdim *
10276789Sdim * This file implements __divsi3 for the compiler_rt library.
11276789Sdim *
12276789Sdim * ===----------------------------------------------------------------------===
13276789Sdim */
14276789Sdim
15276789Sdim#include "int_lib.h"
16276789Sdim
17276789Sdim/* Returns: a / b */
18276789Sdim
19276789SdimARM_EABI_FNALIAS(idiv, divsi3)
20276789Sdim
21276789SdimCOMPILER_RT_ABI si_int
22276789Sdim__divsi3(si_int a, si_int b)
23276789Sdim{
24276789Sdim    const int bits_in_word_m1 = (int)(sizeof(si_int) * CHAR_BIT) - 1;
25276789Sdim    si_int s_a = a >> bits_in_word_m1;           /* s_a = a < 0 ? -1 : 0 */
26276789Sdim    si_int s_b = b >> bits_in_word_m1;           /* s_b = b < 0 ? -1 : 0 */
27276789Sdim    a = (a ^ s_a) - s_a;                         /* negate if s_a == -1 */
28276789Sdim    b = (b ^ s_b) - s_b;                         /* negate if s_b == -1 */
29276789Sdim    s_a ^= s_b;                                  /* sign of quotient */
30276789Sdim    /*
31276789Sdim     * On CPUs without unsigned hardware division support,
32276789Sdim     *  this calls __udivsi3 (notice the cast to su_int).
33276789Sdim     * On CPUs with unsigned hardware division support,
34276789Sdim     *  this uses the unsigned division instruction.
35276789Sdim     */
36276789Sdim    return ((su_int)a/(su_int)b ^ s_a) - s_a;    /* negate if s_a == -1 */
37276789Sdim}
38