1168404Spjd/*===-- moddi3.c - Implement __moddi3 -------------------------------------===
2168404Spjd *
3168404Spjd *                    The LLVM Compiler Infrastructure
4168404Spjd *
5168404Spjd * This file is dual licensed under the MIT and the University of Illinois Open
6168404Spjd * Source Licenses. See LICENSE.TXT for details.
7168404Spjd *
8168404Spjd * ===----------------------------------------------------------------------===
9168404Spjd *
10168404Spjd * This file implements __moddi3 for the compiler_rt library.
11168404Spjd *
12168404Spjd * ===----------------------------------------------------------------------===
13168404Spjd */
14168404Spjd
15168404Spjd#include "int_lib.h"
16168404Spjd
17168404SpjdCOMPILER_RT_ABI du_int __udivmoddi4(du_int a, du_int b, du_int* rem);
18168404Spjd
19168404Spjd/* Returns: a % b */
20168404Spjd
21168404SpjdCOMPILER_RT_ABI di_int
22219089Spjd__moddi3(di_int a, di_int b)
23168404Spjd{
24168404Spjd    const int bits_in_dword_m1 = (int)(sizeof(di_int) * CHAR_BIT) - 1;
25169195Spjd    di_int s = b >> bits_in_dword_m1;  /* s = b < 0 ? -1 : 0 */
26169195Spjd    b = (b ^ s) - s;                   /* negate if s == -1 */
27168404Spjd    s = a >> bits_in_dword_m1;         /* s = a < 0 ? -1 : 0 */
28168404Spjd    a = (a ^ s) - s;                   /* negate if s == -1 */
29168404Spjd    di_int r;
30168404Spjd    __udivmoddi4(a, b, (du_int*)&r);
31168404Spjd    return (r ^ s) - s;                /* negate if s == -1 */
32168404Spjd}
33168404Spjd