divdi3.c revision 353358
1139969Simp//===-- divdi3.c - Implement __divdi3 -------------------------------------===//
21556Srgrimes//
31556Srgrimes// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
41556Srgrimes// See https://llvm.org/LICENSE.txt for license information.
51556Srgrimes// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61556Srgrimes//
71556Srgrimes//===----------------------------------------------------------------------===//
81556Srgrimes//
91556Srgrimes// This file implements __divdi3 for the compiler_rt library.
101556Srgrimes//
111556Srgrimes//===----------------------------------------------------------------------===//
121556Srgrimes
131556Srgrimes#include "int_lib.h"
141556Srgrimes
151556Srgrimes// Returns: a / b
161556Srgrimes
171556SrgrimesCOMPILER_RT_ABI di_int __divdi3(di_int a, di_int b) {
181556Srgrimes  const int bits_in_dword_m1 = (int)(sizeof(di_int) * CHAR_BIT) - 1;
191556Srgrimes  di_int s_a = a >> bits_in_dword_m1;                   // s_a = a < 0 ? -1 : 0
201556Srgrimes  di_int s_b = b >> bits_in_dword_m1;                   // s_b = b < 0 ? -1 : 0
211556Srgrimes  a = (a ^ s_a) - s_a;                                  // negate if s_a == -1
221556Srgrimes  b = (b ^ s_b) - s_b;                                  // negate if s_b == -1
231556Srgrimes  s_a ^= s_b;                                           // sign of quotient
241556Srgrimes  return (__udivmoddi4(a, b, (du_int *)0) ^ s_a) - s_a; // negate if s_a == -1
251556Srgrimes}
261556Srgrimes