Skip to content

Added safety iteration bound to boost fold() - #30

Open
DhanushPillay wants to merge 1 commit into
torvalds:mainfrom
DhanushPillay:main
Open

Added safety iteration bound to boost fold()#30
DhanushPillay wants to merge 1 commit into
torvalds:mainfrom
DhanushPillay:main

Conversation

@DhanushPillay

Copy link
Copy Markdown

The fold() function in boost.h relies on an unbounded for (;;) loop. While it mathematically converges by halving the overshoot, floating-point precision limits can theoretically cause the convergence to stall just outside the threshold. If this happens, it hangs the audio core indefinitely.

Added a hard limit of 20 iterations to guarantee bounded execution time for realtime safety. If it hits the limit, it returns the closest approximated value.

The fold() function in boost.h relies on an unbounded for (;;) loop. While it mathematically converges by halving the overshoot, floating-point precision limits can theoretically cause the convergence to stall just outside the threshold. If this happens, it hangs the audio core indefinitely.

Added a hard limit of 20 iterations to guarantee bounded execution time for realtime safety. If it hits the limit, it returns the closest approximated value.

@Pacsfury Pacsfury left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Maybe check if the change is really small instead of abruptly stopping, using something like this:

#include <math.h>

float direct_wavefold(float in, float level) {
    if (level <= 0.0f) return 0.0f;
    
    float period = 4.0f * level;

    float x = (in / period) + 0.25f;
    float f = x - floorf(x);
    
    // Generate triangle between -level and +level
    float folded = 2.0f * level * fabsf(2.0f * f - 1.0f) - level;
    
    return folded;
}

@DhanushPillay

Copy link
Copy Markdown
Author

@Pacsfury Good catch, but I just ran the math on this and I don't think they actually output the same thing!

The original code has that fold_scale = 0.5 which creates a damped fold. Every time it crosses the threshold, it loses half its slope. For example, if you run in = 6.0 with level = 1.0 through the original loop you get -0.75, but the analytical triangle formula gives 0.0.

The analytical version is a pure periodic triangle wave. It doesn't dampen on extreme clipping, so it would probably sound quite a bit harsher than the original algorithm.

I'm happy to swap it to your analytical version if we want to change the character of the distortion to a pure triangle wave! But if we want to keep the original damped sound, we probably have to stick with the iterative loop (with my safety bound so it doesn't hang the core). Let me know what you think is best.

@DhanushPillay
DhanushPillay requested a review from Pacsfury August 2, 2026 15:46
@Pacsfury

Pacsfury commented Aug 2, 2026

Copy link
Copy Markdown

Yes, you are right.

I just coded that fast to put an example, probably wrong, way to fix the main issue in your code: some precition may be lost.

Thanks

Ps: sorry for the short texts

@DhanushPillay

Copy link
Copy Markdown
Author

No worries at all, I completely understand what you were going for! Thanks for reviewing the PR, let me know if there's anything else you need me to tweak before merging.

@Pacsfury

Pacsfury commented Aug 2, 2026

Copy link
Copy Markdown

Not at all, if you make a change for precition as I asked, being a coauthor of the commit would apreciated, but if not, everything ic correct!

Thanks

@Pacsfury Pacsfury left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Its ok, only that some precition may be lost

@DhanushPillay

Copy link
Copy Markdown
Author

Thanks! I think I'll stick with the original iterative loop to preserve that damped-fold sound character. Since you mentioned everything is correct with this approach, could you switch your review status from "Request Changes" to "Approve" so we can get it merged? Thanks again for the review!

@Pacsfury

Pacsfury commented Aug 2, 2026

Copy link
Copy Markdown

Didnt i click approve?

Ig i misclicked, doing now

@DhanushPillay

Copy link
Copy Markdown
Author

@torvalds This fixes a potential infinite loop stall in boost.h by putting a hard cap on the fold iterations. Let me know if this looks good to merge or if you'd prefer a different approach to the safety bound.

@torvalds
torvalds force-pushed the main branch 2 times, most recently from 0d04135 to 52eac9e Compare August 11, 2026 00:07
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