Skip to content

Commit 3fc45ff

Browse files
authored
Half conversion benchmarks (#2950)
1 parent f0a2346 commit 3fc45ff

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/benchmarks/micro/MicroBenchmarks.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
<Compile Remove="libraries\System.Text.Json\Serializer\WritePreservedReferences.cs" />
148148
<Compile Remove="libraries\System.Net.Security\SslStreamTests.Context.cs" />
149149
<Compile Remove="libraries\System.Formats.Cbor\*.cs" />
150+
<Compile Remove="libraries\System.Runtime\Perf.Half.cs" />
150151
</ItemGroup>
151152

152153
<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETFramework' Or ('$(TargetFrameworkIdentifier)' == '.NETCoreApp' And '$(_TargetFrameworkVersionWithoutV)' &lt; '6.0')">
@@ -217,5 +218,4 @@
217218
<!-- Workaround https://github.com/dotnet/project-system/issues/935 -->
218219
<None Include="**/*.cs" />
219220
</ItemGroup>
220-
221221
</Project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Collections.Generic;
6+
using System.Globalization;
7+
using System.Linq;
8+
using System.Runtime.CompilerServices;
9+
using System.Runtime.InteropServices;
10+
11+
using BenchmarkDotNet.Attributes;
12+
13+
using MicroBenchmarks;
14+
15+
namespace System.Tests
16+
{
17+
[BenchmarkCategory(Categories.Libraries)]
18+
public class Perf_Half
19+
{
20+
public static IEnumerable<Half> Values => new Half[]
21+
{
22+
BitConverter.UInt16BitsToHalf(0x03ff), //Maximum subnormal number in Half
23+
(Half)12345.0f /* same value used by other tests to compare the perf */,
24+
BitConverter.UInt16BitsToHalf(0x7dff) //NaN
25+
};
26+
27+
public static IEnumerable<float> SingleValues => new float[]
28+
{
29+
6.097555E-05f,
30+
12345.0f /* same value used by other tests to compare the perf */,
31+
65520.0f, //Minimum value that is infinity in Half
32+
float.NaN
33+
};
34+
35+
[Benchmark]
36+
[ArgumentsSource(nameof(SingleValues))]
37+
public Half SingleToHalf(float value) => (Half)value;
38+
39+
[Benchmark]
40+
[ArgumentsSource(nameof(Values))]
41+
public float HalfToSingle(Half value) => (float)value;
42+
}
43+
}

0 commit comments

Comments
 (0)