divdi3.c revision 276851
1139804Simp/* ===-- divdi3.c - Implement __divdi3 -------------------------------------===
2885Swollman *
3885Swollman *                     The LLVM Compiler Infrastructure
4885Swollman *
5885Swollman * This file is dual licensed under the MIT and the University of Illinois Open
6885Swollman * Source Licenses. See LICENSE.TXT for details.
7885Swollman *
8885Swollman * ===----------------------------------------------------------------------===
9885Swollman *
10885Swollman * This file implements __divdi3 for the compiler_rt library.
11885Swollman *
12885Swollman * ===----------------------------------------------------------------------===
13885Swollman */
14885Swollman
15885Swollman#include "int_lib.h"
16885Swollman
1710625Sdg/* Returns: a / b */
18885Swollman
19885SwollmanCOMPILER_RT_ABI di_int
20885Swollman__divdi3(di_int a, di_int b)
21885Swollman{
22885Swollman    const int bits_in_dword_m1 = (int)(sizeof(di_int) * CHAR_BIT) - 1;
23885Swollman    di_int s_a = a >> bits_in_dword_m1;           /* s_a = a < 0 ? -1 : 0 */
24885Swollman    di_int s_b = b >> bits_in_dword_m1;           /* s_b = b < 0 ? -1 : 0 */
25885Swollman    a = (a ^ s_a) - s_a;                         /* negate if s_a == -1 */
26885Swollman    b = (b ^ s_b) - s_b;                         /* negate if s_b == -1 */
27116182Sobrien    s_a ^= s_b;                                  /*sign of quotient */
28116182Sobrien    return (__udivmoddi4(a, b, (du_int*)0) ^ s_a) - s_a;  /* negate if s_a == -1 */
29116182Sobrien}
302056Swollman