瀏覽代碼

Remove more math-related headers.

legacy
Sam Hocevar 4 年之前
父節點
當前提交
e340757a51
共有 16 個檔案被更改,包括 21 行新增500 行删除
  1. +2
    -2
      doc/samples/benchmark/real.cpp
  2. +2
    -2
      doc/samples/math/pi.cpp
  3. +3
    -7
      doc/samples/math/poly.cpp
  4. +2
    -1
      doc/samples/sandbox/sample.cpp
  5. +2
    -2
      doc/tutorial/11_fractal.cpp
  6. +1
    -1
      lol-core
  7. +0
    -1
      src/Makefile.am
  8. +0
    -2
      src/lol-core.vcxproj
  9. +0
    -6
      src/lol-core.vcxproj.filters
  10. +1
    -1
      src/lol/gpu/vertexbuffer.h
  11. +0
    -2
      src/lol/math/all.h
  12. +0
    -420
      src/lol/math/bigint.h
  13. +0
    -49
      src/lol/math/constants.h
  14. +4
    -1
      src/t/math/bigint.cpp
  15. +2
    -1
      src/t/math/polynomial.cpp
  16. +2
    -2
      src/t/math/real.cpp

+ 2
- 2
doc/samples/benchmark/real.cpp 查看文件

@@ -1,7 +1,7 @@
//
// Lol Engine — Benchmark program
//
// Copyright © 2005—2018 Sam Hocevar <sam@hocevar.net>
// Copyright © 2005—2020 Sam Hocevar <sam@hocevar.net>
//
// This program is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
@@ -17,7 +17,7 @@
#include <cstdio>

#include <lol/engine.h>
#include <lol/math/real.h>
#include <lol/types/real.h>

using namespace lol;



+ 2
- 2
doc/samples/math/pi.cpp 查看文件

@@ -1,7 +1,7 @@
//
// Lol Engine — Sample math program: compute Pi
//
// Copyright © 2005—2019 Sam Hocevar <sam@hocevar.net>
// Copyright © 2005—2020 Sam Hocevar <sam@hocevar.net>
//
// This program is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
@@ -18,7 +18,7 @@
#include <iomanip>

#include <lol/engine.h>
#include <lol/math/real.h>
#include <lol/types/real.h>

using lol::real;



+ 3
- 7
doc/samples/math/poly.cpp 查看文件

@@ -1,7 +1,7 @@
//
// Lol Engine — Sample math program: polynomials
//
// Copyright © 2005—2017 Sam Hocevar <sam@hocevar.net>
// Copyright © 2005—2020 Sam Hocevar <sam@hocevar.net>
//
// This program is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
@@ -14,16 +14,12 @@
# include "config.h"
#endif

#include <cstdio>

#include <lol/engine.h>
#include <lol/math/polynomial.h>

using namespace lol;

