math.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copright (C) 2014 Linaro Ltd.
  3. * Author: Ard Biesheuvel <[email protected]>
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice and this list of conditions, without modification.
  10. * 2. The name of the author may not be used to endorse or promote products
  11. * derived from this software without specific prior written permission.
  12. *
  13. * Alternatively, this software may be distributed under the terms of the
  14. * GNU General Public License as published by the Free Software Foundation;
  15. * either version 2 of the License, or (at your option) any later version.
  16. */
  17. #include "lib.h"
  18. UINT64
  19. LShiftU64 (
  20. IN UINT64 Operand,
  21. IN UINTN Count
  22. )
  23. // Left shift 64bit by 32bit and get a 64bit result
  24. {
  25. return Operand << Count;
  26. }
  27. UINT64
  28. RShiftU64 (
  29. IN UINT64 Operand,
  30. IN UINTN Count
  31. )
  32. // Right shift 64bit by 32bit and get a 64bit result
  33. {
  34. return Operand >> Count;
  35. }
  36. UINT64
  37. MultU64x32 (
  38. IN UINT64 Multiplicand,
  39. IN UINTN Multiplier
  40. )
  41. // Multiply 64bit by 32bit and get a 64bit result
  42. {
  43. return Multiplicand * Multiplier;
  44. }
  45. UINT64
  46. DivU64x32 (
  47. IN UINT64 Dividend,
  48. IN UINTN Divisor,
  49. OUT UINTN *Remainder OPTIONAL
  50. )
  51. {
  52. /*
  53. * GCC turns a division into a multiplication and shift with precalculated
  54. * constants if the divisor is constant and the dividend fits into a 32 bit
  55. * variable. Otherwise, it will turn this into calls into the 32-bit div
  56. * library functions.
  57. */
  58. if (Remainder)
  59. *Remainder = Dividend % Divisor;
  60. return Dividend / Divisor;
  61. }