-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNumericBitFields.cs
More file actions
384 lines (325 loc) · 17.8 KB
/
Copy pathNumericBitFields.cs
File metadata and controls
384 lines (325 loc) · 17.8 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
namespace Stardust.Utilities;
/// <summary>
/// IEEE 754 half-precision (16-bit) floating-point decomposition.
/// <code>
/// Bit: 15 | 14-10 | 9-0
/// S | Exp | Mantissa
/// 1 | 5 bits| 10 bits
/// </code>
/// </summary>
/// <remarks>
/// <para>
/// The stored exponent is biased by 15. Use <see cref="BiasedExponent"/> to read
/// the raw stored value, or <see cref="Exponent"/> to get the true mathematical
/// exponent with the bias removed. The implicit leading 1 of the mantissa is not stored.
/// </para>
/// <para>
/// Implicit conversions to/from <see cref="Half"/> allow seamless use:
/// <code>
/// IEEE754Half h = (Half)1.5;
/// h.Sign; // false
/// h.BiasedExponent; // 15 (raw stored value)
/// h.Exponent; // 0 (true power: 2^0)
/// h.Mantissa; // 0x200
/// Half value = h;
/// </code>
/// </para>
/// </remarks>
[BitFields(StorageType.Half, Description = "IEEE 754 Half-Precision (16-bit)")]
public partial struct IEEE754Half
{
/// <summary>10-bit significand (mantissa). The implicit leading 1 is not stored.</summary>
[BitField(0, End = 9, Description = "10-bit significand (fractional part); implicit leading 1 not stored")] public partial ushort Mantissa { get; set; }
/// <summary>
/// 5-bit biased exponent as stored in the IEEE 754 encoding (bias = 15).
/// For the true mathematical exponent, use <see cref="Exponent"/> instead.
/// </summary>
[BitField(10, End = 14, Description = "5-bit biased exponent (bias 15); subtract 15 for true power of 2")] public partial byte BiasedExponent { get; set; }
/// <summary>Sign bit. <c>true</c> = negative, <c>false</c> = positive.</summary>
[BitFlag(15, Description = "Sign bit: 1 = negative, 0 = positive")] public partial bool Sign { get; set; }
// ── Constants ───────────────────────────────────────────────
/// <summary>Exponent bias for IEEE 754 half-precision (15).</summary>
public const int EXPONENT_BIAS = 15;
/// <summary>Maximum biased exponent value (all 5 bits set).</summary>
public const int MAX_BIASED_EXPONENT = 0x1F;
// ── Classification properties ───────────────────────────────
/// <summary><c>true</c> if this value is NaN (exponent = all 1s, mantissa != 0).</summary>
public bool IsNaN => BiasedExponent == MAX_BIASED_EXPONENT && Mantissa != 0;
/// <summary><c>true</c> if this value is +/- infinity (exponent = all 1s, mantissa = 0).</summary>
public bool IsInfinity => BiasedExponent == MAX_BIASED_EXPONENT && Mantissa == 0;
/// <summary><c>true</c> if this value is a denormalized (subnormal) number (exponent = 0, mantissa != 0).</summary>
public bool IsDenormalized => BiasedExponent == 0 && Mantissa != 0;
/// <summary><c>true</c> if this value is a normalized number (0 < exponent < 31).</summary>
public bool IsNormal => BiasedExponent > 0 && BiasedExponent < MAX_BIASED_EXPONENT;
/// <summary><c>true</c> if this value is +/- zero (exponent = 0, mantissa = 0).</summary>
public bool IsZero => BiasedExponent == 0 && Mantissa == 0;
/// <summary>
/// The true mathematical exponent (<see cref="BiasedExponent"/> - 15), or <c>null</c>
/// for non-normal values (zero, denormalized, infinity, NaN).
/// Setting to <c>null</c> sets <see cref="BiasedExponent"/> to 0;
/// otherwise the bias is added automatically.
/// </summary>
/// <example>
/// <code>
/// IEEE754Half h = (Half)4.0;
/// h.BiasedExponent; // 17 (raw stored value)
/// h.Exponent; // 2 (true power: 4.0 = 2^2)
/// </code>
/// </example>
public int? Exponent
{
get => IsNormal ? BiasedExponent - EXPONENT_BIAS : null;
set => BiasedExponent = value is { } v ? (byte)(v + EXPONENT_BIAS) : (byte)0;
}
// ── Computed exponent constants ──────────────────────────────
/// <summary>Minimum true exponent for a normal half-precision value (-14).</summary>
public const int MIN_EXPONENT = 1 - EXPONENT_BIAS;
/// <summary>Maximum true exponent for a normal half-precision value (15).</summary>
public const int MAX_EXPONENT = MAX_BIASED_EXPONENT - 1 - EXPONENT_BIAS;
// ── Fluent exponent setter ───────────────────────────────────
/// <summary>
/// Returns a new <see cref="IEEE754Half"/> with the <see cref="BiasedExponent"/> set
/// from a true mathematical exponent (the bias is added automatically).
/// Out-of-range values are masked by <see cref="WithBiasedExponent"/>.
/// </summary>
/// <param name="exponent">True exponent (bias is added automatically; out-of-range values are masked).</param>
/// <returns>A copy of this value with the exponent field updated.</returns>
/// <example>
/// <code>
/// // Build 2^3 = 8.0 from parts
/// var h = default(IEEE754Half).WithExponent(3).WithMantissa(0);
/// h.Sign = false;
/// Half value = h; // 8.0
/// </code>
/// </example>
public IEEE754Half WithExponent(int exponent) =>
WithBiasedExponent((byte)(exponent + EXPONENT_BIAS));
}
/// <summary>
/// IEEE 754 single-precision (32-bit) floating-point decomposition.
/// <code>
/// Bit: 31 | 30-23 | 22-0
/// S | Exp | Mantissa
/// 1 | 8 bits| 23 bits
/// </code>
/// </summary>
/// <remarks>
/// <para>
/// The stored exponent is biased by 127. Use <see cref="BiasedExponent"/> to read
/// the raw stored value, or <see cref="Exponent"/> to get the true mathematical
/// exponent with the bias removed. The implicit leading 1 of the mantissa is not stored.
/// </para>
/// <para>
/// Implicit conversions to/from <see cref="float"/> allow seamless use:
/// <code>
/// IEEE754Single f = 3.14f;
/// f.Sign; // false
/// f.BiasedExponent; // 128 (raw stored value)
/// f.Exponent; // 1 (true power: 2^1, since 2 <= 3.14 < 4)
/// f.Mantissa; // 0x48F5C3
/// float value = f;
/// </code>
/// </para>
/// </remarks>
[BitFields(StorageType.Single, Description = "IEEE 754 Single-Precision (32-bit)")]
public partial struct IEEE754Single
{
/// <summary>23-bit significand (mantissa). The implicit leading 1 is not stored.</summary>
[BitField(0, End = 22, Description = "23-bit significand (fractional part); implicit leading 1 not stored")] public partial uint Mantissa { get; set; }
/// <summary>
/// 8-bit biased exponent as stored in the IEEE 754 encoding (bias = 127).
/// For the true mathematical exponent, use <see cref="Exponent"/> instead.
/// </summary>
[BitField(23, End = 30, Description = "8-bit biased exponent (bias 127); subtract 127 for true power of 2")] public partial byte BiasedExponent { get; set; }
/// <summary>Sign bit. <c>true</c> = negative, <c>false</c> = positive.</summary>
[BitFlag(31, Description = "Sign bit: 1 = negative, 0 = positive")] public partial bool Sign { get; set; }
// ── Constants ───────────────────────────────────────────────
/// <summary>Exponent bias for IEEE 754 single-precision (127).</summary>
public const int EXPONENT_BIAS = 127;
/// <summary>Maximum biased exponent value (all 8 bits set).</summary>
public const int MAX_BIASED_EXPONENT = 0xFF;
// ── Classification properties ───────────────────────────────
/// <summary><c>true</c> if this value is NaN (exponent = all 1s, mantissa != 0).</summary>
public bool IsNaN => BiasedExponent == MAX_BIASED_EXPONENT && Mantissa != 0;
/// <summary><c>true</c> if this value is +/- infinity (exponent = all 1s, mantissa = 0).</summary>
public bool IsInfinity => BiasedExponent == MAX_BIASED_EXPONENT && Mantissa == 0;
/// <summary><c>true</c> if this value is a denormalized (subnormal) number (exponent = 0, mantissa != 0).</summary>
public bool IsDenormalized => BiasedExponent == 0 && Mantissa != 0;
/// <summary><c>true</c> if this value is a normalized number (0 < exponent < 255).</summary>
public bool IsNormal => BiasedExponent > 0 && BiasedExponent < MAX_BIASED_EXPONENT;
/// <summary><c>true</c> if this value is +/- zero (exponent = 0, mantissa = 0).</summary>
public bool IsZero => BiasedExponent == 0 && Mantissa == 0;
/// <summary>
/// The true mathematical exponent (<see cref="BiasedExponent"/> - 127), or <c>null</c>
/// for non-normal values (zero, denormalized, infinity, NaN).
/// Setting to <c>null</c> sets <see cref="BiasedExponent"/> to 0;
/// otherwise the bias is added automatically.
/// </summary>
/// <example>
/// <code>
/// IEEE754Single f = 8.0f;
/// f.BiasedExponent; // 130 (raw stored value)
/// f.Exponent; // 3 (true power: 8.0 = 2^3)
/// </code>
/// </example>
public int? Exponent
{
get => IsNormal ? BiasedExponent - EXPONENT_BIAS : null;
set => BiasedExponent = value is { } v ? (byte)(v + EXPONENT_BIAS) : (byte)0;
}
// ── Computed exponent constants ──────────────────────────────
/// <summary>Minimum true exponent for a normal single-precision value (-126).</summary>
public const int MIN_EXPONENT = 1 - EXPONENT_BIAS;
/// <summary>Maximum true exponent for a normal single-precision value (127).</summary>
public const int MAX_EXPONENT = MAX_BIASED_EXPONENT - 1 - EXPONENT_BIAS;
// ── Fluent exponent setter ───────────────────────────────────
/// <summary>
/// Returns a new <see cref="IEEE754Single"/> with the <see cref="BiasedExponent"/> set
/// from a true mathematical exponent (the bias is added automatically).
/// Out-of-range values are masked by <see cref="WithBiasedExponent"/>.
/// </summary>
/// <param name="exponent">True exponent (bias is added automatically; out-of-range values are masked).</param>
/// <returns>A copy of this value with the exponent field updated.</returns>
/// <example>
/// <code>
/// // Build 2^3 = 8.0f from parts
/// var f = default(IEEE754Single).WithExponent(3).WithMantissa(0);
/// f.Sign = false;
/// float value = f; // 8.0f
/// </code>
/// </example>
public IEEE754Single WithExponent(int exponent) =>
WithBiasedExponent((byte)(exponent + EXPONENT_BIAS));
}
/// <summary>
/// IEEE 754 double-precision (64-bit) floating-point decomposition.
/// <code>
/// Bit: 63 | 62-52 | 51-0
/// S | Exp | Mantissa
/// 1 | 11 bits| 52 bits
/// </code>
/// </summary>
/// <remarks>
/// <para>
/// The stored exponent is biased by 1023. Use <see cref="BiasedExponent"/> to read
/// the raw stored value, or <see cref="Exponent"/> to get the true mathematical
/// exponent with the bias removed. The implicit leading 1 of the mantissa is not stored.
/// </para>
/// <para>
/// Implicit conversions to/from <see cref="double"/> allow seamless use:
/// <code>
/// IEEE754Double d = Math.PI;
/// d.Sign; // false
/// d.BiasedExponent; // 1024 (raw stored value)
/// d.Exponent; // 1 (true power: 2^1, since 2 <= pi < 4)
/// d.Mantissa; // 0x921FB54442D18
/// double value = d;
/// </code>
/// </para>
/// </remarks>
[BitFields(StorageType.Double, Description = "IEEE 754 Double-Precision (64-bit)")]
public partial struct IEEE754Double
{
/// <summary>52-bit significand (mantissa). The implicit leading 1 is not stored.</summary>
[BitField(0, End = 51, Description = "52-bit significand (fractional part); implicit leading 1 not stored")] public partial ulong Mantissa { get; set; }
/// <summary>
/// 11-bit biased exponent as stored in the IEEE 754 encoding (bias = 1023).
/// For the true mathematical exponent, use <see cref="Exponent"/> instead.
/// </summary>
[BitField(52, End = 62, Description = "11-bit biased exponent (bias 1023); subtract 1023 for true power of 2")] public partial ushort BiasedExponent { get; set; }
/// <summary>Sign bit. <c>true</c> = negative, <c>false</c> = positive.</summary>
[BitFlag(63, Description = "Sign bit: 1 = negative, 0 = positive")] public partial bool Sign { get; set; }
// ── Constants ───────────────────────────────────────────────
/// <summary>Exponent bias for IEEE 754 double-precision (1023).</summary>
public const int EXPONENT_BIAS = 1023;
/// <summary>Maximum biased exponent value (all 11 bits set).</summary>
public const int MAX_BIASED_EXPONENT = 0x7FF;
// ── Classification properties ───────────────────────────────
/// <summary><c>true</c> if this value is NaN (exponent = all 1s, mantissa != 0).</summary>
public bool IsNaN => BiasedExponent == MAX_BIASED_EXPONENT && Mantissa != 0;
/// <summary><c>true</c> if this value is +/- infinity (exponent = all 1s, mantissa = 0).</summary>
public bool IsInfinity => BiasedExponent == MAX_BIASED_EXPONENT && Mantissa == 0;
/// <summary><c>true</c> if this value is a denormalized (subnormal) number (exponent = 0, mantissa != 0).</summary>
public bool IsDenormalized => BiasedExponent == 0 && Mantissa != 0;
/// <summary><c>true</c> if this value is a normalized number (0 < exponent < 2047).</summary>
public bool IsNormal => BiasedExponent > 0 && BiasedExponent < MAX_BIASED_EXPONENT;
/// <summary><c>true</c> if this value is +/- zero (exponent = 0, mantissa = 0).</summary>
public bool IsZero => BiasedExponent == 0 && Mantissa == 0;
/// <summary>
/// The true mathematical exponent (<see cref="BiasedExponent"/> - 1023), or <c>null</c>
/// for non-normal values (zero, denormalized, infinity, NaN).
/// Setting to <c>null</c> sets <see cref="BiasedExponent"/> to 0;
/// otherwise the bias is added automatically.
/// </summary>
/// <example>
/// <code>
/// IEEE754Double d = Math.PI;
/// d.BiasedExponent; // 1024 (raw stored value)
/// d.Exponent; // 1 (true power: pi is in [2, 4), so 2^1)
/// </code>
/// </example>
public int? Exponent
{
get => IsNormal ? BiasedExponent - EXPONENT_BIAS : null;
set => BiasedExponent = value is { } v ? (ushort)(v + EXPONENT_BIAS) : (ushort)0;
}
// ── Computed exponent constants ──────────────────────────────
/// <summary>Minimum true exponent for a normal double-precision value (-1022).</summary>
public const int MIN_EXPONENT = 1 - EXPONENT_BIAS;
/// <summary>Maximum true exponent for a normal double-precision value (1023).</summary>
public const int MAX_EXPONENT = MAX_BIASED_EXPONENT - 1 - EXPONENT_BIAS;
// ── Fluent exponent setter ───────────────────────────────────
/// <summary>
/// Returns a new <see cref="IEEE754Double"/> with the <see cref="BiasedExponent"/> set
/// from a true mathematical exponent (the bias is added automatically).
/// Out-of-range values are masked by <see cref="WithBiasedExponent"/>.
/// </summary>
/// <param name="exponent">True exponent (bias is added automatically; out-of-range values are masked).</param>
/// <returns>A copy of this value with the exponent field updated.</returns>
/// <example>
/// <code>
/// // Build 2^3 = 8.0 from parts
/// var d = default(IEEE754Double).WithExponent(3).WithMantissa(0);
/// d.Sign = false;
/// double value = d; // 8.0
/// </code>
/// </example>
public IEEE754Double WithExponent(int exponent) =>
WithBiasedExponent((ushort)(exponent + EXPONENT_BIAS));
}
/// <summary>
/// .NET <see cref="decimal"/> (128-bit) decomposition into coefficient, scale, and sign.
/// <code>
/// Bits: 127 | 126-120 | 119-112 | 111-96 | 95-0
/// Sign| Reserved| Scale | Reserved | 96-bit unsigned coefficient
/// </code>
/// </summary>
/// <remarks>
/// <para>
/// A <see cref="decimal"/> value equals <c>(-1)^Sign * Coefficient / 10^Scale</c>.
/// The scale ranges from 0 to 28. The coefficient is a 96-bit unsigned integer.
/// </para>
/// <para>
/// Implicit conversions to/from <see cref="decimal"/> allow seamless use:
/// <code>
/// DecimalBitFields d = 19.99m;
/// d.Sign; // false
/// d.Scale; // 2
/// d.Coefficient; // 1999
/// decimal value = d;
/// </code>
/// </para>
/// </remarks>
[BitFields(StorageType.Decimal, UndefinedBitsMustBe.Zeroes, Description = ".NET Decimal (128-bit)")]
public partial struct DecimalBitFields
{
/// <summary>96-bit unsigned integer coefficient.</summary>
[BitField(0, End = 95, Description = "96-bit unsigned integer coefficient (value before scaling)")] public partial UInt128 Coefficient { get; set; }
/// <summary>Scale factor (0-28). The value is divided by 10^Scale. The .NET format allocates 8 bits (bits 16-23 of the flags word) but only values 0-28 are valid.</summary>
[BitField(112, End = 119, Description = "Scale factor (0-28); value = Coefficient / 10^Scale")] public partial byte Scale { get; set; }
/// <summary>Sign bit. <c>true</c> = negative, <c>false</c> = positive.</summary>
[BitFlag(127, Description = "Sign bit: 1 = negative, 0 = positive")] public partial bool Sign { get; set; }
// ── Constants ───────────────────────────────────────────────
/// <summary>Maximum scale value for a .NET decimal (28).</summary>
public const int MAX_SCALE = 28;
}