divdi3.c revision 214152
1238106Sdes/* ===-- divdi3.c - Implement __divdi3 -------------------------------------===
2238106Sdes *
3238106Sdes *                     The LLVM Compiler Infrastructure
4238106Sdes *
5238106Sdes * This file is distributed under the University of Illinois Open Source
6238106Sdes * License. See LICENSE.TXT for details.
7238106Sdes *
8238106Sdes * ===----------------------------------------------------------------------===
9238106Sdes *
10238106Sdes * This file implements __divdi3 for the compiler_rt library.
11238106Sdes *
12238106Sdes * ===----------------------------------------------------------------------===
13238106Sdes */
14238106Sdes
15238106Sdes#include "int_lib.h"
16238106Sdes
17238106Sdesdu_int __udivmoddi4(du_int a, du_int b, du_int* rem);
18238106Sdes
19238106Sdes/* Returns: a / b */
20238106Sdes
21238106Sdesdi_int
22238106Sdes__divdi3(di_int a, di_int b)
23238106Sdes{
24266114Sdes    const int bits_in_dword_m1 = (int)(sizeof(di_int) * CHAR_BIT) - 1;
25266114Sdes    di_int s_a = a >> bits_in_dword_m1;           /* s_a = a < 0 ? -1 : 0 */
26266114Sdes    di_int s_b = b >> bits_in_dword_m1;           /* s_b = b < 0 ? -1 : 0 */
27266114Sdes    a = (a ^ s_a) - s_a;                         /* negate if s_a == -1 */
28266114Sdes    b = (b ^ s_b) - s_b;                         /* negate if s_b == -1 */
29266114Sdes    s_a ^= s_b;                                  /*sign of quotient */
30266114Sdes    return (__udivmoddi4(a, b, (du_int*)0) ^ s_a) - s_a;  /* negate if s_a == -1 */
31266114Sdes}
32266114Sdes