Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/VM/src/lnumutils.h
2725 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
// This code is based on Lua 5.x implementation licensed under MIT License; see lua_LICENSE.txt for details
3
#pragma once
4
5
#include <math.h>
6
#include <cstdint>
7
8
#define luai_numadd(a, b) ((a) + (b))
9
#define luai_numsub(a, b) ((a) - (b))
10
#define luai_nummul(a, b) ((a) * (b))
11
#define luai_numdiv(a, b) ((a) / (b))
12
#define luai_numpow(a, b) (pow(a, b))
13
#define luai_numunm(a) (-(a))
14
#define luai_numisnan(a) ((a) != (a))
15
#define luai_numeq(a, b) ((a) == (b))
16
#define luai_numlt(a, b) ((a) < (b))
17
#define luai_numle(a, b) ((a) <= (b))
18
#define luai_inteq(a, b) ((a) == (b))
19
20
inline bool luai_veceq(const float* a, const float* b)
21
{
22
#if LUA_VECTOR_SIZE == 4
23
return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3];
24
#else
25
return a[0] == b[0] && a[1] == b[1] && a[2] == b[2];
26
#endif
27
}
28
29
inline bool luai_vecisnan(const float* a)
30
{
31
#if LUA_VECTOR_SIZE == 4
32
return a[0] != a[0] || a[1] != a[1] || a[2] != a[2] || a[3] != a[3];
33
#else
34
return a[0] != a[0] || a[1] != a[1] || a[2] != a[2];
35
#endif
36
}
37
38
inline float luaui_signf(float v)
39
{
40
return v > 0.0f ? 1.0f : v < 0.0f ? -1.0f : 0.0f;
41
}
42
43
inline float luaui_clampf(float v, float min, float max)
44
{
45
float r = v < min ? min : v;
46
return r > max ? max : r;
47
}
48
49
LUAU_FASTMATH_BEGIN
50
inline double luai_nummod(double a, double b)
51
{
52
return a - floor(a / b) * b;
53
}
54
LUAU_FASTMATH_END
55
56
LUAU_FASTMATH_BEGIN
57
inline double luai_numidiv(double a, double b)
58
{
59
return floor(a / b);
60
}
61
LUAU_FASTMATH_END
62
63
inline float luai_lerpf(float a, float b, float t)
64
{
65
return (t == 1.0) ? b : a + (b - a) * t;
66
}
67
68
#define luai_num2int(i, d) ((i) = (int)(d))
69
70
#define luai_num2long(i, d) ((i) = (int64_t)(d))
71
72
// On MSVC in 32-bit, double to unsigned cast compiles into a call to __dtoui3, so we invoke x87->int64 conversion path manually
73
#if defined(_MSC_VER) && defined(_M_IX86)
74
#define luai_num2unsigned(i, n) \
75
{ \
76
__int64 l; \
77
__asm { __asm fld n __asm fistp l} \
78
; \
79
i = (unsigned int)l; \
80
}
81
#else
82
#define luai_num2unsigned(i, n) ((i) = (unsigned)(long long)(n))
83
#endif
84
85
#define LUAI_MAXNUM2STR 48
86
#define LUAI_MAXINT2STR 30
87
88
LUAI_FUNC char* luai_num2str(char* buf, double n);
89
LUAI_FUNC char* luai_int2str(char* buf, int64_t n);
90
91
#define luai_str2num(s, p) strtod((s), (p))
92
#define luai_str2long(s, p, base) strtoll((s), (p), base)
93
94