BigInt.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. namespace Js
  7. {
  8. /***************************************************************************
  9. Big non-negative integer class.
  10. ***************************************************************************/
  11. class BigInt
  12. {
  13. private:
  14. // Make this big enough that we rarely have to call malloc.
  15. enum { kcluMaxInit = 30 };
  16. long m_cluMax;
  17. long m_clu;
  18. ulong *m_prglu;
  19. ulong m_rgluInit[kcluMaxInit];
  20. inline BigInt & operator= (BigInt &bi);
  21. bool FResize(long clu);
  22. #if DBG
  23. #define AssertBi(pbi) Assert(pbi); (pbi)->AssertValid(true);
  24. #define AssertBiNoVal(pbi) Assert(pbi); (pbi)->AssertValid(false);
  25. inline void AssertValid(bool fCheckVal);
  26. #else //!DBG
  27. #define AssertBi(pbi)
  28. #define AssertBiNoVal(pbi)
  29. #endif //!DBG
  30. public:
  31. BigInt(void);
  32. ~BigInt(void);
  33. bool FInitFromRglu(ulong *prglu, long clu);
  34. bool FInitFromBigint(BigInt *pbiSrc);
  35. template <typename EncodedChar>
  36. bool FInitFromDigits(const EncodedChar *prgch, long cch, long *pcchDec);
  37. bool FMulAdd(ulong luMul, ulong luAdd);
  38. bool FMulPow5(long c5);
  39. bool FShiftLeft(long cbit);
  40. void ShiftLusRight(long clu);
  41. void ShiftRight(long cbit);
  42. int Compare(BigInt *pbi);
  43. bool FAdd(BigInt *pbi);
  44. void Subtract(BigInt *pbi);
  45. int DivRem(BigInt *pbi);
  46. long Clu(void);
  47. ulong Lu(long ilu);
  48. double GetDbl(void);
  49. };
  50. }