]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/Math64.c
UefiLib:
[mirror_edk2.git] / MdePkg / Library / BaseLib / Math64.c
1 /** @file
2 Leaf math worker functions that require 64-bit arithmetic support from the
3 compiler.
4
5 Copyright (c) 2006, Intel Corporation<BR>
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 Module Name: Math64.c
15
16 **/
17
18 UINT64
19 EFIAPI
20 InternalMathLShiftU64 (
21 IN UINT64 Operand,
22 IN UINTN Count
23 )
24 {
25 return Operand << Count;
26 }
27
28 UINT64
29 EFIAPI
30 InternalMathRShiftU64 (
31 IN UINT64 Operand,
32 IN UINTN Count
33 )
34 {
35 return Operand >> Count;
36 }
37
38 UINT64
39 EFIAPI
40 InternalMathARShiftU64 (
41 IN UINT64 Operand,
42 IN UINTN Count
43 )
44 {
45 //
46 // Test if this compiler supports arithmetic shift
47 //
48 if ((((-1) << (sizeof (-1) * 8 - 1)) >> (sizeof (-1) * 8 - 1)) == -1) {
49 //
50 // Arithmetic shift is supported
51 //
52 return (UINT64)((INT64)Operand >> Count);
53 }
54
55 //
56 // Arithmetic is not supported
57 //
58 return (Operand >> Count) |
59 ((INTN)Operand < 0 ? ~((UINTN)-1 >> Count) : 0);
60 }
61
62 UINT64
63 EFIAPI
64 InternalMathLRotU64 (
65 IN UINT64 Operand,
66 IN UINTN Count
67 )
68 {
69 return (Operand << Count) | (Operand >> (64 - Count));
70 }
71
72 UINT64
73 EFIAPI
74 InternalMathRRotU64 (
75 IN UINT64 Operand,
76 IN UINTN Count
77 )
78 {
79 return (Operand >> Count) | (Operand << (64 - Count));
80 }
81
82 UINT64
83 EFIAPI
84 InternalMathSwapBytes64 (
85 IN UINT64 Operand
86 )
87 {
88 return (UINT64)(
89 ((UINT64)SwapBytes32 ((UINT32)Operand) << 32) |
90 ((UINT64)SwapBytes32 ((UINT32)(Operand >> 32)))
91 );
92 }
93
94 UINT64
95 EFIAPI
96 InternalMathMultU64x32 (
97 IN UINT64 Multiplicand,
98 IN UINT32 Multiplier
99 )
100 {
101 return Multiplicand * Multiplier;
102 }
103
104 UINT64
105 EFIAPI
106 InternalMathMultU64x64 (
107 IN UINT64 Multiplicand,
108 IN UINT64 Multiplier
109 )
110 {
111 return Multiplicand * Multiplier;
112 }
113
114 UINT64
115 EFIAPI
116 InternalMathDivU64x32 (
117 IN UINT64 Dividend,
118 IN UINT32 Divisor
119 )
120 {
121 return Dividend / Divisor;
122 }
123
124 UINT32
125 EFIAPI
126 InternalMathModU64x32 (
127 IN UINT64 Dividend,
128 IN UINT32 Divisor
129 )
130 {
131 return (UINT32)(Dividend % Divisor);
132 }
133
134 UINT64
135 EFIAPI
136 InternalMathDivRemU64x32 (
137 IN UINT64 Dividend,
138 IN UINT32 Divisor,
139 OUT UINT32 *Remainder
140 )
141 {
142 if (Remainder != NULL) {
143 *Remainder = (UINT32)(Dividend % Divisor);
144 }
145 return Dividend / Divisor;
146 }
147
148 UINT64
149 EFIAPI
150 InternalMathDivRemU64x64 (
151 IN UINT64 Dividend,
152 IN UINT64 Divisor,
153 OUT UINT64 *Remainder
154 )
155 {
156 if (Remainder != NULL) {
157 *Remainder = Dividend % Divisor;
158 }
159 return Dividend / Divisor;
160 }
161
162 INT64
163 EFIAPI
164 InternalMathDivRemS64x64 (
165 IN INT64 Dividend,
166 IN INT64 Divisor,
167 OUT INT64 *Remainder
168 )
169 {
170 if (Remainder != NULL) {
171 *Remainder = Dividend % Divisor;
172 }
173 return Dividend / Divisor;
174 }