Repository navigation
Expand file tree
/
Copy pathLuaC.cs
More file actions
684 lines (613 loc) · 23.7 KB
/
Copy pathLuaC.cs
File metadata and controls
684 lines (613 loc) · 23.7 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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
// The C standard library routines the Lua sources lean on, plus the glue for
// treating UTF-8 string literals as `const char*`. Not part of Lua itself.
// See LICENSE_LUA.txt.
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace LinkBridge.Lua;
public static unsafe partial class LuaApi
{
// Takes a `const char*` to the bytes of a UTF-8 string literal. Literals of
// the form "..."u8 live in the assembly's data section, so the pointer
// stays valid for the life of the process and needs no pinning.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte* CSTR(ReadOnlySpan<byte> s)
{
return (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(s));
}
// Same, for the managed string constants of luaconf.h. The bytes are
// interned once per distinct string and never freed.
private static readonly System.Collections.Generic.Dictionary<string, IntPtr> _literals = new();
public static byte* CSTR(string s)
{
lock (_literals)
{
if (_literals.TryGetValue(s, out IntPtr p)) return (byte*)p;
int n = Encoding.UTF8.GetByteCount(s);
byte* mem = (byte*)NativeMemory.Alloc((nuint)n + 1);
fixed (char* src = s) Encoding.UTF8.GetBytes(src, s.Length, mem, n);
mem[n] = 0;
_literals[s] = (IntPtr)mem;
return mem;
}
}
// Decodes a null-terminated UTF-8 string into a managed string. Used only
// where the port has to cross back into the .NET world.
public static string CStr(byte* s)
{
if (s == null) return string.Empty;
return Encoding.UTF8.GetString(s, (int)strlen(s));
}
public static string CStr(byte* s, int length)
{
if (s == null || length <= 0) return string.Empty;
return Encoding.UTF8.GetString(s, length);
}
/*
** {==================================================================
** string.h / memory
** ===================================================================
*/
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static nuint strlen(byte* s)
{
if (s == null) return 0;
byte* p = s;
while (*p != 0) p++;
return (nuint)(p - s);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void memcpy(void* dst, void* src, nuint count)
{
Buffer.MemoryCopy(src, dst, count, count);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void memmove(void* dst, void* src, nuint count)
{
Buffer.MemoryCopy(src, dst, count, count);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void memset(void* dst, int value, nuint count)
{
Unsafe.InitBlockUnaligned(dst, (byte)value, (uint)count);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int memcmp(void* a, void* b, nuint count)
{
return new ReadOnlySpan<byte>(a, (int)count)
.SequenceCompareTo(new ReadOnlySpan<byte>(b, (int)count));
}
public static byte* memchr(byte* s, int c, nuint n)
{
int i = new ReadOnlySpan<byte>(s, (int)n).IndexOf((byte)c);
return i < 0 ? null : s + i;
}
public static int strcmp(byte* a, byte* b)
{
while (*a != 0 && *a == *b) { a++; b++; }
if (*a == *b) return 0;
return *a < *b ? -1 : 1;
}
// Lua only uses 'strcoll' for the default string comparison; the port has
// no locale support, so it is a plain byte comparison.
public static int strcoll(byte* a, byte* b) => strcmp(a, b);
public static byte* strcpy(byte* dst, byte* src)
{
byte* p = dst;
while ((*p++ = *src++) != 0) { }
return dst;
}
public static byte* strcat(byte* dst, byte* src)
{
strcpy(dst + strlen(dst), src);
return dst;
}
public static byte* strchr(byte* s, int c)
{
for (; *s != 0; s++)
if (*s == (byte)c) return s;
return c == 0 ? s : null;
}
public static byte* strpbrk(byte* s, byte* set)
{
for (; *s != 0; s++)
for (byte* p = set; *p != 0; p++)
if (*s == *p) return s;
return null;
}
public static nuint strspn(byte* s, byte* set)
{
byte* start = s;
for (; *s != 0; s++)
{
byte* p = set;
while (*p != 0 && *p != *s) p++;
if (*p == 0) break;
}
return (nuint)(s - start);
}
public static byte* strstr(byte* hay, byte* needle)
{
if (*needle == 0) return hay;
for (; *hay != 0; hay++)
{
byte* h = hay;
byte* n = needle;
while (*n != 0 && *h == *n) { h++; n++; }
if (*n == 0) return hay;
}
return null;
}
/*
** {==================================================================
** ctype.h (C locale only)
** ===================================================================
*/
public static int tolower(int c) => (c >= 'A' && c <= 'Z') ? c + ('a' - 'A') : c;
public static int toupper(int c) => (c >= 'a' && c <= 'z') ? c - ('a' - 'A') : c;
/*
** {==================================================================
** stdlib.h
** ===================================================================
*/
public static void* malloc(nuint size) => NativeMemory.Alloc(size);
public static void* realloc(void* p, nuint size) => NativeMemory.Realloc(p, size);
public static void free(void* p) => NativeMemory.Free(p);
public static int abs(int v) => v < 0 ? -v : v;
public static byte* getenv(string name)
{
string? v = Environment.GetEnvironmentVariable(name);
return v == null ? null : CSTR(v);
}
/*
** {==================================================================
** Number formatting. C's '%g'/'%d'/'%a' are reproduced here because the
** exact spelling of a number leaks into Lua programs ('tostring',
** 'string.format').
** ===================================================================
*/
// Writes 'src' into 'buff' (capacity 'sz', always NUL terminated) and
// returns the number of chars written, like 'snprintf' with a full result.
private static int putbuff(byte* buff, int sz, ReadOnlySpan<char> src)
{
int n = 0;
for (int i = 0; i < src.Length && n < sz - 1; i++)
buff[n++] = (byte)src[i];
buff[n] = 0;
return n;
}
// C's "%.<prec>g".
public static int fmt_g(byte* buff, int sz, double v, int prec, bool upper)
{
if (double.IsNaN(v))
return putbuff(buff, sz, double.IsNegative(v) ? "-nan" : "nan");
if (double.IsInfinity(v))
return putbuff(buff, sz, v < 0 ? "-inf" : "inf");
if (prec == 0) prec = 1;
// Round to 'prec' significant digits first; the exponent of the rounded
// value decides between the '%e' and '%f' forms.
string e = v.ToString("E" + (prec - 1).ToString(CultureInfo.InvariantCulture),
CultureInfo.InvariantCulture);
int epos = e.IndexOf('E');
int exp = int.Parse(e.AsSpan(epos + 1), NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture);
string s;
if (exp < -4 || exp >= prec)
{
string mant = trimzeros(e.AsSpan(0, epos));
s = mant + (upper ? "E" : "e") + (exp < 0 ? "-" : "+") +
Math.Abs(exp).ToString("00", CultureInfo.InvariantCulture);
}
else
{
int dec = prec - 1 - exp;
s = trimzeros(v.ToString("F" + dec.ToString(CultureInfo.InvariantCulture),
CultureInfo.InvariantCulture));
}
return putbuff(buff, sz, s);
}
// Drops the trailing zeros of a fractional part, and the '.' left alone.
private static string trimzeros(ReadOnlySpan<char> s)
{
if (s.IndexOf('.') < 0) return s.ToString();
int end = s.Length;
while (end > 0 && s[end - 1] == '0') end--;
if (end > 0 && s[end - 1] == '.') end--;
return s.Slice(0, end).ToString();
}
// C's "%.<prec>f"; a negative 'prec' means the default of 6.
public static int fmt_f(byte* buff, int sz, double v, int prec, bool upper)
{
if (double.IsNaN(v) || double.IsInfinity(v)) return fmt_g(buff, sz, v, 17, upper);
if (prec < 0) prec = 6;
return putbuff(buff, sz, v.ToString("F" + prec.ToString(CultureInfo.InvariantCulture),
CultureInfo.InvariantCulture));
}
// C's "%.<prec>e"; a negative 'prec' means the default of 6.
public static int fmt_e(byte* buff, int sz, double v, int prec, bool upper)
{
if (double.IsNaN(v) || double.IsInfinity(v)) return fmt_g(buff, sz, v, 17, upper);
if (prec < 0) prec = 6;
string e = v.ToString("E" + prec.ToString(CultureInfo.InvariantCulture),
CultureInfo.InvariantCulture);
int epos = e.IndexOf('E');
int exp = int.Parse(e.AsSpan(epos + 1), NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture);
string s = e.Substring(0, epos) + (upper ? "E" : "e") + (exp < 0 ? "-" : "+") +
Math.Abs(exp).ToString("00", CultureInfo.InvariantCulture);
return putbuff(buff, sz, s);
}
// C's "%a": a hexadecimal float. 'prec' < 0 means "as many digits as
// needed", which for a double is at most 13 after the point.
public static int fmt_a(byte* buff, int sz, double v, int prec, bool upper)
{
if (double.IsNaN(v) || double.IsInfinity(v)) return fmt_g(buff, sz, v, 17, upper);
ulong bits = (ulong)BitConverter.DoubleToInt64Bits(v);
bool neg = (bits >> 63) != 0;
int exp = (int)((bits >> 52) & 0x7ff);
ulong mant = bits & 0xfffffffffffffUL;
int lead;
if (exp == 0) // zero or subnormal
{
lead = 0;
exp = mant == 0 ? 0 : -1022;
}
else
{
lead = 1;
exp -= 1023;
}
var sb = new StringBuilder(32);
if (neg) sb.Append('-');
sb.Append(upper ? "0X" : "0x");
sb.Append((char)('0' + lead));
// The 52 mantissa bits are exactly 13 hex digits.
Span<char> digits = stackalloc char[13];
string hex = upper ? "0123456789ABCDEF" : "0123456789abcdef";
for (int i = 0; i < 13; i++)
digits[i] = hex[(int)((mant >> (48 - i * 4)) & 0xf)];
int nd = 13;
if (prec >= 0) nd = prec < 13 ? prec : 13;
else while (nd > 0 && digits[nd - 1] == '0') nd--;
if (nd > 0)
{
sb.Append('.');
sb.Append(digits.Slice(0, nd));
for (int i = nd; i < prec; i++) sb.Append('0');
}
sb.Append(upper ? 'P' : 'p');
sb.Append(exp < 0 ? '-' : '+');
sb.Append(Math.Abs(exp).ToString(CultureInfo.InvariantCulture));
return putbuff(buff, sz, sb.ToString());
}
public static int fmt_d(byte* buff, int sz, long v)
{
return putbuff(buff, sz, v.ToString(CultureInfo.InvariantCulture));
}
public static int fmt_u(byte* buff, int sz, ulong v)
{
return putbuff(buff, sz, v.ToString(CultureInfo.InvariantCulture));
}
public static int fmt_x(byte* buff, int sz, ulong v, bool upper)
{
return putbuff(buff, sz, v.ToString(upper ? "X" : "x", CultureInfo.InvariantCulture));
}
public static int fmt_o(byte* buff, int sz, ulong v)
{
return putbuff(buff, sz, Convert.ToString((long)v, 8));
}
public static int fmt_p(byte* buff, int sz, void* p)
{
return putbuff(buff, sz, "0x" + ((ulong)p).ToString("x", CultureInfo.InvariantCulture));
}
/*
** {==================================================================
** 'snprintf' for a single conversion item, which is all that
** 'string.format' needs ("%-+ #0<width>.<prec><conv>").
** ===================================================================
*/
private struct FmtSpec
{
public bool minus, plus, space, hash, zero;
public int width; // -1 if absent
public int prec; // -1 if absent
public byte conv;
}
private static FmtSpec parsespec(byte* form)
{
FmtSpec f = default;
f.width = -1;
f.prec = -1;
byte* p = form;
if (*p == '%') p++;
for (; ; p++)
{
if (*p == '-') f.minus = true;
else if (*p == '+') f.plus = true;
else if (*p == ' ') f.space = true;
else if (*p == '#') f.hash = true;
else if (*p == '0') f.zero = true;
else break;
}
if (*p >= '0' && *p <= '9')
{
int w = 0;
while (*p >= '0' && *p <= '9') w = w * 10 + (*p++ - '0');
f.width = w;
}
if (*p == '.')
{
p++;
int pr = 0;
while (*p >= '0' && *p <= '9') pr = pr * 10 + (*p++ - '0');
f.prec = pr;
}
// skip a length modifier, if any
while (*p == 'l' || *p == 'h' || *p == 'j' || *p == 'z' ||
*p == 't' || *p == 'L' || *p == 'I' || (*p >= '0' && *p <= '9'))
p++;
f.conv = *p;
return f;
}
// Applies precision, sign and width padding to the digits of a number.
private static int finishnum(byte* buff, int sz, in FmtSpec f, string digits,
bool neg, string prefix)
{
if (f.prec >= 0 && digits.Length < f.prec)
digits = digits.PadLeft(f.prec, '0');
string sign = neg ? "-" : f.plus ? "+" : f.space ? " " : "";
string body = sign + prefix + digits;
if (f.width > body.Length)
{
if (f.minus)
body = body.PadRight(f.width);
else if (f.zero && f.prec < 0)
body = sign + prefix + digits.PadLeft(f.width - sign.Length - prefix.Length, '0');
else
body = body.PadLeft(f.width);
}
return putbuff(buff, sz, body);
}
private static int finishstr(byte* buff, int sz, in FmtSpec f, string s)
{
if (f.prec >= 0 && s.Length > f.prec)
s = s.Substring(0, f.prec);
if (f.width > s.Length)
s = f.minus ? s.PadRight(f.width) : s.PadLeft(f.width);
return putbuff(buff, sz, s);
}
// '%d', '%i', '%u', '%o', '%x', '%X'
public static int l_sprintf(byte* buff, int sz, byte* form, lua_Integer v)
{
FmtSpec f = parsespec(form);
switch (f.conv)
{
case (byte)'d':
case (byte)'i':
{
bool neg = v < 0;
ulong m = neg ? (ulong)(-(v + 1)) + 1 : (ulong)v;
string digits = m.ToString(CultureInfo.InvariantCulture);
if (f.prec == 0 && m == 0) digits = "";
return finishnum(buff, sz, f, digits, neg, "");
}
case (byte)'u':
{
string digits = ((ulong)v).ToString(CultureInfo.InvariantCulture);
if (f.prec == 0 && v == 0) digits = "";
return finishnum(buff, sz, f, digits, false, "");
}
case (byte)'o':
{
string digits = Convert.ToString(v, 8);
if (f.prec == 0 && v == 0) digits = "";
string prefix = "";
if (f.hash && (digits.Length == 0 || digits[0] != '0') && f.prec <= digits.Length) prefix = "0";
return finishnum(buff, sz, f, digits, false, prefix);
}
case (byte)'x':
{
string digits = ((ulong)v).ToString("x", CultureInfo.InvariantCulture);
if (f.prec == 0 && v == 0) digits = "";
string prefix = "";
if (f.hash && v != 0) prefix = "0x";
return finishnum(buff, sz, f, digits, false, prefix);
}
case (byte)'X':
{
string digits = ((ulong)v).ToString("X", CultureInfo.InvariantCulture);
if (f.prec == 0 && v == 0) digits = "";
string prefix = "";
if (f.hash && v != 0) prefix = "0X";
return finishnum(buff, sz, f, digits, false, prefix);
}
case (byte)'c':
return finishstr(buff, sz, f, ((char)(byte)v).ToString());
default:
return finishnum(buff, sz, f, v.ToString(CultureInfo.InvariantCulture),
false, "");
}
}
// '%a', '%A', '%e', '%E', '%f', '%F', '%g', '%G'
public static int l_sprintf(byte* buff, int sz, byte* form, lua_Number v)
{
FmtSpec f = parsespec(form);
byte* tmp = stackalloc byte[MAX_ITEM_FMT];
int n;
bool neg = v < 0 || (v == 0 && double.IsNegative(v));
double av = neg ? -v : v;
bool upper = f.conv == 'E' || f.conv == 'G' || f.conv == 'A' || f.conv == 'F';
switch (f.conv)
{
case (byte)'f':
case (byte)'F':
n = fmt_f(tmp, MAX_ITEM_FMT, av, f.prec, upper);
break;
case (byte)'e':
case (byte)'E':
n = fmt_e(tmp, MAX_ITEM_FMT, av, f.prec, upper);
break;
case (byte)'a':
case (byte)'A':
n = fmt_a(tmp, MAX_ITEM_FMT, av, f.prec, upper);
break;
default: // 'g'/'G'
n = fmt_g(tmp, MAX_ITEM_FMT, av, f.prec < 0 ? 6 : (f.prec == 0 ? 1 : f.prec),
upper);
break;
}
string digits = CStr(tmp, n);
if (f.hash && !digits.Contains('.'))
{
int exponent = digits.IndexOfAny(['e', 'E', 'p', 'P']);
if (exponent < 0) digits += ".";
else digits = digits.Insert(exponent, ".");
}
// 'finishnum' would apply the precision again; the conversions above
// already did it, so pass a spec without precision
FmtSpec g = f;
g.prec = -1;
return finishnum(buff, sz, g, digits, neg, "");
}
// '%s'
public static int l_sprintf(byte* buff, int sz, byte* form, byte* s)
{
FmtSpec f = parsespec(form);
return finishstr(buff, sz, f, CStr(s));
}
// '%p'
public static int l_sprintf(byte* buff, int sz, byte* form, void* p)
{
FmtSpec f = parsespec(form);
return finishstr(buff, sz, f,
"0x" + ((ulong)p).ToString("x", CultureInfo.InvariantCulture));
}
private const int MAX_ITEM_FMT = 512;
/*
** {==================================================================
** Number scanning ('strtod' in the C locale, decimal and hexadecimal).
** ===================================================================
*/
// Converts the numeral prefix of 's' into a float. Returns the first char
// after the numeral in '*endptr' (which is 's' itself when there is none).
public static double strtod(byte* s, byte** endptr)
{
byte* p = s;
while (*p == ' ' || (*p >= 0x09 && *p <= 0x0d)) p++;
byte* num = p;
if (*p == '+' || *p == '-') p++;
if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
return strtox(s, num, p + 2, endptr);
bool any = false;
while (*p >= '0' && *p <= '9') { p++; any = true; }
if (*p == '.')
{
p++;
while (*p >= '0' && *p <= '9') { p++; any = true; }
}
if (!any) { if (endptr != null) *endptr = s; return 0.0; }
if (*p == 'e' || *p == 'E')
{
byte* save = p;
p++;
if (*p == '+' || *p == '-') p++;
if (*p >= '0' && *p <= '9') { while (*p >= '0' && *p <= '9') p++; }
else p = save;
}
if (endptr != null) *endptr = p;
int len = (int)(p - num);
Span<char> tmp = len <= 64 ? stackalloc char[len] : new char[len];
for (int i = 0; i < len; i++) tmp[i] = (char)num[i];
double.TryParse(tmp, NumberStyles.Float, CultureInfo.InvariantCulture, out double r);
return r;
}
// The hexadecimal case of 'strtod': [+-]0x<hex digits>[.<hex digits>][p<exp>]
private static double strtox(byte* s, byte* num, byte* p, byte** endptr)
{
bool neg = *num == '-';
double r = 0.0;
int sigdig = 0, nosigdig = 0, e = 0;
bool any = false;
for (; ; p++)
{
int d = hexvalue_or(-1, *p);
if (d < 0) break;
any = true;
if (sigdig == 0 && d == 0) nosigdig++;
else if (++sigdig <= 30) r = r * 16.0 + d;
else e++; // too many digits; ignore, but still count for exponent
}
if (*p == '.')
{
p++;
for (; ; p++)
{
int d = hexvalue_or(-1, *p);
if (d < 0) break;
any = true;
if (sigdig == 0 && d == 0) { nosigdig++; e--; }
else if (++sigdig <= 30) { r = r * 16.0 + d; e -= 1; }
}
}
if (nosigdig + sigdig == 0 || !any) { if (endptr != null) *endptr = s; return 0.0; }
e *= 4; // each digit multiplies/divides the value by 2^4
if (*p == 'p' || *p == 'P')
{
byte* save = p;
p++;
bool negexp = *p == '-';
if (*p == '+' || *p == '-') p++;
if (*p >= '0' && *p <= '9')
{
int exp1 = 0;
while (*p >= '0' && *p <= '9')
{
exp1 = exp1 * 10 + (*p - '0');
if (exp1 > 100000) exp1 = 100000;
p++;
}
e += negexp ? -exp1 : exp1;
}
else p = save; // no digits: the 'p' is not part of the numeral
}
if (endptr != null) *endptr = p;
r = ldexp(r, e);
return neg ? -r : r;
}
private static int hexvalue_or(int def, byte c)
{
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return def;
}
/*
** {==================================================================
** math.h
** ===================================================================
*/
public static double l_floor(double x) => Math.Floor(x);
public static double ceil(double x) => Math.Ceiling(x);
public static double fmod(double a, double b) => a % b;
public static double pow(double a, double b) => Math.Pow(a, b);
public static double sqrt(double x) => Math.Sqrt(x);
public static double exp(double x) => Math.Exp(x);
public static double log(double x) => Math.Log(x);
public static double log2(double x) => Math.Log2(x);
public static double log10(double x) => Math.Log10(x);
public static double sin(double x) => Math.Sin(x);
public static double cos(double x) => Math.Cos(x);
public static double tan(double x) => Math.Tan(x);
public static double asin(double x) => Math.Asin(x);
public static double acos(double x) => Math.Acos(x);
public static double atan2(double y, double x) => Math.Atan2(y, x);
public static double fabs(double x) => Math.Abs(x);
public static double ldexp(double x, int e) => Math.ScaleB(x, e);
public static double frexp(double x, int* e)
{
if (x == 0.0 || double.IsNaN(x) || double.IsInfinity(x)) { *e = 0; return x; }
int exp = (int)Math.ILogB(x) + 1;
*e = exp;
return Math.ScaleB(x, -exp);
}
}