Browse Source

math: use std::vector instead of lol::array.

legacy
Sam Hocevar 6 years ago
parent
commit
67a5138718
2 changed files with 16 additions and 15 deletions
  1. +6
    -5
      src/lol/math/real.h
  2. +10
    -10
      src/math/real.cpp

+ 6
- 5
src/lol/math/real.h View File

@@ -1,7 +1,7 @@
// //
// Lol Engine // Lol Engine
// //
// Copyright © 2010—2017 Sam Hocevar <sam@hocevar.net>
// Copyright © 2010—2018 Sam Hocevar <sam@hocevar.net>
// //
// Lol Engine is free software. It comes without any warranty, to // Lol Engine is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it // the extent permitted by applicable law. You can redistribute it
@@ -19,7 +19,8 @@


#include <lol/base/types.h> #include <lol/base/types.h>


#include <stdint.h>
#include <cstdint>
#include <vector>


namespace lol namespace lol
{ {
@@ -56,7 +57,7 @@ public:


Real(char const *str); Real(char const *str);


LOL_ATTR_NODISCARD bool is_zero() const { return m_mantissa.count() == 0; }
LOL_ATTR_NODISCARD bool is_zero() const { return m_mantissa.size() == 0; }
LOL_ATTR_NODISCARD bool is_negative() const { return m_sign; } LOL_ATTR_NODISCARD bool is_negative() const { return m_sign; }
LOL_ATTR_NODISCARD bool is_nan() const { return m_nan; } LOL_ATTR_NODISCARD bool is_nan() const { return m_nan; }
LOL_ATTR_NODISCARD bool is_inf() const { return m_inf; } LOL_ATTR_NODISCARD bool is_inf() const { return m_inf; }
@@ -226,7 +227,7 @@ public:
static Real<T> const& R_MAX(); static Real<T> const& R_MAX();


private: private:
array<T> m_mantissa;
std::vector<T> m_mantissa;
exponent_t m_exponent = 0; exponent_t m_exponent = 0;
bool m_sign = false, m_nan = false, m_inf = false; bool m_sign = false, m_nan = false, m_inf = false;


@@ -234,7 +235,7 @@ public:
static int DEFAULT_BIGIT_COUNT; static int DEFAULT_BIGIT_COUNT;


static inline int bigit_bits() { return 8 * sizeof(bigit_t); } static inline int bigit_bits() { return 8 * sizeof(bigit_t); }
inline int bigit_count() const { return m_mantissa.count(); }
inline int bigit_count() const { return m_mantissa.size(); }
inline int total_bits() const { return bigit_count() * bigit_bits(); } inline int total_bits() const { return bigit_count() * bigit_bits(); }
}; };




+ 10
- 10
src/math/real.cpp View File

@@ -122,7 +122,7 @@ template<> real::Real(uint64_t i)
m_exponent = 64 - delta; m_exponent = 64 - delta;
m_mantissa.resize(DEFAULT_BIGIT_COUNT); m_mantissa.resize(DEFAULT_BIGIT_COUNT);
m_mantissa[0] = (bigit_t)(i >> 32); m_mantissa[0] = (bigit_t)(i >> 32);
if (m_mantissa.count() > 1)
if (bigit_count() > 1)
m_mantissa[1] = (bigit_t)i; m_mantissa[1] = (bigit_t)i;
} }
} }
@@ -148,7 +148,7 @@ template<> real::Real(double d)
m_exponent = exponent - ((1 << 10) - 1); m_exponent = exponent - ((1 << 10) - 1);
m_mantissa.resize(DEFAULT_BIGIT_COUNT); m_mantissa.resize(DEFAULT_BIGIT_COUNT);
m_mantissa[0] = (bigit_t)(u.x >> 20); m_mantissa[0] = (bigit_t)(u.x >> 20);
if (m_mantissa.count() > 1)
if (bigit_count() > 1)
m_mantissa[1] = (bigit_t)(u.x << 12); m_mantissa[1] = (bigit_t)(u.x << 12);
break; break;
} }
@@ -209,10 +209,10 @@ template<> real::operator double() const


