1214152Sed/* ===-- divti3.c - Implement __divti3 -------------------------------------===
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 __divti3 for the compiler_rt library.
11214152Sed *
12214152Sed * ===----------------------------------------------------------------------===
13214152Sed */
14214152Sed
15239138Sandrew#include "int_lib.h"
16239138Sandrew
17263763Sdim#ifdef CRT_HAS_128BIT
18214152Sed
19214152Sedtu_int __udivmodti4(tu_int a, tu_int b, tu_int* rem);
20214152Sed
21214152Sed/* Returns: a / b */
22214152Sed
23214152Sedti_int
24214152Sed__divti3(ti_int a, ti_int b)
25214152Sed{
26214152Sed    const int bits_in_tword_m1 = (int)(sizeof(ti_int) * CHAR_BIT) - 1;
27214152Sed    ti_int s_a = a >> bits_in_tword_m1;           /* s_a = a < 0 ? -1 : 0 */
28214152Sed    ti_int s_b = b >> bits_in_tword_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 */
32214152Sed    return (__udivmodti4(a, b, (tu_int*)0) ^ s_a) - s_a;  /* negate if s_a == -1 */
33214152Sed}
34214152Sed
35263763Sdim#endif /* CRT_HAS_128BIT */
36