int main(int argc, char **argv)
int main()
{
UNUSED(argc, argv);

polynomial<polynomial<float>> p;

return EXIT_SUCCESS;


+ 2
- 1
doc/samples/sandbox/sample.cpp 查看文件

@@ -1,7 +1,7 @@
//
// Lol Engine — Sandbox program
//
// Copyright © 2005—2018 Sam Hocevar <sam@hocevar.net>
// Copyright © 2005—2020 Sam Hocevar <sam@hocevar.net>
//
// This program is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
@@ -15,6 +15,7 @@
#endif

#include <lol/engine.h>
#include <lol/types/bigint.h>

using namespace lol;



+ 2
- 2
doc/tutorial/11_fractal.cpp 查看文件

@@ -1,7 +1,7 @@
//
// Lol Engine — Fractal tutorial
//
// Copyright © 2011—2019 Sam Hocevar <sam@hocevar.net>
// Copyright © 2011—2020 Sam Hocevar <sam@hocevar.net>
//
// Lol Engine is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
@@ -18,8 +18,8 @@
#include <sstream>

#include <lol/engine.h>
#include <lol/types/real.h>
#include <lol/math/vector.h>
#include <lol/math/real.h>
#include <lol/math/transform.h>

#include "loldebug.h"


+ 1
- 1
lol-core

@@ -1 +1 @@
Subproject commit 59a4b5bd0c6e0f3e4d6decaf260682a8e86672cb
Subproject commit 71d9bc1ec83979000547c5134a45d1ee8cb4b55e

+ 0
- 1
src/Makefile.am 查看文件

@@ -38,7 +38,6 @@ liblol_core_headers = \
lol/math/all.h \
lol/math/functions.h \
lol/math/geometry.h lol/math/interp.h lol/math/arraynd.h \
lol/math/constants.h lol/math/bigint.h \
lol/math/noise/gradient.h lol/math/noise/perlin.h \
lol/math/noise/simplex.h \
\


+ 0
- 2
src/lol-core.vcxproj 查看文件

@@ -278,8 +278,6 @@
<ClInclude Include="lol\lua.h" />
<ClInclude Include="lol\math\all.h" />
<ClInclude Include="lol\math\arraynd.h" />
<ClInclude Include="lol\math\bigint.h" />
<ClInclude Include="lol\math\constants.h" />
<ClInclude Include="lol\math\functions.h" />
<ClInclude Include="lol\math\geometry.h" />
<ClInclude Include="lol\math\interp.h" />


+ 0
- 6
src/lol-core.vcxproj.filters 查看文件

@@ -419,12 +419,6 @@
<ClInclude Include="lol\math\arraynd.h">
<Filter>lol\math</Filter>
</ClInclude>
<ClInclude Include="lol\math\bigint.h">
<Filter>lol\math</Filter>
</ClInclude>
<ClInclude Include="lol\math\constants.h">
<Filter>lol\math</Filter>
</ClInclude>
<ClInclude Include="lol\math\functions.h">
<Filter>lol\math</Filter>
</ClInclude>


+ 1
- 1
src/lol/gpu/vertexbuffer.h 查看文件

@@ -17,7 +17,7 @@
// ----------------------------------------------
//

#include <lol/math/half.h>
#include <lol/types/half.h>

#include <cstring>
#include <map>


+ 0
- 2
src/lol/math/all.h 查看文件

@@ -14,14 +14,12 @@

#include <lol/math/constants.h>
#include <lol/math/functions.h>
#include <lol/math/bigint.h>
#include <lol/math/vector.h>
#include <lol/math/transform.h>
#include <lol/math/arraynd.h>
#include <lol/math/geometry.h>
#include <lol/math/interp.h>
#include <lol/math/rand.h>
#include <lol/math/polynomial.h>

#include <lol/math/noise/gradient.h>
#include <lol/math/noise/perlin.h>


+ 0
- 420
src/lol/math/bigint.h 查看文件

@@ -1,420 +0,0 @@
//
// Lol Engine
//
// Copyright © 2010—2020 Sam Hocevar <sam@hocevar.net>
//
// Lol Engine is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
// and/or modify it under the terms of the Do What the Fuck You Want
// to Public License, Version 2, as published by the WTFPL Task Force.
// See http://www.wtfpl.net/ for more details.
//

#pragma once

//
// The bigint class
// ----------------
//

#include <lol/base/types.h>

#include <array>
#include <cstdint>

namespace lol
{

/* This is OUR namespace. Don't let Windows headers mess with it. */
#undef min
#undef max

/*
* 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.
*
* Digits are stored in little endian mode.
*/

template<unsigned int N = 16, typename T = uint32_t>
class [[nodiscard]] bigint
{
static int const bits_per_digit = sizeof(T) * 8 - 1;
static T const digit_mask = ~((T)1 << bits_per_digit);

public:
inline bigint()
{
}

explicit bigint(int32_t x)
{
/* FIXME: doesn’t work if sizeof(T) < sizeof(int32_t) */
for (auto &digit : m_digits)
digit = x >= 0 ? (T)0 : digit_mask;
if (N > 0)
m_digits[0] = x & digit_mask;
}

explicit bigint(uint32_t x)
{
/* FIXME: doesn’t work if sizeof(T) > sizeof(uint32_t) */
for (auto &digit : m_digits)
digit = (T)0;
if (N > 0)
m_digits[0] = x & digit_mask;
if (N > 1)
m_digits[1] = x >> bits_per_digit;
}

explicit inline operator uint32_t() const
{
/* FIXME: doesn’t work if sizeof(T) != sizeof(int32_t) */
if (N < 1)
return 0;
if (N < 2)
return (uint32_t)(m_digits[0]
| ((m_digits[0] << 1) & ~digit_mask));

return (uint32_t)(m_digits[0] | (m_digits[1] << bits_per_digit));
}

inline operator int32_t() const
{
return (int32_t)(uint32_t)*this;
}

/*
* bigint cast to bigint of different size: we copy all digits then
* pad the rest (if applicable) with zeroes or ones to extend the
* sign bit.
*/
template<int M>
explicit bigint(bigint<M,T> const &x)
{
for (unsigned int i = 0; i < (N < M) ? N : M; ++i)
m_digits[i] = x.m_digits[i];

if (N > M)
{
T padding = x.is_negative() ? digit_mask : (T)0;
for (unsigned int i = M; i < N; ++i)
m_digits[i] = padding;
}
}

/*
* bigint bitwise NOT: we just flip all bits except the unused ones.
*/
bigint<N,T> operator ~() const
{
bigint<N,T> ret;
for (unsigned int i = 0; i < N; ++i)
ret.m_digits[i] = m_digits[i] ^ digit_mask;
return ret;
}

/*
* bigint bitwise AND: just perform a bitwise AND on all digits.
*/
bigint<N,T> & operator &=(bigint<N,T> const &x)
{
for (unsigned int i = 0; i < N; ++i)
m_digits[i] &= x.m_digits[i];
return *this;
}

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

/*
* bigint bitwise OR: just perform a bitwise OR on all digits.
*/
bigint<N,T> & operator |=(bigint<N,T> const &x)
{
for (unsigned int i = 0; i < N; ++i)
m_digits[i] |= x.m_digits[i];
return *this;
}

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

/*
* bigint bitwise XOR: just perform a bitwise XOR on all digits.
*/
bigint<N,T> & operator ^=(bigint<N,T> const &x)
{
for (unsigned int i = 0; i < N; ++i)
m_digits[i] ^= x.m_digits[i];
return *this;
}

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

/*
* bigint unary plus: a no-op
*/
inline bigint<N,T> operator +() const
{
return *this;
}

/*
* bigint unary minus: flip bits and add one
*/
bigint<N,T> operator -() const
{
bigint<N,T> ret;
T carry(1);
for (unsigned int i = 0; i < N; ++i)
{
T digit = (m_digits[i] ^ digit_mask) + carry;
ret.m_digits[i] = digit & digit_mask;
carry = (digit & ~digit_mask) ? T(1) : T(0);
}
return ret;
}

/*
* bigint addition: we add the digits one-to-one, carrying overflows,
* and pad missing digits if one of the two operands is shorter.
*/
template<unsigned int M>
bigint<((N > M) ? N : M), T> operator +(bigint<M,T> const &x) const
{
bigint<((N > M) ? N : M), T> ret;
T padding = is_negative() ? digit_mask : (T)0;
T x_padding = x.is_negative() ? digit_mask : (T)0;
T carry(0);
for (unsigned int i = 0; i < ((N > M) ? N : M); ++i)
{
T digit = (i < N ? m_digits[i] : padding)
+ (i < M ? x.m_digits[i] : x_padding)
+ carry;
ret.m_digits[i] = digit & digit_mask;
carry = (digit & ~digit_mask) ? T(1) : T(0);
}
return ret;
}

/*
* bigint subtraction: a combination of addition and unary minus;
* we add the result of flipping digits and adding one.
* FIXME: this could be factored with operator+().
*/
template<unsigned int M>
bigint<((N > M) ? N : M), T> operator -(bigint<M,T> const &x) const
{
bigint<(N > M) ? N : M, T> ret;
T padding = is_negative() ? digit_mask : (T)0;
T x_padding = x.is_negative() ? digit_mask : (T)0;
T carry(1);
for (unsigned int i = 0; i < ((N > M) ? N : M); ++i)
{
T digit = (i < N ? m_digits[i] : padding)
+ ((i < M ? x.m_digits[i] : x_padding) ^ digit_mask)
+ carry;
ret.m_digits[i] = digit & digit_mask;
carry = (digit & ~digit_mask) ? T(1) : T(0);
}
return ret;
}

/*
* bigint multiplication: the resulting integer has as many digits
* as the sum of the two operands.
*/
template<unsigned int M>
bigint<N + M, T> operator *(bigint<M,T> const &x) const
{
return multiply(x);
}

/*
* bigint equality operator: just use memcmp.
* FIXME: we could easily support operands of different sizes.
*/
inline bool operator ==(bigint<N,T> const &x) const
{
return m_digits == x.m_digits;
}

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

/*
* bigint comparison operators: take a quick decision if signs
* differ. Otherwise, compare all digits.
*/
bool operator >(bigint<N,T> const &x) const
{
if (is_negative() ^ x.is_negative())
return x.is_negative();
for (unsigned int i = 0; i < N; ++i)
if (m_digits[i] != x.m_digits[i])
return m_digits[i] > x.m_digits[i];
return false;
}

bool operator <(bigint<N,T> const &x) const
{
if (is_negative() ^ x.is_negative())
return is_negative();
for (unsigned int i = 0; i < N; ++i)
if (m_digits[i] != x.m_digits[i])
return m_digits[i] < x.m_digits[i];
return false;
}

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

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

void print() const
{
printf("0x");

int n = (bits_per_digit * N + 31) / 32;
while (n > 1 && get_uint32(n) == 0)
--n;

if (n > 0)
printf("%x", get_uint32(--n));
while (n--)
printf("%08x", get_uint32(n));

printf("\n");
}

template<unsigned int A, unsigned int B>
inline bigint<B - A, T> &slice()
{
return *reinterpret_cast<bigint<B - A, T> *>((T *)this + A);
}

template<unsigned int A, unsigned int B>
inline bigint<B - A, T> const &slice() const
{
return *reinterpret_cast<bigint<B - A, T> const *>((T const *)this + A);
}

private:
/* Allow other types of bigints to access our private members */
template<unsigned int, typename> friend class bigint;

inline bool is_negative() const
{
if (N < 1)
return false;
return (m_digits[N - 1] >> (bits_per_digit - 1)) != 0;
}

template<unsigned int M>
typename std::enable_if<(N != M || (N > 1 && N < 64)), bigint<N + M, T>>
::type inline multiply(bigint<M,T> const &b) const
{
/* FIXME: other digit sizes are not supported */
static_assert(sizeof(T) <= sizeof(uint32_t), "ensure T is uint32_t");

bigint<N + M> ret(0);
for (unsigned int i = 0; i < N; ++i)
{
T carry(0);
for (unsigned int j = 0; j < M; ++j)
{
uint64_t digit = ret.m_digits[i + j]
+ (uint64_t)m_digits[i] * b.m_digits[j]
+ carry;
ret.m_digits[i + j] = (T)digit & digit_mask;
carry = (digit >> bits_per_digit) & digit_mask;
}

for (unsigned int j = M; i + j < M + N && carry != 0; ++i)
{
T digit = ret.m_digits[i + j] + carry;
ret.m_digits[i + j] = (T)digit & digit_mask;
carry = (digit & ~digit_mask) ? T(1) : T(0);
}
}

return ret;
}

template<unsigned int M>
typename std::enable_if<(N == M) && (N >= 64), bigint<N + M, T>>
::type inline multiply(bigint<M,T> const &b) const
{
bigint<2 * N, T> ret, tmp(0);

bigint<N / 2, T> const &a0 = slice<0, N / 2>();
bigint<N / 2, T> const &a1 = slice<N / 2, N>();
bigint<N / 2, T> const &b0 = b.template slice<0, N / 2>();
bigint<N / 2, T> const &b1 = b.template slice<N / 2, N>();
bigint<N, T> &r0 = ret.template slice<0, N>();
bigint<N, T> &r1 = ret.template slice<N / 2, N + N / 2>();
bigint<N, T> &r2 = ret.template slice<N, 2 * N>();
r0 = a0 * b0;
r2 = a1 * b1;
/* FIXME: check for overflows here */
r1 = (a0 + a1) * (b0 + b1) - r0 - r2;

return ret + tmp;
}

template<unsigned int M, int = 0>
typename std::enable_if<(N == M) && (N == 1), bigint<N + M, T>>
::type inline multiply(bigint<M,T> const &b) const
{
bigint<2, T> ret;
uint64_t digit = (uint64_t)m_digits[0] * b.m_digits[0];
ret.m_digits[0] = (T)(digit) & ret.digit_mask;
ret.m_digits[1] = (T)(digit >> ret.bits_per_digit) & ret.digit_mask;
return ret;
}

inline uint32_t get_uint32(int offset) const
{
unsigned int bit = offset * 32;
unsigned int digit_index = bit / bits_per_digit;
unsigned int bit_index = bit % bits_per_digit;

if (digit_index >= N)
return 0;

uint32_t ret = m_digits[digit_index] >> bit_index;
if (bits_per_digit - bit_index < 32 && digit_index < N - 1)
ret |= m_digits[digit_index + 1] << (bits_per_digit - bit_index);
return ret;
}

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

/*
* Some convenience typedefs
*/

typedef bigint<8, uint32_t> int248_t;
typedef bigint<16, uint32_t> int496_t;
typedef bigint<32, uint32_t> int992_t;
typedef bigint<64, uint32_t> int1984_t;

} /* namespace lol */


+ 0
- 49
src/lol/math/constants.h 查看文件

@@ -1,49 +0,0 @@
//
// Lol Engine
//
// Copyright © 2010—2015 Sam Hocevar <sam@hocevar.net>
//
// Lol Engine is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
// and/or modify it under the terms of the Do What the Fuck You Want
// to Public License, Version 2, as published by the WTFPL Task Force.
// See http://www.wtfpl.net/ for more details.
//

#pragma once

//
// Various maths constants
// ———————————————————————
//

#include <lol/base/types.h>

namespace lol
{

#define LOL_MATH_CONSTANT(name, value) \
static ldouble const LD_##name = value; \
static double const D_##name = (double)LD_##name; \
static float const F_##name = (float)LD_##name;

/* These values are truncated, not rounded, because I don’t care. */

LOL_MATH_CONSTANT(TAU, 6.28318530717958647692528676655900576839433879875L)

LOL_MATH_CONSTANT(PI, 3.14159265358979323846264338327950288419716939937L)
LOL_MATH_CONSTANT(PI_2, 1.57079632679489661923132169163975144209858469968L)
LOL_MATH_CONSTANT(PI_3, 1.04719755119659774615421446109316762806572313312L)
LOL_MATH_CONSTANT(PI_4, 0.78539816339744830961566084581987572104929234984L)

LOL_MATH_CONSTANT(1_PI, 0.31830988618379067153776752674502872406891929148L)
LOL_MATH_CONSTANT(2_PI, 0.63661977236758134307553505349005744813783858296L)

LOL_MATH_CONSTANT(SQRT_2, 1.41421356237309504880168872420969807856967187537L)
LOL_MATH_CONSTANT(SQRT_3, 1.73205080756887729352744634150587236694280525381L)
LOL_MATH_CONSTANT(SQRT_1_2, 0.70710678118654752440084436210484903928483593768L)

#undef LOL_MATH_CONSTANT

} /* namespace lol */


+ 4
- 1
src/t/math/bigint.cpp 查看文件

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

#include <cmath>

#include <lol/types/bigint.h>
#include <lolunit.h>

namespace lol
@@ -26,6 +27,8 @@ lolunit_declare_fixture(bigint_test)
bigint<> a;
bigint<0> b;
bigint<32> c;

UNUSED(a, b,c);
}

lolunit_declare_test(int32_cast)


+ 2
- 1
src/t/math/polynomial.cpp 查看文件

@@ -12,7 +12,8 @@

#include <lol/engine-internal.h>

#include <lol/math/real.h>
#include <lol/types/real.h>
#include <lol/math/polynomial.h>
#include <lolunit.h>

namespace lol


+ 2
- 2
src/t/math/real.cpp 查看文件

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

#include <lol/engine-internal.h>

#include <lol/math/real.h>
#include <lol/types/real.h>
#include <lolunit.h>

#include <cmath>


Loading…
取消
儲存