xref: /netbsd-src/sys/external/bsd/compiler_rt/dist/lib/builtins/divti3.c (revision f7f78b3373aafafa211473d7733a8806c0e402c0)
1156cd587Sjoerg /* ===-- divti3.c - Implement __divti3 -------------------------------------===
2156cd587Sjoerg  *
3156cd587Sjoerg  *                     The LLVM Compiler Infrastructure
4156cd587Sjoerg  *
5156cd587Sjoerg  * This file is dual licensed under the MIT and the University of Illinois Open
6156cd587Sjoerg  * Source Licenses. See LICENSE.TXT for details.
7156cd587Sjoerg  *
8156cd587Sjoerg  * ===----------------------------------------------------------------------===
9156cd587Sjoerg  *
10156cd587Sjoerg  * This file implements __divti3 for the compiler_rt library.
11156cd587Sjoerg  *
12156cd587Sjoerg  * ===----------------------------------------------------------------------===
13156cd587Sjoerg  */
14156cd587Sjoerg 
15156cd587Sjoerg #include "int_lib.h"
16156cd587Sjoerg 
17156cd587Sjoerg #ifdef CRT_HAS_128BIT
18156cd587Sjoerg 
19156cd587Sjoerg /* Returns: a / b */
20156cd587Sjoerg 
21*f7f78b33Sjoerg COMPILER_RT_ABI ti_int
__divti3(ti_int a,ti_int b)22156cd587Sjoerg __divti3(ti_int a, ti_int b)
23156cd587Sjoerg {
24156cd587Sjoerg     const int bits_in_tword_m1 = (int)(sizeof(ti_int) * CHAR_BIT) - 1;
25156cd587Sjoerg     ti_int s_a = a >> bits_in_tword_m1;           /* s_a = a < 0 ? -1 : 0 */
26156cd587Sjoerg     ti_int s_b = b >> bits_in_tword_m1;           /* s_b = b < 0 ? -1 : 0 */
27156cd587Sjoerg     a = (a ^ s_a) - s_a;                         /* negate if s_a == -1 */
28156cd587Sjoerg     b = (b ^ s_b) - s_b;                         /* negate if s_b == -1 */
29156cd587Sjoerg     s_a ^= s_b;                                  /* sign of quotient */
30156cd587Sjoerg     return (__udivmodti4(a, b, (tu_int*)0) ^ s_a) - s_a;  /* negate if s_a == -1 */
31156cd587Sjoerg }
32156cd587Sjoerg 
33156cd587Sjoerg #endif /* CRT_HAS_128BIT */
34