Files
dotnet/Microsoft.Toolkit.HighPerformance/Extensions/BoolExtensions.cs
T
Sergio Pedri 21547f3712 Microsoft.Toolkit.HighPerformance package (part 2) (#3273)
* Added support for .NET Core 2.1 and 3.1 targets

* Fixed T[] dangerous accessors on .NET Standard

* Minor speed improvements to Array2DColumnEnumerable<T>

* Updated 2D array extensions

* Minor code tweaks

* Added DangerousGetLookupReferenceAt<T> extension

* Added BitHelper.HasLookupFlag APIs

* Added BitHelper.ExtractRange and SetRange APIs

* Added bool bitwise mask extensions

* Added StreamExtensions class on .NET Standard 2.0

* Added IBufferWriterExtensions type

* Added more Stream extensions

* Added object field offset extensions

* Minor code refactoring

* Improved description

* Switched managed count to IntPtr

* Refactored GetDjb2HashCode code, minor tweaks

* Unit tests fixes

* Refactored Count extensions, minor tweaks

* Fixed incorrect operators precedence

* Added SpanHelper.GetDjb2LikeByteHash method

* Minor code refactoring

* Code refactoring to HashCode<T>

* Minor style tweaks

* Updated comments styles to follow style convention

* Minor code tweaks to the Count method

* More code tweaks to the Count method

* Added tests for DangerousGetObjectDataByteOffset

* Fixed visibility of new object extensions

* Added tests for 64-bit extract/set range APIs

* Added tests for bool to bitwise mask APIs

* Fixed MemoryStream EOF exception type

* Enabled Stream extension tests on UWP

* Added StreamExtensions read/write value tests

* Minor code refactoring to IBufferWriter<T> extensions

* Added IBufferWriterExtensions tests

* Added MemoryBufferWriter<T> type

* Added IBuffer<T> interface

* Added MemoryBufferWriterDebugView<T> type

* Added MemoryBufferWriter<T> tests

* Fixed a unit test

* Fixed incorrect inlining attribute

* Removed leftover partial modifier

* Added missing readonly modifier for lock structs

* Improved summary for the Box<T> type

* Added comments for #pragma warning disable items

* Added info on (x << 5) + x over x * 33 optimization

* Improved XML docs for BitHelper APIs

* Fixed potentially misleading test method names

* Added comment for MemoryOwner<T>.Dispose test

* Fixed multi-target support for string extensions

* Improved IMemoryOwner.AsStream tests

* Added XML remarks to the HashCode<T> type

* Improved docs for ReadOnlySpanExtensions.GetDjb2HashCode<T>

* Tweaked an XML link

* Tweaked attributes for SpanHelper methods

* Minor optimizations to SpanHelper.Count

* Fixed an overflow bug in SpanHelper.Count

* Speed improvements to SpanHelper.Count

~35% speedup with large collections of byte-size values, ~10% with other types

* Updated .csproj description

* Added float/double support to SpanHelper.Count

* Added exception to the Box<T> constructor

* Removed unnecessary [SuppressMessage] attributes

* Removed [SuppressMessage] attributes

* Changed NullableRef<T>-s exceptions to follow Nullable<T>

* Added more operators to NullableRef<T> types

* Added missing in modifiers in ParallelHelper.For overloads

* Removed SIMD support for float/double count

It lacked proper handling of NaN values and both (signed) zeros

* Fixed compiler directives in SpanEnumerable<T>

* Updated ReadOnlySpanEnumerable<T> type

* Updated NuGet package tags

* Added HighPerformance package to readme.md list

* Added .NET Standard 1.4 target

* Added tests for implicit NullableRef<T> operators

* Removed unnecessary using directive

* Added missing sealed modifiers to MemoryStream methods

* Added Span<T>.IndexOf(ref T) extension

* Added ReadOnlySpan<T>.IndexOf<T>(in T) extension

* Added missing changes (incorrect commit)

* Removed unnecessary workaround (method is inlined)

* Added improved remarks for bool.ToBitwiseMask32/64

* Added internal NotNullWhen attribute when < .NET Standard 2.1

* Enabled .csproj-wide nullability annotations

* Minor code style tweaks

* Added [ReadOnly]NullableRef<T>.Null static properties
2020-05-13 12:32:20 -07:00

74 lines
3.3 KiB
C#

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
namespace Microsoft.Toolkit.HighPerformance.Extensions
{
/// <summary>
/// Helpers for working with the <see cref="bool"/> type.
/// </summary>
public static class BoolExtensions
{
/// <summary>
/// Converts the given <see cref="bool"/> value into an <see cref="int"/>.
/// </summary>
/// <param name="flag">The input value to convert.</param>
/// <returns>1 if <paramref name="flag"/> is <see langword="true"/>, 0 otherwise.</returns>
/// <remarks>This method does not contain branching instructions.</remarks>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ToInt(this bool flag)
{
return Unsafe.As<bool, byte>(ref flag);
}
/// <summary>
/// Converts the given <see cref="bool"/> value to an <see cref="int"/> mask with
/// all bits representing the value of the input flag (either 0xFFFFFFFF or 0x00000000).
/// </summary>
/// <param name="flag">The input value to convert.</param>
/// <returns>0xFFFFFFFF if <paramref name="flag"/> is <see langword="true"/>, 0x00000000 otherwise.</returns>
/// <remarks>
/// This method does not contain branching instructions, and it is only guaranteed to work with
/// <see cref="bool"/> values being either 0 or 1. Operations producing a <see cref="bool"/> result,
/// such as numerical comparisons, always result in a valid value. If the <see cref="bool"/> value is
/// produced by fields with a custom <see cref="System.Runtime.InteropServices.FieldOffsetAttribute"/>,
/// or by using <see cref="Unsafe.As{T}"/> or other unsafe APIs to directly manipulate the underlying
/// data though, it is responsibility of the caller to ensure the validity of the provided value.
/// </remarks>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ToBitwiseMask32(this bool flag)
{
byte rangeFlag = Unsafe.As<bool, byte>(ref flag);
int
negativeFlag = rangeFlag - 1,
mask = ~negativeFlag;
return mask;
}
/// <summary>
/// Converts the given <see cref="bool"/> value to a <see cref="long"/> mask with
/// all bits representing the value of the input flag (either all 1s or 0s).
/// </summary>
/// <param name="flag">The input value to convert.</param>
/// <returns>All 1s if <paramref name="flag"/> is <see langword="true"/>, all 0s otherwise.</returns>
/// <remarks>This method does not contain branching instructions. See additional note in <see cref="ToBitwiseMask32"/>.</remarks>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long ToBitwiseMask64(this bool flag)
{
byte rangeFlag = Unsafe.As<bool, byte>(ref flag);
long
negativeFlag = (long)rangeFlag - 1,
mask = ~negativeFlag;
return mask;
}
}
}