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