math.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. // Multiple 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. // divide 64bit by 32bit and get a 64bit result
  52. // N.B. only works for 31bit divisors!!
  53. {
  54. if (Remainder)
  55. *Remainder = Dividend % Divisor;
  56. return Dividend / Divisor;
  57. }