<!--This is just a template - feel free to delete any and all of it and replace as appropriate.--> ### Description ```C# [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int CopySign_A1(int value, int sign) { return int.CopySign(value, sign); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int SignZeroToOne(Int32 value) { return (value >> (32 - 1)) - (~value >> (32 - 1)); } [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static int CopySign_A2(int value, int sign) { if (int.MinValue != value || 0 > sign) { return value * SignZeroToOne(value ^ sign); } return checked(-value); } ``` CopySign_A2 is mush faster than CopySign_A1. ### Configuration .NET 7 RC ### Data Random inputs (x10000) X64 RyuJIT AVX2 (Coffee Lake) | Method | Mean | Error | StdDev | |----------------- |----------:|----------:|----------:| | Test_0 | 3.093 us | 0.0064 us | 0.0060 us | | Test_CopySign_A1 | 25.423 us | 0.4784 us | 0.4475 us | | Test_CopySign_A2 | 6.486 us | 0.1288 us | 0.1323 us | Test_0: nop
Activity
EgorBo commentedon Oct 28, 2022
@LEI-Hongfaan do you want to contribute that to
CopySignimpl?Although, this API doesn't look popular: https://grep.app/search?q=.CopySign%28&words=true&filter[lang][0]=C%23
EgorBo commentedon Oct 28, 2022
Removing codegen label as it's unlikely we want/need to teach jit to recognize such idioms
Fix dotnet#77579
Improve integer CopySign with branchless bit-twiddling (#130563)
Improve integer CopySign with branchless bit-twiddling (#130563)