/* * Modulo Operator */ word BigInt::operator%=(word mod) { if(mod == 0) throw BigInt::DivideByZero(); if(is_power_of_2(mod)) { word result = (word_at(0) & (mod - 1)); clear(); grow_to(2); m_reg[0] = result; return result; } word remainder = 0; for(size_t j = sig_words(); j > 0; --j) remainder = bigint_modop(remainder, word_at(j-1), mod); clear(); grow_to(2); if(remainder && sign() == BigInt::Negative) m_reg[0] = mod - remainder; else m_reg[0] = remainder; set_sign(BigInt::Positive); return word_at(0); }
/************************************************* * Set bit number n * *************************************************/ void BigInt::set_bit(u32bit n) { const u32bit which = n / MP_WORD_BITS; const word mask = (word)1 << (n % MP_WORD_BITS); if(which >= size()) grow_to(which + 1); reg[which] |= mask; }
/* * Left Shift Operator */ BigInt& BigInt::operator<<=(size_t shift) { if(shift) { const size_t shift_words = shift / MP_WORD_BITS, shift_bits = shift % MP_WORD_BITS, words = sig_words(); grow_to(words + shift_words + (shift_bits ? 1 : 0)); bigint_shl1(mutable_data(), words, shift_words, shift_bits); } return (*this); }
bool arena::expand( size_t bytes ) { MM_ASSERT( is_valid() && "Arena : Im sick!" ); // increase head_ byte* new_head = head_ + bytes; if ( (new_head > end_) && !grow_to( new_head ) ) { // grow error! MM_ASSERT( 0 == "Arena : Grow failed!" ); return false; } head_ = new_head; return true; }
void* arena::alloc( size_t bytes, size_t alignment /*= DefaultAlignment*/ ) { MM_ASSERT( is_valid() && "Arena : Im sick!" ); // align head_ ptr according to the 'alignment' byte* start = AlignUp( head_, alignment ); // new head_ ptr is byte* new_head = start + bytes; if ( (new_head > end_) && !grow_to( new_head ) ) { // grow failed MM_ASSERT( 0 == "Arena : Grow failed!" ); return NULL; } head_ = new_head; return (void*)start; }
bool arena::init( size_t capacity, size_t initial ) { // init is already called MM_ASSERT( begin_ == NULL && "Arena : Trying to initialize second time!" ); // initialize region size_t bytesReserve = AlignUp( capacity, VASPageSize ); begin_ = (byte*)VirtualAlloc( NULL, bytesReserve, MEM_RESERVE, PAGE_NOACCESS ); // fails for unknown reason ;) MM_ASSERT( begin_ != NULL && "Arena : Reservation failed!" ); if ( !begin_ ) return false; // this is our reservation bound limit_ = begin_ + bytesReserve; end_ = begin_; head_ = begin_; // preallocate initial region return grow_to( begin_ + initial ); }
void cell::grow_by(int cx, int cy) { grow_to(x2 + cx, y2 + cy); }