-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.jai
222 lines (178 loc) · 4.42 KB
/
utils.jai
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
#scope_module
Abs :: (val : $T) -> T #expand
#modify { return IsNumeric(T), "T must be numeric."; }
{
if val < 0 then return -val;
return val;
}
Min :: (a : $T, b : T) -> T #expand
#modify { return IsNumeric(T), "T must be numeric."; }
{
return ifx a < b then a else b;
}
Max :: (a : $T, b : T) -> T #expand
#modify { return IsNumeric(T), "T must be numeric."; }
{
return ifx a > b then a else b;
}
Sign :: (val : $T) -> T #expand
#modify { return IsNumeric(T), "T must be numeric."; }
{
#if #run IsUnsigned(T)
return cast(T) 1;
else
return ifx val < 0 then cast(T) -1 else cast(T) 1;
}
Clamp :: (x : $T, min : T, max : T) -> T #expand
#modify { return IsNumeric(T), "T must be numeric."; }
{
if x > max then x = max;
if x < min then x = min;
return x;
}
Lerp :: inline (x : $T, y : T, t : $T2) -> T #must
#modify { return IsNumeric(T), "T must be numeric."; }
{
return x + cast(T) (t * (y - x));
}
Round :: inline (x : $T) -> T #must
#modify { return IsNumeric(T), "T must be numeric."; }
{
#if #run IsFloatingPoint(T)
{
return cast(T) (cast(int) (x + Sign(x) * 0.5));
}
else
{
return x;
}
}
Floor :: inline (x : $T) -> T #must
#modify { return IsNumeric(T), "T must be numeric."; }
{
#if #run IsFloatingPoint(T)
{
i := cast(int) Round(x);
diff := x - i;
return cast(T) (i - cast(int) (diff < 0));
}
else
{
return x;
}
}
Ceil :: inline (x : $T) -> T #must
#modify { return IsNumeric(T), "T must be numeric."; }
{
#if #run IsFloatingPoint(T)
{
i := cast(int) Round(x);
diff := i - x;
return cast(T) (i + cast(int) (diff < 0));
}
else
{
return x;
}
}
#scope_export
IsFinite :: inline (val : $T) -> bool #must
#modify { return IsFloatingPoint(T); }
{
nan, inf := math.is_nan_is_inf(val);
return !nan && !inf;
}
ApproxZero :: inline (val : $T, epsilon : $T2) -> bool #must
#modify {
return IsNumeric(T) && IsFloatingPoint(T2), "T must be numeric, T2 must be a float type.";
}
{
#if #run IsFloatingPoint(T)
return Abs(val) <= epsilon;
else
return val == 0;
}
ApproxEquals :: inline (left : $T, right : T, epsilon : $T2) -> bool #must
#modify {
return IsNumeric(T) && IsFloatingPoint(T2), "T must be numeric, T2 must be a float type.";
}
{
#if #run IsFloatingPoint(T)
return Abs(left - right) <= epsilon;
else
return left == right;
}
#scope_module
ToRads :: inline (angle_in_degrees : $T) -> T #must
#modify { return IsFloatingPoint(T), "T must be a float type."; }
{
return angle_in_degrees * PI / 180.0;
}
ToDegs :: inline (angle_in_radians : $T) -> T #must
#modify { return IsFloatingPoint(T), "T must be a float type."; }
{
return angle_in_radians * 180.0 / PI;
}
// Type traits
SharesSameSourcePolymorph :: inline (info : *Type_Info_Struct, of : *Type_Info_Struct) -> bool #must
{
while info && info.polymorph_source_struct
{
info = info.polymorph_source_struct;
}
while of && of.polymorph_source_struct
{
of = of.polymorph_source_struct;
}
return info && info == of;
}
Devariantize :: inline (info : *Type_Info) -> *Type_Info #must
{
if info.type == .VARIANT
info = (cast(*Type_Info_Variant) info).variant_of;
return info;
}
IsSigned :: inline (type : Type) -> bool #must
{
info := cast(*Type_Info) type;
info = Devariantize(info);
if info.type != .INTEGER && info.type != .FLOAT
return false;
if info.type == .INTEGER
return (cast(*Type_Info_Integer) info).signed;
return true;
}
IsUnsigned :: inline (type : Type) -> bool #must
{
info := cast(*Type_Info) type;
info = Devariantize(info);
if info.type != .INTEGER
return false;
return !(cast(*Type_Info_Integer) info).signed;
}
IsNumeric :: inline (type : Type) -> bool #must
{
info := cast(*Type_Info) type;
info = Devariantize(info);
if info.type != .FLOAT && info.type != .INTEGER
return false;
return true;
}
IsFloatingPoint :: inline (type : Type) -> bool #must
{
info := cast(*Type_Info) type;
info = Devariantize(info);
if info.type != .FLOAT
return false;
return true;
}
Basic :: #import "Basic";
assert :: Basic.assert;
String_Builder :: Basic.String_Builder;
to_string :: Basic.builder_to_string;
print_to_builder :: Basic.print_to_builder;
println_to_builder :: inline (builder : *String_Builder, fmt_str : string, args : ..Any)
{
print_to_builder(builder, fmt_str, ..args);
print_to_builder(builder, "\n");
}