Path: blob/main/contrib/llvm-project/compiler-rt/lib/builtins/divmodsi4.c
35260 views
//===-- divmodsi4.c - Implement __divmodsi41//--------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// This file implements __divmodsi4 for the compiler_rt library.10//11//===----------------------------------------------------------------------===//1213#include "int_lib.h"1415// Returns: a / b, *rem = a % b1617COMPILER_RT_ABI si_int __divmodsi4(si_int a, si_int b, si_int *rem) {18const int bits_in_word_m1 = (int)(sizeof(si_int) * CHAR_BIT) - 1;19si_int s_a = a >> bits_in_word_m1; // s_a = a < 0 ? -1 : 020si_int s_b = b >> bits_in_word_m1; // s_b = b < 0 ? -1 : 021a = (su_int)(a ^ s_a) - s_a; // negate if s_a == -122b = (su_int)(b ^ s_b) - s_b; // negate if s_b == -123s_b ^= s_a; // sign of quotient24su_int r;25si_int q = (__udivmodsi4(a, b, &r) ^ s_b) - s_b; // negate if s_b == -126*rem = (r ^ s_a) - s_a; // negate if s_a == -127return q;28}293031