1214152Sed/* ===-- divdi3.c - Implement __divdi3 -------------------------------------===
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 __divdi3 for the compiler_rt library.
11214152Sed *
12214152Sed * ===----------------------------------------------------------------------===
13214152Sed */
14214152Sed
15214152Sed#include "int_lib.h"
16214152Sed
17222656Seddu_int COMPILER_RT_ABI __udivmoddi4(du_int a, du_int b, du_int* rem);
18214152Sed
19214152Sed/* Returns: a / b */
20214152Sed
21222656SedCOMPILER_RT_ABI di_int
22214152Sed__divdi3(di_int a, di_int b)
23214152Sed{
24214152Sed    const int bits_in_dword_m1 = (int)(sizeof(di_int) * CHAR_BIT) - 1;
25214152Sed    di_int s_a = a >> bits_in_dword_m1;           /* s_a = a < 0 ? -1 : 0 */
26214152Sed    di_int s_b = b >> bits_in_dword_m1;           /* s_b = b < 0 ? -1 : 0 */
27214152Sed    a = (a ^ s_a) - s_a;                         /* negate if s_a == -1 */
28214152Sed    b = (b ^ s_b) - s_b;                         /* negate if s_b == -1 */
29214152Sed    s_a ^= s_b;                                  /*sign of quotient */
30214152Sed    return (__udivmoddi4(a, b, (du_int*)0) ^ s_a) - s_a;  /* negate if s_a == -1 */
31214152Sed}
32