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

[c++20] Rewrite Vector, Location2D, Matrix and LUDecomposition #782

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

TomSaw
Copy link
Contributor

@TomSaw TomSaw commented Nov 15, 2021

This is an excerpt from #665.

modm::Vector<T, 2> is passed as template-parameter Resolution to modm::graphic::Buffer<..>
-> Thus the constructors require constexpr

By the chance, i may give some maintenance to all of these vector files.
Just modernization, no changes in logic C++20 flavoured, more feature complete and 100% backwards compatible.

Key changes

  • Constexpr constructors everywhere
  • using <algorithm> everywhere (did i miss an oportunity? let me know!)
  • Applying std::round whenever float->integral conversion occurs
    • GeometricTraits<T>::round() could be removed
  • [[depricated]] all Vector<>::convert() in fave of fresh conversion constructors .
  • Added some missing routines here and there, f.e IOStream operator<<
  • Improved readability
    • using declaratives for local instances of Vector<T, N>

    • (Re)assorted methods

    • Migrated all method definitions into vector{N}.hpp

TODO

  • Re-add Vector<T, 2>::convert() and Location2D<U>::convert() for backwards-comp and depricate it
  • Treat deprecations
  • geometric_traits.hpp avr specialisation
  • Simplify the Vector tests

@TomSaw TomSaw changed the title constexpr for Vector<2> constructors [math:vector] constexpr for Vector<2> constructors Nov 15, 2021
@TomSaw TomSaw changed the title [math:vector] constexpr for Vector<2> constructors [math:vector] constexpr constructors Nov 15, 2021
@TomSaw TomSaw mentioned this pull request Nov 15, 2021
20 tasks
@salkinium
Copy link
Member

Yes please. Constexpr constructors are particularly useful as they reduce to constinit .data section to be copied on startup instead of constructed by instructions.

@TomSaw
Copy link
Contributor Author

TomSaw commented Nov 15, 2021

Vector and Matrix use uint8_t as template and function parameter type for size metrics.
Shouldn't this be std::size_t or would it be an performance drawback when used as function parameter?

I think it may be desired to restrict these metrics to 0-255.

@chris-durand
Copy link
Member

chris-durand commented Nov 15, 2021

Vector and Matrix use uint8_t as template and function parameter type for size metrics.
Shouldn't this be std::size_t or would it be an performance drawback when used as function parameter?

That code was written for 8 bit AVR more than ten years ago. On those there is a penalty since arithmetic registers are 8 bit only but size_t is 16 bit. std::size_t is the right idiomatic choice. I guess std::size_t would be fine for operator[] on AVR. The compiler will inline it anyway.

@TomSaw
Copy link
Contributor Author

TomSaw commented Nov 15, 2021

Vector and Matrix use uint8_t as template and function parameter type for size metrics.
Shouldn't this be std::size_t or would it be an performance drawback when used as function parameter?

That code was written for 8 bit AVR more than ten years ago. On those there is a penalty since arithmetic registers are 8 bit only but size_t is 16 bit. std::size_t is the right idiomatic choice. I guess std::size_t would be fine for operator[] on AVR. The compiler will inline it anyway.

Great, so i'll take this to 2020!

@TomSaw TomSaw changed the title [math:vector] constexpr constructors [math:vector] C++20 housekeeping Nov 16, 2021
@TomSaw TomSaw changed the title [math:vector] C++20 housekeeping [c++20] housekeeping math:vector Nov 16, 2021
@TomSaw TomSaw force-pushed the fix/constexpr-math-vector branch 9 times, most recently from 55ce405 to cab8d18 Compare November 16, 2021 21:21
@TomSaw
Copy link
Contributor Author

TomSaw commented Nov 16, 2021

Got some CI claims @windows only but no machine around..
Is there a smarter way than setting up a win-VM @salkinium ???

@chris-durand
Copy link
Member

There are the same errors on Linux. You could be lucky and not need Windows to debug this.

@TomSaw TomSaw force-pushed the fix/constexpr-math-vector branch 8 times, most recently from 5760b8b to 11a5c8a Compare November 18, 2021 13:13
@TomSaw TomSaw force-pushed the fix/constexpr-math-vector branch 8 times, most recently from 7582629 to a6f7724 Compare January 27, 2022 21:01
Copy link
Contributor Author

@TomSaw TomSaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a complete rewrite, maybe a little too radical but feels like natural: modm::Vector is now an std::array with support for arithmetic operations.
I've benchmarked against the original code and found the exact same - in some cases even faster - execution times.

src/modm/math/geometry/vector.hpp Show resolved Hide resolved
src/modm/math/geometry/vector.hpp Outdated Show resolved Hide resolved
src/modm/math/geometry/vector.hpp Show resolved Hide resolved
Comment on lines 172 to 180

// no clue why, but RVO / Loop unrolling is not applied for this ...
// std::transform(this->begin(), this->end(), other.begin(), res.begin(), Func{});

// ... but this optimizes perfectly.
res.x() = BinFunc{}(this->x(), other.x());
if constexpr(N > 1) res.y() = BinFunc{}(this->y(), other.y());
if constexpr(N > 2) res.z() = BinFunc{}(this->z(), other.z());
if constexpr(N > 3) res.w() = BinFunc{}(this->w(), other.w());
if constexpr(N > 4) res.v() = BinFunc{}(this->v(), other.v());
if constexpr(N > 5) res.u() = BinFunc{}(this->u(), other.u());

Copy link
Contributor Author

@TomSaw TomSaw Jan 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For operators returning a new Vector, the <algorithm>-path did not optimize sufficiently (Line:174) - execution time has tripled. I've found a workaround (Line:177-182) but there's for sure a more elegant solution.

src/modm/math/geometry/vector.hpp Outdated Show resolved Hide resolved
test/modm/math/geometry/vector3_test.cpp Show resolved Hide resolved
@TomSaw TomSaw force-pushed the fix/constexpr-math-vector branch 2 times, most recently from b57e8ed to 9af3c48 Compare January 28, 2022 20:32
@chris-durand
Copy link
Member

@TomSaw I am quite busy with work right now, but will try to review tomorrow.

Copy link
Member

@chris-durand chris-durand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for all the effort you put into this. I got about halfway through your changes and will have a more detailed look when I find more time to do so.

src/modm/math/geometry/vector.hpp Outdated Show resolved Hide resolved
src/modm/math/geometry/vector.hpp Outdated Show resolved Hide resolved
src/modm/math/geometry/line_2d_impl.hpp Outdated Show resolved Hide resolved
src/modm/math/geometry/line_segment_2d.hpp Outdated Show resolved Hide resolved
src/modm/math/geometry/location_2d.hpp Outdated Show resolved Hide resolved
Comment on lines 67 to 68
position == other.position and
std::abs(orientation - other.orientation) < __FLT_EPSILON__
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can position be float? Should we handle that case somehow?

Not sure if __FLT_EPSILON__ is the right thing to do. For numbers between 1.0 and 2.0 it's the distance between adjacent floats. For numbers greater than 2.0 the comparison will do the same thing as ==.

src/modm/math/geometry/location_2d.hpp Outdated Show resolved Hide resolved
src/modm/math/matrix.hpp Outdated Show resolved Hide resolved
src/modm/math/matrix.hpp Outdated Show resolved Hide resolved
src/modm/math/geometry/vector.hpp Outdated Show resolved Hide resolved
@TomSaw
Copy link
Contributor Author

TomSaw commented Feb 1, 2022

Oops... due to my vector deep dive, i've forgot to mention 🤦‍♂️:
Haven't yet taken the same care for these other geometry classes like i did for vector! This PRs title is kinda misleading.

If you find another minute, better take a second look on vector.hpp so that the new design is approved.
I'll take your review comments as inspirational boost into a last session with the other geometry candidates.

My pleasure. Also thanks for taking the time to review my code and support my practice! Win-win, i 💙 open-source.

@TomSaw TomSaw force-pushed the fix/constexpr-math-vector branch 8 times, most recently from 480cf7a to 291a456 Compare March 10, 2022 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

3 participants