-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmathx.logic.common.cs
37 lines (26 loc) · 1.07 KB
/
mathx.logic.common.cs
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
#region Header
// ** Copyright (C) 2023 Nicolas Reinhard, @LTMX. All rights reserved.
// ** Github Profile: https://github.com/LTMX
// ** Repository : https://github.com/LTMX/Unity.Athena
#endregion
using System;
using MI = System.Runtime.CompilerServices.MethodImplAttribute;
namespace Unity.Mathematics
{
public static partial class mathx
{
[MI(IL)] public static bool ispow2(this short x) => x != 0 && (x & (x - 1)) == 0;
[MI(IL)] public static bool ispow2(this ushort x) => x != 0 && (x & (x - 1)) == 0;
[MI(IL)] public static bool ispow2(this int x) => x != 0 && (x & (x - 1)) == 0;
[MI(IL)] public static bool ispow2(this uint x) => x != 0 && (x & (x - 1)) == 0;
[MI(IL)]
public static bool ispow2(this float x)
{
if (x <= 0) return false;
var bits = BitConverter.SingleToInt32Bits(x);
var exponent = (bits >> 23) & 0xFF;
var mantissa = bits & 0x007FFFFF;
return mantissa == 0 && exponent != 0 && exponent != 0xFF;
}
}
}