Changeset 994


Ignore:
Timestamp:
Sep 28, 2011, 7:04:44 PM (11 years ago)
Author:
sam
Message:

core: implement shift operators for reals; they're useful for fast
multiplications by integers, especially powers of two.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/real.cpp

    r993 r994  
    349349    real tmp = *this;
    350350    return *this = tmp / x;
     351}
     352
     353real real::operator <<(int x) const
     354{
     355    real tmp = *this;
     356    return tmp <<= x;
     357}
     358
     359real real::operator >>(int x) const
     360{
     361    real tmp = *this;
     362    return tmp >>= x;
     363}
     364
     365real &real::operator <<=(int x)
     366{
     367    if (m_signexp << 1)
     368        m_signexp += x;
     369    return *this;
     370}
     371
     372real &real::operator >>=(int x)
     373{
     374    if (m_signexp << 1)
     375        m_signexp -= x;
     376    return *this;
    351377}
    352378
     
    542568    }
    543569
    544     /* FIXME: sum.m_signexp += 2; (but needs private data access) */
    545     sum *= (real)4;
    546     return z * sum;
     570    return z * (sum << 2);
    547571}
    548572
  • trunk/src/real.h

    r992 r994  
    4747    real &operator /=(real const &x);
    4848
     49    real operator <<(int x) const;
     50    real operator >>(int x) const;
     51    real &operator <<=(int x);
     52    real &operator >>=(int x);
     53
    4954    bool operator ==(real const &x) const;
    5055    bool operator !=(real const &x) const;
  • trunk/test/unit/real.cpp

    r982 r994  
    7070    }
    7171
    72     LOLUNIT_TEST(RealComparison)
     72    LOLUNIT_TEST(Comparison)
    7373    {
    7474        LOLUNIT_ASSERT(real(1.0f) > real(0.5f));
     
    9191    }
    9292
    93     LOLUNIT_TEST(RealAddition)
     93    LOLUNIT_TEST(Addition)
    9494    {
    9595        float a1 = real(1.0f) + real(0.0f);
     
    113113    }
    114114
    115     LOLUNIT_TEST(RealSubtraction)
     115    LOLUNIT_TEST(Subtraction)
    116116    {
    117117        float a1 = real(1.0f) + real(1e20f) - real(1e20f);
     
    120120    }
    121121
    122     LOLUNIT_TEST(RealMultiplication)
     122    LOLUNIT_TEST(Multiplication)
    123123    {
    124124        real x(1.25f);
     
    138138    }
    139139
    140     LOLUNIT_TEST(RealDivision)
     140    LOLUNIT_TEST(Division)
    141141    {
    142142        real a1(1.0f);
     
    155155        LOLUNIT_ASSERT_EQUAL(m5, -0.5f);
    156156    }
     157
     158    LOLUNIT_TEST(Shift)
     159    {
     160        real a1(1.5);
     161        real a2(-1.5);
     162        real a3(0.0);
     163
     164        LOLUNIT_ASSERT_EQUAL((double)(a1 >> 7), 0.01171875);
     165        LOLUNIT_ASSERT_EQUAL((double)(a1 >> -7), 192.0);
     166        LOLUNIT_ASSERT_EQUAL((double)(a1 << 7), 192.0);
     167        LOLUNIT_ASSERT_EQUAL((double)(a1 << -7), 0.01171875);
     168
     169        LOLUNIT_ASSERT_EQUAL((double)(a2 >> 7), -0.01171875);
     170        LOLUNIT_ASSERT_EQUAL((double)(a2 >> -7), -192.0);
     171        LOLUNIT_ASSERT_EQUAL((double)(a2 << 7), -192.0);
     172        LOLUNIT_ASSERT_EQUAL((double)(a2 << -7), -0.01171875);
     173
     174        LOLUNIT_ASSERT_EQUAL((double)(a3 >> 7), 0.0);
     175        LOLUNIT_ASSERT_EQUAL((double)(a3 >> -7), 0.0);
     176        LOLUNIT_ASSERT_EQUAL((double)(a3 << 7), 0.0);
     177        LOLUNIT_ASSERT_EQUAL((double)(a3 << -7), 0.0);
     178    }
    157179};
    158180
Note: See TracChangeset for help on using the changeset viewer.