Changeset 1029


Ignore:
Timestamp:
Oct 20, 2011, 2:38:49 AM (9 years ago)
Author:
sam
Message:

core: dynamically allocate mantissa for real numbers. One day we'll be able
to modify their precision at runtime.

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/real.cpp

    r1026 r1029  
    2323{
    2424
    25 real::real(float f) { *this = (double)f; }
    26 real::real(int i) { *this = (double)i; }
    27 real::real(unsigned int i) { *this = (double)i; }
     25real::real()
     26{
     27    m_mantissa = new uint32_t[BIGITS];
     28    m_signexp = 0;
     29}
     30
     31real::real(real const &x)
     32{
     33    m_mantissa = new uint32_t[BIGITS];
     34    memcpy(m_mantissa, x.m_mantissa, BIGITS * sizeof(uint32_t));
     35    m_signexp = x.m_signexp;
     36}
     37
     38real const &real::operator =(real const &x)
     39{
     40    if (&x != this)
     41    {
     42        memcpy(m_mantissa, x.m_mantissa, BIGITS * sizeof(uint32_t));
     43        m_signexp = x.m_signexp;
     44    }
     45
     46    return *this;
     47}
     48
     49real::~real()
     50{
     51    delete[] m_mantissa;
     52}
     53
     54real::real(float f) { new(this) real((double)f); }
     55real::real(int i) { new(this) real((double)i); }
     56real::real(unsigned int i) { new(this) real((double)i); }
    2857
    2958real::real(double d)
    3059{
     60    new(this) real();
     61
    3162    union { double d; uint64_t x; } u = { d };
    3263
     
    199230    for (int i = 0; i < bigoff; i++)
    200231    {
    201         carry -= x.m_mantissa[BIGITS - i];
     232        carry -= x.m_mantissa[BIGITS - 1 - i];
    202233        /* Emulates a signed shift */
    203234        carry >>= BIGIT_BITS;
    204235        carry |= carry << BIGIT_BITS;
    205236    }
    206     carry -= x.m_mantissa[BIGITS - 1 - bigoff] & (((int64_t)1 << off) - 1);
     237    if (bigoff < BIGITS)
     238        carry -= x.m_mantissa[BIGITS - 1 - bigoff] & (((int64_t)1 << off) - 1);
    207239    carry /= (int64_t)1 << off;
    208240
     
    345377}
    346378
    347 real &real::operator +=(real const &x)
     379real const &real::operator +=(real const &x)
    348380{
    349381    real tmp = *this;
     
    351383}
    352384
    353 real &real::operator -=(real const &x)
     385real const &real::operator -=(real const &x)
    354386{
    355387    real tmp = *this;
     
    357389}
    358390
    359 real &real::operator *=(real const &x)
     391real const &real::operator *=(real const &x)
    360392{
    361393    real tmp = *this;
     
    363395}
    364396
    365 real &real::operator /=(real const &x)
     397real const &real::operator /=(real const &x)
    366398{
    367399    real tmp = *this;
     
    381413}
    382414
    383 real &real::operator <<=(int x)
     415real const &real::operator <<=(int x)
    384416{
    385417    if (m_signexp << 1)
     
    388420}
    389421
    390 real &real::operator >>=(int x)
     422real const &real::operator >>=(int x)
    391423{
    392424    if (m_signexp << 1)
     
    403435        return false;
    404436
    405     return memcmp(m_mantissa, x.m_mantissa, sizeof(m_mantissa)) == 0;
     437    return memcmp(m_mantissa, x.m_mantissa, BIGITS * sizeof(uint32_t)) == 0;
    406438}
    407439
  • trunk/src/real.h

    r1026 r1029  
    2525{
    2626public:
    27     inline real() { }
     27    real();
     28    real(real const &x);
     29    real const &operator =(real const &x);
     30    ~real();
    2831
    2932    real(float f);
     
    4346    real operator *(real const &x) const;
    4447    real operator /(real const &x) const;
    45     real &operator +=(real const &x);
    46     real &operator -=(real const &x);
    47     real &operator *=(real const &x);
    48     real &operator /=(real const &x);
     48    real const &operator +=(real const &x);
     49    real const &operator -=(real const &x);
     50    real const &operator *=(real const &x);
     51    real const &operator /=(real const &x);
    4952
    5053    real operator <<(int x) const;
    5154    real operator >>(int x) const;
    52     real &operator <<=(int x);
    53     real &operator >>=(int x);
     55    real const &operator <<=(int x);
     56    real const &operator >>=(int x);
    5457
    5558    bool operator ==(real const &x) const;
     
    134137
    135138private:
    136     uint32_t m_size;
     139    uint32_t *m_mantissa;
    137140    uint32_t m_signexp;
    138     uint32_t m_mantissa[BIGITS];
    139141};
    140142
Note: See TracChangeset for help on using the changeset viewer.