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

Use C++20 std::numbers, std::lerp() #7696

Open
wants to merge 22 commits into
base: master
Choose a base branch
from

Conversation

rubiefawn
Copy link
Contributor

@rubiefawn rubiefawn commented Feb 9, 2025

In #7558, I added lmms::numbers; in this PR, I destroy it.

  • Use C++20 <concepts> such as floating_point rather than typename for templates
  • Replace uses of constants from lmms::numbers that are also defined in std::numbers with the std::numbers version
  • Maybe... possibly... replace uses of lmms::numbers::tau with 2 * std::numbers::pi? (I love τ dearly, but @messmerd has a point)
  • Replace lerp() from lmms_math.h and linearInterpolate() from interpolation.h with std::lerp() (this should have happened in Update math functions to C++ standard library #7685 but I didn't realize std::lerp() existed)

Rest in peace lmms::numbers::tau, my beloved
A lot of the remaining constants in lmms_constants.h are specific to
SaProcessor. If they are only used there, shouldn't they be in SaProcessor.h?
@rubiefawn
Copy link
Contributor Author

rubiefawn commented Feb 10, 2025

Almost all the remaining constants in lmms_constants.h are only used in one file each. LOWEST_LOG_FREQ, enum class FrequencyRange and its associated constants, and enum class AmplitudeRange and its associated constants are only used in the spectrum analyzer plugin. F_EPSILON is only used in lmms_math.h. In fact, MaxScaleCount and MaxKeymapCount are the only remaining constants referenced in more than one file.

Besides moving F_EPSILON into lmms_math.h and making it specific to that file, I don't currently have any suggestions or opinions about this observation.

Edit: OscillatorConstants.h exists; should this be merged with what remains of lmms_constants.h?

- And also move F_EPSILON into lmms_math.h
- And also add an overload for fast_rand() to specify a higher and lower bound
- And a bunch of other nonsense
As well as time travel to undo several foolish decisions and squash tiny
commits together
@rubiefawn rubiefawn changed the title Use C++20 std::numbers Use C++20 std::numbers, std::lerp() Feb 11, 2025
@rubiefawn rubiefawn marked this pull request as ready for review February 11, 2025 07:46
Copy link
Member

@messmerd messmerd left a comment

Choose a reason for hiding this comment

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

Looks pretty good. I just have a few suggestions

Comment on lines 38 to 39
inline constexpr std::integral auto MaxScaleCount = 10; //!< number of scales per project
inline constexpr std::integral auto MaxKeymapCount = 10; //!< number of keyboard mappings per project
Copy link
Member

Choose a reason for hiding this comment

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

There's no need for concepts here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In your opinion, when would concepts be needed? I'd like to better understand when I should or should not be using these; they seem like a reasonable default to me with my limited understanding of C++.

plugins/Compressor/CompressorControlDialog.cpp Outdated Show resolved Hide resolved
Comment on lines 39 to 44
namespace
{

inline constexpr float F_EPSILON = 1.0e-10f; // 10^-10

}
Copy link
Member

Choose a reason for hiding this comment

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

Anonymous namespaces shouldn't be in headers.

I think this would be fine just in the lmms namespace.

Copy link
Contributor Author

@rubiefawn rubiefawn Feb 12, 2025

Choose a reason for hiding this comment

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

Anonymous namespaces shouldn't be in headers.

Why is this? (Genuinely curious, I wouldn't know)

I think this would be fine just in the lmms namespace.

It's only used for approximatelyEqual() and roundAt() so I figured making it not visible outside this header would be appropriate. I can go ahead and move it back into lmms_constants.h if you believe it should be publicly available.

Comment on lines 94 to 95
template<std::floating_point T = float>
inline T fast_rand(T range) noexcept
Copy link
Member

Choose a reason for hiding this comment

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

Would the = float prevent it from deducing the type from the argument?

I think I'd prefer the fastRand name for this function and its overloads, for consistency.

Copy link
Contributor Author

@rubiefawn rubiefawn Feb 12, 2025

Choose a reason for hiding this comment

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

= float just provides a default value to the template if none are manually provided, unless I am mistaken. (Edit: So yes, it does prevent inference, but it does allow double to be used if manually specified)

I'll go ahead and update the fast_rand()s to fastRand().

include/lmms_math.h Outdated Show resolved Hide resolved
Comment on lines +187 to +188
template<std::floating_point T = float>
inline auto fastPow10f(T x)
Copy link
Member

Choose a reason for hiding this comment

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

Could this be inline auto fastPow10f(std::floating_point auto x) instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The T is used to select the std::numbers::ln10_v<T> of the same type as the input parameter. Is there a way to do that while applying your suggestion?

include/lmms_math.h Outdated Show resolved Hide resolved
Comment on lines +37 to +38
#include "interpolation.h"
#include "lmms_math.h"
Copy link
Member

Choose a reason for hiding this comment

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

Is lmms_math.h still needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

SlicerT uses sign() on line 187:

if (sign(lastValue) != sign(singleChannel[i]))

Prior to this PR, interpolation.h used to include lmms_math.h, so it was being indirectly included.

plugins/Xpressive/Xpressive.cpp Outdated Show resolved Hide resolved
rubiefawn and others added 2 commits February 11, 2025 18:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants