BigUInt.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 BigUInt
  12. {
  13. // Non-negative BigInt is stored as an array of 'digit' where each digit is unit32
  14. private:
  15. // Make this big enough that we rarely have to call malloc.
  16. enum { kcluMaxInit = 30 };// initilize 30 digits
  17. int32 m_cluMax; // current maximum length (or number of digits) it can contains
  18. int32 m_clu; // current length (or number of digits)
  19. uint32 *m_prglu; // pointer to array of digits
  20. uint32 m_rgluInit[kcluMaxInit]; // pre-defined space to store array
  21. inline BigUInt & operator= (BigUInt &bi);
  22. bool FResize(int32 clu);// allocate more space if length go over maximum
  23. #if DBG
  24. #define AssertBi(pbi) Assert(pbi); (pbi)->AssertValid(true);
  25. #define AssertBiNoVal(pbi) Assert(pbi); (pbi)->AssertValid(false);
  26. inline void AssertValid(bool fCheckVal);
  27. #else //!DBG
  28. #define AssertBi(pbi)
  29. #define AssertBiNoVal(pbi)
  30. #endif //!DBG
  31. public:
  32. BigUInt(void);
  33. ~BigUInt(void);
  34. bool FInitFromRglu(uint32 *prglu, int32 clu); // init from array and length
  35. bool FInitFromBigint(BigUInt *pbiSrc);
  36. template <typename EncodedChar>
  37. bool FInitFromDigits(const EncodedChar *prgch, int32 cch, int32 *pcchDec); // init from char of digits
  38. bool FMulAdd(uint32 luMul, uint32 luAdd);
  39. bool FMulPow5(int32 c5);
  40. bool FShiftLeft(int32 cbit);
  41. void ShiftLusRight(int32 clu);
  42. void ShiftRight(int32 cbit);
  43. int Compare(BigUInt *pbi);
  44. bool FAdd(BigUInt *pbi);
  45. void Subtract(BigUInt *pbi);
  46. int DivRem(BigUInt *pbi);
  47. int32 Clu(void); // return current length
  48. uint32 Lu(int32 ilu); // return digit at position ilu start from 0
  49. double GetDbl(void);
  50. };
  51. }