]>
Commit | Line | Data |
---|---|---|
6c6c850a | 1 | /** @file\r |
2 | Utility functions for performing basic math operations constrained within a\r | |
3 | modulus.\r | |
4 | \r | |
5 | These functions are intended to simplify small changes to a value which much\r | |
6 | remain within a specified modulus.\r | |
7 | \r | |
8 | NOTE: Changes must be less than or equal to the modulus specified by MaxVal.\r | |
9 | \r | |
10 | Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>\r | |
11 | This program and the accompanying materials are licensed and made available\r | |
12 | under the terms and conditions of the BSD License which accompanies this\r | |
13 | distribution. The full text of the license may be found at\r | |
14 | http://opensource.org/licenses/bsd-license.php.\r | |
15 | \r | |
16 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
17 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
18 | **/\r | |
19 | #include <Uefi.h>\r | |
20 | #include <LibConfig.h>\r | |
21 | #include <assert.h>\r | |
22 | \r | |
23 | /** Counter = (Counter + 1) % MaxVal;\r | |
24 | \r | |
25 | Counter is always expected to be LESS THAN MaxVal.\r | |
26 | 0 <= Counter < MaxVal\r | |
27 | \r | |
28 | @param[in] Counter The value to be incremented.\r | |
29 | @param[in] MaxVal Modulus of the operation.\r | |
30 | \r | |
31 | @return Returns the result of incrementing Counter, modulus MaxVal.\r | |
32 | If Counter >= MaxVal, returns -1.\r | |
33 | **/\r | |
34 | INT32\r | |
35 | EFIAPI\r | |
36 | ModuloIncrement(\r | |
37 | UINT32 Counter,\r | |
38 | UINT32 MaxVal\r | |
39 | )\r | |
40 | {\r | |
41 | INT32 Temp;\r | |
42 | \r | |
43 | if(Counter < MaxVal) {\r | |
44 | Temp = (INT32)(Counter + 1);\r | |
45 | if(Temp >= (INT32)MaxVal) {\r | |
46 | Temp = 0;\r | |
47 | }\r | |
48 | }\r | |
49 | else {\r | |
50 | Temp = -1;\r | |
51 | }\r | |
52 | return Temp;\r | |
53 | }\r | |
54 | \r | |
55 | /** Counter = (Counter - 1) % MaxVal;\r | |
56 | \r | |
57 | Counter is always expected to be LESS THAN MaxVal.\r | |
58 | 0 <= Counter < MaxVal\r | |
59 | \r | |
60 | @param[in] Counter The value to be decremented.\r | |
61 | @param[in] MaxVal Modulus of the operation.\r | |
62 | \r | |
63 | @return Returns the result of decrementing Counter, modulus MaxVal.\r | |
64 | If Counter >= MaxVal, returns -1.\r | |
65 | **/\r | |
66 | INT32\r | |
67 | EFIAPI\r | |
68 | ModuloDecrement(\r | |
69 | UINT32 Counter,\r | |
70 | UINT32 MaxVal\r | |
71 | )\r | |
72 | {\r | |
73 | INT32 Temp;\r | |
74 | \r | |
75 | if(Counter < MaxVal) {\r | |
76 | Temp = (INT32)Counter - 1;\r | |
77 | // If Counter is zero, Temp will become -1.\r | |
78 | if(Temp < 0) {\r | |
79 | Temp = (INT32)MaxVal - 1;\r | |
80 | }\r | |
81 | }\r | |
82 | else {\r | |
83 | Temp = -1;\r | |
84 | }\r | |
85 | \r | |
86 | return Temp;\r | |
87 | }\r | |
88 | \r | |
89 | /** Decrement Counter but don't decrement past zero.\r | |
90 | \r | |
91 | @param[in] Counter The value to be decremented.\r | |
92 | \r | |
93 | @return Returns the result of decrementing Counter.\r | |
94 | **/\r | |
95 | UINT32\r | |
96 | EFIAPI\r | |
97 | BoundDecrement(\r | |
98 | UINT32 Counter\r | |
99 | )\r | |
100 | {\r | |
101 | return ((Counter > 0) ? (Counter - 1) : 0);\r | |
102 | }\r | |
103 | \r | |
104 | /** Increment Counter but don't increment past MaxVal.\r | |
105 | Counter should be maintained in the range (0 <= Counter < MaxVal).\r | |
106 | \r | |
107 | @param[in] Counter The value to be decremented.\r | |
108 | @param[in] MaxVal The upper bound for Counter.\r | |
109 | \r | |
110 | @return Returns the result of incrementing Counter.\r | |
111 | **/\r | |
112 | UINT32\r | |
113 | EFIAPI\r | |
114 | BoundIncrement(\r | |
115 | UINT32 Counter,\r | |
116 | UINT32 MaxVal\r | |
117 | )\r | |
118 | {\r | |
119 | return ((Counter < (MaxVal - 1)) ? (Counter + 1) : (MaxVal - 1));\r | |
120 | }\r | |
121 | \r | |
122 | /** Counter = (Counter + Increment) % MaxVal;\r | |
123 | \r | |
124 | @param[in] Counter The value to be incremented.\r | |
125 | @param[in] Increment The value to add to Counter.\r | |
126 | @param[in] MaxVal Modulus of the operation.\r | |
127 | \r | |
128 | @return Returns the result of adding Increment to Counter, modulus MaxVal,\r | |
129 | or -1 if Increment is larger than MaxVal.\r | |
130 | **/\r | |
131 | INT32\r | |
132 | EFIAPI\r | |
133 | ModuloAdd (\r | |
134 | UINT32 Counter,\r | |
135 | UINT32 Increment,\r | |
136 | UINT32 MaxVal\r | |
137 | )\r | |
138 | {\r | |
139 | UINT32 Temp;\r | |
140 | \r | |
141 | if(Increment > MaxVal) {\r | |
142 | return -1;\r | |
143 | }\r | |
144 | Temp = (Counter + Increment);\r | |
145 | while(Temp >= MaxVal) {\r | |
146 | Temp -= MaxVal;\r | |
147 | }\r | |
148 | return Temp;\r | |
149 | }\r |