11556Srgrimes/* ===-- divdi3.c - Implement __divdi3 -------------------------------------===
21556Srgrimes *
31556Srgrimes *                     The LLVM Compiler Infrastructure
41556Srgrimes *
51556Srgrimes * This file is dual licensed under the MIT and the University of Illinois Open
61556Srgrimes * Source Licenses. See LICENSE.TXT for details.
71556Srgrimes *
81556Srgrimes * ===----------------------------------------------------------------------===
91556Srgrimes *
101556Srgrimes * This file implements __divdi3 for the compiler_rt library.
111556Srgrimes *
121556Srgrimes * ===----------------------------------------------------------------------===
131556Srgrimes */
141556Srgrimes
151556Srgrimes#include "int_lib.h"
161556Srgrimes
171556Srgrimesdu_int COMPILER_RT_ABI __udivmoddi4(du_int a, du_int b, du_int* rem);
181556Srgrimes
191556Srgrimes/* Returns: a / b */
201556Srgrimes
211556SrgrimesCOMPILER_RT_ABI di_int
221556Srgrimes__divdi3(di_int a, di_int b)
231556Srgrimes{
241556Srgrimes    const int bits_in_dword_m1 = (int)(sizeof(di_int) * CHAR_BIT) - 1;
251556Srgrimes    di_int s_a = a >> bits_in_dword_m1;           /* s_a = a < 0 ? -1 : 0 */
261556Srgrimes    di_int s_b = b >> bits_in_dword_m1;           /* s_b = b < 0 ? -1 : 0 */
271556Srgrimes    a = (a ^ s_a) - s_a;                         /* negate if s_a == -1 */
281556Srgrimes    b = (b ^ s_b) - s_b;                         /* negate if s_b == -1 */
291556Srgrimes    s_a ^= s_b;                                  /*sign of quotient */
301556Srgrimes    return (__udivmoddi4(a, b, (du_int*)0) ^ s_a) - s_a;  /* negate if s_a == -1 */
311556Srgrimes}
321556Srgrimes