1 | /* $Id: divmoddi4.c 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - __divmoddi4 implementation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | #include <iprt/stdint.h>
|
---|
28 | #include <iprt/uint64.h>
|
---|
29 |
|
---|
30 | int64_t __divmoddi4(int64_t i64A, int64_t i64B, int64_t *pi64R);
|
---|
31 | uint64_t __udivmoddi4(uint64_t u64A, uint64_t u64B, uint64_t *pu64R);
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * __divmoddi4() implementation to satisfy external references from 32-bit code
|
---|
35 | * generated by gcc-7 or later (more likely with gcc-11).
|
---|
36 | *
|
---|
37 | * @param i64A The divident value.
|
---|
38 | * @param i64B The divisor value.
|
---|
39 | * @param pi64R A pointer to the reminder. May be NULL.
|
---|
40 | * @returns i64A / i64B
|
---|
41 | */
|
---|
42 | int64_t __divmoddi4(int64_t i64A, int64_t i64B, int64_t *pi64R)
|
---|
43 | {
|
---|
44 | int64_t i64Ret;
|
---|
45 | if (i64A >= 0)
|
---|
46 | {
|
---|
47 | /* Dividing two non-negative numbers is the same as unsigned division. */
|
---|
48 | if (i64B >= 0)
|
---|
49 | i64Ret = (int64_t)__udivmoddi4((uint64_t)i64A, (uint64_t)i64B, (uint64_t *)pi64R);
|
---|
50 | /* Dividing a non-negative number by a negative one yields a negative
|
---|
51 | result and positive remainder. */
|
---|
52 | else
|
---|
53 | i64Ret = -(int64_t)__udivmoddi4((uint64_t)i64A, (uint64_t)-i64B, (uint64_t *)pi64R);
|
---|
54 | }
|
---|
55 | else
|
---|
56 | {
|
---|
57 | uint64_t u64R;
|
---|
58 |
|
---|
59 | /* Dividing a negative number by a non-negative one yields a negative
|
---|
60 | result and negative remainder. */
|
---|
61 | if (i64B >= 0)
|
---|
62 | i64Ret = -(int64_t)__udivmoddi4((uint64_t)-i64A, (uint64_t)i64B, &u64R);
|
---|
63 | /* Dividing two negative numbers yields a positive result and a
|
---|
64 | negative remainder. */
|
---|
65 | else
|
---|
66 | i64Ret = (int64_t)__udivmoddi4((uint64_t)-i64A, (uint64_t)-i64B, &u64R);
|
---|
67 |
|
---|
68 | if (pi64R)
|
---|
69 | *pi64R = -(int64_t)u64R;
|
---|
70 | }
|
---|
71 |
|
---|
72 | return i64Ret;
|
---|
73 | }
|
---|
74 |
|
---|