+++ /dev/null
-/** @file\r
- Utility functions for performing basic math operations constrained within a\r
- modulus.\r
-\r
- These functions are intended to simplify small changes to a value which much\r
- remain within a specified modulus.\r
-\r
- NOTE: Changes must be less than or equal to the modulus specified by MaxVal.\r
-\r
- Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>\r
- This program and the accompanying materials are licensed and made available\r
- under the terms and conditions of the BSD License which accompanies this\r
- distribution. The full text of the license may be found at\r
- http://opensource.org/licenses/bsd-license.php.\r
-\r
- THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-**/\r
-#include <Uefi.h>\r
-#include <LibConfig.h>\r
-#include <assert.h>\r
-\r
-/** Counter = (Counter + 1) % MaxVal;\r
-\r
- Counter is always expected to be LESS THAN MaxVal.\r
- 0 <= Counter < MaxVal\r
-\r
- @param[in] Counter The value to be incremented.\r
- @param[in] MaxVal Modulus of the operation.\r
-\r
- @return Returns the result of incrementing Counter, modulus MaxVal.\r
- If Counter >= MaxVal, returns -1.\r
-**/\r
-INT32\r
-EFIAPI\r
-ModuloIncrement(\r
- UINT32 Counter,\r
- UINT32 MaxVal\r
- )\r
-{\r
- INT32 Temp;\r
-\r
- if(Counter < MaxVal) {\r
- Temp = (INT32)(Counter + 1);\r
- if(Temp >= (INT32)MaxVal) {\r
- Temp = 0;\r
- }\r
- }\r
- else {\r
- Temp = -1;\r
- }\r
- return Temp;\r
-}\r
-\r
-/** Counter = (Counter - 1) % MaxVal;\r
-\r
- Counter is always expected to be LESS THAN MaxVal.\r
- 0 <= Counter < MaxVal\r
-\r
- @param[in] Counter The value to be decremented.\r
- @param[in] MaxVal Modulus of the operation.\r
-\r
- @return Returns the result of decrementing Counter, modulus MaxVal.\r
- If Counter >= MaxVal, returns -1.\r
-**/\r
-INT32\r
-EFIAPI\r
-ModuloDecrement(\r
- UINT32 Counter,\r
- UINT32 MaxVal\r
- )\r
-{\r
- INT32 Temp;\r
-\r
- if(Counter < MaxVal) {\r
- Temp = (INT32)Counter - 1;\r
- // If Counter is zero, Temp will become -1.\r
- if(Temp < 0) {\r
- Temp = (INT32)MaxVal - 1;\r
- }\r
- }\r
- else {\r
- Temp = -1;\r
- }\r
-\r
- return Temp;\r
-}\r
-\r
-/** Decrement Counter but don't decrement past zero.\r
-\r
- @param[in] Counter The value to be decremented.\r
-\r
- @return Returns the result of decrementing Counter.\r
-**/\r
-UINT32\r
-EFIAPI\r
-BoundDecrement(\r
- UINT32 Counter\r
- )\r
-{\r
- return ((Counter > 0) ? (Counter - 1) : 0);\r
-}\r
-\r
-/** Increment Counter but don't increment past MaxVal.\r
- Counter should be maintained in the range (0 <= Counter < MaxVal).\r
-\r
- @param[in] Counter The value to be decremented.\r
- @param[in] MaxVal The upper bound for Counter.\r
-\r
- @return Returns the result of incrementing Counter.\r
-**/\r
-UINT32\r
-EFIAPI\r
-BoundIncrement(\r
- UINT32 Counter,\r
- UINT32 MaxVal\r
- )\r
-{\r
- return ((Counter < (MaxVal - 1)) ? (Counter + 1) : (MaxVal - 1));\r
-}\r
-\r
-/** Counter = (Counter + Increment) % MaxVal;\r
-\r
- @param[in] Counter The value to be incremented.\r
- @param[in] Increment The value to add to Counter.\r
- @param[in] MaxVal Modulus of the operation.\r
-\r
- @return Returns the result of adding Increment to Counter, modulus MaxVal,\r
- or -1 if Increment is larger than MaxVal.\r
-**/\r
-INT32\r
-EFIAPI\r
-ModuloAdd (\r
- UINT32 Counter,\r
- UINT32 Increment,\r
- UINT32 MaxVal\r
- )\r
-{\r
- UINT32 Temp;\r
-\r
- if(Increment > MaxVal) {\r
- return -1;\r
- }\r
- Temp = (Counter + Increment);\r
- while(Temp >= MaxVal) {\r
- Temp -= MaxVal;\r
- }\r
- return Temp;\r
-}\r