Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
MathF.cs
Go to the documentation of this file.
6
7namespace System;
8
9public static class MathF
10{
11 public const float E = 2.7182817f;
12
13 public const float PI = 3.1415927f;
14
15 public const float Tau = (float)Math.PI * 2f;
16
17 private static readonly float[] roundPower10Single = new float[7] { 1f, 10f, 100f, 1000f, 10000f, 100000f, 1000000f };
18
19 [MethodImpl(MethodImplOptions.InternalCall)]
20 [Intrinsic]
21 public static extern float Acos(float x);
22
23 [MethodImpl(MethodImplOptions.InternalCall)]
24 [Intrinsic]
25 public static extern float Acosh(float x);
26
27 [MethodImpl(MethodImplOptions.InternalCall)]
28 [Intrinsic]
29 public static extern float Asin(float x);
30
31 [MethodImpl(MethodImplOptions.InternalCall)]
32 [Intrinsic]
33 public static extern float Asinh(float x);
34
35 [MethodImpl(MethodImplOptions.InternalCall)]
36 [Intrinsic]
37 public static extern float Atan(float x);
38
39 [MethodImpl(MethodImplOptions.InternalCall)]
40 [Intrinsic]
41 public static extern float Atanh(float x);
42
43 [MethodImpl(MethodImplOptions.InternalCall)]
44 [Intrinsic]
45 public static extern float Atan2(float y, float x);
46
47 [MethodImpl(MethodImplOptions.InternalCall)]
48 [Intrinsic]
49 public static extern float Cbrt(float x);
50
51 [MethodImpl(MethodImplOptions.InternalCall)]
52 [Intrinsic]
53 public static extern float Ceiling(float x);
54
55 [MethodImpl(MethodImplOptions.InternalCall)]
56 [Intrinsic]
57 public static extern float Cos(float x);
58
59 [MethodImpl(MethodImplOptions.InternalCall)]
60 [Intrinsic]
61 public static extern float Cosh(float x);
62
63 [MethodImpl(MethodImplOptions.InternalCall)]
64 [Intrinsic]
65 public static extern float Exp(float x);
66
67 [MethodImpl(MethodImplOptions.InternalCall)]
68 [Intrinsic]
69 public static extern float Floor(float x);
70
71 [MethodImpl(MethodImplOptions.InternalCall)]
72 [Intrinsic]
73 public static extern float FusedMultiplyAdd(float x, float y, float z);
74
75 [MethodImpl(MethodImplOptions.InternalCall)]
76 [Intrinsic]
77 public static extern int ILogB(float x);
78
79 [MethodImpl(MethodImplOptions.InternalCall)]
80 [Intrinsic]
81 public static extern float Log(float x);
82
83 [MethodImpl(MethodImplOptions.InternalCall)]
84 [Intrinsic]
85 public static extern float Log2(float x);
86
87 [MethodImpl(MethodImplOptions.InternalCall)]
88 [Intrinsic]
89 public static extern float Log10(float x);
90
91 [MethodImpl(MethodImplOptions.InternalCall)]
92 [Intrinsic]
93 public static extern float Pow(float x, float y);
94
95 [MethodImpl(MethodImplOptions.InternalCall)]
96 [Intrinsic]
97 public static extern float Sin(float x);
98
99 public unsafe static (float Sin, float Cos) SinCos(float x)
100 {
101 System.Runtime.CompilerServices.Unsafe.SkipInit(out float item);
102 System.Runtime.CompilerServices.Unsafe.SkipInit(out float item2);
103 SinCos(x, &item, &item2);
104 return (Sin: item, Cos: item2);
105 }
106
107 [MethodImpl(MethodImplOptions.InternalCall)]
108 [Intrinsic]
109 public static extern float Sinh(float x);
110
111 [MethodImpl(MethodImplOptions.InternalCall)]
112 [Intrinsic]
113 public static extern float Sqrt(float x);
114
115 [MethodImpl(MethodImplOptions.InternalCall)]
116 [Intrinsic]
117 public static extern float Tan(float x);
118
119 [MethodImpl(MethodImplOptions.InternalCall)]
120 [Intrinsic]
121 public static extern float Tanh(float x);
122
123 [MethodImpl(MethodImplOptions.InternalCall)]
124 private unsafe static extern float ModF(float x, float* intptr);
125
126 [MethodImpl(MethodImplOptions.InternalCall)]
127 private unsafe static extern void SinCos(float x, float* sin, float* cos);
128
129 [MethodImpl(MethodImplOptions.AggressiveInlining)]
130 public static float Abs(float x)
131 {
132 return Math.Abs(x);
133 }
134
135 public static float BitDecrement(float x)
136 {
137 int num = BitConverter.SingleToInt32Bits(x);
138 if ((num & 0x7F800000) >= 2139095040)
139 {
140 if (num != 2139095040)
141 {
142 return x;
143 }
144 return float.MaxValue;
145 }
146 if (num == 0)
147 {
148 return -1E-45f;
149 }
150 num += ((num < 0) ? 1 : (-1));
152 }
153
154 public static float BitIncrement(float x)
155 {
156 int num = BitConverter.SingleToInt32Bits(x);
157 if ((num & 0x7F800000) >= 2139095040)
158 {
159 if (num != -8388608)
160 {
161 return x;
162 }
163 return float.MinValue;
164 }
165 if (num == int.MinValue)
166 {
167 return float.Epsilon;
168 }
169 num += ((num >= 0) ? 1 : (-1));
171 }
172
173 [MethodImpl(MethodImplOptions.AggressiveInlining)]
174 public static float CopySign(float x, float y)
175 {
177 {
179 }
180 return SoftwareFallback(x, y);
181 static float SoftwareFallback(float x, float y)
182 {
183 int num = BitConverter.SingleToInt32Bits(x);
184 int num2 = BitConverter.SingleToInt32Bits(y);
185 num &= 0x7FFFFFFF;
186 num2 &= int.MinValue;
187 return BitConverter.Int32BitsToSingle(num | num2);
188 }
189 }
190
191 public static float IEEERemainder(float x, float y)
192 {
193 if (float.IsNaN(x))
194 {
195 return x;
196 }
197 if (float.IsNaN(y))
198 {
199 return y;
200 }
201 float num = x % y;
202 if (float.IsNaN(num))
203 {
204 return float.NaN;
205 }
206 if (num == 0f && float.IsNegative(x))
207 {
208 return -0f;
209 }
210 float num2 = num - Abs(y) * (float)Sign(x);
211 if (Abs(num2) == Abs(num))
212 {
213 float x2 = x / y;
214 float x3 = Round(x2);
215 if (Abs(x3) > Abs(x2))
216 {
217 return num2;
218 }
219 return num;
220 }
221 if (Abs(num2) < Abs(num))
222 {
223 return num2;
224 }
225 return num;
226 }
227
228 public static float Log(float x, float y)
229 {
230 if (float.IsNaN(x))
231 {
232 return x;
233 }
234 if (float.IsNaN(y))
235 {
236 return y;
237 }
238 if (y == 1f)
239 {
240 return float.NaN;
241 }
242 if (x != 1f && (y == 0f || float.IsPositiveInfinity(y)))
243 {
244 return float.NaN;
245 }
246 return Log(x) / Log(y);
247 }
248
249 [MethodImpl(MethodImplOptions.AggressiveInlining)]
250 public static float Max(float x, float y)
251 {
252 return Math.Max(x, y);
253 }
254
255 public static float MaxMagnitude(float x, float y)
256 {
257 float num = Abs(x);
258 float num2 = Abs(y);
259 if (num > num2 || float.IsNaN(num))
260 {
261 return x;
262 }
263 if (num == num2)
264 {
265 if (!float.IsNegative(x))
266 {
267 return x;
268 }
269 return y;
270 }
271 return y;
272 }
273
274 [MethodImpl(MethodImplOptions.AggressiveInlining)]
275 public static float Min(float x, float y)
276 {
277 return Math.Min(x, y);
278 }
279
280 public static float MinMagnitude(float x, float y)
281 {
282 float num = Abs(x);
283 float num2 = Abs(y);
284 if (num < num2 || float.IsNaN(num))
285 {
286 return x;
287 }
288 if (num == num2)
289 {
290 if (!float.IsNegative(x))
291 {
292 return y;
293 }
294 return x;
295 }
296 return y;
297 }
298
299 [MethodImpl(MethodImplOptions.AggressiveInlining)]
300 public static float ReciprocalEstimate(float x)
301 {
302 if (Sse.IsSupported)
303 {
304 return Sse.ReciprocalScalar(Vector128.CreateScalarUnsafe(x)).ToScalar();
305 }
307 {
308 }
309 return 1f / x;
310 }
311
312 [MethodImpl(MethodImplOptions.AggressiveInlining)]
313 public static float ReciprocalSqrtEstimate(float x)
314 {
315 if (Sse.IsSupported)
316 {
318 }
320 {
321 }
322 return 1f / Sqrt(x);
323 }
324
325 [Intrinsic]
326 public static float Round(float x)
327 {
328 uint num = BitConverter.SingleToUInt32Bits(x);
329 int num2 = float.ExtractExponentFromBits(num);
330 if (num2 <= 126)
331 {
332 if (num << 1 == 0)
333 {
334 return x;
335 }
336 float x2 = ((num2 == 126 && float.ExtractSignificandFromBits(num) != 0) ? 1f : 0f);
337 return CopySign(x2, x);
338 }
339 if (num2 >= 150)
340 {
341 return x;
342 }
343 uint num3 = (uint)(1 << 150 - num2);
344 uint num4 = num3 - 1;
345 num += num3 >> 1;
346 num = (((num & num4) != 0) ? (num & ~num4) : (num & ~num3));
348 }
349
350 [MethodImpl(MethodImplOptions.AggressiveInlining)]
351 public static float Round(float x, int digits)
352 {
353 return Round(x, digits, MidpointRounding.ToEven);
354 }
355
356 [MethodImpl(MethodImplOptions.AggressiveInlining)]
357 public static float Round(float x, MidpointRounding mode)
358 {
359 return Round(x, 0, mode);
360 }
361
362 public unsafe static float Round(float x, int digits, MidpointRounding mode)
363 {
364 if (digits < 0 || digits > 6)
365 {
367 }
368 if (mode < MidpointRounding.ToEven || mode > MidpointRounding.ToPositiveInfinity)
369 {
370 throw new ArgumentException(SR.Format(SR.Argument_InvalidEnumValue, mode, "MidpointRounding"), "mode");
371 }
372 if (Abs(x) < 100000000f)
373 {
374 float num = roundPower10Single[digits];
375 x *= num;
376 switch (mode)
377 {
378 case MidpointRounding.ToEven:
379 x = Round(x);
380 break;
381 case MidpointRounding.AwayFromZero:
382 {
383 float x2 = ModF(x, &x);
384 if (Abs(x2) >= 0.5f)
385 {
386 x += (float)Sign(x2);
387 }
388 break;
389 }
390 case MidpointRounding.ToZero:
391 x = Truncate(x);
392 break;
393 case MidpointRounding.ToNegativeInfinity:
394 x = Floor(x);
395 break;
396 case MidpointRounding.ToPositiveInfinity:
397 x = Ceiling(x);
398 break;
399 default:
400 throw new ArgumentException(SR.Format(SR.Argument_InvalidEnumValue, mode, "MidpointRounding"), "mode");
401 }
402 x /= num;
403 }
404 return x;
405 }
406
407 [MethodImpl(MethodImplOptions.AggressiveInlining)]
408 public static int Sign(float x)
409 {
410 return Math.Sign(x);
411 }
412
413 public unsafe static float Truncate(float x)
414 {
415 ModF(x, &x);
416 return x;
417 }
418
419 public static float ScaleB(float x, int n)
420 {
421 float num = x;
422 if (n > 127)
423 {
424 num *= 1.7014118E+38f;
425 n -= 127;
426 if (n > 127)
427 {
428 num *= 1.7014118E+38f;
429 n -= 127;
430 if (n > 127)
431 {
432 n = 127;
433 }
434 }
435 }
436 else if (n < -126)
437 {
438 num *= 1.9721523E-31f;
439 n += 102;
440 if (n < -126)
441 {
442 num *= 1.9721523E-31f;
443 n += 102;
444 if (n < -126)
445 {
446 n = -126;
447 }
448 }
449 }
450 float num2 = BitConverter.Int32BitsToSingle(127 + n << 23);
451 return num * num2;
452 }
453}
static unsafe float Int32BitsToSingle(int value)
static unsafe int SingleToInt32Bits(float value)
static uint SingleToUInt32Bits(float value)
static float UInt32BitsToSingle(uint value)
static unsafe void SinCos(float x, float *sin, float *cos)
static float Abs(float x)
Definition MathF.cs:130
static float Log(float x)
const float E
Definition MathF.cs:11
static unsafe float Truncate(float x)
Definition MathF.cs:413
const float Tau
Definition MathF.cs:15
static float ReciprocalEstimate(float x)
Definition MathF.cs:300
static int Sign(float x)
Definition MathF.cs:408
static float MinMagnitude(float x, float y)
Definition MathF.cs:280
static float ScaleB(float x, int n)
Definition MathF.cs:419
static float Log2(float x)
static float Max(float x, float y)
Definition MathF.cs:250
static float Atan(float x)
static float Acosh(float x)
static float FusedMultiplyAdd(float x, float y, float z)
static float Atan2(float y, float x)
static float Sqrt(float x)
static float BitIncrement(float x)
Definition MathF.cs:154
static unsafe float Round(float x, int digits, MidpointRounding mode)
Definition MathF.cs:362
static float MaxMagnitude(float x, float y)
Definition MathF.cs:255
static float Pow(float x, float y)
static float Min(float x, float y)
Definition MathF.cs:275
static float Log10(float x)
static float Round(float x, int digits)
Definition MathF.cs:351
static float Asinh(float x)
static float Acos(float x)
const float PI
Definition MathF.cs:13
static float Cos(float x)
static float Sinh(float x)
static float ReciprocalSqrtEstimate(float x)
Definition MathF.cs:313
static float Asin(float x)
static float Round(float x, MidpointRounding mode)
Definition MathF.cs:357
static float Atanh(float x)
static float Cbrt(float x)
static float Cosh(float x)
static float IEEERemainder(float x, float y)
Definition MathF.cs:191
static float BitDecrement(float x)
Definition MathF.cs:135
static unsafe float ModF(float x, float *intptr)
static float CopySign(float x, float y)
Definition MathF.cs:174
static unsafe(float Sin, float Cos) SinCos(float x)
Definition MathF.cs:99
static float Log(float x, float y)
Definition MathF.cs:228
static float Ceiling(float x)
static float Sin(float x)
static float Floor(float x)
static float Exp(float x)
static float Tan(float x)
static float Tanh(float x)
static readonly float[] roundPower10Single
Definition MathF.cs:17
static int ILogB(float x)
static float Round(float x)
Definition MathF.cs:326
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static double Abs(double value)
const double PI
Definition Math.cs:16
static int Sign(decimal value)
Definition Math.cs:1202
static byte Max(byte val1, byte val2)
Definition Math.cs:738
static Vector128< float > ConditionalSelectBitwise(Vector128< float > selector, Vector128< float > ifTrue, Vector128< float > ifFalse)
Definition VectorMath.cs:11
static unsafe Vector128< byte > CreateScalarUnsafe(byte value)
Definition Vector128.cs:829
static new bool IsSupported
Definition Sse.cs:30
static Vector128< float > ReciprocalScalar(Vector128< float > value)
Definition Sse.cs:362
static Vector128< float > ReciprocalSqrtScalar(Vector128< float > value)
Definition Sse.cs:377
static string ArgumentOutOfRange_RoundingDigits_MathF
Definition SR.cs:1100
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Argument_InvalidEnumValue
Definition SR.cs:18
Definition SR.cs:7