Changeset 972
- Timestamp:
- Sep 22, 2011, 9:15:59 AM (11 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/real.cpp
r971 r972 54 54 uint32_t mantissa = (m_mantissa[0] << 7) | (m_mantissa[1] >> 9); 55 55 56 int e = (int) (m_signexp & 0x7fffffffu)- (1 << 30) + (1 << 7);56 int e = (int)exponent - (1 << 30) + (1 << 7); 57 57 58 58 if (e < 0) … … 66 66 } 67 67 68 real real::operator -() 69 { 70 m_signexp ^= 0x80000000u; 71 return *this; 68 real real::operator -() const 69 { 70 real ret = *this; 71 ret.m_signexp ^= 0x80000000u; 72 return ret; 72 73 } 73 74 74 75 real real::operator +(real const &x) const 75 76 { 77 if (x.m_signexp << 1 == 0) 78 return *this; 79 80 /* Ensure *this is the larger exponent, and both arguments are 81 * positive (otherwise, switch signs, or replace + with -). */ 76 82 if ((m_signexp << 1) < (x.m_signexp << 1)) 77 83 return x + *this; 78 84 79 if (x.m_signexp << 1 == 0) 80 return *this; 81 82 /* For now, assume both numbers are positive. */ 85 if (m_signexp >> 31) 86 return -(-*this + -x); 87 88 if (x.m_signexp >> 31) 89 return *this - x; 90 83 91 real ret; 84 92 85 int e1 = (m_signexp & 0x7fffffffu)- (1 << 30) + 1;86 int e2 = (x.m_signexp & 0x7fffffffu)- (1 << 30) + 1;93 int e1 = m_signexp - (1 << 30) + 1; 94 int e2 = x.m_signexp - (1 << 30) + 1; 87 95 88 96 int bigoff = (e1 - e2) / (sizeof(uint16_t) * 8); … … 92 100 93 101 uint32_t carry = 0; 94 for (int i = 0; i < BIGITS; i++)95 { 96 carry = m_mantissa[ BIGITS - 1 -i];97 if ( BIGITS - 1 -i - bigoff >= 0)98 carry += x.m_mantissa[ BIGITS - 1 -i - bigoff] >> off;99 else if ( BIGITS - 1 -i - bigoff == -1)102 for (int i = BIGITS; i--; ) 103 { 104 carry = m_mantissa[i]; 105 if (i - bigoff >= 0) 106 carry += x.m_mantissa[i - bigoff] >> off; 107 else if (i - bigoff == -1) 100 108 carry += 0x0001u >> off; 101 109 102 if ( BIGITS - 1 - i - bigoff - 1 >=0)103 carry += (x.m_mantissa[ BIGITS - 1 -i - bigoff - 1] << (16 - off)) & 0xffffu;104 else if ( BIGITS - 1 - i - bigoff - 1 == -1)110 if (i - bigoff > 0) 111 carry += (x.m_mantissa[i - bigoff - 1] << (16 - off)) & 0xffffu; 112 else if (i - bigoff == 0) 105 113 carry += 0x0001u << (16 - off); 106 114 107 ret.m_mantissa[ BIGITS - 1 -i] = carry;115 ret.m_mantissa[i] = carry; 108 116 carry >>= 16; 109 117 } … … 125 133 } 126 134 135 real real::operator -(real const &x) const 136 { 137 if (x.m_signexp << 1 == 0) 138 return *this; 139 140 /* Ensure *this is the larger exponent, and both arguments are 141 * positive (otherwise, switch signs, or replace - with +). */ 142 if ((m_signexp << 1) < (x.m_signexp << 1)) 143 return -(x - *this); 144 145 if (m_signexp >> 31) 146 return -(-*this + x); 147 148 if (x.m_signexp >> 31) 149 return (*this) + (-x); 150 151 real ret; 152 153 return ret; 154 } 155 127 156 real real::operator *(real const &x) const 128 157 { -
trunk/src/real.h
r970 r972 31 31 32 32 operator float() const; 33 real operator -() ;33 real operator -() const; 34 34 real operator +(real const &x) const; 35 real operator -(real const &x) const; 35 36 real operator *(real const &x) const; 36 37 -
trunk/test/unit/real.cpp
r971 r972 23 23 LOLUNIT_FIXTURE(RealTest) 24 24 { 25 public:26 25 LOLUNIT_TEST(test_real_from_float) 27 26 { … … 54 53 LOLUNIT_TEST(test_real_add) 55 54 { 55 float a1 = real(1.0f) + real(0.0f); 56 float a2 = real(0.0f) + real(1.0f); 57 float a3 = real(1.0f) + real(1.0f); 58 float a4 = real(-1.0f) + real(-1.0f); 59 float a5 = real(1.0f) + real(0.125f); 60 61 LOLUNIT_ASSERT_EQUAL(a1, 1.0f); 62 LOLUNIT_ASSERT_EQUAL(a2, 1.0f); 63 LOLUNIT_ASSERT_EQUAL(a3, 2.0f); 64 LOLUNIT_ASSERT_EQUAL(a4, -2.0f); 65 LOLUNIT_ASSERT_EQUAL(a5, 1.125f); 66 } 67 68 LOLUNIT_TEST(test_real_sub) 69 { 70 #if 0 71 printf("\n"); 72 real k(1.25f); 73 k.print(); 74 real l(1.0f); 75 l.print(); 76 real m = k - l; 77 m.print(); 78 #endif 56 79 LOLUNIT_ASSERT(true); 57 80 }
Note: See TracChangeset
for help on using the changeset viewer.