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. int32 m_cluMax;
  17. int32 m_clu;
  18. uint32 *m_prglu;
  19. uint32 m_rgluInit[kcluMaxInit];
  20. inline BigInt & operator= (BigInt &bi);
  21. bool FResize(int32 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(uint32 *prglu, int32 clu);
  34. bool FInitFromBigint(BigInt *pbiSrc);
  35. template <typename EncodedChar>
  36. bool FInitFromDigits(const EncodedChar *prgch, int32 cch, int32 *pcchDec);
  37. bool FMulAdd(uint32 luMul, uint32 luAdd);
  38. bool FMulPow5(int32 c5);
  39. bool FShiftLeft(int32 cbit);
  40. void ShiftLusRight(int32 clu);
  41. void ShiftRight(int32 cbit);
  42. int Compare(BigInt *pbi);
  43. bool FAdd(BigInt *pbi);
  44. void Subtract(BigInt *pbi);
  45. int DivRem(BigInt *pbi);
  46. int32 Clu(void);
  47. uint32 Lu(int32 ilu);
  48. double GetDbl(void);
  49. };
  50. }