Skip to content

double.IsEvenInteger / double.IsOddInteger ~20× slower than double.IsInteger #133916

Closed

Description

@LEI-Hongfaan

Description

double.IsEvenInteger(x) and double.IsOddInteger(x) exhibit a severe performance penalty compared to double.IsInteger(x).
Benchmarking shows both even/odd checks are ~20× slower, despite being derivable from the same integer‑classification logic.

Configuration

arch: x64

Regression?

Unknown

Analysis

I suggest using an implementation similar to the code below, except for Single.IsInteger (since its current implementation is already optimized).

private enum IntegerKind {

    NotInteger = 0,

    Odd = 1,

    Even = 2,
}

internal static bool IsInteger<TFloat, TUIntBits>(TFloat value)
    where TFloat : unmanaged, IBinaryFloatingPointIeee754<TFloat>
    where TUIntBits : unmanaged, IBinaryInteger<TUIntBits>, IUnsignedNumber<TUIntBits> {
    return GetIntegerKind<TFloat, TUIntBits>(value) != IntegerKind.NotInteger;
}

internal static bool IsEvenInteger<TFloat, TUIntBits>(TFloat value)
    where TFloat : unmanaged, IBinaryFloatingPointIeee754<TFloat>
    where TUIntBits : unmanaged, IBinaryInteger<TUIntBits>, IUnsignedNumber<TUIntBits> {
    return GetIntegerKind<TFloat, TUIntBits>(value) == IntegerKind.Even;
}

internal static bool IsOddInteger<TFloat, TUIntBits>(TFloat value)
    where TFloat : unmanaged, IBinaryFloatingPointIeee754<TFloat>
    where TUIntBits : unmanaged, IBinaryInteger<TUIntBits>, IUnsignedNumber<TUIntBits> {
    return GetIntegerKind<TFloat, TUIntBits>(value) == IntegerKind.Odd;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static IntegerKind GetIntegerKind<TFloat, TUIntBits>(TFloat value)
    where TFloat : unmanaged, IBinaryFloatingPointIeee754<TFloat>
    where TUIntBits : unmanaged, IBinaryInteger<TUIntBits>, IUnsignedNumber<TUIntBits> {
    TUIntBits bits = Unsafe.BitCast<TFloat, TUIntBits>(value);

    int rawExponent = GetRawExponent<TFloat, TUIntBits>(bits);

    int maxRawExponent =
        BinaryFloatingPointIeee754TypeTraitsInternal<TFloat>.MaxRawExponent;

    int exponentBias =
        BinaryFloatingPointIeee754TypeTraitsInternal<TFloat>.MaxExponent;

    int fractionBits =
        BinaryFloatingPointIeee754TypeTraitsInternal<TFloat>
            .TrailingSignificandFieldBitWidth;


    // NaN / infinity
    if (rawExponent == maxRawExponent) {
        return IntegerKind.NotInteger;
    }


    TUIntBits mantissa =
        bits & ((TUIntBits.One << fractionBits) - TUIntBits.One);


    // Zero
    if (rawExponent == 0) {
        return mantissa == TUIntBits.Zero
            ? IntegerKind.Even
            : IntegerKind.NotInteger;
    }


    int e = rawExponent - exponentBias;


    // |x| < 1
    if (e < 0) {
        return IntegerKind.NotInteger;
    }


    // Spacing >= 2. Every representable integer is even.
    if (e > fractionBits) {
        return IntegerKind.Even;
    }


    int fractionalBits = fractionBits - e;


    // Has fractional part.
    if (!LowBitsAreZero(mantissa, fractionalBits)) {
        return IntegerKind.NotInteger;
    }


    // Units bit is the hidden leading 1.
    if (fractionalBits == fractionBits) {
        return IntegerKind.Odd;
    }


    return ((mantissa >> fractionalBits) & TUIntBits.One) != TUIntBits.Zero
        ? IntegerKind.Odd
        : IntegerKind.Even;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetRawExponent<TFloat, TUIntBits>(TUIntBits bits)
    where TFloat : unmanaged, IBinaryFloatingPointIeee754<TFloat>
    where TUIntBits : unmanaged, IBinaryInteger<TUIntBits>, IUnsignedNumber<TUIntBits> {
    int shift =
        BinaryFloatingPointIeee754TypeTraitsInternal<TFloat>
            .TrailingSignificandFieldBitWidth;

    int exponentBits =
        BinaryFloatingPointIeee754TypeTraitsInternal<TFloat>
            .ExponentFieldBitWidth;


    return int.CreateTruncating(bits >> shift)
        & ((1 << exponentBits) - 1);
}



[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool LowBitsAreZero<TUIntBits>(
    TUIntBits value,
    int count)
    where TUIntBits : unmanaged, IBinaryInteger<TUIntBits>, IUnsignedNumber<TUIntBits> {
    if (count == 0) {
        return true;
    }

    if (count >= Unsafe.SizeOf<TUIntBits>() * 8) {
        return value == TUIntBits.Zero;
    }

    return (value & ((TUIntBits.One << count) - TUIntBits.One))
        == TUIntBits.Zero;
}

Activity

  1. added
    tenet-performance
    on Sep 14, 2026
  2. added
    untriaged
    on Sep 14, 2026
  3. dotnet-policy-service commented on Sep 14, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-numerics
    See info in area-owners.md if you want to be subscribed.

  4. removed
    untriaged
    on Sep 25, 2026
  5. added a commit that references this issue on Sep 25, 2026
    0b87c1c
  6. locked and limited conversation to collaborators on Oct 26, 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.Numericstenet-performance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions