1*8a6a9560SDaniel Boulby //===-- divmoddi4.c - Implement __divmoddi4 -------------------------------===// 2*8a6a9560SDaniel Boulby // 3*8a6a9560SDaniel Boulby // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*8a6a9560SDaniel Boulby // See https://llvm.org/LICENSE.txt for license information. 5*8a6a9560SDaniel Boulby // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*8a6a9560SDaniel Boulby // 7*8a6a9560SDaniel Boulby //===----------------------------------------------------------------------===// 8*8a6a9560SDaniel Boulby // 9*8a6a9560SDaniel Boulby // This file implements __divmoddi4 for the compiler_rt library. 10*8a6a9560SDaniel Boulby // 11*8a6a9560SDaniel Boulby //===----------------------------------------------------------------------===// 128c80c865SLionel Debieve 138c80c865SLionel Debieve #include "int_lib.h" 148c80c865SLionel Debieve 15*8a6a9560SDaniel Boulby // Returns: a / b, *rem = a % b 168c80c865SLionel Debieve 17*8a6a9560SDaniel Boulby COMPILER_RT_ABI di_int __divmoddi4(di_int a, di_int b, di_int *rem) { 18*8a6a9560SDaniel Boulby const int bits_in_dword_m1 = (int)(sizeof(di_int) * CHAR_BIT) - 1; 19*8a6a9560SDaniel Boulby di_int s_a = a >> bits_in_dword_m1; // s_a = a < 0 ? -1 : 0 20*8a6a9560SDaniel Boulby di_int s_b = b >> bits_in_dword_m1; // s_b = b < 0 ? -1 : 0 21*8a6a9560SDaniel Boulby a = (a ^ s_a) - s_a; // negate if s_a == -1 22*8a6a9560SDaniel Boulby b = (b ^ s_b) - s_b; // negate if s_b == -1 23*8a6a9560SDaniel Boulby s_b ^= s_a; // sign of quotient 24*8a6a9560SDaniel Boulby du_int r; 25*8a6a9560SDaniel Boulby di_int q = (__udivmoddi4(a, b, &r) ^ s_b) - s_b; // negate if s_b == -1 26*8a6a9560SDaniel Boulby *rem = (r ^ s_a) - s_a; // negate if s_a == -1 27*8a6a9560SDaniel Boulby return q; 288c80c865SLionel Debieve } 29