Path: blob/master/libs/compiler-rt/lib/builtins/divsi3.c
4395 views
/* ===-- divsi3.c - Implement __divsi3 -------------------------------------===1*2* The LLVM Compiler Infrastructure3*4* This file is dual licensed under the MIT and the University of Illinois Open5* Source Licenses. See LICENSE.TXT for details.6*7* ===----------------------------------------------------------------------===8*9* This file implements __divsi3 for the compiler_rt library.10*11* ===----------------------------------------------------------------------===12*/1314#include "int_lib.h"1516/* Returns: a / b */1718COMPILER_RT_ABI si_int19__divsi3(si_int a, si_int b)20{21const int bits_in_word_m1 = (int)(sizeof(si_int) * CHAR_BIT) - 1;22si_int s_a = a >> bits_in_word_m1; /* s_a = a < 0 ? -1 : 0 */23si_int s_b = b >> bits_in_word_m1; /* s_b = b < 0 ? -1 : 0 */24a = (a ^ s_a) - s_a; /* negate if s_a == -1 */25b = (b ^ s_b) - s_b; /* negate if s_b == -1 */26s_a ^= s_b; /* sign of quotient */27/*28* On CPUs without unsigned hardware division support,29* this calls __udivsi3 (notice the cast to su_int).30* On CPUs with unsigned hardware division support,31* this uses the unsigned division instruction.32*/33return ((su_int)a/(su_int)b ^ s_a) - s_a; /* negate if s_a == -1 */34}3536#if defined(__ARM_EABI__)37AEABI_RTABI si_int __aeabi_idiv(si_int a, si_int b) COMPILER_RT_ALIAS(__divsi3);38#endif394041