]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BasePrintLib/PrintLibInternal.c
1.Fix SetMem64.S to not use SSE3 instruction
[mirror_edk2.git] / MdePkg / Library / BasePrintLib / PrintLibInternal.c
CommitLineData
878ddf1f 1/** @file\r
2 Print Library worker functions.\r
3\r
4 Copyright (c) 2006, Intel Corporation<BR>\r
5 All rights reserved. This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13 Module Name: PrintLibInternal.c\r
14\r
15**/\r
16\r
17#include "PrintLibInternal.h"\r
18\r
19static CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};\r
20\r
3f9f540d 21\r
22/**\r
23 Internal function that places the character into the Buffer.\r
24\r
25 Internal function that places ASCII or Unicode character into the Buffer.\r
26\r
27 @param Buffer Buffer to place the Unicode or ASCII string.\r
4f7f1f5f 28 @param EndBuffer The end of the input Buffer. No characters will be\r
29 placed after that. \r
3f9f540d 30 @param Length Count of character to be placed into Buffer.\r
31 @param Character Character to be placed into Buffer.\r
32 @param Increment Character increment in Buffer.\r
33\r
34 @return Number of characters printed.\r
35\r
36**/\r
878ddf1f 37CHAR8 *\r
38BasePrintLibFillBuffer (\r
39 CHAR8 *Buffer,\r
4f7f1f5f 40 CHAR8 *EndBuffer,\r
878ddf1f 41 INTN Length,\r
42 UINTN Character,\r
43 INTN Increment\r
44 )\r
45{\r
46 INTN Index;\r
47\r
4f7f1f5f 48 for (Index = 0; Index < Length && Buffer < EndBuffer; Index++) {\r
878ddf1f 49 *Buffer = (CHAR8) Character;\r
50 *(Buffer + 1) = (CHAR8) (Character >> 8);\r
51 Buffer += Increment;\r
52 }\r
53 return Buffer;\r
54}\r
55\r
56/**\r
3f9f540d 57 Internal function that convert a decimal number to a string in Buffer.\r
878ddf1f 58\r
3f9f540d 59 Print worker function that convert a decimal number to a string in Buffer.\r
60\r
61 @param Buffer Location to place the Unicode or ASCII string of Value.\r
62 @param Value Value to convert to a Decimal or Hexidecimal string in Buffer.\r
63 @param Radix Radix of the value\r
878ddf1f 64\r
65 @return Number of characters printed.\r
66\r
67**/\r
68UINTN\r
69EFIAPI\r
70BasePrintLibValueToString (\r
71 IN OUT CHAR8 *Buffer,\r
72 IN INT64 Value,\r
73 IN UINTN Radix\r
74 )\r
75{\r
76 UINTN Digits;\r
77 UINT32 Remainder;\r
78\r
79 //\r
80 // Loop to convert one digit at a time in reverse order\r
81 //\r
82 *(Buffer++) = 0;\r
83 Digits = 0;\r
84 do {\r
85 Value = (INT64)DivU64x32Remainder ((UINT64)Value, (UINT32)Radix, &Remainder);\r
86 *(Buffer++) = mHexStr[Remainder];\r
87 Digits++;\r
88 } while (Value != 0);\r
89 return Digits;\r
90}\r
91\r
3f9f540d 92/**\r
93 Internal function that converts a decimal value to a Null-terminated string.\r
94 \r
95 Converts the decimal number specified by Value to a Null-terminated \r
96 string specified by Buffer containing at most Width characters.\r
97 If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.\r
add13dc2 98 The number of characters in Buffer is returned not including the Null-terminator.\r
3f9f540d 99 If the conversion contains more than Width characters, then only the first\r
100 Width characters are returned, and the total number of characters \r
101 required to perform the conversion is returned.\r
102 Additional conversion parameters are specified in Flags. \r
103 The Flags bit LEFT_JUSTIFY is always ignored.\r
104 All conversions are left justified in Buffer.\r
105 If Width is 0, PREFIX_ZERO is ignored in Flags.\r
106 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas\r
107 are inserted every 3rd digit starting from the right.\r
108 If Value is < 0, then the fist character in Buffer is a '-'.\r
109 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored, \r
110 then Buffer is padded with '0' characters so the combination of the optional '-' \r
111 sign character, '0' characters, digit characters for Value, and the Null-terminator\r
112 add up to Width characters.\r
113\r
114 If Buffer is NULL, then ASSERT().\r
115 If unsupported bits are set in Flags, then ASSERT().\r
116 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()\r
117\r
118 @param Buffer Pointer to the output buffer for the produced Null-terminated\r
119 string.\r
120 @param Flags The bitmask of flags that specify left justification, zero pad,\r
121 and commas.\r
122 @param Value The 64-bit signed value to convert to a string.\r
4f7f1f5f 123 @param Width The maximum number of characters to place in Buffer, not including\r
124 the Null-terminator.\r
3f9f540d 125 @param Increment Character increment in Buffer.\r
126 \r
add13dc2 127 @return The number of characters in Buffer not including the Null-terminator.\r
3f9f540d 128\r
129**/\r
878ddf1f 130UINTN\r
131BasePrintLibConvertValueToString (\r
132 IN OUT CHAR8 *Buffer,\r
133 IN UINTN Flags,\r
134 IN INT64 Value,\r
135 IN UINTN Width,\r
136 IN UINTN Increment\r
137 )\r
138{\r
139 CHAR8 *OriginalBuffer;\r
4f7f1f5f 140 CHAR8 *EndBuffer;\r
878ddf1f 141 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];\r
142 UINTN Count;\r
143 UINTN Digits;\r
144 UINTN Index;\r
145\r
8960cdeb 146 ASSERT (Buffer != NULL);\r
147 ASSERT (Width < MAXIMUM_VALUE_CHARACTERS);\r
148 //\r
149 // Make sure Flags can only contain supported bits.\r
150 //\r
151 ASSERT ((Flags & ~(LEFT_JUSTIFY | COMMA_TYPE | PREFIX_ZERO)) == 0);\r
152\r
878ddf1f 153 OriginalBuffer = Buffer;\r
154\r
155 if (Width == 0 || (Flags & COMMA_TYPE) != 0) {\r
156 Flags &= (~PREFIX_ZERO);\r
157 }\r
158\r
8960cdeb 159 if (Width == 0) {\r
878ddf1f 160 Width = MAXIMUM_VALUE_CHARACTERS - 1;\r
161 }\r
4f7f1f5f 162 //\r
163 // Set the tag for the end of the input Buffer.\r
164 //\r
165 EndBuffer = Buffer + Width * Increment;\r
878ddf1f 166\r
167 if (Value < 0) {\r
168 Value = -Value;\r
4f7f1f5f 169 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, '-', Increment);\r
ab1096fd 170 Width--;\r
878ddf1f 171 }\r
172\r
173 Count = BasePrintLibValueToString (ValueBuffer, Value, 10);\r
174\r
175 if ((Flags & PREFIX_ZERO) != 0) {\r
4f7f1f5f 176 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Count, '0', Increment);\r
878ddf1f 177 }\r
178\r
ab1096fd 179 Digits = Count % 3;\r
180 if (Digits != 0) {\r
181 Digits = 3 - Digits;\r
182 }\r
878ddf1f 183 for (Index = 0; Index < Count; Index++) {\r
4f7f1f5f 184 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ValueBuffer[Count - Index], Increment);\r
878ddf1f 185 if ((Flags & COMMA_TYPE) != 0) {\r
186 Digits++;\r
187 if (Digits == 3) {\r
188 Digits = 0;\r
189 if ((Index + 1) < Count) {\r
4f7f1f5f 190 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', Increment);\r
878ddf1f 191 }\r
192 }\r
193 }\r
194 }\r
195\r
d08b357f 196 BasePrintLibFillBuffer (Buffer, EndBuffer + Increment, 1, 0, Increment);\r
878ddf1f 197\r
198 return ((Buffer - OriginalBuffer) / Increment);\r
199}\r