divti3.c revision 239138
117829Spst/* ===-- divti3.c - Implement __divti3 -------------------------------------===
237Srgrimes *
337Srgrimes *                     The LLVM Compiler Infrastructure
4204Snate *
52878Srgrimes * This file is dual licensed under the MIT and the University of Illinois Open
68571Srgrimes * Source Licenses. See LICENSE.TXT for details.
72878Srgrimes *
88571Srgrimes * ===----------------------------------------------------------------------===
98571Srgrimes *
108571Srgrimes * This file implements __divti3 for the compiler_rt library.
112878Srgrimes *
128571Srgrimes * ===----------------------------------------------------------------------===
138571Srgrimes */
142878Srgrimes
158571Srgrimes#include "int_lib.h"
162878Srgrimes
178571Srgrimes#if __x86_64
182878Srgrimes
192878Srgrimestu_int __udivmodti4(tu_int a, tu_int b, tu_int* rem);
202878Srgrimes
218571Srgrimes/* Returns: a / b */
222878Srgrimes
232878Srgrimesti_int
242878Srgrimes__divti3(ti_int a, ti_int b)
252878Srgrimes{
262878Srgrimes    const int bits_in_tword_m1 = (int)(sizeof(ti_int) * CHAR_BIT) - 1;
278571Srgrimes    ti_int s_a = a >> bits_in_tword_m1;           /* s_a = a < 0 ? -1 : 0 */
288571Srgrimes    ti_int s_b = b >> bits_in_tword_m1;           /* s_b = b < 0 ? -1 : 0 */
292878Srgrimes    a = (a ^ s_a) - s_a;                         /* negate if s_a == -1 */
308571Srgrimes    b = (b ^ s_b) - s_b;                         /* negate if s_b == -1 */
312878Srgrimes    s_a ^= s_b;                                  /* sign of quotient */
322878Srgrimes    return (__udivmodti4(a, b, (tu_int*)0) ^ s_a) - s_a;  /* negate if s_a == -1 */
332878Srgrimes}
342878Srgrimes
352878Srgrimes#endif
362878Srgrimes