divti3.c revision 214152
1214152Sed/* ===-- divti3.c - Implement __divti3 -------------------------------------===
2214152Sed *
3214152Sed *                     The LLVM Compiler Infrastructure
4214152Sed *
5214152Sed * This file is distributed under the University of Illinois Open Source
6214152Sed * License. See LICENSE.TXT for details.
7214152Sed *
8214152Sed * ===----------------------------------------------------------------------===
9214152Sed *
10214152Sed * This file implements __divti3 for the compiler_rt library.
11214152Sed *
12214152Sed * ===----------------------------------------------------------------------===
13214152Sed */
14214152Sed
15214152Sed#if __x86_64
16214152Sed
17214152Sed#include "int_lib.h"
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
35214152Sed#endif
36