Skip to content

[API Proposal]: Add AllBitsSet to the IBinaryNumber Interface #69676

Description

@dakersnar

Background and motivation

Conceptually, T.AllBitsSet returns an instance of the binary number type T where every bit is set to 1. For example, Int32.AllBitsSet is 0xFFFFFFFF, or -1. This concept is generally useful for masking purposes. For example, Vectors use AllBitsSet often for Conditional Select, as bitwise-anding a number with AllBitsSet will produce the same number, in contrast to bitwise-anding a number with Zero. As brought up by #60882, AllBitsSet would be a nice addition to the IBinaryNumber interface, potentially enabling future generic refactoring of Scalar<T>.

The following types would explicitly implement AllBitsSet: Double, Half, NFloat, Single, Byte, Char, Int16, Int32, Int64, IntPtr, SByte, UInt16, UInt32, UInt64, UIntPtr, BigInteger, Int128, Uint128.

Design of the implementation for all of these types is trivial except for BigInteger. For BigInteger, the most obvious implementation would be to return MinusOne, as despite the internal bit representation not being literally AllBitsSet (due to BigInteger having unbounded size), it behaves as such for bitwise operations.

API Proposal

namespace System.Numerics
{

  public partial interface IBinaryNumber<TSelf>
  {
       static abstract TSelf AllBitsSet { get; }
  }

}

API Usage

T genericAlgorithm(T input) 
where T : IBinaryNumber<T> {
    return input + T.AllBitsSet;
}

Alternative Designs

When deciding whether a method is implemented explicitly or implicitly for Generic Math, we have used the following rule: if the concept is trivially handled by knowing the concrete type, it's implemented explicitly. In other words, it is not exposed on the concrete type. This way, the implementation is only available when working with the generic interface.

In this case, we follow this logic and implement AllBitsSet explicitly, as knowing the concrete type leads to a trivial implementation (https://source.dot.net/#System.Private.CoreLib/Scalar.cs,11). However, it could be argued that AllBitsSet is conceptually tricky enough for some of these types that a implicit implementation is warranted.

Additional Notes

Currently, refactoring Scalar<T> to take advantage of this is not possible without a language change. More specifically, the language doesn't have a way to "bridge" type constraints. So, since Vector<T> is where T : struct but we'd want Scalar<T> to be where T : struct, IBinaryNumber<T>, we'd need the language to provide some way to allow Vector<T> to use Scalar<T> despite the mismatching type requirements.

Activity

  1. added
    api-suggestion
    on May 23, 2022
  2. ghost added
    untriaged
    on May 23, 2022
  3. added
    api-ready-for-review
    and removed
    api-suggestion
    untriaged
    on May 23, 2022
  4. added this to the 7.0.0 milestone on May 23, 2022
  5. added
    blocking
    on May 27, 2022
  6. bartonjs commented on May 31, 2022

    @bartonjs
    Member

    Video

    • Looks good as proposed
    • Consider DIMming it as ~Zero
    namespace System.Numerics
    {
        public partial interface IBinaryNumber<TSelf>
        {
            static abstract TSelf AllBitsSet { get; }
        }
    }
  7. added
    api-approved
    and removed
    blocking
    api-ready-for-review
    on May 31, 2022
  8. ghost added
    in-pr
    on Jun 27, 2022
  9. ghost removed
    in-pr
    on Jun 29, 2022
  10. ghost added
    in-pr
    on Jul 27, 2022
  11. ghost removed
    in-pr
    on Aug 12, 2022
  12. ghost locked as resolved and limited conversation to collaborators on Sep 12, 2022
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

    api-approvedarea-System.Numerics

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions