Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add constexpr to Vector{2,3,4} and Vector<N, T> #597

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Magnum/Math/BitVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#ifndef CORRADE_NO_DEBUG
#include <Corrade/Utility/Debug.h>
#endif
#include <Corrade/Utility/Macros.h>

#include "Magnum/Types.h"
#include "Magnum/Math/Math.h"
Expand Down Expand Up @@ -174,14 +175,14 @@ template<std::size_t size> class BitVector {
* Equivalent to @ref all().
* @see @ref any(), @ref none()
*/
explicit operator bool() const { return all(); }
explicit MAGNUM_CONSTEXPR14 operator bool() const { return all(); }

/**
* @brief Whether all bits are set
*
* @see @ref none(), @ref any(), @ref operator bool()
*/
bool all() const;
MAGNUM_CONSTEXPR14 bool all() const;

/**
* @brief Whether no bits are set
Expand Down Expand Up @@ -361,7 +362,7 @@ template<std::size_t size> inline bool BitVector<size>::operator==(const BitVect
return true;
}

template<std::size_t size> inline bool BitVector<size>::all() const {
template<std::size_t size> MAGNUM_CONSTEXPR14 inline bool BitVector<size>::all() const {
/* Check all full segments */
for(std::size_t i = 0; i != size/8; ++i)
if(_data[i] != FullSegmentMask) return false;
Expand Down
13 changes: 13 additions & 0 deletions src/Magnum/Math/Math.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,17 @@ namespace Implementation {

}}

// TODO remove this once corrade#152 gets merged
#if defined __cpp_constexpr && __cpp_constexpr >= 201304 || \
defined CORRADE_TARGET_MSVC && _MSC_VER >= 1910 && CORRADE_CXX_STANDARD >= 201402L
#define MAGNUM_CONSTEXPR14 constexpr
#else
#define MAGNUM_CONSTEXPR14
#endif
#if defined(__clang__) && __clang_major__ >= 9 || defined(__GNUG__) && __GNUG__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1931
#define MAGNUM_CONSTEVAL (__builtin_is_constant_evaluated())
#elif CORRADE_CXX_STANDARD >= 202002L
#define MAGNUM_CONSTEVAL (std::is_constant_evaluated())
#endif

#endif
7 changes: 7 additions & 0 deletions src/Magnum/Math/Test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ corrade_add_test(MathVectorTest VectorTest.cpp LIBRARIES MagnumMathTestLib)
corrade_add_test(MathVector2Test Vector2Test.cpp LIBRARIES MagnumMathTestLib)
corrade_add_test(MathVector3Test Vector3Test.cpp LIBRARIES MagnumMathTestLib)
corrade_add_test(MathVector4Test Vector4Test.cpp LIBRARIES MagnumMathTestLib)

foreach(_test VectorTest Vector2Test Vector3Test Vector4Test)
corrade_add_test(Cpp14Math${_test} ${_test}.cpp LIBRARIES MagnumMathTestLib)
set_target_properties(Cpp14Math${_test} PROPERTIES CORRADE_CXX_STANDARD 14)
target_compile_definitions(Cpp14Math${_test} PRIVATE TESTING_CONSTEXPR CORRADE_GRACEFUL_ASSERT)
endforeach()

corrade_add_test(MathColorTest ColorTest.cpp LIBRARIES MagnumMathTestLib)

corrade_add_test(MathRectangularMatrixTest RectangularMatrixTest.cpp LIBRARIES MagnumMathTestLib)
Expand Down
14 changes: 14 additions & 0 deletions src/Magnum/Math/Test/Cpp14VectorTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef Magnum_Math_Test_Cpp14Vector
#define Magnum_Math_Test_Cpp14Vector

#define CE
#ifdef TESTING_CONSTEXPR
#if __cpp_constexpr >= 201304
#undef CE
#define CE constexpr
#else
#define SKIP_TESTING
#endif
#endif

#endif
34 changes: 29 additions & 5 deletions src/Magnum/Math/Test/Vector2Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include "Magnum/Math/StrictWeakOrdering.h"
#include "Magnum/Math/Swizzle.h"

#include "Cpp14VectorTest.h"

