Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
MicrosoftPlatformMath.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6#include "GenericPlatform/GenericPlatformMath.h"
7#include "Math/UnrealPlatformMathSSE4.h"
8
9/**
10* Microsoft base implementation of the Math OS functions
11**/
13{
15 static FORCEINLINE bool IsNaN( float A ) { return _isnan(A) != 0; }
16 static FORCEINLINE bool IsNaN(double A) { return _isnan(A) != 0; }
17 static FORCEINLINE bool IsFinite( float A ) { return _finite(A) != 0; }
18 static FORCEINLINE bool IsFinite(double A) { return _finite(A) != 0; }
19
20 #pragma intrinsic(_BitScanReverse)
21
22 static FORCEINLINE uint32 FloorLog2(uint32 Value)
23 {
24 // Use BSR to return the log2 of the integer
25 // return 0 if value is 0
26 unsigned long BitIndex;
27 return _BitScanReverse(&BitIndex, Value) ? BitIndex : 0;
28 }
29 static FORCEINLINE uint8 CountLeadingZeros8(uint8 Value)
30 {
31 unsigned long BitIndex;
32 _BitScanReverse(&BitIndex, uint32(Value)*2 + 1);
33 return uint8(8 - BitIndex);
34 }
35
36 static FORCEINLINE uint32 CountTrailingZeros(uint32 Value)
37 {
38 // return 32 if value was 0
39 unsigned long BitIndex; // 0-based, where the LSB is 0 and MSB is 31
40 return _BitScanForward( &BitIndex, Value ) ? BitIndex : 32;
41 }
42
43 static FORCEINLINE uint32 CeilLogTwo( uint32 Arg )
44 {
45 // if Arg is 0, change it to 1 so that we return 0
46 Arg = Arg ? Arg : 1;
47 return 32 - CountLeadingZeros(Arg - 1);
48 }
49
50 static FORCEINLINE uint32 RoundUpToPowerOfTwo(uint32 Arg)
51 {
52 return 1 << CeilLogTwo(Arg);
53 }
54
55 static FORCEINLINE uint64 RoundUpToPowerOfTwo64(uint64 Arg)
56 {
57 return uint64(1) << CeilLogTwo64(Arg);
58 }
59
61
62 #pragma intrinsic(_BitScanReverse64)
63
64 static FORCEINLINE uint64 FloorLog2_64(uint64 Value)
65 {
66 unsigned long BitIndex;
67 return _BitScanReverse64(&BitIndex, Value) ? BitIndex : 0;
68 }
69
70 static FORCEINLINE uint64 CeilLogTwo64(uint64 Arg)
71 {
72 // if Arg is 0, change it to 1 so that we return 0
73 Arg = Arg ? Arg : 1;
74 return 64 - CountLeadingZeros64(Arg - 1);
75 }
76
77 static FORCEINLINE uint64 CountLeadingZeros64(uint64 Value)
78 {
79 //https://godbolt.org/z/Ejh5G4vPK
80 // return 64 if value if was 0
81 unsigned long BitIndex;
82 if ( ! _BitScanReverse64(&BitIndex, Value) ) BitIndex = -1;
83 return 63 - BitIndex;
84 }
85
86 static FORCEINLINE uint64 CountTrailingZeros64(uint64 Value)
87 {
88 // return 64 if Value is 0
89 unsigned long BitIndex; // 0-based, where the LSB is 0 and MSB is 63
90 return _BitScanForward64( &BitIndex, Value ) ? BitIndex : 64;
91 }
92
93 static FORCEINLINE uint32 CountLeadingZeros(uint32 Value)
94 {
95 // return 32 if value is zero
96 unsigned long BitIndex;
97 _BitScanReverse64(&BitIndex, uint64(Value)*2 + 1);
98 return 32 - BitIndex;
99 }
100
101#else // 32-bit
102
104 {
105 // return 32 if value is zero
106 unsigned long BitIndex;
107 if ( ! _BitScanReverse(&BitIndex, Value) ) BitIndex = -1;
108 return 31 - BitIndex;
109 }
110
111#endif
112
115 {
116 return _mm_popcnt_u64(Bits);
117 }
118#endif
119
120#endif
121};
#define PLATFORM_ENABLE_VECTORINTRINSICS
Definition Platform.h:162
#define FORCEINLINE
Definition Platform.h:644
#define PLATFORM_ENABLE_POPCNT_INTRINSIC
Definition Platform.h:209
#define PLATFORM_64BITS