浏览代码

math: use std::array in bigints to avoid zero-sized C array errors.

undefined
Sam Hocevar 10 年前
父节点
当前提交
51d413f891
共有 1 个文件被更改,包括 6 次插入17 次删除
  1. +6
    -17
      src/lol/math/bigint.h

+ 6
- 17
src/lol/math/bigint.h 查看文件

@@ -31,20 +31,6 @@ namespace lol
/* Avoid issues with NaCl headers */ /* Avoid issues with NaCl headers */
#undef log2 #undef log2


template<unsigned int N, typename T>
class bigint_digits
{
protected:
T m_digits[N];
};

template<typename T>
class bigint_digits<0, T>
{
protected:
static T const m_digits[1];
};

/* /*
* A bigint stores its digits in an array of integers. The MSB of the * A bigint stores its digits in an array of integers. The MSB of the
* integers are unused. The highest used bit is the sign bit. * integers are unused. The highest used bit is the sign bit.
@@ -53,7 +39,7 @@ protected:
*/ */


template<unsigned int N = 16, typename T = uint32_t> template<unsigned int N = 16, typename T = uint32_t>
class bigint : public bigint_digits<N, T>
class bigint
{ {
static int const bits_per_digit = sizeof(T) * 8 - 1; static int const bits_per_digit = sizeof(T) * 8 - 1;
static T const digit_mask = ~((T)1 << bits_per_digit); static T const digit_mask = ~((T)1 << bits_per_digit);
@@ -260,12 +246,12 @@ public:
*/ */
inline bool operator ==(bigint<N,T> const &x) const inline bool operator ==(bigint<N,T> const &x) const
{ {
return memcmp(m_digits, x.m_digits, sizeof(m_digits)) == 0;
return m_digits == x.m_digits;
} }


inline bool operator !=(bigint<N,T> const &x) const inline bool operator !=(bigint<N,T> const &x) const
{ {
return !(*this == x);
return m_digits != x.m_digits;
} }


/* /*
@@ -418,6 +404,9 @@ private:
ret |= m_digits[digit_index + 1] << (bits_per_digit - bit_index); ret |= m_digits[digit_index + 1] << (bits_per_digit - bit_index);
return ret; return ret;
} }

/* Use std::array instead of C-style array to handle N == 0. */
std::array<T, N> m_digits;
}; };


/* /*


正在加载...
取消
保存