struct Vec2 {
float x, y;
};
Expand All @@ -57,6 +59,7 @@ namespace Test { namespace {
struct Vector2Test: Corrade::TestSuite::Tester {
explicit Vector2Test();

#ifndef SKIP_TESTING
void construct();
void constructDefault();
void constructNoInit();
Expand All @@ -76,14 +79,24 @@ struct Vector2Test: Corrade::TestSuite::Tester {

void swizzleType();
void debug();
#else
void skipTesting();
#endif
};

typedef Math::Vector3<Int> Vector3i;
typedef Math::Vector2<Float> Vector2;
typedef Math::Vector2<Int> Vector2i;

Vector2Test::Vector2Test() {
addTests({&Vector2Test::construct,
#ifndef TESTING_CONSTEXPR
setTestName("MathVector2Test");
#else
setTestName("Cpp14MathVector2Test");
#endif
addTests({
#ifndef SKIP_TESTING
&Vector2Test::construct,
&Vector2Test::constructDefault,
&Vector2Test::constructNoInit,
&Vector2Test::constructOneValue,
Expand All @@ -101,9 +114,15 @@ Vector2Test::Vector2Test() {
&Vector2Test::strictWeakOrdering,

&Vector2Test::swizzleType,
&Vector2Test::debug});
&Vector2Test::debug
#else
&Vector2Test::skipTesting
#endif
});
}

#ifndef SKIP_TESTING

void Vector2Test::construct() {
constexpr Vector2 a = {1.5f, 2.5f};
CORRADE_COMPARE(a, (Vector<2, Float>(1.5f, 2.5f)));
Expand Down Expand Up @@ -197,7 +216,7 @@ void Vector2Test::convert() {
}

void Vector2Test::access() {
Vector2 vec(1.0f, -2.0f);
CE Vector2 vec(1.0f, -2.0f);
CORRADE_COMPARE(vec.x(), 1.0f);
CORRADE_COMPARE(vec.y(), -2.0f);

Expand All @@ -209,8 +228,8 @@ void Vector2Test::access() {
}

void Vector2Test::cross() {
Vector2i a(1, -1);
Vector2i b(4, 3);
CE Vector2i a(1, -1);
CE Vector2i b(4, 3);

CORRADE_COMPARE(Math::cross(a, b), 7);
CORRADE_COMPARE(Math::cross<Int>({a, 0}, {b, 0}), Vector3i(0, 0, Math::cross(a, b)));
Expand Down Expand Up @@ -268,6 +287,11 @@ void Vector2Test::debug() {
Debug(&o) << Vector2(0.5f, 15.0f);
CORRADE_COMPARE(o.str(), "Vector(0.5, 15)\n");
}
#else
void Vector2Test::skipTesting() {
CORRADE_SKIP("Relaxed constexpr not supported by the compiler.");
}
#endif

}}}}

Expand Down
37 changes: 29 additions & 8 deletions src/Magnum/Math/Test/Vector3Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
DEALINGS IN THE SOFTWARE.
*/

#include "Cpp14VectorTest.h"

#include <sstream>
#include <Corrade/TestSuite/Tester.h>
#include <Corrade/Utility/DebugStl.h>
Expand Down Expand Up @@ -57,6 +59,7 @@ namespace Test { namespace {
struct Vector3Test: Corrade::TestSuite::Tester {
explicit Vector3Test();

#ifndef SKIP_TESTING
void construct();
void constructDefault();
void constructNoInit();
Expand All @@ -76,14 +79,24 @@ struct Vector3Test: Corrade::TestSuite::Tester {

void swizzleType();
void debug();
#else
void skipTesting();
#endif
};

typedef Math::Vector3<Float> Vector3;
typedef Math::Vector3<Int> Vector3i;
typedef Math::Vector2<Float> Vector2;

Vector3Test::Vector3Test() {
addTests({&Vector3Test::construct,
#ifndef TESTING_CONSTEXPR
setTestName("MathVector3Test");
#else
setTestName("Cpp14MathVector3Test");
#endif
addTests({
#ifndef SKIP_TESTING
&Vector3Test::construct,
&Vector3Test::constructDefault,
&Vector3Test::constructNoInit,
&Vector3Test::constructOneValue,
Expand All @@ -101,9 +114,13 @@ Vector3Test::Vector3Test() {
&Vector3Test::strictWeakOrdering,

&Vector3Test::swizzleType,
&Vector3Test::debug});
&Vector3Test::debug
#else
&Vector3Test::skipTesting
#endif
});
}

#ifndef SKIP_TESTING
void Vector3Test::construct() {
constexpr Vector3 a = {1.0f, 2.5f, -3.0f};
CORRADE_COMPARE(a, (Vector<3, Float>(1.0f, 2.5f, -3.0f)));
Expand Down Expand Up @@ -206,7 +223,7 @@ void Vector3Test::convert() {
}

void Vector3Test::access() {
Vector3 vec(1.0f, -2.0f, 5.0f);
CE Vector3 vec(1.0f, -2.0f, 5.0f);
CORRADE_COMPARE(vec.x(), 1.0f);
CORRADE_COMPARE(vec.r(), 1.0f);
CORRADE_COMPARE(vec.y(), -2.0f);
Expand All @@ -230,8 +247,8 @@ void Vector3Test::access() {
}

void Vector3Test::cross() {
Vector3i a(1, -1, 1);
Vector3i b(4, 3, 7);
CE Vector3i a(1, -1, 1);
CE Vector3i b(4, 3, 7);

CORRADE_COMPARE(Math::cross(a, b), Vector3i(-10, -3, 7));
}
Expand All @@ -255,7 +272,7 @@ void Vector3Test::scales() {
}

void Vector3Test::twoComponent() {
Vector3 a(1.0f, 2.0f, 3.0f);
CE Vector3 a(1.0f, 2.0f, 3.0f);
CORRADE_COMPARE(a.xy(), Vector2(1.0f, 2.0f));

constexpr Vector3 b(1.0f, 2.0f, 3.0f);
Expand Down Expand Up @@ -292,7 +309,11 @@ void Vector3Test::debug() {
Debug(&o) << Vector3(0.5f, 15.0f, 1.0f);
CORRADE_COMPARE(o.str(), "Vector(0.5, 15, 1)\n");
}

#else
void Vector3Test::skipTesting() {
CORRADE_SKIP("Relaxed constexpr not supported by the compiler.");
}
#endif
}}}}

CORRADE_TEST_MAIN(Magnum::Math::Test::Vector3Test)
Loading