+++ /dev/null
-/*++\r
-\r
-Copyright (c) 2006, Intel Corporation.\r
-All rights reserved. This program and the accompanying materials \r
-are licensed and made available under the terms and conditions of the BSD License \r
-which accompanies this 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
-Module Name:\r
-\r
- x64math.c\r
-\r
-Abstract:\r
-\r
- Math routines for x64.\r
-\r
---*/\r
-\r
-UINT64\r
-LeftShiftU64 (\r
- IN UINT64 Operand,\r
- IN UINT64 Count\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- \r
- Left-shift a 64 bit value.\r
-\r
-Arguments:\r
-\r
- Operand - 64-bit value to shift\r
- Count - shift count\r
-\r
-Returns:\r
-\r
- Operand << Count\r
-\r
---*/\r
-{\r
- if (Count > 63) {\r
- return 0;\r
- }\r
-\r
- return Operand << Count;\r
-}\r
-\r
-UINT64\r
-RightShiftU64 (\r
- IN UINT64 Operand,\r
- IN UINT64 Count\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- \r
- Right-shift a 64 bit value.\r
-\r
-Arguments:\r
-\r
- Operand - 64-bit value to shift\r
- Count - shift count\r
-\r
-Returns:\r
-\r
- Operand >> Count\r
-\r
---*/\r
-{\r
- if (Count > 63) {\r
- return 0;\r
- }\r
-\r
- return Operand >> Count;\r
-}\r
-\r
-INT64\r
-ARightShift64 (\r
- IN INT64 Operand,\r
- IN UINT64 Count\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- \r
- Right-shift a 64 bit signed value.\r
-\r
-Arguments:\r
-\r
- Operand - 64-bit value to shift\r
- Count - shift count\r
-\r
-Returns:\r
-\r
- Operand >> Count\r
-\r
---*/\r
-{\r
- if (Count > 63) {\r
-\r
- if (Operand & 0x8000000000000000ULL) {\r
- return (INT64)~0;\r
- }\r
-\r
- return 0;\r
- }\r
-\r
- return Operand >> Count;\r
-}\r
-\r
-#if 0\r
-//\r
-// The compiler generates true assembly for these, so we don't need them.\r
-//\r
-INT32\r
-ARightShift32 (\r
- IN INT32 Operand,\r
- IN UINTN Count\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- \r
- Right shift a 32-bit value\r
-\r
-Arguments:\r
-\r
- Operand - value to shift\r
- Count - shift count\r
-\r
-Returns:\r
-\r
- Operand >> Count\r
-\r
---*/\r
-{\r
- return Operand >> (Count & 0x1f);\r
-}\r
-\r
-INT32\r
-MulS32x32 (\r
- INT32 Value1,\r
- INT32 Value2,\r
- INT32 *ResultHigh\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- \r
- Multiply two signed 32-bit numbers.\r
-\r
-Arguments:\r
-\r
- Value1 - first value to multiply\r
- Value2 - value to multiply Value1 by\r
- ResultHigh - overflow\r
-\r
-Returns:\r
-\r
- Value1 * Value2\r
-\r
-Notes:\r
-\r
- The 64-bit result is the concatenation of *ResultHigh and the return value\r
-\r
- The product fits in 32 bits if\r
- (*ResultHigh == 0x00000000 AND *ResultLow_bit31 == 0)\r
- OR\r
- (*ResultHigh == 0xffffffff AND *ResultLow_bit31 == 1)\r
-\r
---*/\r
-{\r
- INT64 Rres64;\r
- INT32 Result;\r
-\r
- Res64 = (INT64) Value1 * (INT64) Value2;\r
- *ResultHigh = (Res64 >> 32) & 0xffffffff;\r
- Result = Res64 & 0xffffffff;\r
- return Result;\r
-}\r
-\r
-UINT32\r
-MulU32x32 (\r
- UINT32 Value1,\r
- UINT32 Value2,\r
- UINT32 *ResultHigh\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- \r
- Multiply two unsigned 32-bit values.\r
-\r
-Arguments:\r
-\r
- Value1 - first number\r
- Value2 - number to multiply by Value1 \r
- ResultHigh - overflow\r
-\r
-Returns:\r
-\r
- Value1 * Value2\r
-\r
-Notes:\r
-\r
- The 64-bit result is the concatenation of *ResultHigh and the return value.\r
- The product fits in 32 bits if *ResultHigh == 0x00000000\r
-\r
---*/\r
-{\r
- UINT64 Res64;\r
- UINT32 Result;\r
-\r
- Res64 = (INT64) Value1 * (INT64) Value2;\r
- *ResultHigh = (Res64 >> 32) & 0xffffffff;\r
- Result = Res64 & 0xffffffff;\r
- return Result;\r
-}\r
-\r
-INT32\r
-DivS32x32 (\r
- INT32 Value1,\r
- INT32 Value2,\r
- INT32 *Remainder,\r
- UINTN *error\r
- )\r
-//\r
-// signed 32-bit by signed 32-bit divide; the 32-bit remainder is\r
-// in *Remainder and the quotient is the return value; *error = 1 if the\r
-// divisor is 0, and it is 1 otherwise\r
-//\r
-{\r
- INT32 Result;\r
-\r
- *error = 0;\r
-\r
- if (Value2 == 0x0) {\r
- *error = 1;\r
- Result = 0x80000000;\r
- *Remainder = 0x80000000;\r
- } else {\r
- Result = Value1 / Value2;\r
- *Remainder = Value1 - Result * Value2;\r
- }\r
-\r
- return Result;\r
-}\r
-\r
-UINT32\r
-DivU32x32 (\r
- UINT32 Value1,\r
- UINT32 Value2,\r
- UINT32 *Remainder,\r
- UINTN *Error\r
- )\r
-//\r
-// unsigned 32-bit by unsigned 32-bit divide; the 32-bit remainder is\r
-// in *Remainder and the quotient is the return value; *error = 1 if the\r
-// divisor is 0, and it is 1 otherwise\r
-//\r
-{\r
- UINT32 Result;\r
-\r
- *Error = 0;\r
-\r
- if (Value2 == 0x0) {\r
- *Error = 1;\r
- Result = 0x80000000;\r
- *Remainder = 0x80000000;\r
- } else {\r
- Result = Value1 / Value2;\r
- *Remainder = Value1 - Result * Value2;\r
- }\r
-\r
- return Result;\r
-}\r
-\r
-#endif\r
-\r
-INT64\r
-MulS64x64 (\r
- INT64 Value1,\r
- INT64 Value2,\r
- INT64 *ResultHigh\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- \r
- Multiply two signed 32-bit numbers.\r
-\r
-Arguments:\r
-\r
- Value1 - first value to multiply\r
- Value2 - value to multiply Value1 by\r
- ResultHigh - overflow\r
-\r
-Returns:\r
-\r
- Value1 * Value2\r
-\r
-Notes:\r
-\r
- The 64-bit result is the concatenation of *ResultHigh and the return value\r
-\r
- The product fits in 32 bits if\r
- (*ResultHigh == 0x00000000 AND *ResultLow_bit31 == 0)\r
- OR\r
- (*ResultHigh == 0xffffffff AND *ResultLow_bit31 == 1)\r
-\r
---*/\r
-{\r
- INT64 Result;\r
- \r
- Result = Value1 * Value2;\r
-\r
- return Result;\r
-}\r
-\r
-UINT64\r
-MulU64x64 (\r
- UINT64 Value1,\r
- UINT64 Value2,\r
- UINT64 *ResultHigh\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- \r
- Multiply two unsigned 32-bit values.\r
-\r
-Arguments:\r
-\r
- Value1 - first number\r
- Value2 - number to multiply by Value1 \r
- ResultHigh - overflow\r
-\r
-Returns:\r
-\r
- Value1 * Value2\r
-\r
-Notes:\r
-\r
- The 64-bit result is the concatenation of *ResultHigh and the return value.\r
- The product fits in 32 bits if *ResultHigh == 0x00000000\r
-\r
---*/\r
-{\r
- UINT64 Result;\r
-\r
- Result = Value1 * Value2;\r
-\r
- return Result;\r
-}\r
-\r
-INT64\r
-DivS64x64 (\r
- INT64 Value1,\r
- INT64 Value2,\r
- INT64 *Remainder,\r
- UINTN *Error\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- \r
- Divide two 64-bit signed values.\r
-\r
-Arguments:\r
-\r
- Value1 - dividend\r
- Value2 - divisor\r
- Remainder - remainder of Value1/Value2\r
- Error - to flag errors (divide-by-0)\r
-\r
-Returns:\r
-\r
- Value1 / Valu2\r
-\r
-Note:\r
-\r
- The 64-bit remainder is in *Remainder and the quotient is the return value.\r
- *Error = 1 if the divisor is 0, and it is 1 otherwise\r
-\r
---*/\r
-{\r
- INT64 Result;\r
-\r
- *Error = 0;\r
-\r
- if (Value2 == 0x0) {\r
- *Error = 1;\r
- Result = 0x8000000000000000;\r
- *Remainder = 0x8000000000000000;\r
- } else {\r
- Result = Value1 / Value2;\r
- *Remainder = Value1 - Result * Value2;\r
- }\r
-\r
- return Result;\r
-}\r
-\r
-UINT64\r
-DivU64x64 (\r
- UINT64 Value1,\r
- UINT64 Value2,\r
- UINT64 *Remainder,\r
- UINTN *Error\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- \r
- Divide two 64-bit unsigned values.\r
-\r
-Arguments:\r
-\r
- Value1 - dividend\r
- Value2 - divisor\r
- Remainder - remainder of Value1/Value2\r
- Error - to flag errors (divide-by-0)\r
-\r
-Returns:\r
-\r
- Value1 / Valu2\r
-\r
-Note:\r
-\r
- The 64-bit remainder is in *Remainder and the quotient is the return value.\r
- *Error = 1 if the divisor is 0, and it is 1 otherwise\r
-\r
---*/\r
-{\r
- UINT64 Result;\r
-\r
- *Error = 0;\r
-\r
- if (Value2 == 0x0) {\r
- *Error = 1;\r
- Result = 0x8000000000000000;\r
- *Remainder = 0x8000000000000000;\r
- } else {\r
- Result = Value1 / Value2;\r
- *Remainder = Value1 - Result * Value2;\r
- }\r
-\r
- return Result;\r
-}\r