137a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan/*===-- moddi3.c - Implement __moddi3 -------------------------------------===
237a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan *
337a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan *                    The LLVM Compiler Infrastructure
437a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan *
59ad441ffec97db647fee3725b3424284fb913e14Howard Hinnant * This file is dual licensed under the MIT and the University of Illinois Open
69ad441ffec97db647fee3725b3424284fb913e14Howard Hinnant * Source Licenses. See LICENSE.TXT for details.
737a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan *
837a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan * ===----------------------------------------------------------------------===
937a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan *
1037a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan * This file implements __moddi3 for the compiler_rt library.
1137a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan *
1237a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan * ===----------------------------------------------------------------------===
1337a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan */
14b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
15b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar#include "int_lib.h"
16b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
1737a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan/* Returns: a % b */
18b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
191c5f89b1dd741135a4007ab577723d422f421eecAnton KorobeynikovCOMPILER_RT_ABI di_int
20b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar__moddi3(di_int a, di_int b)
21b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar{
22b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    const int bits_in_dword_m1 = (int)(sizeof(di_int) * CHAR_BIT) - 1;
2337a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan    di_int s = b >> bits_in_dword_m1;  /* s = b < 0 ? -1 : 0 */
2437a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan    b = (b ^ s) - s;                   /* negate if s == -1 */
2537a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan    s = a >> bits_in_dword_m1;         /* s = a < 0 ? -1 : 0 */
2637a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan    a = (a ^ s) - s;                   /* negate if s == -1 */
272d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines    du_int r;
282d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines    __udivmoddi4(a, b, &r);
292d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines    return ((di_int)r ^ s) - s;                /* negate if s == -1 */
30b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar}
31