xref: /rk3399_ARM-atf/lib/compiler-rt/builtins/divmoddi4.c (revision d4f50633373ba48ab9e1244979fc74593fa2b91b)
18a6a9560SDaniel Boulby //===-- divmoddi4.c - Implement __divmoddi4 -------------------------------===//
28a6a9560SDaniel Boulby //
38a6a9560SDaniel Boulby // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
48a6a9560SDaniel Boulby // See https://llvm.org/LICENSE.txt for license information.
58a6a9560SDaniel Boulby // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
68a6a9560SDaniel Boulby //
78a6a9560SDaniel Boulby //===----------------------------------------------------------------------===//
88a6a9560SDaniel Boulby //
98a6a9560SDaniel Boulby // This file implements __divmoddi4 for the compiler_rt library.
108a6a9560SDaniel Boulby //
118a6a9560SDaniel Boulby //===----------------------------------------------------------------------===//
128c80c865SLionel Debieve 
138c80c865SLionel Debieve #include "int_lib.h"
148c80c865SLionel Debieve 
158a6a9560SDaniel Boulby // Returns: a / b, *rem = a % b
168c80c865SLionel Debieve 
__divmoddi4(di_int a,di_int b,di_int * rem)178a6a9560SDaniel Boulby COMPILER_RT_ABI di_int __divmoddi4(di_int a, di_int b, di_int *rem) {
188a6a9560SDaniel Boulby   const int bits_in_dword_m1 = (int)(sizeof(di_int) * CHAR_BIT) - 1;
198a6a9560SDaniel Boulby   di_int s_a = a >> bits_in_dword_m1;                   // s_a = a < 0 ? -1 : 0
208a6a9560SDaniel Boulby   di_int s_b = b >> bits_in_dword_m1;                   // s_b = b < 0 ? -1 : 0
21*cdd6089dSManish Pandey   a = (du_int)(a ^ s_a) - s_a;                          // negate if s_a == -1
22*cdd6089dSManish Pandey   b = (du_int)(b ^ s_b) - s_b;                          // negate if s_b == -1
238a6a9560SDaniel Boulby   s_b ^= s_a;                                           // sign of quotient
248a6a9560SDaniel Boulby   du_int r;
258a6a9560SDaniel Boulby   di_int q = (__udivmoddi4(a, b, &r) ^ s_b) - s_b;      // negate if s_b == -1
268a6a9560SDaniel Boulby   *rem = (r ^ s_a) - s_a;                               // negate if s_a == -1
278a6a9560SDaniel Boulby   return q;
288c80c865SLionel Debieve }
29