Skip to content

Conversation

@tomcur
Copy link
Member

@tomcur tomcur commented Sep 14, 2025

This is a branchless calculation of a point-onto-line-segment projection. Also marks the method #[inline] and adds some testing.

I started looking at this because of the math we introduced in linebender/vello#1214. This might allow us to introduce some tighter bounds there without introducing additional branching.

This should be useful here regardless of whether the above happens.

// Clamp the parameter to be on the line segment. This results in `t==0` if `t==inf` above.
#[expect(
clippy::manual_clamp,
reason = "`clamp` uses slightly more instructions than chained `max` and `min` on x86 and aarch64"
Copy link
Member Author

Choose a reason for hiding this comment

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

aarch64: https://godbolt.org/z/Mv4Y19jrM
x86: https://godbolt.org/z/WW19hMd6v

Perhaps we should disable this lint in kurbo.

Copy link
Member

Choose a reason for hiding this comment

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

Huh. I guess clamp can't really be improved because of how its NaN behavior is specified.

Copy link
Member Author

Choose a reason for hiding this comment

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

Seems that way! (As a separate note, we actually use some of the NaN semantics of the chained-min-max in the analytical pixel coverage calculation in the sparse strips work, so this lint can be a bit noisy.)

tomcur and others added 5 commits September 19, 2025 10:51
This is a branchless calculation of a point-onto-line-segment
projection. Also marks the method `#[inline]`.

I started looking at this because of the math we introduced in
linebender/vello#1214. This might allow us to
introduce some tighter bounds there without introducing additional
branching.

This should be useful here regardless of whether the above happens.
Co-authored-by: jneem <joeneeman@gmail.com>
@tomcur tomcur force-pushed the branchless-line-nearest branch from fb967fb to e8baf8a Compare September 19, 2025 08:54
@tomcur tomcur added this pull request to the merge queue Sep 19, 2025
Merged via the queue into linebender:main with commit f37816a Sep 19, 2025
15 checks passed
@tomcur tomcur deleted the branchless-line-nearest branch September 19, 2025 08:59
github-merge-queue bot pushed a commit that referenced this pull request Sep 20, 2025
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