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

Scsynth & Plugin: Remove c cast from CALCSCOPE and fix GVerbs roomsize. #6262

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

Conversation

JordanHendersonMusic
Copy link
Contributor

@JordanHendersonMusic JordanHendersonMusic commented Apr 21, 2024

Purpose and Motivation

This decltype...

float a[12];
decltype( a[0] );

...returns a referenced type, e.g. float&, not float.
The macro sc_typeof_cast and CALCSCOPE used this to perform a c cast.
When converting from a double to a float, this is fine.
When converting from an double to a float&, this is very bad, and was the main issue with GVerb's roomsize.

This PR creates a new conversion function using static_cast that does the correct behaviour and will break at compile time if the user attempts to pass something unexpected.

Following the alias violations (#6245) I retested GVerb, and this stopped it from blowing up. The second commit here just adds just more static_casts and uses std::pow converting everything to doubles, which further improved it.

Fixes #2302.
Partially fixes #6245, and an outstanding issues from 2007 in GVerb's roomsize — @joshpar.

Is it possible to remove sc_typeof_cast? It is in a header that other plugins use. Is there any way to stop people from using it if it can't be removed?

Types of changes

  • Documentation
  • Bug fix

To-do list

  • Code is tested
  • All tests are passing
  • Updated documentation
  • This PR is ready for review

JordanHendersonMusic added 2 commits April 21, 2024 18:26
`decltype( a[0] )` returns a referenced type. This sc_typeof_cast used this to perform a c cast, when converting from a double to a float, producing the wrong value.

This PR creates a new conversion function using static_asserts and static_cast that does the correct behaviour and will break at compile time if the user attempts to pass something unexpected.

An example of a bug caused by this error can be found in the following PR.
…nning.

The previous commit no longer causes GVerb to blow up when the room size is dynamically changed, but reveals another issue with bursts of noise.

This commit converts all power functions to use doubles and, following clangds hints, places more static_casts.

The issue with GVerb has existed since 2007.
After this commit, GVerb still does unusual pitch shifting and introduces noise when changing the roomsize while running, but it does not blow up as it did before.
@JordanHendersonMusic JordanHendersonMusic force-pushed the topic/fix-easy-ccast-mistake-in-calcscope branch from 0857dc3 to 83cf08b Compare April 21, 2024 17:56
@JordanHendersonMusic JordanHendersonMusic added bug Issues that relate to unexpected/unwanted behavior. Don't use for PRs. comp: scsynth comp: server plugins labels Apr 21, 2024
@JordanHendersonMusic JordanHendersonMusic changed the title Remove c cast from CALCSCOPE and fix GVerbs roomsize. Scsynth & Plugin: Remove c cast from CALCSCOPE and fix GVerbs roomsize. Apr 22, 2024
Copy link
Member

@joshpar joshpar left a comment

Choose a reason for hiding this comment

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

I like this fix, but I'm worried about other guess that use this change and whether or not anything changes for them. I may have time this weekend to check for side effects.

@JordanHendersonMusic
Copy link
Contributor Author

JordanHendersonMusic commented Apr 25, 2024

whether or not anything changes for them

Is there some easy way to test this?

As far as I can tell, the only other places where an array is passed (grepping across this repo and sc3-plugins) is here in GVerb and in SkUGens:FM7, who narrowly avoids this bug by placing the array in another slot.

Perhaps limiting this to just arithmetic types is too restrictive?

Copying off cpp-ref

Arithmetic types are the built-in types for which the arithmetic operators (+, -, *, /) are defined (possibly in combination with the usual arithmetic conversions).

As far as I can tell, this means the only way something would break was if someone passed in a struct for which those operators where defined, as the macro requires '-' and '*' be defined.

I could remove the type trait checks, and instead just let static_cast deal with it.

Edit: Actualy, I think just doing the static_cast is enough here.

Previously, the cast was restricted to arithmetic types, this lifts that restriction, but will always do a copy.
@JordanHendersonMusic JordanHendersonMusic removed the bug Issues that relate to unexpected/unwanted behavior. Don't use for PRs. label Apr 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Build fails with strict-aliasing violations GVerb glitch
2 participants