Skip to content

Commit 97fd126

Browse files
author
msftbot[bot]
authored
Improved RuntimeHelpers.ConvertLength codegen (#3608)
<!-- 🚨 Please Do Not skip any instructions and information mentioned below as they are all required and essential to evaluate and test the PR. By fulfilling all the required information you will be able to reduce the volume of questions and most likely help merge the PR faster 🚨 --> <!-- 📝 It is preferred if you keep the "☑️ Allow edits by maintainers" checked in the Pull Request Template as it increases collaboration with the Toolkit maintainers by permitting commits to your PR branch (only) created from your fork. This can let us quickly make fixes for minor typos or forgotten StyleCop issues during review without needing to wait on you doing extra work. Let us help you help us! 🎉 --> ## Follow up from #3520 <!-- Add the relevant issue number after the "#" mentioned above (for ex: Fixes #1234) which will automatically close the issue once the PR is merged. --> <!-- Add a brief overview here of the feature/bug & fix. --> ## PR Type What kind of change does this PR introduce? <!-- Please uncomment one or more that apply to this PR. --> - Optimization <!-- - Bugfix --> <!-- - Feature --> <!-- - Code style update (formatting) --> <!-- - Refactoring (no functional changes, no api changes) --> <!-- - Build or CI related changes --> <!-- - Documentation content changes --> <!-- - Sample app changes --> <!-- - Other... Please describe: --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying, or link to a relevant issue. --> The codegen for the second branch in `RuntimeHelpers.ConvertLength` does a signed division: https://github.com/windows-toolkit/WindowsCommunityToolkit/blob/9b75c9f910f999834c64bd00e522d7ae464c121b/Microsoft.Toolkit.HighPerformance/Helpers/Internals/RuntimeHelpers.cs#L43-L46 This is not the best for the codegen, as the JIT has to handle the sign in that division, resulting in the following: ```asm ; [System.Byte, System.Private.CoreLib],[System.Numerics.Vector4, System.Numerics.Vectors] ConvertLength[TFrom, TTo](Int32) L0000: mov eax, ecx L0002: sar eax, 0x1f L0005: and eax, 0xf L0008: add eax, ecx L000a: sar eax, 4 L000d: ret ``` ## What is the new behavior? <!-- Describe how was this issue resolved or changed? --> Avoided that with a cast to `uint`, since the length is guaranteed to be a positive value in `[0, int.MaxValue]` anyway: ```asm ; [System.Byte, System.Private.CoreLib],[System.Numerics.Vector4, System.Numerics.Vectors] L0000: mov eax, ecx L0002: shr eax, 4 L0005: ret ``` Perfect! 😄🎉 ## PR Checklist Please check if your PR fulfills the following requirements: - [X] Tested code with current [supported SDKs](../readme.md#supported) - [ ] ~~Pull Request has been submitted to the documentation repository [instructions](..\contributing.md#docs). Link: <!-- docs PR link -->~~ - [ ] ~~Sample in sample app has been added / updated (for bug fixes / features)~~ - [ ] ~~Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https://github.com/windows-toolkit/WindowsCommunityToolkit-design-assets)~~ - [X] Tests for the changes have been added (for bug fixes / features) (if applicable) - [X] Header has been added to all new source files (run *build/UpdateHeaders.bat*) - [X] Contains **NO** breaking changes
2 parents e0a5d28 + e2e1c64 commit 97fd126

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

Microsoft.Toolkit.HighPerformance/Helpers/Internals/RuntimeHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static unsafe int ConvertLength<TFrom, TTo>(int length)
4242
}
4343
else if (sizeof(TFrom) == 1)
4444
{
45-
return length / sizeof(TTo);
45+
return (int)((uint)length / (uint)sizeof(TTo));
4646
}
4747
else
4848
{

0 commit comments

Comments
 (0)