-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUInt16Le.cs
More file actions
460 lines (393 loc) · 25.1 KB
/
UInt16Le.cs
File metadata and controls
460 lines (393 loc) · 25.1 KB
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
using System;
using System.Buffers.Binary;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Stardust.Utilities
{
/// <summary>
/// Little-endian 16-bit unsigned integer type.
/// Stores bytes in little-endian order (least significant byte first).
/// </summary>
[Serializable]
[TypeConverter(typeof(UInt16LeTypeConverter))]
[StructLayout(LayoutKind.Explicit, Size = 2)]
public struct UInt16Le : IComparable, IComparable<UInt16Le>, IEquatable<UInt16Le>,
IFormattable, ISpanFormattable, IParsable<UInt16Le>, ISpanParsable<UInt16Le>
{
[FieldOffset(0)] internal byte lo;
[FieldOffset(1)] internal byte hi;
// Overlapping native field gives the JIT a single primitive to keep in a
// register. On LE hosts (x86/x64/ARM), bytes [lo, hi] = native ushort directly.
[FieldOffset(0)] private ushort _value;
/// <summary>Initializes a new <see cref="UInt16Le"/> from a <see cref="ushort"/> value.</summary>
/// <param name="num">The value to store.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public UInt16Le(ushort num)
{
// SkipInit suppresses the definite-assignment check so we can write the
// overlapping _value field once without first zeroing lo/hi separately.
Unsafe.SkipInit(out this);
#if BIG_ENDIAN
_value = BinaryPrimitives.ReverseEndianness(num);
#else
_value = num;
#endif
}
/// <summary>Initializes a new <see cref="UInt16Le"/> from an <see cref="int"/> value.</summary>
/// <param name="num">The value to store. Must be within <see cref="ushort"/> range.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="num"/> is outside the valid range.</exception>
public UInt16Le(int num)
{
if (num > ushort.MaxValue || num < ushort.MinValue)
throw new ArgumentOutOfRangeException(nameof(num));
lo = (byte)(num & 0xff);
hi = (byte)((num & 0xffff) >> 8);
}
/// <summary>Initializes a new <see cref="UInt16Le"/> from a <see cref="uint"/> value.</summary>
/// <param name="num">The value to store. Must be within <see cref="ushort"/> range.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="num"/> exceeds <see cref="ushort.MaxValue"/>.</exception>
public UInt16Le(uint num)
{
if (num > ushort.MaxValue)
throw new ArgumentOutOfRangeException(nameof(num));
lo = (byte)(num & 0xff);
hi = (byte)((num & 0xffff) >> 8);
}
/// <summary>Initializes a new <see cref="UInt16Le"/> from a read-only byte span.</summary>
/// <param name="bytes">A span containing at least 2 bytes.</param>
/// <exception cref="ArgumentException"><paramref name="bytes"/> has fewer than 2 bytes.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public UInt16Le(ReadOnlySpan<byte> bytes)
{
if (bytes.Length < 2)
throw new ArgumentException("Span must have at least 2 bytes", nameof(bytes));
lo = bytes[0];
hi = bytes[1];
}
/// <summary>
/// Initializes a new <see cref="UInt16Le"/> from a read-only byte span whose byte order is specified.
/// </summary>
/// <param name="bytes">Source span (must have at least 2 bytes).</param>
/// <param name="isBigEndian">If <see langword="false"/> (default) the source is interpreted as little-endian; if <see langword="true"/> it is interpreted as big-endian and reversed during storage.</param>
/// <exception cref="ArgumentException">If span is too short.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public UInt16Le(ReadOnlySpan<byte> bytes, bool isBigEndian = false)
{
if (bytes.Length < 2)
throw new ArgumentException("Span must have at least 2 bytes", nameof(bytes));
if (isBigEndian)
{
lo = bytes[1];
hi = bytes[0];
}
else
{
lo = bytes[0];
hi = bytes[1];
}
}
/// <summary>
/// Initializes a new <see cref="UInt16Le"/> from a read-only byte span at the given offset.
/// </summary>
/// <param name="bytes">Source span (must have at least 2 bytes starting from <paramref name="offset"/>).</param>
/// <param name="offset">Starting offset in the span.</param>
/// <param name="isBigEndian">If <see langword="false"/> (default) the source is interpreted as little-endian; if <see langword="true"/> it is interpreted as big-endian and reversed during storage.</param>
/// <exception cref="ArgumentException">If span is too short from the given offset.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public UInt16Le(ReadOnlySpan<byte> bytes, int offset, bool isBigEndian = false)
{
if (bytes.Length - offset < 2)
throw new ArgumentException("Span must have at least 2 bytes", nameof(bytes));
if (isBigEndian)
{
lo = bytes[offset + 1];
hi = bytes[offset + 0];
}
else
{
lo = bytes[offset + 0];
hi = bytes[offset + 1];
}
}
/// <summary>Initializes a new <see cref="UInt16Le"/> from a byte array whose byte order is specified.</summary>
/// <param name="bytes">Source byte array (must have at least 2 bytes).</param>
/// <param name="isBigEndian">If <see langword="false"/> (default) the source is interpreted as little-endian; if <see langword="true"/> it is interpreted as big-endian and reversed during storage.</param>
/// <exception cref="ArgumentException">The array is too short.</exception>
public UInt16Le(byte[] bytes, bool isBigEndian = false) : this(new ReadOnlySpan<byte>(bytes), isBigEndian) { }
/// <summary>Initializes a new <see cref="UInt16Le"/> from a byte array at the given offset.</summary>
/// <param name="bytes">Source byte array (must have at least 2 bytes starting from <paramref name="offset"/>).</param>
/// <param name="offset">The zero-based offset into <paramref name="bytes"/>.</param>
/// <param name="isBigEndian">If <see langword="false"/> (default) the source is interpreted as little-endian; if <see langword="true"/> it is interpreted as big-endian and reversed during storage.</param>
/// <exception cref="ArgumentException">The array is too short for the given offset.</exception>
public UInt16Le(byte[] bytes, int offset, bool isBigEndian = false) : this(new ReadOnlySpan<byte>(bytes), offset, isBigEndian) { }
/// <summary>Writes the value to a byte array in the specified byte order.</summary>
/// <param name="bytes">The destination byte array.</param>
/// <param name="offset">The zero-based offset into <paramref name="bytes"/>.</param>
/// <param name="isBigEndian">If <see langword="false"/> (default) bytes are written little-endian; if <see langword="true"/> they are written big-endian.</param>
[Obsolete("Use WriteTo(byte[], int, bool) instead.")]
public readonly void ToBytes(byte[] bytes, int offset = 0, bool isBigEndian = false)
=> WriteTo(new Span<byte>(bytes), offset, isBigEndian);
/// <summary>Writes the value to a destination span in the specified byte order.</summary>
/// <param name="destination">A span with at least 2 bytes of space starting from <paramref name="offset"/>.</param>
/// <param name="offset">The zero-based offset into <paramref name="destination"/>.</param>
/// <param name="isBigEndian">If <see langword="false"/> (default) bytes are written little-endian; if <see langword="true"/> they are written big-endian.</param>
/// <exception cref="ArgumentException"><paramref name="destination"/> is too short.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void WriteTo(Span<byte> destination, int offset = 0, bool isBigEndian = false)
{
if (destination.Length - offset < 2)
throw new ArgumentException("Destination span must have at least 2 bytes", nameof(destination));
if (isBigEndian)
{
destination[offset + 0] = hi;
destination[offset + 1] = lo;
}
else
{
destination[offset + 0] = lo;
destination[offset + 1] = hi;
}
}
/// <summary>Writes the value to a byte array in the specified byte order.</summary>
/// <param name="destination">Destination byte array (must have at least 2 bytes starting from <paramref name="offset"/>).</param>
/// <param name="offset">The zero-based offset into <paramref name="destination"/>.</param>
/// <param name="isBigEndian">If <see langword="false"/> (default) bytes are written little-endian; if <see langword="true"/> they are written big-endian.</param>
/// <exception cref="ArgumentException">The array is too short.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void WriteTo(byte[] destination, int offset = 0, bool isBigEndian = false)
=> WriteTo(new Span<byte>(destination), offset, isBigEndian);
/// <summary>Attempts to write the value to a destination span in the specified byte order.</summary>
/// <param name="destination">The destination span.</param>
/// <param name="offset">The zero-based offset into <paramref name="destination"/>.</param>
/// <param name="isBigEndian">If <see langword="false"/> (default) bytes are written little-endian; if <see langword="true"/> they are written big-endian.</param>
/// <returns><see langword="true"/> if the span was large enough; otherwise <see langword="false"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool TryWriteTo(Span<byte> destination, int offset = 0, bool isBigEndian = false)
{
if (destination.Length - offset < 2)
return false;
if (isBigEndian)
{
destination[offset + 0] = hi;
destination[offset + 1] = lo;
}
else
{
destination[offset + 0] = lo;
destination[offset + 1] = hi;
}
return true;
}
/// <summary>Attempts to write the value to a byte array in the specified byte order.</summary>
/// <param name="destination">Destination byte array.</param>
/// <param name="offset">The zero-based offset into <paramref name="destination"/>.</param>
/// <param name="isBigEndian">If <see langword="false"/> (default) bytes are written little-endian; if <see langword="true"/> they are written big-endian.</param>
/// <returns><see langword="true"/> if the array was large enough; otherwise <see langword="false"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool TryWriteTo(byte[] destination, int offset = 0, bool isBigEndian = false)
=> TryWriteTo(new Span<byte>(destination), offset, isBigEndian);
/// <summary>Writes a <see cref="ushort"/> value to a byte array in little-endian order.</summary>
/// <param name="num">The value to write.</param>
/// <param name="bytes">The destination byte array.</param>
/// <param name="offset">The zero-based offset into <paramref name="bytes"/>.</param>
public static void ToBytes(ushort num, byte[] bytes, int offset = 0)
{
bytes[offset + 0] = (byte)(num & 0xff);
bytes[offset + 1] = (byte)(num >> 8);
}
/// <summary>Writes a <see cref="ushort"/> value to a span in little-endian order.</summary>
/// <param name="num">The value to write.</param>
/// <param name="destination">The destination span.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteTo(ushort num, Span<byte> destination)
{
BinaryPrimitives.WriteUInt16LittleEndian(destination, num);
}
/// <summary>Reads a <see cref="UInt16Le"/> from a read-only byte span in the specified byte order.</summary>
/// <param name="source">A span containing at least 2 bytes.</param>
/// <param name="offset">The zero-based offset into <paramref name="source"/>.</param>
/// <param name="isBigEndian">If <see langword="false"/> (default) the source is interpreted as little-endian; if <see langword="true"/> it is interpreted as big-endian.</param>
/// <returns>The value read from the span.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt16Le ReadFrom(ReadOnlySpan<byte> source, int offset = 0, bool isBigEndian = false) => new(source, offset, isBigEndian);
/// <summary>Reads a <see cref="UInt16Le"/> from a byte array in the specified byte order.</summary>
/// <param name="source">A byte array containing at least 2 bytes.</param>
/// <param name="offset">The zero-based offset into <paramref name="source"/>.</param>
/// <param name="isBigEndian">If <see langword="false"/> (default) the source is interpreted as little-endian; if <see langword="true"/> it is interpreted as big-endian.</param>
/// <returns>The value read from the array.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt16Le ReadFrom(byte[] source, int offset = 0, bool isBigEndian = false) => new(new ReadOnlySpan<byte>(source), offset, isBigEndian);
/// <summary>Parses a string into a <see cref="UInt16Le"/>.</summary>
/// <param name="s">The string to parse.</param>
/// <param name="style">The number style to use.</param>
/// <returns>The parsed value.</returns>
public static UInt16Le Parse(string s, NumberStyles style = NumberStyles.Integer)
{
if (style == NumberStyles.HexNumber)
s = s.ToLower().Replace("0x", "");
return new UInt16Le(ushort.Parse(s, style));
}
/// <summary>Parses a string into a <see cref="UInt16Le"/> using the specified format provider.</summary>
/// <param name="s">The string to parse.</param>
/// <param name="provider">The format provider.</param>
/// <returns>The parsed value.</returns>
public static UInt16Le Parse(string s, IFormatProvider? provider) => new(ushort.Parse(s, provider));
/// <summary>Parses a character span into a <see cref="UInt16Le"/> using the specified format provider.</summary>
/// <param name="s">The character span to parse.</param>
/// <param name="provider">The format provider.</param>
/// <returns>The parsed value.</returns>
public static UInt16Le Parse(ReadOnlySpan<char> s, IFormatProvider? provider) => new(ushort.Parse(s, provider));
/// <summary>Tries to parse a string into a <see cref="UInt16Le"/>.</summary>
/// <param name="s">The string to parse.</param>
/// <param name="provider">The format provider.</param>
/// <param name="result">When successful, the parsed value.</param>
/// <returns><see langword="true"/> if parsing succeeded; otherwise <see langword="false"/>.</returns>
public static bool TryParse(string? s, IFormatProvider? provider, out UInt16Le result)
{
if (ushort.TryParse(s, provider, out ushort value)) { result = new(value); return true; }
result = default; return false;
}
/// <summary>Tries to parse a character span into a <see cref="UInt16Le"/>.</summary>
/// <param name="s">The character span to parse.</param>
/// <param name="provider">The format provider.</param>
/// <param name="result">When successful, the parsed value.</param>
/// <returns><see langword="true"/> if parsing succeeded; otherwise <see langword="false"/>.</returns>
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out UInt16Le result)
{
if (ushort.TryParse(s, provider, out ushort value)) { result = new(value); return true; }
result = default; return false;
}
/// <inheritdoc/>
public override readonly string ToString() => $"0x{(ushort)this:x4}";
/// <inheritdoc/>
public readonly string ToString(string? format, IFormatProvider? formatProvider) =>
((ushort)this).ToString(format, formatProvider);
/// <inheritdoc/>
public readonly bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider) =>
((ushort)this).TryFormat(destination, out charsWritten, format, provider);
/// <inheritdoc/>
public readonly int CompareTo(object? obj)
{
if (obj == null) throw new ArgumentException("obj is null");
return CompareTo((UInt16Le)obj);
}
/// <inheritdoc/>
public readonly int CompareTo(UInt16Le other) => ((ushort)this).CompareTo((ushort)other);
/// <inheritdoc/>
public override readonly bool Equals(object? obj) => obj != null && Equals((UInt16Le)obj);
/// <inheritdoc/>
public readonly bool Equals(UInt16Le other) => this == other;
/// <inheritdoc/>
public override readonly int GetHashCode() => ((ushort)this).GetHashCode();
#region Operators
/// <summary>Returns the value of the operand (unary plus).</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt16Le operator +(UInt16Le a) => a;
/// <summary>Negates the value (unary minus).</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt16Le operator -(UInt16Le a) => new((ushort)-(ushort)a);
/// <summary>Adds two values.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt16Le operator +(UInt16Le a, UInt16Le b) => new((ushort)((ushort)a + (ushort)b));
/// <summary>Subtracts two values.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt16Le operator -(UInt16Le a, UInt16Le b) => a + (-b);
/// <summary>Determines whether the left operand is greater than the right.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator >(UInt16Le a, UInt16Le b) => (ushort)a > (ushort)b;
/// <summary>Determines whether the left operand is less than the right.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator <(UInt16Le a, UInt16Le b) => (ushort)a < (ushort)b;
/// <summary>Determines whether the left operand is greater than or equal to the right.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator >=(UInt16Le a, UInt16Le b) => (ushort)a >= (ushort)b;
/// <summary>Determines whether the left operand is less than or equal to the right.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator <=(UInt16Le a, UInt16Le b) => (ushort)a <= (ushort)b;
/// <summary>Determines whether two values are equal.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(UInt16Le a, UInt16Le b) => (ushort)a == (ushort)b;
/// <summary>Determines whether two values are not equal.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(UInt16Le a, UInt16Le b) => (ushort)a != (ushort)b;
/// <summary>Multiplies two values.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt16Le operator *(UInt16Le a, UInt16Le b) => new((ushort)((ushort)a * (ushort)b));
/// <summary>Divides two values.</summary>
/// <exception cref="DivideByZeroException">The divisor is zero.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt16Le operator /(UInt16Le a, UInt16Le b)
{
if (b.hi == 0 && b.lo == 0) throw new DivideByZeroException();
return new((ushort)((ushort)a / (ushort)b));
}
/// <summary>Computes the bitwise AND of two values.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt16Le operator &(UInt16Le a, UInt16Le b) => new((ushort)((ushort)a & (ushort)b));
/// <summary>Computes the bitwise OR of two values.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt16Le operator |(UInt16Le a, UInt16Le b) => new((ushort)((ushort)a | (ushort)b));
/// <summary>Shifts the value right by the specified amount.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt16Le operator >>(UInt16Le a, uint b) => new((ushort)((ushort)a >> (byte)b));
/// <summary>Performs an unsigned (logical) right shift. For unsigned types this is identical to <c>>></c>.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt16Le operator >>>(UInt16Le a, int b) => new((ushort)((ushort)a >> b));
/// <summary>Shifts the value left by the specified amount.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt16Le operator <<(UInt16Le a, uint b) => new((ushort)((ushort)a << (byte)b));
/// <summary>Computes the modulo of a value.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt16Le operator %(UInt16Le a, uint b) => new((ushort)((ushort)a % b));
/// <summary>Computes the bitwise XOR of two values.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt16Le operator ^(UInt16Le a, UInt16Le b) => new((ushort)((ushort)a ^ (ushort)b));
/// <summary>Computes the bitwise XOR of a value and an unsigned mask.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt16Le operator ^(UInt16Le a, uint b) => new((ushort)((ushort)a ^ b));
/// <summary>Computes the bitwise complement of the value.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt16Le operator ~(UInt16Le a) => new((ushort)~(ushort)a);
/// <summary>Increments the value by one.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt16Le operator ++(UInt16Le a) => new((ushort)((ushort)a + 1));
/// <summary>Decrements the value by one.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt16Le operator --(UInt16Le a) => new((ushort)((ushort)a - 1));
#endregion
#region Conversions
/// <summary>Implicitly converts a <see cref="UInt16Le"/> to a <see cref="ushort"/>.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#if BIG_ENDIAN
public static implicit operator ushort(UInt16Le a) => BinaryPrimitives.ReverseEndianness(a._value);
#else
public static implicit operator ushort(UInt16Le a) => a._value;
#endif
/// <summary>Implicitly converts a <see cref="UInt16Le"/> to a <see cref="uint"/>.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator uint(UInt16Le a) => (ushort)a;
/// <summary>Implicitly converts a <see cref="ushort"/> to a <see cref="UInt16Le"/>.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator UInt16Le(ushort a) => new(a);
/// <summary>Explicitly converts a <see cref="UInt16Le"/> to a <see cref="byte"/>.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator byte(UInt16Le a) => a.lo;
/// <summary>Explicitly converts a <see cref="uint"/> to a <see cref="UInt16Le"/>.</summary>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="a"/> exceeds <see cref="ushort.MaxValue"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator UInt16Le(uint a)
{
if (a > ushort.MaxValue) throw new ArgumentOutOfRangeException(nameof(a));
return new((ushort)a);
}
/// <summary>Explicitly converts an <see cref="int"/> to a <see cref="UInt16Le"/>.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator UInt16Le(int a) => (UInt16Le)(uint)a;
/// <summary>Widening conversion from a 16-bit little-endian value to a 32-bit little-endian value.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator UInt32Le(UInt16Le a) => new((ushort)a);
#endregion
}
}