1214152Sed/*===-- moddi3.c - Implement __moddi3 -------------------------------------===
2214152Sed *
3214152Sed *                    The LLVM Compiler Infrastructure
4214152Sed *
5222656Sed * This file is dual licensed under the MIT and the University of Illinois Open
6222656Sed * Source Licenses. See LICENSE.TXT for details.
7214152Sed *
8214152Sed * ===----------------------------------------------------------------------===
9214152Sed *
10214152Sed * This file implements __moddi3 for the compiler_rt library.
11214152Sed *
12214152Sed * ===----------------------------------------------------------------------===
13214152Sed */
14214152Sed
15214152Sed#include "int_lib.h"
16214152Sed
17222656SedCOMPILER_RT_ABI du_int __udivmoddi4(du_int a, du_int b, du_int* rem);
18214152Sed
19214152Sed/* Returns: a % b */
20214152Sed
21222656SedCOMPILER_RT_ABI di_int
22214152Sed__moddi3(di_int a, di_int b)
23214152Sed{
24214152Sed    const int bits_in_dword_m1 = (int)(sizeof(di_int) * CHAR_BIT) - 1;
25214152Sed    di_int s = b >> bits_in_dword_m1;  /* s = b < 0 ? -1 : 0 */
26214152Sed    b = (b ^ s) - s;                   /* negate if s == -1 */
27214152Sed    s = a >> bits_in_dword_m1;         /* s = a < 0 ? -1 : 0 */
28214152Sed    a = (a ^ s) - s;                   /* negate if s == -1 */
29214152Sed    di_int r;
30214152Sed    __udivmoddi4(a, b, (du_int*)&r);
31214152Sed    return (r ^ s) - s;                /* negate if s == -1 */
32214152Sed}
33