]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Containers/Common/ModuloUtil.c
5f75698bd6634f35cefd4a190b5a326e2072b73a
2 Utility functions for performing basic math operations constrained within a
5 These functions are intended to simplify small changes to a value which much
6 remain within a specified modulus.
8 NOTE: Changes must be less than or equal to the modulus specified by MaxVal.
10 Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>
11 This program and the accompanying materials are licensed and made available
12 under the terms and conditions of the BSD License which accompanies this
13 distribution. The full text of the license may be found at
14 http://opensource.org/licenses/bsd-license.php.
16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 #include <LibConfig.h>
23 /** Counter = (Counter + 1) % MaxVal;
25 Counter is always expected to be LESS THAN MaxVal.
28 @param[in] Counter The value to be incremented.
29 @param[in] MaxVal Modulus of the operation.
31 @return Returns the result of incrementing Counter, modulus MaxVal.
32 If Counter >= MaxVal, returns -1.
43 if(Counter
< MaxVal
) {
44 Temp
= (INT32
)(Counter
+ 1);
45 if(Temp
>= (INT32
)MaxVal
) {
55 /** Counter = (Counter - 1) % MaxVal;
57 Counter is always expected to be LESS THAN MaxVal.
60 @param[in] Counter The value to be decremented.
61 @param[in] MaxVal Modulus of the operation.
63 @return Returns the result of decrementing Counter, modulus MaxVal.
64 If Counter >= MaxVal, returns -1.
75 if(Counter
< MaxVal
) {
76 Temp
= (INT32
)Counter
- 1;
77 // If Counter is zero, Temp will become -1.
79 Temp
= (INT32
)MaxVal
- 1;
89 /** Decrement Counter but don't decrement past zero.
91 @param[in] Counter The value to be decremented.
93 @return Returns the result of decrementing Counter.
101 return ((Counter
> 0) ? (Counter
- 1) : 0);
104 /** Increment Counter but don't increment past MaxVal.
105 Counter should be maintained in the range (0 <= Counter < MaxVal).
107 @param[in] Counter The value to be decremented.
108 @param[in] MaxVal The upper bound for Counter.
110 @return Returns the result of incrementing Counter.
119 return ((Counter
< (MaxVal
- 1)) ? (Counter
+ 1) : (MaxVal
- 1));
122 /** Counter = (Counter + Increment) % MaxVal;
124 @param[in] Counter The value to be incremented.
125 @param[in] Increment The value to add to Counter.
126 @param[in] MaxVal Modulus of the operation.
128 @return Returns the result of adding Increment to Counter, modulus MaxVal,
129 or -1 if Increment is larger than MaxVal.
141 if(Increment
> MaxVal
) {
144 Temp
= (Counter
+ Increment
);
145 while(Temp
>= MaxVal
) {