-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
TensorPrimitives.Round.cs
323 lines (278 loc) · 16.1 KB
/
TensorPrimitives.Round.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;
namespace System.Numerics.Tensors
{
public static partial class TensorPrimitives
{
/// <summary>Computes the element-wise rounding of the numbers in the specified tensor</summary>
/// <param name="x">The tensor, represented as a span.</param>
/// <param name="destination">The destination tensor, represented as a span.</param>
/// <exception cref="ArgumentException">Destination is too short.</exception>
/// <exception cref="ArgumentException"><paramref name="x"/> and <paramref name="destination"/> reference overlapping memory locations and do not begin at the same location.</exception>
/// <remarks>
/// <para>
/// This method effectively computes <c><paramref name="destination" />[i] = T.Round(<paramref name="x" />[i])</c>.
/// </para>
/// </remarks>
public static void Round<T>(ReadOnlySpan<T> x, Span<T> destination)
where T : IFloatingPoint<T> =>
InvokeSpanIntoSpan<T, RoundToEvenOperator<T>>(x, destination);
/// <summary>Computes the element-wise rounding of the numbers in the specified tensor</summary>
/// <param name="x">The tensor, represented as a span.</param>
/// <param name="mode">The mode under which <paramref name="x" /> should be rounded.</param>
/// <param name="destination">The destination tensor, represented as a span.</param>
/// <exception cref="ArgumentException">Destination is too short.</exception>
/// <exception cref="ArgumentException"><paramref name="x"/> and <paramref name="destination"/> reference overlapping memory locations and do not begin at the same location.</exception>
/// <remarks>
/// <para>
/// This method effectively computes <c><paramref name="destination" />[i] = T.Round(<paramref name="x" />[i], <paramref name="mode"/>)</c>.
/// </para>
/// </remarks>
public static void Round<T>(ReadOnlySpan<T> x, MidpointRounding mode, Span<T> destination)
where T : IFloatingPoint<T>
{
switch (mode)
{
case MidpointRounding.ToEven:
Round(x, destination);
return;
case MidpointRounding.AwayFromZero:
InvokeSpanIntoSpan<T, RoundAwayFromZeroOperator<T>>(x, destination);
break;
case MidpointRounding.ToZero:
Truncate(x, destination);
return;
case MidpointRounding.ToNegativeInfinity:
Floor(x, destination);
return;
case MidpointRounding.ToPositiveInfinity:
Ceiling(x, destination);
return;
default:
throw new ArgumentException(SR.Format(SR.Argument_InvalidEnumValue, mode, typeof(MidpointRounding)), nameof(mode));
}
}
/// <summary>Computes the element-wise rounding of the numbers in the specified tensor</summary>
/// <param name="x">The tensor, represented as a span.</param>
/// <param name="digits">The number of fractional digits to which the numbers in <paramref name="x" /> should be rounded.</param>
/// <param name="destination">The destination tensor, represented as a span.</param>
/// <exception cref="ArgumentException">Destination is too short.</exception>
/// <exception cref="ArgumentException"><paramref name="x"/> and <paramref name="destination"/> reference overlapping memory locations and do not begin at the same location.</exception>
/// <remarks>
/// <para>
/// This method effectively computes <c><paramref name="destination" />[i] = T.Round(<paramref name="x" />[i], <paramref name="digits"/>)</c>.
/// </para>
/// </remarks>
public static void Round<T>(ReadOnlySpan<T> x, int digits, Span<T> destination) where T : IFloatingPoint<T> =>
Round(x, digits, MidpointRounding.ToEven, destination);
/// <summary>Computes the element-wise rounding of the numbers in the specified tensor</summary>
/// <param name="x">The tensor, represented as a span.</param>
/// <param name="digits">The number of fractional digits to which the numbers in <paramref name="x" /> should be rounded.</param>
/// <param name="mode">The mode under which <paramref name="x" /> should be rounded.</param>
/// <param name="destination">The destination tensor, represented as a span.</param>
/// <exception cref="ArgumentException">Destination is too short.</exception>
/// <exception cref="ArgumentException"><paramref name="x"/> and <paramref name="destination"/> reference overlapping memory locations and do not begin at the same location.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="digits"/> is invalid.</exception>
/// <exception cref="ArgumentException"><paramref name="mode"/> is invalid.</exception>
/// <remarks>
/// <para>
/// This method effectively computes <c><paramref name="destination" />[i] = T.Round(<paramref name="x" />[i], <paramref name="digits"/>, <paramref name="mode"/>)</c>.
/// </para>
/// </remarks>
public static void Round<T>(ReadOnlySpan<T> x, int digits, MidpointRounding mode, Span<T> destination)
where T : IFloatingPoint<T>
{
if (digits == 0)
{
Round(x, mode, destination);
}
ReadOnlySpan<T> roundPower10;
if (typeof(T) == typeof(float))
{
ReadOnlySpan<float> roundPower10Single = [1e0f, 1e1f, 1e2f, 1e3f, 1e4f, 1e5f, 1e6f];
roundPower10 = Rename<float, T>(roundPower10Single);
}
else if (typeof(T) == typeof(double))
{
Debug.Assert(typeof(T) == typeof(double));
ReadOnlySpan<double> roundPower10Double = [1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15];
roundPower10 = Rename<double, T>(roundPower10Double);
}
else
{
if ((uint)mode > (uint)MidpointRounding.ToPositiveInfinity)
{
throw new ArgumentException(SR.Format(SR.Argument_InvalidEnumValue, mode, typeof(MidpointRounding)), nameof(mode));
}
InvokeSpanIntoSpan(x, new RoundFallbackOperator<T>(digits, mode), destination);
return;
}
if ((uint)digits >= (uint)roundPower10.Length)
{
throw new ArgumentOutOfRangeException(nameof(digits));
}
T power10 = roundPower10[digits];
switch (mode)
{
case MidpointRounding.ToEven:
InvokeSpanIntoSpan(x, new MultiplyRoundDivideOperator<T, RoundToEvenOperator<T>>(power10), destination);
return;
case MidpointRounding.AwayFromZero:
InvokeSpanIntoSpan(x, new MultiplyRoundDivideOperator<T, RoundAwayFromZeroOperator<T>>(power10), destination);
break;
case MidpointRounding.ToZero:
InvokeSpanIntoSpan(x, new MultiplyRoundDivideOperator<T, TruncateOperator<T>>(power10), destination);
return;
case MidpointRounding.ToNegativeInfinity:
InvokeSpanIntoSpan(x, new MultiplyRoundDivideOperator<T, FloorOperator<T>>(power10), destination);
return;
case MidpointRounding.ToPositiveInfinity:
InvokeSpanIntoSpan(x, new MultiplyRoundDivideOperator<T, CeilingOperator<T>>(power10), destination);
return;
default:
throw new ArgumentException(SR.Format(SR.Argument_InvalidEnumValue, mode, typeof(MidpointRounding)), nameof(mode));
}
}
/// <summary>T.Round(x)</summary>
private readonly struct RoundToEvenOperator<T> : IUnaryOperator<T, T> where T : IFloatingPoint<T>
{
// This code is based on `nearbyint` from amd/aocl-libm-ose
// Copyright (C) 2008-2022 Advanced Micro Devices, Inc. All rights reserved.
//
// Licensed under the BSD 3-Clause "New" or "Revised" License
// See THIRD-PARTY-NOTICES.TXT for the full license text
public static bool Vectorizable => typeof(T) == typeof(float) || typeof(T) == typeof(double);
public static T Invoke(T x) => T.Round(x);
private const float SingleBoundary = 8388608.0f; // 2^23
private const double DoubleBoundary = 4503599627370496.0; // 2^52
public static Vector128<T> Invoke(Vector128<T> x)
{
Vector128<T> boundary = Vector128.Create(typeof(T) == typeof(float) ? T.CreateTruncating(SingleBoundary) : T.CreateTruncating(DoubleBoundary));
Vector128<T> temp = CopySignOperator<T>.Invoke(boundary, x);
return Vector128.ConditionalSelect(Vector128.GreaterThan(Vector128.Abs(x), boundary), x, CopySignOperator<T>.Invoke((x + temp) - temp, x));
}
public static Vector256<T> Invoke(Vector256<T> x)
{
Vector256<T> boundary = Vector256.Create(typeof(T) == typeof(float) ? T.CreateTruncating(SingleBoundary) : T.CreateTruncating(DoubleBoundary));
Vector256<T> temp = CopySignOperator<T>.Invoke(boundary, x);
return Vector256.ConditionalSelect(Vector256.GreaterThan(Vector256.Abs(x), boundary), x, CopySignOperator<T>.Invoke((x + temp) - temp, x));
}
public static Vector512<T> Invoke(Vector512<T> x)
{
Vector512<T> boundary = Vector512.Create(typeof(T) == typeof(float) ? T.CreateTruncating(SingleBoundary) : T.CreateTruncating(DoubleBoundary));
Vector512<T> temp = CopySignOperator<T>.Invoke(boundary, x);
return Vector512.ConditionalSelect(Vector512.GreaterThan(Vector512.Abs(x), boundary), x, CopySignOperator<T>.Invoke((x + temp) - temp, x));
}
}
/// <summary>T.Round(x, MidpointRounding.AwayFromZero)</summary>
private readonly struct RoundAwayFromZeroOperator<T> : IUnaryOperator<T, T> where T : IFloatingPoint<T>
{
public static bool Vectorizable => typeof(T) == typeof(float) || typeof(T) == typeof(double);
public static T Invoke(T x) => T.Round(x, MidpointRounding.AwayFromZero);
public static Vector128<T> Invoke(Vector128<T> x)
{
if (typeof(T) == typeof(float))
{
if (AdvSimd.IsSupported)
{
return AdvSimd.RoundAwayFromZero(x.AsSingle()).As<float, T>();
}
return TruncateOperator<float>.Invoke(x.AsSingle() + CopySignOperator<float>.Invoke(Vector128.Create(0.49999997f), x.AsSingle())).As<float, T>();
}
else
{
if (AdvSimd.Arm64.IsSupported)
{
return AdvSimd.Arm64.RoundAwayFromZero(x.AsDouble()).As<double, T>();
}
Debug.Assert(typeof(T) == typeof(double));
return TruncateOperator<double>.Invoke(x.AsDouble() + CopySignOperator<double>.Invoke(Vector128.Create(0.49999999999999994), x.AsDouble())).As<double, T>();
}
}
public static Vector256<T> Invoke(Vector256<T> x)
{
if (typeof(T) == typeof(float))
{
return TruncateOperator<float>.Invoke(x.AsSingle() + CopySignOperator<float>.Invoke(Vector256.Create(0.49999997f), x.AsSingle())).As<float, T>();
}
else
{
Debug.Assert(typeof(T) == typeof(double));
return TruncateOperator<double>.Invoke(x.AsDouble() + CopySignOperator<double>.Invoke(Vector256.Create(0.49999999999999994), x.AsDouble())).As<double, T>();
}
}
public static Vector512<T> Invoke(Vector512<T> x)
{
if (typeof(T) == typeof(float))
{
return TruncateOperator<float>.Invoke(x.AsSingle() + CopySignOperator<float>.Invoke(Vector512.Create(0.49999997f), x.AsSingle())).As<float, T>();
}
else
{
Debug.Assert(typeof(T) == typeof(double));
return TruncateOperator<double>.Invoke(x.AsDouble() + CopySignOperator<double>.Invoke(Vector512.Create(0.49999999999999994), x.AsDouble())).As<double, T>();
}
}
}
/// <summary>(T.Round(x * power10, digits, mode)) / power10</summary>
private readonly struct MultiplyRoundDivideOperator<T, TDelegatedRound> : IStatefulUnaryOperator<T>
where T : IFloatingPoint<T>
where TDelegatedRound : IUnaryOperator<T, T>
{
private readonly T _factor;
public MultiplyRoundDivideOperator(T factor)
{
Debug.Assert(typeof(T) == typeof(float) || typeof(T) == typeof(double));
_factor = factor;
}
public static bool Vectorizable => true;
private const float Single_RoundLimit = 1e8f;
private const double Double_RoundLimit = 1e16d;
public T Invoke(T x)
{
T limit = typeof(T) == typeof(float) ? T.CreateTruncating(Single_RoundLimit) : T.CreateTruncating(Double_RoundLimit);
return T.Abs(x) < limit ?
TDelegatedRound.Invoke(x * _factor) / _factor :
x;
}
public Vector128<T> Invoke(Vector128<T> x)
{
Vector128<T> limit = Vector128.Create(typeof(T) == typeof(float) ? T.CreateTruncating(Single_RoundLimit) : T.CreateTruncating(Double_RoundLimit));
return Vector128.ConditionalSelect(Vector128.LessThan(Vector128.Abs(x), limit),
TDelegatedRound.Invoke(x * _factor) / _factor,
x);
}
public Vector256<T> Invoke(Vector256<T> x)
{
Vector256<T> limit = Vector256.Create(typeof(T) == typeof(float) ? T.CreateTruncating(Single_RoundLimit) : T.CreateTruncating(Double_RoundLimit));
return Vector256.ConditionalSelect(Vector256.LessThan(Vector256.Abs(x), limit),
TDelegatedRound.Invoke(x * _factor) / _factor,
x);
}
public Vector512<T> Invoke(Vector512<T> x)
{
Vector512<T> limit = Vector512.Create(typeof(T) == typeof(float) ? T.CreateTruncating(Single_RoundLimit) : T.CreateTruncating(Double_RoundLimit));
return Vector512.ConditionalSelect(Vector512.LessThan(Vector512.Abs(x), limit),
TDelegatedRound.Invoke(x * _factor) / _factor,
x);
}
}
/// <summary>T.Round(x, digits, mode)</summary>
private readonly struct RoundFallbackOperator<T>(int digits, MidpointRounding mode) : IStatefulUnaryOperator<T>
where T : IFloatingPoint<T>
{
private readonly int _digits = digits;
private readonly MidpointRounding _mode = mode;
public static bool Vectorizable => false;
public T Invoke(T x) => T.Round(x, _digits, _mode);
public Vector128<T> Invoke(Vector128<T> x) => throw new NotSupportedException();
public Vector256<T> Invoke(Vector256<T> x) => throw new NotSupportedException();
public Vector512<T> Invoke(Vector512<T> x) => throw new NotSupportedException();
}
}
}