Skip to content

Conversation

@stephentoub
Copy link
Member

@stephentoub stephentoub requested a review from EgorBo August 10, 2025 02:09
Copilot AI review requested due to automatic review settings August 10, 2025 02:09
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR removes unsafe code from the Log2SoftwareFallback method by replacing a manual unsafe memory access with a simple array indexing operation. The change simplifies the code while maintaining the same functionality for computing logarithm base 2 using a de Bruijn sequence lookup table.

Key Changes

  • Replaces Unsafe.AddByteOffset with direct array indexing
  • Removes unnecessary MemoryMarshal.GetReference call
  • Eliminates manual IntPtr casting

Copy link
Member

@tannergooding tannergooding left a comment

Choose a reason for hiding this comment

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

This should be fine since all platforms have a baseline accelerated path.

We might, however, want to double check Mono to ensure that Arm and x64 isn't regressed for them.

The JIT could probably elide the bounds check itself here, but it would require it understanding that x >> 27 produces something where only the lower 5 bits can be set and so therefore must be in the range [0, 31]. Might be worth logging an issue for that as part of this.

@stephentoub
Copy link
Member Author

The JIT could probably elide the bounds check itself here, but it would require it understanding that x >> 27 produces something where only the lower 5 bits can be set and so therefore must be in the range [0, 31]. Might be worth logging an issue for that as part of this.

Isn't that what #109900 does?

@stephentoub stephentoub merged commit 908c827 into dotnet:main Aug 10, 2025
139 checks passed
@stephentoub stephentoub deleted the log2unsafe branch August 10, 2025 18:10
@tannergooding
Copy link
Member

Isn't that what #109900 does?

looks like it. I forgot we landed that this release

@github-actions github-actions bot locked and limited conversation to collaborators Sep 10, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants