diff --git a/doc/samples/benchmark/real.cpp b/doc/samples/benchmark/real.cpp index 2f2d2d51..a9f3018d 100644 --- a/doc/samples/benchmark/real.cpp +++ b/doc/samples/benchmark/real.cpp @@ -1,7 +1,7 @@ // // Lol Engine — Benchmark program // -// Copyright © 2005—2018 Sam Hocevar +// Copyright © 2005—2020 Sam Hocevar // // 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 #include -#include +#include using namespace lol; diff --git a/doc/samples/math/pi.cpp b/doc/samples/math/pi.cpp index 93883288..a61c63af 100644 --- a/doc/samples/math/pi.cpp +++ b/doc/samples/math/pi.cpp @@ -1,7 +1,7 @@ // // Lol Engine — Sample math program: compute Pi // -// Copyright © 2005—2019 Sam Hocevar +// Copyright © 2005—2020 Sam Hocevar // // 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 #include -#include +#include using lol::real; diff --git a/doc/samples/math/poly.cpp b/doc/samples/math/poly.cpp index a7eead7d..b367fcac 100644 --- a/doc/samples/math/poly.cpp +++ b/doc/samples/math/poly.cpp @@ -1,7 +1,7 @@ // // Lol Engine — Sample math program: polynomials // -// Copyright © 2005—2017 Sam Hocevar +// Copyright © 2005—2020 Sam Hocevar // // 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 - -#include +#include using namespace lol; -int main(int argc, char **argv) +int main() { - UNUSED(argc, argv); - polynomial> p; return EXIT_SUCCESS; diff --git a/doc/samples/sandbox/sample.cpp b/doc/samples/sandbox/sample.cpp index 8fb497ce..da435b2d 100644 --- a/doc/samples/sandbox/sample.cpp +++ b/doc/samples/sandbox/sample.cpp @@ -1,7 +1,7 @@ // // Lol Engine — Sandbox program // -// Copyright © 2005—2018 Sam Hocevar +// Copyright © 2005—2020 Sam Hocevar // // 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 +#include using namespace lol; diff --git a/doc/tutorial/11_fractal.cpp b/doc/tutorial/11_fractal.cpp index 43187796..a7641703 100644 --- a/doc/tutorial/11_fractal.cpp +++ b/doc/tutorial/11_fractal.cpp @@ -1,7 +1,7 @@ // // Lol Engine — Fractal tutorial // -// Copyright © 2011—2019 Sam Hocevar +// Copyright © 2011—2020 Sam Hocevar // // 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 #include +#include #include -#include #include #include "loldebug.h" diff --git a/lol-core b/lol-core index 59a4b5bd..71d9bc1e 160000 --- a/lol-core +++ b/lol-core @@ -1 +1 @@ -Subproject commit 59a4b5bd0c6e0f3e4d6decaf260682a8e86672cb +Subproject commit 71d9bc1ec83979000547c5134a45d1ee8cb4b55e diff --git a/src/Makefile.am b/src/Makefile.am index 353cd6cb..4d48637c 100644 --- a/src/Makefile.am +++ b/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 \ \ diff --git a/src/lol-core.vcxproj b/src/lol-core.vcxproj index 24613981..0dff5313 100644 --- a/src/lol-core.vcxproj +++ b/src/lol-core.vcxproj @@ -278,8 +278,6 @@ - - diff --git a/src/lol-core.vcxproj.filters b/src/lol-core.vcxproj.filters index a6c73bdd..71253425 100644 --- a/src/lol-core.vcxproj.filters +++ b/src/lol-core.vcxproj.filters @@ -419,12 +419,6 @@ lol\math - - lol\math - - - lol\math - lol\math diff --git a/src/lol/gpu/vertexbuffer.h b/src/lol/gpu/vertexbuffer.h index 30a366be..eef95b6e 100644 --- a/src/lol/gpu/vertexbuffer.h +++ b/src/lol/gpu/vertexbuffer.h @@ -17,7 +17,7 @@ // ---------------------------------------------- // -#include +#include #include #include diff --git a/src/lol/math/all.h b/src/lol/math/all.h index ed78c54e..1af58239 100644 --- a/src/lol/math/all.h +++ b/src/lol/math/all.h @@ -14,14 +14,12 @@ #include #include -#include #include #include #include #include #include #include -#include #include #include diff --git a/src/lol/math/bigint.h b/src/lol/math/bigint.h deleted file mode 100644 index 363721d3..00000000 --- a/src/lol/math/bigint.h +++ /dev/null @@ -1,420 +0,0 @@ -// -// Lol Engine -// -// Copyright © 2010—2020 Sam Hocevar -// -// 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 - -#include -#include - -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 -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 - explicit bigint(bigint 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 operator ~() const - { - bigint 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 & operator &=(bigint const &x) - { - for (unsigned int i = 0; i < N; ++i) - m_digits[i] &= x.m_digits[i]; - return *this; - } - - inline bigint operator &(bigint const &x) const - { - return bigint(*this) &= x; - } - - /* - * bigint bitwise OR: just perform a bitwise OR on all digits. - */ - bigint & operator |=(bigint const &x) - { - for (unsigned int i = 0; i < N; ++i) - m_digits[i] |= x.m_digits[i]; - return *this; - } - - inline bigint operator |(bigint const &x) const - { - return bigint(*this) |= x; - } - - /* - * bigint bitwise XOR: just perform a bitwise XOR on all digits. - */ - bigint & operator ^=(bigint const &x) - { - for (unsigned int i = 0; i < N; ++i) - m_digits[i] ^= x.m_digits[i]; - return *this; - } - - inline bigint operator ^(bigint const &x) const - { - return bigint(*this) ^= x; - } - - /* - * bigint unary plus: a no-op - */ - inline bigint operator +() const - { - return *this; - } - - /* - * bigint unary minus: flip bits and add one - */ - bigint operator -() const - { - bigint 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 - bigint<((N > M) ? N : M), T> operator +(bigint 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 - bigint<((N > M) ? N : M), T> operator -(bigint 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 - bigint operator *(bigint 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 const &x) const - { - return m_digits == x.m_digits; - } - - inline bool operator !=(bigint 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 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 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 const &x) const - { - return !(*this < x); - } - - inline bool operator <=(bigint 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 - inline bigint &slice() - { - return *reinterpret_cast *>((T *)this + A); - } - - template - inline bigint const &slice() const - { - return *reinterpret_cast const *>((T const *)this + A); - } - -private: - /* Allow other types of bigints to access our private members */ - template friend class bigint; - - inline bool is_negative() const - { - if (N < 1) - return false; - return (m_digits[N - 1] >> (bits_per_digit - 1)) != 0; - } - - template - typename std::enable_if<(N != M || (N > 1 && N < 64)), bigint> - ::type inline multiply(bigint const &b) const - { - /* FIXME: other digit sizes are not supported */ - static_assert(sizeof(T) <= sizeof(uint32_t), "ensure T is uint32_t"); - - bigint 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 - typename std::enable_if<(N == M) && (N >= 64), bigint> - ::type inline multiply(bigint const &b) const - { - bigint<2 * N, T> ret, tmp(0); - - bigint const &a0 = slice<0, N / 2>(); - bigint const &a1 = slice(); - bigint const &b0 = b.template slice<0, N / 2>(); - bigint const &b1 = b.template slice(); - bigint &r0 = ret.template slice<0, N>(); - bigint &r1 = ret.template slice(); - bigint &r2 = ret.template slice(); - r0 = a0 * b0; - r2 = a1 * b1; - /* FIXME: check for overflows here */ - r1 = (a0 + a1) * (b0 + b1) - r0 - r2; - - return ret + tmp; - } - - template - typename std::enable_if<(N == M) && (N == 1), bigint> - ::type inline multiply(bigint 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 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 */ - diff --git a/src/lol/math/constants.h b/src/lol/math/constants.h deleted file mode 100644 index 49185987..00000000 --- a/src/lol/math/constants.h +++ /dev/null @@ -1,49 +0,0 @@ -// -// Lol Engine -// -// Copyright © 2010—2015 Sam Hocevar -// -// 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 - -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 */ - diff --git a/src/t/math/bigint.cpp b/src/t/math/bigint.cpp index 1d9360ac..63be0876 100644 --- a/src/t/math/bigint.cpp +++ b/src/t/math/bigint.cpp @@ -1,7 +1,7 @@ // // Lol Engine — Unit tests // -// Copyright © 2010—2015 Sam Hocevar +// Copyright © 2010—2020 Sam Hocevar // // 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 +#include #include 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) diff --git a/src/t/math/polynomial.cpp b/src/t/math/polynomial.cpp index 2a95c65d..720ac7e3 100644 --- a/src/t/math/polynomial.cpp +++ b/src/t/math/polynomial.cpp @@ -12,7 +12,8 @@ #include -#include +#include +#include #include namespace lol diff --git a/src/t/math/real.cpp b/src/t/math/real.cpp index 823eabba..6a31d864 100644 --- a/src/t/math/real.cpp +++ b/src/t/math/real.cpp @@ -1,7 +1,7 @@ // // Lol Engine — Unit tests // -// Copyright © 2010—2019 Sam Hocevar +// Copyright © 2010—2020 Sam Hocevar // // 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 -#include +#include #include #include