]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/StdLib/Xdiv.c
Standard Libraries for EDK II.
[mirror_edk2.git] / StdLib / LibC / StdLib / Xdiv.c
1 /** @file
2 The div, ldiv, and lldiv, functions compute numer / denom and
3 numer % denom in a single operation.
4
5 The div, ldiv, and lldiv functions return a structure of type div_t, ldiv_t,
6 and lldiv_t, respectively, comprising both the quotient and the remainder.
7 The structures shall contain (in either order) the members quot
8 (the quotient) and rem (the remainder), each of which has the same type as
9 the arguments numer and denom. If either part of the result cannot be
10 represented, the behavior is undefined.
11
12 Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
13 This program and the accompanying materials are licensed and made available under
14 the terms and conditions of the BSD License that accompanies this distribution.
15 The full text of the license may be found at
16 http://opensource.org/licenses/bsd-license.php.
17
18 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
19 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 **/
21 #include <LibConfig.h>
22 #include <sys/EfiCdefs.h>
23
24 #include <Library/BaseLib.h>
25 #include <stdlib.h> /* div_t, ldiv_t, lldiv_t */
26
27 /* DivS64x64Remainder will write the remainder as a 64-bit value, so we store
28 it first into bigrem and then into r.rem. This avoids writing the remainder
29 beyond the end of the div_t structure.
30 */
31 div_t
32 div(int num, int denom)
33 {
34 div_t r;
35 INT64 bigrem;
36
37 r.quot = (int)DivS64x64Remainder( (INT64)num, (INT64)denom, &bigrem);
38 r.rem = (int)bigrem;
39
40 return (r);
41 }
42
43 /* DivS64x64Remainder will write the remainder as a 64-bit value, so we store
44 it first into bigrem and then into r.rem. This avoids writing the remainder
45 beyond the end of the div_t structure.
46 */
47 ldiv_t
48 ldiv(long num, long denom)
49 {
50 ldiv_t r;
51 INT64 bigrem;
52
53 r.quot = (long)DivS64x64Remainder( (INT64)num, (INT64)denom, &bigrem);
54 r.rem = (long)bigrem;
55
56 return (r);
57 }
58
59 /* DivS64x64Remainder will write the remainder as a 64-bit value, so we store
60 it first into bigrem and then into r.rem. This avoids writing the remainder
61 beyond the end of the div_t structure if r.rem is narrower than 64-bits.
62
63 Even though most implementations make long long 64 bits wide, we still go
64 through bigrem, just-in-case.
65 */
66 lldiv_t
67 lldiv(long long num, long long denom)
68 {
69 lldiv_t r;
70 INT64 bigrem;
71
72 r.quot = (long long)DivS64x64Remainder( (INT64)num, (INT64)denom, &bigrem);
73 r.rem = (long long)bigrem;
74
75 return (r);
76 }