Skip to content

[API Proposal]: MemoryExtensions.BitwiseAnd/Or/Xor/Not #75578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
stephentoub opened this issue Sep 14, 2022 · 4 comments
Closed

[API Proposal]: MemoryExtensions.BitwiseAnd/Or/Xor/Not #75578

stephentoub opened this issue Sep 14, 2022 · 4 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Memory
Milestone

Comments

@stephentoub
Copy link
Member

stephentoub commented Sep 14, 2022

Background and motivation

BitArray has And, Or, and Xor methods that combine another BitArray's bits with this BitArray's bits, and a Not method that operates on the BitArray's bits. These are vectorized and operate on the BitArray's internal int[]. But such And/Or/Xor/Not operations are more general, and as long as we've gone to the trouble to implement these efficiently, we can expose those operations for others to use as well.

API Proposal

namespace System;

public class MemoryExtensions
{
    public static void BitwiseAnd(this Span<byte> span, ReadOnlySpan<byte> other); // span[i] &= other[i];
    public static void BitwiseOr(this Span<byte> span, ReadOnlySpan<byte> other); // span[i] |= other[i];
    public static void BitwiseXor(this Span<byte> span, ReadOnlySpan<byte> other); // span[i] ^= other[i];
    public static void BitwiseNot(this Span<byte> span); // span[i] = ~span[i];
}

Notes:

  • The inputs would need to be the same byte length. Differing lengths would result in exception.
  • Callers with data types other than byte would use MemoryMarshal.AsBytes to get a byte span from their data type. We don't want to have tons of overloads for these methods to accomodate lots of different types being used to hold the bits.
  • Name is negotiable, as always.

API Usage

e.g. BitArray.And would become:

public unsafe BitArray And(BitArray value)
{
    ...
    Span<byte> thisSpan = MemoryMarshal.AsBytes(m_array.AsSpan(0, count));
    Span<byte> valueSpan = MemoryMarshal.AsBytes(value.m_array.AsSpan(0, count));

    thisSpan.BitwiseAnd(valueSpan);

    _version++;
    return this;
}

Alternative Designs

  • We might consider overloads that take two ReadOnlySpan<byte> sources and a third Span<byte> destination. Such an overload could also be instead of the two-arg overloads if we document that the same span can be used as both a source and destination input.

Risks

No response

@stephentoub stephentoub added api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Memory labels Sep 14, 2022
@stephentoub stephentoub added this to the 8.0.0 milestone Sep 14, 2022
@ghost
Copy link

ghost commented Sep 14, 2022

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

Issue Details

Background and motivation

BitArray has And, Or, and Xor methods that combine another BitArray's bits with this BitArray's bits, and a Not method that operates on the BitArray's bits. These are vectorized and operate on the BitArray's internal int[]. But such And/Or/Xor/Not operations are more general, and as long as we've gone to the trouble to implement these efficiently, we can expose those operations for others to use as well.

API Proposal

namespace System;

public class MemoryExtensions
{
    public static void BitwiseAnd(this Span<byte> span, ReadOnlySpan<byte> other); // span[i] &= other[i];
    public static void BitwiseOr(this Span<byte> span, ReadOnlySpan<byte> other); // span[i] |= other[i];
    public static void BitwiseXor(this Span<byte> span, ReadOnlySpan<byte> other); // span[i] ^= other[i];
    public static void BitwiseNot(this Span<byte> span); // span[i] = ~span[i];
}

Notes:

  • The inputs would need to be the same byte length. Differing lengths would result in exception.
  • Callers with data types other than byte would use MemoryMarshal.AsBytes to get a byte span from their data type. We don't want to have tons of overloads for these methods to accomodate lots of different types being used to hold the bits.
  • Name is negotiable, as always.

API Usage

e.g. BitArray.And would become:

public unsafe BitArray And(BitArray value)
{
    ...
    Span<byte> thisSpan = MemoryMarshal.AsBytes(m_array.AsSpan(0, count));
    Span<byte> valueSpan = MemoryMarshal.AsBytes(value.m_array.AsSpan(0, count));

    thisSpan.And(valueSpan);

    _version++;
    return this;
}

Alternative Designs

No response

Risks

No response

Author: stephentoub
Assignees: -
Labels:

api-suggestion, area-System.Memory

Milestone: 8.0.0

@stephentoub stephentoub changed the title [API Proposal]: MemoryExtensions.And/Or/Xor/Not [API Proposal]: MemoryExtensions.BitwiseAnd/Or/Xor/Not Sep 14, 2022
@vcsjones
Copy link
Member

vcsjones commented Sep 14, 2022

As a point of some previous discussion, there is a similar proposal for this at #26651 that expresses interest in this functionality.

@stephentoub
Copy link
Member Author

Thanks; I searched but somehow missed that. Will close this in favor of that one since it's preexisting and has existing discussion.
cc: @GrabYourPitchforks

@stephentoub stephentoub closed this as not planned Won't fix, can't repro, duplicate, stale Sep 14, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Oct 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Memory
Projects
None yet
Development

No branches or pull requests

2 participants