镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

CopySign is slow #77579

Closed
Closed

Description

@LEI-Hongfaan

Description

        [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

  1. ghost added
    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
    on Oct 28, 2022
  2. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Oct 28, 2022
  3. EgorBo commented on Oct 28, 2022

    @EgorBo
    Member

    @LEI-Hongfaan do you want to contribute that to CopySign impl?
    Although, this API doesn't look popular: https://grep.app/search?q=.CopySign%28&words=true&filter[lang][0]=C%23

  4. added this to the Future milestone on Oct 28, 2022
  5. added and removed
    untriagedNew issue has not been triaged by the area owner
    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
    on Oct 28, 2022
  6. EgorBo commented on Oct 28, 2022

    @EgorBo
    Member

    Removing codegen label as it's unlikely we want/need to teach jit to recognize such idioms

  7. added a commit that references this issue on Aug 23, 2023
    271014a
  8. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Aug 23, 2023
  9. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Feb 28, 2024
  10. added
    in-prThere is an active PR which will close this issue when it is merged
    on Jul 11, 2026
  11. added a commit that references this issue on Jul 13, 2026
    9556631
  12. added a commit that references this issue on Jul 15, 2026
    6e2d142
  13. locked and limited conversation to collaborators on Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Runtimein-prThere is an active PR which will close this issue when it is mergedtenet-performancePerformance related issue

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions