Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef HUGEVAL_HPP
- #define HUGEVAL_HPP
- #include <string>
- #include <algorithm>
- #include <cstdio>
- #include <assert.h>
- #include "x86_instr.h"
- namespace math
- {
- template<int words> class HugeVal
- {
- public:
- explicit HugeVal(){ std::fill(data, data + words, 0); }
- explicit HugeVal(const std::string& str)
- {
- }
- explicit HugeVal(ui32 val)
- {
- std::fill(data, data + words, 0);
- data[0] = val;
- }
- /* pretty-print */
- void print() const
- {
- for(int i=(words-1); i>=0; --i)
- printf("%.8X ", data[i]);
- }
- HugeVal operator+(const HugeVal<words>& val) const
- {
- HugeVal<words> tmp;
- ui32 carry = 0;
- ui32 op = 0;
- for(int i=0; i<words; ++i){
- if(!(carry)){
- tmp.data[i] = data[i] + val.data[i];
- eflags32(carry);
- carry &= EFLAG_CARRY;
- } else {
- if(val.data[i] < 0xFFFFFFFF){
- op = val.data[i] + 1UL;
- tmp.data[i] = data[i] + op;
- eflags32(carry);
- carry &= EFLAG_CARRY;
- } else {
- carry = EFLAG_CARRY;
- }
- }
- }
- return tmp;
- }
- HugeVal operator-(const HugeVal<words>& val) const
- {
- HugeVal<words> tmp;
- ui32 flag = 0;
- ui32 op = 0;
- for(int i=0; i<words; ++i){
- if(!(flag & EFLAG_CARRY))
- op = val.data[i];
- else
- op = val.data[i] + 1;
- tmp.data[i] = data[i] - op;
- eflags32(flag);
- }
- return tmp;
- }
- HugeVal operator*(const HugeVal<words>& val) const
- {
- HugeVal<words> tmp;
- ui32 tmp2;
- ui32 carry = 0;
- for(int j=0;j<words; ++j){ //val data (right)
- for(int i=0;i<words; ++i){ //data (left)
- ui32 pos = j+i;
- unsigned long long result = (unsigned long long)val.data[j] * data[i];
- ui32 high = (result >> 0x20);
- ui32 low = (result & 0xFFFFFFFF);
- tmp.data[pos] += low;
- if(carry)
- tmp.data[pos] += 1;
- eflags32(carry);
- carry &= EFLAG_CARRY;
- if(pos<(words-2)){
- tmp.data[pos+1] += high;
- if(carry){
- tmp.data[pos+1] += 1;
- eflags32(carry);
- carry &= EFLAG_CARRY;
- }
- }
- else
- carry = 0;
- }
- }
- return tmp;
- }
- private:
- ui32 data[words];
- };
- }
- #endif
Add Comment
Please, Sign In to add comment