Repository navigation
Expand file tree
/
Copy pathFastDecimal.cs
More file actions
291 lines (233 loc) · 10.3 KB
/
Copy pathFastDecimal.cs
File metadata and controls
291 lines (233 loc) · 10.3 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
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace FastDecimal
{
[StructLayout(LayoutKind.Sequential, Pack = 16)]
public unsafe struct RawDecimal
{
public ulong SigLo;
public ulong SigHi;
public short Exp;
public sbyte Sign;
public byte Tag;
public fixed byte Pad[12];
}
public readonly unsafe struct Decimal : IEquatable<Decimal>, IComparable<Decimal>
{
static Decimal()
{
NativeLibrary.SetDllImportResolver(typeof(Decimal).Assembly, (name, assembly, path) =>
{
if (name == LibName)
{
string[] candidates = {
"fastdecimal",
"libfastdecimal.so",
"libfastdecimal.dylib",
"build/libfastdecimal.so",
"build/libfastdecimal.dylib",
"../build/libfastdecimal.so",
"../build/libfastdecimal.dylib",
"../../build/libfastdecimal.so",
"../../build/libfastdecimal.dylib"
};
foreach (var c in candidates)
{
if (NativeLibrary.TryLoad(c, out IntPtr handle))
return handle;
}
}
return IntPtr.Zero;
});
}
private const string LibName = "fastdecimal";
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern void fd_from_i64(long v, out RawDecimal result);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern void fd_from_u64(ulong v, out RawDecimal result);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern void fd_from_double(double v, out RawDecimal result);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern void fd_from_str(byte* s, nuint len, out RawDecimal result);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern void fd_nan(out RawDecimal result);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern void fd_inf(int negative, out RawDecimal result);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern void fd_add(in RawDecimal a, in RawDecimal b, out RawDecimal result);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern void fd_sub(in RawDecimal a, in RawDecimal b, out RawDecimal result);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern void fd_mul(in RawDecimal a, in RawDecimal b, out RawDecimal result);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern void fd_div(in RawDecimal a, in RawDecimal b, out RawDecimal result);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern void fd_neg(in RawDecimal a, out RawDecimal result);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern void fd_abs(in RawDecimal a, out RawDecimal result);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern void fd_sqrt(in RawDecimal a, out RawDecimal result);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern void fd_pow_i64(in RawDecimal a, long n, out RawDecimal result);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern void fd_round(in RawDecimal a, int places, out RawDecimal result);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern void fd_trunc(in RawDecimal a, out RawDecimal result);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern void fd_floor(in RawDecimal a, out RawDecimal result);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern void fd_ceil(in RawDecimal a, out RawDecimal result);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern int fd_cmp(in RawDecimal a, in RawDecimal b);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern int fd_eq(in RawDecimal a, in RawDecimal b);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern long fd_to_i64(in RawDecimal a);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern double fd_to_double(in RawDecimal a);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern int fd_to_str(in RawDecimal a, byte* buf, nuint buflen);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern int fd_is_nan(in RawDecimal a);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern int fd_is_inf(in RawDecimal a);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern int fd_is_zero(in RawDecimal a);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern int fd_is_neg(in RawDecimal a);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
private static extern int fd_is_finite(in RawDecimal a);
public readonly RawDecimal Raw;
public Decimal(in RawDecimal raw) => Raw = raw;
public Decimal(long v)
{
fd_from_i64(v, out Raw);
}
public Decimal(ulong v)
{
fd_from_u64(v, out Raw);
}
public Decimal(double v)
{
fd_from_double(v, out Raw);
}
public static Decimal Parse(string s)
{
if (s == null) throw new ArgumentNullException(nameof(s));
byte[] bytes = Encoding.UTF8.GetBytes(s);
fixed (byte* p = bytes)
{
fd_from_str(p, (nuint)bytes.Length, out RawDecimal raw);
return new Decimal(in raw);
}
}
public static Decimal NaN
{
get
{
fd_nan(out RawDecimal raw);
return new Decimal(in raw);
}
}
public static Decimal PositiveInfinity
{
get
{
fd_inf(0, out RawDecimal raw);
return new Decimal(in raw);
}
}
public static Decimal NegativeInfinity
{
get
{
fd_inf(1, out RawDecimal raw);
return new Decimal(in raw);
}
}
public bool IsNaN => fd_is_nan(in Raw) != 0;
public bool IsInfinity => fd_is_inf(in Raw) != 0;
public bool IsZero => fd_is_zero(in Raw) != 0;
public bool IsNegative => fd_is_neg(in Raw) != 0;
public bool IsFinite => fd_is_finite(in Raw) != 0;
public static Decimal operator +(in Decimal a, in Decimal b)
{
fd_add(in a.Raw, in b.Raw, out RawDecimal res);
return new Decimal(in res);
}
public static Decimal operator -(in Decimal a, in Decimal b)
{
fd_sub(in a.Raw, in b.Raw, out RawDecimal res);
return new Decimal(in res);
}
public static Decimal operator *(in Decimal a, in Decimal b)
{
fd_mul(in a.Raw, in b.Raw, out RawDecimal res);
return new Decimal(in res);
}
public static Decimal operator /(in Decimal a, in Decimal b)
{
fd_div(in a.Raw, in b.Raw, out RawDecimal res);
return new Decimal(in res);
}
public static Decimal operator -(in Decimal a)
{
fd_neg(in a.Raw, out RawDecimal res);
return new Decimal(in res);
}
public Decimal Abs()
{
fd_abs(in Raw, out RawDecimal res);
return new Decimal(in res);
}
public Decimal Sqrt()
{
fd_sqrt(in Raw, out RawDecimal res);
return new Decimal(in res);
}
public Decimal Pow(long n)
{
fd_pow_i64(in Raw, n, out RawDecimal res);
return new Decimal(in res);
}
public Decimal Round(int places)
{
fd_round(in Raw, places, out RawDecimal res);
return new Decimal(in res);
}
public Decimal Trunc()
{
fd_trunc(in Raw, out RawDecimal res);
return new Decimal(in res);
}
public Decimal Floor()
{
fd_floor(in Raw, out RawDecimal res);
return new Decimal(in res);
}
public Decimal Ceil()
{
fd_ceil(in Raw, out RawDecimal res);
return new Decimal(in res);
}
public long ToInt64() => fd_to_i64(in Raw);
public double ToDouble() => fd_to_double(in Raw);
public bool Equals(Decimal other) => fd_eq(in Raw, in other.Raw) != 0;
public override bool Equals(object obj) => obj is Decimal d && Equals(d);
public override int GetHashCode() => HashCode.Combine(Raw.SigLo, Raw.SigHi, Raw.Exp, Raw.Sign);
public int CompareTo(Decimal other) => fd_cmp(in Raw, in other.Raw);
public static bool operator ==(in Decimal a, in Decimal b) => a.Equals(b);
public static bool operator !=(in Decimal a, in Decimal b) => !a.Equals(b);
public static bool operator <(in Decimal a, in Decimal b) => a.CompareTo(b) < 0;
public static bool operator >(in Decimal a, in Decimal b) => a.CompareTo(b) > 0;
public static bool operator <=(in Decimal a, in Decimal b) => a.CompareTo(b) <= 0;
public static bool operator >=(in Decimal a, in Decimal b) => a.CompareTo(b) >= 0;
public override string ToString()
{
byte* buf = stackalloc byte[64];
int len = fd_to_str(in Raw, buf, 64);
return len > 0 ? Encoding.UTF8.GetString(buf, len) : "0";
}
}
}