Browse Source

core: start working on the extended floating point addition.

legacy
Sam Hocevar sam 13 years ago
parent
commit
dbb0463f4c
3 changed files with 63 additions and 0 deletions
  1. +56
    -0
      src/real.cpp
  2. +2
    -0
      src/real.h
  3. +5
    -0
      test/unit/real.cpp

+ 56
- 0
src/real.cpp View File

@@ -47,6 +47,62 @@ real::operator float() const
return u.f;
}

real real::operator -()
{
m_signexp ^= 0x80000000u;
return *this;
}

real real::operator +(real const &x) const
{
if ((m_signexp << 1) < (x.m_signexp << 1))
return x + *this;

/* For now, assume both numbers are positive. */
real ret;

int e1 = (m_signexp & 0x7fffffffu) - (1 << 30) + 1;
int e2 = (x.m_signexp & 0x7fffffffu) - (1 << 30) + 1;

int bigoff = (e1 - e2) / (sizeof(uint16_t) * 8);
int off = e1 - e2 - bigoff * (sizeof(uint16_t) * 8);

ret.m_signexp = m_signexp;

uint32_t carry = 0;
for (int i = 0; i < BIGITS; i++)
{
carry = m_mantissa[BIGITS - 1 - i];
if (BIGITS - 1 - i - bigoff >= 0)
carry += x.m_mantissa[BIGITS - 1 - i - bigoff] >> off;
else if (BIGITS - 1 - i - bigoff == -1)
carry += 0x0001u >> off;

if (BIGITS - 1 - i - bigoff - 1 >= 0)
carry += (x.m_mantissa[BIGITS - 1 - i - bigoff - 1] << (16 - off)) & 0xffffu;
else if (BIGITS - 1 - i - bigoff - 1 == -1)
carry += 0x0001u << (16 - off);

ret.m_mantissa[BIGITS - 1 - i] = carry;
carry >>= 16;
}

/* Renormalise in case we overflowed the mantissa */
if (carry)
{
carry--;
for (int i = 0; i < BIGITS; i++)
{
uint16_t tmp = ret.m_mantissa[i];
ret.m_mantissa[i] = (carry << 15) | (tmp >> 1);
carry = tmp & 0x0001u;
}
ret.m_signexp++;
}

return ret;
}

real real::operator *(real const &x) const
{
real ret;


+ 2
- 0
src/real.h View File

@@ -30,6 +30,8 @@ public:
real(float f);

operator float() const;
real operator -();
real operator +(real const &x) const;
real operator *(real const &x) const;

void print() const;


+ 5
- 0
test/unit/real.cpp View File

@@ -34,6 +34,11 @@ public:
LOLUNIT_ASSERT_EQUAL(z, 1.5f);
}

LOLUNIT_TEST(test_real_add)
{
LOLUNIT_ASSERT(true);
}

LOLUNIT_TEST(test_real_mul)
{
real x(1.25f);


Loading…
Cancel
Save