xref: /netbsd-src/sys/external/bsd/compiler_rt/dist/lib/builtins/divsi3.c (revision d31434597b255f3fd7646698e04170d148f1de49)
1156cd587Sjoerg /* ===-- divsi3.c - Implement __divsi3 -------------------------------------===
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 __divsi3 for the compiler_rt library.
11156cd587Sjoerg  *
12156cd587Sjoerg  * ===----------------------------------------------------------------------===
13156cd587Sjoerg  */
14156cd587Sjoerg 
15156cd587Sjoerg #include "int_lib.h"
16156cd587Sjoerg 
17156cd587Sjoerg /* Returns: a / b */
18156cd587Sjoerg 
19156cd587Sjoerg COMPILER_RT_ABI si_int
__divsi3(si_int a,si_int b)20156cd587Sjoerg __divsi3(si_int a, si_int b)
21156cd587Sjoerg {
22156cd587Sjoerg     const int bits_in_word_m1 = (int)(sizeof(si_int) * CHAR_BIT) - 1;
23156cd587Sjoerg     si_int s_a = a >> bits_in_word_m1;           /* s_a = a < 0 ? -1 : 0 */
24156cd587Sjoerg     si_int s_b = b >> bits_in_word_m1;           /* s_b = b < 0 ? -1 : 0 */
25156cd587Sjoerg     a = (a ^ s_a) - s_a;                         /* negate if s_a == -1 */
26156cd587Sjoerg     b = (b ^ s_b) - s_b;                         /* negate if s_b == -1 */
27156cd587Sjoerg     s_a ^= s_b;                                  /* sign of quotient */
28156cd587Sjoerg     /*
29156cd587Sjoerg      * On CPUs without unsigned hardware division support,
30156cd587Sjoerg      *  this calls __udivsi3 (notice the cast to su_int).
31156cd587Sjoerg      * On CPUs with unsigned hardware division support,
32156cd587Sjoerg      *  this uses the unsigned division instruction.
33156cd587Sjoerg      */
34156cd587Sjoerg     return ((su_int)a/(su_int)b ^ s_a) - s_a;    /* negate if s_a == -1 */
35156cd587Sjoerg }
363044ee7eSrin 
373044ee7eSrin #if defined(__ARM_EABI__)
38*d3143459Srin AEABI_RTABI si_int __aeabi_idiv(si_int a, si_int b) COMPILER_RT_ALIAS(__divsi3);
393044ee7eSrin #endif
40