Преглед на файлове

core: implement shift operators for reals; they're useful for fast

multiplications by integers, especially powers of two.
legacy
Sam Hocevar sam преди 13 години
родител
ревизия
4822f02d8d
променени са 3 файла, в които са добавени 59 реда и са изтрити 8 реда
  1. +27
    -3
      src/real.cpp
  2. +5
    -0
      src/real.h
  3. +27
    -5
      test/unit/real.cpp

+ 27
- 3
src/real.cpp Целия файл

@@ -350,6 +350,32 @@ real &real::operator /=(real const &x)
return *this = tmp / x;
}

real real::operator <<(int x) const
{
real tmp = *this;
return tmp <<= x;
}

real real::operator >>(int x) const
{
real tmp = *this;
return tmp >>= x;
}

real &real::operator <<=(int x)
{
if (m_signexp << 1)
m_signexp += x;
return *this;
}

real &real::operator >>=(int x)
{
if (m_signexp << 1)
m_signexp -= x;
return *this;
}

bool real::operator ==(real const &x) const
{
if ((m_signexp << 1) == 0 && (x.m_signexp << 1) == 0)
@@ -541,9 +567,7 @@ static real fastlog(real const &x)
zn *= z2;
}

/* FIXME: sum.m_signexp += 2; (but needs private data access) */
sum *= (real)4;
return z * sum;
return z * (sum << 2);
}

static real LOG_2 = fastlog((real)2);


+ 5
- 0
src/real.h Целия файл

@@ -46,6 +46,11 @@ public:
real &operator *=(real const &x);
real &operator /=(real const &x);

real operator <<(int x) const;
real operator >>(int x) const;
real &operator <<=(int x);
real &operator >>=(int x);

bool operator ==(real const &x) const;
bool operator !=(real const &x) const;
bool operator <(real const &x) const;


+ 27
- 5
test/unit/real.cpp Целия файл

@@ -69,7 +69,7 @@ LOLUNIT_FIXTURE(RealTest)
LOLUNIT_ASSERT_EQUAL(a4, 0.0f);
}

LOLUNIT_TEST(RealComparison)
LOLUNIT_TEST(Comparison)
{
LOLUNIT_ASSERT(real(1.0f) > real(0.5f));
LOLUNIT_ASSERT(real(1.0f) >= real(0.5f));
@@ -90,7 +90,7 @@ LOLUNIT_FIXTURE(RealTest)
LOLUNIT_ASSERT(real(0.5f) >= real(-1.0f));
}

LOLUNIT_TEST(RealAddition)
LOLUNIT_TEST(Addition)
{
float a1 = real(1.0f) + real(0.0f);
float a2 = real(0.0f) + real(1.0f);
@@ -112,14 +112,14 @@ LOLUNIT_FIXTURE(RealTest)
LOLUNIT_ASSERT_DOUBLES_EQUAL(a8, 0.1, 1.0e-13);
}

LOLUNIT_TEST(RealSubtraction)
LOLUNIT_TEST(Subtraction)
{
float a1 = real(1.0f) + real(1e20f) - real(1e20f);

LOLUNIT_ASSERT_EQUAL(a1, 1.0f);
}

LOLUNIT_TEST(RealMultiplication)
LOLUNIT_TEST(Multiplication)
{
real x(1.25f);
real y(1.5f);
@@ -137,7 +137,7 @@ LOLUNIT_FIXTURE(RealTest)
LOLUNIT_ASSERT_EQUAL(m4, -1.5f * -1.5f);
}

LOLUNIT_TEST(RealDivision)
LOLUNIT_TEST(Division)
{
real a1(1.0f);
real a2(2.0f);
@@ -154,6 +154,28 @@ LOLUNIT_FIXTURE(RealTest)
LOLUNIT_ASSERT_EQUAL(m4, 1.0f);
LOLUNIT_ASSERT_EQUAL(m5, -0.5f);
}

LOLUNIT_TEST(Shift)
{
real a1(1.5);
real a2(-1.5);
real a3(0.0);

LOLUNIT_ASSERT_EQUAL((double)(a1 >> 7), 0.01171875);
LOLUNIT_ASSERT_EQUAL((double)(a1 >> -7), 192.0);
LOLUNIT_ASSERT_EQUAL((double)(a1 << 7), 192.0);
LOLUNIT_ASSERT_EQUAL((double)(a1 << -7), 0.01171875);

LOLUNIT_ASSERT_EQUAL((double)(a2 >> 7), -0.01171875);
LOLUNIT_ASSERT_EQUAL((double)(a2 >> -7), -192.0);
LOLUNIT_ASSERT_EQUAL((double)(a2 << 7), -192.0);
LOLUNIT_ASSERT_EQUAL((double)(a2 << -7), -0.01171875);

LOLUNIT_ASSERT_EQUAL((double)(a3 >> 7), 0.0);
LOLUNIT_ASSERT_EQUAL((double)(a3 >> -7), 0.0);
LOLUNIT_ASSERT_EQUAL((double)(a3 << 7), 0.0);
LOLUNIT_ASSERT_EQUAL((double)(a3 << -7), 0.0);
}
};

} /* namespace lol */


Зареждане…
Отказ
Запис