/* Store mantissa if necessary */ /* Store mantissa if necessary */
u.x <<= 32; u.x <<= 32;
if (m_mantissa.count() > 0)
if (bigit_count() > 0)
u.x |= m_mantissa[0]; u.x |= m_mantissa[0];
u.x <<= 20; u.x <<= 20;
if (m_mantissa.count() > 1)
if (bigit_count() > 1)
{ {
u.x |= m_mantissa[1] >> 12; u.x |= m_mantissa[1] >> 12;
/* Rounding */ /* Rounding */
@@ -361,7 +361,7 @@ template<> real real::operator +(real const &x) const
return *this; return *this;


real ret; real ret;
ret.m_mantissa.resize(m_mantissa.count());
ret.m_mantissa.resize(bigit_count());
ret.m_exponent = m_exponent; ret.m_exponent = m_exponent;


uint64_t carry = 0; uint64_t carry = 0;
@@ -428,7 +428,7 @@ template<> real real::operator -(real const &x) const
return *this; return *this;


real ret; real ret;
ret.m_mantissa.resize(m_mantissa.count());
ret.m_mantissa.resize(bigit_count());
ret.m_exponent = m_exponent; ret.m_exponent = m_exponent;


/* int64_t instead of uint64_t to preserve sign through shifts */ /* int64_t instead of uint64_t to preserve sign through shifts */
@@ -517,7 +517,7 @@ template<> real real::operator *(real const &x) const
if (is_zero() || x.is_zero()) if (is_zero() || x.is_zero())
return ret; return ret;


ret.m_mantissa.resize(m_mantissa.count());
ret.m_mantissa.resize(bigit_count());
ret.m_exponent = m_exponent + x.m_exponent; ret.m_exponent = m_exponent + x.m_exponent;


/* Accumulate low order product; no need to store it, we just /* Accumulate low order product; no need to store it, we just
@@ -738,7 +738,7 @@ template<> real inverse(real const &x)
u.x |= x.m_mantissa[0] >> 9; u.x |= x.m_mantissa[0] >> 9;
u.f = 1.0f / u.f; u.f = 1.0f / u.f;


ret.m_mantissa.resize(x.m_mantissa.count());
ret.m_mantissa.resize(x.bigit_count());
ret.m_mantissa[0] = u.x << 9; ret.m_mantissa[0] = u.x << 9;
ret.m_sign = x.m_sign; ret.m_sign = x.m_sign;
ret.m_exponent = -x.m_exponent + (u.x >> 23) - 0x7f; ret.m_exponent = -x.m_exponent + (u.x >> 23) - 0x7f;
@@ -774,7 +774,7 @@ template<> real sqrt(real const &x)
u.f = 1.0f / sqrtf(u.f); u.f = 1.0f / sqrtf(u.f);


real ret; real ret;
ret.m_mantissa.resize(x.m_mantissa.count());
ret.m_mantissa.resize(x.bigit_count());
ret.m_mantissa[0] = u.x << 9; ret.m_mantissa[0] = u.x << 9;


ret.m_exponent = -(x.m_exponent - tweak) / 2 + (u.x >> 23) - 0x7f; ret.m_exponent = -(x.m_exponent - tweak) / 2 + (u.x >> 23) - 0x7f;
@@ -811,7 +811,7 @@ template<> real cbrt(real const &x)
u.f = powf(u.f, 1.f / 3); u.f = powf(u.f, 1.f / 3);


real ret; real ret;
ret.m_mantissa.resize(x.m_mantissa.count());
ret.m_mantissa.resize(x.bigit_count());
ret.m_mantissa[0] = u.x << 9; ret.m_mantissa[0] = u.x << 9;
ret.m_exponent = (x.m_exponent - tweak) / 3 + (u.x >> 23) - 0x7f; ret.m_exponent = (x.m_exponent - tweak) / 3 + (u.x >> 23) - 0x7f;
ret.m_sign = x.m_sign; ret.m_sign = x.m_sign;


Loading…
Cancel
Save