Guest User

Untitled

a guest
Apr 19th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #ifndef HUGEVAL_HPP
  2. #define HUGEVAL_HPP
  3. #include <string>
  4. #include <algorithm>
  5. #include <cstdio>
  6. #include <assert.h>
  7. #include "x86_instr.h"
  8.  
  9. namespace math
  10. {
  11. template<int words> class HugeVal
  12. {
  13. public:
  14. explicit HugeVal(){ std::fill(data, data + words, 0); }
  15. explicit HugeVal(const std::string& str)
  16. {
  17.  
  18. }
  19. explicit HugeVal(ui32 val)
  20. {
  21. std::fill(data, data + words, 0);
  22. data[0] = val;
  23. }
  24.  
  25. /* pretty-print */
  26. void print() const
  27. {
  28. for(int i=(words-1); i>=0; --i)
  29. printf("%.8X ", data[i]);
  30. }
  31. HugeVal operator+(const HugeVal<words>& val) const
  32. {
  33. HugeVal<words> tmp;
  34. ui32 carry = 0;
  35. ui32 op = 0;
  36. for(int i=0; i<words; ++i){
  37. if(!(carry)){
  38. tmp.data[i] = data[i] + val.data[i];
  39. eflags32(carry);
  40. carry &= EFLAG_CARRY;
  41. } else {
  42. if(val.data[i] < 0xFFFFFFFF){
  43. op = val.data[i] + 1UL;
  44. tmp.data[i] = data[i] + op;
  45. eflags32(carry);
  46. carry &= EFLAG_CARRY;
  47. } else {
  48. carry = EFLAG_CARRY;
  49. }
  50. }
  51. }
  52. return tmp;
  53. }
  54. HugeVal operator-(const HugeVal<words>& val) const
  55. {
  56. HugeVal<words> tmp;
  57. ui32 flag = 0;
  58. ui32 op = 0;
  59. for(int i=0; i<words; ++i){
  60. if(!(flag & EFLAG_CARRY))
  61. op = val.data[i];
  62. else
  63. op = val.data[i] + 1;
  64.  
  65. tmp.data[i] = data[i] - op;
  66. eflags32(flag);
  67. }
  68. return tmp;
  69. }
  70. HugeVal operator*(const HugeVal<words>& val) const
  71. {
  72. HugeVal<words> tmp;
  73. ui32 tmp2;
  74. ui32 carry = 0;
  75. for(int j=0;j<words; ++j){ //val data (right)
  76. for(int i=0;i<words; ++i){ //data (left)
  77. ui32 pos = j+i;
  78. unsigned long long result = (unsigned long long)val.data[j] * data[i];
  79. ui32 high = (result >> 0x20);
  80. ui32 low = (result & 0xFFFFFFFF);
  81. tmp.data[pos] += low;
  82. if(carry)
  83. tmp.data[pos] += 1;
  84. eflags32(carry);
  85. carry &= EFLAG_CARRY;
  86. if(pos<(words-2)){
  87. tmp.data[pos+1] += high;
  88. if(carry){
  89. tmp.data[pos+1] += 1;
  90. eflags32(carry);
  91. carry &= EFLAG_CARRY;
  92. }
  93. }
  94. else
  95. carry = 0;
  96. }
  97. }
  98. return tmp;
  99. }
  100. private:
  101. ui32 data[words];
  102. };
  103. }
  104. #endif
Add Comment
Please, Sign In to add comment